From fa9f9ab462baffbb06374e3f4f97c76a4a46f808 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 May 2023 16:02:33 +1000 Subject: [PATCH] Fixes for tests --- app/DataMapper/Tax/BaseRule.php | 7 +++- app/DataMapper/Tax/US/Rule.php | 2 +- tests/Unit/Tax/SumTaxTest.php | 65 +++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index 5674a4d57717..0f61cd8212e8 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -215,9 +215,12 @@ class BaseRule implements RuleInterface } /** Applies the tax data to the invoice */ - if($this->invoice instanceof Invoice && \DB::transactionLevel() == 0) { + if($this->invoice instanceof Invoice) { + $this->invoice->tax_data = $tax_data; - $this->invoice->saveQuietly(); + + if(\DB::transactionLevel() == 0) + $this->invoice->saveQuietly(); } return $this; diff --git a/app/DataMapper/Tax/US/Rule.php b/app/DataMapper/Tax/US/Rule.php index 2a4e46b1dc33..9538a4f3085e 100644 --- a/app/DataMapper/Tax/US/Rule.php +++ b/app/DataMapper/Tax/US/Rule.php @@ -156,7 +156,7 @@ class Rule extends BaseRule implements RuleInterface */ public function default($item): self { - + if($this->tax_data?->stateSalesTax == 0) { $this->tax_rate1 = $this->invoice->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->tax_rate; diff --git a/tests/Unit/Tax/SumTaxTest.php b/tests/Unit/Tax/SumTaxTest.php index ec3b3047237f..c57add9afac4 100644 --- a/tests/Unit/Tax/SumTaxTest.php +++ b/tests/Unit/Tax/SumTaxTest.php @@ -13,6 +13,7 @@ namespace Tests\Unit\Tax; use Tests\TestCase; use App\Models\Client; +use App\Models\Company; use App\Models\Invoice; use App\Models\Product; use Tests\MockAccountData; @@ -20,6 +21,7 @@ use App\DataMapper\InvoiceItem; use App\DataMapper\Tax\TaxData; use App\Factory\InvoiceFactory; use App\DataMapper\Tax\TaxModel; +use App\DataMapper\CompanySettings; use App\DataMapper\Tax\ZipTax\Response; use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -92,26 +94,32 @@ class SumTaxTest extends TestCase public function testCalcInvoiceNoTax() { + $settings = CompanySettings::defaults(); + $settings->country_id = '840'; // germany $tax_data = new TaxModel(); $tax_data->seller_subregion = 'CA'; $tax_data->regions->US->has_sales_above_threshold = true; $tax_data->regions->US->tax_all_subregions = true; - $this->company->calculate_taxes = false; - $this->company->tax_data = $tax_data; - $this->company->save(); - - $tax_data = new TaxData($this->response); + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + 'calculate_taxes' => false, + 'origin_tax_data' => new Response($this->resp), + ]); $client = Client::factory()->create([ 'user_id' => $this->user->id, - 'company_id' => $this->company->id, + 'company_id' => $company->id, 'country_id' => 840, - 'tax_data' => $tax_data, + 'state' => 'CA', + 'postal_code' => '90210', + 'tax_data' => new Response($this->resp), ]); - $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice = InvoiceFactory::create($company->id, $this->user->id); $invoice->client_id = $client->id; $invoice->uses_inclusive_taxes = false; @@ -143,35 +151,39 @@ class SumTaxTest extends TestCase public function testCalcInvoiceTax() { - + $settings = CompanySettings::defaults(); + $settings->country_id = '840'; + $settings->currency_id = '1'; $tax_data = new TaxModel(); $tax_data->seller_subregion = 'CA'; $tax_data->regions->US->has_sales_above_threshold = true; $tax_data->regions->US->tax_all_subregions = true; - $this->company->calculate_taxes = true; - $this->company->tax_data = $tax_data; - $this->company->save(); + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + 'calculate_taxes' => true, + 'origin_tax_data' => new Response($this->resp), + ]); -$tax_data = new TaxData($this->response); -$client = Client::factory()->create([ - 'user_id' => $this->user->id, - 'company_id' => $this->company->id, - 'country_id' => 840, - 'tax_data' => $tax_data, -]); + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 840, + 'postal_code' => '90210', + 'state' => 'CA', + 'tax_data' => new Response($this->resp), + ]); -$invoice = InvoiceFactory::create($this->company->id, $this->user->id); -$invoice->client_id = $client->id; -$invoice->uses_inclusive_taxes = false; + $invoice = InvoiceFactory::create($company->id, $this->user->id); + $invoice->client_id = $client->id; + $invoice->uses_inclusive_taxes = false; -$line_items = []; + $line_items = []; -$invoice->tax_data = $tax_data; - - $line_item = new InvoiceItem; $line_item->quantity = 1; $line_item->cost = 10; @@ -187,7 +199,6 @@ $invoice->tax_data = $tax_data; $line_items = $invoice->line_items; - $this->assertEquals(10.88, $invoice->amount); $this->assertEquals("CA Sales Tax", $line_items[0]->tax_name1); $this->assertEquals(8.75, $line_items[0]->tax_rate1);