From b0b6106ee5758fb428c18566b5ebd87d56148972 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Thu, 15 Aug 2024 14:38:52 +0100 Subject: [PATCH] Update tax zone actions to use active column --- packages/core/database/factories/TaxZoneFactory.php | 2 +- packages/core/src/Actions/Taxes/GetTaxZoneCountry.php | 6 +++++- packages/core/src/Actions/Taxes/GetTaxZonePostcode.php | 10 ++++++++-- packages/core/src/Actions/Taxes/GetTaxZoneState.php | 2 ++ tests/core/Feature/Drivers/SystemTaxDriverTest.php | 3 ++- .../core/Unit/Actions/Taxes/GetTaxZoneCountryTest.php | 3 ++- .../core/Unit/Actions/Taxes/GetTaxZonePostcodeTest.php | 4 +++- tests/core/Unit/Actions/Taxes/GetTaxZoneTest.php | 4 +++- 8 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/core/database/factories/TaxZoneFactory.php b/packages/core/database/factories/TaxZoneFactory.php index aa87c42da7..e64de5c89c 100644 --- a/packages/core/database/factories/TaxZoneFactory.php +++ b/packages/core/database/factories/TaxZoneFactory.php @@ -15,7 +15,7 @@ public function definition(): array 'name' => $this->faker->name, 'zone_type' => $this->faker->randomElement(['country', 'postcode', 'state']), 'price_display' => $this->faker->randomElement(['tax_inclusive', 'tax_exclusive']), - 'active' => $this->faker->boolean, + 'active' => true, 'default' => true, ]; } diff --git a/packages/core/src/Actions/Taxes/GetTaxZoneCountry.php b/packages/core/src/Actions/Taxes/GetTaxZoneCountry.php index a73c444e90..e986fbd958 100644 --- a/packages/core/src/Actions/Taxes/GetTaxZoneCountry.php +++ b/packages/core/src/Actions/Taxes/GetTaxZoneCountry.php @@ -2,6 +2,7 @@ namespace Lunar\Actions\Taxes; +use Illuminate\Database\Eloquent\Builder; use Lunar\Models\TaxZoneCountry; class GetTaxZoneCountry @@ -26,6 +27,9 @@ public function execute($countryId) */ protected function getZone(int $countryId) { - return TaxZoneCountry::whereCountryId($countryId)->first(); + return TaxZoneCountry::whereHas( + 'taxZone', + fn (Builder $query) => $query->where('active', true) + )->whereCountryId($countryId)->first(); } } diff --git a/packages/core/src/Actions/Taxes/GetTaxZonePostcode.php b/packages/core/src/Actions/Taxes/GetTaxZonePostcode.php index 1ec3419160..7f6836c96f 100644 --- a/packages/core/src/Actions/Taxes/GetTaxZonePostcode.php +++ b/packages/core/src/Actions/Taxes/GetTaxZonePostcode.php @@ -2,6 +2,7 @@ namespace Lunar\Actions\Taxes; +use Illuminate\Database\Eloquent\Builder; use Lunar\Models\TaxZonePostcode; class GetTaxZonePostcode @@ -49,11 +50,16 @@ protected function getZoneMatches($postcode) { $postcode = (string) $postcode; - if ($zone = TaxZonePostcode::wherePostcode($postcode)->first()) { + $query = TaxZonePostcode::whereHas( + 'taxZone', + fn (Builder $query) => $query->where('active', true) + ); + + if ($zone = $query->clone()->wherePostcode($postcode)->first()) { return $zone; } - return TaxZonePostcode::where('postcode', 'LIKE', "{$postcode[0]}%")->get(); + return $query->clone()->where('postcode', 'LIKE', "{$postcode[0]}%")->get(); } /** diff --git a/packages/core/src/Actions/Taxes/GetTaxZoneState.php b/packages/core/src/Actions/Taxes/GetTaxZoneState.php index d781e3f26c..774ff0865a 100644 --- a/packages/core/src/Actions/Taxes/GetTaxZoneState.php +++ b/packages/core/src/Actions/Taxes/GetTaxZoneState.php @@ -37,6 +37,8 @@ protected function getZoneMatches($state) return $query ->where('name', $state) ->orWhere('code', $state); + })->whereHas('taxZone', function ($query) { + return $query->where('active', true); })->first(); if ($stateZone) { diff --git a/tests/core/Feature/Drivers/SystemTaxDriverTest.php b/tests/core/Feature/Drivers/SystemTaxDriverTest.php index 7565b795f6..ee6c8fb3c1 100644 --- a/tests/core/Feature/Drivers/SystemTaxDriverTest.php +++ b/tests/core/Feature/Drivers/SystemTaxDriverTest.php @@ -1,6 +1,7 @@ group('taxes'); test('can set shipping address', function () { $address = Address::factory()->create(); diff --git a/tests/core/Unit/Actions/Taxes/GetTaxZoneCountryTest.php b/tests/core/Unit/Actions/Taxes/GetTaxZoneCountryTest.php index 1e39fa092d..1b25f66a97 100644 --- a/tests/core/Unit/Actions/Taxes/GetTaxZoneCountryTest.php +++ b/tests/core/Unit/Actions/Taxes/GetTaxZoneCountryTest.php @@ -1,11 +1,12 @@ group('taxes'); test('can match country id', function () { $belgium = Country::factory()->create([ diff --git a/tests/core/Unit/Actions/Taxes/GetTaxZonePostcodeTest.php b/tests/core/Unit/Actions/Taxes/GetTaxZonePostcodeTest.php index 4b41b63a43..cf15c01267 100644 --- a/tests/core/Unit/Actions/Taxes/GetTaxZonePostcodeTest.php +++ b/tests/core/Unit/Actions/Taxes/GetTaxZonePostcodeTest.php @@ -1,10 +1,12 @@ group('taxes'); test('can match exact postcode', function () { $uk = TaxZonePostcode::factory()->create([ diff --git a/tests/core/Unit/Actions/Taxes/GetTaxZoneTest.php b/tests/core/Unit/Actions/Taxes/GetTaxZoneTest.php index 5020283647..4ddc2c0e2e 100644 --- a/tests/core/Unit/Actions/Taxes/GetTaxZoneTest.php +++ b/tests/core/Unit/Actions/Taxes/GetTaxZoneTest.php @@ -1,6 +1,7 @@ group('taxes'); test('can prioritize taxzones', function () { $postcode = 'SW1A 0AA';