Skip to content

Commit

Permalink
fix(Medication): enable tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajinsr committed Sep 27, 2024
1 parent ac19668 commit 4897d0c
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
12 changes: 12 additions & 0 deletions healthcare/healthcare/doctype/medication/medication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
);
}
}
});

Expand Down
40 changes: 40 additions & 0 deletions healthcare/healthcare/doctype/medication/medication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<div style="padding: 15px;">
<div class="row mb-5">
<div class="col-md-4" style="max-height: 500px">
{% if data.image && !["undefined", "''"].includes(data.image) %}
<div class="border image-field " style="overflow: hidden;border-color:#e6e6e6">
<img class="responsive" src={{ data.image }}>
</div>
{% endif %}
</div>
<div class="col-md-8 h-500">
{% if data.value && data.value != "Medication" %}
<h4>
{{ __("Medication Details") }}
</h4>
<div style="padding-top: 10px;">
<p>Class : {{ data.medication_class ? data.medication_class : "" }}</p>
<p>Strength : {{ data.strength ? data.strength : "" }}</p>
<p>UOM : {{ data.uom ? data.uom : "" }}</p>
</div>
{% endif %}
{% if data.item_code %}
<h4>
{{ __("Description") }}
</h4>
<div style="padding-top: 10px;">
<p>{{ data.description }}</p>
<p>Rate: {{ format_currency(data.rate, data.currency) }}</p>
</div>
{% endif %}
<hr style="margin: 15px -15px;">
<p>
{% if data.value && data.value != "Medication" %}
<a style="margin-right: 7px; margin-bottom: 7px" class="btn btn-default btn-xs" href="/app/medication/{{ data.value }}">
{{ __("Open Medication {0}", [data.value.bold()]) }}</a>
{% endif %}
{% if data.item_code %}
<a style="margin-right: 7px; margin-bottom: 7px" class="btn btn-default btn-xs" href="/app/item/{{ data.item_code }}">
{{ __("Open Item {0}", [data.item_code.bold()]) }}</a>
{% endif %}
</p>
</div>
</div>
</div>
71 changes: 71 additions & 0 deletions healthcare/healthcare/doctype/medication/medication_tree.js
Original file line number Diff line number Diff line change
@@ -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",
};

0 comments on commit 4897d0c

Please sign in to comment.