Skip to content

Commit

Permalink
Fix OrderItemsTaxesApplicator with removed units
Browse files Browse the repository at this point in the history
This fixes an undefined offset error caused by holes in ArrayCollection
keys after removing units.
  • Loading branch information
aleho committed Mar 2, 2021
1 parent 7016d49 commit 97245d7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions spec/TaxesApplicator/OrderItemsTaxesApplicatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function it_applies_taxes_on_units_based_on_item_total_and_rate(
$taxRate->isIncludedInPrice()->willReturn(false);

$orderItem->getUnits()->willReturn($units);
$units->getIterator()->willReturn(new \ArrayIterator([$unit1->getWrappedObject(), $unit2->getWrappedObject()]));
$units->getIterator()->willReturn(new \ArrayIterator([4 => $unit1->getWrappedObject(), 5 => $unit2->getWrappedObject()]));

$distributor->distribute(100, 2)->willReturn([50, 50]);

Expand Down Expand Up @@ -191,7 +191,7 @@ function it_does_not_apply_taxes_with_amount_0(
$taxRate->isIncludedInPrice()->willReturn(false);

$orderItem->getUnits()->willReturn($units);
$units->getIterator()->willReturn(new \ArrayIterator([$unit1->getWrappedObject(), $unit2->getWrappedObject()]));
$units->getIterator()->willReturn(new \ArrayIterator([4 => $unit1->getWrappedObject(), 5 => $unit2->getWrappedObject()]));

$distributor->distribute(0, 2)->willReturn([0, 0]);

Expand Down
8 changes: 5 additions & 3 deletions src/TaxesApplicator/OrderItemsTaxesApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ public function apply(OrderInterface $order, ZoneInterface $zone): void
$totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate);
$splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity);

$i = 0;
/** @var OrderItemUnitInterface $unit */
foreach ($item->getUnits() as $index => $unit) {
if (0 === $splitTaxes[$index]) {
foreach ($item->getUnits() as $unit) {
if (0 === $splitTaxes[$i]) {
continue;
}

$this->addAdjustment($unit, $splitTaxes[$index], $taxRate);
$this->addAdjustment($unit, $splitTaxes[$i], $taxRate);
++$i;
}
}
}
Expand Down

0 comments on commit 97245d7

Please sign in to comment.