diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 70b4308027..36f7302247 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -322,8 +322,6 @@ class BaseDriver extends AbstractPaymentDriver if (collect($invoice->line_items)->contains('type_id', '3')) { $invoice->service()->toggleFeesPaid()->save(); - // $invoice->client->service()->updateBalance($fee_total)->save(); - // $invoice->ledger()->updateInvoiceBalance($fee_total, "Gateway fee adjustment for invoice {$invoice->number}"); } $transaction = [ diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index d611f5a7da..555432e87d 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -404,7 +404,6 @@ class InvoiceService public function updatePartial($amount) { $this->invoice->partial += $amount; - // $this->invoice->increment('partial', $amount); return $this; } @@ -535,7 +534,7 @@ class InvoiceService /* Throws: Payment amount xxx does not match invoice totals. */ if ($this->invoice->trashed()) - return; + return $this; $this->invoice->delete(); diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 5054c9b87a..64c2b2b523 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -40,19 +40,20 @@ class UpdateInvoicePayment $paid_invoices = $this->payment_hash->invoices(); $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get(); + + $client = $this->payment->client; - collect($paid_invoices)->each(function ($paid_invoice) use ($invoices) { + if($client->trashed()) + $client->restore(); - $client = $this->payment->client; + collect($paid_invoices)->each(function ($paid_invoice) use ($invoices, $client) { - if($client->trashed()) - $client->restore(); + $client = $client->fresh(); $invoice = $invoices->first(function ($inv) use ($paid_invoice) { return $paid_invoice->invoice_id == $inv->hashed_id; }); - if($invoice->trashed()) $invoice->restore(); @@ -62,23 +63,23 @@ class UpdateInvoicePayment $paid_amount = $paid_invoice->amount; } - $client - ->service() - ->updatePaidToDate($paid_amount) - ->save(); + $client->paid_to_date += $paid_amount; + $client->balance -= $paid_amount; + $client->save(); /* 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) $paid_amount = $invoice->balance; /*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! - ->clearPartial() - ->updateBalance($paid_amount * -1) - ->updatePaidToDate($paid_amount) - ->updateStatus() - ->touchPdf() - ->save(); + //caution what if we amount paid was less than partial - we wipe it! + $invoice = $invoice->service() + ->clearPartial() + ->updateBalance($paid_amount * -1) + ->updatePaidToDate($paid_amount) + ->updateStatus() + ->touchPdf() + ->save(); $invoice->service() ->workFlow() @@ -89,11 +90,6 @@ class UpdateInvoicePayment ->ledger() ->updatePaymentBalance($paid_amount * -1); - $client - ->service() - ->updateBalance($paid_amount * -1) - ->save(); - $pivot_invoice = $this->payment->invoices->first(function ($inv) use ($paid_invoice) { return $inv->hashed_id == $paid_invoice->invoice_id; });