From 54e6a314bc1a0524347a4bb909b7f5a5a2be4049 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 16:47:01 +0530 Subject: [PATCH 01/11] refactor: remove repeated conditions in get_party_account --- erpnext/accounts/party.py | 68 ++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 008614e6d3f4..8f7a82f54d46 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -392,7 +392,7 @@ def set_account_and_due_date( @frappe.whitelist() -def get_party_account(party_type, party=None, company=None, include_advance=False): +def get_party_account(party_type, party=None, company=None): """Returns the account for the given `party`. Will first search in party (Customer / Supplier) record, if not found, will search in group (Customer Group / Supplier Group), @@ -400,31 +400,30 @@ def get_party_account(party_type, party=None, company=None, include_advance=Fals if not company: frappe.throw(_("Please select a Company")) - if not party and party_type in ["Customer", "Supplier"]: - default_account_name = ( - "default_receivable_account" if party_type == "Customer" else "default_payable_account" - ) - - return frappe.get_cached_value("Company", company, default_account_name) - - account = frappe.db.get_value( - "Party Account", {"parenttype": party_type, "parent": party, "company": company}, "account" - ) + account = None - if not account and party_type in ["Customer", "Supplier"]: - party_group_doctype = "Customer Group" if party_type == "Customer" else "Supplier Group" - group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype)) + if party: account = frappe.db.get_value( - "Party Account", - {"parenttype": party_group_doctype, "parent": group, "company": company}, - "account", + "Party Account", {"parenttype": party_type, "parent": party, "company": company}, "account" ) - if not account and party_type in ["Customer", "Supplier"]: - default_account_name = ( - "default_receivable_account" if party_type == "Customer" else "default_payable_account" - ) - account = frappe.get_cached_value("Company", company, default_account_name) + if party_type in ["Customer", "Supplier"]: + default_account = get_default_payable_receivable_account(company, party_type) + + if party: + if not account: + party_group_doctype = "Customer Group" if party_type == "Customer" else "Supplier Group" + group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype)) + account = ( + frappe.db.get_value( + "Party Account", + {"parenttype": party_group_doctype, "parent": group, "company": company}, + "account", + ) + or default_account + ) + else: + return default_account existing_gle_currency = get_party_gle_currency(party_type, party, company) if existing_gle_currency: @@ -433,16 +432,27 @@ def get_party_account(party_type, party=None, company=None, include_advance=Fals if (account and account_currency != existing_gle_currency) or not account: account = get_party_gle_account(party_type, party, company) - if include_advance and party_type in ["Customer", "Supplier", "Student"]: - advance_account = get_party_advance_account(party_type, party, company) - if advance_account: - return [account, advance_account] - else: - return [account] - return account +@frappe.whitelist() +def get_party_and_advance_accounts(party_type, party=None, company=None): + """ + Returns a list containing the party account and the advance account. + """ + party_accounts = [get_party_account(party_type, party, company)] + if party and party_type in ["Customer", "Supplier"]: + party_accounts.append(get_party_advance_account(party_type, party, company)) + return party_accounts + + +def get_default_payable_receivable_account(company, party_type): + default_account_name = ( + "default_receivable_account" if party_type == "Customer" else "default_payable_account" + ) + return frappe.get_cached_value("Company", company, default_account_name) + + def get_party_advance_account(party_type, party, company): account = frappe.db.get_value( "Party Account", From 4f7ccb72f881860a4a5dd9817bf123e531c47019 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 16:50:29 +0530 Subject: [PATCH 02/11] refactor: separate out function for party advance account --- erpnext/controllers/accounts_controller.py | 25 +++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index febad180589f..bec45bda3c20 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -37,6 +37,7 @@ from erpnext.accounts.party import ( get_party_account, get_party_account_currency, + get_party_and_advance_accounts, get_party_gle_currency, validate_party_frozen_disabled, ) @@ -1059,18 +1060,16 @@ def get_advance_entries(self, include_unallocated=True): order_field = "purchase_order" order_doctype = "Purchase Order" - party_account = get_party_account( - party_type, party=party, company=self.company, include_advance=True - ) + party_accounts = get_party_and_advance_accounts(party_type, party=party, company=self.company) order_list = list(set(d.get(order_field) for d in self.get("items") if d.get(order_field))) journal_entries = get_advance_journal_entries( - party_type, party, party_account, amount_field, order_doctype, order_list, include_unallocated + party_type, party, party_accounts, amount_field, order_doctype, order_list, include_unallocated ) payment_entries = get_advance_payment_entries_for_regional( - party_type, party, party_account, order_doctype, order_list, include_unallocated + party_type, party, party_accounts, order_doctype, order_list, include_unallocated ) res = journal_entries + payment_entries @@ -2514,7 +2513,7 @@ def set_balance_in_account_currency( def get_advance_journal_entries( party_type, party, - party_account, + party_accounts, amount_field, order_doctype, order_list, @@ -2536,7 +2535,7 @@ def get_advance_journal_entries( (journal_acc.exchange_rate), ) .where( - journal_acc.account.isin(party_account) + journal_acc.account.isin(party_accounts) & (journal_acc.party_type == party_type) & (journal_acc.party == party) & (journal_acc.is_advance == "Yes") @@ -2571,7 +2570,7 @@ def get_advance_payment_entries_for_regional(*args, **kwargs): def get_advance_payment_entries( party_type, party, - party_account, + party_accounts, order_doctype, order_list=None, include_unallocated=True, @@ -2587,7 +2586,7 @@ def get_advance_payment_entries( q = get_common_query( party_type, party, - party_account, + party_accounts, limit, condition, ) @@ -2610,7 +2609,7 @@ def get_advance_payment_entries( q = get_common_query( party_type, party, - party_account, + party_accounts, limit, condition, ) @@ -2625,7 +2624,7 @@ def get_advance_payment_entries( def get_common_query( party_type, party, - party_account, + party_accounts, limit, condition, ): @@ -2650,11 +2649,11 @@ def get_common_query( if payment_type == "Receive": q = q.select((payment_entry.paid_from_account_currency).as_("currency")) q = q.select(payment_entry.paid_from) - q = q.where(payment_entry.paid_from.isin(party_account)) + q = q.where(payment_entry.paid_from.isin(party_accounts)) else: q = q.select((payment_entry.paid_to_account_currency).as_("currency")) q = q.select(payment_entry.paid_to) - q = q.where(payment_entry.paid_to.isin(party_account)) + q = q.where(payment_entry.paid_to.isin(party_accounts)) if payment_type == "Receive": q = q.select((payment_entry.source_exchange_rate).as_("exchange_rate")) From 3477f0f68b5fbdbf7756c5449cbebba6072ef008 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 16:51:34 +0530 Subject: [PATCH 03/11] fix: utils using get_party_account --- erpnext/accounts/report/utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py index 9f96449ba7c4..6423beb3111b 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -10,7 +10,7 @@ get_dimension_with_children, ) from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date -from erpnext.accounts.party import get_party_account +from erpnext.accounts.party import get_party_and_advance_accounts from erpnext.setup.utils import get_exchange_rate __exchange_rates = {} @@ -252,7 +252,7 @@ def get_journal_entries(filters, args): .where( (je.voucher_type == "Journal Entry") & (journal_account.party == filters.get(args.party)) - & (journal_account.account.isin(args.party_account)) + & (journal_account.account.isin(args.party_accounts)) ) .orderby(je.posting_date, je.name, order=Order.desc) ) @@ -281,7 +281,7 @@ def get_payment_entries(filters, args): pe.cost_center, ) .where( - (pe.party == filters.get(args.party)) & (pe[args.account_fieldname].isin(args.party_account)) + (pe.party == filters.get(args.party)) & (pe[args.account_fieldname].isin(args.party_accounts)) ) .orderby(pe.posting_date, pe.name, order=Order.desc) ) @@ -370,7 +370,7 @@ def filter_invoices_based_on_dimensions(filters, query, parent_doc): def get_opening_row(party_type, party, from_date, company): - party_account = get_party_account(party_type, party, company, include_advance=True) + party_accounts = get_party_and_advance_accounts(party_type, party, company) gle = frappe.qb.DocType("GL Entry") return ( frappe.qb.from_(gle) @@ -381,7 +381,7 @@ def get_opening_row(party_type, party, from_date, company): (Sum(gle.debit) - Sum(gle.credit)).as_("balance"), ) .where( - (gle.account.isin(party_account)) + (gle.account.isin(party_accounts)) & (gle.party == party) & (gle.posting_date < from_date) & (gle.is_cancelled == 0) From f8daee37fa717203bd79aac94da8d5d40b58abea Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 16:52:52 +0530 Subject: [PATCH 04/11] fix: use advance account function for register reports --- .../report/purchase_register/purchase_register.py | 12 +++++------- .../accounts/report/sales_register/sales_register.py | 6 ++---- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/erpnext/accounts/report/purchase_register/purchase_register.py b/erpnext/accounts/report/purchase_register/purchase_register.py index 39eb312e4aec..bb84a83e04a8 100644 --- a/erpnext/accounts/report/purchase_register/purchase_register.py +++ b/erpnext/accounts/report/purchase_register/purchase_register.py @@ -8,7 +8,7 @@ from frappe.utils import flt, getdate from pypika import Order -from erpnext.accounts.party import get_party_account +from erpnext.accounts.party import get_party_and_advance_accounts from erpnext.accounts.report.utils import ( apply_common_conditions, get_advance_taxes_and_charges, @@ -419,10 +419,10 @@ def get_invoices(filters, additional_query_columns): ) if filters.get("include_payments"): - party_account = get_party_account( - "Supplier", filters.get("supplier"), filters.get("company"), include_advance=True + party_accounts = get_party_and_advance_accounts( + "Supplier", filters.get("supplier"), filters.get("company") ) - query = query.where(pi.credit_to.isin(party_account)) + query = query.where(pi.credit_to.isin(party_accounts)) invoices = query.run(as_dict=True) return invoices @@ -443,9 +443,7 @@ def get_payments(filters): account_fieldname="paid_to", party="supplier", party_name="supplier_name", - party_account=get_party_account( - "Supplier", filters.supplier, filters.company, include_advance=True - ), + party_accounts=get_party_and_advance_accounts("Supplier", filters.supplier, filters.company), ) payment_entries = get_payment_entries(filters, args) journal_entries = get_journal_entries(filters, args) diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py index ec6dd729082a..48ec01b2a88a 100644 --- a/erpnext/accounts/report/sales_register/sales_register.py +++ b/erpnext/accounts/report/sales_register/sales_register.py @@ -9,7 +9,7 @@ from frappe.utils import flt, getdate from pypika import Order -from erpnext.accounts.party import get_party_account +from erpnext.accounts.party import get_party_and_advance_accounts from erpnext.accounts.report.utils import ( apply_common_conditions, get_advance_taxes_and_charges, @@ -480,9 +480,7 @@ def get_payments(filters): account_fieldname="paid_from", party="customer", party_name="customer_name", - party_account=get_party_account( - "Customer", filters.customer, filters.company, include_advance=True - ), + party_accounts=get_party_and_advance_accounts("Customer", filters.customer, filters.company), ) payment_entries = get_payment_entries(filters, args) journal_entries = get_journal_entries(filters, args) From 83613be5309cd14686898507a32f6abb63e51f57 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 16:53:53 +0530 Subject: [PATCH 05/11] fix: mandatory eval to use correct check --- erpnext/setup/doctype/company/company.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json index b9ff3dddd196..6ee8ab5827c6 100644 --- a/erpnext/setup/doctype/company/company.json +++ b/erpnext/setup/doctype/company/company.json @@ -694,7 +694,7 @@ "fieldname": "default_advance_received_account", "fieldtype": "Link", "label": "Default Advance Received Account", - "mandatory_depends_on": "book_advance_payments_as_liability", + "mandatory_depends_on": "book_advance_payments_in_separate_party_account", "options": "Account" }, { @@ -702,7 +702,7 @@ "fieldname": "default_advance_paid_account", "fieldtype": "Link", "label": "Default Advance Paid Account", - "mandatory_depends_on": "book_advance_payments_as_liability", + "mandatory_depends_on": "book_advance_payments_in_separate_party_account", "options": "Account" }, { @@ -766,7 +766,7 @@ "image_field": "company_logo", "is_tree": 1, "links": [], - "modified": "2023-10-23 10:19:24.322898", + "modified": "2023-12-29 02:35:15.121778", "modified_by": "Administrator", "module": "Setup", "name": "Company", From d93fdad0795efef117abd7d0c7c85c2d685fa502 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 16:55:16 +0530 Subject: [PATCH 06/11] fix: liability account in payments --- erpnext/accounts/doctype/payment_entry/payment_entry.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 11c7c179b67f..d35fc295829a 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -29,7 +29,7 @@ make_reverse_gl_entries, process_gl_map, ) -from erpnext.accounts.party import get_party_account +from erpnext.accounts.party import get_party_account, get_party_and_advance_accounts from erpnext.accounts.utils import ( cancel_exchange_gain_loss_journal, get_account_currency, @@ -135,9 +135,7 @@ def set_liability_account(self): self.book_advance_payments_in_separate_party_account = False return - liability_account = get_party_account( - self.party_type, self.party, self.company, include_advance=True - )[1] + liability_account = get_party_and_advance_accounts(self.party_type, self.party, self.company)[1] self.set(self.party_account_field, liability_account) From 433416bb052ac4ad4a7f0c7082a3d5e793726f41 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 17:26:07 +0530 Subject: [PATCH 07/11] fix: advance account in payment recon script --- .../doctype/payment_reconciliation/payment_reconciliation.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js index fc90c3dec04f..7728c67744ae 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js +++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js @@ -141,12 +141,11 @@ erpnext.accounts.PaymentReconciliationController = class PaymentReconciliationCo if (!this.frm.doc.receivable_payable_account && this.frm.doc.party_type && this.frm.doc.party) { frappe.call({ - method: "erpnext.accounts.party.get_party_account", + method: "erpnext.accounts.party.get_party_and_advance_accounts", args: { company: this.frm.doc.company, party_type: this.frm.doc.party_type, party: this.frm.doc.party, - include_advance: 1 }, callback: (r) => { if (!r.exc && r.message) { From 565fa111370afe4195235b699d49f1c64a383cda Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 29 Dec 2023 18:00:25 +0530 Subject: [PATCH 08/11] fix: arg name for overridden function --- erpnext/controllers/accounts_controller.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index bec45bda3c20..9dea8d59ed08 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -2570,7 +2570,7 @@ def get_advance_payment_entries_for_regional(*args, **kwargs): def get_advance_payment_entries( party_type, party, - party_accounts, + party_account, order_doctype, order_list=None, include_unallocated=True, @@ -2586,7 +2586,7 @@ def get_advance_payment_entries( q = get_common_query( party_type, party, - party_accounts, + party_account, limit, condition, ) @@ -2609,7 +2609,7 @@ def get_advance_payment_entries( q = get_common_query( party_type, party, - party_accounts, + party_account, limit, condition, ) From 3c5e43d0e48c069d7c619e1b8ec41af8f019f9af Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 12 Jan 2024 13:04:42 +0530 Subject: [PATCH 09/11] chore: resolve conflicts --- erpnext/accounts/report/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py index 6423beb3111b..5edb7c6489be 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -281,7 +281,9 @@ def get_payment_entries(filters, args): pe.cost_center, ) .where( - (pe.party == filters.get(args.party)) & (pe[args.account_fieldname].isin(args.party_accounts)) + (pe.docstatus == 1) + & (pe.party == filters.get(args.party)) + & (pe[args.account_fieldname].isin(args.party_account)) ) .orderby(pe.posting_date, pe.name, order=Order.desc) ) From 0f0edc1807d1db8c99f7e775fa5a3f08f8c07819 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Mon, 29 Jan 2024 17:08:23 +0530 Subject: [PATCH 10/11] fix: test for register view --- erpnext/accounts/report/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py index ec78f8b7aa2e..254e51906444 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -284,7 +284,7 @@ def get_payment_entries(filters, args): .where( (pe.docstatus == 1) & (pe.party == filters.get(args.party)) - & (pe[args.account_fieldname].isin(args.party_account)) + & (pe[args.account_fieldname].isin(args.party_accounts)) ) .orderby(pe.posting_date, pe.name, order=Order.desc) ) From cd741008260e38062368f1e639bfd960bdabb75c Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Tue, 30 Jan 2024 13:47:41 +0530 Subject: [PATCH 11/11] fix: set liability account for all party types --- erpnext/accounts/party.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 2c3b0f207ead..1c82bea17680 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -439,8 +439,7 @@ def get_party_and_advance_accounts(party_type, party=None, company=None): Returns a list containing the party account and the advance account. """ party_accounts = [get_party_account(party_type, party, company)] - if party and party_type in ["Customer", "Supplier"]: - party_accounts.append(get_party_advance_account(party_type, party, company)) + party_accounts.append(get_party_advance_account(party_type, party, company)) return party_accounts