You are here

function hook_uc_calculate_tax in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_taxes/uc_taxes.api.php \hook_uc_calculate_tax()

Calculates tax line items for an order.

Parameters

mixed $order: An order object or an order id.

Return value

array An array of tax line item objects keyed by a module-specific id.

1 function implements hook_uc_calculate_tax()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

uc_tax_uc_calculate_tax in uc_tax/uc_tax.module
Calculates the amount and types of taxes that apply to an order.
1 invocation of hook_uc_calculate_tax()
uc_tax_calculate in uc_tax/uc_tax.module
Calculates the taxes for an order based on enabled tax modules.

File

uc_tax/uc_tax.api.php, line 22
Hooks provided by the Tax module.

Code

function hook_uc_calculate_tax($order) {
  if (!is_object($order)) {
    return [];
  }
  if (empty($order->delivery_postal_code)) {
    $order->delivery_postal_code = $order->billing_postal_code;
  }
  if (empty($order->delivery_zone)) {
    $order->delivery_zone = $order->billing_zone;
  }
  if (empty($order->delivery_country)) {
    $order->delivery_country = $order->billing_country;
  }
  $order->tax = [];
  if ($order
    ->getStatusId()) {
    $use_same_rates = in_array($order
      ->getStateId(), [
      'payment_received',
      'completed',
    ]);
  }
  else {
    $use_same_rates = FALSE;
  }
  foreach (uc_tax_rate_load() as $tax) {
    if ($use_same_rates) {
      foreach ((array) $order->line_items as $old_line) {
        if ($old_line['type'] == 'tax' && $old_line['data']['tax_id'] == $tax->id) {
          $tax->rate = $old_line['data']['tax_rate'];
          break;
        }
      }
    }
    $set = rules_config_load('uc_tax_' . $tax->id);
    if ($set
      ->execute($order)) {
      $line_item = uc_tax_apply_tax($order, $tax);
      if ($line_item) {
        $order->tax[$line_item->id] = $line_item;
      }
    }
  }
  return $order->tax;
}