You are here

function commerce_tax_rate_apply in Commerce Core 7

Applies a tax rate to the unit price of a line item.

Parameters

$tax_rate: The tax rate to apply to the line item.

$line_item: The line item whose unit price will be modified to include the tax.

Return value

A price array representing the tax applied to the line item or FALSE if none was applied.

1 call to commerce_tax_rate_apply()
commerce_tax_rate_rules_apply in modules/tax/commerce_tax.rules.inc
Rules action: loads and applies a tax rate to the given line item.
1 string reference to 'commerce_tax_rate_apply'
commerce_tax_default_rules_configuration in modules/tax/commerce_tax.rules_defaults.inc
Implements hook_default_rules_configuration().

File

modules/tax/commerce_tax.module, line 263
Defines tax rates and Rules integration for configuring tax rules for applicability and display.

Code

function commerce_tax_rate_apply($tax_rate, $line_item) {

  // If a valid rate is specified...
  if (isset($tax_rate['rate']) && is_numeric($tax_rate['rate'])) {
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

    // Don't apply tax if the unit price has a NULL amount.
    if (is_null($wrapper->commerce_unit_price
      ->value())) {
      return;
    }

    // Invoke the tax rate's calculation callback and apply the returned tax
    // price to the line item.
    if ($tax_price = $tax_rate['calculation_callback']($tax_rate, $wrapper)) {

      // Add the tax to the unit price's data array along with a display inclusive
      // property used to track whether or not the tax is included in the price.
      $included = FALSE;

      // If the rate specifies a valid tax type that is display inclusive...
      if (($tax_type = commerce_tax_type_load($tax_rate['type'])) && $tax_type['display_inclusive']) {

        // Include the tax amount in the displayed unit price.
        $wrapper->commerce_unit_price->amount = $wrapper->commerce_unit_price->amount
          ->value() + $tax_price['amount'];
        $included = TRUE;
      }

      // Update the data array with the tax component.
      $wrapper->commerce_unit_price->data = commerce_price_component_add($wrapper->commerce_unit_price
        ->value(), $tax_rate['price_component'], $tax_price, $included);
      return $tax_price;
    }
  }
  return FALSE;
}