1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Migrate to services (#3358)

* Working on ledger

* Migrate to services

* Refactor to services

* Migrate to services

* Jobs cleanup
This commit is contained in:
David Bomba 2020-02-21 08:05:01 +11:00 committed by GitHub
parent cf3bcb90be
commit 4eebaf7eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 135 additions and 465 deletions

View File

@ -12,8 +12,6 @@ use App\Factory\InvoiceItemFactory;
use App\Factory\PaymentFactory;
use App\Factory\QuoteFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Jobs\Quote\CreateQuoteInvitations;
use App\Listeners\Credit\CreateCreditInvitation;
use App\Listeners\Invoice\CreateInvoiceInvitation;
@ -459,7 +457,9 @@ class CreateTestData extends Command
$invoice->save();
$invoice->service()->createInvitations();
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
$invoice->ledger()->updateInvoiceBalance($invoice->balance);
//UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
$this->invoice_repo->markSent($invoice);
@ -480,7 +480,8 @@ class CreateTestData extends Command
event(new PaymentWasCreated($payment, $payment->company));
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
$payment->service()->updateInvoicePayment();
//UpdateInvoicePayment::dispatchNow($payment, $payment->company);
}
//@todo this slow things down, but gives us PDFs of the invoices for inspection whilst debugging.
event(new InvoiceWasCreated($invoice, $invoice->company));

View File

@ -442,8 +442,8 @@ class PaymentController extends BaseController
*/
public function destroy(DestroyPaymentRequest $request, Payment $payment)
{
ReverseInvoicePayment::dispatchNow($payment, $payment->company);
$payment->service()->reversePayment();
$payment->is_deleted = true;
$payment->save();
$payment->delete();

View File

@ -1,76 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateCompanyLedgerWithInvoice
{
use Dispatchable;
public $adjustment;
public $invoice;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, float $adjustment, Company $company)
{
$this->invoice = $invoice;
$this->adjustment = $adjustment;
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$balance = 0;
$ledger = CompanyLedger::whereClientId($this->invoice->client_id)
->whereCompanyId($this->invoice->company_id)
->orderBy('id', 'DESC')
->first();
if ($ledger) {
$balance = $ledger->balance;
}
$adjustment = $balance + $this->adjustment;
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
$company_ledger->client_id = $this->invoice->client_id;
$company_ledger->adjustment = $this->adjustment;
$company_ledger->balance = $balance + $this->adjustment;
$company_ledger->save();
$this->invoice->company_ledger()->save($company_ledger);
}
}

View File

@ -1,80 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use App\Models\Payment;
use Illuminate\Foundation\Bus\Dispatchable;
/**
* Class for update company ledger with payment.
*/
class UpdateCompanyLedgerWithPayment
{
use Dispatchable;
public $adjustment;
public $payment;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, float $adjustment, Company $company)
{
$this->payment = $payment;
$this->adjustment = $adjustment;
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$balance = 0;
/* Get the last record for the client and set the current balance*/
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
->whereCompanyId($this->payment->company_id)
->orderBy('id', 'DESC')
->first();
if ($ledger) {
$balance = $ledger->balance;
}
$company_ledger = CompanyLedgerFactory::create($this->payment->company_id, $this->payment->user_id);
$company_ledger->client_id = $this->payment->client_id;
$company_ledger->adjustment = $this->adjustment;
$company_ledger->balance = $balance + $this->adjustment;
$company_ledger->save();
$this->payment->company_ledger()->save($company_ledger); //todo add model directive here
}
}

View File

@ -13,7 +13,6 @@ namespace App\Jobs\Credit;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Credit\ApplyPaymentToCredit;
use App\Libraries\MultiDB;
use App\Models\Company;

View File

@ -61,7 +61,8 @@ class MarkOpened implements ShouldQueue
return false;
}
$invitation->email_error = $error;
$invitation->opened_date = now();
//$invitation->email_error = $error;
$invitation->save();
}
}

View File

