feat: add check for fetching PE along with Invoice details in Purchase Register

This commit is contained in:
Gursheen Anand
2023-07-10 13:38:30 +05:30
parent 988d755906
commit 4f0aa54c09
2 changed files with 65 additions and 16 deletions

View File

@@ -52,6 +52,12 @@ frappe.query_reports["Purchase Register"] = {
"label": __("Item Group"), "label": __("Item Group"),
"fieldtype": "Link", "fieldtype": "Link",
"options": "Item Group" "options": "Item Group"
},
{
"fieldname": "include_payments",
"label": __("Include Payments"),
"fieldtype": "Check",
"default": 0
} }
] ]
} }

View File

@@ -21,6 +21,8 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
filters = {} filters = {}
invoice_list = get_invoices(filters, additional_query_columns) invoice_list = get_invoices(filters, additional_query_columns)
if filters.get("include_payments") and filters.include_payments:
invoice_list += get_payments(filters, additional_query_columns)
columns, expense_accounts, tax_accounts, unrealized_profit_loss_accounts = get_columns( columns, expense_accounts, tax_accounts, unrealized_profit_loss_accounts = get_columns(
invoice_list, additional_table_columns invoice_list, additional_table_columns
) )
@@ -54,11 +56,11 @@ def _execute(filters=None, additional_table_columns=None, additional_query_colum
row.append(inv.get(col)) row.append(inv.get(col))
row += [ row += [
supplier_details.get(inv.supplier), # supplier_group supplier_details.get(inv.supplier)[0], # supplier_group
inv.tax_id, supplier_details.get(inv.supplier)[1],
inv.credit_to, inv.credit_to,
inv.mode_of_payment, inv.mode_of_payment,
", ".join(project), ", ".join(project) if inv.doctype == "Purchase Invoice" else inv.project,
inv.bill_no, inv.bill_no,
inv.bill_date, inv.bill_date,
inv.remarks, inv.remarks,
@@ -141,9 +143,10 @@ def get_columns(invoice_list, additional_table_columns):
tax_accounts = frappe.db.sql_list( tax_accounts = frappe.db.sql_list(
"""select distinct account_head """select distinct account_head
from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice' from (select account_head, parent, docstatus from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice'
and docstatus = 1 and (account_head is not null and account_head != '') and category in ('Total', 'Valuation and Total') union select account_head, parent, docstatus
and category in ('Total', 'Valuation and Total') from `tabAdvance Taxes and Charges` where parenttype = 'Payment Entry' and charge_type in ('On Paid Amount', 'Actual')) a
where docstatus = 1 and (account_head is not null and account_head != '')
and parent in (%s) order by account_head""" and parent in (%s) order by account_head"""
% ", ".join(["%s"] * len(invoice_list)), % ", ".join(["%s"] * len(invoice_list)),
tuple(inv.name for inv in invoice_list), tuple(inv.name for inv in invoice_list),
@@ -185,13 +188,13 @@ def get_columns(invoice_list, additional_table_columns):
return columns, expense_accounts, tax_accounts, unrealized_profit_loss_accounts return columns, expense_accounts, tax_accounts, unrealized_profit_loss_accounts
def get_conditions(filters): def get_conditions(filters, payments=False):
conditions = "" conditions = ""
if filters.get("company"): if filters.get("company"):
conditions += " and company=%(company)s" conditions += " and company=%(company)s"
if filters.get("supplier"): if filters.get("supplier"):
conditions += " and supplier = %(supplier)s" conditions += " and party = %(supplier)s" if payments else " and supplier = %(supplier)s"
if filters.get("from_date"): if filters.get("from_date"):
conditions += " and posting_date>=%(from_date)s" conditions += " and posting_date>=%(from_date)s"
@@ -202,16 +205,19 @@ def get_conditions(filters):
conditions += " and ifnull(mode_of_payment, '') = %(mode_of_payment)s" conditions += " and ifnull(mode_of_payment, '') = %(mode_of_payment)s"
if filters.get("cost_center"): if filters.get("cost_center"):
conditions += """ and exists(select name from `tabPurchase Invoice Item` if payments:
where parent=`tabPurchase Invoice`.name conditions += " and cost_center = %(cost_center)s"
and ifnull(`tabPurchase Invoice Item`.cost_center, '') = %(cost_center)s)""" else:
conditions += """ and exists(select name from `tabPurchase Invoice Item`
where parent=`tabPurchase Invoice`.name
and ifnull(`tabPurchase Invoice Item`.cost_center, '') = %(cost_center)s)"""
if filters.get("warehouse"): if filters.get("warehouse") and not payments:
conditions += """ and exists(select name from `tabPurchase Invoice Item` conditions += """ and exists(select name from `tabPurchase Invoice Item`
where parent=`tabPurchase Invoice`.name where parent=`tabPurchase Invoice`.name
and ifnull(`tabPurchase Invoice Item`.warehouse, '') = %(warehouse)s)""" and ifnull(`tabPurchase Invoice Item`.warehouse, '') = %(warehouse)s)"""
if filters.get("item_group"): if filters.get("item_group") and not payments:
conditions += """ and exists(select name from `tabPurchase Invoice Item` conditions += """ and exists(select name from `tabPurchase Invoice Item`
where parent=`tabPurchase Invoice`.name where parent=`tabPurchase Invoice`.name
and ifnull(`tabPurchase Invoice Item`.item_group, '') = %(item_group)s)""" and ifnull(`tabPurchase Invoice Item`.item_group, '') = %(item_group)s)"""
@@ -251,7 +257,7 @@ def get_invoices(filters, additional_query_columns):
return frappe.db.sql( return frappe.db.sql(
""" """
select select
name, posting_date, credit_to, supplier, supplier_name, tax_id, bill_no, bill_date, 'Purchase Invoice' as doctype, name, posting_date, credit_to, supplier, supplier_name, tax_id, bill_no, bill_date,
remarks, base_net_total, base_grand_total, outstanding_amount, remarks, base_net_total, base_grand_total, outstanding_amount,
mode_of_payment {0} mode_of_payment {0}
from `tabPurchase Invoice` from `tabPurchase Invoice`
@@ -265,6 +271,28 @@ def get_invoices(filters, additional_query_columns):
) )
def get_payments(filters, additional_query_columns):
if additional_query_columns:
additional_query_columns = ", " + ", ".join(additional_query_columns)
conditions = get_conditions(filters, payments=True)
return frappe.db.sql(
"""
select
'Payment Entry' as doctype, name, posting_date, paid_to as credit_to, party as supplier, party_name as supplier_name,
remarks, paid_amount as base_net_total, paid_amount_after_tax as base_grand_total,
mode_of_payment {0}, project
from `tabPayment Entry`
where party_type = 'Supplier' %s
order by posting_date desc, name desc""".format(
additional_query_columns or ""
)
% conditions,
filters,
as_dict=1,
)
def get_invoice_expense_map(invoice_list): def get_invoice_expense_map(invoice_list):
expense_details = frappe.db.sql( expense_details = frappe.db.sql(
""" """
@@ -319,6 +347,21 @@ def get_invoice_tax_map(invoice_list, invoice_expense_map, expense_accounts):
as_dict=1, as_dict=1,
) )
advance_tax_details = frappe.db.sql(
"""
select parent, account_head, case add_deduct_tax when "Add" then sum(base_tax_amount)
else sum(base_tax_amount) * -1 end as tax_amount
from `tabAdvance Taxes and Charges`
where parent in (%s) and charge_type in ('On Paid Amount', 'Actual')
and base_tax_amount != 0
group by parent, account_head, add_deduct_tax
"""
% ", ".join(["%s"] * len(invoice_list)),
tuple(inv.name for inv in invoice_list),
as_dict=1,
)
tax_details += advance_tax_details
invoice_tax_map = {} invoice_tax_map = {}
for d in tax_details: for d in tax_details:
if d.account_head in expense_accounts: if d.account_head in expense_accounts:
@@ -391,12 +434,12 @@ def get_account_details(invoice_list):
def get_supplier_details(suppliers): def get_supplier_details(suppliers):
supplier_details = {} supplier_details = {}
for supp in frappe.db.sql( for supp in frappe.db.sql(
"""select name, supplier_group from `tabSupplier` """select name, supplier_group, tax_id from `tabSupplier`
where name in (%s)""" where name in (%s)"""
% ", ".join(["%s"] * len(suppliers)), % ", ".join(["%s"] * len(suppliers)),
tuple(suppliers), tuple(suppliers),
as_dict=1, as_dict=1,
): ):
supplier_details.setdefault(supp.name, supp.supplier_group) supplier_details.setdefault(supp.name, [supp.supplier_group, supp.tax_id])
return supplier_details return supplier_details