-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2872190 by bojanz: Create tax rate resolvers
- Loading branch information
Showing
9 changed files
with
184 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_tax\Resolver; | ||
|
||
use Drupal\commerce_order\Entity\OrderItemInterface; | ||
use Drupal\commerce_tax\TaxZone; | ||
use Drupal\profile\Entity\ProfileInterface; | ||
|
||
class ChainTaxRateResolver implements ChainTaxRateResolverInterface { | ||
|
||
/** | ||
* The resolvers. | ||
* | ||
* @var \Drupal\commerce_tax\Resolver\TaxRateResolverInterface[] | ||
*/ | ||
protected $resolvers = []; | ||
|
||
/** | ||
* Constructs a new ChainTaxRateResolver object. | ||
* | ||
* @param \Drupal\commerce_tax\Resolver\TaxRateResolverInterface[] $resolvers | ||
* The resolvers. | ||
*/ | ||
public function __construct(array $resolvers = []) { | ||
$this->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; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
modules/tax/src/Resolver/ChainTaxRateResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_tax\Resolver; | ||
|
||
/** | ||
* Runs the added resolvers one by one until one of them returns the tax rate. | ||
* | ||
* Each resolver in the chain can be another chain, which is why this interface | ||
* extends the tax rate resolver one. | ||
*/ | ||
interface ChainTaxRateResolverInterface extends TaxRateResolverInterface { | ||
|
||
/** | ||
* Adds a resolver. | ||
* | ||
* @param \Drupal\commerce_tax\Resolver\TaxRateResolverInterface $resolver | ||
* The resolver. | ||
*/ | ||
public function addResolver(TaxRateResolverInterface $resolver); | ||
|
||
/** | ||
* Gets all added resolvers. | ||
* | ||
* @return \Drupal\commerce_tax\Resolver\TaxRateResolverInterface[] | ||
* The resolvers. | ||
*/ | ||
public function getResolvers(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_tax\Resolver; | ||
|
||
use Drupal\commerce_order\Entity\OrderItemInterface; | ||
use Drupal\commerce_tax\TaxZone; | ||
use Drupal\profile\Entity\ProfileInterface; | ||
|
||
/** | ||
* Returns the tax zone's default tax rate. | ||
*/ | ||
class DefaultTaxRateResolver implements TaxRateResolverInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(TaxZone $zone, OrderItemInterface $order_item, ProfileInterface $customer_profile) { | ||
$rates = $zone->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_tax\Resolver; | ||
|
||
use Drupal\commerce_order\Entity\OrderItemInterface; | ||
use Drupal\commerce_tax\TaxZone; | ||
use Drupal\profile\Entity\ProfileInterface; | ||
|
||
/** | ||
* Defines the interface for tax rate resolvers. | ||
*/ | ||
interface TaxRateResolverInterface { | ||
|
||
// Stops resolving when there is no applicable tax rate (cause the | ||
// provided order item is exempt from sales tax, for example). | ||
const NO_APPLICABLE_TAX_RATE = 'no_applicable_tax_rate'; | ||
|
||
/** | ||
* Resolves the tax rate for the given tax zone. | ||
* | ||
* @param \Drupal\commerce_tax\TaxZone $zone | ||
* The tax zone. | ||
* @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item | ||
* The order item. | ||
* @param \Drupal\profile\Entity\ProfileInterface $customer_profile | ||
* The customer profile. Contains the address and tax number. | ||
* | ||
* @return \Drupal\commerce_tax\TaxRate|string|null | ||
* The tax rate, NO_APPLICABLE_TAX_RATE, or NULL. | ||
*/ | ||
public function resolve(TaxZone $zone, OrderItemInterface $order_item, ProfileInterface $customer_profile); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters