Refactor jobs to be MultiDB aware (#3174)

This commit is contained in:
David Bomba 2019-12-27 11:28:36 +11:00 committed by GitHub
parent 8eb3c75eb4
commit 54fc78a88b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 201 additions and 302 deletions

View File

@ -320,7 +320,7 @@ class CreateTestData extends Command
$this->invoice_repo->markSent($invoice);
CreateInvoiceInvitations::dispatch($invoice);
CreateInvoiceInvitations::dispatch($invoice, $invoice->company);
if(rand(0, 1)) {
@ -337,7 +337,7 @@ class CreateTestData extends Command
event(new PaymentWasCreated($payment));
UpdateInvoicePayment::dispatchNow($payment);
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
}
}
@ -380,7 +380,7 @@ class CreateTestData extends Command
$quote->save();
CreateQuoteInvitations::dispatch($quote);
CreateQuoteInvitations::dispatch($quote, $quote->company);
}

View File

@ -11,6 +11,7 @@
namespace App\Events\Payment;
use App\Models\Company;
use App\Models\Payment;
use Illuminate\Queue\SerializesModels;
@ -26,13 +27,15 @@ class PaymentWasCreated
*/
public $payment;
public $company;
/**
* Create a new event instance.
*
* @param Payment $payment
*/
public function __construct(Payment $payment)
public function __construct(Payment $payment, Company $company)
{
$this->payment = $payment;
$this->company = $company;
}
}

View File

