Fixes for handling partial payments with credits

This commit is contained in:
David Bomba 2022-09-06 19:18:05 +10:00
parent 84e634c28c
commit a0d1635a58
3 changed files with 34 additions and 25 deletions

View File

@ -901,9 +901,10 @@ class BaseController extends Controller
return redirect('/')->with(['signup' => 'true']); return redirect('/')->with(['signup' => 'true']);
} }
// 06-09-2022 - parse the path if loaded in a subdirectory for canvaskit resolution
$canvas_path_array = parse_url(config('ninja.app_url')); $canvas_path_array = parse_url(config('ninja.app_url'));
$canvas_path = (array_key_exists('path', $canvas_path_array)) ? $canvas_path_array['path'] : ''; $canvas_path = (array_key_exists('path', $canvas_path_array)) ? $canvas_path_array['path'] : '';
$canvas_path = rtrim(str_replace("index.php", "", $canvas_path),'/');
$data = []; $data = [];

View File

@ -35,6 +35,9 @@ class AutoBillInvoice extends AbstractService
protected $db; protected $db;
/*Specific variable for partial payments */
private bool $is_partial_amount = false;
public function __construct(Invoice $invoice, $db) public function __construct(Invoice $invoice, $db)
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
@ -46,7 +49,8 @@ class AutoBillInvoice extends AbstractService
{ {
MultiDB::setDb($this->db); MultiDB::setDb($this->db);
$this->client = $this->invoice->client->fresh(); /* Harvest Client*/
$this->client = $this->invoice->client;
$is_partial = false; $is_partial = false;
@ -68,6 +72,10 @@ class AutoBillInvoice extends AbstractService
$this->applyCreditPayment(); $this->applyCreditPayment();
} }
//If this returns true, it means a partial invoice amount was paid as a credit and there is no further balance payable
if($this->is_partial_amount && $this->invoice->partial == 0)
return;
$amount = 0; $amount = 0;
/* Determine $amount */ /* Determine $amount */
@ -169,9 +177,9 @@ class AutoBillInvoice extends AbstractService
$payment->invoices()->attach($this->invoice->id, ['amount' => $amount]); $payment->invoices()->attach($this->invoice->id, ['amount' => $amount]);
$this->invoice $this->invoice
->service() ->service()
->setStatus(Invoice::STATUS_PAID) ->setCalculatedStatus()
->save(); ->save();
foreach ($this->used_credit as $credit) { foreach ($this->used_credit as $credit) {
$current_credit = Credit::find($credit['credit_id']); $current_credit = Credit::find($credit['credit_id']);
@ -191,18 +199,18 @@ class AutoBillInvoice extends AbstractService
->updatePaymentBalance($amount * -1) ->updatePaymentBalance($amount * -1)
->save(); ->save();
$client = $this->invoice->client->fresh(); $this->invoice
->client
$client->service() ->service()
->updateBalance($amount * -1) ->updateBalanceAndPaidToDate($amount * -1, $amount)
->updatePaidToDate($amount) // ->updateBalance($amount * -1)
->adjustCreditBalance($amount * -1) // ->updatePaidToDate($amount)
->save(); ->adjustCreditBalance($amount * -1)
->save();
$this->invoice->ledger() //09-03-2022 $this->invoice->ledger() //09-03-2022
// ->updateInvoiceBalance($amount * -1, "Invoice {$this->invoice->number} payment using Credit {$current_credit->number}") ->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}")
->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}") ->save();
->save();
event('eloquent.created: App\Models\Payment', $payment); event('eloquent.created: App\Models\Payment', $payment);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
@ -235,16 +243,14 @@ class AutoBillInvoice extends AbstractService
return; return;
} }
$is_partial_amount = false;
if ($this->invoice->partial > 0) { if ($this->invoice->partial > 0) {
$is_partial_amount = true; $this->is_partial_amount = true;
} }
$this->used_credit = []; $this->used_credit = [];
foreach ($available_credits as $key => $credit) { foreach ($available_credits as $key => $credit) {
if ($is_partial_amount) { if ($this->is_partial_amount) {
//more credit than needed //more credit than needed
if ($credit->balance > $this->invoice->partial) { if ($credit->balance > $this->invoice->partial) {

View File

@ -62,15 +62,17 @@ class UpdateInvoicePayment
$paid_amount = $paid_invoice->amount; $paid_amount = $paid_invoice->amount;
} }
\DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){ $client->service()->updateBalanceAndPaidToDate($paid_amount*-1, $paid_amount);
// \DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){
$update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first(); // $update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first();
$update_client->paid_to_date += $paid_amount; // $update_client->paid_to_date += $paid_amount;
$update_client->balance -= $paid_amount; // $update_client->balance -= $paid_amount;
$update_client->save(); // $update_client->save();
}, 1); // }, 1);
/* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */ /* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */
if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance) if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance)