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

View File

@ -11,6 +11,7 @@
namespace App\Events\Payment; namespace App\Events\Payment;
use App\Models\Company;
use App\Models\Payment; use App\Models\Payment;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
@ -26,13 +27,15 @@ class PaymentWasCreated
*/ */
public $payment; public $payment;
public $company;
/** /**
* Create a new event instance. * Create a new event instance.
* *
* @param Payment $payment * @param Payment $payment
*/ */
public function __construct(Payment $payment) public function __construct(Payment $payment, Company $company)
{ {
$this->payment = $payment; $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))); 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 = $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)); event(new InvoiceWasCreated($invoice));
@ -396,7 +396,7 @@ class InvoiceController extends BaseController
$invoice = $this->invoice_repo->save($request->all(), $invoice); $invoice = $this->invoice_repo->save($request->all(), $invoice);
event(new InvoiceWasUpdated($invoice)); event(new InvoiceWasUpdated($invoice, $invoice->company));
return $this->itemResponse($invoice); return $this->itemResponse($invoice);
@ -634,7 +634,7 @@ class InvoiceController extends BaseController
if($invoice->balance <= 0 || $invoice->status_id == Invoice::STATUS_PAID) if($invoice->balance <= 0 || $invoice->status_id == Invoice::STATUS_PAID)
return $this->errorResponse(['message' => 'Invoice has no balance owing'], 400); return $this->errorResponse(['message' => 'Invoice has no balance owing'], 400);
$invoice = MarkInvoicePaid::dispatchNow($invoice); $invoice = MarkInvoicePaid::dispatchNow($invoice, $invoice->company);
if(!$bulk) if(!$bulk)
return $this->itemResponse($invoice); return $this->itemResponse($invoice);
@ -661,7 +661,7 @@ class InvoiceController extends BaseController
return $this->listResponse($invoice); return $this->listResponse($invoice);
break; break;
case 'email': case 'email':
EmailInvoice::dispatch($invoice); EmailInvoice::dispatch($invoice, $invoice->company);
if(!$bulk) if(!$bulk)
return response()->json(['message'=>'email sent'],200); return response()->json(['message'=>'email sent'],200);
break; break;

View File

