diff --git a/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.js b/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.js index 6eb88eef9e..aa5cfce70d 100644 --- a/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.js +++ b/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.js @@ -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) { @@ -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 + }); +}; diff --git a/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.json b/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.json index 9c81c65f6b..27a9ab116e 100644 --- a/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.json +++ b/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.json @@ -96,6 +96,7 @@ "read_only": 1 }, { + "fetch_from": "item.item_name", "fieldname": "item_code", "fieldtype": "Data", "hide_days": 1, @@ -153,6 +154,7 @@ "no_copy": 1 }, { + "fetch_from": "item.description", "fieldname": "description", "fieldtype": "Small Text", "hide_days": 1, @@ -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", @@ -192,5 +194,6 @@ "restrict_to_domain": "Healthcare", "sort_field": "modified", "sort_order": "DESC", + "states": [], "title_field": "service_unit_type" } \ No newline at end of file diff --git a/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.py b/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.py index 6e40a8abf0..f997714a7b 100644 --- a/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.py +++ b/healthcare/healthcare/doctype/healthcare_service_unit_type/healthcare_service_unit_type.py @@ -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: @@ -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 diff --git a/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js b/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js index cb13270654..59cee6984b 100644 --- a/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js +++ b/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js @@ -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: [ @@ -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'); };