mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-16 16:15:02 +00:00
chore: update condition queries in qb
This commit is contained in:
@@ -5,14 +5,15 @@
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.utils import flt
|
||||
from pypika import Order
|
||||
|
||||
import erpnext
|
||||
from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register import (
|
||||
add_sub_total_row,
|
||||
add_total_row,
|
||||
apply_group_by_conditions,
|
||||
get_grand_total,
|
||||
get_group_by_and_display_fields,
|
||||
get_group_by_conditions,
|
||||
get_tax_accounts,
|
||||
)
|
||||
from erpnext.accounts.report.utils import get_query_columns, get_values_for_columns
|
||||
@@ -287,29 +288,27 @@ def get_columns(additional_table_columns, filters):
|
||||
return columns
|
||||
|
||||
|
||||
def get_conditions(filters):
|
||||
conditions = ""
|
||||
def apply_conditions(query, pi, pii, filters):
|
||||
for opts in ("company", "supplier", "item_code", "mode_of_payment"):
|
||||
if filters.get(opts):
|
||||
query = query.where(pi[opts] == filters[opts])
|
||||
|
||||
for opts in (
|
||||
("company", " and `tabPurchase Invoice`.company=%(company)s"),
|
||||
("supplier", " and `tabPurchase Invoice`.supplier = %(supplier)s"),
|
||||
("item_code", " and `tabPurchase Invoice Item`.item_code = %(item_code)s"),
|
||||
("from_date", " and `tabPurchase Invoice`.posting_date>=%(from_date)s"),
|
||||
("to_date", " and `tabPurchase Invoice`.posting_date<=%(to_date)s"),
|
||||
("mode_of_payment", " and ifnull(mode_of_payment, '') = %(mode_of_payment)s"),
|
||||
("item_group", " and ifnull(`tabPurchase Invoice Item`.item_group, '') = %(item_group)s"),
|
||||
):
|
||||
if filters.get(opts[0]):
|
||||
conditions += opts[1]
|
||||
if filters.get("from_date"):
|
||||
query = query.where(pi.posting_date >= filters.get("from_date"))
|
||||
|
||||
if filters.get("to_date"):
|
||||
query = query.where(pi.posting_date <= filters.get("to_date"))
|
||||
|
||||
if filters.get("item_group"):
|
||||
query = query.where(pii.item_group == filters.get("item_group"))
|
||||
|
||||
if not filters.get("group_by"):
|
||||
conditions += (
|
||||
"ORDER BY `tabPurchase Invoice`.posting_date desc, `tabPurchase Invoice Item`.item_code desc"
|
||||
)
|
||||
query = query.orderby(pi.posting_date, order=Order.desc)
|
||||
query = query.orderby(pii.item_group, order=Order.desc)
|
||||
else:
|
||||
conditions += get_group_by_conditions(filters, "Purchase Invoice")
|
||||
query = apply_group_by_conditions(filters, "Purchase Invoice")
|
||||
|
||||
return conditions
|
||||
return query
|
||||
|
||||
|
||||
def get_items(filters, additional_query_columns):
|
||||
@@ -361,6 +360,8 @@ def get_items(filters, additional_query_columns):
|
||||
if filters.get("company"):
|
||||
query = query.where(pi.company == filters["company"])
|
||||
|
||||
query = apply_conditions(query, pi, pii, filters)
|
||||
|
||||
return query.run(as_dict=True)
|
||||
|
||||
|
||||
|
||||
@@ -41,6 +41,12 @@ frappe.query_reports["Item-wise Sales Register"] = {
|
||||
label: __("Warehouse"),
|
||||
fieldtype: "Link",
|
||||
options: "Warehouse",
|
||||
get_query: function () {
|
||||
const company = frappe.query_report.get_filter_value("company");
|
||||
return {
|
||||
filters: { company: company },
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldname: "brand",
|
||||
|
||||
@@ -7,6 +7,7 @@ from frappe import _
|
||||
from frappe.model.meta import get_field_precision
|
||||
from frappe.utils import cstr, flt
|
||||
from frappe.utils.xlsxutils import handle_html
|
||||
from pypika import Order
|
||||
|
||||
from erpnext.accounts.report.sales_register.sales_register import get_mode_of_payments
|
||||
from erpnext.accounts.report.utils import get_query_columns, get_values_for_columns
|
||||
@@ -340,80 +341,79 @@ def get_columns(additional_table_columns, filters):
|
||||
return columns
|
||||
|
||||
|
||||
def get_conditions(filters, additional_conditions=None):
|
||||
conditions = ""
|
||||
def apply_conditions(query, si, sii, filters, additional_conditions=None):
|
||||
for opts in ("company", "customer", "item_code"):
|
||||
if filters.get(opts):
|
||||
query = query.where(si[opts] == filters[opts])
|
||||
|
||||
for opts in (
|
||||
("company", " and `tabSales Invoice`.company=%(company)s"),
|
||||
("customer", " and `tabSales Invoice`.customer = %(customer)s"),
|
||||
("item_code", " and `tabSales Invoice Item`.item_code = %(item_code)s"),
|
||||
("from_date", " and `tabSales Invoice`.posting_date>=%(from_date)s"),
|
||||
("to_date", " and `tabSales Invoice`.posting_date<=%(to_date)s"),
|
||||
):
|
||||
if filters.get(opts[0]):
|
||||
conditions += opts[1]
|
||||
if filters.get("from_date"):
|
||||
query = query.where(si.posting_date >= filters.get("from_date"))
|
||||
|
||||
if additional_conditions:
|
||||
conditions += additional_conditions
|
||||
if filters.get("to_date"):
|
||||
query = query.where(si.posting_date <= filters.get("to_date"))
|
||||
|
||||
if filters.get("mode_of_payment"):
|
||||
conditions += """ and exists(select name from `tabSales Invoice Payment`
|
||||
where parent=`tabSales Invoice`.name
|
||||
and ifnull(`tabSales Invoice Payment`.mode_of_payment, '') = %(mode_of_payment)s)"""
|
||||
sales_invoice = frappe.db.get_all(
|
||||
"Sales Invoice Payment", {"mode_of_payment": filters.get("mode_of_payment")}, pluck="parent"
|
||||
)
|
||||
query = query.where(si.name.isin(sales_invoice))
|
||||
|
||||
if filters.get("warehouse"):
|
||||
if frappe.db.get_value("Warehouse", filters.get("warehouse"), "is_group"):
|
||||
lft, rgt = frappe.db.get_all(
|
||||
"Warehouse", filters={"name": filters.get("warehouse")}, fields=["lft", "rgt"], as_list=True
|
||||
)[0]
|
||||
conditions += f"and ifnull(`tabSales Invoice Item`.warehouse, '') in (select name from `tabWarehouse` where lft > {lft} and rgt < {rgt}) "
|
||||
warehouses = frappe.db.get_all("Warehouse", {"lft": (">", lft), "rgt": ("<", rgt)}, pluck="name")
|
||||
query = query.where(sii.warehouse.isin(warehouses))
|
||||
else:
|
||||
conditions += """and ifnull(`tabSales Invoice Item`.warehouse, '') = %(warehouse)s"""
|
||||
query = query.where(sii.warehouse == filters.get("warehouse"))
|
||||
|
||||
if filters.get("brand"):
|
||||
conditions += """and ifnull(`tabSales Invoice Item`.brand, '') = %(brand)s"""
|
||||
query = query.where(sii.brand == filters.get("brand"))
|
||||
|
||||
if filters.get("item_group"):
|
||||
conditions += """and ifnull(`tabSales Invoice Item`.item_group, '') = %(item_group)s"""
|
||||
query = query.where(sii.item_group == filters.get("item_group"))
|
||||
|
||||
if filters.get("income_account"):
|
||||
conditions += """
|
||||
and (ifnull(`tabSales Invoice Item`.income_account, '') = %(income_account)s
|
||||
or ifnull(`tabSales Invoice Item`.deferred_revenue_account, '') = %(income_account)s
|
||||
or ifnull(`tabSales Invoice`.unrealized_profit_loss_account, '') = %(income_account)s)
|
||||
"""
|
||||
query = query.where(
|
||||
(sii.income_account == filters.get("income_account"))
|
||||
| (sii.deferred_revenue_account == filters.get("income_account"))
|
||||
| (si.unrealized_profit_loss_account == filters.get("income_account"))
|
||||
)
|
||||
|
||||
if not filters.get("group_by"):
|
||||
conditions += "ORDER BY `tabSales Invoice`.posting_date desc, `tabSales Invoice Item`.item_group desc"
|
||||
query = query.orderby(si.posting_date, order=Order.desc)
|
||||
query = query.orderby(sii.item_group, order=Order.desc)
|
||||
else:
|
||||
conditions += get_group_by_conditions(filters, "Sales Invoice")
|
||||
query = apply_group_by_conditions(query, si, sii, filters)
|
||||
|
||||
return conditions
|
||||
return query
|
||||
|
||||
|
||||
def get_group_by_conditions(filters, doctype):
|
||||
def apply_group_by_conditions(query, si, ii, filters):
|
||||
if filters.get("group_by") == "Invoice":
|
||||
return f"ORDER BY `tab{doctype} Item`.parent desc"
|
||||
query = query.orderby(ii.parent, order=Order.desc)
|
||||
elif filters.get("group_by") == "Item":
|
||||
return f"ORDER BY `tab{doctype} Item`.`item_code`"
|
||||
query = query.orderby(ii.item_code)
|
||||
elif filters.get("group_by") == "Item Group":
|
||||
return "ORDER BY `tab{} Item`.{}".format(doctype, frappe.scrub(filters.get("group_by")))
|
||||
query = query.orderby(ii.item_group)
|
||||
elif filters.get("group_by") in ("Customer", "Customer Group", "Territory", "Supplier"):
|
||||
return "ORDER BY `tab{}`.{}".format(doctype, frappe.scrub(filters.get("group_by")))
|
||||
query = query.orderby(si[frappe.scrub(filters.get("group_by"))])
|
||||
|
||||
return query
|
||||
|
||||
|
||||
def get_items(filters, additional_query_columns, additional_conditions=None):
|
||||
si = frappe.qb.DocType("Sales Invoice")
|
||||
sii = frappe.qb.DocType("Sales Invoice Item")
|
||||
Item = frappe.qb.DocType("Item")
|
||||
item = frappe.qb.DocType("Item")
|
||||
|
||||
query = (
|
||||
frappe.qb.from_(si)
|
||||
.join(sii)
|
||||
.on(si.name == sii.parent)
|
||||
# added left join
|
||||
.left_join(Item)
|
||||
.on(sii.item_code == Item.name)
|
||||
.left_join(item)
|
||||
.on(sii.item_code == item.name)
|
||||
.select(
|
||||
sii.name,
|
||||
sii.parent,
|
||||
@@ -433,8 +433,8 @@ def get_items(filters, additional_query_columns, additional_conditions=None):
|
||||
sii.item_group,
|
||||
sii.item_name.as_("si_item_name"),
|
||||
sii.item_group.as_("si_item_group"),
|
||||
Item.item_name.as_("i_item_name"),
|
||||
Item.item_group.as_("i_item_group"),
|
||||
item.item_name.as_("i_item_name"),
|
||||
item.item_group.as_("i_item_group"),
|
||||
sii.sales_order,
|
||||
sii.delivery_note,
|
||||
sii.income_account,
|
||||
@@ -454,12 +454,18 @@ def get_items(filters, additional_query_columns, additional_conditions=None):
|
||||
)
|
||||
.where(si.docstatus == 1)
|
||||
)
|
||||
|
||||
if additional_query_columns:
|
||||
query = query.select(*additional_query_columns)
|
||||
|
||||
if filters.get("customer"):
|
||||
query = query.where(si.customer == filters["customer"])
|
||||
|
||||
if filters.get("customer_group"):
|
||||
query = query.where(si.customer_group == filters["customer_group"])
|
||||
|
||||
query = apply_conditions(query, si, sii, filters, additional_conditions)
|
||||
|
||||
return query.run(as_dict=True)
|
||||
|
||||
|
||||
@@ -468,16 +474,14 @@ def get_delivery_notes_against_sales_order(item_list):
|
||||
so_item_rows = list(set([d.so_detail for d in item_list]))
|
||||
|
||||
if so_item_rows:
|
||||
delivery_notes = frappe.db.sql(
|
||||
"""
|
||||
select parent, so_detail
|
||||
from `tabDelivery Note Item`
|
||||
where docstatus=1 and so_detail in (%s)
|
||||
group by so_detail, parent
|
||||
"""
|
||||
% (", ".join(["%s"] * len(so_item_rows))),
|
||||
tuple(so_item_rows),
|
||||
as_dict=1,
|
||||
dn_item = frappe.qb.DocType("Delivery Note Item")
|
||||
delivery_notes = (
|
||||
frappe.qb.from_(dn_item)
|
||||
.select(dn_item.parent, dn_item.so_detail)
|
||||
.where(dn_item.docstatus == 1)
|
||||
.where(dn_item.so_detail.isin(so_item_rows))
|
||||
.groupby(dn_item.so_detail, dn_item.parent)
|
||||
.run(as_dict=True)
|
||||
)
|
||||
|
||||
for dn in delivery_notes:
|
||||
@@ -487,15 +491,16 @@ def get_delivery_notes_against_sales_order(item_list):
|
||||
|
||||
|
||||
def get_grand_total(filters, doctype):
|
||||
return frappe.db.sql(
|
||||
f""" SELECT
|
||||
SUM(`tab{doctype}`.base_grand_total)
|
||||
FROM `tab{doctype}`
|
||||
WHERE `tab{doctype}`.docstatus = 1
|
||||
and posting_date between %s and %s
|
||||
""",
|
||||
(filters.get("from_date"), filters.get("to_date")),
|
||||
)[0][0] # nosec
|
||||
return flt(
|
||||
frappe.db.get_value(
|
||||
doctype,
|
||||
{
|
||||
"docstatus": 1,
|
||||
"posting_date": ("between", [filters.get("from_date"), filters.get("to_date")]),
|
||||
},
|
||||
"sum(base_grand_total)",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_tax_accounts(
|
||||
|
||||
@@ -377,7 +377,7 @@ def get_account_columns(invoice_list, include_payments):
|
||||
def get_invoices(filters, additional_query_columns):
|
||||
pi = frappe.qb.DocType("Purchase Invoice")
|
||||
query = (
|
||||
frappe.qb.from_(pi) # qb
|
||||
frappe.qb.from_(pi)
|
||||
.select(
|
||||
ConstantColumn("Purchase Invoice").as_("doctype"),
|
||||
pi.name,
|
||||
|
||||
@@ -415,7 +415,7 @@ def get_account_columns(invoice_list, include_payments):
|
||||
def get_invoices(filters, additional_query_columns):
|
||||
si = frappe.qb.DocType("Sales Invoice")
|
||||
query = (
|
||||
frappe.qb.from_(si) # qb
|
||||
frappe.qb.from_(si)
|
||||
.select(
|
||||
ConstantColumn("Sales Invoice").as_("doctype"),
|
||||
si.name,
|
||||
|
||||
Reference in New Issue
Block a user