frappe.provide('frappe.patient_history'); frappe.pages['patient_history'].on_page_load = function(wrapper) { let me = this; let page = frappe.ui.make_app_page({ parent: wrapper, title: 'Patient History', single_column: true }); frappe.breadcrumbs.add('Healthcare'); let pid = ''; page.main.html(frappe.render_template('patient_history', {})); page.main.find('.header-separator').hide(); let patient = frappe.ui.form.make_control({ parent: page.main.find('.patient'), df: { fieldtype: 'Link', options: 'Patient', fieldname: 'patient', placeholder: __('Select Patient'), only_select: true, change: function() { let patient_id = patient.get_value(); if (pid != patient_id && patient_id) { me.start = 0; me.page.main.find('.patient_documents_list').html(''); setup_filters(patient_id, me); get_documents(patient_id, me); show_patient_info(patient_id, me); show_patient_vital_charts(patient_id, me, 'bp', 'mmHg', 'Blood Pressure'); } pid = patient_id; } }, }); patient.refresh(); if (frappe.route_options) { patient.set_value(frappe.route_options.patient); } this.page.main.on('click', '.btn-show-chart', function() { let btn_show_id = $(this).attr('data-show-chart-id'), pts = $(this).attr('data-pts'); let title = $(this).attr('data-title'); show_patient_vital_charts(patient.get_value(), me, btn_show_id, pts, title); }); this.page.main.on('click', '.btn-more', function() { let doctype = $(this).attr('data-doctype'), docname = $(this).attr('data-docname'); if (me.page.main.find('.'+docname).parent().find('.document-html').attr('data-fetched') == '1') { me.page.main.find('.'+docname).hide(); me.page.main.find('.'+docname).parent().find('.document-html').show(); } else { if (doctype && docname) { let exclude = ['patient', 'patient_name', 'patient_sex', 'encounter_date']; frappe.call({ method: 'erpnext.healthcare.utils.render_doc_as_html', args:{ doctype: doctype, docname: docname, exclude_fields: exclude }, freeze: true, callback: function(r) { if (r.message) { me.page.main.find('.' + docname).hide(); me.page.main.find('.' + docname).parent().find('.document-html').html( `${r.message.html}
`); me.page.main.find('.' + docname).parent().find('.document-html').show(); me.page.main.find('.' + docname).parent().find('.document-html').attr('data-fetched', '1'); } } }); } } }); this.page.main.on('click', '.btn-less', function() { let docname = $(this).attr('data-docname'); me.page.main.find('.' + docname).parent().find('.document-id').show(); me.page.main.find('.' + docname).parent().find('.document-html').hide(); }); me.start = 0; me.page.main.on('click', '.btn-get-records', function() { get_documents(patient.get_value(), me); }); }; let setup_filters = function(patient, me) { $('.doctype-filter').empty(); frappe.xcall( 'erpnext.healthcare.page.patient_history.patient_history.get_patient_history_doctypes' ).then(document_types => { let doctype_filter = frappe.ui.form.make_control({ parent: $('.doctype-filter'), df: { fieldtype: 'MultiSelectList', fieldname: 'document_type', placeholder: __('Select Document Type'), input_class: 'input-xs', change: () => { me.start = 0; me.page.main.find('.patient_documents_list').html(''); get_documents(patient, me, doctype_filter.get_value(), date_range_field.get_value()); }, get_data: () => { return document_types.map(document_type => { return { description: document_type, value: document_type }; }); }, } }); doctype_filter.refresh(); $('.date-filter').empty(); let date_range_field = frappe.ui.form.make_control({ df: { fieldtype: 'DateRange', fieldname: 'date_range', placeholder: __('Date Range'), input_class: 'input-xs', change: () => { let selected_date_range = date_range_field.get_value(); if (selected_date_range && selected_date_range.length === 2) { me.start = 0; me.page.main.find('.patient_documents_list').html(''); get_documents(patient, me, doctype_filter.get_value(), selected_date_range); } } }, parent: $('.date-filter') }); date_range_field.refresh(); }); }; let get_documents = function(patient, me, document_types="", selected_date_range="") { let filters = { name: patient, start: me.start, page_length: 20 }; if (document_types) filters['document_types'] = document_types; if (selected_date_range) filters['date_range'] = selected_date_range; frappe.call({ 'method': 'erpnext.healthcare.page.patient_history.patient_history.get_feed', args: filters, callback: function(r) { let data = r.message; if (data.length) { add_to_records(me, data); } else { me.page.main.find('.patient_documents_list').append(`