Skip to content

Commit

Permalink
CET-510/fix: Fix magento calculations
Browse files Browse the repository at this point in the history
At present, we derive the gross amount for line item. This is causing rounding error in some cases.
Instead we should use gross amount provided by magento
  • Loading branch information
brtkwr committed Nov 15, 2024
1 parent a1a60b4 commit 704e053
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions Service/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public function getProductImageUrl(Product $product): string
*
* @param mixed $amt
* @param int $dp
*
* @return string
*/
public function roundAmt($amt, $dp = 2): string
Expand All @@ -234,77 +235,81 @@ public function roundAmt($amt, $dp = 2): string
}

/**
* Get gross amount (inclusive of tax after discount)
*
* @param OrderItem|InvoiceItem|CreditmemoItem $item
*
* @return float
*/
public function getGrossAmountItem($item): float
{
return $this->getNetAmountItem($item) + $this->roundAmt($this->getTaxAmountItem($item));
}
return $this->getNetAmountItem($item) + $this->getTaxAmountItem($item);

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

/**
* Get net amount (exclusive of tax after discount)
*
* @param OrderItem|InvoiceItem|CreditmemoItem $item
* @return float
*/
public function getNetAmountBeforeDiscountItem($item): float
public function getNetAmountItem($item): float
{
return (float)$item->getRowTotalInclTax() / (1 + $this->getTaxRateItem($item));
return (float)$item->getRowTotal() - $this->getDiscountAmountItem($item);
}

/**
* Get unit price
*
* @param OrderItem|InvoiceItem|CreditmemoItem $item
*
* @return float
*/
public function getUnitPriceItem($item): float
{
return $this->getNetAmountBeforeDiscountItem($item) / $item->getQtyOrdered();
// $item->getRowTotal() is before discount
return (float)$item->getRowTotal() / (float)$item->getQtyOrdered();
}

/**
* @param OrderItem|InvoiceItem|CreditmemoItem $item
*
* @return float
*/
public function getTaxRateItem($item): float
{
return $item->getTaxPercent() / 100;
return (float)$item->getTaxPercent() / 100;
}

/**
* Get tax amount inclusive of discount
*
* @param OrderItem|InvoiceItem|CreditmemoItem $item
*
* @return float
*/
public function getTaxAmountItem($item): float
{
return $this->getNetAmountItem($item) * $this->getTaxRateItem($item);
return (float)$item->getTaxAmount();
}

/**
* Get discount amount before tax
*
* @param OrderItem|InvoiceItem|CreditmemoItem $item
*
* @return float
*/
public function getDiscountAmountItem($item): float
{
$discount = abs((float)$item->getDiscountAmount());
$discountTaxCompensation = abs((float)$item->getDiscountTaxCompensationAmount());
if ($discountTaxCompensation > 0) {
$discountTaxCompensation = $discount * (1 - 1 / (1 + $this->getTaxRateItem($item)));
}
return $discount - $discountTaxCompensation;
return (float)$item->getDiscountAmount() - (float)$item->getDiscountTaxCompensationAmount();
}

/**
* Get category array by category ids
*
* @param array $categoryIds
*
* @return array
* @throws LocalizedException
*/
Expand All @@ -324,7 +329,9 @@ public function getCategories(array $categoryIds): array

/**
* Get category collection
* @param $categoryIds
*
* @param array $categoryIds
*
* @return Collection
*/
private function getCategoryCollection($categoryIds): Collection
Expand Down

0 comments on commit 704e053

Please sign in to comment.