From 766a1955ee2e41a156e580fd00705f717027db49 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 14 Aug 2024 23:34:17 +1000 Subject: [PATCH] Ensure no recalculations once locked --- app/DataMapper/Tax/BaseRule.php | 37 ++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index 4448c7525898..016a38840db9 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -130,9 +130,8 @@ class BaseRule implements RuleInterface return $this; } - public function shouldCalcTax(): bool - { - return $this->should_calc_tax; + public function shouldCalcTax(): bool { + return $this->should_calc_tax && $this->checkIfInvoiceLocked(); } /** * Initializes the tax rule for the entity. @@ -400,4 +399,36 @@ class BaseRule implements RuleInterface return ! in_array($iso_3166_2, array_merge($this->eu_country_codes, array_keys($this->region_codes))); } + private function checkIfInvoiceLocked(): bool + { + $lock_invoices = $this->client->getSetting('lock_invoices'); + + switch ($lock_invoices) { + case 'off': + return true; + case 'when_sent': + if ($this->invoice->status_id == Invoice::STATUS_SENT) { + return false; + } + + return true; + + case 'when_paid': + if ($this->invoice->status_id == Invoice::STATUS_PAID) { + return false; + } + + return true; + + //if now is greater than the end of month the invoice was dated - do not modify + case 'end_of_month': + if(\Carbon\Carbon::parse($this->invoice->date)->setTimezone($this->invoice->company->timezone()->name)->endOfMonth()->lte(now())) { + return false; + } + return true; + default: + return true; + } + } + }