Skip to content

Commit

Permalink
fix(Sales Invoice): get user confirmation to link customer with patient
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajinsr committed Jun 5, 2024
1 parent ca66fed commit 3242d3e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 66 deletions.
126 changes: 67 additions & 59 deletions healthcare/healthcare/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 += " <b><a href='/app/Form/Patient/{0}'>{0}</a></b>".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 <b>{0}</b> is not linked to a Customer <b><a href='/app/Form/Patient/{1}'>{1}</a></b>"
).format(patient.patient_name, patient.name)

if message:
frappe.msgprint(message, alert=True)


def get_appointments_to_invoice(patient, company):
Expand Down Expand Up @@ -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()
Expand Down
43 changes: 36 additions & 7 deletions healthcare/public/js/sales_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?";

This comment has been minimized.

Copy link
@ernestoruiz89

ernestoruiz89 Jun 6, 2024

Contributor

This text must be in function?: _("text" )

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'));
}
},
Expand Down Expand Up @@ -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({
Expand All @@ -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' }
]
});
Expand All @@ -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);
}
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 3242d3e

Please sign in to comment.