Skip to content

Commit

Permalink
feat: fetch member card details from jubilee server
Browse files Browse the repository at this point in the history
  • Loading branch information
av-dev2 committed Apr 22, 2024
1 parent d623bd5 commit 446df5c
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 77 deletions.
79 changes: 79 additions & 0 deletions hms_tz/jubilee/api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import json
import frappe
import requests
from frappe import _
from time import sleep
from erpnext import get_default_company
from hms_tz.jubilee.api.token import get_jubilee_service_token
from hms_tz.jubilee.doctype.jubilee_response_log.jubilee_response_log import add_jubilee_log


@frappe.whitelist()
def get_member_card_detials(card_no, insurance_provider):
if not card_no or insurance_provider != "Jubilee":
return

company = get_default_company()
if not company:
company = frappe.defaults.get_user_default("Company")

if not company:
company = frappe.get_list(
"Company Insurance Setting", fields=["company"], filters={"enable": 1, "api_provider": insurance_provider}
)[0].company
if not company:
frappe.throw(_("No companies found to connect to Jubilee"))

token = get_jubilee_service_token(company, insurance_provider)

service_url = frappe.get_cached_value(
"Company Insurance Setting",
{"company": company, "api_provider": insurance_provider},
"service_url",
)
headers = {"Authorization": "Bearer " + token}
url = (
str(service_url) + f"/jubileeapi/Getcarddetails?MemberNo={str(card_no)}"
)
for i in range(3):
try:
r = requests.get(url, headers=headers, timeout=5)
r.raise_for_status()
frappe.logger().debug({"webhook_success": r.text})

