refactor: move repeating code to common controller
diff --git a/erpnext/accounts/report/purchase_register/purchase_register.py b/erpnext/accounts/report/purchase_register/purchase_register.py
index 72d7209..c8f1906 100644
--- a/erpnext/accounts/report/purchase_register/purchase_register.py
+++ b/erpnext/accounts/report/purchase_register/purchase_register.py
@@ -10,6 +10,7 @@
get_accounting_dimensions,
get_dimension_with_children,
)
+from erpnext.accounts.report.utils import get_party_details, get_taxes_query
def execute(filters=None):
@@ -39,7 +40,7 @@
)
invoice_po_pr_map = get_invoice_po_pr_map(invoice_list)
suppliers = list(set(d.supplier for d in invoice_list))
- supplier_details = get_supplier_details(suppliers)
+ supplier_details = get_party_details("Supplier", suppliers)
company_currency = frappe.get_cached_value("Company", filters.company, "default_currency")
@@ -57,8 +58,8 @@
row.append(inv.get(col))
row += [
- supplier_details.get(inv.supplier)[0], # supplier_group
- supplier_details.get(inv.supplier)[1],
+ supplier_details.get(inv.supplier).get("supplier_group"), # supplier_group
+ supplier_details.get(inv.supplier).get("tax_id"),
inv.credit_to,
inv.mode_of_payment,
", ".join(project) if inv.doctype == "Purchase Invoice" else inv.project,
@@ -191,27 +192,6 @@
return columns, expense_accounts, tax_accounts, unrealized_profit_loss_accounts
-def get_taxes_query(invoice_list, doctype, parenttype):
- taxes = frappe.qb.DocType(doctype)
-
- query = (
- frappe.qb.from_(taxes)
- .select(taxes.account_head)
- .distinct()
- .where(
- (taxes.parenttype == parenttype)
- & (taxes.docstatus == 1)
- & (taxes.account_head.isnotnull())
- & (taxes.parent.isin([inv.name for inv in invoice_list]))
- )
- .orderby(taxes.account_head)
- )
-
- if doctype == "Purchase Taxes and Charges":
- return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
- return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
-
-
def get_conditions(filters, payments=False):
conditions = ""
@@ -455,17 +435,3 @@
account_map[acc.name] = acc.parent_account
return account_map
-
-
-def get_supplier_details(suppliers):
- supplier_details = {}
- for supp in frappe.db.sql(
- """select name, supplier_group, tax_id from `tabSupplier`
- where name in (%s)"""
- % ", ".join(["%s"] * len(suppliers)),
- tuple(suppliers),
- as_dict=1,
- ):
- supplier_details.setdefault(supp.name, [supp.supplier_group, supp.tax_id])
-
- return supplier_details
diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py
index a2bba55..1ecfbfe 100644
--- a/erpnext/accounts/report/sales_register/sales_register.py
+++ b/erpnext/accounts/report/sales_register/sales_register.py
@@ -11,6 +11,7 @@
get_accounting_dimensions,
get_dimension_with_children,
)
+from erpnext.accounts.report.utils import get_party_details, get_taxes_query
def execute(filters=None):
@@ -44,7 +45,7 @@
company_currency = frappe.get_cached_value("Company", filters.get("company"), "default_currency")
mode_of_payments = get_mode_of_payments([inv.name for inv in invoice_list])
customers = list(set(d.customer for d in invoice_list))
- customer_details = get_customer_details(customers)
+ customer_details = get_party_details("Customer", customers)
data = []
for inv in invoice_list:
@@ -67,9 +68,9 @@
row.update(
{
- "customer_group": customer_details.get(inv.customer)[0],
- "territory": customer_details.get(inv.customer)[1],
- "tax_id": customer_details.get(inv.customer)[2],
+ "customer_group": customer_details.get(inv.customer).get("customer_group"),
+ "territory": customer_details.get(inv.customer).get("territory"),
+ "tax_id": customer_details.get(inv.customer).get("tax_id"),
"receivable_account": inv.debit_to,
"mode_of_payment": ", ".join(mode_of_payments.get(inv.name, [])),
"project": inv.project,
@@ -250,7 +251,7 @@
advance_taxes_query = get_taxes_query(
invoice_list, "Advance Taxes and Charges", "Payment Entry"
)
- advance_tax_accounts = advance_taxes_query.run(as_dict=True, debug=True, pluck="account_head")
+ advance_tax_accounts = advance_taxes_query.run(as_dict=True, pluck="account_head")
tax_accounts = set(tax_accounts + advance_tax_accounts)
unrealized_profit_loss_accounts = frappe.db.sql_list(
@@ -350,27 +351,6 @@
return columns, income_accounts, tax_accounts, unrealized_profit_loss_accounts
-def get_taxes_query(invoice_list, doctype, parenttype):
- taxes = frappe.qb.DocType(doctype)
-
- query = (
- frappe.qb.from_(taxes)
- .select(taxes.account_head)
- .distinct()
- .where(
- (taxes.parenttype == parenttype)
- & (taxes.docstatus == 1)
- & (taxes.account_head.isnotnull())
- & (taxes.parent.isin([inv.name for inv in invoice_list]))
- )
- .orderby(taxes.account_head)
- )
-
- if doctype == "Sales Taxes and Charges":
- return query.where(taxes.charge_type.isin(["Total", "Valuation and Total"]))
- return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
-
-
def get_conditions(filters, payments=False):
conditions = ""
@@ -622,19 +602,3 @@
mode_of_payments.setdefault(d.parent, []).append(d.mode_of_payment)
return mode_of_payments
-
-
-def get_customer_details(customers):
- customer_details = {}
- for customer in frappe.db.sql(
- """select name, customer_group, territory, tax_id from `tabCustomer`
- where name in (%s)"""
- % ", ".join(["%s"] * len(customers)),
- tuple(customers),
- as_dict=1,
- ):
- customer_details.setdefault(
- customer.name, [customer.customer_group, customer.territory, customer.tax_id]
- )
-
- return customer_details
diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py
index 97cc1c4..e049301 100644
--- a/erpnext/accounts/report/utils.py
+++ b/erpnext/accounts/report/utils.py
@@ -151,3 +151,41 @@
result = sum(d.gross_profit for d in result)
return result
+
+
+def get_party_details(party_type, party_list):
+ party_details = {}
+ party = frappe.qb.DocType(party_type)
+ query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
+ if party_type == "Supplier":
+ query = query.select(party.supplier_group)
+ else:
+ query = query.select(party.customer_group, party.territory)
+
+ party_detail_list = query.run(as_dict=True)
+ for party_dict in party_detail_list:
+ party_details[party_dict.name] = party_dict
+ return party_details
+
+
+def get_taxes_query(invoice_list, doctype, parenttype):
+ taxes = frappe.qb.DocType(doctype)
+
+ query = (
+ frappe.qb.from_(taxes)
+ .select(taxes.account_head)
+ .distinct()
+ .where(
+ (taxes.parenttype == parenttype)
+ & (taxes.docstatus == 1)
+ & (taxes.account_head.isnotnull())
+ & (taxes.parent.isin([inv.name for inv in invoice_list]))
+ )
+ .orderby(taxes.account_head)
+ )
+
+ if doctype == "Purchase Taxes and Charges":
+ return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
+ elif doctype == "Sales Taxes and Charges":
+ return query.where(taxes.charge_type.isin(["Total", "Valuation and Total"]))
+ return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))