Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creditmemo creation error - Forgotten shipping discount tax compensation in creditmemo calculation ? #38088

Closed
1 of 5 tasks
Nuranto opened this issue Oct 16, 2023 · 17 comments
Closed
1 of 5 tasks
Assignees
Labels
Issue: needs update Additional information is require, waiting for response Reported on 2.4.6-p2 Indicates original Magento version for the Issue report.

Comments

@Nuranto
Copy link
Contributor

Nuranto commented Oct 16, 2023

Preconditions and environment

  • Magento version 2.4.6-p2

Steps to reproduce

It's kind hard to say how to reproduce step by step because it's a very very rare rounding issue on shipping amounts. But I'll try anyway, and then give the order details :

  1. Place an order*, with a discount (not sure if the discount is mandatory)
  2. Invoice & Ship order
  3. Create a creditmemo (via invoice) to refund shipping amount
  4. try to create another creditmemo (via invoice)

(*) Here is some order detail that might be relevant :

  • grand_total = 780.26
  • subtotal = 710.77
  • subtotal_incl_tax = 770.90
  • total_refunded = 125
  • tax_amount = 62.48
  • tax_invoiced = 62.48
  • tax_refunded = 11.36
  • discount_tax_compensation_amount = 9.01
  • discount_amount = -115.64
  • shipping_amount = 113.63
  • shipping_invoiced = 113.63
  • shipping_refunded = 113.63
  • shipping_tax_amount = 11.36
  • shipping_tax_refunded = 11.36
  • shipping_discount_tax_compensation_amount = 0.01
  • shipping_incl_tax = 125
  • shipping_discount_amount = 0

Expected result

We can create another creditmemo

Actual result

We can not create another creditmemo, and have an error message : Maximum shipping amount allowed to refund is: 0,00 €

Additional information

desiredAmount is 0.01 in

$desiredAmount = $this->priceCurrency->round($creditmemo->getBaseShippingAmount());
$maxAllowedAmount = ($useAmountsWithTax ? $baseAllowedAmountInclTax : $baseAllowedAmount);
$originalTotalAmount = ($useAmountsWithTax ? $orderBaseShippingInclTax : $orderBaseShippingAmount);
// Note: ($x < $y + 0.0001) means ($x <= $y) for floats
if ($desiredAmount < $this->priceCurrency->round($maxAllowedAmount) + 0.0001) {
// since the admin is returning less than the allowed amount, compute the ratio being returned
$ratio = 0;
if ($originalTotalAmount > 0) {
$ratio = $desiredAmount / $originalTotalAmount;
}
// capture amounts without tax
// Note: ($x > $y - 0.0001) means ($x >= $y) for floats
if ($desiredAmount > $maxAllowedAmount - 0.0001) {
$shippingAmount = $allowedAmount;
$baseShippingAmount = $baseAllowedAmount;
} else {
$shippingAmount = $this->priceCurrency->round($orderShippingAmount * $ratio);
$baseShippingAmount = $this->priceCurrency->round($orderBaseShippingAmount * $ratio);
}
$shippingInclTax = $this->priceCurrency->round($orderShippingInclTax * $ratio);
$baseShippingInclTax = $this->priceCurrency->round($orderBaseShippingInclTax * $ratio);
} else {
$maxAllowedAmount = $order->getBaseCurrency()->format($maxAllowedAmount, null, false);
throw new \Magento\Framework\Exception\LocalizedException(
__('Maximum shipping amount allowed to refund is: %1', $maxAllowedAmount)
);
}

So $desiredAmount < [..]$allowedAmount gives 0.01 < 0 which causes the exception. That 0.01 seems to correspond to shipping_discount_tax_compensation_amount field.

DesiredAmount is set previously here :

if (!isset($data['shipping_amount'])) {
$baseAllowedAmount = $this->getShippingAmount($invoice);
$creditmemo->setBaseShippingAmount($baseAllowedAmount);
}

and calculated here :

private function getShippingAmount(Invoice $invoice): float
{
$order = $invoice->getOrder();
$isShippingInclTax = $this->taxConfig->displaySalesShippingInclTax($order->getStoreId());
if ($isShippingInclTax) {
$amount = $order->getBaseShippingInclTax() -
$order->getBaseShippingRefunded() -
$order->getBaseShippingTaxRefunded();
} else {
$amount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded();
$amount = min($amount, $invoice->getBaseShippingAmount());
}
return (float)$amount;
}

As you can see, if we apply my order numbers in that code, it gives :

$amount = 125 - 113.63 - 11.36 = 0.01 => So I guess the calculation is forgetting about shipping_discount_tax_compensation_amount

And a fix would be :

$amount = $order->getBaseShippingInclTax() -
                $order->getBaseShippingRefunded() -
                $order->getBaseShippingTaxRefunded() -
                $order->getBaseShippingDiscountTaxCompensationAmnt();

However, tax calculation is so complex in Magento 2 that I won't try to make a PR for this. It would be safer if someone used to such calculations can have a look. However, this fix seems to fix the issue.

EDIT : This fix was not OK, and it seems unrelated from shipping_discount_tax_compensation_amount after all.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Oct 16, 2023

Hi @Nuranto. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

@Nuranto Nuranto changed the title Creditmemo creation error Creditmemo creation error - Forgotten shipping discount tax compensation in creditmemo calculation ? Oct 16, 2023
@engcom-Bravo engcom-Bravo added the Reported on 2.4.6-p2 Indicates original Magento version for the Issue report. label Oct 16, 2023
@engcom-Bravo engcom-Bravo self-assigned this Oct 16, 2023
@m2-assistant
Copy link