@ -505,36 +505,19 @@ class ClientController extends BaseController
* ),
* )
*/
public function bulk(BulkClientRequest $request)
public function bulk()
{
$action = $request->action;
$ids = [];
if ($request->action !== self::$STORE_METHOD) {
$ids = $request->ids;
$clients = Client::withTrashed()->find($this->transformKeys($ids));
$clients->each(function ($client, $key) use ($request, $action) {
if (auth()->user()->can($request->action, $client))
$this->client_repo->{$action}($client);
});
}
if($request->action == self::$STORE_METHOD) {
/** Small hunks of data (originally 100) */
$chunks = array_chunk($request->clients, self::$CHUNK_SIZE);
foreach($chunks as $data) {
dispatch(new ProcessBulk($data, $this->client_repo, self::$STORE_METHOD))->onQueue(self::$DEFAULT_QUEUE);
}
}
$action = request()->input('action');
$ids = request()->input('ids');
$clients = Client::withTrashed()->find($this->transformKeys($ids));
$clients->each(function ($client, $key) use($action){
if(auth()->user()->can('edit', $client))
$this->client_repo->{$action}($client);
});
return $this->listResponse(Client::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}

View File

@ -214,7 +214,7 @@ class InvoiceController extends BaseController
$invoice = $this->invoice_repo->save($request->all(), InvoiceFactory::create(auth()->user()->company()->id, auth()->user()->id));
$invoice = StoreInvoice::dispatchNow($invoice, $request->all()); //todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI
$invoice = StoreInvoice::dispatchNow($invoice, $request->all(), $invoice->company); //todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI
event(new InvoiceWasCreated($invoice));
@ -396,7 +396,7 @@ class InvoiceController extends BaseController
$invoice = $this->invoice_repo->save($request->all(), $invoice);
event(new InvoiceWasUpdated($invoice));
event(new InvoiceWasUpdated($invoice, $invoice->company));
return $this->itemResponse($invoice);
@ -634,7 +634,7 @@ class InvoiceController extends BaseController
if($invoice->balance <= 0 || $invoice->status_id == Invoice::STATUS_PAID)
return $this->errorResponse(['message' => 'Invoice has no balance owing'], 400);
$invoice = MarkInvoicePaid::dispatchNow($invoice);
$invoice = MarkInvoicePaid::dispatchNow($invoice, $invoice->company);
if(!$bulk)
return $this->itemResponse($invoice);
@ -661,7 +661,7 @@ class InvoiceController extends BaseController
return $this->listResponse($invoice);
break;
case 'email':
EmailInvoice::dispatch($invoice);
EmailInvoice::dispatch($invoice, $invoice->company);
if(!$bulk)
return response()->json(['message'=>'email sent'],200);
break;

View File

@ -484,7 +484,7 @@ class PaymentController extends BaseController
public function destroy(DestroyPaymentRequest $request, Payment $payment)
{
ReverseInvoicePayment::dispatchNow($payment);
ReverseInvoicePayment::dispatchNow($payment, $payment->company);
$payment->is_deleted = true;
$payment->save();

View File

@ -1,59 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Client;
use App\Models\Client;
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class StoreClient
{
use Dispatchable;
protected $data;
protected $client;
/**
* Create a new job instance.
*
* @param array $data
* @param Client $client
*/
public function __construct(array $data, Client $client)
{
$this->data = $data;
$this->client = $client;
}
/**
* Execute the job.
*
* @param ClientRepository $client_repo
* @param ClientContactRepository $client_contact_repo
* @return Client|null
*/
public function handle(ClientRepository $client_repo, ClientContactRepository $client_contact_repo) : ?Client {
$client = $client_repo->save($this->data, $this->client);
$contacts = $client_contact_repo->save($data['contacts']), $client);
return $client;
}
}

View File

@ -1,56 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Client;
use App\Models\Client;
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class UpdateClient
{
use Dispatchable;
protected $data;
protected $client;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(array $data, Client $client)
{
$this->data = $data;
$this->client = $client;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(ClientRepository $client_repo, ClientContactRepository $client_contact_repo) :?Client
{
$client = $client_repo->save($this->data, $this->client);
$contacts = $client_contact_repo->save($data['contacts']), $client);
return $client->fresh();
}
}

View File

@ -23,6 +23,8 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Mail;
//todo - ensure we are MultiDB Aware in dispatched jobs
class MarkOpened implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter;

View File

@ -17,6 +17,8 @@ use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Repositories\InvoiceRepository;
@ -32,15 +34,18 @@ class ApplyClientPayment implements ShouldQueue
public $payment;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment)
public function __construct(Payment $payment, Company $company)
{
$this->payment = $payment;
$this->company = $company;
}
@ -53,6 +58,8 @@ class ApplyClientPayment implements ShouldQueue
public function handle()
{
MultiDB::setDB($this->company->db);
$client = $this->payment->client;
$client->credit_balance += $this->payment->amount;
$client->save();

View File

@ -11,6 +11,7 @@
namespace App\Jobs\Invoice;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentTerm;
@ -32,17 +33,20 @@ class ApplyInvoiceNumber implements ShouldQueue
public $settings;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, $settings)
public function __construct(Invoice $invoice, $settings, $company)
{
$this->invoice = $invoice;
$this->settings = $settings;
$this->company = $company;
}
/**
@ -53,6 +57,10 @@ class ApplyInvoiceNumber implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
//return early
if($this->invoice->number != '')
return $this->invoice;

View File

@ -17,6 +17,8 @@ use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Repositories\InvoiceRepository;
@ -35,17 +37,21 @@ class ApplyInvoicePayment implements ShouldQueue
public $payment;
public $amount;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, Payment $payment, float $amount)
public function __construct(Invoice $invoice, Payment $payment, float $amount, Company $company)
{
$this->invoice = $invoice;
$this->payment = $payment;
$this->amount = $amount;
$this->company = $company;
}
@ -58,6 +64,8 @@ class ApplyInvoicePayment implements ShouldQueue
public function handle()
{
MultiDB::setDB($this->company->db);
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->amount*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $this->amount*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $this->amount);

View File

@ -13,6 +13,8 @@ namespace App\Jobs\Invoice;
use App\Events\Invoice\InvoiceWasPaid;
use App\Jobs\Invoice\ApplyInvoiceNumber;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentTerm;
@ -33,18 +35,21 @@ class ApplyPaymentToInvoice implements ShouldQueue
public $payment;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, Invoice $invoice)
public function __construct(Payment $payment, Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->payment = $payment;
$this->company = $company;
}
/**
@ -56,6 +61,8 @@ class ApplyPaymentToInvoice implements ShouldQueue
public function handle()
{
MultiDB::setDB($this->company->db);
/* The amount we are adjusting the invoice by*/
$adjustment = $this->payment->amount * -1;
@ -113,7 +120,7 @@ class ApplyPaymentToInvoice implements ShouldQueue
$this->invoice->save();
$this->invoice = ApplyInvoiceNumber::dispatchNow($this->invoice, $invoice->client->getMergedSettings());
$this->invoice = ApplyInvoiceNumber::dispatchNow($this->invoice, $invoice->client->getMergedSettings(), $this->invoice->company);
return $this->invoice;
}

View File

