From 3242d3ece10b58a71f08c6934ed8ba61c9a5a10b Mon Sep 17 00:00:00 2001 From: Sajin SR Date: Wed, 5 Jun 2024 08:26:37 +0530 Subject: [PATCH] fix(Sales Invoice): get user confirmation to link customer with patient --- healthcare/healthcare/utils.py | 126 ++++++++++++++------------ healthcare/public/js/sales_invoice.js | 43 +++++++-- 2 files changed, 103 insertions(+), 66 deletions(-) diff --git a/healthcare/healthcare/utils.py b/healthcare/healthcare/utils.py index 631aa76b22..67d43ac528 100644 --- a/healthcare/healthcare/utils.py +++ b/healthcare/healthcare/utils.py @@ -26,11 +26,10 @@ @frappe.whitelist() -def get_healthcare_services_to_invoice(patient, company): +def get_healthcare_services_to_invoice(patient, customer, company, link_customer=False): patient = frappe.get_doc("Patient", patient) items_to_invoice = [] if patient: - validate_customer_created(patient) # Customer validated, build a list of billable services items_to_invoice += get_appointments_to_invoice(patient, company) items_to_invoice += get_encounters_to_invoice(patient, company) @@ -41,14 +40,22 @@ def get_healthcare_services_to_invoice(patient, company): items_to_invoice += get_therapy_sessions_to_invoice(patient, company) items_to_invoice += get_service_requests_to_invoice(patient, company) items_to_invoice += get_observations_to_invoice(patient, company) + validate_customer_created(patient, customer, link_customer) return items_to_invoice -def validate_customer_created(patient): - if not frappe.db.get_value("Patient", patient.name, "customer"): - msg = _("Please set a Customer linked to the Patient") - msg += " {0}".format(patient.name) - frappe.throw(msg, title=_("Customer Not Found")) +def validate_customer_created(patient, customer, link_customer): + message = "" + if link_customer: + frappe.db.set_value("Patient", patient, "customer", customer) + message = _("Customer {0} has been linked to Patient").format(customer) + elif not frappe.db.get_value("Patient", patient.name, "customer"): + message = _( + "Patient {0} is not linked to a Customer {1}" + ).format(patient.patient_name, patient.name) + + if message: + frappe.msgprint(message, alert=True) def get_appointments_to_invoice(patient, company): @@ -666,62 +673,63 @@ def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced): @frappe.whitelist() -def get_drugs_to_invoice(encounter): +def get_drugs_to_invoice(encounter, customer, link_customer=False): encounter = frappe.get_doc("Patient Encounter", encounter) + if link_customer: + frappe.db.set_value("Patient", encounter.patient, "customer", customer) if encounter: patient = frappe.get_doc("Patient", encounter.patient) if patient: - if patient.customer: - orders_to_invoice = [] - medication_requests = frappe.get_list( - "Medication Request", - fields=["*"], - filters={ - "patient": patient.name, - "order_group": encounter.name, - "billing_status": ["in", ["Pending", "Partly Invoiced"]], - "docstatus": 1, - }, - ) - for medication_request in medication_requests: - if medication_request.medication: - is_billable = frappe.get_cached_value( - "Medication", medication_request.medication, ["is_billable"] - ) - else: - is_billable = frappe.db.exists( - "Item", {"name": medication_request.medication_item, "disabled": False} - ) - - description = "" - if medication_request.dosage and medication_request.period: - description = _("{0} for {1}").format(medication_request.dosage, medication_request.period) - - if medication_request.medication_item and is_billable: - billable_order_qty = medication_request.get("quantity", 1) - medication_request.get( - "qty_invoiced", 0 - ) - if medication_request.number_of_repeats_allowed: - if ( - medication_request.total_dispensable_quantity - >= medication_request.quantity + medication_request.qty_invoiced - ): - billable_order_qty = medication_request.get("quantity", 1) - else: - billable_order_qty = ( - medication_request.total_dispensable_quantity - medication_request.get("qty_invoiced", 0) - ) - - orders_to_invoice.append( - { - "reference_type": "Medication Request", - "reference_name": medication_request.name, - "drug_code": medication_request.medication_item, - "quantity": billable_order_qty, - "description": description, - } - ) - return orders_to_invoice + orders_to_invoice = [] + medication_requests = frappe.get_list( + "Medication Request", + fields=["*"], + filters={ + "patient": patient.name, + "order_group": encounter.name, + "billing_status": ["in", ["Pending", "Partly Invoiced"]], + "docstatus": 1, + }, + ) + for medication_request in medication_requests: + if medication_request.medication: + is_billable = frappe.get_cached_value( + "Medication", medication_request.medication, ["is_billable"] + ) + else: + is_billable = frappe.db.exists( + "Item", {"name": medication_request.medication_item, "disabled": False} + ) + + description = "" + if medication_request.dosage and medication_request.period: + description = _("{0} for {1}").format(medication_request.dosage, medication_request.period) + + if medication_request.medication_item and is_billable: + billable_order_qty = medication_request.get("quantity", 1) - medication_request.get( + "qty_invoiced", 0 + ) + if medication_request.number_of_repeats_allowed: + if ( + medication_request.total_dispensable_quantity + >= medication_request.quantity + medication_request.qty_invoiced + ): + billable_order_qty = medication_request.get("quantity", 1) + else: + billable_order_qty = ( + medication_request.total_dispensable_quantity - medication_request.get("qty_invoiced", 0) + ) + + orders_to_invoice.append( + { + "reference_type": "Medication Request", + "reference_name": medication_request.name, + "drug_code": medication_request.medication_item, + "quantity": billable_order_qty, + "description": description, + } + ) + return orders_to_invoice @frappe.whitelist() diff --git a/healthcare/public/js/sales_invoice.js b/healthcare/public/js/sales_invoice.js index a2f8d8f21e..61bfbd0826 100644 --- a/healthcare/public/js/sales_invoice.js +++ b/healthcare/public/js/sales_invoice.js @@ -3,10 +3,39 @@ frappe.ui.form.on('Sales Invoice', { refresh(frm) { if (frm.doc.docstatus === 0 && !frm.doc.is_return) { frm.add_custom_button(__('Healthcare Services'), function() { - get_healthcare_services_to_invoice(frm); + frappe.db.get_value("Patient", frm.doc.patient, "customer") + .then(r => { + let link_customer = null; + let msg = "Patient is not linked to a customer. Do you want to link the selected customer to the patient permanently?"; + if (r.message.customer){ + get_healthcare_services_to_invoice(frm, link_customer); + } else { + frappe.confirm(msg, + () => { + link_customer = true; + get_healthcare_services_to_invoice(frm, link_customer); + }, () => { + get_healthcare_services_to_invoice(frm, link_customer); + }) + } + }) },__('Get Items From')); frm.add_custom_button(__('Prescriptions'), function() { - get_drugs_to_invoice(frm); + frappe.db.get_value("Patient", frm.doc.patient, "customer") + .then(r => { + let link_customer = null; + if (r.message.customer){ + get_drugs_to_invoice(frm, link_customer); + } else { + frappe.confirm(msg, + () => { + link_customer = true; + get_drugs_to_invoice(frm, link_customer); + }, () => { + get_drugs_to_invoice(frm, link_customer); + }) + } + }) },__('Get Items From')); } }, @@ -53,7 +82,7 @@ var set_service_unit = function (frm) { } }; -var get_healthcare_services_to_invoice = function(frm) { +var get_healthcare_services_to_invoice = function(frm, link_customer) { var me = this; let selected_patient = ''; var dialog = new frappe.ui.Dialog({ @@ -66,7 +95,7 @@ var get_healthcare_services_to_invoice = function(frm) { fieldname: "patient", reqd: true }, - { fieldtype: 'Section Break' }, + { fieldtype: 'Section Break' }, { fieldtype: 'HTML', fieldname: 'results_area' } ] }); @@ -81,7 +110,7 @@ var get_healthcare_services_to_invoice = function(frm) { if(patient && patient!=selected_patient){ selected_patient = patient; var method = "healthcare.healthcare.utils.get_healthcare_services_to_invoice"; - var args = {patient: patient, company: frm.doc.company}; + var args = {patient: patient, customer: frm.doc.customer, company: frm.doc.company, link_customer: link_customer}; var columns = (["service", "reference_name", "reference_type"]); get_healthcare_items(frm, true, $results, $placeholder, method, args, columns); } @@ -213,7 +242,7 @@ var get_checked_values= function($results) { }).get(); }; -var get_drugs_to_invoice = function(frm) { +var get_drugs_to_invoice = function(frm, link_customer) { var me = this; let selected_encounter = ''; var dialog = new frappe.ui.Dialog({ @@ -248,7 +277,7 @@ var get_drugs_to_invoice = function(frm) { if(encounter && encounter!=selected_encounter){ selected_encounter = encounter; var method = "healthcare.healthcare.utils.get_drugs_to_invoice"; - var args = {encounter: encounter}; + var args = {encounter: encounter, customer: frm.doc.customer, link_customer: link_customer}; var columns = (["drug_code", "quantity", "description"]); get_healthcare_items(frm, false, $results, $placeholder, method, args, columns); }