You are here

protected function Shipping::applyHighest in Commerce Shipping 8.2

Applies the highest tax rate found on the order.

If an order has one order item taxed using the standard rate (e.g. 20%) and one taxed using the intermediate rate (e.g. 15%), then the standard rate will be applied, just like with applyDefault().

However, if the order only has an order item taxed using the intermediate rate, then the intermediate rate will be applied.

Parameters

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

\Drupal\commerce_order\Adjustment[] $tax_adjustments: The tax adjustments.

2 calls to Shipping::applyHighest()
Shipping::apply in src/Plugin/Commerce/TaxType/Shipping.php
Applies the tax type to the given order.
Shipping::applyProportional in src/Plugin/Commerce/TaxType/Shipping.php
Applies each order item's tax rate proportionally.

File

src/Plugin/Commerce/TaxType/Shipping.php, line 288

Class

Shipping
Provides the Shipping tax type.

Namespace

Drupal\commerce_shipping\Plugin\Commerce\TaxType

Code

protected function applyHighest(OrderInterface $order, array $tax_adjustments) {

  /** @var \Drupal\commerce_order\Adjustment[] $tax_adjustments_by_source */
  $tax_adjustments_by_source = [];
  foreach ($tax_adjustments as $adjustment) {
    $tax_adjustments_by_source[$adjustment
      ->getSourceId()] = $adjustment;
  }

  // Sort by percentage descending.
  uasort($tax_adjustments_by_source, function (Adjustment $a, Adjustment $b) {
    return $b
      ->getPercentage() <=> $a
      ->getPercentage();
  });
  $highest_adjustment = reset($tax_adjustments_by_source);
  foreach ($this
    ->getShipments($order) as $shipment) {
    $display_inclusive = $highest_adjustment
      ->isIncluded();
    $percentage = $highest_adjustment
      ->getPercentage();
    $tax_amount = $this
      ->calculateTaxAmount($shipment, $percentage, $display_inclusive);
    $tax_amount = $this->rounder
      ->round($tax_amount);
    $definition = [
      'amount' => $tax_amount,
    ] + $highest_adjustment
      ->toArray();
    $shipment
      ->addAdjustment(new Adjustment($definition));
  }
}