You are here

function uc_taxes_action_apply_tax in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_taxes/uc_taxes.ca.inc \uc_taxes_action_apply_tax()

Workflow-ng action callback to calculate a tax.

Parameters

$order: The order object being considered.

$tax: The tax rule calculating the amount.

Return value

The line item array representing the amount of tax.

2 string references to 'uc_taxes_action_apply_tax'
uc_taxes_configuration in uc_taxes/uc_taxes_workflow.inc
Implementation of hook_configuration().
uc_taxes_update_6 in uc_taxes/uc_taxes.install

File

uc_taxes/uc_taxes_workflow.inc, line 82
This file contains the Workflow-ng hooks and functions necessary to make the tax related entity, conditions, events, and actions work.

Code

function uc_taxes_action_apply_tax($order, $tax) {
  $amount = 0;
  $taxable_amount = 0;
  if (is_array($order->products)) {
    foreach ($order->products as $item) {
      $node = node_load($item->nid);

      // Tax products if they are of a taxed type and if it is shippable if
      // the tax only applies to shippable products.
      if (in_array($node->type, $tax->taxed_product_types) && ($tax->shippable == 0 || $node->shippable == 1)) {
        $taxable_amount += $item->price * $item->qty;
      }
    }
  }
  $taxed_line_items = $tax->taxed_line_items;
  if (is_array($order->line_items) && is_array($taxed_line_items)) {
    foreach ($order->line_items as $key => $line_item) {
      if ($line_item['type'] == 'tax' && $line_item['title'] == $tax->name) {

        // Don't tax yourself.
        continue;
      }
      if (in_array($line_item['type'], $taxed_line_items)) {
        $taxable_amount += $line_item['amount'];
      }
    }
  }
  if (isset($taxed_line_items['tax'])) {
    foreach ($_SESSION['taxes'] as $other_tax) {
      $taxable_amount += $other_tax['amount'];
    }
  }
  $amount = $taxable_amount * $tax->rate;
  if ($amount) {
    $line_item = array(
      'id' => $tax->id,
      'name' => $tax->name,
      'amount' => $amount,
      'weight' => $tax->weight,
      'data' => array(
        'tax_rate' => $tax->rate,
        'taxable_amount' => $taxable_amount,
        'tax_jurisdiction' => $tax->name,
      ),
    );
    return array(
      'tax_line_item' => $line_item,
    );
  }
}