if json.loads(r.text):
add_jubilee_log(
request_type="GetCardDetails",
request_url=url,
request_header=headers,
response_data=json.loads(r.text),
status_code=r.status_code,
ref_doctype="Patient",
)
card = json.loads(r.text)
frappe.msgprint(_(card["Status"]), alert=True)
# add_scheme(card.get("SchemeID"), card.get("SchemeName"))
# add_product(card.get("ProductCode"), card.get("ProductName"))
return card
else:
add_jubilee_log(
request_type="GetCardDetails",
request_url=url,
request_header=headers,
response_data=str(r),
status_code=r.status_code,
ref_doctype="Patient",
)
frappe.msgprint(json.loads(r.text))
frappe.msgprint(
_(
"Getting information from NHIF failed. Try again after sometime, or continue manually."
)
)
except Exception as e:
frappe.logger().debug({"webhook_error": e, "try": i + 1})
sleep(3 * i + 1)
if i != 2:
continue
else:
raise e
272 changes: 196 additions & 76 deletions hms_tz/nhif/api/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ frappe.ui.form.on('Patient', {
});
},
card_no: function (frm) {
frm.fields_dict.card_no.$input.focusout(function() {
frm.trigger('get_patient_info');
frm.fields_dict.card_no.$input.focusout(function () {
if (frm.doc.insurance_provider) {
frm.trigger('get_patient_info');
}
frm.set_df_property('card_no', 'read_only', 1);
});
},
insurance_provider: function (frm) {
if (frm.doc.card_no && frm.doc.insurance_provider) {
frm.trigger('get_patient_info');
}
},
mobile: function (frm) {
frappe.call({
method: 'hms_tz.nhif.api.patient.validate_mobile_number',
Expand Down Expand Up @@ -53,26 +60,86 @@ frappe.ui.form.on('Patient', {
}
});
if (exists) return;
frappe.call({
method: 'hms_tz.nhif.api.patient.get_patient_info',
args: {
'card_no': frm.doc.card_no,
},
freeze: true,
freeze_message: __("Please Wait..."),
callback: function (data) {
if (data.message) {
const card = data.message;
if (!frm.is_new()) {
const d = new frappe.ui.Dialog({
title: "Patient's information",
primary_action_label: 'Submit',
primary_action(values) {
update_patient_info(frm, card);
d.hide();
}

if (frm.doc.card_no && frm.doc.insurance_provider) {
if (frm.doc.insurance_provider == 'NHIF') {
get_nhif_patient_info(frm);
} else if (frm.doc.insurance_provider == "Jubilee") {
get_jubilee_patient_info(frm);
}
}
},
update_cash_limit: function (frm) {
if (frappe.user.has_role('Healthcare Administrator')) {
frm.add_custom_button(__('Update Cash Limit'), function () {
let d = new frappe.ui.Dialog({
title: 'Change Cash Limit',
fields: [
{
fieldname: 'current_cash_limit',
fieldtype: 'Currency',
label: __('Current Cash Limit'),
default: frm.doc.cash_limit,
reqd: true
},
{
fieldname: 'column_break_1',
fieldtype: 'Column Break'
},
{
fieldname: 'new_cash_limit',
fieldtype: 'Currency',
label: 'New Cash Limit',
reqd: true
}
],
});
d.set_primary_action(__('Submit'), function () {
if (d.get_value('new_cash_limit') == 0) {
frappe.msgprint({
title: 'Notification',
indicator: 'red',
message: __('<b>New cash limit cannot be zero</b>')
});
$(`<div class="modal-body ui-front">
} else {
frappe.call('hms_tz.nhif.api.patient.enqueue_update_cash_limit', {
old_cash_limit: d.get_value('current_cash_limit'),
new_cash_limit: d.get_value('new_cash_limit')
}).then(r => {
frappe.show_alert(__("Processing patient's cash limit"))
})
d.hide();
}

});
d.show();
}).removeClass('btn-default').addClass('btn-info font-weight-bold text-dark');
}
}
});

function get_nhif_patient_info(frm) {
frappe.call({
method: 'hms_tz.nhif.api.patient.get_patient_info',
args: {
'card_no': frm.doc.card_no,
'insurance_provider': frm.doc.insurance_provider
},
freeze: true,
freeze_message: __("Please Wait..."),
callback: function (data) {
if (data.message) {
const card = data.message;
if (!frm.is_new()) {
const d = new frappe.ui.Dialog({
title: "Patient's information",
primary_action_label: 'Submit',
primary_action(values) {
update_nhif_patient_info(frm, card);
d.hide();
}
});
$(`<div class="modal-body ui-front">
<table class="table table-bordered">
<thead>
<tr>
Expand Down Expand Up @@ -125,65 +192,17 @@ frappe.ui.form.on('Patient', {
</tbody>
</table>
</div>`).appendTo(d.body);
d.show();
}
else {
update_patient_info(frm, card);
}
d.show();
}
else {
update_nhif_patient_info(frm, card);
}
}
});
},
update_cash_limit: function(frm) {
if (frappe.user.has_role('Healthcare Administrator')) {
frm.add_custom_button(__('Update Cash Limit'), function () {
let d = new frappe.ui.Dialog({
title: 'Change Cash Limit',
fields: [
{
fieldname: 'current_cash_limit',
fieldtype: 'Currency',
label: __('Current Cash Limit'),
default: frm.doc.cash_limit,
reqd: true
},
{
fieldname: 'column_break_1',
fieldtype: 'Column Break'
},
{
fieldname: 'new_cash_limit',
fieldtype: 'Currency',
label: 'New Cash Limit',
reqd: true
}
],
});
d.set_primary_action(__('Submit'), function() {
if (d.get_value('new_cash_limit') == 0) {
frappe.msgprint({
title: 'Notification',
indicator: 'red',
message: __('<b>New cash limit cannot be zero</b>')
});
} else {
frappe.call('hms_tz.nhif.api.patient.enqueue_update_cash_limit', {
old_cash_limit: d.get_value('current_cash_limit'),
new_cash_limit: d.get_value('new_cash_limit')
}).then(r => {
frappe.show_alert(__("Processing patient's cash limit"))
})
d.hide();
}

});
d.show();
}).removeClass('btn-default').addClass('btn-info font-weight-bold text-dark');
}
}
});
});
}

function update_patient_info(frm, card) {
function update_nhif_patient_info(frm, card) {
frm.set_value("first_name", card.FirstName);
frm.set_value("middle_name", card.MiddleName);
frm.set_value("last_name", card.LastName);
Expand All @@ -200,3 +219,104 @@ function update_patient_info(frm, card) {
indicator: 'green'
}, 5);
}

function get_jubilee_patient_info(frm) {
frappe.call({
method: 'hms_tz.jubilee.api.api.get_member_card_detials',
args: {
'card_no': frm.doc.card_no,
'insurance_provider': frm.doc.insurance_provider
},
freeze: true,
freeze_message: __("Please Wait..."),
callback: function (data) {
if (data.message) {
const cardinfo = data.message["Description"];
if (!frm.is_new()) {
const d = new frappe.ui.Dialog({
title: "Patient's information",
primary_action_label: 'Submit',
primary_action(values) {
update_jubilee_patient_info(frm, cardinfo);
d.hide();
}
});
$(`<div class="modal-body ui-front">
<table class="table table-bordered">
<thead>
<tr>
<th>Field Name</th>
<th>Current Values</th>
<th>New Values</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td>${frm.doc.first_name}</td>
<td>${cardinfo.FirstName}</td>
</tr>
<tr>
<td>Last Name</td>
<td>${frm.doc.middle_name}</td>
<td>${cardinfo.MiddleName}</td>
</tr>
<tr>
<td>Last Name</td>
<td>${frm.doc.last_name}</td>
<td>${cardinfo.LastName}</td>
</tr>
<tr>
<td>Full Name</td>
<td>${frm.doc.patient_name}</td>
<td>${cardinfo.MemberName}</td>
</tr>
<tr>
<td>Mobile</td>
<td>${frm.doc.mobile}</td>
<td>${cardinfo.Phone}</td>
</tr>
<tr>
<td>Gender</td>
<td>${frm.doc.sex}</td>
<td>${cardinfo.Gender}</td>
</tr>
<tr>
<td>Date of birth</td>
<td>${frm.doc.dob}</td>
<td>${cardinfo.Dob.slice(0, 10)}</td>
</tr>
<tr>
<td>Membership No</td>
<td>${frm.doc.membership_no}</td>
<td>${cardinfo.PrincipleMemberNo}</td>
</tr>
</tbody>
</table>
</div>`).appendTo(d.body);
d.show();
}
else {
update_jubilee_patient_info(frm, cardinfo);
}
}
}
});
}

function update_jubilee_patient_info(frm, cardinfo) {
frm.set_value("first_name", cardinfo.FirstName);
frm.set_value("middle_name", cardinfo.MiddleName);
frm.set_value("last_name", cardinfo.LastName);
frm.set_value("patient_name", cardinfo.MemberName);
frm.set_value("sex", cardinfo.Gender);
frm.set_value("dob", cardinfo.Dob);
frm.set_value("nhif_employername", cardinfo.Company);
frm.set_value("membership_no", cardinfo.PrincipleMemberNo);
frm.set_value("mobile", cardinfo.Phone);
frm.save();
frappe.show_alert({
message: __("Patient's information is updated"),
indicator: 'green'
}, 5);
}
2 changes: 1 addition & 1 deletion hms_tz/patches/custom_fields/hms_tz_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ def execute():
},
{
"fieldtype": "Section Break",
"label": "NHIF Details",
"label": "Insurance Details",
"fieldname": "insurance_details",

},
Expand Down

0 comments on commit 446df5c

Please sign in to comment.