You are here

function commerce_vat_rate_calculate in Commerce VAT 7

Calculates a price array for the vat on the unit price of a line item.

Parameters

$vat_rate: The vat rate array for the vat to calculate.

$rate_info: The applicable vat rate which might depend on order date.

$line_item_wrapper: An entity_metadata_wrapper() for the line item whose unit price should be used in the vat calculation.

int|null $amount: Only for proportional VAT calculation: The unit price amount to calculate the vat for. Defaults to unit price amount of line item.

Return value

array|bool The vat price array or FALSE if the vat is already applied.

1 string reference to 'commerce_vat_rate_calculate'
commerce_vat_rates in ./commerce_vat.module
Returns an array of vat rate objects keyed by name.

File

./commerce_vat.module, line 404
Defines VAT rates and Rules integration for configuring vat rules for applicability and display.

Code

function commerce_vat_rate_calculate($vat_rate, $rate_info, $line_item_wrapper, $amount = NULL) {

  // By default, do not duplicate a vat that's already on the line item.
  if (!is_null($line_item_wrapper->commerce_unit_price
    ->value()) && !commerce_price_component_load($line_item_wrapper->commerce_unit_price
    ->value(), $vat_rate['price_component'])) {

    // If not given, calculate the vat amount.
    if (!isset($amount)) {
      $amount = $line_item_wrapper->commerce_unit_price->amount
        ->value();
    }
    $data = $line_item_wrapper->commerce_unit_price->data
      ->value();
    $direction = variable_get('commerce_vat_direction', 'forward');
    if ($direction == 'reverse') {
      $vat_amount = $amount - $amount / (1 + $rate_info['rate']);
    }
    else {
      if (isset($data['rounded_up']) && $data['rounded_up']) {
        $amount = $amount - 0.5;
      }
      $vat_amount = $amount * $rate_info['rate'];
    }
    $currency_code = $line_item_wrapper->commerce_unit_price->currency_code
      ->value();
    return array(
      'amount' => commerce_vat_rate_round_amount($vat_amount, $currency_code),
      'currency_code' => $currency_code,
      'data' => array(
        'vat_rate' => $vat_rate,
        'vat_rate_info' => $rate_info,
      ),
    );
  }
  return FALSE;
}