From a397c1dea8e9cd1a42ef4962b5735adcf627ca5f Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 6 Jun 2025 20:05:34 +0530 Subject: [PATCH] fix: update sql function usage syntax --- .../tax_withholding_category/tax_withholding_category.py | 4 ++-- .../item_wise_sales_register/item_wise_sales_register.py | 2 +- erpnext/controllers/sales_and_purchase_return.py | 6 +++++- erpnext/patches/v12_0/set_total_batch_quantity.py | 2 +- erpnext/setup/doctype/sales_person/sales_person.py | 4 ++-- .../stock/doctype/purchase_receipt/test_purchase_receipt.py | 2 +- .../doctype/stock_ledger_entry/test_stock_ledger_entry.py | 2 +- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py index fa9b226374c..2c6d13b3147 100644 --- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py +++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py @@ -657,7 +657,7 @@ def get_tcs_amount(parties, inv, tax_details, vouchers, adv_vouchers): "company": inv.company, "voucher_no": ["in", vouchers], }, - "sum(debit)", + [{"SUM": "debit"}], ) or 0.0 ) @@ -735,7 +735,7 @@ def get_limit_consumed(ldc, parties): "posting_date": ("between", (ldc.valid_from, ldc.valid_upto)), "company": ldc.company, }, - "sum(tax_withholding_net_total)", + [{"SUM": "tax_withholding_net_total"}], ) return limit_consumed diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py index a88a36c642c..52096ce365a 100644 --- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py +++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py @@ -525,7 +525,7 @@ def get_grand_total(filters, doctype): "docstatus": 1, "posting_date": ("between", [filters.get("from_date"), filters.get("to_date")]), }, - "sum(base_grand_total)", + [{"SUM": "base_grand_total"}], ) ) diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index a7c5b72cbb9..e5864ce13ad 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -7,6 +7,7 @@ import frappe from frappe import _ from frappe.model.meta import get_field_precision from frappe.query_builder import DocType +from frappe.query_builder.functions import Abs from frappe.utils import cint, flt, format_datetime, get_datetime import erpnext @@ -661,7 +662,10 @@ def get_rate_for_return( if voucher_type in ("Purchase Receipt", "Purchase Invoice", "Subcontracting Receipt"): select_field = "incoming_rate" else: - select_field = "abs(stock_value_difference / actual_qty)" + StockLedgerEntry = frappe.qb.DocType("Stock Ledger Entry") + select_field = Abs( + StockLedgerEntry.stock_value_difference / StockLedgerEntry.actual_qty + ) rate = flt(frappe.db.get_value("Stock Ledger Entry", filters, select_field)) if not (rate and return_against) and voucher_type in ["Sales Invoice", "Delivery Note"]: diff --git a/erpnext/patches/v12_0/set_total_batch_quantity.py b/erpnext/patches/v12_0/set_total_batch_quantity.py index 068e0a6a4c1..cedcbfa36e9 100644 --- a/erpnext/patches/v12_0/set_total_batch_quantity.py +++ b/erpnext/patches/v12_0/set_total_batch_quantity.py @@ -9,7 +9,7 @@ def execute(): frappe.db.get_value( "Stock Ledger Entry", {"docstatus": 1, "batch_no": batch.batch_id, "is_cancelled": 0}, - "sum(actual_qty)", + [{"SUM": "actual_qty"}], ) or 0.0 ) diff --git a/erpnext/setup/doctype/sales_person/sales_person.py b/erpnext/setup/doctype/sales_person/sales_person.py index 43c5afa313e..73e9e9641d3 100644 --- a/erpnext/setup/doctype/sales_person/sales_person.py +++ b/erpnext/setup/doctype/sales_person/sales_person.py @@ -64,7 +64,7 @@ class SalesPerson(NestedSet): frappe.db.get_value( "Sales Team", {"docstatus": 1, "parenttype": "Sales Order", "sales_person": self.sales_person_name}, - "sum(allocated_amount)", + [{"SUM": "allocated_amount"}], ) ) @@ -72,7 +72,7 @@ class SalesPerson(NestedSet): frappe.db.get_value( "Sales Team", {"docstatus": 1, "parenttype": "Sales Invoice", "sales_person": self.sales_person_name}, - "sum(allocated_amount)", + {"SUM": "allocated_amount"}, ) ) diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py index 7c58bdc45a7..d1e9cd8766b 100644 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py @@ -1799,7 +1799,7 @@ class TestPurchaseReceipt(IntegrationTestCase): "voucher_no": pr.name, "is_cancelled": 0, }, - fieldname=["sum(stock_value_difference)"], + fieldname=[{"SUM": "stock_value_difference"}], ) # Value of Stock Account should be equal to the sum of Stock Value Difference diff --git a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py index 4855327b08e..af950d04071 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py +++ b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py @@ -1039,7 +1039,7 @@ class TestStockLedgerEntry(IntegrationTestCase, StockTestMixin): "is_cancelled": 0, "account": "Stock In Hand - TCP1", }, - "sum(credit)", + [{"SUM": "credit"}], ) def _day(days):