From b94743f42de601a8999cd6d72988cdf53566ded1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 27 Mar 2023 07:57:29 +1100 Subject: [PATCH] Refactor for taxes --- app/DataMapper/Tax/de/Rule.php | 11 ++++++++ app/DataMapper/Tax/us/Rule.php | 8 ++++++ app/Helpers/Invoice/InvoiceItemSum.php | 37 ++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/app/DataMapper/Tax/de/Rule.php b/app/DataMapper/Tax/de/Rule.php index 55d47ef208cc..ee995c724de3 100644 --- a/app/DataMapper/Tax/de/Rule.php +++ b/app/DataMapper/Tax/de/Rule.php @@ -103,6 +103,8 @@ class Rule implements RuleInterface protected ?Client $client; + protected ?Response $tax_data; + public function __construct() { } @@ -123,6 +125,10 @@ class Rule implements RuleInterface public function tax(): self { + if($this->client->is_tax_exempt) + return $this->taxExempt(); + + $this->tax_name1 = $this->vat_rate; $this->tax_rate1 = "VAT"; @@ -132,6 +138,11 @@ class Rule implements RuleInterface public function taxByType(?int $product_tax_type): self { + + if ($this->client->is_tax_exempt) { + return $this->taxExempt(); + } + if(!$product_tax_type) return $this; diff --git a/app/DataMapper/Tax/us/Rule.php b/app/DataMapper/Tax/us/Rule.php index 7454f6b0ca3a..370ac0c3da82 100644 --- a/app/DataMapper/Tax/us/Rule.php +++ b/app/DataMapper/Tax/us/Rule.php @@ -105,6 +105,9 @@ class Rule implements RuleInterface public function tax(): self { + if($this->client->is_tax_exempt) + return $this->taxExempt(); + $this->tax_rate1 = $this->tax_data->taxSales * 100; $this->tax_name1 = "{$this->tax_data->geoState} Sales Tax"; @@ -117,6 +120,11 @@ class Rule implements RuleInterface if(!$product_tax_type) return $this; + + if ($this->client->is_tax_exempt) { + return $this->taxExempt(); + } + match($product_tax_type){ Product::PRODUCT_TAX_EXEMPT => $this->taxExempt(), Product::PRODUCT_TYPE_DIGITAL => $this->taxDigital(), diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 99de411c0be7..b35ad245d211 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -25,6 +25,38 @@ class InvoiceItemSum use Discounter; use Taxer; + private array $tax_jurisdictions = [ + 'AT', // Austria + 'BE', // Belgium + 'BG', // Bulgaria + 'CY', // Cyprus + 'CZ', // Czech Republic + 'DE', // Germany + 'DK', // Denmark + 'EE', // Estonia + 'ES', // Spain + 'FI', // Finland + 'FR', // France + 'GR', // Greece + 'HR', // Croatia + 'HU', // Hungary + 'IE', // Ireland + 'IT', // Italy + 'LT', // Lithuania + 'LU', // Luxembourg + 'LV', // Latvia + 'MT', // Malta + 'NL', // Netherlands + 'PL', // Poland + 'PT', // Portugal + 'RO', // Romania + 'SE', // Sweden + 'SI', // Slovenia + 'SK', // Slovakia + + 'US', //USA + ]; + protected $invoice; private $items; @@ -102,18 +134,19 @@ class InvoiceItemSum private function shouldCalculateTax(): self { - if (!$this->invoice->company->calculate_taxes || $this->client->is_tax_exempt) { + if (!$this->invoice->company->calculate_taxes) { $this->calc_tax = false; return $this; } - if (in_array($this->client->country->iso_3166_2, ['US'])) { //only calculate for USA + if (in_array($this->client->country->iso_3166_2, $this->tax_jurisdictions)) { //only calculate for supported tax jurisdictions $class = "App\DataMapper\Tax\\".strtolower($this->client->country->iso_3166_2)."\\Rule"; $tax_data = new Response($this->invoice->tax_data); $this->rule = new $class(); $this->rule->setTaxData($tax_data); + $this->rule->setClient($this->client); $this->calc_tax = true; return $this;