You are here

protected function Shipping::applyDefault in Commerce Shipping 8.2

Applies the default tax rate of the order's tax type.

Parameters

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

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

1 call to Shipping::applyDefault()
Shipping::apply in src/Plugin/Commerce/TaxType/Shipping.php
Applies the tax type to the given order.

File

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

Class

Shipping
Provides the Shipping tax type.

Namespace

Drupal\commerce_shipping\Plugin\Commerce\TaxType

Code

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

  // Assume that all tax adjustments have the same tax type and zone ID.
  $tax_adjustment = reset($tax_adjustments);
  list($tax_type_id, $zone_id, $rate_id) = explode('|', $tax_adjustment
    ->getSourceId());
  $tax_type_storage = $this->entityTypeManager
    ->getStorage('commerce_tax_type');

  /** @var \Drupal\commerce_tax\Entity\TaxTypeInterface $tax_type */
  $tax_type = $tax_type_storage
    ->load($tax_type_id);
  if (!$tax_type) {
    return;
  }

  /** @var \Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeInterface $tax_type_plugin */
  $tax_type_plugin = $tax_type
    ->getPlugin();
  if (!$tax_type_plugin instanceof LocalTaxTypeInterface) {
    return;
  }
  $zones = $tax_type_plugin
    ->getZones();
  $zone = $zones[$zone_id];
  $default_rate = $zone
    ->getDefaultRate();
  $percentage = $default_rate
    ->getPercentage($order
    ->getCalculationDate());
  foreach ($this
    ->getShipments($order) as $shipment) {
    $display_inclusive = $tax_type_plugin
      ->isDisplayInclusive();
    $tax_amount = $this
      ->calculateTaxAmount($shipment, $percentage
      ->getNumber(), $display_inclusive);
    $tax_amount = $this->rounder
      ->round($tax_amount);
    $shipment
      ->addAdjustment(new Adjustment([
      'type' => 'tax',
      'label' => $zone
        ->getDisplayLabel(),
      'amount' => $tax_amount,
      'percentage' => $percentage
        ->getNumber(),
      'source_id' => $tax_type
        ->id() . '|' . $zone
        ->getId() . '|' . $default_rate
        ->getId(),
      'included' => $display_inclusive,
    ]));
  }
}