frappe.provide('frappe.patient_history');
frappe.pages['patient_history'].on_page_load = function(wrapper) {
frappe.ui.make_app_page({
parent: wrapper,
title: __('Patient History')
});
let patient_history = new PatientHistory(wrapper);
$(wrapper).bind('show', ()=> {
patient_history.show();
});
};
class PatientHistory {
constructor(wrapper) {
this.wrapper = $(wrapper);
this.page = wrapper.page;
this.sidebar = this.wrapper.find('.layout-side-section');
this.main_section = this.wrapper.find('.layout-main-section');
this.start = 0;
}
show() {
frappe.breadcrumbs.add('Healthcare');
this.sidebar.empty();
let me = this;
let patient = frappe.ui.form.make_control({
parent: me.sidebar,
df: {
fieldtype: 'Link',
options: 'Patient',
fieldname: 'patient',
placeholder: __('Select Patient'),
only_select: true,
change: () => {
me.patient_id = '';
if (me.patient_id != patient.get_value() && patient.get_value()) {
me.start = 0;
me.patient_id = patient.get_value();
me.make_patient_profile();
}
}
}
});
patient.refresh();
if (frappe.route_options && !this.patient_id) {
patient.set_value(frappe.route_options.patient);
this.patient_id = frappe.route_options.patient;
}
this.sidebar.find('[data-fieldname="patient"]').append('
');
}
make_patient_profile() {
this.page.set_title(__('Patient History'));
this.main_section.empty().append(frappe.render_template('patient_history'));
this.setup_filters();
this.setup_documents();
this.show_patient_info();
this.setup_buttons();
this.show_patient_vital_charts('bp', 'mmHg', 'Blood Pressure');
}
setup_filters() {
$('.doctype-filter').empty();
let me = this;
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'),
change: () => {
me.start = 0;
me.page.main.find('.patient_documents_list').html('');
this.setup_documents(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('');
this.setup_documents(doctype_filter.get_value(), date_range_field.get_value());
}
}
},
parent: $('.date-filter')
});
date_range_field.refresh();
});
}
setup_documents(document_types="", selected_date_range="") {
let filters = {
name: this.patient_id,
start: this.start,
page_length: 20
};
if (document_types)
filters['document_types'] = document_types;
if (selected_date_range)
filters['date_range'] = selected_date_range;
let me = this;
frappe.call({
'method': 'erpnext.healthcare.page.patient_history.patient_history.get_feed',
args: filters,
callback: function(r) {
let data = r.message;
if (data.length) {
me.add_to_records(data);
} else {
me.page.main.find('.patient_documents_list').append(`
${__('No more records..')}
`);
me.page.main.find('.btn-get-records').hide();
}
}
});
}
add_to_records(data) {
let details = "";
let i;
for (i=0; i" + data[i].subject;
}
data[i] = this.add_date_separator(data[i]);
if (frappe.user_info(data[i].owner).image) {
data[i].imgsrc = frappe.utils.get_file_link(frappe.user_info(data[i].owner).image);
} else {
data[i].imgsrc = false;
}
let time_line_heading = data[i].practitioner ? `${data[i].practitioner} ` : ``;
time_line_heading += data[i].reference_doctype + " - " +
`
${data[i].reference_name}
`;
details += `
`;
}
}
this.page.main.find('.patient_documents_list').append(details);
this.start += data.length;
if (data.length === 20) {
this.page.main.find(".btn-get-records").show();
} else {
this.page.main.find(".btn-get-records").hide();
this.page.main.find(".patient_documents_list").append(`
${__('No more records..')}
`);
}
}
add_date_separator(data) {
let date = frappe.datetime.str_to_obj(data.communication_date);
let pdate = '';
let diff = frappe.datetime.get_day_diff(frappe.datetime.get_today(),
frappe.datetime.obj_to_str(date));
if (diff < 1) {
pdate = __('Today');
} else if (diff < 2) {
pdate = __('Yesterday');
} else {
pdate = __('on {0}', [frappe.datetime.global_date_format(date)]);
}
data.date_sep = pdate;
return data;
}
show_patient_info() {
this.get_patient_info().then(() => {
$('.patient-info').empty().append(frappe.render_template('patient_history_sidebar', {
patient_image: this.patient.image,
patient_name: this.patient.patient_name,
patient_gender: this.patient.sex,
patient_mobile: this.patient.mobile
}));
this.show_patient_details();
});
}
show_patient_details() {
let me = this;
frappe.call({
'method': 'erpnext.healthcare.doctype.patient.patient.get_patient_detail',
args: {
patient: me.patient_id
},
callback: function(r) {
let data = r.message;
let details = ``;
if (data.occupation) details += `
${__('Occupation')} : ${data.occupation}`;
if (data.blood_group) details += `
${__('Blood Group')} : ${data.blood_group}`;
if (data.allergies) details += `
${__('Allerigies')} : ${data.allergies.replace("\n", ", ")}`;
if (data.medication) details += `
${__('Medication')} : ${data.medication.replace("\n", ", ")}`;
if (data.alcohol_current_use) details += `
${__('Alcohol use')} : ${data.alcohol_current_use}`;
if (data.alcohol_past_use) details += `
${__('Alcohol past use')} : ${data.alcohol_past_use}`;
if (data.tobacco_current_use) details += `
${__('Tobacco use')} : ${data.tobacco_current_use}`;
if (data.tobacco_past_use) details += `
${__('Tobacco past use')} : ${data.tobacco_past_use}`;
if (data.medical_history) details += `
${__('Medical history')} : ${data.medical_history.replace("\n", ", ")}`;
if (data.surgical_history) details += `
${__('Surgical history')} : ${data.surgical_history.replace("\n", ", ")}`;
if (data.surrounding_factors) details += `
${__('Occupational hazards')} : ${data.surrounding_factors.replace("\n", ", ")}`;
if (data.other_risk_factors) details += `
${__('Other risk factors')} : ${data.other_risk_factors.replace("\n", ", ")}`;
if (data.patient_details) details += `
${__('More info')} : ${data.patient_details.replace("\n", ", ")}`;
if (details) {
details = `` + details + `
`;
}
me.sidebar.find('.patient-details').html(details);
}
});
}
get_patient_info() {
return frappe.xcall('frappe.client.get', {
doctype: 'Patient',
name: this.patient_id,
}).then((patient) => {
if (patient) {
this.patient = patient;
}
});
}
setup_buttons() {
let me = this;
this.page.main.on("click", ".btn-show-chart", function() {
let btn_id = $(this).attr("data-show-chart-id"), scale_unit = $(this).attr("data-pts");
let title = $(this).attr("data-title");
me.show_patient_vital_charts(btn_id, scale_unit, 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', 'naming_series'];
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').attr('hidden', false);
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.page.main.on('click', '.btn-get-records', function() {
this.setup_documents();
});
}
show_patient_vital_charts(btn_id, scale_unit, title) {
let me = this;
frappe.call({
method: 'erpnext.healthcare.utils.get_patient_vitals',
args: {
patient: me.patient_id
},
callback: function(r) {
if (r.message) {
let show_chart_btns_html = `
`;
me.page.main.find('.show_chart_btns').html(show_chart_btns_html);
let data = r.message;
let labels = [], datasets = [];
let bp_systolic = [], bp_diastolic = [], temperature = [];
let pulse = [], respiratory_rate = [], bmi = [], height = [], weight = [];
for (let i=0; i (d + '').toUpperCase(),
formatTooltipY: d => d + ' ' + scale_unit,
}
});
me.page.main.find('.header-separator').show();
} else {
me.page.main.find('.patient_vital_charts').html('');
me.page.main.find('.show_chart_btns').html('');
me.page.main.find('.header-separator').hide();
}
}
});
}
}