From 79383b8811f9d54085c5f110491ff054aab7a7bd Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 May 2023 16:33:40 +1000 Subject: [PATCH] Fixes for tests --- app/DataMapper/Tax/BaseRule.php | 11 +++-- app/DataMapper/Tax/DE/Rule.php | 20 +++++---- tests/Unit/Tax/EuTaxTest.php | 79 ++++++++++++++++++++++++++++++++- 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index 0f61cd8212e8..a184f2ac211b 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -147,6 +147,8 @@ class BaseRule implements RuleInterface $this->configTaxData(); + nlog($this->invoice->tax_data); + $this->tax_data = new Response($this->invoice->tax_data); return $this; @@ -176,7 +178,8 @@ class BaseRule implements RuleInterface * Destination - Client Tax Data * */ - $tax_data = new Response([]); + // $tax_data = new Response([]); + $tax_data = false; if($this->seller_region == 'US' && $this->client_region == 'US'){ @@ -215,9 +218,9 @@ class BaseRule implements RuleInterface } /** Applies the tax data to the invoice */ - if($this->invoice instanceof Invoice) { + if($this->invoice instanceof Invoice && $tax_data) { - $this->invoice->tax_data = $tax_data; + $this->invoice->tax_data = $tax_data ; if(\DB::transactionLevel() == 0) $this->invoice->saveQuietly(); @@ -273,7 +276,7 @@ class BaseRule implements RuleInterface public function defaultForeign(): self { - if($this->client_region == 'US') { + if($this->client_region == 'US' && isset($this->tax_data?->taxSales)) { $this->tax_rate1 = $this->tax_data->taxSales * 100; $this->tax_name1 = "{$this->tax_data->geoState} Sales Tax"; diff --git a/app/DataMapper/Tax/DE/Rule.php b/app/DataMapper/Tax/DE/Rule.php index 288a5aec2e6a..7cb58bc938be 100644 --- a/app/DataMapper/Tax/DE/Rule.php +++ b/app/DataMapper/Tax/DE/Rule.php @@ -30,10 +30,10 @@ class Rule extends BaseRule implements RuleInterface public bool $eu_business_tax_exempt = true; /** @var bool $foreign_business_tax_exempt */ - public bool $foreign_business_tax_exempt = true; + public bool $foreign_business_tax_exempt = false; /** @var bool $foreign_consumer_tax_exempt */ - public bool $foreign_consumer_tax_exempt = true; + public bool $foreign_consumer_tax_exempt = false; /** @var float $tax_rate */ public float $tax_rate = 0; @@ -223,38 +223,42 @@ class Rule extends BaseRule implements RuleInterface public function calculateRates(): self { if ($this->client->is_tax_exempt) { - // nlog("tax exempt"); + nlog("tax exempt"); $this->tax_rate = 0; $this->reduced_tax_rate = 0; } elseif($this->client_subregion != $this->client->company->tax_data->seller_subregion && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt) { - // nlog("euro zone and tax exempt"); + nlog("euro zone and tax exempt"); $this->tax_rate = 0; $this->reduced_tax_rate = 0; } elseif(!in_array($this->client_subregion, $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt { - // nlog("foreign and tax exempt"); + nlog("foreign and tax exempt"); $this->tax_rate = 0; $this->reduced_tax_rate = 0; } + elseif(!in_array($this->client_subregion, $this->eu_country_codes)) + { + $this->defaultForeign(); + } elseif(in_array($this->client_subregion, $this->eu_country_codes) && !$this->client->has_valid_vat_number) //eu country / no valid vat { if(($this->client->company->tax_data->seller_subregion != $this->client_subregion) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold) { - // nlog("eu zone with sales above threshold"); + nlog("eu zone with sales above threshold"); $this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->tax_rate; $this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->reduced_tax_rate; } else { - // nlog("EU with intra-community supply ie DE to DE"); + nlog("EU with intra-community supply ie DE to DE"); $this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate; $this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate; } } else { - // nlog("default tax"); + nlog("default tax"); $this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate; $this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate; } diff --git a/tests/Unit/Tax/EuTaxTest.php b/tests/Unit/Tax/EuTaxTest.php index 2087cec9f57f..7f88675347df 100644 --- a/tests/Unit/Tax/EuTaxTest.php +++ b/tests/Unit/Tax/EuTaxTest.php @@ -70,6 +70,8 @@ class EuTaxTest extends TestCase 'user_id' => $this->user->id, 'company_id' => $company->id, 'country_id' => 840, + 'state' => 'CA', + 'postal_code' => '90210', 'shipping_country_id' => 840, 'has_valid_vat_number' => false, 'is_tax_exempt' => false, @@ -113,13 +115,88 @@ class EuTaxTest extends TestCase 'taxSales' => 0.07, ]), ]); - + $invoice = $invoice->calc()->getInvoice()->service()->markSent()->save(); $this->assertEquals(107, $invoice->amount); } + public function testEuToAuTaxCalculation() + { + + $settings = CompanySettings::defaults(); + $settings->country_id = '276'; // germany + + $tax_data = new TaxModel(); + $tax_data->seller_subregion = 'DE'; + $tax_data->regions->EU->has_sales_above_threshold = false; + $tax_data->regions->EU->tax_all_subregions = true; + $tax_data->regions->AU->tax_all_subregions = true; + $tax_data->regions->AU->has_sales_above_threshold = true; + + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + 'calculate_taxes' => true, + ]); + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 36, + 'shipping_country_id' => 36, + 'has_valid_vat_number' => false, + 'is_tax_exempt' => false, + // 'tax_data' => new Response([ + // 'geoState' => 'CA', + // 'taxSales' => 0.07, + // ]), + ]); + + $invoice = Invoice::factory()->create([ + 'company_id' => $company->id, + 'client_id' => $client->id, + 'status_id' => 1, + 'user_id' => $this->user->id, + 'uses_inclusive_taxes' => false, + 'discount' => 0, + 'line_items' => [ + [ + 'product_key' => 'Test', + 'notes' => 'Test', + 'cost' => 100, + 'quantity' => 1, + 'tax_name1' => '', + 'tax_rate1' => 0, + 'tax_name2' => '', + 'tax_rate2' => 0, + 'tax_name3' => '', + 'tax_rate3' => 0, + 'type_id' => '1', + 'tax_id' => Product::PRODUCT_TYPE_PHYSICAL, + ], + ], + 'tax_rate1' => 0, + 'tax_rate2' => 0, + 'tax_rate3' => 0, + 'tax_name1' => '', + 'tax_name2' => '', + 'tax_name3' => '', + // 'tax_data' => new Response([ + // 'geoState' => 'CA', + // 'taxSales' => 0.07, + // ]), + ]); + + $invoice = $invoice->calc()->getInvoice()->service()->markSent()->save(); + + $this->assertEquals(110, $invoice->amount); + + } + + public function testInvoiceTaxCalcDetoBeNoVat() {