feat: document naming rule will now use posting date of the document

(cherry picked from commit 22fd1a1cfd)
This commit is contained in:
Mihir Kandoi
2026-01-01 15:11:58 +05:30
committed by Mergify
parent 0a5aac9ce7
commit b03494bb67
2 changed files with 24 additions and 5 deletions

View File

@@ -11,6 +11,7 @@ import frappe.defaults
from frappe import _, qb, throw from frappe import _, qb, throw
from frappe.desk.reportview import build_match_conditions from frappe.desk.reportview import build_match_conditions
from frappe.model.meta import get_field_precision from frappe.model.meta import get_field_precision
from frappe.model.naming import determine_consecutive_week_number
from frappe.query_builder import AliasedQuery, Case, Criterion, Field, Table from frappe.query_builder import AliasedQuery, Case, Criterion, Field, Table
from frappe.query_builder.functions import Count, IfNull, Max, Round, Sum from frappe.query_builder.functions import Count, IfNull, Max, Round, Sum
from frappe.query_builder.utils import DocType from frappe.query_builder.utils import DocType
@@ -25,6 +26,7 @@ from frappe.utils import (
get_number_format_info, get_number_format_info,
getdate, getdate,
now, now,
now_datetime,
nowdate, nowdate,
) )
from frappe.utils.caching import site_cache from frappe.utils.caching import site_cache
@@ -66,6 +68,7 @@ def get_fiscal_year(
as_dict=False, as_dict=False,
boolean=None, boolean=None,
raise_on_missing=True, raise_on_missing=True,
truncate=False,
): ):
if isinstance(raise_on_missing, str): if isinstance(raise_on_missing, str):
raise_on_missing = loads(raise_on_missing) raise_on_missing = loads(raise_on_missing)
@@ -79,7 +82,14 @@ def get_fiscal_year(
fiscal_years = get_fiscal_years( fiscal_years = get_fiscal_years(
date, fiscal_year, label, verbose, company, as_dict=as_dict, raise_on_missing=raise_on_missing date, fiscal_year, label, verbose, company, as_dict=as_dict, raise_on_missing=raise_on_missing
) )
return False if not fiscal_years else fiscal_years[0]
if fiscal_years:
fiscal_year = fiscal_years[0]
if truncate:
return ("-".join(y[-2:] for y in fiscal_year[0].split("-")), fiscal_year[1], fiscal_year[2])
return fiscal_year
return False
def get_fiscal_years( def get_fiscal_years(
@@ -1501,14 +1511,14 @@ def get_autoname_with_number(number_value, doc_title, company):
def parse_naming_series_variable(doc, variable): def parse_naming_series_variable(doc, variable):
if variable == "FY": if variable in ["FY", "TFY"]:
if doc: if doc:
date = doc.get("posting_date") or doc.get("transaction_date") or getdate() date = doc.get("posting_date") or doc.get("transaction_date") or getdate()
company = doc.get("company") company = doc.get("company")
else: else:
date = getdate() date = getdate()
company = None company = None
return get_fiscal_year(date=date, company=company)[0] return get_fiscal_year(date=date, company=company, truncate=variable == "TFY")[0]
elif variable == "ABBR": elif variable == "ABBR":
if doc: if doc:
@@ -1518,6 +1528,14 @@ def parse_naming_series_variable(doc, variable):
return frappe.db.get_value("Company", company, "abbr") if company else "" return frappe.db.get_value("Company", company, "abbr") if company else ""
else:
data = {"YY": "%y", "YYYY": "%Y", "MM": "%m", "DD": "%d", "JJJ": "%j"}
date = (
getdate(doc.get("posting_date") or doc.get("transaction_date") or doc.get("posting_datetime"))
or now_datetime()
)
return date.strftime(data[variable]) if variable in data else determine_consecutive_week_number(date)
@frappe.whitelist() @frappe.whitelist()
def get_coa(doctype, parent, is_root=None, chart=None): def get_coa(doctype, parent, is_root=None, chart=None):

View File

@@ -402,9 +402,10 @@ doc_events = {
} }
# function should expect the variable and doc as arguments # function should expect the variable and doc as arguments
naming_series_variables_list = ["FY", "TFY", "ABBR", "MM", "DD", "YY", "YYYY", "JJJ", "WW"]
naming_series_variables = { naming_series_variables = {
"FY": "erpnext.accounts.utils.parse_naming_series_variable", variable: "erpnext.accounts.utils.parse_naming_series_variable"
"ABBR": "erpnext.accounts.utils.parse_naming_series_variable", for variable in naming_series_variables_list
} }
# On cancel event Payment Entry will be exempted and all linked submittable doctype will get cancelled. # On cancel event Payment Entry will be exempted and all linked submittable doctype will get cancelled.