Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Created February 2, 2023 17:11
Show Gist options
  • Save ahebrank/ab0a7bd00082c3e751776b08b7dff199 to your computer and use it in GitHub Desktop.
Save ahebrank/ab0a7bd00082c3e751776b08b7dff199 to your computer and use it in GitHub Desktop.
diff --cc commerce_vat.module
index 1d7c018,e5b2420..0000000
--- a/commerce_vat.module
+++ b/commerce_vat.module
@@@ -311,44 -332,72 +327,47 @@@ function commerce_vat_rate_apply($vat_r
}
}
- // If a valid rate is specified...
- if (isset($rate_info['rate']) && is_numeric($rate_info['rate'])) {
+ $total_vat_price = 0;
+ foreach ($valid_vates as $rate_info) {
+
+ // If a valid rate is specified...
+ if (isset($rate_info['rate']) && is_numeric($rate_info['rate'])) {
- // Don't apply vat if the unit price has a NULL amount.
- if (is_null($wrapper->commerce_unit_price->value())) {
- return;
- }
+ // Don't apply vat if the unit price has a NULL amount.
+ if (is_null($wrapper->commerce_unit_price->value())) {
- return;
++ continue;
+ }
- // Invoke the vat rate's calculation callback and apply the returned vat
- // price to the line item.
- if ($vat_price = $vat_rate['calculation_callback']($vat_rate, $rate_info, $wrapper, $amount)) {
- // Add the vat to the unit price's data array along with a display inclusive
- // property used to track whether or not the vat is included in the price.
- $included = FALSE;
+ // Invoke the vat rate's calculation callback and apply the returned vat
+ // price to the line item.
+ if ($vat_price = $vat_rate['calculation_callback']($vat_rate, $rate_info, $wrapper)) {
+ // Add the vat to the unit price's data array along with a display inclusive
+ // property used to track whether or not the vat is included in the price.
+ $included = FALSE;
- $direction = variable_get('commerce_vat_direction', 'forward');
-
- if ($direction == 'reverse') {
- $data = $wrapper->commerce_unit_price->data->value();
- $data['components'][0]['price']['amount'] = $data['components'][0]['price']['amount'] - $vat_price['amount'];
- $wrapper->commerce_unit_price->data = $data;
- $included = TRUE;
- }
- else {
- // Include the vat amount in the displayed unit price.
- $wrapper->commerce_unit_price->amount = $wrapper->commerce_unit_price->amount->value() + $vat_price['amount'];
- $included = TRUE;
- }
-
- // Update the data array with the vat component.
- $wrapper->commerce_unit_price->data = commerce_price_component_add(
- $wrapper->commerce_unit_price->value(),
- $vat_rate['price_component'] . '|' . $rate_info['name'],
- $vat_price,
- $included
- );
-
- return $vat_price;
+ $direction = variable_get('commerce_vat_direction', 'forward');
+
+ if ($direction == 'reverse') {
+ $data = $wrapper->commerce_unit_price->data->value();
- $amount = $wrapper->commerce_unit_price->amount->value();
- // For 0 amount line items the VAT is also 0.
- if ($amount == 0) {
- continue;
- }
- // Reduce components proportionally.
- $amount_to_reduce_total = $vat_price['amount'];
- $amount_to_reduce_left = $vat_price['amount'];
- $nonzero_components = array();
- foreach ($data['components'] as &$component) {
- // Do not apply vat on vat components.
- if (strpos($component['name'], 'vat') === 0) {
- continue;
- }
- if ($component['price']['amount']) {
- $nonzero_components[] = &$component;
- }
- $amount_to_reduce_current = commerce_vat_rate_round_amount($component['price']['amount'] / $amount * $amount_to_reduce_total, $vat_price['currency_code']);
- $component['price']['amount'] -= $amount_to_reduce_current;
- $amount_to_reduce_left -= $amount_to_reduce_current;
- }
- // Change components only if there was a nonzero-one.
- if (!empty($nonzero_components)) {
- // Remaining rounding difference deliberately reduces first nonzero component.
- $nonzero_components[0]['price']['amount'] -= $amount_to_reduce_left;
- $wrapper->commerce_unit_price->data = $data;
- $included = TRUE;
- }
++ $data['components'][0]['price']['amount'] = $data['components'][0]['price']['amount'] - $vat_price['amount'];
++ $wrapper->commerce_unit_price->data = $data;
++ $included = TRUE;
+ }
+ else {
+ // Include the vat amount in the displayed unit price.
+ $wrapper->commerce_unit_price->amount = $wrapper->commerce_unit_price->amount->value() + $vat_price['amount'];
+ $included = TRUE;
+ }
+
+ // Update the data array with the vat component.
+ $wrapper->commerce_unit_price->data = commerce_price_component_add(
+ $wrapper->commerce_unit_price->value(),
+ $vat_rate['price_component'] . '|' . $rate_info['name'],
+ $vat_price,
+ $included
+ );
+ $total_vat_price += $vat_price;
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment