Skip to content

Commit

Permalink
bug #260 Fix OrderItemsTaxesApplicator with removed units (aleho)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

This fixes an undefined offset error caused by holes in ArrayCollection keys after removing units.

Fixes #259

Commits
-------

97245d7 Fix OrderItemsTaxesApplicator with removed units
  • Loading branch information
GSadee authored Mar 4, 2021
2 parents 7016d49 + 97245d7 commit e902806
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 e902806

Please sign in to comment.