Skip to content

Commit 83bb820

Browse files
committed
feat(Code List): default common code
Allow linking to a Common Code that shall be used as the default. Add helper methods to retrieve the default code.
1 parent 16c786c commit 83bb820

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

erpnext/edi/doctype/code_list/code_list.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,13 @@ frappe.ui.form.on("Code List", {
3737
}
3838
);
3939
};
40+
41+
frm.set_query("default_common_code", function (doc) {
42+
return {
43+
filters: {
44+
code_list: doc.name,
45+
},
46+
};
47+
});
4048
},
4149
});

erpnext/edi/doctype/code_list/code_list.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"title",
1111
"canonical_uri",
1212
"url",
13+
"default_common_code",
1314
"column_break_nkls",
1415
"version",
1516
"publisher",
@@ -66,6 +67,13 @@
6667
"fieldtype": "Data",
6768
"label": "URL",
6869
"options": "URL"
70+
},
71+
{
72+
"description": "This value shall be used when no matching Common Code for a record is found.",
73+
"fieldname": "default_common_code",
74+
"fieldtype": "Link",
75+
"label": "Default Common Code",
76+
"options": "Common Code"
6977
}
7078
],
7179
"index_web_pages_for_search": 1,
@@ -75,7 +83,7 @@
7583
"link_fieldname": "code_list"
7684
}
7785
],
78-
"modified": "2024-10-01 12:32:27.990888",
86+
"modified": "2024-11-06 18:56:23.177120",
7987
"modified_by": "Administrator",
8088
"module": "EDI",
8189
"name": "Code List",

erpnext/edi/doctype/code_list/code_list.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CodeList(Document):
2020
from frappe.types import DF
2121

2222
canonical_uri: DF.Data | None
23+
default_common_code: DF.Link | None
2324
description: DF.SmallText | None
2425
publisher: DF.Data | None
2526
publisher_id: DF.Data | None
@@ -33,6 +34,8 @@ def on_trash(self):
3334
self.__delete_linked_docs()
3435

3536
def __delete_linked_docs(self):
37+
self.db_set("default_common_code", None)
38+
3639
linked_docs = frappe.get_all(
3740
"Common Code",
3841
filters={"code_list": self.name},
@@ -50,6 +53,14 @@ def get_docnames_for(self, doctype: str, code: str) -> tuple[str]:
5053
"""Get the mapped docnames for a doctype and code"""
5154
return get_docnames_for(self.name, doctype, code)
5255

56+
def get_default_code(self) -> str | None:
57+
"""Get the default common code for this code list"""
58+
return (
59+
frappe.db.get_value("Common Code", self.default_common_code, "common_code")
60+
if self.default_common_code
61+
else None
62+
)
63+
5364
def from_genericode(self, root: "Element"):
5465
"""Extract Code List details from genericode XML"""
5566
self.title = root.find(".//Identification/ShortName").text
@@ -106,3 +117,9 @@ def get_docnames_for(code_list: str, doctype: str, code: str) -> tuple[str]:
106117
).run()
107118

108119
return tuple(d[0] for d in docnames) if docnames else ()
120+
121+
122+
def get_default_code(code_list: str) -> str | None:
123+
"""Return the default common code for a given code list"""
124+
code_id = frappe.db.get_value("Code List", code_list, "default_common_code")
125+
return frappe.db.get_value("Common Code", code_id, "common_code") if code_id else None

0 commit comments

Comments
 (0)