@ -1,120 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Events\Invoice\InvoiceWasPaid;
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 Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
class ApplyPaymentToInvoice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter;
public $invoice;
public $payment;
private $company;
/**
* @deprecated confirm to be deleted
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->payment = $payment;
$this->company = $company;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
/* The amount we are adjusting the invoice by*/
$adjustment = $this->payment->amount * -1;
/* Calculate if the amount paid is less than the partial value.
* Needed if there is a condition under which a value LESS
* than the partial amount has been paid. The Invoice will
* be updated to reflect the NEW partial amount
*/
$partial = max(0, $this->invoice->partial - $this->payment->amount);
/* check if partial exists */
if ($this->invoice->partial > 0) {
//if payment amount = partial
if ($this->formatvalue($this->invoice->partial, 4) == $this->formatValue($this->payment->amount, 4)) {
$this->invoice->partial = 0;
$this->invoice->partial_due_date = null;
}
//if payment amount < partial amount
if ($this->formatvalue($this->invoice->partial, 4) > $this->formatValue($this->payment->amount, 4)) {
//set the new partial amount to the balance
$this->invoice->partial = $partial;
}
if (!$this->invoice->due_date) {
$this->invoice->due_date = Carbon::now()->addDays(PaymentTerm::find($this->invoice->settings->payment_terms)->num_days);
}
}
/* Update Invoice Balance */
$this->invoice->balance = $this->invoice->balance + $adjustment;
/* Update Invoice Status */
if ($this->invoice->balance == 0) {
$this->invoice->status_id = Invoice::STATUS_PAID;
$this->invoice->save();
event(new InvoiceWasPaid($this->invoice, $this->invoice->company));
} elseif ($this->payment->amount > 0 && $this->invoice->balance > 0) {
$this->invoice->status_id = Invoice::STATUS_PARTIAL;
}
/*If auto-archive is enabled, and balance = 0 - archive invoice */
if ($this->invoice->settings->auto_archive_invoice && $this->invoice->balance == 0) {
$invoiceRepo = app('App\Repositories\InvoiceRepository');
$invoiceRepo->archive($this->invoice);
}
$this->invoice->save();
$this->invoice = $this->invoice->service()->applyNumber()->save();
return $this->invoice;
}
}

View File

@ -100,7 +100,7 @@ class CreateInvoicePdf implements ShouldQueue {
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
//$instance= Storage::disk($this->disk)->path($file_path);
//
return $file_path;
}

View File

@ -1,77 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
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;
use App\Utils\Traits\SystemLogTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
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, Company $company)
{
$this->payment = $payment;
$this->company = $company;
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$invoices = $this->payment->invoices()->get();
$client = $this->payment->client;
$invoices->each(function ($invoice) {
if ($invoice->pivot->amount > 0) {
$invoice->status_id = Invoice::STATUS_SENT;
$invoice->balance = $invoice->pivot->amount;
$invoice->save();
}
});
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment->amount), $this->company);
$client->service()
->updateBalance($this->payment->amount)
->updatePaidToDate($this->payment->amount*-1)
->save();
}
}

View File

@ -100,28 +100,8 @@ class CreateQuotePdf implements ShouldQueue {
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
//$instance= Storage::disk($this->disk)->path($file_path);
//
return $file_path;
}
/**
* Returns a PDF stream
*
* @param string $header Header to be included in PDF
* @param string $footer Footer to be included in PDF
* @param string $html The HTML object to be converted into PDF
*
* @return string The PDF string
*/
private function makePdf($header, $footer, $html) {
return Browsershot::html($html)
//->showBrowserHeaderAndFooter()
//->headerHtml($header)
//->footerHtml($footer)
->deviceScaleFactor(1)
->showBackground()
->waitUntilNetworkIdle(true) ->pdf();
//->margins(10,10,10,10)
//->savePdf('test.pdf');
}
}

View File

