Apply payments and adjust ledgeR

This commit is contained in:
David Bomba 2019-05-16 09:40:53 +10:00
parent c236925e6d
commit 8b3432f350
5 changed files with 18 additions and 4 deletions

View File

@ -25,6 +25,7 @@ use App\Http\Requests\Invoice\ShowInvoiceRequest;
use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Invoice\UpdateInvoiceRequest; use App\Http\Requests\Invoice\UpdateInvoiceRequest;
use App\Jobs\Entity\ActionEntity; use App\Jobs\Entity\ActionEntity;
use App\Jobs\Invoice\MarkPaid;
use App\Jobs\Invoice\StoreInvoice; use App\Jobs\Invoice\StoreInvoice;
use App\Models\Invoice; use App\Models\Invoice;
use App\Repositories\BaseRepository; use App\Repositories\BaseRepository;
@ -233,7 +234,11 @@ class InvoiceController extends BaseController
# code... # code...
break; break;
case 'mark_paid': 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; break;
case 'archive': case 'archive':
# code... # code...

View File

@ -23,6 +23,7 @@ class UpdateCompanyLedgerWithInvoice
public $adjustment; public $adjustment;
public $invoice; public $invoice;
/** /**
* Create a new job instance. * 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 = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
$company_ledger->client_id = $this->invoice->client_id; $company_ledger->client_id = $this->invoice->client_id;
$company_ledger->adjustment = $this->adjustment;
$company_ledger->balance = $balance + $this->adjustment; $company_ledger->balance = $balance + $this->adjustment;
$company_ledger->save(); $company_ledger->save();

View File

@ -26,7 +26,7 @@ class UpdateCompanyLedgerWithPayment
public $adjustment; public $adjustment;
public $payment public $payment;
/** /**
* Create a new job instance. * Create a new job instance.
@ -51,6 +51,7 @@ class UpdateCompanyLedgerWithPayment
public function handle() public function handle()
{ {
$balance = 0; $balance = 0;
$this->adjustment = $this->adjustment * -1;
/* Get the last record for the client and set the current balance*/ /* Get the last record for the client and set the current balance*/
$ledger = CompanyLedger::whereClientId($this->payment->client_id) $ledger = CompanyLedger::whereClientId($this->payment->client_id)
@ -62,8 +63,9 @@ class UpdateCompanyLedgerWithPayment
$balance = $ledger->balance; $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->client_id = $this->payment->client_id;
$company_ledger->adjustment = $this->adjustment;
$company_ledger->balance = $balance + $this->adjustment; $company_ledger->balance = $balance + $this->adjustment;
$company_ledger->save(); $company_ledger->save();

View File

@ -107,5 +107,6 @@ class ApplyPaymentToInvoice implements ShouldQueue
$this->invoice->save(); $this->invoice->save();
return $this->invoice;
} }
} }

View File

@ -13,6 +13,7 @@ namespace App\Jobs\Invoice;
use App\Events\Payment\PaymentWasCreated; use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory; use App\Factory\PaymentFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice; use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
@ -72,7 +73,10 @@ class MarkPaid implements ShouldQueue
event(new PaymentWasCreated($data)); event(new PaymentWasCreated($data));
/* Update Invoice balance */ /* Update Invoice balance */
ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice); $invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice);
UpdateCompanyLedgerWithPayment::dispatchNow($payment, $payment->amount);
return $invoice;
} }
} }