From 8b3432f3505686d8e747e76ad2ada43d7c5d1d99 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 16 May 2019 09:40:53 +1000 Subject: [PATCH] Apply payments and adjust ledgeR --- app/Http/Controllers/InvoiceController.php | 7 ++++++- app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php | 2 ++ app/Jobs/Company/UpdateCompanyLedgerWithPayment.php | 6 ++++-- app/Jobs/Invoice/ApplyPaymentToInvoice.php | 1 + app/Jobs/Invoice/MarkPaid.php | 6 +++++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index afb5dca882d7..80af5bacc9ff 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -25,6 +25,7 @@ use App\Http\Requests\Invoice\ShowInvoiceRequest; use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Invoice\UpdateInvoiceRequest; use App\Jobs\Entity\ActionEntity; +use App\Jobs\Invoice\MarkPaid; use App\Jobs\Invoice\StoreInvoice; use App\Models\Invoice; use App\Repositories\BaseRepository; @@ -233,7 +234,11 @@ class InvoiceController extends BaseController # code... break; case 'mark_paid': - # code... + if($invoice->balance == 0 || $invoice->status_id == Invoice::STATUS_PAID) + return $this->errorResponse(['message' => 'Invoice has no balance owing'], 400); + + $invoice = MarkPaid::dispatchNow($invoice); + return $this->itemResponse($invoice); break; case 'archive': # code... diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php index 0a3a75d3be83..ddec4f9ec974 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -23,6 +23,7 @@ class UpdateCompanyLedgerWithInvoice public $adjustment; public $invoice; + /** * Create a new job instance. * @@ -58,6 +59,7 @@ class UpdateCompanyLedgerWithInvoice $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(); diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php index 7aaacb3774f3..d95c80b9a1b6 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php @@ -26,7 +26,7 @@ class UpdateCompanyLedgerWithPayment public $adjustment; - public $payment + public $payment; /** * Create a new job instance. @@ -51,6 +51,7 @@ class UpdateCompanyLedgerWithPayment public function handle() { $balance = 0; + $this->adjustment = $this->adjustment * -1; /* Get the last record for the client and set the current balance*/ $ledger = CompanyLedger::whereClientId($this->payment->client_id) @@ -62,8 +63,9 @@ class UpdateCompanyLedgerWithPayment $balance = $ledger->balance; - $company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id); + $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(); diff --git a/app/Jobs/Invoice/ApplyPaymentToInvoice.php b/app/Jobs/Invoice/ApplyPaymentToInvoice.php index af4b47f9d6cb..14f1b7754643 100644 --- a/app/Jobs/Invoice/ApplyPaymentToInvoice.php +++ b/app/Jobs/Invoice/ApplyPaymentToInvoice.php @@ -107,5 +107,6 @@ class ApplyPaymentToInvoice implements ShouldQueue $this->invoice->save(); + return $this->invoice; } } diff --git a/app/Jobs/Invoice/MarkPaid.php b/app/Jobs/Invoice/MarkPaid.php index 6ff3d6073ea2..758fc5a226d8 100644 --- a/app/Jobs/Invoice/MarkPaid.php +++ b/app/Jobs/Invoice/MarkPaid.php @@ -13,6 +13,7 @@ namespace App\Jobs\Invoice; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; +use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Invoice\ApplyPaymentToInvoice; use App\Models\Invoice; use App\Models\Payment; @@ -72,7 +73,10 @@ class MarkPaid implements ShouldQueue event(new PaymentWasCreated($data)); /* Update Invoice balance */ - ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice); + $invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice); + UpdateCompanyLedgerWithPayment::dispatchNow($payment, $payment->amount); + + return $invoice; } }