From d29f1798a26360b719516dce2c41406c526c1366 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 8 Aug 2021 08:40:04 +1000 Subject: [PATCH] Fixes for auto-archive invoices --- app/Services/Invoice/ApplyPayment.php | 2 +- app/Services/Invoice/InvoiceService.php | 18 +++++++++++++++--- app/Services/Invoice/MarkPaid.php | 3 ++- app/Services/Payment/UpdateInvoicePayment.php | 2 -- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/Services/Invoice/ApplyPayment.php b/app/Services/Invoice/ApplyPayment.php index da4dbe96449e..de82966abb09 100644 --- a/app/Services/Invoice/ApplyPayment.php +++ b/app/Services/Invoice/ApplyPayment.php @@ -126,7 +126,7 @@ class ApplyPayment extends AbstractService }); - $this->invoice->service()->applyNumber()->save(); + $this->invoice->service()->applyNumber()->workFlow()->save(); return $this->invoice; } diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 8fa43294be92..9f7f7bdf6b74 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -20,6 +20,7 @@ use App\Models\Expense; use App\Models\Invoice; use App\Models\Payment; use App\Models\Task; +use App\Repositories\BaseRepository; use App\Services\Client\ClientService; use App\Services\Invoice\UpdateReminder; use App\Utils\Ninja; @@ -271,9 +272,8 @@ class InvoiceService { if ((int)$this->invoice->balance == 0) { - InvoiceWorkflowSettings::dispatchNow($this->invoice); - - $this->setStatus(Invoice::STATUS_PAID); + $this->setStatus(Invoice::STATUS_PAID)->workFlow(); + // InvoiceWorkflowSettings::dispatchNow($this->invoice); } if ($this->invoice->balance > 0 && $this->invoice->balance < $this->invoice->amount) { @@ -449,6 +449,18 @@ class InvoiceService return $this; } + public function workFlow() + { + + if ($this->invoice->status_id == Invoice::STATUS_PAID && $this->invoice->client->getSetting('auto_archive_invoice')) { + /* Throws: Payment amount xxx does not match invoice totals. */ + $base_repository = new BaseRepository(); + $base_repository->archive($this->invoice); + } + + return $this; + } + /** * Saves the invoice. * @return Invoice object diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 7cbd52736242..8b8d700c6b1d 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -95,7 +95,8 @@ class MarkPaid extends AbstractService ->updatePaidToDate($payment->amount) ->save(); - InvoiceWorkflowSettings::dispatchNow($this->invoice); + $this->invoice->service()->workFlow()->save(); + // InvoiceWorkflowSettings::dispatchNow($this->invoice); return $this->invoice; } diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 1d70921764ff..e344050cfa63 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -85,8 +85,6 @@ class UpdateInvoicePayment ->deletePdf() ->save(); - InvoiceWorkflowSettings::dispatchNow($invoice); - event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); });