mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-13 17:53:49 +00:00
fix(postgres): compute current month sales without DATE_FORMAT
(cherry picked from commit 64f391adf7)
This commit is contained in:
@@ -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.contacts.address_and_contact import load_address_and_contact
|
||||||
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
|
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.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 frappe.utils.nestedset import NestedSet, rebuild_tree
|
||||||
|
|
||||||
from erpnext.accounts.doctype.account.account import get_account_currency
|
from erpnext.accounts.doctype.account.account import get_account_currency
|
||||||
@@ -762,31 +771,41 @@ def install_country_fixtures(company, country):
|
|||||||
|
|
||||||
|
|
||||||
def update_company_current_month_sales(company):
|
def update_company_current_month_sales(company):
|
||||||
from_date = get_first_day(today())
|
"""Update Company's Total Monthly Sales.
|
||||||
to_date = get_first_day(add_months(from_date, 1))
|
|
||||||
|
|
||||||
results = frappe.db.sql(
|
Postgres compatibility:
|
||||||
"""
|
- Avoid MariaDB-only DATE_FORMAT().
|
||||||
SELECT
|
- Use a date range for the current month instead (portable + index-friendly).
|
||||||
SUM(base_grand_total) AS total,
|
"""
|
||||||
DATE_FORMAT(posting_date, '%%m-%%Y') AS month_year
|
|
||||||
FROM
|
# Local imports so you don't have to touch file-level imports
|
||||||
`tabSales Invoice`
|
from frappe.query_builder.functions import Sum
|
||||||
WHERE
|
|
||||||
posting_date >= %s
|
start_date = get_first_day(today())
|
||||||
AND posting_date < %s
|
end_date = get_last_day(today())
|
||||||
AND docstatus = 1
|
|
||||||
AND company = %s
|
si = frappe.qb.DocType("Sales Invoice")
|
||||||
GROUP BY
|
|
||||||
month_year
|
total_monthly_sales = (
|
||||||
""",
|
frappe.qb.from_(si)
|
||||||
(from_date, to_date, company),
|
.select(Sum(si.base_grand_total))
|
||||||
as_dict=True,
|
.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):
|
def update_company_monthly_sales(company):
|
||||||
"""Cache past year monthly sales of every company based on sales invoices"""
|
"""Cache past year monthly sales of every company based on sales invoices"""
|
||||||
|
|||||||
Reference in New Issue
Block a user