From 36b951d018f5a4fe88f1f1cd2e67ec2d476af5ba Mon Sep 17 00:00:00 2001 From: venkat102 Date: Fri, 21 Mar 2025 13:17:32 +0530 Subject: [PATCH 1/3] fix(payment term): allocate payment amount when payment term is fetched from order (cherry picked from commit 5618859bd8e7a11f6bdf3a9be123c2d742aaa343) --- erpnext/controllers/accounts_controller.py | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 89fd7e30ef2..c455a68a80c 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -2100,7 +2100,7 @@ class AccountsController(TransactionBase): and automatically_fetch_payment_terms and self.linked_order_has_payment_terms(po_or_so, fieldname, doctype) ): - self.fetch_payment_terms_from_order(po_or_so, doctype) + self.fetch_payment_terms_from_order(po_or_so, doctype, grand_total, base_grand_total) if self.get("payment_terms_template"): self.ignore_default_payment_terms_template = 1 elif self.get("payment_terms_template"): @@ -2141,7 +2141,7 @@ class AccountsController(TransactionBase): d.payment_amount * self.get("conversion_rate"), d.precision("base_payment_amount") ) else: - self.fetch_payment_terms_from_order(po_or_so, doctype) + self.fetch_payment_terms_from_order(po_or_so, doctype, grand_total, base_grand_total) self.ignore_default_payment_terms_template = 1 def get_order_details(self): @@ -2179,7 +2179,7 @@ class AccountsController(TransactionBase): def linked_order_has_payment_schedule(self, po_or_so): return frappe.get_all("Payment Schedule", filters={"parent": po_or_so}) - def fetch_payment_terms_from_order(self, po_or_so, po_or_so_doctype): + def fetch_payment_terms_from_order(self, po_or_so, po_or_so_doctype, grand_total, base_grand_total): """ Fetch Payment Terms from Purchase/Sales Order on creating a new Purchase/Sales Invoice. """ @@ -2195,12 +2195,25 @@ class AccountsController(TransactionBase): "invoice_portion": schedule.invoice_portion, "mode_of_payment": schedule.mode_of_payment, "description": schedule.description, - "payment_amount": schedule.payment_amount, - "base_payment_amount": schedule.base_payment_amount, - "outstanding": schedule.outstanding, "paid_amount": schedule.paid_amount, } + if payment_schedule["invoice_portion"]: + payment_schedule["payment_amount"] = flt( + grand_total * flt(payment_schedule["invoice_portion"]) / 100, + schedule.precision("payment_amount"), + ) + payment_schedule["base_payment_amount"] = flt( + base_grand_total * flt(payment_schedule["invoice_portion"]) / 100, + schedule.precision("base_payment_amount"), + ) + payment_schedule["outstanding"] = payment_schedule["payment_amount"] + else: + payment_schedule["base_payment_amount"] = flt( + schedule.base_payment_amount * self.get("conversion_rate"), + schedule.precision("base_payment_amount"), + ) + if schedule.discount_type == "Percentage": payment_schedule["discount_type"] = schedule.discount_type payment_schedule["discount"] = schedule.discount From 4e0d7d88ecd42571d21a5379d7f43dc966d3dd43 Mon Sep 17 00:00:00 2001 From: venkat102 Date: Fri, 21 Mar 2025 13:18:19 +0530 Subject: [PATCH 2/3] test: validate payment schedule based on invoice amount (cherry picked from commit 77852965736add87613d2c65a4fee27de8d681f8) --- .../accounts/doctype/purchase_invoice/test_purchase_invoice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 32e9ceefdbd..7d52fdcff25 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -1852,7 +1852,7 @@ class TestPurchaseInvoice(FrappeTestCase, StockTestMixin): 1, ) pi = make_pi_from_pr(pr.name) - self.assertEqual(pi.payment_schedule[0].payment_amount, 2500) + self.assertEqual(pi.payment_schedule[0].payment_amount, 1000) automatically_fetch_payment_terms(enable=0) frappe.db.set_value( From dda35b8e51a3c3414fccee7f8beba9f5d2f0667b Mon Sep 17 00:00:00 2001 From: venkat102 Date: Thu, 27 Mar 2025 22:28:20 +0530 Subject: [PATCH 3/3] fix: update payment amount if automatically_fetch_payment_terms is enabled (cherry picked from commit 7bf1a39861e2841c04ea7818de6d03028a095fee) --- erpnext/controllers/accounts_controller.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index c455a68a80c..0ba33999c5c 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -2100,7 +2100,9 @@ class AccountsController(TransactionBase): and automatically_fetch_payment_terms and self.linked_order_has_payment_terms(po_or_so, fieldname, doctype) ): - self.fetch_payment_terms_from_order(po_or_so, doctype, grand_total, base_grand_total) + self.fetch_payment_terms_from_order( + po_or_so, doctype, grand_total, base_grand_total, automatically_fetch_payment_terms + ) if self.get("payment_terms_template"): self.ignore_default_payment_terms_template = 1 elif self.get("payment_terms_template"): @@ -2141,7 +2143,9 @@ class AccountsController(TransactionBase): d.payment_amount * self.get("conversion_rate"), d.precision("base_payment_amount") ) else: - self.fetch_payment_terms_from_order(po_or_so, doctype, grand_total, base_grand_total) + self.fetch_payment_terms_from_order( + po_or_so, doctype, grand_total, base_grand_total, automatically_fetch_payment_terms + ) self.ignore_default_payment_terms_template = 1 def get_order_details(self): @@ -2179,7 +2183,9 @@ class AccountsController(TransactionBase): def linked_order_has_payment_schedule(self, po_or_so): return frappe.get_all("Payment Schedule", filters={"parent": po_or_so}) - def fetch_payment_terms_from_order(self, po_or_so, po_or_so_doctype, grand_total, base_grand_total): + def fetch_payment_terms_from_order( + self, po_or_so, po_or_so_doctype, grand_total, base_grand_total, automatically_fetch_payment_terms + ): """ Fetch Payment Terms from Purchase/Sales Order on creating a new Purchase/Sales Invoice. """ @@ -2198,7 +2204,7 @@ class AccountsController(TransactionBase): "paid_amount": schedule.paid_amount, } - if payment_schedule["invoice_portion"]: + if automatically_fetch_payment_terms: payment_schedule["payment_amount"] = flt( grand_total * flt(payment_schedule["invoice_portion"]) / 100, schedule.precision("payment_amount"),