|
8 | 8 | from frappe.utils import flt |
9 | 9 | from frappe.utils.nestedset import get_descendants_of |
10 | 10 | from frappe.utils.xlsxutils import handle_html |
| 11 | +from pypika.terms import Bracket, LiteralValue, Order |
11 | 12 |
|
12 | 13 | from erpnext.accounts.report.sales_register.sales_register import get_mode_of_payments |
13 | 14 | from erpnext.accounts.report.utils import get_values_for_columns |
@@ -390,20 +391,21 @@ def apply_conditions(query, si, sii, sip, filters, additional_conditions=None): |
390 | 391 |
|
391 | 392 |
|
392 | 393 | def apply_order_by_conditions(doctype, query, filters): |
393 | | - invoice = f"`tab{doctype}`" |
394 | | - invoice_item = f"`tab{doctype} Item`" |
| 394 | + invoice = frappe.qb.DocType(doctype) |
| 395 | + invoice_item = frappe.qb.DocType(f"{doctype} Item") |
395 | 396 |
|
396 | 397 | if not filters.get("group_by"): |
397 | | - query += f" order by {invoice}.posting_date desc, {invoice_item}.item_group desc" |
| 398 | + query = query.orderby(invoice.posting_date, order=Order.desc) |
| 399 | + query = query.orderby(invoice_item.item_group, order=Order.desc) |
398 | 400 | elif filters.get("group_by") == "Invoice": |
399 | | - query += f" order by {invoice_item}.parent desc" |
| 401 | + query = query.orderby(invoice_item.parent, order=Order.desc) |
400 | 402 | elif filters.get("group_by") == "Item": |
401 | | - query += f" order by {invoice_item}.item_code" |
| 403 | + query = query.orderby(invoice_item.item_code) |
402 | 404 | elif filters.get("group_by") == "Item Group": |
403 | | - query += f" order by {invoice_item}.item_group" |
| 405 | + query = query.orderby(invoice_item.item_group) |
404 | 406 | elif filters.get("group_by") in ("Customer", "Customer Group", "Territory", "Supplier"): |
405 | 407 | filter_field = frappe.scrub(filters.get("group_by")) |
406 | | - query += f" order by {filter_field} desc" |
| 408 | + query = query.orderby(filter_field, order=Order.desc) |
407 | 409 |
|
408 | 410 | return query |
409 | 411 |
|
@@ -481,15 +483,12 @@ def get_items(filters, additional_query_columns, additional_conditions=None): |
481 | 483 |
|
482 | 484 | from frappe.desk.reportview import build_match_conditions |
483 | 485 |
|
484 | | - query, params = query.walk() |
485 | | - match_conditions = build_match_conditions(doctype) |
486 | | - |
487 | | - if match_conditions: |
488 | | - query += " and " + match_conditions |
| 486 | + if match_conditions := build_match_conditions(doctype): |
| 487 | + query = query.where(Bracket(LiteralValue(match_conditions))) |
489 | 488 |
|
490 | 489 | query = apply_order_by_conditions(doctype, query, filters) |
491 | 490 |
|
492 | | - return frappe.db.sql(query, params, as_dict=True) |
| 491 | + return query.run(as_dict=True) |
493 | 492 |
|
494 | 493 |
|
495 | 494 | def get_delivery_notes_against_sales_order(item_list): |
|
0 commit comments