refactor: Patient master

This commit is contained in:
Rucha Mahabal 2020-01-29 22:22:39 +05:30
parent a4202d1660
commit be29a24de5
6 changed files with 241 additions and 287 deletions

View File

@ -7,7 +7,7 @@
"engine": "InnoDB",
"field_order": [
"sb_op_settings",
"patient_master_name",
"patient_name_by",
"link_customer_to_patient",
"default_medical_code_standard",
"column_break_9",
@ -52,12 +52,6 @@
"fieldtype": "Section Break",
"label": "Out Patient Settings"
},
{
"fieldname": "patient_master_name",
"fieldtype": "Select",
"label": "Patient Name By",
"options": "Patient Name\nNaming Series"
},
{
"fieldname": "default_medical_code_standard",
"fieldtype": "Link",
@ -291,11 +285,17 @@
"fieldname": "create_sample_collection_for_lab_test",
"fieldtype": "Check",
"label": "Create Sample Collection document for Lab Test"
},
{
"fieldname": "patient_name_by",
"fieldtype": "Select",
"label": "Patient Name By",
"options": "Patient Name\nNaming Series"
}
],
"issingle": 1,
"links": [],
"modified": "2020-01-29 10:49:00.758580",
"modified": "2020-01-29 14:31:56.983534",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Healthcare Settings",

View File