@ -484,7 +484,7 @@ class PaymentController extends BaseController
public function destroy(DestroyPaymentRequest $request, Payment $payment) public function destroy(DestroyPaymentRequest $request, Payment $payment)
{ {
ReverseInvoicePayment::dispatchNow($payment); ReverseInvoicePayment::dispatchNow($payment, $payment->company);
$payment->is_deleted = true; $payment->is_deleted = true;
$payment->save(); $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\Carbon;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
//todo - ensure we are MultiDB Aware in dispatched jobs
class MarkOpened implements ShouldQueue class MarkOpened implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter; 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\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice; use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
@ -32,15 +34,18 @@ class ApplyClientPayment implements ShouldQueue
public $payment; public $payment;
private $company;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(Payment $payment) public function __construct(Payment $payment, Company $company)
{ {
$this->payment = $payment; $this->payment = $payment;
$this->company = $company;
} }
@ -53,6 +58,8 @@ class ApplyClientPayment implements ShouldQueue
public function handle() public function handle()
{ {
MultiDB::setDB($this->company->db);
$client = $this->payment->client; $client = $this->payment->client;
$client->credit_balance += $this->payment->amount; $client->credit_balance += $this->payment->amount;
$client->save(); $client->save();

View File

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

View File

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

View File

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

View File

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

View File

@ -11,12 +11,14 @@
namespace App\Jobs\Invoice; namespace App\Jobs\Invoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentTerm; use App\Models\PaymentTerm;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
@ -40,16 +42,18 @@ class CreateInvoicePdf implements ShouldQueue
* *
* @return void * @return void
*/ */
public function __construct(Invoice $invoice) public function __construct(Invoice $invoice, Company $company)
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
$this->company = $company;
} }
public function handle() public function handle()
{ {
MultiDB::setDB($this->company->db);
$this->invoice->load('client'); $this->invoice->load('client');
$path = 'public/' . $this->invoice->client->client_hash . '/invoices/'; $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\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\TemplateEmail; use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\SystemLog; use App\Models\SystemLog;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
@ -32,16 +33,20 @@ class EmailInvoice implements ShouldQueue
public $message_array = []; public $message_array = [];
private $company;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(Invoice $invoice) public function __construct(Invoice $invoice, Company $company)
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
$this->company = $company;
} }
/** /**
@ -53,7 +58,7 @@ class EmailInvoice implements ShouldQueue
public function handle() public function handle()
{ {
/*Jobs are not multi-db aware, need to set! */ /*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 //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\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice; use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
@ -32,15 +34,17 @@ class MarkInvoicePaid implements ShouldQueue
public $invoice; public $invoice;
private $company;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(Invoice $invoice) public function __construct(Invoice $invoice, Company $company)
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
$this->company = $company;
} }
@ -52,6 +56,9 @@ class MarkInvoicePaid implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
MultiDB::setDB($this->company->db);
/* Create Payment */ /* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); $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); $this->invoice->updateBalance($payment->amount*-1);
/* Update Invoice balance */ /* Update Invoice balance */
event(new PaymentWasCreated($payment)); event(new PaymentWasCreated($payment, $payment->company));
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1)); UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
UpdateClientBalance::dispatchNow($payment->client, $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\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\SystemLog; use App\Models\SystemLog;
@ -31,14 +33,18 @@ class ReverseInvoicePayment implements ShouldQueue
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment; public $payment;
private $company;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct(Payment $payment) public function __construct(Payment $payment, Company $company)
{ {
$this->payment = $payment; $this->payment = $payment;
$this->company = $company;
} }
/** /**
@ -49,6 +55,7 @@ class ReverseInvoicePayment implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
MultiDB::setDB($this->company->db);
$invoices = $this->payment->invoices()->get(); $invoices = $this->payment->invoices()->get();
$client = $this->payment->client; $client = $this->payment->client;

View File

@ -13,6 +13,8 @@ namespace App\Jobs\Invoice;
use App\Jobs\Invoice\InvoiceNotification; use App\Jobs\Invoice\InvoiceNotification;
use App\Jobs\Payment\PaymentNotification; use App\Jobs\Payment\PaymentNotification;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
@ -29,18 +31,20 @@ class StoreInvoice implements ShouldQueue
protected $data; protected $data;
private $company;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(Invoice $invoice, array $data) public function __construct(Invoice $invoice, array $data, Company $company)
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
$this->data = $data; $this->data = $data;
$this->company = $company;
} }
/** /**
@ -61,6 +65,7 @@ class StoreInvoice implements ShouldQueue
*/ */
public function handle(InvoiceRepository $invoice_repo) : ?Invoice public function handle(InvoiceRepository $invoice_repo) : ?Invoice
{ {
MultiDB::setDB($this->company->db);
$payment = false; $payment = false;
@ -81,7 +86,7 @@ class StoreInvoice implements ShouldQueue
$this->invoice = $invoice_repo->markSent($this->invoice); $this->invoice = $invoice_repo->markSent($this->invoice);
//fire invoice job (the job performs the filtering logic of the email recipients... if any.) //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) if($payment)
{ {
//fire payment notifications here //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\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Payment; use App\Models\Payment;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Utils\Traits\SystemLogTrait; use App\Utils\Traits\SystemLogTrait;
@ -30,14 +32,18 @@ class UpdateInvoicePayment implements ShouldQueue
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment; public $payment;
private $company;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct(Payment $payment) public function __construct(Payment $payment, Company $company)
{ {
$this->payment = $payment; $this->payment = $payment;
$this->company = $company;
} }
/** /**
@ -48,6 +54,7 @@ class UpdateInvoicePayment implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
MultiDB::setDB($this->company->db);
$invoices = $this->payment->invoices()->get(); $invoices = $this->payment->invoices()->get();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -166,7 +166,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
event(new PaymentWasCreated($payment)); event(new PaymentWasCreated($payment));
UpdateInvoicePayment::dispatchNow($payment); UpdateInvoicePayment::dispatchNow($payment, $payment->company);
return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]); 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*/ /* Link invoices to payment*/
$this->attachInvoices($payment, $hashed_ids); $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([ SystemLogger::dispatch([
'server_response' => $payment_intent, '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 no invitations have been created, this is our fail safe to maintain state*/
if($invoice->invitations->count() == 0) if($invoice->invitations->count() == 0)
CreateInvoiceInvitations::dispatchNow($invoice); CreateInvoiceInvitations::dispatchNow($invoice, $invoice->company);
$invoice = $invoice->calc()->getInvoice(); $invoice = $invoice->calc()->getInvoice();
@ -125,7 +125,7 @@ class InvoiceRepository extends BaseRepository
if($finished_amount != $starting_amount) if($finished_amount != $starting_amount)
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($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) if($invoice->company->update_products !== false)
UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice); UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice);
@ -150,7 +150,7 @@ class InvoiceRepository extends BaseRepository
* When marked as sent it becomes a ledgerable item. * 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); UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);

View File

@ -51,17 +51,17 @@ class PaymentRepository extends BaseRepository
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first(); $invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
if($invoice) if($invoice)
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount']); ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
} }
} }
else { else {
//paid is made, but not to any invoice, therefore we are applying the payment to the clients credit //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); //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 no invitations have been created, this is our fail safe to maintain state*/
if($quote->invitations->count() == 0) if($quote->invitations->count() == 0)
CreateQuoteInvitations::dispatchNow($quote); CreateQuoteInvitations::dispatchNow($quote, $quote->company);
$quote = $quote->calc()->getInvoice(); $quote = $quote->calc()->getInvoice();
@ -101,7 +101,7 @@ class QuoteRepository extends BaseRepository
$finished_amount = $quote->amount; $finished_amount = $quote->amount;
$quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings()); $quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings(), $quote->company);
return $quote->fresh(); return $quote->fresh();
} }

View File

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

View File

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

View File

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

View File

@ -63,7 +63,7 @@ class InvoiceEmailTest extends TestCase
$invitations = InvoiceInvitation::whereInvoiceId($this->invoice->id)->get(); $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; $contact = $invitation->contact;

View File

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

View File

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