@ -19,10 +19,12 @@ use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Models\CompanyLedger;
use App\Models\Currency;
use App\Models\Filterable;
use App\Models\PaymentTerm;
use App\Services\Invoice\InvoiceService;
use App\Services\Ledger\LedgerService;
use App\Utils\Number;
use App\Utils\Traits\InvoiceEmailBuilder;
use App\Utils\Traits\MakesDates;
@ -163,11 +165,16 @@ class Invoice extends BaseModel
/**
* Service entry points
*/
public function service(): InvoiceService
public function service() :InvoiceService
{
return new InvoiceService($this);
}
public function ledger()
{
return new LedgerService($this);
}
/* ---------------- */
/* Settings getters */
/* ---------------- */

View File

@ -17,6 +17,7 @@ use App\Models\Credit;
use App\Models\DateFormat;
use App\Models\Filterable;
use App\Models\Paymentable;
use App\Services\Ledger\LedgerService;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
@ -169,6 +170,11 @@ class Payment extends BaseModel
}
}
public function ledger()
{
return new LedgerService($this);
}
public function resolveRouteBinding($value)
{
return $this

View File

@ -12,7 +12,7 @@
namespace App\PaymentDrivers;
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Invoice\UpdateInvoicePayment;
//use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
@ -160,7 +160,9 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
event(new PaymentWasCreated($payment, $payment->company));
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
$payment->service()->UpdateInvoicePayment();
//UpdateInvoicePayment::dispatchNow($payment, $payment->company);
return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]);
}

View File

@ -13,7 +13,7 @@ namespace App\PaymentDrivers;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Invoice\UpdateInvoicePayment;
//use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
@ -361,7 +361,8 @@ class StripePaymentDriver extends BasePaymentDriver
event(new PaymentWasCreated($payment, $payment->company));
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
$payment->service()->UpdateInvoicePayment();
//UpdateInvoicePayment::dispatchNow($payment, $payment->company);
SystemLogger::dispatch(
[

View File

@ -38,7 +38,6 @@ use App\Listeners\Invoice\InvoiceEmailActivity;
use App\Listeners\Invoice\InvoiceEmailFailedActivity;
use App\Listeners\Invoice\UpdateInvoiceActivity;
use App\Listeners\Invoice\UpdateInvoiceInvitations;
use App\Listeners\Invoice\UpdateInvoicePayment;
use App\Listeners\SendVerificationNotification;
use App\Listeners\User\UpdateUserLastLogin;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@ -73,8 +72,6 @@ class EventServiceProvider extends ServiceProvider
],
PaymentWasCreated::class => [
PaymentCreatedActivity::class,
//UpdateInvoicePayment::class,
//UpdateInvoiceInvitations::class,
],
PaymentWasDeleted::class => [
PaymentDeletedActivity::class,

View File

@ -13,9 +13,7 @@ namespace App\Repositories;
use App\Factory\InvoiceInvitationFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Product\UpdateOrCreateProduct;
use App\Models\ClientContact;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
@ -109,7 +107,7 @@ class InvoiceRepository extends BaseRepository {
/**/
if (($finished_amount != $starting_amount) && ($invoice->status_id != Invoice::STATUS_DRAFT)) {
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount-$starting_amount), $invoice->company);
$invoice->ledger()->updateInvoiceBalance(($finished_amount-$starting_amount));
}
$invoice = $invoice->service()->applyNumber()->save();

View File

@ -13,9 +13,8 @@ namespace App\Repositories;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\CreditFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Credit\ApplyCreditPayment;
use App\Jobs\Invoice\UpdateInvoicePayment;
//use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
@ -130,7 +129,6 @@ class PaymentRepository extends BaseRepository
elseif ($invoice_totals < $payment->amount)
$payment->applied += $invoice_totals;
//UpdateInvoicePayment::dispatchNow($payment);
$payment->save();
return $payment->fresh();

View File

@ -5,7 +5,6 @@ namespace App\Services\Credit;
use App\Credit;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Customer\UpdateCustomerBalance;
use App\Jobs\Customer\UpdateCustomerPaidToDate;
use App\Models\Client;

View File

@ -13,7 +13,6 @@ namespace App\Services\Invoice;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Payment;

View File

@ -11,7 +11,6 @@
namespace App\Services\Invoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
@ -35,8 +34,9 @@ class ApplyPayment extends AbstractService
public function run()
{
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment_amount*-1), $this->payment->company);
$this->payment
->ledger()
->updatePaymentBalance($this->payment_amount*-1);
$this->payment->client->service()->updateBalance($this->payment_amount*-1)->save();