@ -11,7 +11,7 @@ import json
class HealthcareSettings(Document):
def validate(self):
for key in ['collect_registration_fee', 'link_customer_to_patient', 'patient_master_name',
for key in ['collect_registration_fee', 'link_customer_to_patient', 'patient_name_by',
'lab_test_approval_required', 'create_sample_collection_for_lab_test', 'default_medical_code_standard']:
frappe.db.set_default(key, self.get(key, ""))
@ -46,12 +46,12 @@ def get_sms_text(doc):
return sms_text
def send_registration_sms(doc):
if frappe.db.get_value('Healthcare Settings', None, 'reg_sms'):
if frappe.db.get_single_value('Healthcare Settings', 'reg_sms'):
if doc.mobile:
context = {'doc': doc, 'alert': doc, 'comments': None}
if doc.get('_comments'):
context['comments'] = json.loads(doc.get('_comments'))
messages = frappe.db.get_value('Healthcare Settings', None, 'reg_msg')
messages = frappe.db.get_single_value('Healthcare Settings', 'reg_msg')
messages = frappe.render_template(messages, context)
number = [doc.mobile]
send_sms(number,messages)

View File

@ -3,128 +3,77 @@
frappe.ui.form.on('Patient', {
refresh: function (frm) {
frm.set_query("patient", "patient_relation", function () {
frm.set_query('patient', 'patient_relation', function () {
return {
filters: [
["Patient", "name", "!=", frm.doc.name]
['Patient', 'name', '!=', frm.doc.name]
]
};
});
if (frappe.defaults.get_default("patient_master_name") != "Naming Series") {
frm.toggle_display("naming_series", false);
if (frappe.defaults.get_default('patient_name_by') != 'Naming Series') {
frm.toggle_display('naming_series', false);
} else {
erpnext.toggle_naming_series();
}
if (frappe.defaults.get_default("collect_registration_fee") && frm.doc.status == 'Disabled') {
if (frappe.defaults.get_default('collect_registration_fee') && frm.doc.status == 'Disabled') {
frm.add_custom_button(__('Invoice Patient Registration'), function () {
btn_invoice_registration(frm);
invoice_registration(frm);
});
}
if (frm.doc.patient_name && frappe.user.has_role("Physician")) {
frm.add_custom_button(__('Patient History'), function () {
frappe.route_options = { "patient": frm.doc.name };
frappe.set_route("patient_history");
},"View");
if (frm.doc.patient_name && frappe.user.has_role('Physician')) {
frm.add_custom_button(__('Patient History'), function() {
frappe.route_options = {'patient': frm.doc.name};
frappe.set_route('patient_history');
},'View');
}
if (!frm.doc.__islocal && (frappe.user.has_role("Nursing User") || frappe.user.has_role("Physician"))) {
if (!frm.doc.__islocal && (frappe.user.has_role('Nursing User') || frappe.user.has_role('Physician'))) {
frm.add_custom_button(__('Vital Signs'), function () {
btn_create_vital_signs(frm);
}, "Create");
create_vital_signs(frm);
}, 'Create');
frm.add_custom_button(__('Medical Record'), function () {
create_medical_record(frm);
}, "Create");
}, 'Create');
frm.add_custom_button(__('Patient Encounter'), function () {
btn_create_encounter(frm);
}, "Create");
create_encounter(frm);
}, 'Create');
}
},
onload: function (frm) {
if(!frm.doc.dob){
$(frm.fields_dict['age_html'].wrapper).html("");
$(frm.fields_dict['age_html'].wrapper).html('');
}
if(frm.doc.dob){
$(frm.fields_dict['age_html'].wrapper).html("AGE : " + get_age(frm.doc.dob));
$(frm.fields_dict['age_html'].wrapper).html('AGE : ' + get_age(frm.doc.dob));
}
}
});
frappe.ui.form.on("Patient", "dob", function(frm) {
if(frm.doc.dob) {
var today = new Date();
var birthDate = new Date(frm.doc.dob);
if(today < birthDate){
frappe.msgprint(__("Please select a valid Date"));
frappe.model.set_value(frm.doctype,frm.docname, "dob", "");
frappe.ui.form.on('Patient', 'dob', function(frm) {
if (frm.doc.dob) {
let today = new Date();
let birthDate = new Date(frm.doc.dob);
if (today < birthDate){
frappe.msgprint(__('Please select a valid Date'));
frappe.model.set_value(frm.doctype,frm.docname, 'dob', '');
}
else{
var age_str = get_age(frm.doc.dob);
$(frm.fields_dict['age_html'].wrapper).html("AGE : " + age_str);
else {
let age_str = get_age(frm.doc.dob);
$(frm.fields_dict['age_html'].wrapper).html('AGE : ' + age_str);
}
}
else {
$(frm.fields_dict['age_html'].wrapper).html("");
$(frm.fields_dict['age_html'].wrapper).html('');
}
});
var create_medical_record = function (frm) {
frappe.route_options = {
"patient": frm.doc.name,
"status": "Open",
"reference_doctype": "Patient Medical Record",
"reference_owner": frm.doc.owner
};
frappe.new_doc("Patient Medical Record");
};
var get_age = function (birth) {
var ageMS = Date.parse(Date()) - Date.parse(birth);
var age = new Date();
age.setTime(ageMS);
var years = age.getFullYear() - 1970;
return years + " Year(s) " + age.getMonth() + " Month(s) " + age.getDate() + " Day(s)";
};
var btn_create_vital_signs = function (frm) {
if (!frm.doc.name) {
frappe.throw(__("Please save the patient first"));
}
frappe.route_options = {
"patient": frm.doc.name,
};
frappe.new_doc("Vital Signs");
};
var btn_create_encounter = function (frm) {
if (!frm.doc.name) {
frappe.throw(__("Please save the patient first"));
}
frappe.route_options = {
"patient": frm.doc.name,
};
frappe.new_doc("Patient Encounter");
};
var btn_invoice_registration = function (frm) {
frappe.call({
doc: frm.doc,
method: "invoice_patient_registration",
callback: function(data){
if(!data.exc){
if(data.message.invoice){
/* frappe.show_alert(__('Sales Invoice {0} created',
['<a href="#Form/Sales Invoice/'+data.message.invoice+'">' + data.message.invoice+ '</a>'])); */
frappe.set_route("Form", "Sales Invoice", data.message.invoice);
}
cur_frm.reload_doc();
}
}
});
};
frappe.ui.form.on('Patient Relation', {
patient_relation_add: function(frm){
frm.fields_dict['patient_relation'].grid.get_field('patient').get_query = function(doc){
var patient_list = [];
let patient_list = [];
if(!doc.__islocal) patient_list.push(doc.name);
$.each(doc.patient_relation, function(idx, val){
if (val.patient) patient_list.push(val.patient);
@ -133,3 +82,56 @@ frappe.ui.form.on('Patient Relation', {
};
}
});
let create_medical_record = function (frm) {
frappe.route_options = {
'patient': frm.doc.name,
'status': 'Open',
'reference_doctype': 'Patient Medical Record',
'reference_owner': frm.doc.owner
};
frappe.new_doc('Patient Medical Record');
};
let get_age = function (birth) {
let ageMS = Date.parse(Date()) - Date.parse(birth);
let age = new Date();
age.setTime(ageMS);
let years = age.getFullYear() - 1970;
return years + ' Year(s) ' + age.getMonth() + ' Month(s) ' + age.getDate() + ' Day(s)';
};
let create_vital_signs = function (frm) {
if (!frm.doc.name) {
frappe.throw(__('Please save the patient first'));
}
frappe.route_options = {
'patient': frm.doc.name,
};
frappe.new_doc('Vital Signs');
};
let create_encounter = function (frm) {
if (!frm.doc.name) {
frappe.throw(__('Please save the patient first'));
}
frappe.route_options = {
'patient': frm.doc.name,
};
frappe.new_doc('Patient Encounter');
};
let invoice_registration = function (frm) {
frappe.call({
doc: frm.doc,
method: 'invoice_patient_registration',
callback: function(data) {
if (!data.exc) {
if (data.message.invoice) {
frappe.set_route('Form', 'Sales Invoice', data.message.invoice);
}
cur_frm.reload_doc();
}
}
});
};

View File

@ -12,9 +12,10 @@
"engine": "InnoDB",
"field_order": [
"basic_info",
"inpatient_status",
"inpatient_record",
"naming_series",
"first_name",
"middle_name",
"last_name",
"patient_name",
"sex",
"blood_group",
@ -23,11 +24,17 @@
"image",
"column_break_14",
"status",
"inpatient_status",
"inpatient_record",
"customer",
"report_preference",
"mobile",
"email",
"phone",
"report_preference",
"personal_and_social_history",
"occupation",
"column_break_25",
"marital_status",
"sb_relation",
"patient_relation",
"allergy_medical_and_surgical_history",
@ -36,10 +43,6 @@
"column_break_20",
"medical_history",
"surgical_history",
"personal_and_social_history",
"occupation",
"column_break_25",
"marital_status",
"risk_factors",
"tobacco_past_use",
"tobacco_current_use",
@ -87,12 +90,11 @@
"bold": 1,
"fieldname": "patient_name",
"fieldtype": "Data",
"in_filter": 1,
"in_global_search": 1,
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Full Name",
"no_copy": 1,
"oldfieldtype": "Data",
"reqd": 1,
"read_only": 1,
"search_index": 1
},
{
@ -149,6 +151,7 @@
"fieldtype": "Column Break"
},
{
"description": "If \"Link Customer to Patient\" is checked in Healthcare Settings and an existing Customer is not selected then, a Customer will be created for this Patient for recording transactions in Accounts module.",
"fieldname": "customer",
"fieldtype": "Link",
"ignore_user_permissions": 1,
@ -263,27 +266,31 @@
},
{
"fieldname": "tobacco_past_use",
"fieldtype": "Data",
"fieldtype": "Select",
"ignore_xss_filter": 1,
"label": "Tobacco Past Use"
"label": "Tobacco Consumption (Past)",
"options": "\nYes\nNo"
},
{
"fieldname": "tobacco_current_use",
"fieldtype": "Data",
"fieldtype": "Select",
"ignore_xss_filter": 1,
"label": "Tobacco Current Use"
"label": "Tobacco Consumption (Present)",
"options": "\nYes\nNo"
},
{
"fieldname": "alcohol_past_use",
"fieldtype": "Data",
"fieldtype": "Select",
"ignore_xss_filter": 1,
"label": "Alcohol Past Use"
"label": "Alcohol Consumption (Past)",
"options": "\nYes\nNo"
},
{
"fieldname": "alcohol_current_use",
"fieldtype": "Data",
"fieldtype": "Select",
"ignore_user_permissions": 1,
"label": "Alcohol Current Use"
"label": "Alcohol Consumption (Present)",
"options": "\nYes\nNo"
},
{
"fieldname": "column_break_32",
@ -331,13 +338,31 @@
"label": "Default Currency",
"options": "Currency",
"print_hide": 1
},
{
"fieldname": "last_name",
"fieldtype": "Data",
"label": "Last Name",
"reqd": 1
},
{
"fieldname": "first_name",
"fieldtype": "Data",
"label": "First Name",
"oldfieldtype": "Data",
"reqd": 1
},
{
"fieldname": "middle_name",
"fieldtype": "Data",
"label": "Middle Name (optional)"
}
],
"icon": "fa fa-user",
"image_field": "image",
"links": [],
"max_attachments": 50,
"modified": "2020-01-29 11:22:40.698125",
"modified": "2020-01-29 13:56:10.174897",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Patient",

View File

@ -9,43 +9,49 @@ from frappe.model.document import Document
from frappe.utils import cint, cstr, getdate, flt
import dateutil
from frappe.model.naming import set_name_by_naming_series
from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account,get_income_account,send_registration_sms
from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account, get_income_account, send_registration_sms
class Patient(Document):
def validate(self):
self.set_full_name()
self.add_as_website_user()
def after_insert(self):
if(frappe.db.get_value("Healthcare Settings", None, "link_customer_to_patient") == '1' and not self.customer):
if frappe.db.get_value('Healthcare Settings', None, 'link_customer_to_patient') and not self.customer:
create_customer(self)
if frappe.db.get_single_value('Healthcare Settings', 'collect_registration_fee'):
frappe.db.set_value('Patient', self.name, 'status', 'Disabled')
else:
send_registration_sms(self)
self.add_as_website_user()
self.reload()
def on_update(self):
self.add_as_website_user()
def set_full_name(self):
self.patient_name = ' '.join(filter(None, [self.first_name, self.last_name]))
def add_as_website_user(self):
if(self.email):
if not frappe.db.exists ("User", self.email):
if self.email:
if not frappe.db.exists ('User', self.email):
user = frappe.get_doc({
"doctype": "User",
"first_name": self.patient_name,
"email": self.email,
"user_type": "Website User"
'doctype': 'User',
'first_name': self.first_name,
'last_name': self.last_name,
'email': self.email,
'user_type': 'Website User'
})
user.flags.ignore_permissions = True
user.add_roles("Patient")
user.add_roles('Patient')
def autoname(self):
patient_master_name = frappe.defaults.get_global_default('patient_master_name')
if patient_master_name == 'Patient Name':
patient_name_by = frappe.db.get_single_value('Healthcare Settings', 'patient_name_by')
if patient_name_by == 'Patient Name':
self.name = self.get_patient_name()
else:
set_name_by_naming_series(self)
def get_patient_name(self):
name = self.patient_name
if frappe.db.get_value("Patient", name):
if frappe.db.get_value('Patient', name):
count = frappe.db.sql("""select ifnull(MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)), 0) from tabPatient
where name like %s""", "%{0} - %".format(name), as_list=1)[0][0]
count = cint(count) + 1
@ -54,56 +60,59 @@ class Patient(Document):
return name
def get_age(self):
age_str = ""
age_str = ''
if self.dob:
born = getdate(self.dob)
age = dateutil.relativedelta.relativedelta(getdate(), born)
age_str = str(age.years) + " year(s) " + str(age.months) + " month(s) " + str(age.days) + " day(s)"
dob = getdate(self.dob)
age = dateutil.relativedelta.relativedelta(getdate(), dob)
age_str = str(age.years) + ' year(s) ' + str(age.months) + ' month(s) ' + str(age.days) + ' day(s)'
return age_str
def invoice_patient_registration(self):
frappe.db.set_value("Patient", self.name, "status", "Active")
send_registration_sms(self)
if(flt(frappe.get_value("Healthcare Settings", None, "registration_fee"))>0):
if frappe.db.get_single_value('Healthcare Settings', 'registration_fee'):
company = frappe.defaults.get_user_default('company')
if not company:
company = frappe.db.get_value("Global Defaults", None, "default_company")
company = frappe.db.get_single_value('Global Defaults', 'default_company')
sales_invoice = make_invoice(self.name, company)
sales_invoice.save(ignore_permissions=True)
frappe.db.set_value('Patient', self.name, 'status', 'Active')
send_registration_sms(self)
return {'invoice': sales_invoice.name}
def create_customer(doc):
customer_group = frappe.get_value("Selling Settings", None, "customer_group")
territory = frappe.get_value("Selling Settings", None, "territory")
customer_group = frappe.db.get_single_value('Selling Settings', 'customer_group')
territory = frappe.db.get_single_value('Selling Settings', 'territory')
if not (customer_group and territory):
customer_group = "Commercial"
territory = "Rest Of The World"
frappe.msgprint(_("Please set default customer group and territory in Selling Settings"), alert=True)
customer = frappe.get_doc({"doctype": "Customer",
"customer_name": doc.patient_name,
"customer_group": customer_group,
"territory" : territory,
"customer_type": "Individual"
customer_group = 'All Customer Groups'
territory = 'All Territories'
frappe.msgprint(_('Please set default customer group and territory in Selling Settings'), alert=True)
customer = frappe.get_doc({
'doctype': 'Customer',
'customer_name': doc.patient_name,
'customer_group': customer_group,
'territory' : territory,
'customer_type': 'Individual'
}).insert(ignore_permissions=True)
frappe.db.set_value("Patient", doc.name, "customer", customer.name)
frappe.msgprint(_("Customer {0} is created.").format(customer.name), alert=True)
frappe.db.set_value('Patient', doc.name, 'customer', customer.name)
frappe.msgprint(_('Customer {0} is created.').format(customer.name), alert=True)
def make_invoice(patient, company):
sales_invoice = frappe.new_doc("Sales Invoice")
sales_invoice.customer = frappe.get_value("Patient", patient, "customer")
sales_invoice = frappe.new_doc('Sales Invoice')
sales_invoice.customer = frappe.get_value('Patient', patient, 'customer')
sales_invoice.due_date = getdate()
sales_invoice.company = company
sales_invoice.is_pos = '0'
sales_invoice.is_pos = 0
sales_invoice.debit_to = get_receivable_account(company)
item_line = sales_invoice.append("items")
item_line.item_name = "Registeration Fee"
item_line.description = "Registeration Fee"
item_line = sales_invoice.append('items')
item_line.item_name = 'Registeration Fee'
item_line.description = 'Registeration Fee'
item_line.qty = 1
item_line.uom = "Nos"
item_line.uom = 'Nos'
item_line.conversion_factor = 1
item_line.income_account = get_income_account(None, company)
item_line.rate = frappe.get_value("Healthcare Settings", None, "registration_fee")
item_line.rate = frappe.db.get_single_value('Healthcare Settings', 'registration_fee')
item_line.amount = item_line.rate
sales_invoice.set_missing_values()
return sales_invoice
@ -112,7 +121,7 @@ def make_invoice(patient, company):
def get_patient_detail(patient):
patient_dict = frappe.db.sql("""select * from tabPatient where name=%s""", (patient), as_dict=1)
if not patient_dict:
frappe.throw(_("Patient not found"))
frappe.throw(_('Patient not found'))
vital_sign = frappe.db.sql("""select * from `tabVital Signs` where patient=%s
order by signs_date desc limit 1""", (patient), as_dict=1)

View File

@ -1,134 +1,52 @@
{
"allow_copy": 1,
"allow_guest_to_view": 0,
"allow_import": 0,
"allow_rename": 0,
"beta": 1,
"creation": "2017-04-26 15:40:11.561855",
"custom": 0,
"docstatus": 0,
"doctype": "DocType",
"document_type": "",
"editable_grid": 1,
"engine": "InnoDB",
"actions": [],
"allow_copy": 1,
"beta": 1,
"creation": "2017-04-26 15:40:11.561855",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"patient",
"relation",
"description"
],
"fields": [
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "relation",
"fieldtype": "Select",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 1,
"in_standard_filter": 0,
"label": "Relation",
"length": 0,
"no_copy": 0,
"options": "\nFather\nMother\nSpouse\nSiblings\nFamily",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 1,
"set_only_once": 0,
"unique": 0
},
"fieldname": "relation",
"fieldtype": "Select",
"in_list_view": 1,
"label": "Relation",
"options": "\nFather\nMother\nSpouse\nSiblings\nFamily\nOther",
"search_index": 1
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "patient",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 1,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 1,
"in_standard_filter": 0,
"label": "Patient",
"length": 0,
"no_copy": 0,
"options": "Patient",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
"fieldname": "patient",
"fieldtype": "Link",
"ignore_user_permissions": 1,
"in_list_view": 1,
"label": "Patient",
"options": "Patient",
"reqd": 1
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "description",
"fieldtype": "Data",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 1,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Description",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
"fieldname": "description",
"fieldtype": "Small Text",
"ignore_xss_filter": 1,
"label": "Description"
}
],
"has_web_view": 0,
"hide_heading": 0,
"hide_toolbar": 0,
"idx": 0,
"image_view": 0,
"in_create": 0,
"is_submittable": 0,
"issingle": 0,
"istable": 1,
"max_attachments": 0,
"modified": "2017-10-04 16:12:45.485333",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Patient Relation",
"name_case": "",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,
"read_only": 0,
"read_only_onload": 0,
"restrict_to_domain": "Healthcare",
"show_name_in_global_search": 0,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1,
"track_seen": 0
],
"istable": 1,
"links": [],
"modified": "2020-01-29 12:45:40.081899",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Patient Relation",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,
"restrict_to_domain": "Healthcare",
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}