You are here

public function TaxOrderProcessor::process in Commerce Core 8.2

Processes an order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

Overrides OrderProcessorInterface::process

File

modules/tax/src/TaxOrderProcessor.php, line 56

Class

TaxOrderProcessor
Applies taxes to orders during the order refresh process.

Namespace

Drupal\commerce_tax

Code

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);
    }
  }

  // Don't overcharge a tax-exempt customer if the price is tax-inclusive.
  // For example, a 12 EUR price with 20% EU VAT gets reduced to 10 EUR
  // when selling to customers outside the EU, but only if no other tax
  // was applied (e.g. a Japanese customer paying Japanese tax due to the
  // store being registered to collect tax there).
  $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) {

        /** @var \Drupal\commerce_order\Adjustment $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());
      }
    }
  }
}