feat: preview next numbers on naming series tool

This commit is contained in:
Ankush Menat 2022-05-26 13:13:49 +05:30
parent 82cd54b40b
commit 24d1bf5328
3 changed files with 88 additions and 4 deletions

View File

@ -54,5 +54,35 @@ frappe.ui.form.on("Naming Series", {
frm.events.get_doc_and_prefix(frm);
}
});
}
},
naming_series_to_check(frm) {
frappe.call({
method: "preview_series",
doc: frm.doc,
callback: function(r) {
if (!r.exc) {
frm.set_value("preview", r.message);
} else {
frm.set_value("preview", __("Failed to generate preview of series"));
}
}
});
},
add_series(frm) {
const series = frm.doc.naming_series_to_check;
if (!series) {
frappe.show_alert(__("Please type a valid series."));
return;
}
if (!frm.doc.set_options.includes(series)) {
const current_series = frm.doc.set_options;
frm.set_value("set_options", `${current_series}\n${series}`);
} else {
frappe.show_alert(__("Series already added to transaction."));
}
},
});

View File

@ -8,9 +8,13 @@
"setup_series",
"select_doc_for_series",
"help_html",
"naming_series_to_check",
"preview",
"add_series",
"set_options",
"user_must_always_select",
"update",
"column_break_13",
"update_series",
"prefix",
"current_value",
@ -33,7 +37,7 @@
"fieldname": "help_html",
"fieldtype": "HTML",
"label": "Help HTML",
"options": "<div class=\"well\">\nEdit list of Series in the box below. Rules:\n<ul>\n<li>Each Series Prefix on a new line.</li>\n<li>Allowed special characters are \"/\" and \"-\"</li>\n<li>Optionally, set the number of digits in the series using dot (.) followed by hashes (#). For example, \".####\" means that the series will have four digits. Default is five digits.</li>\n</ul>\nExamples:<br>\nINV-<br>\nINV-10-<br>\nINVK-<br>\nINV-.####<br>\n</div>"
"options": "<div class=\"well\">\nEdit list of Series in the box below. Rules:\n<ul>\n<li>Each Series Prefix on a new line.</li>\n<li>Allowed special characters are \"/\" and \"-\"</li>\n<li>Optionally, set the number of digits in the series using dot (.) followed by hashes (#). For example, \".####\" means that the series will have four digits. Default is five digits.</li>\n</ul>\nExamples:<br>\nINV-<br>\nINV-10-<br>\nINVK-<br>\nINV-.####<br>\n</div>\n<br>"
},
{
"depends_on": "select_doc_for_series",
@ -77,6 +81,27 @@
"fieldtype": "Button",
"label": "Update Series Number",
"options": "update_series_start"
},
{
"fieldname": "naming_series_to_check",
"fieldtype": "Data",
"label": "Try a naming Series"
},
{
"default": " ",
"fieldname": "preview",
"fieldtype": "Text",
"label": "Preview of generated names",
"read_only": 1
},
{
"fieldname": "column_break_13",
"fieldtype": "Column Break"
},
{
"fieldname": "add_series",
"fieldtype": "Button",
"label": "Add this Series"
}
],
"hide_toolbar": 1,
@ -84,7 +109,7 @@
"idx": 1,
"issingle": 1,
"links": [],
"modified": "2022-05-26 03:13:05.357751",
"modified": "2022-05-26 05:19:10.392657",
"modified_by": "Administrator",
"module": "Setup",
"name": "Naming Series",

View File

@ -6,7 +6,7 @@ import frappe
from frappe import _, msgprint, throw
from frappe.core.doctype.doctype.doctype import validate_series
from frappe.model.document import Document
from frappe.model.naming import parse_naming_series
from frappe.model.naming import make_autoname, parse_naming_series
from frappe.permissions import get_doctypes_with_read
from frappe.utils import cint, cstr
@ -206,6 +206,35 @@ class NamingSeries(Document):
prefix = parse_naming_series(parts)
return prefix
@frappe.whitelist()
def preview_series(self) -> str:
"""Preview what the naming series will generate."""
generated_names = []
series = self.naming_series_to_check
if not series:
return ""
try:
doc = self._fetch_last_doc_if_available()
for _count in range(3):
generated_names.append(make_autoname(series, doc=doc))
except Exception as e:
if frappe.message_log:
frappe.message_log.pop()
return _("Failed to generate names from the series") + f"\n{str(e)}"
# Explcitly rollback in case any changes were made to series table.
frappe.db.rollback() # nosemgrep
return "\n".join(generated_names)
def _fetch_last_doc_if_available(self):
"""Fetch last doc for evaluating naming series with fields."""
try:
return frappe.get_last_doc(self.select_doc_for_series)
except Exception:
return None
def set_by_naming_series(
doctype, fieldname, naming_series, hide_name_field=True, make_mandatory=1