From c3711ad226ff9afa8c908bd575657ae4e824e47d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 4 Jun 2025 14:25:14 +1000 Subject: [PATCH] Refactor for markpaid to prevent duplicate balance updated --- app/Services/Invoice/MarkPaid.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 7a4a8d8456..4b5f373b99 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -41,7 +41,30 @@ class MarkPaid extends AbstractService } if ($this->invoice->status_id == Invoice::STATUS_DRAFT) { - $this->invoice = $this->invoice->service()->markSent()->save(); + // $this->invoice = $this->invoice->service()->markSent()->save(); + + /*Set status*/ + $this->invoice->status_id = Invoice::STATUS_SENT; + $this->invoice->balance = $this->invoice->amount; + + /*Update ledger*/ + $this->invoice + ->ledger() + ->updateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} marked as sent."); + + $this->invoice->client->service()->updateBalance($this->invoice->amount); + /* Perform additional actions on invoice */ + $this->invoice + ->service() + ->applyNumber() + ->setDueDate() + ->setReminder() + ->save(); + + $this->invoice->markInvitationsSent(); + + event(new \App\Events\Invoice\InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + } $already_paid = false;