@ -12,6 +12,8 @@
namespace App\Jobs\Invoice;
use App\Factory\InvoiceInvitationFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use Illuminate\Bus\Queueable;
@ -27,21 +29,25 @@ class CreateInvoiceInvitations implements ShouldQueue
private $invoice;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice)
public function __construct(Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->company = $company;
}
public function handle()
{
MultiDB::setDB($this->company->db);
$contacts = $this->invoice->client->contacts;
$contacts->each(function ($contact) {

View File

@ -11,12 +11,14 @@
namespace App\Jobs\Invoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentTerm;
use App\Repositories\InvoiceRepository;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -40,16 +42,18 @@ class CreateInvoicePdf implements ShouldQueue
*
* @return void
*/
public function __construct(Invoice $invoice)
public function __construct(Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->company = $company;
}
public function handle()
{
MultiDB::setDB($this->company->db);
$this->invoice->load('client');
$path = 'public/' . $this->invoice->client->client_hash . '/invoices/';

View File

@ -15,6 +15,7 @@ use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\SystemLog;
use Illuminate\Bus\Queueable;
@ -32,16 +33,20 @@ class EmailInvoice implements ShouldQueue
public $message_array = [];
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice)
public function __construct(Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->company = $company;
}
/**
@ -53,7 +58,7 @@ class EmailInvoice implements ShouldQueue
public function handle()
{
/*Jobs are not multi-db aware, need to set! */
MultiDB::setDB($this->invoice->company->db);
MultiDB::setDB($this->company->db);
//todo - change runtime config of mail driver if necessary

View File

@ -1,69 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Models\Invoice;
use App\Repositories\InvoiceRepository;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
/**
* Class InvoiceActions
* @package App\Jobs\Invoice
*/
class InvoiceActions implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var Invoice $invoice
*/
public $invoice;
/**
* @var array $data
*/
public $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, array $data)
{
$this->invoice = $invoice;
$this->data = $data;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
switch($this->data['action'])
{
//fire actions here
}
}
}

View File

@ -17,6 +17,8 @@ use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Repositories\InvoiceRepository;
@ -32,15 +34,17 @@ class MarkInvoicePaid implements ShouldQueue
public $invoice;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice)
public function __construct(Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->company = $company;
}
@ -52,6 +56,9 @@ class MarkInvoicePaid implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
/* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
@ -69,7 +76,7 @@ class MarkInvoicePaid implements ShouldQueue
$this->invoice->updateBalance($payment->amount*-1);
/* Update Invoice balance */
event(new PaymentWasCreated($payment));
event(new PaymentWasCreated($payment, $payment->company));
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
UpdateClientBalance::dispatchNow($payment->client, $payment->amount*-1);

View File

@ -16,6 +16,8 @@ use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\SystemLog;
@ -31,14 +33,18 @@ class ReverseInvoicePayment implements ShouldQueue
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment;
private $company;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(Payment $payment)
public function __construct(Payment $payment, Company $company)
{
$this->payment = $payment;
$this->company = $company;
}
/**
@ -49,6 +55,7 @@ class ReverseInvoicePayment implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$invoices = $this->payment->invoices()->get();
$client = $this->payment->client;

View File

@ -13,6 +13,8 @@ namespace App\Jobs\Invoice;
use App\Jobs\Invoice\InvoiceNotification;
use App\Jobs\Payment\PaymentNotification;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Repositories\InvoiceRepository;
use Illuminate\Bus\Queueable;
@ -29,18 +31,20 @@ class StoreInvoice implements ShouldQueue
protected $data;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, array $data)
public function __construct(Invoice $invoice, array $data, Company $company)
{
$this->invoice = $invoice;
$this->data = $data;
$this->company = $company;
}
/**
@ -61,6 +65,7 @@ class StoreInvoice implements ShouldQueue
*/
public function handle(InvoiceRepository $invoice_repo) : ?Invoice
{
MultiDB::setDB($this->company->db);
$payment = false;
@ -81,7 +86,7 @@ class StoreInvoice implements ShouldQueue
$this->invoice = $invoice_repo->markSent($this->invoice);
//fire invoice job (the job performs the filtering logic of the email recipients... if any.)
InvoiceNotification::dispatch($invoice);
InvoiceNotification::dispatch($invoice, $invoice->company);
}
@ -100,7 +105,7 @@ class StoreInvoice implements ShouldQueue
if($payment)
{
//fire payment notifications here
PaymentNotification::dispatch($payment);
PaymentNotification::dispatch($payment, $payment->company);
}

View File