m2-assistant bot commented Oct 16, 2023

Hi @engcom-Bravo. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • 5. Add label Issue: Confirmed once verification is complete.
  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@m2-assistant
Copy link

m2-assistant bot commented Oct 17, 2023

Hi @engcom-Dash. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    1. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
    1. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
      - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

@engcom-Dash
Copy link

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

@engcom-Dash
Copy link

Hi @Nuranto
Thanks for reporting and collaboration. verified in magento 2.4 develop instance
and able to create another credit memo. Not able to reproduce the issue. Please refer the screenshots below.

Creditmemos:
creditmemos

Creditmemo1:
creditmemo1

Creditmemo2:
creditmemo2

@engcom-Dash engcom-Dash added Issue: needs update Additional information is require, waiting for response and removed Issue: ready for confirmation labels Oct 27, 2023
@Nuranto
Copy link
Contributor Author

Nuranto commented Nov 6, 2023

As I said, it is a very rare issue (we got 1 case for thousands of orders). It happened on a big order (douzens of products), with a cart discount. However, I don't know how to reproduce for sure I'm afraid.

@engcom-Dash
Copy link

Hi @Nuranto
Thanks for reporting and collaboration.
We have tested with number of products.Sometimes the issue depends on the operating system.Can you please let us know the operating system you are using?
Thanks.

@Nuranto
Copy link
Contributor Author

Nuranto commented Nov 8, 2023

We're running on docker php:8.1-apache-bullseye image.

Note : If you refunded Shipping fees on first creditmemo, why do you have 10.03 proposed in second creditmemo creation ? First creditmemo should refund 100% of shipping fees

@engcom-Dash
Copy link

@magento give me 2.4-develop instance

Copy link

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

Copy link

@engcom-Dash
Copy link

Hi @Nuranto

Thanks for reporting and collaboration.

we can divide the shipping fee into two creditmemos

We followed the steps to reproduce but not able to reproduce the issue.

1.Place an order*, with a discount (not sure if the discount is mandatory)
2.Invoice & Ship order
3.Create a creditmemo (via invoice) to refund shipping amount
4.try to create another creditmemo (via invoice)

Please let us know the detailed steps to reproduce the issue.

@engcom-Dash
Copy link

Hi @Nuranto

We have noticed that this issue has not been updated since long time.
Hence we assume that this issue is fixed now, so we are closing it. Please feel to raise a fresh ticket or reopen this ticket if you need more assistance on this.

Thanks.

@Nuranto
Copy link
Contributor Author

Nuranto commented Apr 17, 2024

(related to #38134)

@Nuranto
Copy link
Contributor Author

Nuranto commented Apr 17, 2024

Workaround Fix proposal, for those who have the issue, or if the issue is reopened one day :

Replace :

if ($desiredAmount < $this->priceCurrency->round($maxAllowedAmount) + 0.0001) { 
     // since the admin is returning less than the allowed amount, compute the ratio being returned 
     $ratio = 0; 
     if ($originalTotalAmount > 0) { 
         $ratio = $desiredAmount / $originalTotalAmount; 
     } 
     // capture amounts without tax 
     // Note: ($x > $y - 0.0001) means ($x >= $y) for floats 
     if ($desiredAmount > $maxAllowedAmount - 0.0001) { 
         $shippingAmount = $allowedAmount; 
         $baseShippingAmount = $baseAllowedAmount; 
     } else { 
         $shippingAmount = $this->priceCurrency->round($orderShippingAmount * $ratio); 
         $baseShippingAmount = $this->priceCurrency->round($orderBaseShippingAmount * $ratio); 
     } 
     $shippingInclTax = $this->priceCurrency->round($orderShippingInclTax * $ratio); 
     $baseShippingInclTax = $this->priceCurrency->round($orderBaseShippingInclTax * $ratio); 
 } else { 
     $maxAllowedAmount = $order->getBaseCurrency()->format($maxAllowedAmount, null, false); 
     throw new \Magento\Framework\Exception\LocalizedException( 
         __('Maximum shipping amount allowed to refund is: %1', $maxAllowedAmount) 
     ); 
 } 

With

if ($desiredAmount >= $this->priceCurrency->round($maxAllowedAmount) + 0.0001) { 
  $desiredAmount = $maxAllowedAmount;
}
     // since the admin is returning less than the allowed amount, compute the ratio being returned 
     $ratio = 0; 
     if ($originalTotalAmount > 0) { 
         $ratio = $desiredAmount / $originalTotalAmount; 
     } 
     // capture amounts without tax 
     // Note: ($x > $y - 0.0001) means ($x >= $y) for floats 
     if ($desiredAmount > $maxAllowedAmount - 0.0001) { 
         $shippingAmount = $allowedAmount; 
         $baseShippingAmount = $baseAllowedAmount; 
     } else { 
         $shippingAmount = $this->priceCurrency->round($orderShippingAmount * $ratio); 
         $baseShippingAmount = $this->priceCurrency->round($orderBaseShippingAmount * $ratio); 
     } 
     $shippingInclTax = $this->priceCurrency->round($orderShippingInclTax * $ratio); 
     $baseShippingInclTax = $this->priceCurrency->round($orderBaseShippingInclTax * $ratio); 

This doesn't fix the underlaying issue that I did not identified yet. But that will prevent the exception to be thrown without damaging creditmemo data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: needs update Additional information is require, waiting for response Reported on 2.4.6-p2 Indicates original Magento version for the Issue report.
Projects
None yet
Development

No branches or pull requests

3 participants