From e07e327790ef70e6cc87ed7b06a5f5e6e73a6111 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 30 Jul 2024 09:59:03 +1000 Subject: [PATCH] migrations for tax models --- app/DataMapper/Tax/TaxModel.php | 31 +++++++++++++--- ..._235430_2024_30_07_tax_model_migration.php | 36 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2024_07_29_235430_2024_30_07_tax_model_migration.php diff --git a/app/DataMapper/Tax/TaxModel.php b/app/DataMapper/Tax/TaxModel.php index 062a52c16c3e..c5d4afd185bc 100644 --- a/app/DataMapper/Tax/TaxModel.php +++ b/app/DataMapper/Tax/TaxModel.php @@ -17,7 +17,7 @@ class TaxModel public string $seller_subregion = 'CA'; /** @var string $version */ - public string $version = 'alpha'; + public string $version = 'beta'; /** @var object $regions */ public object $regions; @@ -28,15 +28,38 @@ class TaxModel * @param TaxModel $model * @return void */ - public function __construct(public ?TaxModel $model = null) + public function __construct(public mixed $model = null) { - if(!$this->model) { + if(!$model) { $this->regions = $this->init(); } else { - $this->regions = $model; + + //@phpstan-ignore-next-line + foreach($model as $key => $value) { + $this->{$key} = $value; + } + } + $this->migrate(); + } + + public function migrate(): self + { + + if($this->version == 'alpha') + { + $this->regions->EU->subregions->PL = new \stdClass(); + $this->regions->EU->subregions->PL->tax_rate = 23; + $this->regions->EU->subregions->PL->tax_name = 'VAT'; + $this->regions->EU->subregions->PL->reduced_tax_rate = 8; + $this->regions->EU->subregions->PL->apply_tax = false; + + $this->version = 'beta'; + } + + return $this; } /** diff --git a/database/migrations/2024_07_29_235430_2024_30_07_tax_model_migration.php b/database/migrations/2024_07_29_235430_2024_30_07_tax_model_migration.php new file mode 100644 index 000000000000..e542bcd2be8a --- /dev/null +++ b/database/migrations/2024_07_29_235430_2024_30_07_tax_model_migration.php @@ -0,0 +1,36 @@ +cursor() + ->each(function($company){ + + if($company->tax_data?->version == 'alpha') + { + + $company->update(['tax_data' => new \App\DataMapper\Tax\TaxModel($company->tax_data)]); + + } + + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +};