Skip to content

Commit

Permalink
Update tax zone actions to use active column
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Aug 15, 2024
1 parent c343ac3 commit b0b6106
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core/database/factories/TaxZoneFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/Actions/Taxes/GetTaxZoneCountry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lunar\Actions\Taxes;

use Illuminate\Database\Eloquent\Builder;
use Lunar\Models\TaxZoneCountry;

class GetTaxZoneCountry
Expand All @@ -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();
}
}
10 changes: 8 additions & 2 deletions packages/core/src/Actions/Taxes/GetTaxZonePostcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lunar\Actions\Taxes;

use Illuminate\Database\Eloquent\Builder;
use Lunar\Models\TaxZonePostcode;

class GetTaxZonePostcode
Expand Down Expand Up @@ -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();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/Actions/Taxes/GetTaxZoneState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion tests/core/Feature/Drivers/SystemTaxDriverTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Support\Facades\Config;
use Lunar\Base\ValueObjects\Cart\TaxBreakdown;
Expand All @@ -14,7 +15,7 @@
use Lunar\Models\TaxRateAmount;
use Lunar\Models\TaxZone;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class)->group('taxes');

test('can set shipping address', function () {
$address = Address::factory()->create();
Expand Down
3 changes: 2 additions & 1 deletion tests/core/Unit/Actions/Taxes/GetTaxZoneCountryTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Lunar\Actions\Taxes\GetTaxZoneCountry;
use Lunar\Models\Country;
use Lunar\Models\TaxZoneCountry;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class)->group('taxes');

test('can match country id', function () {
$belgium = Country::factory()->create([
Expand Down
4 changes: 3 additions & 1 deletion tests/core/Unit/Actions/Taxes/GetTaxZonePostcodeTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Lunar\Actions\Taxes\GetTaxZonePostcode;
use Lunar\Models\TaxZonePostcode;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class)
->group('taxes');

test('can match exact postcode', function () {
$uk = TaxZonePostcode::factory()->create([
Expand Down
4 changes: 3 additions & 1 deletion tests/core/Unit/Actions/Taxes/GetTaxZoneTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Lunar\Actions\Taxes\GetTaxZone;
use Lunar\Models\Address;
use Lunar\Models\Country;
Expand All @@ -10,7 +11,8 @@
use Lunar\Models\TaxZonePostcode;
use Lunar\Models\TaxZoneState;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class)
->group('taxes');

test('can prioritize taxzones', function () {
$postcode = 'SW1A 0AA';
Expand Down

0 comments on commit b0b6106

Please sign in to comment.