View source
<?php
namespace Drupal\commerce_avatax\Plugin\Commerce\TaxType;
use Drupal\commerce_avatax\AvataxLibInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_price\Price;
use Drupal\commerce_tax\Plugin\Commerce\TaxType\RemoteTaxTypeBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Avatax extends RemoteTaxTypeBase {
protected $avataxLib;
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, AvataxLibInterface $avatax_lib, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $event_dispatcher);
$this->avataxLib = $avatax_lib;
$this->configFactory = $config_factory;
}
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_avatax.avatax_lib'), $container
->get('config.factory'));
}
public function defaultConfiguration() {
return [
'display_inclusive' => FALSE,
] + parent::defaultConfiguration();
}
public function applies(OrderInterface $order) {
$config = $this->configFactory
->get('commerce_avatax.settings');
return !$config
->get('disable_tax_calculation');
}
public function apply(OrderInterface $order) {
$response_body = $this->avataxLib
->transactionsCreate($order);
if (empty($response_body['lines'])) {
return;
}
$currency_code = $order
->getTotalPrice() ? $order
->getTotalPrice()
->getCurrencyCode() : $order
->getStore()
->getDefaultCurrencyCode();
$adjustments = [];
$applied_adjustments = [];
foreach ($response_body['lines'] as $tax_adjustment) {
$label = isset($tax_adjustment['details'][0]['taxName']) ? Html::escape($tax_adjustment['details'][0]['taxName']) : $this
->t('Sales tax');
$adjustments[$tax_adjustment['lineNumber']] = [
'amount' => $tax_adjustment['tax'],
'label' => $label,
];
}
foreach ($order
->getItems() as $order_item) {
if (!isset($adjustments[$order_item
->uuid()])) {
continue;
}
$order_item
->addAdjustment(new Adjustment([
'type' => 'tax',
'label' => $adjustments[$order_item
->uuid()]['label'],
'amount' => new Price((string) $adjustments[$order_item
->uuid()]['amount'], $currency_code),
'source_id' => $this->pluginId . '|' . $this->parentEntity
->id(),
]));
$applied_adjustments[$order_item
->uuid()] = $order_item
->uuid();
}
$remaining_adjustments = array_diff_key($adjustments, $applied_adjustments);
if (!$remaining_adjustments) {
return;
}
$tax_adjustment_total = NULL;
foreach ($remaining_adjustments as $remaining_adjustment) {
$adjustment_amount = new Price((string) $remaining_adjustment['amount'], $currency_code);
$tax_adjustment_total = $tax_adjustment_total ? $tax_adjustment_total
->add($adjustment_amount) : $adjustment_amount;
}
$order
->addAdjustment(new Adjustment([
'type' => 'tax',
'label' => $this
->t('Sales tax'),
'amount' => $tax_adjustment_total,
'source_id' => $this->pluginId . '|' . $this->parentEntity
->id(),
]));
}
}