Looking for an answer to my issue brought me in the first place to this 4 years old issue, describing more or less what i am dealing with currently :
https://www.drupal.org/node/1352916
Instead of attaching my issue over there, i'd like to explain it more generally in Drupal Commerce.
I developed a custom shipping module, starting from the available example in Commerce Shipping and written following hooks and callback functions :
hook_commerce_shipping_method_info()
hook_commerce_shipping_service_info()
hook_commerce_price_component_type_info()
custom_shipping_service_rate($shipping_service, $order)
custom_shipping_service_details_form($pane_form, $pane_values, $checkout_pane, $order, $shipping_service)
custom_shipping_service_details_form_validate($details_form, $details_values, $shipping_service, $order, $form_parents)
custom_shipping_service_details_form_submit($details_form, $details_values, $line_item)
An example of my shipping pane is visible in attachment "Commerce_shipping_insurance_example.JPG"
The result after completing this pane is visible in attachment "VAT-on-shipping-insurance-missing.JPG"
9 times out of 10, the VAT total amount does not contain the VAT applicable on the shipping insurance price component which i have added manually inside shipping_service_details_form_submit() function.
function custom_shipping_service_details_form_submit($details_form, $details_values, $line_item) {
if ($details_values && $details_values['insurance']) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$insurance_amount = $details_form['insurance']['#amount'];
$order_currency_code = $line_item_wrapper->commerce_unit_price->currency_code->value();
if($order_currency_code != 'EUR' && module_exists('commerce_multicurrency')) {
$insurance_amount = round(commerce_multicurrency_conversion($insurance_amount, 'EUR', $order_currency_code));
}
($insurance_amount > 0) ? $vat_rate = get_order_vat_rate(commerce_order_load($line_item->order_id)) : $vat_rate = 0;
dpm($vat_rate);
// Build a price array for the insurance
$insurance_price = array(
'amount' => $insurance_amount,
'currency_code' => $order_currency_code,
' data' => array(),
);
// Add the insurance upcharge to the line item unit price.
$line_item_wrapper->commerce_unit_price->amount
= $line_item_wrapper->commerce_unit_price->amount->value() + $insurance_amount*(1+$vat_rate);
// Add the VAT upcharge to the line item VAT component price.
if($vat_rate > 0) {
foreach($line_item->commerce_unit_price['und'][0]['data']['components'] as $component) {
if(substr($component['name'],0,3) == 'vat') {
dpm($component);
$component['price']['amount'] = $component['price']['amount'] + round($insurance_amount * $vat_rate);
dpm($component);
break;
}
}
}
// Add the insurance fee component to the unit price.
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'bpost_shipping_service_insurance',
$insurance_price,
FALSE,
TRUE
);
}
I can see with my 2 dpm($component)'s (before and after) that the insurance vat is added to the shipping VAT amount, but it's not reflected on the review pane and later on in the invoice either.
I have the rule Calculate Shipping VAT enabled on the Calculate Shipping rate event.
Somebody have a clue why this isn't working (all the time) ?
Comment | File | Size | Author |
---|---|---|---|
#2 | VAT-on-shipping-insurance-OK.JPG | 44.33 KB | RAWDESK |
VAT-on-shipping-insurance-missing.JPG | 53.25 KB | RAWDESK | |
Commerce_shipping_insurance_example.JPG | 35.58 KB | RAWDESK |
Comments
Comment #2
RAWDESK CreditAttribution: RAWDESK commentedUPDATE:
Fixed by simply adding the php passing reference & to the $line_item function variable :
The VAT upcharge for the shipping insurance is now correctly applied in the pricing totals
Comment #3
RAWDESK CreditAttribution: RAWDESK commented