Skip to content

Commit

Permalink
fix(Sales Register): incorrect query with dimensions (backport #31242) (
Browse files Browse the repository at this point in the history
#31251)

fix(Sales Register): incorrect query with dimensions

If accounting dimension is also part of the default filters then same
query is repeated with incorrect syntax.

e.g. `item_group = (child1, child2)` instead of `in` query.

fix: don't add default filter if they are part of dimensions to be
added.

(cherry picked from commit c3219eb)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush authored Jun 6, 2022
1 parent b58c775 commit 0badfc8
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions erpnext/accounts/report/sales_register/sales_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,13 @@ def get_columns(invoice_list, additional_table_columns):
def get_conditions(filters):
conditions = ""

accounting_dimensions = get_accounting_dimensions(as_list=False) or []
accounting_dimensions_list = [d.fieldname for d in accounting_dimensions]

if filters.get("company"):
conditions += " and company=%(company)s"
if filters.get("customer"):

if filters.get("customer") and "customer" not in accounting_dimensions_list:
conditions += " and customer = %(customer)s"

if filters.get("from_date"):
Expand All @@ -359,32 +363,18 @@ def get_conditions(filters):
if filters.get("owner"):
conditions += " and owner = %(owner)s"

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)"""

if filters.get("cost_center"):
conditions += """ and exists(select name from `tabSales Invoice Item`
where parent=`tabSales Invoice`.name
and ifnull(`tabSales Invoice Item`.cost_center, '') = %(cost_center)s)"""

if filters.get("warehouse"):
conditions += """ and exists(select name from `tabSales Invoice Item`
where parent=`tabSales Invoice`.name
and ifnull(`tabSales Invoice Item`.warehouse, '') = %(warehouse)s)"""

if filters.get("brand"):
conditions += """ and exists(select name from `tabSales Invoice Item`
where parent=`tabSales Invoice`.name
and ifnull(`tabSales Invoice Item`.brand, '') = %(brand)s)"""

if filters.get("item_group"):
conditions += """ and exists(select name from `tabSales Invoice Item`
def get_sales_invoice_item_field_condition(field, table="Sales Invoice Item") -> str:
if not filters.get(field) or field in accounting_dimensions_list:
return ""
return f""" and exists(select name from `tab{table}`
where parent=`tabSales Invoice`.name
and ifnull(`tabSales Invoice Item`.item_group, '') = %(item_group)s)"""
and ifnull(`tab{table}`.{field}, '') = %({field})s)"""

accounting_dimensions = get_accounting_dimensions(as_list=False)
conditions += get_sales_invoice_item_field_condition("mode_of_payments", "Sales Invoice Payment")
conditions += get_sales_invoice_item_field_condition("cost_center")
conditions += get_sales_invoice_item_field_condition("warehouse")
conditions += get_sales_invoice_item_field_condition("brand")
conditions += get_sales_invoice_item_field_condition("item_group")

if accounting_dimensions:
common_condition = """
Expand Down

0 comments on commit 0badfc8

Please sign in to comment.