From 446df5c3d225903246da58c5cf03afbe75bf18b7 Mon Sep 17 00:00:00 2001 From: av-dev2 Date: Mon, 22 Apr 2024 16:39:15 +0300 Subject: [PATCH] feat: fetch member card details from jubilee server --- hms_tz/jubilee/api/api.py | 79 +++++ hms_tz/nhif/api/patient.js | 272 +++++++++++++----- .../custom_fields/hms_tz_custom_fields.py | 2 +- 3 files changed, 276 insertions(+), 77 deletions(-) create mode 100644 hms_tz/jubilee/api/api.py diff --git a/hms_tz/jubilee/api/api.py b/hms_tz/jubilee/api/api.py new file mode 100644 index 000000000..cb7505444 --- /dev/null +++ b/hms_tz/jubilee/api/api.py @@ -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 diff --git a/hms_tz/nhif/api/patient.js b/hms_tz/nhif/api/patient.js index 490dff701..20da49ec4 100644 --- a/hms_tz/nhif/api/patient.js +++ b/hms_tz/nhif/api/patient.js @@ -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', @@ -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: __('New cash limit cannot be zero') }); - $(`