Merge pull request #37069 from marination/advance-unlink-impact
fix: Recalculate `advance_paid` in SO/PO after unlinking from advance entry
This commit is contained in:
commit
d4773872aa
@ -1801,6 +1801,10 @@ class TestSalesInvoice(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_outstanding_amount_after_advance_payment_entry_cancellation(self):
|
def test_outstanding_amount_after_advance_payment_entry_cancellation(self):
|
||||||
|
"""Test impact of advance PE submission/cancellation on SI and SO."""
|
||||||
|
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
|
||||||
|
|
||||||
|
sales_order = make_sales_order(item_code="138-CMS Shoe", qty=1, price_list_rate=500)
|
||||||
pe = frappe.get_doc(
|
pe = frappe.get_doc(
|
||||||
{
|
{
|
||||||
"doctype": "Payment Entry",
|
"doctype": "Payment Entry",
|
||||||
@ -1820,10 +1824,25 @@ class TestSalesInvoice(unittest.TestCase):
|
|||||||
"paid_to": "_Test Cash - _TC",
|
"paid_to": "_Test Cash - _TC",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
pe.append(
|
||||||
|
"references",
|
||||||
|
{
|
||||||
|
"reference_doctype": "Sales Order",
|
||||||
|
"reference_name": sales_order.name,
|
||||||
|
"total_amount": sales_order.grand_total,
|
||||||
|
"outstanding_amount": sales_order.grand_total,
|
||||||
|
"allocated_amount": 300,
|
||||||
|
},
|
||||||
|
)
|
||||||
pe.insert()
|
pe.insert()
|
||||||
pe.submit()
|
pe.submit()
|
||||||
|
|
||||||
|
sales_order.reload()
|
||||||
|
self.assertEqual(sales_order.advance_paid, 300)
|
||||||
|
|
||||||
si = frappe.copy_doc(test_records[0])
|
si = frappe.copy_doc(test_records[0])
|
||||||
|
si.items[0].sales_order = sales_order.name
|
||||||
|
si.items[0].so_detail = sales_order.get("items")[0].name
|
||||||
si.is_pos = 0
|
si.is_pos = 0
|
||||||
si.append(
|
si.append(
|
||||||
"advances",
|
"advances",
|
||||||
@ -1831,6 +1850,7 @@ class TestSalesInvoice(unittest.TestCase):
|
|||||||
"doctype": "Sales Invoice Advance",
|
"doctype": "Sales Invoice Advance",
|
||||||
"reference_type": "Payment Entry",
|
"reference_type": "Payment Entry",
|
||||||
"reference_name": pe.name,
|
"reference_name": pe.name,
|
||||||
|
"reference_row": pe.references[0].name,
|
||||||
"advance_amount": 300,
|
"advance_amount": 300,
|
||||||
"allocated_amount": 300,
|
"allocated_amount": 300,
|
||||||
"remarks": pe.remarks,
|
"remarks": pe.remarks,
|
||||||
@ -1839,7 +1859,13 @@ class TestSalesInvoice(unittest.TestCase):
|
|||||||
si.insert()
|
si.insert()
|
||||||
si.submit()
|
si.submit()
|
||||||
|
|
||||||
si.load_from_db()
|
si.reload()
|
||||||
|
pe.reload()
|
||||||
|
sales_order.reload()
|
||||||
|
|
||||||
|
# Check if SO is unlinked/replaced by SI in PE & if SO advance paid is 0
|
||||||
|
self.assertEqual(pe.references[0].reference_name, si.name)
|
||||||
|
self.assertEqual(sales_order.advance_paid, 0.0)
|
||||||
|
|
||||||
# check outstanding after advance allocation
|
# check outstanding after advance allocation
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
@ -1847,11 +1873,9 @@ class TestSalesInvoice(unittest.TestCase):
|
|||||||
flt(si.rounded_total - si.total_advance, si.precision("outstanding_amount")),
|
flt(si.rounded_total - si.total_advance, si.precision("outstanding_amount")),
|
||||||
)
|
)
|
||||||
|
|
||||||
# added to avoid Document has been modified exception
|
|
||||||
pe = frappe.get_doc("Payment Entry", pe.name)
|
|
||||||
pe.cancel()
|
pe.cancel()
|
||||||
|
si.reload()
|
||||||
|
|
||||||
si.load_from_db()
|
|
||||||
# check outstanding after advance cancellation
|
# check outstanding after advance cancellation
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
flt(si.outstanding_amount),
|
flt(si.outstanding_amount),
|
||||||
|
@ -581,6 +581,10 @@ def update_reference_in_journal_entry(d, journal_entry, do_not_save=False):
|
|||||||
"""
|
"""
|
||||||
jv_detail = journal_entry.get("accounts", {"name": d["voucher_detail_no"]})[0]
|
jv_detail = journal_entry.get("accounts", {"name": d["voucher_detail_no"]})[0]
|
||||||
|
|
||||||
|
# Update Advance Paid in SO/PO since they might be getting unlinked
|
||||||
|
if jv_detail.get("reference_type") in ("Sales Order", "Purchase Order"):
|
||||||
|
frappe.get_doc(jv_detail.reference_type, jv_detail.reference_name).set_total_advance_paid()
|
||||||
|
|
||||||
if flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) != 0:
|
if flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) != 0:
|
||||||
# adjust the unreconciled balance
|
# adjust the unreconciled balance
|
||||||
amount_in_account_currency = flt(d["unadjusted_amount"]) - flt(d["allocated_amount"])
|
amount_in_account_currency = flt(d["unadjusted_amount"]) - flt(d["allocated_amount"])
|
||||||
@ -647,6 +651,13 @@ def update_reference_in_payment_entry(
|
|||||||
|
|
||||||
if d.voucher_detail_no:
|
if d.voucher_detail_no:
|
||||||
existing_row = payment_entry.get("references", {"name": d["voucher_detail_no"]})[0]
|
existing_row = payment_entry.get("references", {"name": d["voucher_detail_no"]})[0]
|
||||||
|
|
||||||
|
# Update Advance Paid in SO/PO since they are getting unlinked
|
||||||
|
if existing_row.get("reference_doctype") in ("Sales Order", "Purchase Order"):
|
||||||
|
frappe.get_doc(
|
||||||
|
existing_row.reference_doctype, existing_row.reference_name
|
||||||
|
).set_total_advance_paid()
|
||||||
|
|
||||||
original_row = existing_row.as_dict().copy()
|
original_row = existing_row.as_dict().copy()
|
||||||
existing_row.update(reference_details)
|
existing_row.update(reference_details)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user