Skip to content

Commit

Permalink
Hotfix - Fix taxbreakdown state update (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson authored Oct 13, 2023
1 parent 80fa3a1 commit f0dd629
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
31 changes: 21 additions & 10 deletions packages/core/database/state/ConvertTaxbreakdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Lunar\Database\State;

use Illuminate\Support\Facades\Schema;
use Lunar\Base\ValueObjects\Cart\TaxBreakdown;
use Lunar\Base\ValueObjects\Cart\TaxBreakdownAmount;
use Lunar\DataTypes\Price;
use Lunar\Models\Order;
Expand All @@ -24,12 +23,8 @@ public function run()

Order::chunk(500, function ($orders) {
foreach ($orders as $order) {
if (is_a($order->tax_breakdown, TaxBreakdown::class)) {
continue;
}
// Get the raw tax_breakdown
$breakdown = json_decode($order->getRawOriginal('tax_breakdown'), true);

$amounts = collect($breakdown)->map(function ($row) use ($order) {
return new TaxBreakdownAmount(
price: new Price($row['total'], $order->currency),
Expand All @@ -47,10 +42,6 @@ public function run()

OrderLine::chunk(500, function ($orderLines) {
foreach ($orderLines as $orderLine) {
if (is_a($orderLine->tax_breakdown, TaxBreakdown::class)) {
continue;
}

// Get the raw tax_breakdown
$breakdown = json_decode($orderLine->getRawOriginal('tax_breakdown'), true);

Expand All @@ -74,6 +65,26 @@ protected function canRun()
{
$prefix = config('lunar.database.table_prefix');

return Schema::hasTable("{$prefix}orders") && Schema::hasTable("{$prefix}order_lines");
$hasSchema = Schema::hasTable("{$prefix}orders") && Schema::hasTable("{$prefix}order_lines");

if (!$hasSchema) {
return false;
}

// Grab an order and determine whether the tax breakdown has already been converted.
// This will save us having to run the command and check each order.
$order = Order::first();

if (!$order) {
return false;
}

$breakdownItem = json_decode($order->getRawOriginal('tax_breakdown'), true)[0] ?? null;

if (!$breakdownItem) {
return false;
}

return $breakdownItem['total'] ?? false;
}
}
64 changes: 64 additions & 0 deletions packages/core/tests/Database/State/ConvertTaxBreakdownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Lunar\Tests\Database\State;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Lunar\Database\State\ConvertTaxbreakdown;
use Lunar\Facades\DB;
use Lunar\Models\Channel;
use Lunar\Models\Currency;
use Lunar\Models\Language;
use Lunar\Tests\TestCase;

/**
* @group database.state
*/
class ConvertTaxBreakdownTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function can_run()
{
Storage::fake('local');

Language::factory()->create([
'default' => true,
]);

$channel = Channel::factory()->create([
'default' => true,
]);

Currency::factory()->create([
'code' => 'GBP',
]);

DB::table('lunar_orders')->insert([
'channel_id' => $channel->id,
'new_customer' => false,
'user_id' => null,
'status' => 'awaiting-payment',
'reference' => 123123,
'sub_total' => 400,
'discount_total' => 0,
'shipping_total' => 0,
'tax_breakdown' => '[{"total": 333, "identifier": "tax_rate_1", "percentage": 20, "description": "VAT"}]',
'tax_total' => 200,
'total' => 100,
'notes' => null,
'currency_code' => 'GBP',
'compare_currency_code' => 'GBP',
'exchange_rate' => 1,
'meta' => '[]',
]);

(new ConvertTaxbreakdown)->run();

$this->assertDatabaseHas('lunar_orders', [
'tax_breakdown' => '[{"description":"VAT","identifier":"tax_rate_1","percentage":20,"value":333,"currency_code":"GBP"}]'
]);

}
}

0 comments on commit f0dd629

Please sign in to comment.