Skip to content

Commit

Permalink
feat: add active from tax rule
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeproSpace committed Jul 10, 2023
1 parent 086770f commit 30a08e3
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration\V6_5;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1688927492AddTaxActiveFromField extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1688927492;
}

public function update(Connection $connection): void
{
$connection->executeStatement('ALTER TABLE `tax_rule` ADD `active_from` DATETIME(3) NULL AFTER `data`;');
}

public function updateDestructive(Connection $connection): void
{
}
}
9 changes: 4 additions & 5 deletions src/Core/System/SalesChannel/SalesChannelContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ public function buildTaxRules(string $taxId): TaxRuleCollection
throw new TaxNotFoundException($taxId);
}

if ($tax->getRules()->first() !== null) {
// NEXT-21735 - This is covered randomly
// @codeCoverageIgnoreStart
$newestTaxRule = $tax->getRules()->newestTaxRule();

if ($newestTaxRule !== null) {
return new TaxRuleCollection([
new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
new TaxRule($newestTaxRule->getTaxRate(), 100),
]);
// @codeCoverageIgnoreEnd
}

return new TaxRuleCollection([
Expand Down
5 changes: 5 additions & 0 deletions src/Core/System/Tax/Aggregate/TaxRule/TaxRuleCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function sortByTypePosition(): void
$this->sort(fn (TaxRuleEntity $entityA, TaxRuleEntity $entityB) => $entityA->getType()->getPosition() <=> $entityB->getType()->getPosition());
}

public function newestTaxRule(): ?TaxRuleEntity
{
return $this->reduce(fn ($result, $item) => $result === null || $item->getActiveFrom() > $result->getActiveFrom() ? $item : $result);
}

public function getApiAlias(): string
{
return 'tax_rule_collection';
Expand Down
3 changes: 3 additions & 0 deletions src/Core/System/Tax/Aggregate/TaxRule/TaxRuleDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Shopware\Core\System\Tax\Aggregate\TaxRule;

use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\SearchRanking;
Expand Down Expand Up @@ -62,6 +64,7 @@ protected function defineFields(): FieldCollection
new StringField('toZipCode', 'toZipCode'),
]),
(new FkField('tax_id', 'taxId', TaxDefinition::class))->addFlags(new Required()),
(new DateTimeField('active_from', 'activeFrom'))->addFlags(new ApiAware()),
new ManyToOneAssociationField('type', 'tax_rule_type_id', TaxRuleTypeDefinition::class, 'id', $autoload),
new ManyToOneAssociationField('country', 'country_id', CountryDefinition::class, 'id'),
new ManyToOneAssociationField('tax', 'tax_id', TaxDefinition::class, 'id'),
Expand Down
15 changes: 15 additions & 0 deletions src/Core/System/Tax/Aggregate/TaxRule/TaxRuleEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class TaxRuleEntity extends Entity
*/
protected $data;

/**
* @var \DateTimeInterface|null
*/
protected $activeFrom;

public function getTaxId(): string
{
return $this->taxId;
Expand Down Expand Up @@ -136,4 +141,14 @@ public function setData(?array $data): void
{
$this->data = $data;
}

public function getActiveFrom(): ?\DateTimeInterface
{
return $this->activeFrom;
}

public function setActiveFrom(?\DateTimeInterface $activeFrom): void
{
$this->activeFrom = $activeFrom;
}
}
15 changes: 15 additions & 0 deletions src/Core/System/Tax/TaxRuleType/AbstractTaxRuleTypeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Shopware\Core\System\Tax\TaxRuleType;

use Shopware\Core\System\Tax\Aggregate\TaxRule\TaxRuleEntity;

abstract class AbstractTaxRuleTypeFilter implements TaxRuleTypeFilterInterface
{
protected function isTaxActive(TaxRuleEntity $taxRuleEntity): bool
{
return $taxRuleEntity->getActiveFrom() > (new \DateTime())->setTimezone(new \DateTimeZone('UTC'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Shopware\Core\System\Tax\Aggregate\TaxRule\TaxRuleEntity;

#[Package('customer-order')]
class EntireCountryRuleTypeFilter implements TaxRuleTypeFilterInterface
class EntireCountryRuleTypeFilter extends AbstractTaxRuleTypeFilter
{
final public const TECHNICAL_NAME = 'entire_country';

Expand All @@ -20,6 +20,10 @@ public function match(TaxRuleEntity $taxRuleEntity, ?CustomerEntity $customer, S
return false;
}

if($taxRuleEntity->getActiveFrom() !== null ) {
return $this->isTaxActive($taxRuleEntity);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Shopware\Core\System\Tax\Aggregate\TaxRule\TaxRuleEntity;

#[Package('customer-order')]
class IndividualStatesRuleTypeFilter implements TaxRuleTypeFilterInterface
class IndividualStatesRuleTypeFilter extends AbstractTaxRuleTypeFilter
{
final public const TECHNICAL_NAME = 'individual_states';

Expand All @@ -27,6 +27,10 @@ public function match(TaxRuleEntity $taxRuleEntity, ?CustomerEntity $customer, S
return false;
}

if($taxRuleEntity->getActiveFrom() !== null ) {
return $this->isTaxActive($taxRuleEntity);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Shopware\Core\System\Tax\Aggregate\TaxRule\TaxRuleEntity;

#[Package('customer-order')]
class ZipCodeRangeRuleTypeFilter implements TaxRuleTypeFilterInterface
class ZipCodeRangeRuleTypeFilter extends AbstractTaxRuleTypeFilter
{
final public const TECHNICAL_NAME = 'zip_code_range';

Expand All @@ -29,6 +29,10 @@ public function match(TaxRuleEntity $taxRuleEntity, ?CustomerEntity $customer, S
return false;
}

if($taxRuleEntity->getActiveFrom() !== null ) {
return $this->isTaxActive($taxRuleEntity);
}

return true;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Core/System/Tax/TaxRuleType/ZipCodeRuleTypeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Shopware\Core\System\Tax\Aggregate\TaxRule\TaxRuleEntity;

#[Package('customer-order')]
class ZipCodeRuleTypeFilter implements TaxRuleTypeFilterInterface
class ZipCodeRuleTypeFilter extends AbstractTaxRuleTypeFilter
{
final public const TECHNICAL_NAME = 'zip_code';

Expand All @@ -28,6 +28,10 @@ public function match(TaxRuleEntity $taxRuleEntity, ?CustomerEntity $customer, S
return false;
}

if($taxRuleEntity->getActiveFrom() !== null ) {
return $this->isTaxActive($taxRuleEntity);
}

return true;
}

Expand Down

0 comments on commit 30a08e3

Please sign in to comment.