View source
<?php
namespace Drupal\commerce_tax\Plugin\Commerce\TaxType;
use CommerceGuys\Addressing\Address;
use CommerceGuys\Addressing\AddressInterface;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_price\RounderInterface;
use Drupal\commerce_store\Entity\StoreInterface;
use Drupal\commerce_tax\Event\BuildZonesEvent;
use Drupal\commerce_tax\Event\TaxEvents;
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;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
abstract class LocalTaxTypeBase extends TaxTypeBase implements LocalTaxTypeInterface {
protected $rounder;
protected $chainRateResolver;
protected $zones;
protected $matchedZones;
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;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('event_dispatcher'), $container
->get('commerce_price.rounder'), $container
->get('commerce_tax.chain_tax_rate_resolver'));
}
public function shouldRound() {
return TRUE;
}
public function applies(OrderInterface $order) {
$store = $order
->getStore();
return $this
->matchesAddress($store) || $this
->matchesRegistrations($store);
}
public function apply(OrderInterface $order) {
$calculation_date = $order
->getCalculationDate();
$store = $order
->getStore();
$prices_include_tax = $store
->get('prices_include_tax')->value;
$zones = $this
->getZones();
foreach ($order
->getItems() as $order_item) {
$customer_profile = $this
->resolveCustomerProfile($order_item);
if (!$customer_profile) {
continue;
}
$rates = $this
->resolveRates($order_item, $customer_profile);
foreach ($rates as $zone_id => $rate) {
$zone = $zones[$zone_id];
$percentage = $rate
->getPercentage($calculation_date);
if ($prices_include_tax != $this
->isDisplayInclusive()) {
$unit_price = $order_item
->getUnitPrice();
$tax_amount = $percentage
->calculateTaxAmount($unit_price, $prices_include_tax);
$tax_amount = $this->rounder
->round($tax_amount);
if ($prices_include_tax && !$this
->isDisplayInclusive()) {
$unit_price = $unit_price
->subtract($tax_amount);
}
elseif (!$prices_include_tax && $this
->isDisplayInclusive()) {
$unit_price = $unit_price
->add($tax_amount);
}
$order_item
->setUnitPrice($unit_price);
}
$adjusted_total_price = $order_item
->getAdjustedTotalPrice([
'promotion',
'fee',
]);
$tax_amount = $percentage
->calculateTaxAmount($adjusted_total_price, $this
->isDisplayInclusive());
if ($this
->shouldRound()) {
$tax_amount = $this->rounder
->round($tax_amount);
}
$order_item
->addAdjustment(new Adjustment([
'type' => 'tax',
'label' => $zone
->getDisplayLabel(),
'amount' => $tax_amount,
'percentage' => $percentage
->getNumber(),
'source_id' => $this->parentEntity
->id() . '|' . $zone
->getId() . '|' . $rate
->getId(),
'included' => $this
->isDisplayInclusive(),
]));
}
}
}
protected function matchesAddress(StoreInterface $store) {
$zones = $this
->getMatchingZones($store
->getAddress());
return !empty($zones);
}
protected function matchesRegistrations(StoreInterface $store) {
foreach ($this
->getZones() as $zone) {
if ($this
->checkRegistrations($store, $zone)) {
return TRUE;
}
}
return FALSE;
}
protected function checkRegistrations(StoreInterface $store, TaxZone $zone) {
foreach ($store
->get('tax_registrations') as $field_item) {
if ($zone
->match(new Address($field_item->value))) {
return TRUE;
}
}
return FALSE;
}
protected function resolveRates(OrderItemInterface $order_item, ProfileInterface $customer_profile) {
$zones = $this
->resolveZones($order_item, $customer_profile);
if (!$zones) {
return [];
}
$this->chainRateResolver
->setTaxType($this->parentEntity);
$rates = [];
foreach ($zones as $zone) {
$rate = $this->chainRateResolver
->resolve($zone, $order_item, $customer_profile);
if (is_object($rate)) {
$rates[$zone
->getId()] = $rate;
}
}
return $rates;
}
protected function resolveZones(OrderItemInterface $order_item, ProfileInterface $customer_profile) {
$customer_address = $customer_profile
->get('address')
->first();
$resolved_zones = $this
->getMatchingZones($customer_address);
return $resolved_zones;
}
protected function buildRateSummary() {
$zones = $this
->getZones();
usort($zones, function ($a, $b) {
return strcmp($a
->getLabel(), $b
->getLabel());
});
$element = [
'#type' => 'details',
'#title' => $this
->t('Tax rates'),
'#markup' => $this
->t('The following tax rates are provided:'),
'#open' => TRUE,
];
$element['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Tax rate'),
[
'data' => $this
->t('Percentage'),
'colspan' => 2,
],
],
'#input' => FALSE,
];
foreach ($zones as $zone) {
if (count($zones) > 1) {
$element['table'][$zone
->getId()] = [
'#attributes' => [
'class' => [
'region-title',
],
'no_striping' => TRUE,
],
'label' => [
'#markup' => $zone
->getLabel(),
'#wrapper_attributes' => [
'colspan' => 3,
],
],
];
}
foreach ($zone
->getRates() as $rate) {
$formatted_percentages = array_map(function ($percentage) {
return $percentage
->toString();
}, $rate
->getPercentages());
$element['table'][$zone
->getId() . '|' . $rate
->getId()] = [
'rate' => [
'#markup' => $rate
->getLabel(),
],
'percentages' => [
'#markup' => implode('<br>', $formatted_percentages),
],
];
}
}
return $element;
}
public function getZones() {
if (empty($this->zones)) {
$zones = $this
->buildZones();
$event = new BuildZonesEvent($zones, $this);
$this->eventDispatcher
->dispatch(TaxEvents::BUILD_ZONES, $event);
$this->zones = $event
->getZones();
}
return $this->zones;
}
public function getMatchingZones(AddressInterface $address) {
$address_hash = spl_object_hash($address);
if (!isset($this->matchedZones[$address_hash])) {
$this->matchedZones[$address_hash] = [];
foreach ($this
->getZones() as $zone) {
if ($zone
->match($address)) {
$this->matchedZones[$address_hash][] = $zone;
}
}
}
return $this->matchedZones[$address_hash];
}
protected abstract function buildZones();
}