Skip to content

Commit

Permalink
fix(Healthcare Service Unit Type): move item creation from after_inse…
Browse files Browse the repository at this point in the history
…rt to validate

(cherry picked from commit 8b00487)
  • Loading branch information
Sajinsr authored and mergify[bot] committed Apr 30, 2024
1 parent 07fce5c commit 7662a72
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
frappe.ui.form.on('Healthcare Service Unit Type', {
refresh: function(frm) {
frm.set_df_property('item_code', 'read_only', frm.doc.__islocal ? 0 : 1);
if (!frm.doc.__islocal && frm.doc.is_billable) {
if (!frm.doc.__islocal && frm.doc.is_billable && frm.doc.item) {
frm.add_custom_button(__('Change Item Code'), function() {
change_item_code(cur_frm, frm.doc);
});
}
if (!frm.doc.__islocal && frm.doc.is_billable && !frm.doc.item) {
frm.add_custom_button(__("Create/Link Item"), function() {
create_item(frm);
});
}
},

service_unit_type: function(frm) {
Expand Down Expand Up @@ -84,3 +89,55 @@ let change_item_code = function(frm, doc) {
'Item Code': frm.doc.item_code
});
};

let create_item = function(frm) {
let d = new frappe.ui.Dialog({
title: __("Create/Link Item"),
fields: [
{
"fieldtype": "Link",
"label": "Item",
"fieldname": "item",
"options": "Item",
"mandatory_depends_on": "eval:doc.link_existing_item==1",
"depends_on": "eval:doc.link_existing_item==1"
},
{
"fieldtype": "Data",
"label": "Item Code",
"fieldname": "item_code",
"default": frm.doc.item_code,
"mandatory_depends_on": "eval:doc.link_existing_item==0",
"read_only": 1,
"depends_on": "eval:doc.link_existing_item==0"
},
{
"fieldtype": "Check",
"label": "Link Existing Item",
"fieldname": "link_existing_item",
"default": 0
}
],
primary_action: function() {
if (d.get_value("link_existing_item") && d.get_value("item")) {
frm.set_value("item", d.get_value("item"));
frm.save();
} else if (!d.get_value("link_existing_item") && d.get_value("item_code")) {
frappe.call({
"method": "create_service_unit_item",
"doc": frm.doc,
callback: function() {
frm.reload_doc();
}
});
}
d.hide();
},
primary_action_label: __("Create/Link Code")
});

d.show();
d.set_values({
'Item Code': frm.doc.item_code
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"read_only": 1
},
{
"fetch_from": "item.item_name",
"fieldname": "item_code",
"fieldtype": "Data",
"hide_days": 1,
Expand Down Expand Up @@ -153,6 +154,7 @@
"no_copy": 1
},
{
"fetch_from": "item.description",
"fieldname": "description",
"fieldtype": "Small Text",
"hide_days": 1,
Expand All @@ -170,7 +172,7 @@
}
],
"links": [],
"modified": "2021-08-19 17:52:30.266667",
"modified": "2024-04-29 18:59:16.479821",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Healthcare Service Unit Type",
Expand All @@ -192,5 +194,6 @@
"restrict_to_domain": "Healthcare",
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "service_unit_type"
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ def validate(self):
else:
frappe.db.set_value("Item", self.item, "disabled", 0)

def after_insert(self):
if self.inpatient_occupancy and self.is_billable:
create_item(self)
if self.inpatient_occupancy and self.is_billable and not self.item:
self.create_service_unit_item()

def on_trash(self):
if self.item:
Expand Down Expand Up @@ -72,10 +71,14 @@ def on_update(self):
frappe.db.set_value("Item", self.item, "disabled", 1)
self.reload()

@frappe.whitelist()
def create_service_unit_item(self):
create_item(self)


def item_price_exists(doc):
item_price = frappe.db.exists({"doctype": "Item Price", "item_code": doc.item_code})
if len(item_price):
if item_price and len(item_price):
return item_price[0][0]
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ frappe.ui.form.on('Patient Encounter', {
});

var schedule_inpatient = function(frm) {
let service_unit_type = "";
var dialog = new frappe.ui.Dialog({
title: 'Patient Admission',
fields: [
Expand Down Expand Up @@ -388,6 +389,22 @@ var schedule_inpatient = function(frm) {
};
};

dialog.fields_dict["service_unit_type"].df.onchange = () => {
if (dialog.get_value("service_unit_type") && dialog.get_value("service_unit_type") != service_unit_type) {
service_unit_type = dialog.get_value("service_unit_type");
frappe.db.get_value("Healthcare Service Unit Type", {name: dialog.get_value("service_unit_type")}, ["is_billable", "item"])
.then(r => {
if (r.message.is_billable && !r.message.item) {
frappe.msgprint({
message: __("Selected service unit type doesn't have any item linked"),
title: __("Warning"),
indicator: "orange",
});
}
})
}
};

dialog.show();
dialog.$wrapper.find('.modal-dialog').css('width', '800px');
};
Expand Down

0 comments on commit 7662a72

Please sign in to comment.