refactor: move repeating code to common controller

This commit is contained in:
Gursheen Anand 2023-07-10 18:39:35 +05:30
parent d7ffad1dd3
commit cbef6c30c3
3 changed files with 48 additions and 80 deletions

View File

@ -10,6 +10,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
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 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
)
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 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
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 @@ def get_columns(invoice_list, additional_table_columns, include_payments=False):
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 @@ def get_account_details(invoice_list):
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

View File

@ -11,6 +11,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
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 @@ def _execute(filters, additional_table_columns=None, additional_query_columns=No
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 @@ def _execute(filters, additional_table_columns=None, additional_query_columns=No
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 @@ def get_columns(invoice_list, additional_table_columns, include_payments=False):
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 @@ def get_columns(invoice_list, additional_table_columns, include_payments=False):
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 @@ def get_mode_of_payments(invoice_list):
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

View File

@ -151,3 +151,41 @@ def get_invoiced_item_gross_margin(
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"]))