From e82f27ac10c5fc2beed21f6d1a4a45f54883bf01 Mon Sep 17 00:00:00 2001 From: Jamsheer Date: Mon, 30 Jul 2018 11:28:37 +0530 Subject: [PATCH] Healthcare - Invoice Drug Prescription --- .../doctype/sales_invoice/sales_invoice.js | 164 ++++++++++++++---- erpnext/healthcare/utils.py | 16 ++ 2 files changed, 147 insertions(+), 33 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 42363ce50f..36d69fa1f6 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -748,6 +748,9 @@ frappe.ui.form.on('Sales Invoice', { frm.add_custom_button(__('Healthcare Services'), function() { get_healthcare_services_to_invoice(frm); },"Get items from"); + frm.add_custom_button(__('Prescriptions'), function() { + get_drugs_to_invoice(frm); + },"Get items from"); } } else{ @@ -887,7 +890,10 @@ var get_healthcare_services_to_invoice = function(frm) { var patient = dialog.fields_dict.patient.input.value; if(patient && patient!=selected_patient){ selected_patient = patient; - get_services(frm, patient, $results, $placeholder) + var method = "erpnext.healthcare.utils.get_healthcare_services_to_invoice"; + var args = {patient: patient}; + var columns = (["service", "reference_name", "reference_type"]); + get_healthcare_items(frm, true, $results, $placeholder, method, args, columns); } else if(!patient){ selected_patient = ''; @@ -908,37 +914,34 @@ var get_healthcare_services_to_invoice = function(frm) { $results.find('.list-item-container .list-row-check') .prop("checked", ($(e.target).is(':checked'))); }); - set_primary_action(frm, dialog, $results); + set_primary_action(frm, dialog, $results, true); dialog.show(); }; -var get_services= function(frm, patient_id, $results, $placeholder) { +var get_healthcare_items = function(frm, invoice_healthcare_services, $results, $placeholder, method, args, columns) { var me = this; $results.empty(); frappe.call({ - method:"erpnext.healthcare.utils.get_healthcare_services_to_invoice", - args: { - patient: patient_id - }, + method: method, + args: args, callback: function(data) { if(data.message){ - $results.append(make_list_row()); + $results.append(make_list_row(columns, invoice_healthcare_services)); for(let i=0; i ${ @@ -958,39 +961,27 @@ var make_list_row= function(result={}) { ${contents} `); - head ? $row.addClass('list-item--head') - : $row = $(`
-
`).append($row); + $row = list_row_data_items(head, $row, result, invoice_healthcare_services); return $row; }; -var set_primary_action= function(frm, dialog, $results) { +var set_primary_action= function(frm, dialog, $results, invoice_healthcare_services) { var me = this; dialog.set_primary_action(__('Add'), function() { let checked_values = get_checked_values($results); if(checked_values.length > 0){ frm.set_value("patient", dialog.fields_dict.patient.input.value); frm.set_value("items", []); - frappe.call({ - doc: frm.doc, - method: "set_healthcare_services", - args:{ - checked_values: checked_values - }, - callback: function() { - frm.trigger("validate"); - frm.refresh_fields(); - } - }); + add_to_item_line(frm, checked_values, invoice_healthcare_services); dialog.hide(); } else{ - frappe.msgprint(__("Please select Healthcare Service")); + if(invoice_healthcare_services){ + frappe.msgprint(__("Please select Healthcare Service")); + } + else{ + frappe.msgprint(__("Please select Drug")); + } } }); }; @@ -1030,3 +1021,110 @@ var get_checked_values= function($results) { } }).get(); }; + +var get_drugs_to_invoice = function(frm) { + var me = this; + let selected_encounter = ''; + var dialog = new frappe.ui.Dialog({ + title: __("Get Items from Prescriptions"), + fields:[ + { fieldtype: 'Link', options: 'Patient', label: 'Patient', fieldname: "patient", reqd: true }, + { fieldtype: 'Link', options: 'Patient Encounter', label: 'Patient Encounter', fieldname: "encounter", reqd: true, + description:'Quantity will be calculated only for items which has "Nos" as UoM. You may change as required for each invoice item.', + get_query: function(doc) { + return { + filters: { patient :dialog.get_value("patient") } + }; + } + }, + { fieldtype: 'Section Break' }, + { fieldtype: 'HTML', fieldname: 'results_area' } + ] + }); + var $wrapper; + var $results; + var $placeholder; + dialog.set_values({ + 'patient': frm.doc.patient, + 'encounter': "" + }); + dialog.fields_dict["encounter"].df.onchange = () => { + var encounter = dialog.fields_dict.encounter.input.value; + if(encounter && encounter!=selected_encounter){ + selected_encounter = encounter; + var method = "erpnext.healthcare.utils.get_drugs_to_invoice"; + var args = {encounter: encounter}; + var columns = (["drug_code", "quantity", "description"]); + get_healthcare_items(frm, false, $results, $placeholder, method, args, columns); + } + else if(!encounter){ + selected_encounter = ''; + $results.empty(); + $results.append($placeholder); + } + } + $wrapper = dialog.fields_dict.results_area.$wrapper.append(`
`); + $results = $wrapper.find('.results'); + $placeholder = $(`
+ + +

No Drug Prescription found

+
+
`); + $results.on('click', '.list-item--head :checkbox', (e) => { + $results.find('.list-item-container .list-row-check') + .prop("checked", ($(e.target).is(':checked'))); + }); + set_primary_action(frm, dialog, $results, false); + dialog.show(); +}; + +var list_row_data_items = function(head, $row, result, invoice_healthcare_services) { + if(invoice_healthcare_services){ + head ? $row.addClass('list-item--head') + : $row = $(`
+
`).append($row); + } + else{ + head ? $row.addClass('list-item--head') + : $row = $(`
+
`).append($row); + } + return $row +}; + +var add_to_item_line = function(frm, checked_values, invoice_healthcare_services){ + if(invoice_healthcare_services){ + frappe.call({ + doc: frm.doc, + method: "set_healthcare_services", + args:{ + checked_values: checked_values + }, + callback: function() { + frm.trigger("validate"); + frm.refresh_fields(); + } + }); + } + else{ + for(let i=0; i 1){ + frappe.model.set_value(si_item.doctype, si_item.name, 'qty', parseFloat(checked_values[i]['qty'])); + } + } + frm.refresh_fields(); + } +}; diff --git a/erpnext/healthcare/utils.py b/erpnext/healthcare/utils.py index 0b300b0cd0..b9667214d9 100644 --- a/erpnext/healthcare/utils.py +++ b/erpnext/healthcare/utils.py @@ -313,3 +313,19 @@ def manage_doc_for_appoitnment(dt_from_appointment, appointment, invoiced): ) if dn_from_appointment: frappe.db.set_value(dt_from_appointment, dn_from_appointment, "invoiced", invoiced) + +@frappe.whitelist() +def get_drugs_to_invoice(encounter): + encounter = frappe.get_doc("Patient Encounter", encounter) + if encounter: + patient = frappe.get_doc("Patient", encounter.patient) + if patient and patient.customer: + item_to_invoice = [] + for drug_line in encounter.drug_prescription: + if drug_line.drug_code: + qty = 1 + if frappe.db.get_value("Item", drug_line.drug_code, "stock_uom") == "Nos": + qty = drug_line.get_quantity() + item_to_invoice.append({'drug_code': drug_line.drug_code, 'quantity': qty, + 'description': drug_line.dosage+" for "+drug_line.period}) + return item_to_invoice