You are here

function commerce_tax_remove_taxes in Commerce Core 7

Rules actions: removes all taxes applied to a line item.

File

modules/tax/commerce_tax.rules.inc, line 98
Rules integration for line items.

Code

function commerce_tax_remove_taxes($line_item_wrapper, $increase_base_price, $tax_rates) {

  // Load all the tax component types to look for matching price components in
  // the line item's unit price. Filter the list by tax rates that should be
  // removed if only some were specified.
  $component_types = commerce_tax_commerce_price_component_type_info();
  if (!empty($tax_rates)) {
    foreach ($component_types as $name => $component_type) {
      if (!in_array($component_type['tax_rate'], $tax_rates)) {
        unset($component_types[$name]);
      }
    }
  }

  // Loop over the price components in the line item's unit price.
  $price = $line_item_wrapper->commerce_unit_price
    ->value();
  $new_components = array();
  foreach ($price['data']['components'] as $key => $component) {

    // Look for components that match one of the defined tax price components.
    if (array_key_exists($component['name'], $component_types)) {

      // Remove it from the components array.
      unset($price['data']['components'][$key]);

      // If the component was marked as "included" in the price amount, update
      // the price amount to reflect the difference.
      if (!empty($component['included'])) {
        $price['amount'] -= $component['price']['amount'];

        // If the user opted to increase the base price by the amount of the
        // display inclusive taxes removed, add them back as new price components.
        if (!empty($increase_base_price)) {
          $price['data']['components'][] = array(
            'name' => 'base_price',
            'price' => array(
              'amount' => $component['price']['amount'],
              'currency_code' => $component['price']['currency_code'],
              'data' => array(),
            ),
            'included' => TRUE,
          );
          $price['amount'] += $component['price']['amount'];
        }
      }
    }
  }
  $line_item_wrapper->commerce_unit_price = $price;
}