Skip to content

Commit

Permalink
T-13933/fix: Derive net amount from gross and tax amount
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkwr committed Nov 8, 2023
1 parent 1947ac8 commit adb89d4
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions Service/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function roundAmt($amt, $dp = 2): string
*/
public function getGrossAmountItem($item): float
{
return (float)$this->getNetAmountItem($item) + (float)$this->getTaxAmountItem($item);
return $this->getNetAmountAfterDiscountItem($item) + $this->getTaxAmountItem($item);
}

/**
Expand All @@ -248,7 +248,25 @@ public function getGrossAmountItem($item): float
*/
public function getNetAmountItem($item): float
{
return (float)$item->getRowTotal() - $this->getDiscountAmountItem($item);
return round($this->getGrossAmountItem($item), 2) - round($this->getTaxAmountItem($item), 2);
}

/**
* @param OrderItem|InvoiceItem|CreditmemoItem $item
* @return float
*/
public function getNetAmountBeforeDiscountItem($item): float
{
return (float)$item->getRowTotalInclTax() / (1 + $this->getTaxRateItem($item));
}

/**
* @param OrderItem|InvoiceItem|CreditmemoItem $item
* @return float
*/
public function getNetAmountAfterDiscountItem($item): float
{
return $this->getNetAmountBeforeDiscountItem($item) - $this->getDiscountAmountItem($item);
}

/**
Expand All @@ -257,7 +275,7 @@ public function getNetAmountItem($item): float
*/
public function getUnitPriceItem($item): float
{
return (float)$item->getRowTotalInclTax() / (1 + $this->getTaxRateItem($item)) / $item->getQtyOrdered();
return (float)$item->getPriceInclTax() / (1 + $this->getTaxRateItem($item));
}

/**
Expand All @@ -275,7 +293,7 @@ public function getTaxRateItem($item): float
*/
public function getTaxAmountItem($item): float
{
return $this->getNetAmountItem($item) * $this->getTaxRateItem($item);
return $this->getNetAmountAfterDiscountItem($item) * $this->getTaxRateItem($item);
}

/**
Expand All @@ -284,7 +302,12 @@ public function getTaxAmountItem($item): float
*/
public function getDiscountAmountItem($item): float
{
return abs((float)$item->getDiscountAmount()) - abs((float)$item->getDiscountTaxCompensationAmount());
$discount = abs((float)$item->getDiscountAmount());
$discountTaxCompensation = abs((float)$item->getDiscountTaxCompensationAmount());
if ($discountTaxCompensation > 0) {
$discountTaxCompensation = $discount * (1 - 1 / (1 + $this->getTaxRateItem($item)));
}
return $discount - $discountTaxCompensation;
}

/**
Expand Down

0 comments on commit adb89d4

Please sign in to comment.