You are here

function uc_tax_apply_tax in Ubercart 8.4

Applies taxes to an order.

Parameters

\Drupal\uc_order\OrderInterface $order: The order object being considered.

$tax: The tax rule calculating the amount.

Return value

array The line item array representing the amount of tax.

2 calls to uc_tax_apply_tax()
hook_uc_calculate_tax in uc_tax/uc_tax.api.php
Calculates tax line items for an order.
uc_tax_uc_calculate_tax in uc_tax/uc_tax.module
Calculates the amount and types of taxes that apply to an order.

File

uc_tax/uc_tax.module, line 496
Ubercart Tax module.

Code

function uc_tax_apply_tax(OrderInterface $order, $tax) {
  $taxable_amount = 0;
  if (is_array($order->products)) {
    foreach ($order->products as $item) {
      $taxable_amount += $item->qty->value * uc_tax_apply_item_tax($item, $tax);
    }
  }
  $taxed_line_items = $tax->taxed_line_items;
  if (is_array($order->line_items) && is_array($taxed_line_items)) {
    foreach ($order->line_items as $line_item) {
      if ($line_item['type'] == 'tax') {

        // Don't tax old taxes.
        continue;
      }
      if (in_array($line_item['type'], $taxed_line_items)) {
        $taxable_amount += $line_item['amount'];
      }
    }
  }
  if (in_array('tax', $taxed_line_items)) {

    // Tax taxes that were just calculated.
    foreach ($order->tax as $other_tax) {
      $taxable_amount += $other_tax->amount;
    }
  }
  $amount = $taxable_amount * $tax->rate;
  if ($amount) {
    $line_item = (object) [
      'id' => $tax->id,
      'name' => $tax->name,
      'amount' => $amount,
      'weight' => $tax->weight,
      'summed' => 1,
    ];
    $line_item->data = [
      'tax_rate' => $tax->rate,
      'tax' => $tax,
      'taxable_amount' => $taxable_amount,
      'tax_jurisdiction' => $tax->name,
    ];
    return $line_item;
  }
}