diff --git a/erpnext/accounts/report/purchase_register/purchase_register.py b/erpnext/accounts/report/purchase_register/purchase_register.py index 93dadc67fa..8e733cc58a 100644 --- a/erpnext/accounts/report/purchase_register/purchase_register.py +++ b/erpnext/accounts/report/purchase_register/purchase_register.py @@ -12,6 +12,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( get_accounting_dimensions, ) from erpnext.accounts.report.utils import ( + filter_invoices_based_on_dimensions, get_advance_taxes_and_charges, get_conditions, get_journal_entries, @@ -38,6 +39,10 @@ def _execute(filters=None, additional_table_columns=None): frappe.throw(_("Please select a supplier for fetching payments.")) invoice_list += get_payments(filters, additional_table_columns) + accounting_dimensions = get_accounting_dimensions(as_list=False) + if len(invoice_list) > 0 and accounting_dimensions: + invoice_list = filter_invoices_based_on_dimensions(filters, accounting_dimensions, invoice_list) + columns, expense_accounts, tax_accounts, unrealized_profit_loss_accounts = get_columns( invoice_list, additional_table_columns, include_payments ) @@ -207,7 +212,6 @@ def get_columns(invoice_list, additional_table_columns, include_payments=False): def get_invoices(filters, additional_query_columns): - accounting_dimensions = get_accounting_dimensions(as_list=False) pi = frappe.qb.DocType("Purchase Invoice") invoice_item = frappe.qb.DocType("Purchase Invoice Item") query = ( @@ -235,7 +239,7 @@ def get_invoices(filters, additional_query_columns): ) if filters.get("supplier"): query = query.where(pi.supplier == filters.supplier) - query = get_conditions(filters, query, [pi, invoice_item], accounting_dimensions) + query = get_conditions(filters, query, [pi, invoice_item]) invoices = query.run(as_dict=True) return invoices @@ -250,9 +254,8 @@ def get_payments(filters, additional_query_columns): party_name="supplier_name", additional_query_columns="" if not additional_query_columns else additional_query_columns, ) - accounting_dimensions = get_accounting_dimensions(as_list=False) - payment_entries = get_payment_entries(filters, accounting_dimensions, args) - journal_entries = get_journal_entries(filters, accounting_dimensions, args) + payment_entries = get_payment_entries(filters, args) + journal_entries = get_journal_entries(filters, args) return payment_entries + journal_entries diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py index 1daf524704..42fc5b34a7 100644 --- a/erpnext/accounts/report/sales_register/sales_register.py +++ b/erpnext/accounts/report/sales_register/sales_register.py @@ -13,6 +13,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( get_accounting_dimensions, ) from erpnext.accounts.report.utils import ( + filter_invoices_based_on_dimensions, get_advance_taxes_and_charges, get_conditions, get_journal_entries, @@ -39,6 +40,10 @@ def _execute(filters, additional_table_columns=None): frappe.throw(_("Please select a customer for fetching payments.")) invoice_list += get_payments(filters, additional_table_columns) + accounting_dimensions = get_accounting_dimensions(as_list=False) + if len(invoice_list) > 0 and accounting_dimensions: + invoice_list = filter_invoices_based_on_dimensions(filters, accounting_dimensions, invoice_list) + columns, income_accounts, tax_accounts, unrealized_profit_loss_accounts = get_columns( invoice_list, additional_table_columns, include_payments ) @@ -365,7 +370,6 @@ def get_columns(invoice_list, additional_table_columns, include_payments=False): def get_invoices(filters, additional_query_columns): - accounting_dimensions = get_accounting_dimensions(as_list=False) si = frappe.qb.DocType("Sales Invoice") invoice_item = frappe.qb.DocType("Sales Invoice Item") invoice_payment = frappe.qb.DocType("Sales Invoice Payment") @@ -401,7 +405,7 @@ def get_invoices(filters, additional_query_columns): ) if filters.get("customer"): query = query.where(si.customer == filters.customer) - query = get_conditions(filters, query, [si, invoice_item, invoice_payment], accounting_dimensions) + query = get_conditions(filters, query, [si, invoice_item, invoice_payment]) invoices = query.run(as_dict=True) return invoices @@ -416,9 +420,8 @@ def get_payments(filters, additional_query_columns): party_name="customer_name", additional_query_columns="" if not additional_query_columns else additional_query_columns, ) - accounting_dimensions = get_accounting_dimensions(as_list=False) - payment_entries = get_payment_entries(filters, accounting_dimensions, args) - journal_entries = get_journal_entries(filters, accounting_dimensions, args) + payment_entries = get_payment_entries(filters, args) + journal_entries = get_journal_entries(filters, args) return payment_entries + journal_entries diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py index 8b58a46cdf..32ca9fa7ed 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -225,7 +225,7 @@ def get_taxes_query(invoice_list, doctype, parenttype): return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"])) -def get_journal_entries(filters, accounting_dimensions, args): +def get_journal_entries(filters, args): je = frappe.qb.DocType("Journal Entry") journal_account = frappe.qb.DocType("Journal Entry Account") query = ( @@ -250,12 +250,12 @@ def get_journal_entries(filters, accounting_dimensions, args): .where((je.voucher_type == "Journal Entry") & (journal_account.party == filters.get(args.party))) .orderby(je.posting_date, je.name, order=Order.desc) ) - query = get_conditions(filters, query, [je], accounting_dimensions, payments=True) + query = get_conditions(filters, query, [je], payments=True) journal_entries = query.run(as_dict=True) return journal_entries -def get_payment_entries(filters, accounting_dimensions, args): +def get_payment_entries(filters, args): pe = frappe.qb.DocType("Payment Entry") query = ( frappe.qb.from_(pe) @@ -276,12 +276,12 @@ def get_payment_entries(filters, accounting_dimensions, args): .where((pe.party == filters.get(args.party))) .orderby(pe.posting_date, pe.name, order=Order.desc) ) - query = get_conditions(filters, query, [pe], accounting_dimensions, payments=True) + query = get_conditions(filters, query, [pe], payments=True) payment_entries = query.run(as_dict=True) return payment_entries -def get_conditions(filters, query, docs, accounting_dimensions, payments=False): +def get_conditions(filters, query, docs, payments=False): parent_doc = docs[0] if not payments: child_doc = docs[1] @@ -316,16 +316,6 @@ def get_conditions(filters, query, docs, accounting_dimensions, payments=False): query = query.where(child_doc.warehouse == filters.warehouse) if filters.get("item_group"): query = query.where(child_doc.item_group == filters.item_group) - - if accounting_dimensions: - for dimension in accounting_dimensions: - if filters.get(dimension.fieldname): - if frappe.get_cached_value("DocType", dimension.document_type, "is_tree"): - filters[dimension.fieldname] = get_dimension_with_children( - dimension.document_type, filters.get(dimension.fieldname) - ) - fieldname = dimension.fieldname - query = query.where(parent_doc.fieldname.isin(filters.fieldname)) return query @@ -348,4 +338,21 @@ def get_advance_taxes_and_charges(invoice_list): & (adv_taxes.base_tax_amount != 0) ) .groupby(adv_taxes.parent, adv_taxes.account_head, adv_taxes.add_deduct_tax) - ).run(as_dict=True, debug=True) + ).run(as_dict=True) + + +def filter_invoices_based_on_dimensions(filters, accounting_dimensions, invoices): + invoices_with_acc_dimensions = [] + for inv in invoices: + for dimension in accounting_dimensions: + if filters.get(dimension.fieldname): + if frappe.get_cached_value("DocType", dimension.document_type, "is_tree"): + filters[dimension.fieldname] = get_dimension_with_children( + dimension.document_type, filters.get(dimension.fieldname) + ) + fieldname = dimension.fieldname + if inv.fieldname != filters[fieldname]: + break + else: + invoices_with_acc_dimensions.append(inv) + return invoices_with_acc_dimensions