From bd7722f6f039c9d799367fd7eb161d44fdcca04a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 3 Apr 2023 07:48:59 +1000 Subject: [PATCH] Refactor for taxes --- app/DataMapper/Tax/BaseRule.php | 2 +- app/DataMapper/Tax/DE/Rule.php | 8 +++- app/DataMapper/Tax/RuleInterface.php | 2 +- app/DataMapper/Tax/US/Rule.php | 38 ++++++++++--------- app/Helpers/Invoice/InvoiceItemSum.php | 8 +--- ...054758_add_client_is_exempt_from_taxes.php | 9 +++++ tests/Unit/Tax/UsTaxTest.php | 1 - 7 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index 26d01eb57f7d..39bf2f4833d0 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -99,7 +99,7 @@ class BaseRule implements RuleInterface return $this; } - public function tax(): self + public function tax($product_tax_type): self { return $this; } diff --git a/app/DataMapper/Tax/DE/Rule.php b/app/DataMapper/Tax/DE/Rule.php index 50d2c0d48a57..d45d0d74293b 100644 --- a/app/DataMapper/Tax/DE/Rule.php +++ b/app/DataMapper/Tax/DE/Rule.php @@ -81,10 +81,14 @@ class Rule extends BaseRule implements RuleInterface } //need to add logic here to capture if - public function tax(): self + public function tax($type): self { - if($this->client->is_tax_exempt) + + if ($this->client->is_tax_exempt) return $this->taxExempt(); + + if ($type) + return $this->taxByType($type); $this->tax_rate1 = $this->vat_rate; $this->tax_name1 = "MwSt."; diff --git a/app/DataMapper/Tax/RuleInterface.php b/app/DataMapper/Tax/RuleInterface.php index 3ed6f8d06e4f..c210f2809c6a 100644 --- a/app/DataMapper/Tax/RuleInterface.php +++ b/app/DataMapper/Tax/RuleInterface.php @@ -18,7 +18,7 @@ interface RuleInterface { public function init(); - public function tax(); + public function tax(mixed $type); public function taxByType($type); diff --git a/app/DataMapper/Tax/US/Rule.php b/app/DataMapper/Tax/US/Rule.php index 5be5aab6ddda..8dabf504dea5 100644 --- a/app/DataMapper/Tax/US/Rule.php +++ b/app/DataMapper/Tax/US/Rule.php @@ -55,13 +55,22 @@ class Rule implements RuleInterface return $this; } - public function tax(): self + public function tax($type): self { - if($this->client->is_tax_exempt) + + if ($this->client->is_tax_exempt) { return $this->taxExempt(); + } + else if($this->client->company->tax_data->regions->US->tax_all){ - $this->tax_rate1 = $this->tax_data->taxSales * 100; - $this->tax_name1 = "{$this->tax_data->geoState} Sales Tax"; + $this->tax_rate1 = $this->tax_data->taxSales * 100; + $this->tax_name1 = "{$this->tax_data->geoState} Sales Tax"; + + return $this; + } + + if($type) + return $this->taxByType($type); return $this; @@ -72,11 +81,6 @@ 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_TYPE_EXEMPT => $this->taxExempt(), Product::PRODUCT_TYPE_DIGITAL => $this->taxDigital(), @@ -101,7 +105,7 @@ class Rule implements RuleInterface public function taxDigital(): self { - $this->tax(); + $this->default(); return $this; } @@ -109,7 +113,7 @@ class Rule implements RuleInterface public function taxService(): self { if($this->tax_data->txbService == 'Y') - $this->tax(); + $this->default(); return $this; } @@ -117,30 +121,30 @@ class Rule implements RuleInterface public function taxShipping(): self { if($this->tax_data->txbFreight == 'Y') - $this->tax(); + $this->default(); return $this; } public function taxPhysical(): self { - $this->tax(); + $this->default(); return $this; } public function default(): self { - - $this->tax_name1 = 'Tax Exempt'; - $this->tax_rate1 = 0; + + $this->tax_rate1 = $this->tax_data->taxSales * 100; + $this->tax_name1 = "{$this->tax_data->geoState} Sales Tax"; return $this; } public function taxReduced(): self { - $this->tax(); + $this->default(); return $this; } diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 732f343ee8b9..6e9cce110e71 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -199,12 +199,8 @@ class InvoiceItemSum */ private function calcTaxesAutomatically(): self { - if ($this->invoice->company->tax_all_products || ( property_exists($this->item, 'tax_id') && $this->item->tax_id != '')) { - $this->rule->tax(); - } else { - $this->rule->init()->taxByType($this->item->tax_id); - } - + $this->rule->tax($this->item->tax_id ?? null); + $this->item->tax_name1 = $this->rule->tax_name1; $this->item->tax_rate1 = $this->rule->tax_rate1; diff --git a/database/migrations/2023_03_24_054758_add_client_is_exempt_from_taxes.php b/database/migrations/2023_03_24_054758_add_client_is_exempt_from_taxes.php index ee59526750af..0f340053f1b4 100644 --- a/database/migrations/2023_03_24_054758_add_client_is_exempt_from_taxes.php +++ b/database/migrations/2023_03_24_054758_add_client_is_exempt_from_taxes.php @@ -1,5 +1,6 @@ tax_data = null; $company->save(); }); + + Client::query() + ->cursor() + ->each(function ($client) { + $client->tax_data = null; + $client->save(); + }); + } /** diff --git a/tests/Unit/Tax/UsTaxTest.php b/tests/Unit/Tax/UsTaxTest.php index e5791ef9fdab..82f8dc9777ec 100644 --- a/tests/Unit/Tax/UsTaxTest.php +++ b/tests/Unit/Tax/UsTaxTest.php @@ -204,7 +204,6 @@ class UsTaxTest extends TestCase $client->save(); $company = $invoice->company; - $company->tax_all_products = false; $tax_data = $company->tax_data; $tax_data->regions->US->has_sales_above_threshold = true;