Skip to content
Snippets Groups Projects

Issue #3254168: Support taxIncluded in AvaTax response

1 file
+ 28
20
Compare changes
  • Side-by-side
  • Inline
@@ -110,42 +110,50 @@ class Avatax extends RemoteTaxTypeBase {
@@ -110,42 +110,50 @@ class Avatax extends RemoteTaxTypeBase {
foreach ($response_body['lines'] as $tax_adjustment) {
foreach ($response_body['lines'] as $tax_adjustment) {
$label = isset($tax_adjustment['details'][0]['taxName']) ? Html::escape($tax_adjustment['details'][0]['taxName']) : $this->t('Sales tax');
$label = isset($tax_adjustment['details'][0]['taxName']) ? Html::escape($tax_adjustment['details'][0]['taxName']) : $this->t('Sales tax');
$adjustments[$tax_adjustment['lineNumber']] = [
$adjustments[$tax_adjustment['lineNumber']] = [
'amount' => $tax_adjustment['tax'],
'type' => 'tax',
'label' => $label,
'label' => $label,
 
'amount' => new Price ((string) $tax_adjustment['tax'], $currency_code),
 
'source_id' => $this->pluginId . '|' . $this->parentEntity->id(),
 
'included' => !empty($tax_adjustment['taxIncluded']),
];
];
}
}
// Add tax adjustments to order items.
// Add tax adjustments to order items.
foreach ($order->getItems() as $order_item) {
foreach ($order->getItems() as $order_item) {
if (!isset($adjustments[$order_item->uuid()])) {
$adjustment = $adjustments[$order_item->uuid()] ?? NULL;
 
if (!$adjustment) {
continue;
continue;
}
}
$order_item->addAdjustment(new Adjustment([
$order_item->addAdjustment(new Adjustment($adjustment));
'type' => 'tax',
'label' => $adjustments[$order_item->uuid()]['label'],
'amount' => new Price((string) $adjustments[$order_item->uuid()]['amount'], $currency_code),
'source_id' => $this->pluginId . '|' . $this->parentEntity->id(),
]));
$applied_adjustments[$order_item->uuid()] = $order_item->uuid();
$applied_adjustments[$order_item->uuid()] = $order_item->uuid();
}
}
// If we still have Tax adjustments to apply, add a single one to the order.
// If we still have Tax adjustments to apply, add it/them to the order.
$remaining_adjustments = array_diff_key($adjustments, $applied_adjustments);
$remaining_adjustments = array_diff_key($adjustments, $applied_adjustments);
if (!$remaining_adjustments) {
if (!$remaining_adjustments) {
return;
return;
}
}
$tax_adjustment_total = NULL;
// Calculate the total Tax adjustment to add.
// Combine remaining adjustments being careful not to mix inclusive with
foreach ($remaining_adjustments as $remaining_adjustment) {
// non-inclusive.
$adjustment_amount = new Price((string) $remaining_adjustment['amount'], $currency_code);
// @todo This could potentially be handled by creating Adjustment objects
$tax_adjustment_total = $tax_adjustment_total ? $tax_adjustment_total->add($adjustment_amount) : $adjustment_amount;
// and using the adjustment transformer, but it currently doesn't handle
 
// a mixture of included and non-included adjustments.
 
// @see https://www.drupal.org/project/commerce/issues/3285502
 
foreach ($remaining_adjustments as $adjustment) {
 
$index = $adjustment['included'] ? 1 : 0;
 
if (isset($combined[$index])) {
 
$combined[$index]['amount']->add($adjustment['amount']);
 
}
 
else {
 
$combined[$index] = $adjustment;
 
}
 
}
 
foreach ($combined as $adjustment) {
 
$order->addAdjustment(new Adjustment([
 
'label' => $this->t('Sales tax'),
 
] + $adjustment));
}
}
$order->addAdjustment(new Adjustment([
'type' => 'tax',
'label' => $this->t('Sales tax'),
'amount' => $tax_adjustment_total,
'source_id' => $this->pluginId . '|' . $this->parentEntity->id(),
]));
}
}
}
}
Loading