mirror of
https://github.com/frappe/erpnext.git
synced 2026-04-21 15:48:29 +00:00
fix: prevent precision errors in discount distribution with inclusive tax
(cherry picked from commit 2068299766)
This commit is contained in:
@@ -672,6 +672,11 @@ class calculate_taxes_and_totals:
|
|||||||
else:
|
else:
|
||||||
self.grand_total_diff = 0
|
self.grand_total_diff = 0
|
||||||
|
|
||||||
|
# Apply rounding adjustment to grand_total_for_distributing_discount
|
||||||
|
# to prevent precision errors during discount distribution
|
||||||
|
if hasattr(self, "grand_total_for_distributing_discount") and not self.discount_amount_applied:
|
||||||
|
self.grand_total_for_distributing_discount += self.grand_total_diff
|
||||||
|
|
||||||
def calculate_totals(self):
|
def calculate_totals(self):
|
||||||
grand_total_diff = self.grand_total_diff
|
grand_total_diff = self.grand_total_diff
|
||||||
|
|
||||||
|
|||||||
@@ -59,3 +59,41 @@ class TestTaxesAndTotals(AccountsTestMixin, IntegrationTestCase):
|
|||||||
self.assertEqual(so.total, 1500)
|
self.assertEqual(so.total, 1500)
|
||||||
self.assertAlmostEqual(so.net_total, 1272.73, places=2)
|
self.assertAlmostEqual(so.net_total, 1272.73, places=2)
|
||||||
self.assertEqual(so.grand_total, 1400)
|
self.assertEqual(so.grand_total, 1400)
|
||||||
|
|
||||||
|
def test_100_percent_discount_with_inclusive_tax(self):
|
||||||
|
"""Test that 100% discount with inclusive taxes results in zero net_total"""
|
||||||
|
so = make_sales_order(do_not_save=1)
|
||||||
|
so.apply_discount_on = "Grand Total"
|
||||||
|
so.items[0].qty = 2
|
||||||
|
so.items[0].rate = 1300
|
||||||
|
so.append(
|
||||||
|
"taxes",
|
||||||
|
{
|
||||||
|
"charge_type": "On Net Total",
|
||||||
|
"account_head": "_Test Account VAT - _TC",
|
||||||
|
"cost_center": "_Test Cost Center - _TC",
|
||||||
|
"description": "Account VAT",
|
||||||
|
"included_in_print_rate": True,
|
||||||
|
"rate": 9,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
so.append(
|
||||||
|
"taxes",
|
||||||
|
{
|
||||||
|
"charge_type": "On Net Total",
|
||||||
|
"account_head": "_Test Account Service Tax - _TC",
|
||||||
|
"cost_center": "_Test Cost Center - _TC",
|
||||||
|
"description": "Account Service Tax",
|
||||||
|
"included_in_print_rate": True,
|
||||||
|
"rate": 9,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
so.save()
|
||||||
|
|
||||||
|
# Apply 100% discount
|
||||||
|
so.discount_amount = 2600
|
||||||
|
calculate_taxes_and_totals(so)
|
||||||
|
|
||||||
|
# net_total should be exactly 0, not 0.01
|
||||||
|
self.assertEqual(so.net_total, 0)
|
||||||
|
self.assertEqual(so.grand_total, 0)
|
||||||
|
|||||||
@@ -623,6 +623,12 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
} else {
|
} else {
|
||||||
me.grand_total_diff = 0;
|
me.grand_total_diff = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply rounding adjustment to grand_total_for_distributing_discount
|
||||||
|
// to prevent precision errors during discount distribution
|
||||||
|
if (me.grand_total_for_distributing_discount && !me.discount_amount_applied) {
|
||||||
|
me.grand_total_for_distributing_discount += me.grand_total_diff;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user