From 074a7065bed1d52c57c570fa1f1e853236fbba3f Mon Sep 17 00:00:00 2001 From: Ravibharathi <131471282+ravibharathi656@users.noreply.github.com> Date: Tue, 29 Jul 2025 20:35:12 +0530 Subject: [PATCH] fix: update advance paid amount on unreconcile (cherry picked from commit 99f7eb38d383c0e5c42d048235d4d324f34bb84d) --- .../test_unreconcile_payment.py | 61 ++++++++++++++++++- .../unreconcile_payment.py | 15 +++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/unreconcile_payment/test_unreconcile_payment.py b/erpnext/accounts/doctype/unreconcile_payment/test_unreconcile_payment.py index c058dbfa0b8..021703f6483 100644 --- a/erpnext/accounts/doctype/unreconcile_payment/test_unreconcile_payment.py +++ b/erpnext/accounts/doctype/unreconcile_payment/test_unreconcile_payment.py @@ -9,6 +9,7 @@ from erpnext.accounts.doctype.payment_entry.test_payment_entry import create_pay from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice from erpnext.accounts.party import get_party_account from erpnext.accounts.test.accounts_mixin import AccountsTestMixin +from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order @@ -17,6 +18,7 @@ class TestUnreconcilePayment(AccountsTestMixin, FrappeTestCase): def setUp(self): self.create_company() self.create_customer() + self.create_supplier() self.create_usd_receivable_account() self.create_item() self.clear_old_entries() @@ -364,13 +366,13 @@ class TestUnreconcilePayment(AccountsTestMixin, FrappeTestCase): # Assert 'Advance Paid' so.reload() pe.reload() - self.assertEqual(so.advance_paid, 100) + self.assertEqual(so.advance_paid, 0) self.assertEqual(len(pe.references), 0) self.assertEqual(pe.unallocated_amount, 100) pe.cancel() so.reload() - self.assertEqual(so.advance_paid, 100) + self.assertEqual(so.advance_paid, 0) def test_06_unreconcile_advance_from_payment_entry(self): self.enable_advance_as_liability() @@ -417,7 +419,7 @@ class TestUnreconcilePayment(AccountsTestMixin, FrappeTestCase): so2.reload() pe.reload() self.assertEqual(so1.advance_paid, 150) - self.assertEqual(so2.advance_paid, 110) + self.assertEqual(so2.advance_paid, 0) self.assertEqual(len(pe.references), 1) self.assertEqual(pe.unallocated_amount, 110) @@ -468,3 +470,56 @@ class TestUnreconcilePayment(AccountsTestMixin, FrappeTestCase): self.assertEqual(so.advance_paid, 1000) self.disable_advance_as_liability() + + def test_unreconcile_advance_from_journal_entry(self): + po = create_purchase_order( + company=self.company, + supplier=self.supplier, + item=self.item, + qty=1, + rate=100, + transaction_date=today(), + do_not_submit=False, + ) + + je = frappe.get_doc( + { + "doctype": "Journal Entry", + "company": self.company, + "voucher_type": "Journal Entry", + "posting_date": po.transaction_date, + "multi_currency": True, + "accounts": [ + { + "account": "Creditors - _TC", + "party_type": "Supplier", + "party": po.supplier, + "debit_in_account_currency": 100, + "is_advance": "Yes", + "reference_type": po.doctype, + "reference_name": po.name, + }, + {"account": "Cash - _TC", "credit_in_account_currency": 100}, + ], + } + ) + je.save().submit() + po.reload() + self.assertEqual(po.advance_paid, 100) + + unreconcile = frappe.get_doc( + { + "doctype": "Unreconcile Payment", + "company": self.company, + "voucher_type": je.doctype, + "voucher_no": je.name, + } + ) + unreconcile.add_references() + self.assertEqual(len(unreconcile.allocations), 1) + allocations = [x.reference_name for x in unreconcile.allocations] + self.assertEqual([po.name], allocations) + unreconcile.save().submit() + + po.reload() + self.assertEqual(po.advance_paid, 0) diff --git a/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py b/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py index e57b90f11f7..f7c826bf3fc 100644 --- a/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py +++ b/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py @@ -86,10 +86,25 @@ class UnreconcilePayment(Document): alloc.reference_doctype, alloc.reference_name, alloc.account, alloc.party_type, alloc.party ) if doc.doctype in get_advance_payment_doctypes(): + self.make_advance_payment_ledger(alloc) doc.set_total_advance_paid() frappe.db.set_value("Unreconcile Payment Entries", alloc.name, "unlinked", True) + def make_advance_payment_ledger(self, alloc): + if alloc.allocated_amount > 0: + doc = frappe.new_doc("Advance Payment Ledger Entry") + doc.company = self.company + doc.voucher_type = self.voucher_type + doc.voucher_no = self.voucher_no + doc.against_voucher_type = alloc.reference_doctype + doc.against_voucher_no = alloc.reference_name + doc.amount = -1 * alloc.allocated_amount + doc.event = "Unreconcile" + doc.currency = alloc.account_currency + doc.flags.ignore_permissions = 1 + doc.save() + @frappe.whitelist() def doc_has_references(doctype: str | None = None, docname: str | None = None):