View File

@ -13,7 +13,6 @@ namespace App\Services\Invoice;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
@ -61,7 +60,8 @@ class MarkPaid extends AbstractService
/* Update Invoice balance */
event(new PaymentWasCreated($payment, $payment->company));
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1), $payment->company);
$payment->ledger()
->updatePaymentBalance($payment->amount*-1);
$this->client_service
->updateBalance($payment->amount*-1)

View File

@ -12,7 +12,6 @@
namespace App\Services\Invoice;
use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Models\Invoice;
use App\Services\AbstractService;
@ -47,7 +46,9 @@ class MarkSent extends AbstractService
$this->invoice->service()->setStatus(Invoice::STATUS_SENT)->applyNumber()->save();
UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->balance, $this->invoice->company);
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance);
//UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->balance, $this->invoice->company);
return $this->invoice;

View File

@ -11,6 +11,7 @@
namespace App\Services\Ledger;
use App\Factory\CompanyLedgerFactory;
use App\Models\CompanyLedger;
class LedgerService
@ -27,8 +28,10 @@ class LedgerService
{
$balance = 0;
if ($this->ledger()) {
$balance = $this->ledger->balance;
$company_ledger = $this->ledger();
if ($company_ledger) {
$balance = $company_ledger->balance;
}
$adjustment = $balance + $adjustment;
@ -44,13 +47,35 @@ class LedgerService
return $this;
}
private function ledger() :CompanyLedger
public function updatePaymentBalance($adjustment)
{
$balance = 0;
/* Get the last record for the client and set the current balance*/
$company_ledger = $this->ledger();
if ($company_ledger) {
$balance = $company_ledger->balance;
}
$company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);
$company_ledger->client_id = $this->entity->client_id;
$company_ledger->adjustment = $adjustment;
$company_ledger->balance = $balance + $adjustment;
$company_ledger->save();
$this->entity->company_ledger()->save($company_ledger);
}
private function ledger() :?CompanyLedger
{
return CompanyLedger::whereClientId($this->entity->client_id)
->whereCompanyId($this->entity->company_id)
->orderBy('id', 'DESC')
->first();
}
public function save()

View File

@ -12,7 +12,9 @@
namespace App\Services\Payment;
use App\Factory\PaymentFactory;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\Payment\UpdateInvoicePayment;
class PaymentService
@ -45,8 +47,32 @@ class PaymentService
public function sendEmail($contact = null)
{
$send_email = new SendEmail($this->payment);
return (new SendEmail($this->payment))->run(null, $contact);
}
return $send_email->run(null, $contact);
public function reversePayment()
{
$invoices = $this->payment->invoices()->get();
$client = $this->payment->client;
$invoices->each(function ($invoice) {
if ($invoice->pivot->amount > 0) {
$invoice->status_id = Invoice::STATUS_SENT;
$invoice->balance = $invoice->pivot->amount;
$invoice->save();
}
});
$this->payment->ledger()->updatePaymentBalance($this->payment->amount);
$client->service()
->updateBalance($this->payment->amount)
->updatePaidToDate($this->payment->amount*-1)
->save();
}
public function updateInvoicePayment()
{
return ((new UpdateInvoicePayment($this->payment)))->run();
}
}

View File

