[POC] Add change of tax by address

This commit is contained in:
arti0090 2021-06-18 08:34:21 +02:00
parent 468617a133
commit ff8d727a47
5 changed files with 30 additions and 26 deletions

View file

@ -9,13 +9,8 @@ as from 1st July 2021 the new taxation rules will be applied.
You can learn more about new EU taxation rules `here <https://ec.europa.eu/taxation_customs/business/vat/modernising-vat-cross-border-ecommerce_en>`_.
To change the way how the taxes are calculated; by billing or by shipping address, you need to override the service called
``OrderTaxesProcessor.php`` from ``Sylius/Component/Core/OrderProcessing``.
First let's copy code from original Processor to our service
from ``%kernel.project_dir%/vendor/sylius/sylius/src/Sylius/Component/Core/OrderProcessing/OrderTaxesProcessor.php`` to ``src/OrderProcessing/OrderTaxesProcessor.php``
Then register our new service:
To change the way how the taxes are calculated: by billing or by shipping address, you need to declare ``OrderTaxesProcessor`` with
additional argument in your config file:
.. code-block:: yaml
@ -25,25 +20,17 @@ Then register our new service:
- '@sylius.provider.channel_based_default_zone_provider'
- '@sylius.zone_matcher'
- '@sylius.registry.tax_calculation_strategy'
- '@sylius.taxation_address_resolver'
tags:
- { name: sylius.order_processor, priority: 10 }
Now we need to change the method ``getTaxZone`` to be using the shipping address:
And add a parameter to your config:
.. code-block:: php
.. code-block:: yaml
//...
private function getTaxZone(OrderInterface $order): ?ZoneInterface
{
$shippingAddress = $order->getShippingAddress();
$zone = null;
# app/config/packages/_sylius.yaml
parameters:
sylius_core.public_dir: '%kernel.project_dir%/public'
sylius_core.taxation.shipping_address_based_taxation: false
if (null !== $shippingAddress) {
$zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX);
}
return $zone ?: $this->defaultTaxZoneProvider->getZone($order);
}
//...
And with this change, the way how taxes are calculated will be based on shipping address.
And with this change, the way how taxes are calculated is based on shipping address.

View file

@ -27,6 +27,7 @@ imports:
parameters:
sylius_core.public_dir: "%kernel.project_dir%/web"
sylius_core.taxation.default_strategy: true
doctrine:
orm:

View file

@ -54,6 +54,7 @@
<argument type="service" id="sylius.provider.channel_based_default_zone_provider" />
<argument type="service" id="sylius.zone_matcher" />
<argument type="service" id="sylius.registry.tax_calculation_strategy" />
<argument>%sylius_core.taxation.default_strategy%</argument>
<tag name="sylius.order_processor" priority="10"/>
</service>

View file

@ -15,6 +15,7 @@ namespace Sylius\Component\Core\OrderProcessing;
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\Scope;
@ -38,14 +39,19 @@ final class OrderTaxesProcessor implements OrderProcessorInterface
/** @var PrioritizedServiceRegistryInterface */
private $strategyRegistry;
/** @var bool */
private $defaultTaxationStrategy;
public function __construct(
ZoneProviderInterface $defaultTaxZoneProvider,
ZoneMatcherInterface $zoneMatcher,
PrioritizedServiceRegistryInterface $strategyRegistry
PrioritizedServiceRegistryInterface $strategyRegistry,
bool $defaultTaxationStrategy = true
) {
$this->defaultTaxZoneProvider = $defaultTaxZoneProvider;
$this->zoneMatcher = $zoneMatcher;
$this->strategyRegistry = $strategyRegistry;
$this->defaultTaxationStrategy = $defaultTaxationStrategy;
}
public function process(BaseOrderInterface $order): void
@ -78,7 +84,7 @@ final class OrderTaxesProcessor implements OrderProcessorInterface
private function getTaxZone(OrderInterface $order): ?ZoneInterface
{
$billingAddress = $order->getBillingAddress();
$billingAddress = $this->getTaxAddress($order);
$zone = null;
if (null !== $billingAddress) {
@ -101,4 +107,13 @@ final class OrderTaxesProcessor implements OrderProcessorInterface
$shipment->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);
}
}
private function getTaxAddress(OrderInterface $order): ?AddressInterface
{
if (!$this->defaultTaxationStrategy) {
return $order->getShippingAddress();
}
return $order->getBillingAddress();
}
}

View file

@ -36,7 +36,7 @@ final class OrderTaxesProcessorSpec extends ObjectBehavior
ZoneMatcherInterface $zoneMatcher,
PrioritizedServiceRegistryInterface $strategyRegistry
): void {
$this->beConstructedWith($defaultTaxZoneProvider, $zoneMatcher, $strategyRegistry);
$this->beConstructedWith($defaultTaxZoneProvider, $zoneMatcher, $strategyRegistry, true);
}
function it_is_an_order_processor(): void