You are here

function uc_taxes_calculate_tax in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_taxes/uc_taxes.module \uc_taxes_calculate_tax()

Calculate the amount and types of taxes that apply to an order.

File

uc_taxes/uc_taxes.module, line 453

Code

function uc_taxes_calculate_tax($order) {
  global $user;
  if (is_numeric($order)) {
    $order = uc_order_load($order);
    $account = user_load(array(
      'uid' => $order->uid,
    ));
  }
  else {
    if ((int) $order->uid) {
      $account = user_load(array(
        'uid' => intval($order->uid),
      ));
    }
    else {
      $account = $user;
    }
  }
  if (!is_object($order)) {
    return array();
  }
  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;
  }
  if (is_array($order->line_items)) {
    foreach ($order->line_items as $i => $line) {
      if (substr($line['type'], 0, 4) == 'tax_' && substr($line['type'], 5) != 'subtotal') {
        unset($order->line_items[$i]);
      }
    }
  }
  $_SESSION['taxes'] = array();
  $taxes = uc_taxes_get_rates();
  foreach ($taxes as $tax) {

    // Gotta pass a fake line_item entity for the data to be saved to $_SESSION.
    workflow_ng_invoke_event('calculate_tax_' . $tax->id, $order, $tax, $account, array());

    //$order->line_items[] = array('type' => 'tax', 'amount' => $_SESSION['taxes'][$tax->id]['amount']);
  }
  $order->taxes = $_SESSION['taxes'];
  unset($_SESSION['taxes']);

  //array_unshift($order->taxes, array('id' => 'subtotal', 'name' => t('Subtotal excluding taxes'), 'amount' => $amount, 'weight' => -10));
  return $order->taxes;
}