From 4897d0cfc7ad32175f1889eb47abb93cb4e411bb Mon Sep 17 00:00:00 2001 From: Sajin SR Date: Wed, 25 Sep 2024 08:44:02 +0530 Subject: [PATCH] fix(Medication): enable tree view --- .../doctype/medication/medication.js | 12 ++++ .../doctype/medication/medication.py | 40 +++++++++++ .../medication/medication_item_preview.html | 43 +++++++++++ .../doctype/medication/medication_tree.js | 71 +++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 healthcare/healthcare/doctype/medication/medication_item_preview.html create mode 100644 healthcare/healthcare/doctype/medication/medication_tree.js diff --git a/healthcare/healthcare/doctype/medication/medication.js b/healthcare/healthcare/doctype/medication/medication.js index 46f0f4daef..f7b8a2a522 100644 --- a/healthcare/healthcare/doctype/medication/medication.js +++ b/healthcare/healthcare/doctype/medication/medication.js @@ -13,6 +13,18 @@ frappe.ui.form.on('Medication', { } }; }); + + if (!frm.is_new()) { + frm.add_custom_button( + __("Browse Medication"), + function () { + frappe.route_options = { + medication: frm.doc.name, + }; + frappe.set_route("Tree", "Medication"); + }, + ); + } } }); diff --git a/healthcare/healthcare/doctype/medication/medication.py b/healthcare/healthcare/doctype/medication/medication.py index 264d6ec34a..5f9637afbf 100644 --- a/healthcare/healthcare/doctype/medication/medication.py +++ b/healthcare/healthcare/doctype/medication/medication.py @@ -126,3 +126,43 @@ def change_item_code_from_medication(item_code, doc): rename_doc("Item", doc.item_code, item_code, ignore_permissions=True) frappe.db.set_value("Medication", doc.name, "item_code", item_code) return + + +@frappe.whitelist() +def get_children(parent=None, is_root=False, **filters): + if not parent or parent == "Medication": + frappe.msgprint(_("Please select a Medication")) + return + + if parent: + frappe.form_dict.parent = parent + + if frappe.form_dict.parent: + medication_doc = frappe.get_cached_doc("Medication", frappe.form_dict.parent) + frappe.has_permission("Medication", doc=medication_doc, throw=True) + + medication_items = frappe.get_all( + "Medication Linked Item", + fields=["item as item_code", "rate"], + filters=[["parent", "=", frappe.form_dict.parent]], + order_by="idx", + ) + + item_names = tuple(d.get("item_code") for d in medication_items) + + items = frappe.get_list( + "Item", + fields=["image", "description", "name", "stock_uom", "item_name"], + filters=[["name", "in", item_names]], + ) # to get only required item dicts + + for medication_item in medication_items: + # extend medication_item dict with respective item dict + medication_item.update( + # returns an item dict from items list which matches with item_code + next(item for item in items if item.get("name") == medication_item.get("item_code")) + ) + medication_item.image = frappe.db.escape(medication_item.image) + medication_item.currency = frappe.db.get_single_value("Global Defaults", "default_currency") + + return medication_items diff --git a/healthcare/healthcare/doctype/medication/medication_item_preview.html b/healthcare/healthcare/doctype/medication/medication_item_preview.html new file mode 100644 index 0000000000..967bd772bb --- /dev/null +++ b/healthcare/healthcare/doctype/medication/medication_item_preview.html @@ -0,0 +1,43 @@ +
+
+
+ {% if data.image && !["undefined", "''"].includes(data.image) %} +
+ +
+ {% endif %} +
+
+ {% if data.value && data.value != "Medication" %} +

+ {{ __("Medication Details") }} +

+
+

Class : {{ data.medication_class ? data.medication_class : "" }}

+

Strength : {{ data.strength ? data.strength : "" }}

+

UOM : {{ data.uom ? data.uom : "" }}

+
+ {% endif %} + {% if data.item_code %} +

+ {{ __("Description") }} +

+
+

{{ data.description }}

+

Rate: {{ format_currency(data.rate, data.currency) }}

+
+ {% endif %} +
+

+ {% if data.value && data.value != "Medication" %} + + {{ __("Open Medication {0}", [data.value.bold()]) }} + {% endif %} + {% if data.item_code %} + + {{ __("Open Item {0}", [data.item_code.bold()]) }} + {% endif %} +

+
+
+
diff --git a/healthcare/healthcare/doctype/medication/medication_tree.js b/healthcare/healthcare/doctype/medication/medication_tree.js new file mode 100644 index 0000000000..95db8191c1 --- /dev/null +++ b/healthcare/healthcare/doctype/medication/medication_tree.js @@ -0,0 +1,71 @@ +frappe.treeview_settings["Medication"] = { + get_tree_nodes: "healthcare.healthcare.doctype.medication.medication.get_children", + filters: [ + { + fieldname: "medication", + fieldtype: "Link", + options: "Medication", + label: __("Medication"), + }, + ], + title: "Medication", + breadcrumb: "Healthcare", + disable_add_node: true, + root_label: "Medication", + get_tree_root: false, + show_expand_all: false, + get_label: function (node) { + return node.data.item_code || node.data.value; + }, + onload: function (me) { + var label = frappe.get_route()[0] + "/" + frappe.get_route()[1]; + + if (frappe.pages[label]) { + delete frappe.pages[label]; + } + + var filter = me.opts.filters[0]; + if (frappe.route_options && frappe.route_options[filter.fieldname]) { + var val = frappe.route_options[filter.fieldname]; + delete frappe.route_options[filter.fieldname]; + filter.default = ""; + me.args[filter.fieldname] = val; + me.root_label = val; + me.page.set_title(val); + } + me.make_tree(); + }, + toolbar: [ + { toggle_btn: true }, + { + label: __("Edit"), + condition: function (node) { + return node.expandable; + }, + click: function (node) { + frappe.set_route("Form", "Medication", node.data.value); + }, + }, + ], + menu_items: [ + { + label: __("New Medication"), + action: function () { + frappe.new_doc("Medication", true); + }, + condition: 'frappe.boot.user.can_create.indexOf("Medication") !== -1', + }, + ], + onrender: function (node) { + if (node.is_root && node.data.value != "Medication") { + frappe.model.with_doc("Medication", node.data.value, function () { + var medication = frappe.model.get_doc("Medication", node.data.value); + node.data.strength = medication.strength || ""; + node.data.item_code = medication.item || ""; + node.data.uom = medication.strength_uom || ""; + node.data.medication_class = medication.medication_class || ""; + }); + } + }, + view_template: "medication_item_preview", +};