mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-29 06:38:24 +00:00
refactor(postgres): port Purchase Register report query to the query builder
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
|
||||
import frappe
|
||||
from frappe import _, msgprint
|
||||
from frappe.query_builder import Case
|
||||
from frappe.query_builder.custom import ConstantColumn
|
||||
from frappe.query_builder.functions import Sum
|
||||
from frappe.utils import flt, getdate
|
||||
from pypika.terms import Bracket, LiteralValue, Order
|
||||
|
||||
@@ -307,14 +309,17 @@ def get_account_columns(invoice_list, include_payments):
|
||||
unrealized_profit_loss_account_columns = []
|
||||
|
||||
if invoice_list:
|
||||
expense_accounts = frappe.db.sql_list(
|
||||
"""select distinct expense_account
|
||||
from `tabPurchase Invoice Item` where docstatus = 1
|
||||
and (expense_account is not null and expense_account != '')
|
||||
and parenttype='Purchase Invoice'
|
||||
and parent in (%s) order by expense_account"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple([inv.name for inv in invoice_list]),
|
||||
expense_accounts = frappe.get_all(
|
||||
"Purchase Invoice Item",
|
||||
filters={
|
||||
"docstatus": 1,
|
||||
"expense_account": ["is", "set"],
|
||||
"parenttype": "Purchase Invoice",
|
||||
"parent": ["in", [inv.name for inv in invoice_list]],
|
||||
},
|
||||
pluck="expense_account",
|
||||
distinct=True,
|
||||
order_by="expense_account",
|
||||
)
|
||||
|
||||
purchase_taxes_query = get_taxes_query(invoice_list, "Purchase Taxes and Charges", "Purchase Invoice")
|
||||
@@ -326,13 +331,16 @@ def get_account_columns(invoice_list, include_payments):
|
||||
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(
|
||||
"""SELECT distinct unrealized_profit_loss_account
|
||||
from `tabPurchase Invoice` where docstatus = 1 and name in (%s)
|
||||
and ifnull(unrealized_profit_loss_account, '') != ''
|
||||
order by unrealized_profit_loss_account"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple(inv.name for inv in invoice_list),
|
||||
unrealized_profit_loss_accounts = frappe.get_all(
|
||||
"Purchase Invoice",
|
||||
filters={
|
||||
"docstatus": 1,
|
||||
"name": ["in", [inv.name for inv in invoice_list]],
|
||||
"unrealized_profit_loss_account": ["is", "set"],
|
||||
},
|
||||
pluck="unrealized_profit_loss_account",
|
||||
distinct=True,
|
||||
order_by="unrealized_profit_loss_account",
|
||||
)
|
||||
|
||||
for account in expense_accounts:
|
||||
@@ -454,16 +462,11 @@ def get_payments(filters):
|
||||
|
||||
|
||||
def get_invoice_expense_map(invoice_list):
|
||||
expense_details = frappe.db.sql(
|
||||
"""
|
||||
select parent, expense_account, sum(base_net_amount) as amount
|
||||
from `tabPurchase Invoice Item`
|
||||
where parent in (%s) and parenttype='Purchase Invoice'
|
||||
group by parent, expense_account
|
||||
"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple(inv.name for inv in invoice_list),
|
||||
as_dict=1,
|
||||
expense_details = frappe.get_all(
|
||||
"Purchase Invoice Item",
|
||||
filters={"parent": ["in", [inv.name for inv in invoice_list]], "parenttype": "Purchase Invoice"},
|
||||
fields=["parent", "expense_account", {"SUM": "base_net_amount", "as": "amount"}],
|
||||
group_by="parent, expense_account",
|
||||
)
|
||||
|
||||
invoice_expense_map = {}
|
||||
@@ -475,13 +478,16 @@ def get_invoice_expense_map(invoice_list):
|
||||
|
||||
|
||||
def get_internal_invoice_map(invoice_list):
|
||||
unrealized_amount_details = frappe.db.sql(
|
||||
"""SELECT name, unrealized_profit_loss_account,
|
||||
base_net_total as amount from `tabPurchase Invoice` where name in (%s)
|
||||
and is_internal_supplier = 1 and company = represents_company"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple(inv.name for inv in invoice_list),
|
||||
as_dict=1,
|
||||
pi = frappe.qb.DocType("Purchase Invoice")
|
||||
unrealized_amount_details = (
|
||||
frappe.qb.from_(pi)
|
||||
.select(pi.name, pi.unrealized_profit_loss_account, pi.base_net_total.as_("amount"))
|
||||
.where(
|
||||
pi.name.isin([inv.name for inv in invoice_list])
|
||||
& (pi.is_internal_supplier == 1)
|
||||
& (pi.company == pi.represents_company)
|
||||
)
|
||||
.run(as_dict=1)
|
||||
)
|
||||
|
||||
internal_invoice_map = {}
|
||||
@@ -493,18 +499,23 @@ def get_internal_invoice_map(invoice_list):
|
||||
|
||||
|
||||
def get_invoice_tax_map(invoice_list, invoice_expense_map, expense_accounts, include_payments=False):
|
||||
tax_details = frappe.db.sql(
|
||||
"""
|
||||
select parent, account_head, case add_deduct_tax when "Add" then sum(base_tax_amount_after_discount_amount)
|
||||
else sum(base_tax_amount_after_discount_amount) * -1 end as tax_amount
|
||||
from `tabPurchase Taxes and Charges`
|
||||
where parent in (%s) and category in ('Total', 'Valuation and Total')
|
||||
and base_tax_amount_after_discount_amount != 0 and parenttype='Purchase Invoice'
|
||||
group by parent, account_head, add_deduct_tax
|
||||
"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple(inv.name for inv in invoice_list),
|
||||
as_dict=1,
|
||||
ptc = frappe.qb.DocType("Purchase Taxes and Charges")
|
||||
tax_amount = (
|
||||
Case()
|
||||
.when(ptc.add_deduct_tax == "Add", Sum(ptc.base_tax_amount_after_discount_amount))
|
||||
.else_(Sum(ptc.base_tax_amount_after_discount_amount) * -1)
|
||||
)
|
||||
tax_details = (
|
||||
frappe.qb.from_(ptc)
|
||||
.select(ptc.parent, ptc.account_head, tax_amount.as_("tax_amount"))
|
||||
.where(
|
||||
ptc.parent.isin([inv.name for inv in invoice_list])
|
||||
& ptc.category.isin(["Total", "Valuation and Total"])
|
||||
& (ptc.base_tax_amount_after_discount_amount != 0)
|
||||
& (ptc.parenttype == "Purchase Invoice")
|
||||
)
|
||||
.groupby(ptc.parent, ptc.account_head, ptc.add_deduct_tax)
|
||||
.run(as_dict=1)
|
||||
)
|
||||
|
||||
if include_payments:
|
||||
@@ -525,15 +536,10 @@ def get_invoice_tax_map(invoice_list, invoice_expense_map, expense_accounts, inc
|
||||
|
||||
|
||||
def get_invoice_po_pr_map(invoice_list):
|
||||
pi_items = frappe.db.sql(
|
||||
"""
|
||||
select parent, purchase_order, purchase_receipt, po_detail, project
|
||||
from `tabPurchase Invoice Item`
|
||||
where parent in (%s) and parenttype='Purchase Invoice'
|
||||
"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple(inv.name for inv in invoice_list),
|
||||
as_dict=1,
|
||||
pi_items = frappe.get_all(
|
||||
"Purchase Invoice Item",
|
||||
filters={"parent": ["in", [inv.name for inv in invoice_list]], "parenttype": "Purchase Invoice"},
|
||||
fields=["parent", "purchase_order", "purchase_receipt", "po_detail", "project"],
|
||||
)
|
||||
|
||||
invoice_po_pr_map = {}
|
||||
@@ -547,10 +553,11 @@ def get_invoice_po_pr_map(invoice_list):
|
||||
if d.purchase_receipt:
|
||||
pr_list = [d.purchase_receipt]
|
||||
elif d.po_detail:
|
||||
pr_list = frappe.db.sql_list(
|
||||
"""select distinct parent from `tabPurchase Receipt Item`
|
||||
where docstatus=1 and purchase_order_item=%s""",
|
||||
d.po_detail,
|
||||
pr_list = frappe.get_all(
|
||||
"Purchase Receipt Item",
|
||||
filters={"docstatus": 1, "purchase_order_item": d.po_detail},
|
||||
pluck="parent",
|
||||
distinct=True,
|
||||
)
|
||||
|
||||
if pr_list:
|
||||
@@ -565,12 +572,8 @@ def get_invoice_po_pr_map(invoice_list):
|
||||
def get_account_details(invoice_list):
|
||||
account_map = {}
|
||||
accounts = list(set([inv.credit_to for inv in invoice_list]))
|
||||
for acc in frappe.db.sql(
|
||||
"""select name, parent_account from tabAccount
|
||||
where name in (%s)"""
|
||||
% ", ".join(["%s"] * len(accounts)),
|
||||
tuple(accounts),
|
||||
as_dict=1,
|
||||
for acc in frappe.get_all(
|
||||
"Account", filters={"name": ["in", accounts]}, fields=["name", "parent_account"]
|
||||
):
|
||||
account_map[acc.name] = acc.parent_account
|
||||
|
||||
|
||||
Reference in New Issue
Block a user