diff --git a/commerce_vat.module b/commerce_vat.module index 47c152d..c7f052d 100644 --- a/commerce_vat.module +++ b/commerce_vat.module @@ -525,27 +525,42 @@ // Calculate these vates in reverse order to accommodate cumulative display // inclusive vat rates. foreach (array_reverse(\$inclusive_vates) as \$vat_rate) { - // The amount of this vat is determined by dividing the current base price - // by 1 + the vat rate expressed as a decimal (i.e. 1.1 for a 10% vat). - // The result is the base price against which the vat would have been - // applied, so the difference becomes our vat amount. - \$vat_amount = \$price['data']['components'][0]['price']['amount'] - \$price['data']['components'][0]['price']['amount'] / (1 + \$vat_rate['rate']); - \$vat_amount = commerce_vat_rate_round_amount(\$vat_rate, \$vat_amount); + // See: 'commerce_vat_rate_apply()' + \$wrapper = entity_metadata_wrapper('commerce_line_item', \$line_item); + \$order_date = (\$wrapper->order->created->value()) ? : time(); - \$prevat_base = \$price['data']['components'][0]['price']['amount'] - \$vat_amount; + foreach (\$vat_rate['rates'] as \$rate) { + if (strtotime(\$rate['start']) < \$order_date) { + \$rate_info = \$rate; + break; + } + } - // Update the base price component. - \$price['data']['components'][0]['price']['amount'] = \$prevat_base; + // If a valid rate is specified... + if (isset(\$rate_info['rate']) && is_numeric(\$rate_info['rate'])) { + // The amount of this vat is determined by dividing the current base price + // by 1 + the vat rate expressed as a decimal (i.e. 1.1 for a 10% vat). + // The result is the base price against which the vat would have been + // applied, so the difference becomes our vat amount. + \$vat_amount = \$price['data']['components'][0]['price']['amount'] - \$price['data']['components'][0]['price']['amount'] / (1 + \$rate_info['rate']); + \$vat_amount = commerce_vat_rate_round_amount(\$vat_amount); - // Prepare a vat component that will be added to the price after every vat - // has been calculated. - \$vat_components[\$vat_rate['price_component']] = array( - 'amount' => \$vat_amount, - 'currency_code' => \$price['currency_code'], - 'data' => array( - 'vat_rate' => \$vat_rate, - ), - ); + \$prevat_base = \$price['data']['components'][0]['price']['amount'] - \$vat_amount; + + // Update the base price component. + \$price['data']['components'][0]['price']['amount'] = \$prevat_base; + + // Prepare a vat component that will be added to the price after every vat + // has been calculated. + \$vat_components[\$vat_rate['price_component'] . '|' . \$rate_info['name']] = array( + 'amount' => \$vat_amount, + 'currency_code' => \$price['currency_code'], + 'data' => array( + 'vat_rate' => \$vat_rate, + 'vat_rate_info' => \$rate_info, + ), + ); + } } // Add their components to the price in their order of appearance, though.