From 49760e4542a74334cb3df3828a744dbf59b0389a Mon Sep 17 00:00:00 2001 From: Matt Howard Date: Mon, 5 Jan 2026 15:28:45 -0500 Subject: [PATCH] fix(postgres): compute current month sales without DATE_FORMAT (cherry picked from commit 64f391adf7c5c60414af291260bbba3425463964) --- erpnext/setup/doctype/company/company.py | 65 +++++++++++++++--------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index def469e4fa3..5a2b6db83f2 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -11,7 +11,16 @@ from frappe.cache_manager import clear_defaults_cache from frappe.contacts.address_and_contact import load_address_and_contact from frappe.custom.doctype.property_setter.property_setter import make_property_setter from frappe.desk.page.setup_wizard.setup_wizard import make_records -from frappe.utils import add_months, cint, formatdate, get_first_day, get_link_to_form, get_timestamp, today +from frappe.utils import ( + add_months, + cint, + formatdate, + get_first_day, + get_last_day, + get_link_to_form, + get_timestamp, + today, +) from frappe.utils.nestedset import NestedSet, rebuild_tree from erpnext.accounts.doctype.account.account import get_account_currency @@ -866,31 +875,41 @@ def install_country_fixtures(company, country): def update_company_current_month_sales(company): - from_date = get_first_day(today()) - to_date = get_first_day(add_months(from_date, 1)) + """Update Company's Total Monthly Sales. - results = frappe.db.sql( - """ - SELECT - SUM(base_grand_total) AS total, - DATE_FORMAT(posting_date, '%%m-%%Y') AS month_year - FROM - `tabSales Invoice` - WHERE - posting_date >= %s - AND posting_date < %s - AND docstatus = 1 - AND company = %s - GROUP BY - month_year - """, - (from_date, to_date, company), - as_dict=True, + Postgres compatibility: + - Avoid MariaDB-only DATE_FORMAT(). + - Use a date range for the current month instead (portable + index-friendly). + """ + + # Local imports so you don't have to touch file-level imports + from frappe.query_builder.functions import Sum + + start_date = get_first_day(today()) + end_date = get_last_day(today()) + + si = frappe.qb.DocType("Sales Invoice") + + total_monthly_sales = ( + frappe.qb.from_(si) + .select(Sum(si.base_grand_total)) + .where( + (si.docstatus == 1) + & (si.company == company) + & (si.posting_date >= start_date) + & (si.posting_date <= end_date) + ) + ).run(pluck=True)[0] or 0 + + # Fieldname in standard ERPNext is `total_monthly_sales` + frappe.db.set_value( + "Company", + company, + "total_monthly_sales", + total_monthly_sales, + update_modified=False, ) - monthly_total = results[0]["total"] if len(results) > 0 else 0 - frappe.db.set_value("Company", company, "total_monthly_sales", monthly_total) - def update_company_monthly_sales(company): """Cache past year monthly sales of every company based on sales invoices"""