From e7707a919b35e282c14d5a01e1172f90db76dcaf Mon Sep 17 00:00:00 2001 From: = Date: Fri, 9 Apr 2021 19:51:17 +1000 Subject: [PATCH] Delete invoice when payments are applied --- app/Repositories/PaymentRepository.php | 6 +++++- app/Services/Invoice/InvoiceService.php | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 95e3c4ba4c91..b94161609fcc 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -114,6 +114,7 @@ class PaymentRepository extends BaseRepository { /*Iterate through invoices and apply payments*/ if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) { + $invoice_totals = array_sum(array_column($data['invoices'], 'amount')); $invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get(); @@ -125,7 +126,10 @@ class PaymentRepository extends BaseRepository { $invoice = Invoice::whereId($paid_invoice['invoice_id'])->first(); if ($invoice) { - $invoice = $invoice->service()->markSent()->applyPayment($payment, $paid_invoice['amount'])->save(); + $invoice = $invoice->service() + ->markSent() + ->applyPayment($payment, $paid_invoice['amount']) + ->save(); } } } else { diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 6f056e7e900f..86296b632a15 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -101,6 +101,8 @@ class InvoiceService */ public function applyPayment(Payment $payment, float $payment_amount) { + $this->deletePdf(); + $this->invoice = (new ApplyPayment($this->invoice, $payment, $payment_amount))->run(); return $this; @@ -295,6 +297,7 @@ class InvoiceService public function deletePdf() { + nlog("delete PDF"); //UnlinkFile::dispatchNow(config('filesystems.default'), $this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf'); Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');