From b8a74444b19d7f76d41a8132e54ce24b6e746f41 Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Wed, 26 Apr 2017 17:33:38 -0400 Subject: [PATCH] Issue #2872190 by bojanz: Create tax rate resolvers --- modules/tax/commerce_tax.services.yml | 10 ++++ .../src/Plugin/Commerce/TaxType/Custom.php | 8 ++- .../Commerce/TaxType/LocalTaxTypeBase.php | 20 +++++-- .../tax/src/Resolver/ChainTaxRateResolver.php | 57 +++++++++++++++++++ .../ChainTaxRateResolverInterface.php | 29 ++++++++++ .../src/Resolver/DefaultTaxRateResolver.php | 30 ++++++++++ .../src/Resolver/TaxRateResolverInterface.php | 33 +++++++++++ modules/tax/src/TaxRate.php | 4 +- modules/tax/src/TaxZone.php | 2 +- 9 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 modules/tax/src/Resolver/ChainTaxRateResolver.php create mode 100644 modules/tax/src/Resolver/ChainTaxRateResolverInterface.php create mode 100644 modules/tax/src/Resolver/DefaultTaxRateResolver.php create mode 100644 modules/tax/src/Resolver/TaxRateResolverInterface.php diff --git a/modules/tax/commerce_tax.services.yml b/modules/tax/commerce_tax.services.yml index 0e4ec45b6b..dc38e83919 100644 --- a/modules/tax/commerce_tax.services.yml +++ b/modules/tax/commerce_tax.services.yml @@ -3,6 +3,16 @@ services: class: Drupal\commerce_tax\TaxTypeManager parent: default_plugin_manager + commerce_tax.chain_tax_rate_resolver: + class: Drupal\commerce_tax\Resolver\ChainTaxRateResolver + tags: + - { name: service_collector, call: addResolver, tag: commerce_tax.tax_rate_resolver } + + commerce_tax.default_tax_rate_resolver: + class: Drupal\commerce_tax\Resolver\DefaultTaxRateResolver + tags: + - { name: commerce_tax.tax_rate_resolver, priority: -100 } + commerce_tax.tax_order_processor: class: Drupal\commerce_tax\TaxOrderProcessor arguments: ['@entity_type.manager'] diff --git a/modules/tax/src/Plugin/Commerce/TaxType/Custom.php b/modules/tax/src/Plugin/Commerce/TaxType/Custom.php index 1620c291f9..8886be7322 100644 --- a/modules/tax/src/Plugin/Commerce/TaxType/Custom.php +++ b/modules/tax/src/Plugin/Commerce/TaxType/Custom.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_tax\Plugin\Commerce\TaxType; use Drupal\commerce_price\RounderInterface; +use Drupal\commerce_tax\Resolver\ChainTaxRateResolverInterface; use Drupal\commerce_tax\TaxZone; use Drupal\Component\Uuid\UuidInterface; use Drupal\Component\Utility\Html; @@ -43,11 +44,13 @@ class Custom extends LocalTaxTypeBase { * The event dispatcher. * @param \Drupal\commerce_price\RounderInterface $rounder * The rounder. + * @param \Drupal\commerce_tax\ChainTaxRateResolverInterface $chain_rate_resolver + * The chain tax rate resolver. * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator * The UUID generator. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RounderInterface $rounder, UuidInterface $uuid_generator) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $event_dispatcher, $rounder); + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RounderInterface $rounder, ChainTaxRateResolverInterface $chain_rate_resolver, UuidInterface $uuid_generator) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $event_dispatcher, $rounder, $chain_rate_resolver); $this->uuidGenerator = $uuid_generator; } @@ -63,6 +66,7 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity_type.manager'), $container->get('event_dispatcher'), $container->get('commerce_price.rounder'), + $container->get('commerce_tax.chain_tax_rate_resolver'), $container->get('uuid') ); } diff --git a/modules/tax/src/Plugin/Commerce/TaxType/LocalTaxTypeBase.php b/modules/tax/src/Plugin/Commerce/TaxType/LocalTaxTypeBase.php index 322a8fe6a3..e621b87722 100644 --- a/modules/tax/src/Plugin/Commerce/TaxType/LocalTaxTypeBase.php +++ b/modules/tax/src/Plugin/Commerce/TaxType/LocalTaxTypeBase.php @@ -9,6 +9,7 @@ use Drupal\commerce_price\RounderInterface; use Drupal\commerce_store\Entity\StoreInterface; use Drupal\commerce_tax\TaxZone; +use Drupal\commerce_tax\Resolver\ChainTaxRateResolverInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\profile\Entity\ProfileInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -26,6 +27,13 @@ abstract class LocalTaxTypeBase extends TaxTypeBase implements LocalTaxTypeInter */ protected $rounder; + /** + * The chain tax rate resolver. + * + * @var \Drupal\commerce_tax\ChainTaxRateResolverInterface + */ + protected $chainRateResolver; + /** * Constructs a new LocalTaxTypeBase object. * @@ -41,11 +49,14 @@ abstract class LocalTaxTypeBase extends TaxTypeBase implements LocalTaxTypeInter * The event dispatcher. * @param \Drupal\commerce_price\RounderInterface $rounder * The rounder. + * @param \Drupal\commerce_tax\ChainTaxRateResolverInterface $chain_rate_resolver + * The chain tax rate resolver. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RounderInterface $rounder) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RounderInterface $rounder, ChainTaxRateResolverInterface $chain_rate_resolver) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $event_dispatcher); $this->rounder = $rounder; + $this->chainRateResolver = $chain_rate_resolver; } /** @@ -58,7 +69,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('entity_type.manager'), $container->get('event_dispatcher'), - $container->get('commerce_price.rounder') + $container->get('commerce_price.rounder'), + $container->get('commerce_tax.chain_tax_rate_resolver') ); } @@ -93,8 +105,8 @@ public function apply(OrderInterface $order) { $zones = $this->resolveZones($order_item, $customer_profile); foreach ($zones as $zone) { - $rate = $this->resolveRate($zone, $order_item, $customer_profile); - if (!$rate) { + $rate = $this->chainRateResolver->resolve($zone, $order_item, $customer_profile); + if (!is_object($rate)) { // No applicable rate found. continue; } diff --git a/modules/tax/src/Resolver/ChainTaxRateResolver.php b/modules/tax/src/Resolver/ChainTaxRateResolver.php new file mode 100644 index 0000000000..8f2d692733 --- /dev/null +++ b/modules/tax/src/Resolver/ChainTaxRateResolver.php @@ -0,0 +1,57 @@ +resolvers = $resolvers; + } + + /** + * {@inheritdoc} + */ + public function addResolver(TaxRateResolverInterface $resolver) { + $this->resolvers[] = $resolver; + } + + /** + * {@inheritdoc} + */ + public function getResolvers() { + return $this->resolvers; + } + + /** + * {@inheritdoc} + */ + public function resolve(TaxZone $zone, OrderItemInterface $order_item, ProfileInterface $customer_profile) { + $result = NULL; + foreach ($this->resolvers as $resolver) { + $result = $resolver->resolve($zone, $order_item, $customer_profile); + if ($result) { + break; + } + } + + return $result; + } + +} diff --git a/modules/tax/src/Resolver/ChainTaxRateResolverInterface.php b/modules/tax/src/Resolver/ChainTaxRateResolverInterface.php new file mode 100644 index 0000000000..9c5a4026f2 --- /dev/null +++ b/modules/tax/src/Resolver/ChainTaxRateResolverInterface.php @@ -0,0 +1,29 @@ +getRates(); + // Take the default rate, or fallback to the first rate. + $resolved_rate = reset($rates); + foreach ($rates as $rate) { + if ($rate->isDefault()) { + $resolved_rate = $rate; + break; + } + } + return $resolved_rate; + } + +} diff --git a/modules/tax/src/Resolver/TaxRateResolverInterface.php b/modules/tax/src/Resolver/TaxRateResolverInterface.php new file mode 100644 index 0000000000..764dafc0fb --- /dev/null +++ b/modules/tax/src/Resolver/TaxRateResolverInterface.php @@ -0,0 +1,33 @@ +setTime(0, 0); foreach ($this->amounts as $amount) { $start_date = $amount->getStartDate(); diff --git a/modules/tax/src/TaxZone.php b/modules/tax/src/TaxZone.php index 309ebe8207..0c4a97c421 100644 --- a/modules/tax/src/TaxZone.php +++ b/modules/tax/src/TaxZone.php @@ -109,7 +109,7 @@ public function getRates() { } /** - * Checks whether the provided address belongs to the zone. + * Checks whether the given address belongs to the zone. * * @param \CommerceGuys\Addressing\AddressInterface $address * The address.