@ -16,6 +16,8 @@ use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Payment;
use App\Models\SystemLog;
use App\Utils\Traits\SystemLogTrait;
@ -30,14 +32,18 @@ class UpdateInvoicePayment implements ShouldQueue
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment;
private $company;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(Payment $payment)
public function __construct(Payment $payment, Company $company)
{
$this->payment = $payment;
$this->company = $company;
}
/**
@ -48,6 +54,7 @@ class UpdateInvoicePayment implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$invoices = $this->payment->invoices()->get();

View File

@ -11,6 +11,8 @@
namespace App\Jobs\Payment;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Payment;
use App\Repositories\InvoiceRepository;
use Illuminate\Bus\Queueable;
@ -25,16 +27,19 @@ class PaymentNotification implements ShouldQueue
public $payment;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment)
public function __construct(Payment $payment, Company $company)
{
$this->payment = $payment;
$this->company = $company;
}
/**
@ -45,6 +50,7 @@ class PaymentNotification implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
//notification for the payment.
//

View File

@ -11,9 +11,11 @@
namespace App\Jobs\Quote;
use App\Models\Quote;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Payment;
use App\Models\PaymentTerm;
use App\Models\Quote;
use App\Repositories\QuoteRepository;
use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\NumberFormatter;
@ -32,17 +34,20 @@ class ApplyQuoteNumber implements ShouldQueue
private $settings;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Quote $quote, $settings)
public function __construct(Quote $quote, $settings, Company $company)
{
$this->quote = $quote;
$this->settings = $settings;
$this->company = $company;
}
/**
@ -53,6 +58,8 @@ class ApplyQuoteNumber implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
//return early
if($this->quote->number != '')
return $this->quote;

View File

@ -12,6 +12,8 @@
namespace App\Jobs\Quote;
use App\Factory\QuoteInvitationFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use Illuminate\Bus\Queueable;
@ -27,21 +29,26 @@ class CreateQuoteInvitations implements ShouldQueue
private $quote;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Quote $quote)
public function __construct(Quote $quote, Company $company)
{
$this->quote = $quote;
$this->company = $company;
}
public function handle()
{
MultiDB::setDB($this->company->db);
$contacts = $this->quote->client->contacts;
$contacts->each(function ($contact) {

View File

@ -11,16 +11,17 @@
namespace App\Jobs\Util;
use App\Libraries\MultiDB;
use App\Models\Document;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager;
class UploadFile implements ShouldQueue
{
@ -57,6 +58,7 @@ class UploadFile implements ShouldQueue
*/
public function handle() : ?Document
{
MultiDB::setDB($this->company->db);
$path = $this->encodePrimaryKey($this->company->id);

View File

@ -11,6 +11,7 @@
namespace App\Listeners\Invoice;
use App\Libraries\MultiDB;
use App\Models\Activity;
use App\Models\ClientContact;
use App\Models\InvoiceInvitation;
@ -41,6 +42,7 @@ class UpdateInvoiceActivity implements ShouldQueue
*/
public function handle($event)
{
MultiDB::setDB($event->company->db);
$fields = new \stdClass;

View File

@ -329,7 +329,7 @@ class Invoice extends BaseModel
$storage_path = 'public/' . $this->client->client_hash . '/invoices/'. $this->number . '.pdf';
if(!Storage::exists($storage_path)) {
event(new InvoiceWasUpdated($this));
event(new InvoiceWasUpdated($this, $this->company));
}
return $public_path;
@ -340,7 +340,7 @@ class Invoice extends BaseModel
$storage_path = 'storage/' . $this->client->client_hash . '/invoices/'. $this->number . '.pdf';
if(!Storage::exists($storage_path)) {
CreateInvoicePdf::dispatchNow($this);
CreateInvoicePdf::dispatchNow($this, $this->company);
}
return $storage_path;

View File

@ -166,7 +166,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
event(new PaymentWasCreated($payment));
UpdateInvoicePayment::dispatchNow($payment);
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]);

View File

@ -363,9 +363,9 @@ class StripePaymentDriver extends BasePaymentDriver
/* Link invoices to payment*/
$this->attachInvoices($payment, $hashed_ids);
event(new PaymentWasCreated($payment));
event(new PaymentWasCreated($payment, $payment->company));
UpdateInvoicePayment::dispatchNow($payment);
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
SystemLogger::dispatch([
'server_response' => $payment_intent,

View File

@ -113,7 +113,7 @@ class InvoiceRepository extends BaseRepository
/* If no invitations have been created, this is our fail safe to maintain state*/
if($invoice->invitations->count() == 0)
CreateInvoiceInvitations::dispatchNow($invoice);
CreateInvoiceInvitations::dispatchNow($invoice, $invoice->company);
$invoice = $invoice->calc()->getInvoice();
@ -125,7 +125,7 @@ class InvoiceRepository extends BaseRepository
if($finished_amount != $starting_amount)
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount));
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings());
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company);
if($invoice->company->update_products !== false)
UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice);
@ -150,7 +150,7 @@ class InvoiceRepository extends BaseRepository
* When marked as sent it becomes a ledgerable item.
*
*/
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings());
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company);
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);

