TaxOrderProcessor.php in Commerce Core 8.2
File
modules/tax/src/TaxOrderProcessor.php
View source
<?php
namespace Drupal\commerce_tax;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\OrderProcessorInterface;
use Drupal\commerce_price\RounderInterface;
use Drupal\commerce_tax\Entity\TaxType;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class TaxOrderProcessor implements OrderProcessorInterface {
protected $entityTypeManager;
protected $rounder;
protected $storeTax;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RounderInterface $rounder, StoreTaxInterface $store_tax) {
$this->entityTypeManager = $entity_type_manager;
$this->rounder = $rounder;
$this->storeTax = $store_tax;
}
public function process(OrderInterface $order) {
$tax_types = $this
->getTaxTypes();
foreach ($tax_types as $tax_type) {
if ($tax_type
->getPlugin()
->applies($order)) {
$tax_type
->getPlugin()
->apply($order);
}
}
$calculation_date = $order
->getCalculationDate();
$store = $order
->getStore();
if ($store
->get('prices_include_tax')->value) {
foreach ($order
->getItems() as $order_item) {
$tax_adjustments = array_filter($order_item
->getAdjustments(), function ($adjustment) {
return $adjustment
->getType() == 'tax';
});
if (empty($tax_adjustments)) {
$unit_price = $order_item
->getUnitPrice();
$rates = $this->storeTax
->getDefaultRates($store, $order_item);
foreach ($rates as $rate) {
$percentage = $rate
->getPercentage($calculation_date);
$tax_amount = $percentage
->calculateTaxAmount($order_item
->getUnitPrice(), TRUE);
$tax_amount = $this->rounder
->round($tax_amount);
$unit_price = $unit_price
->subtract($tax_amount);
}
$order_item
->setUnitPrice($unit_price, $order_item
->isUnitPriceOverridden());
}
}
}
}
protected function getTaxTypes() {
$tax_type_storage = $this->entityTypeManager
->getStorage('commerce_tax_type');
$tax_types = $tax_type_storage
->loadByProperties([
'status' => TRUE,
]);
uasort($tax_types, [
TaxType::class,
'sort',
]);
return $tax_types;
}
}