@ -1,68 +1,35 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
namespace App\Services\Payment;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Helpers\Email\PaymentEmail;
use App\Jobs\Payment\EmailPayment;
use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Payment;
use App\Models\Invoice;
use App\Models\SystemLog;
use App\Utils\Traits\SystemLogTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class UpdateInvoicePayment implements ShouldQueue
class UpdateInvoicePayment
{
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment;
private $company;
/**
* @deprecated we only use this in test data creation. shouldn't be used in production
* Create the event listener.
*
* @return void
*/
public function __construct(Payment $payment, Company $company)
public function __construct($payment)
{
$this->payment = $payment;
$this->company = $company;
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle()
public function run()
{
MultiDB::setDB($this->company->db);
$invoices = $this->payment->invoices()->get();
$invoices = $this->payment->invoices()->get();
$invoices_total = $invoices->sum('balance');
/* Simplest scenario - All invoices are paid in full*/
if (strval($invoices_total) === strval($this->payment->amount)) {
$invoices->each(function ($invoice) {
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1), $this->company);
$this->payment
->ledger()
->updatePaymentBalance($this->payment, ($invoice->balance*-1));
$this->payment->client
->service()
@ -96,7 +63,10 @@ class UpdateInvoicePayment implements ShouldQueue
if ($this->payment->amount == $total) {
$invoices->each(function ($invoice) {
if ($invoice->hasPartial()) {
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1), $this->company);
$this->payment
->ledger()
->updatePaymentBalance($this->payment, ($invoice->partial*-1));
$this->payment->client->service()
->updateBalance($invoice->partial*-1)
@ -112,7 +82,10 @@ class UpdateInvoicePayment implements ShouldQueue
->setStatus(Invoice::STATUS_PARTIAL)
->save();
} else {
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1), $this->company);
$this->payment
->ledger()
->updatePaymentBalance($this->payment, ($invoice->balance*-1));
$this->payment->client->service()
->updateBalance($invoice->balance*-1)
@ -148,5 +121,8 @@ class UpdateInvoicePayment implements ShouldQueue
$this->payment->delete();
}
}
return $this->payment;
}
}
}

View File

@ -312,6 +312,8 @@ trait MakesInvoiceValues
$data['$invoice3'] = $this->custom_value3 ?: '&nbsp;';
$data['$invoice4'] = $this->custom_value4 ?: '&nbsp;';
$data['$invoice.public_notes'] = $this->public_notes ?: '&nbsp;';
$data['$entity.public_notes'] = &$data['$invoice.public_notes'];
// $data['$your_invoice'] = ;
// $data['$quote'] = ;
// $data['$your_quote'] = ;

View File

@ -9,7 +9,7 @@ use App\Events\Payment\PaymentWasCreated;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\UpdateInvoicePayment;
//use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Listeners\Credit\CreateCreditInvitation;
use App\Listeners\Invoice\CreateInvoiceInvitation;
use App\Models\Account;
@ -187,7 +187,9 @@ class RandomDataSeeder extends Seeder
event(new PaymentWasCreated($payment, $payment->company));
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
$payment->service()->UpdateInvoicePayment();
// UpdateInvoicePayment::dispatchNow($payment, $payment->company);
}
});

View File

@ -38,7 +38,7 @@ class DesignTest extends TestCase
//\Log::error($html);
$settings = $this->invoice->client->settings;
$settings->invoice_design_id = "4";
$settings->invoice_design_id = "5";
$this->client->settings = $settings;
$this->client->save();
@ -60,7 +60,7 @@ class DesignTest extends TestCase
//\Log::error($html);
$settings = $this->invoice->client->settings;
$settings->quote_design_id = "4";
$settings->quote_design_id = "10";
$this->client->settings = $settings;
$this->client->save();

View File

@ -18,6 +18,9 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\MockAccountData;
use Tests\TestCase;
/** @test
/** @covers App\Services\Ledger\LedgerService */
class UpdateCompanyLedgerTest extends TestCase
{
use MockAccountData;

View File

@ -22,7 +22,6 @@ use App\Factory\InvoiceInvitationFactory;
use App\Factory\InvoiceItemFactory;
use App\Factory\InvoiceToRecurringInvoiceFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Models\Client;
use App\Models\CompanyGateway;
use App\Models\CompanyToken;
@ -272,7 +271,8 @@ trait MockAccountData
$this->invoice->save();
UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount, $this->invoice->company);
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount);
// UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount, $this->invoice->company);
$recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice);
$recurring_invoice->next_send_date = Carbon::now();