View File

@ -51,17 +51,17 @@ class PaymentRepository extends BaseRepository
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
if($invoice)
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount']);
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
}
}
else {
//paid is made, but not to any invoice, therefore we are applying the payment to the clients credit
ApplyClientPayment::dispatchNow($payment);
ApplyClientPayment::dispatchNow($payment, $payment->company);
}
event(new PaymentWasCreated($payment));
event(new PaymentWasCreated($payment, $payment->company));
//UpdateInvoicePayment::dispatchNow($payment);

View File

@ -93,7 +93,7 @@ class QuoteRepository extends BaseRepository
/* If no invitations have been created, this is our fail safe to maintain state*/
if($quote->invitations->count() == 0)
CreateQuoteInvitations::dispatchNow($quote);
CreateQuoteInvitations::dispatchNow($quote, $quote->company);
$quote = $quote->calc()->getInvoice();
@ -101,7 +101,7 @@ class QuoteRepository extends BaseRepository
$finished_amount = $quote->amount;
$quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings());
$quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings(), $quote->company);
return $quote->fresh();
}

View File

@ -182,7 +182,7 @@ class RandomDataSeeder extends Seeder
event(new PaymentWasCreated($payment));
UpdateInvoicePayment::dispatchNow($payment);
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
}
});

View File

@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>

View File

@ -254,50 +254,50 @@ class ClientTest extends TestCase
}
/** @test */
public function testMassivelyCreatingClients()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
// public function testMassivelyCreatingClients()
// {
// $data = [
// 'first_name' => $this->faker->firstName,
// 'last_name' => $this->faker->lastName,
// 'name' => $this->faker->company,
// 'email' => $this->faker->unique()->safeEmail,
// 'password' => 'ALongAndBrilliantPassword123',
// '_token' => csrf_token(),
// 'privacy_policy' => 1,
// 'terms_of_service' => 1
// ];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup?include=account', $data);
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// ])->post('/api/v1/signup?include=account', $data);
$response->assertStatus(200);
// $response->assertStatus(200);
$acc = $response->json();
// $acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
// $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
$token = $account->default_company->tokens->first()->token;
// $token = $account->default_company->tokens->first()->token;
$body = [
'action' => 'create',
'clients' => [
['name' => $this->faker->firstName, 'website' => 'my-awesome-website-1.com'],
['name' => $this->faker->firstName, 'website' => 'my-awesome-website-2.com'],
],
];
// $body = [
// 'action' => 'create',
// 'clients' => [
// ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-1.com'],
// ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-2.com'],
// ],
// ];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->post(route('clients.bulk'), $body);
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $token,
// ])->post(route('clients.bulk'), $body);
$response->assertStatus(200);
// $response->assertStatus(200);
$first_record = Client::where('website', 'my-awesome-website-1.com')->first();
$second_record = Client::where('website', 'my-awesome-website-2.com')->first();
// $first_record = Client::where('website', 'my-awesome-website-1.com')->first();
// $second_record = Client::where('website', 'my-awesome-website-2.com')->first();
$this->assertNotNull($first_record);
$this->assertNotNull($second_record);
}
// $this->assertNotNull($first_record);
// $this->assertNotNull($second_record);
// }
}

View File

@ -63,7 +63,7 @@ class InvoiceEmailTest extends TestCase
$invitations = InvoiceInvitation::whereInvoiceId($this->invoice->id)->get();
$invitations->each(function($invitation) use($message_array, $template_styles) {
$invitations->each(function($invitation) use($message_array, $template_style) {
$contact = $invitation->contact;

View File

@ -37,7 +37,7 @@ class MarkInvoicePaidTest extends TestCase
public function testMarkInvoicePaidInvoice()
{
MarkInvoicePaid::dispatchNow($this->invoice);
MarkInvoicePaid::dispatchNow($this->invoice, $this->company);
$invoice = Invoice::find($this->invoice->id);

View File

@ -37,7 +37,7 @@ class UpdateCompanyLedgerTest extends TestCase
public function testPaymentIsPresentInLedger()
{
$invoice = MarkInvoicePaid::dispatchNow($this->invoice);
$invoice = MarkInvoicePaid::dispatchNow($this->invoice, $this->company);
$ledger = CompanyLedger::whereClientId($invoice->client_id)