Improve invoice payment registrations

This commit is contained in:
David Bomba 2022-03-28 16:36:00 +11:00
parent da47f1bf32
commit 4c2c62bd85
3 changed files with 18 additions and 25 deletions

View File

@ -322,8 +322,6 @@ class BaseDriver extends AbstractPaymentDriver
if (collect($invoice->line_items)->contains('type_id', '3')) { if (collect($invoice->line_items)->contains('type_id', '3')) {
$invoice->service()->toggleFeesPaid()->save(); $invoice->service()->toggleFeesPaid()->save();
// $invoice->client->service()->updateBalance($fee_total)->save();
// $invoice->ledger()->updateInvoiceBalance($fee_total, "Gateway fee adjustment for invoice {$invoice->number}");
} }
$transaction = [ $transaction = [

View File

@ -404,7 +404,6 @@ class InvoiceService
public function updatePartial($amount) public function updatePartial($amount)
{ {
$this->invoice->partial += $amount; $this->invoice->partial += $amount;
// $this->invoice->increment('partial', $amount);
return $this; return $this;
} }
@ -535,7 +534,7 @@ class InvoiceService
/* Throws: Payment amount xxx does not match invoice totals. */ /* Throws: Payment amount xxx does not match invoice totals. */
if ($this->invoice->trashed()) if ($this->invoice->trashed())
return; return $this;
$this->invoice->delete(); $this->invoice->delete();

View File

@ -41,18 +41,19 @@ class UpdateInvoicePayment
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get(); $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get();
collect($paid_invoices)->each(function ($paid_invoice) use ($invoices) {
$client = $this->payment->client; $client = $this->payment->client;
if($client->trashed()) if($client->trashed())
$client->restore(); $client->restore();
collect($paid_invoices)->each(function ($paid_invoice) use ($invoices, $client) {
$client = $client->fresh();
$invoice = $invoices->first(function ($inv) use ($paid_invoice) { $invoice = $invoices->first(function ($inv) use ($paid_invoice) {
return $paid_invoice->invoice_id == $inv->hashed_id; return $paid_invoice->invoice_id == $inv->hashed_id;
}); });
if($invoice->trashed()) if($invoice->trashed())
$invoice->restore(); $invoice->restore();
@ -62,17 +63,17 @@ class UpdateInvoicePayment
$paid_amount = $paid_invoice->amount; $paid_amount = $paid_invoice->amount;
} }
$client $client->paid_to_date += $paid_amount;
->service() $client->balance -= $paid_amount;
->updatePaidToDate($paid_amount) $client->save();
->save();
/* 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)
$paid_amount = $invoice->balance; $paid_amount = $invoice->balance;
/*Improve performance here - 26-01-2022 - also change the order of events for invoice first*/ /*Improve performance here - 26-01-2022 - also change the order of events for invoice first*/
$invoice->service() //caution what if we amount paid was less than partial - we wipe it! //caution what if we amount paid was less than partial - we wipe it!
$invoice = $invoice->service()
->clearPartial() ->clearPartial()
->updateBalance($paid_amount * -1) ->updateBalance($paid_amount * -1)
->updatePaidToDate($paid_amount) ->updatePaidToDate($paid_amount)
@ -89,11 +90,6 @@ class UpdateInvoicePayment
->ledger() ->ledger()
->updatePaymentBalance($paid_amount * -1); ->updatePaymentBalance($paid_amount * -1);
$client
->service()
->updateBalance($paid_amount * -1)
->save();
$pivot_invoice = $this->payment->invoices->first(function ($inv) use ($paid_invoice) { $pivot_invoice = $this->payment->invoices->first(function ($inv) use ($paid_invoice) {
return $inv->hashed_id == $paid_invoice->invoice_id; return $inv->hashed_id == $paid_invoice->invoice_id;
}); });