diff --git a/erpnext/accounts/doctype/sales_invoice/test_down_payment.py b/erpnext/accounts/doctype/sales_invoice/test_down_payment.py index a192bdadcd04068c7d284d1b969ac2bf364c0d66..509a41d9d93cb8cc61a9e7971ff33d178a921334 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_down_payment.py +++ b/erpnext/accounts/doctype/sales_invoice/test_down_payment.py @@ -278,9 +278,7 @@ class TestDownPaymentMultiplePayments(FrappeTestCase): } ) - def create_down_payment_sales_invoice( - self, sales_order: "frappe.Document", percentage: float = 0.3 - ): + def create_down_payment_sales_invoice(self, sales_order: "frappe.Document", percentage: float = 0.3): return frappe.get_doc( { "doctype": "Sales Invoice", @@ -367,6 +365,10 @@ class TestDownPaymentMultiplePayments(FrappeTestCase): pe.submit().reload() advances.append(pe) + # Check if advance has been correctly updated in Sales order + advance_paid = frappe.db.get_value("Sales Order", so.name, "advance_paid") + self.assertEqual(advance_paid, 3000) + # Create a draft Sales Invoice against the Sales Order, and add the payments as advances si = self.create_sales_invoice(sales_order=so) for dp in advances: diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index c7bd470cded96ff2c1f8f992af7797af40dc95a1..070556890be5f0e09c32ff33f5c1585a2085e2e1 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1916,6 +1916,11 @@ class AccountsController(TransactionBase): .where((ple.party == party) & (ple.docstatus == 1) & (ple.company == self.company)) ) + # @dokos + advance_query_condition = (ple.against_voucher_type == self.doctype) & ( + ple.against_voucher_no == self.name + ) + if self.doctype == "Sales Order": si = frappe.qb.DocType("Sales Invoice") sii = frappe.qb.DocType("Sales Invoice Item") @@ -1927,18 +1932,17 @@ class AccountsController(TransactionBase): .where((si.is_down_payment_invoice == 1) & (sii.sales_order == self.name)) .run(as_list=True) ) + if down_payment_invoices and down_payment_invoices[0]: - advance_query = advance_query.where( - ((ple.against_voucher_type == self.doctype) & (ple.against_voucher_no == self.name)) - | ( - (ple.against_voucher_type == "Sales Invoice") - & (ple.against_voucher_no.isin(down_payment_invoices[0])) - ) + advance_query_condition = ( + (ple.against_voucher_type == self.doctype) & (ple.against_voucher_no == self.name) + ) | ( + (ple.against_voucher_type == "Sales Invoice") + & (ple.against_voucher_no.isin(down_payment_invoices[0])) ) - advance_query = advance_query.where( - (ple.against_voucher_type == self.doctype) & (ple.against_voucher_no == self.name) - ) + advance_query = advance_query.where(advance_query_condition) + # @dokos advance = advance_query.run(as_dict=True) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 6895db6ef81b2f61ea80a210e44f2d32e3941157..cb5888ea46f3e1879ae9ed579df2a4f6bb0fd6d7 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -383,6 +383,8 @@ erpnext.patches.dokos.v4_0.set_mode_of_payment_in_payment_requests erpnext.patches.dokos.v4_0.add_customer_to_payment_request erpnext.accounts.doctype.mode_of_payment.patches.migrate_fees_and_cost_center_to_account_table execute:frappe.delete_doc_if_exists("DocType", "Integration References") +execute:frappe.delete_doc_if_exists("DocType", "Social Media Post") +erpnext.patches.dokos.v4_0.update_advance_for_down_payments # @dokos diff --git a/erpnext/patches/dokos/v4_0/update_advance_for_down_payments.py b/erpnext/patches/dokos/v4_0/update_advance_for_down_payments.py new file mode 100644 index 0000000000000000000000000000000000000000..fd7e0a62636ec70e927ee939482fd768bc9444ff --- /dev/null +++ b/erpnext/patches/dokos/v4_0/update_advance_for_down_payments.py @@ -0,0 +1,12 @@ +import frappe + + +def execute(): + for invoice in frappe.get_all("Sales Invoice", filters={"is_down_payment_invoice": 1, "docstatus": 1}): + for payment in frappe.get_all( + "Payment Entry Reference", + filters={"reference_doctype": "Sales Invoice", "reference_name": invoice.name}, + pluck="parent", + ): + doc = frappe.get_doc("Payment Entry", payment) + doc.run_method("update_advance_paid")