fix: code clean up, performance improvements

This commit is contained in:
Rucha Mahabal 2020-04-02 18:45:53 +05:30
parent 9f6bf71b95
commit ced978e192
10 changed files with 147 additions and 101 deletions

View File

@ -38,14 +38,13 @@ class ClinicalProcedureTemplate(Document):
if self.rate:
item_price = frappe.get_doc('Item Price', {'item_code': self.item})
item_price.item_name = self.template
item_price.price_list_name = self.rate
item_price.price_list_rate = self.rate
item_price.save()
elif not self.is_billable and self.item:
frappe.db.set_value('Item', self.item, 'disabled', 1)
frappe.db.set_value(self.doctype, self.name, 'change_in_item', 0)
self.reload()
self.db_set('change_in_item', 0)
@frappe.whitelist()
@ -94,12 +93,10 @@ def create_item_from_template(doc):
'is_pro_applicable': 0,
'disabled': disabled,
'stock_uom': 'Unit'
}).insert(ignore_permissions=True)
}).insert(ignore_permissions=True, ignore_mandatory=True)
make_item_price(item.name, doc.rate)
frappe.db.set_value('Clinical Procedure Template', doc.name, 'item', item.name)
doc.reload()
doc.db_set('item', item.name)
def make_item_price(item, item_price):
price_list_name = frappe.db.get_value('Price List', {'selling': 1})
@ -108,7 +105,7 @@ def make_item_price(item, item_price):
'price_list': price_list_name,
'item_code': item,
'price_list_rate': item_price
}).insert(ignore_permissions=True)
}).insert(ignore_permissions=True, ignore_mandatory=True)
@frappe.whitelist()
def change_item_code_from_template(item_code, doc):

View File

@ -70,7 +70,7 @@ def create_item(doc):
'is_pro_applicable': 0,
'disabled': 0,
'stock_uom': doc.uom
}).insert(ignore_permissions=True)
}).insert(ignore_permissions=True, ignore_mandatory=True)
# insert item price
# get item price list to insert item price
@ -83,10 +83,9 @@ def create_item(doc):
item.standard_rate = 0.0
item.save(ignore_permissions=True)
# Set item in the doc
frappe.db.set_value('Healthcare Service Unit Type', doc.name, 'item', item.name)
doc.reload()
# Set item in the doc
doc.db_set('item', item.name)
def make_item_price(item, price_list_name, item_price):
frappe.get_doc({
@ -94,7 +93,7 @@ def make_item_price(item, price_list_name, item_price):
'price_list': price_list_name,
'item_code': item,
'price_list_rate': item_price
}).insert(ignore_permissions=True)
}).insert(ignore_permissions=True, ignore_mandatory=True)
def update_item(doc):
item = frappe.get_doc("Item", doc.item)
@ -106,7 +105,7 @@ def update_item(doc):
"standard_rate": doc.rate,
"description": doc.description
})
item.save()
item.db_update()
@frappe.whitelist()
def change_item_code(item, item_code, doc_name):

View File

@ -202,8 +202,11 @@ def create_sample_doc(template, patient, invoice):
# update Sample Collection by adding quantity
sample_collection = frappe.get_doc("Sample Collection", sample_exists[0][0])
quantity = int(sample_collection.sample_qty) + int(template.sample_qty)
if(template.sample_details):
sample_details = sample_collection.sample_details+"\n==============\n"+"Test :" + (template.get("lab_test_name") or template.get("template")) +"\n"+"Collection Detials:\n\t"+template.sample_details
if template.sample_details:
sample_details = sample_collection.sample_details + "\n==============\n" + _("Test: ")
sample_details += (template.get("lab_test_name") or template.get("template")) + "\n"
sample_details += _("Collection Details: ") + "\n\t" + template.sample_details
frappe.db.set_value("Sample Collection", sample_collection.name, "sample_details", sample_details)
frappe.db.set_value("Sample Collection", sample_collection.name, "sample_qty", quantity)

View File

@ -9,6 +9,7 @@ from frappe.model.document import Document
from frappe.utils import cint, cstr, getdate
import dateutil
from frappe.model.naming import set_name_by_naming_series
from frappe.utils.nestedset import get_root_of
from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account, get_income_account, send_registration_sms
class Patient(Document):
@ -17,14 +18,14 @@ class Patient(Document):
self.add_as_website_user()
def after_insert(self):
if frappe.db.get_value('Healthcare Settings', None, 'link_customer_to_patient') and not self.customer:
self.add_as_website_user()
self.reload()
if frappe.db.get_single_value('Healthcare Settings', '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 set_full_name(self):
if self.last_name:
@ -88,22 +89,25 @@ def create_customer(doc):
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 = 'All Customer Groups'
territory = 'All Territories'
customer_group = get_root_of('Customer Group')
territory = get_root_of('Territory')
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)
}).insert(ignore_permissions=True, ignore_mandatory=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):
uom = frappe.db.exists('UOM', 'Nos') or frappe.db.get_single_value('Stock Settings', 'stock_uom')
sales_invoice = frappe.new_doc('Sales Invoice')
sales_invoice.customer = frappe.get_value('Patient', patient, 'customer')
sales_invoice.customer = frappe.db.get_value('Patient', patient, 'customer')
sales_invoice.due_date = getdate()
sales_invoice.company = company
sales_invoice.is_pos = 0
@ -113,7 +117,7 @@ def make_invoice(patient, company):
item_line.item_name = 'Registeration Fee'
item_line.description = 'Registeration Fee'
item_line.qty = 1
item_line.uom = 'Nos'
item_line.uom = uom
item_line.conversion_factor = 1
item_line.income_account = get_income_account(None, company)
item_line.rate = frappe.db.get_single_value('Healthcare Settings', 'registration_fee')

View File

@ -135,10 +135,10 @@ frappe.ui.form.on('Patient Appointment', {
if (data.message.fee_validity) {
// if fee validity exists and automated appointment invoicing is enabled,
// show payment fields as non-mandatory
frm.set_df_property('mode_of_payment', 'hidden', 0);
frm.set_df_property('paid_amount', 'hidden', 0);
frm.set_df_property('mode_of_payment', 'reqd', 0);
frm.set_df_property('paid_amount', 'reqd', 0);
frm.toggle_display('mode_of_payment', 0);
frm.toggle_display('paid_amount', 0);
frm.toggle_reqd('mode_of_payment', 0);
frm.toggle_reqd('paid_amount', 0);
} else {
// if automated appointment invoicing is disabled, hide fields
frm.toggle_display('mode_of_payment', data.message ? 1 : 0);
@ -319,7 +319,7 @@ let check_and_set_availability = function(frm) {
freeze_message: __('Fetching records......')
});
} else {
fd.available_slots.html('Appointment date and Healthcare Practitioner are Mandatory'.bold());
fd.available_slots.html(__('Appointment date and Healthcare Practitioner are Mandatory').bold());
}
}
};
@ -387,7 +387,7 @@ let show_procedure_templates = function(frm, result){
});
});
if (!result) {
let msg = 'There are no procedure prescribed for '+frm.doc.patient;
let msg = __('There are no procedure prescribed for ') + frm.doc.patient;
$(repl('<div class="col-xs-12" style="padding-top:20px;" >%(msg)s</div></div>', {msg: msg})).appendTo(html_field);
}
d.show();

View File

@ -122,7 +122,7 @@ def invoice_appointment(appointment_doc):
sales_invoice.customer = frappe.get_value('Patient', appointment_doc.patient, 'customer')
sales_invoice.appointment = appointment_doc.name
sales_invoice.due_date = getdate()
sales_invoice.is_pos = True
sales_invoice.is_pos = 1
sales_invoice.company = appointment_doc.company
sales_invoice.debit_to = get_receivable_account(appointment_doc.company)
@ -134,7 +134,7 @@ def invoice_appointment(appointment_doc):
payment.amount = appointment_doc.paid_amount
sales_invoice.set_missing_values(for_validate=True)
sales_invoice.save(ignore_permissions=True)
sales_invoice.save(ignore_permissions=True, ignore_mandatory=True)
sales_invoice.submit()
frappe.msgprint(_('Sales Invoice {0} created as paid'.format(sales_invoice.name)), alert=True)
frappe.db.set_value('Patient Appointment', appointment_doc.name, 'invoiced', 1)
@ -157,6 +157,7 @@ def get_appointment_item(appointment_doc, item):
item.item_code = service_item
item.description = _('Consulting Charges: {0}').format(appointment_doc.practitioner)
item.income_account = get_income_account(appointment_doc.practitioner, appointment_doc.company)
item.cost_center = frappe.get_cached_value('Company', appointment_doc.company, 'cost_center')
item.rate = practitioner_charge
item.amount = practitioner_charge
item.qty = 1
@ -190,20 +191,14 @@ def cancel_sales_invoice(sales_invoice):
return False
def check_si_item_exists(appointment):
return frappe.db.exists(
'Sales Invoice Item',
{
def check_sales_invoice_exists(appointment):
sales_invoice = frappe.db.get_value('Sales Invoice Item', {
'reference_dt': 'Patient Appointment',
'reference_dn': appointment.name
}
)
}, 'parent')
def check_sales_invoice_exists(appointment):
si_item = check_si_item_exists(appointment)
if si_item:
sales_invoice = frappe.get_doc('Sales Invoice', frappe.db.get_value('Sales Invoice Item', si_item, 'parent'))
if sales_invoice:
sales_invoice = frappe.get_doc('Sales Invoice', sales_invoice)
return sales_invoice
return False

View File

@ -106,22 +106,29 @@ frappe.ui.form.on('Patient Encounter', {
name: frm.doc.appointment
},
callback: function(data) {
frappe.model.set_value(frm.doctype, frm.docname, 'patient', data.message.patient);
frappe.model.set_value(frm.doctype, frm.docname, 'type', data.message.appointment_type);
frappe.model.set_value(frm.doctype, frm.docname, 'practitioner', data.message.practitioner);
frappe.model.set_value(frm.doctype, frm.docname, 'invoiced', data.message.invoiced);
let values = {
'patient':data.message.patient,
'type': data.message.appointment_type,
'practitioner': data.message.practitioner,
'invoiced': data.message.invoiced
};
frm.set_value(values);
}
});
}
else {
frappe.model.set_value(frm.doctype, frm.docname, 'patient', '');
frappe.model.set_value(frm.doctype, frm.docname, 'type', '');
frappe.model.set_value(frm.doctype, frm.docname, 'practitioner', '');
frappe.model.set_value(frm.doctype, frm.docname, 'invoiced', 0);
frappe.model.set_value(frm.doctype, frm.docname, 'patient_sex', '');
frappe.model.set_value(frm.doctype, frm.docname, 'patient_age', '');
frappe.model.set_value(frm.doctype, frm.docname, 'inpatient_record', '');
frappe.model.set_value(frm.doctype, frm.docname, 'inpatient_status', '');
let values = {
'patient': '',
'patient_name': '',
'type': '',
'practitioner': '',
'invoiced': 0,
'patient_sex': '',
'patient_age': '',
'inpatient_record': '',
'inpatient_status': ''
};
frm.set_value(values);
}
},
@ -164,7 +171,7 @@ let schedule_inpatient = function(frm) {
}
},
freeze: true,
freeze_message: 'Process Inpatient Scheduling'
freeze_message: __('Process Inpatient Scheduling')
});
};

View File

@ -6,6 +6,7 @@ from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.utils import cstr
from frappe import _
class PatientEncounter(Document):
def on_update(self):
@ -48,20 +49,20 @@ def delete_medical_record(encounter):
def set_subject_field(encounter):
subject = encounter.practitioner + '\n'
if encounter.symptoms:
subject += 'Symptoms: '+ cstr(encounter.symptoms) + '\n'
subject += _('Symptoms: ') + cstr(encounter.symptoms) + '\n'
else:
subject += 'No Symptoms \n'
subject += _('No Symptoms') + '\n'
if encounter.diagnosis:
subject += 'Diagnosis: '+ cstr(encounter.diagnosis) + '\n'
subject += _('Diagnosis: ') + cstr(encounter.diagnosis) + '\n'
else:
subject += 'No Diagnosis \n'
subject += _('No Diagnosis') + '\n'
if encounter.drug_prescription:
subject += '\nDrug(s) Prescribed.'
subject += '\n' + _('Drug(s) Prescribed.')
if encounter.lab_test_prescription:
subject += '\nTest(s) Prescribed.'
subject += '\n' + _('Test(s) Prescribed.')
if encounter.procedure_prescription:
subject += '\nProcedure(s) Prescribed.'
subject += '\n' + _('Procedure(s) Prescribed.')
return subject

View File

@ -6,6 +6,7 @@ from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.utils import cstr
from frappe import _
class VitalSigns(Document):
def on_submit(self):
@ -24,7 +25,7 @@ def insert_vital_signs_to_medical_record(doc):
medical_record.reference_doctype = 'Vital Signs'
medical_record.reference_name = doc.name
medical_record.reference_owner = doc.owner
medical_record.save(ignore_permissions=True)
medical_record.save(ignore_permissions=True, ignore_mandatory=True)
def delete_vital_signs_from_medical_record(doc):
medical_record = frappe.db.get_value('Patient Medical Record', {'reference_name': doc.name})
@ -34,16 +35,16 @@ def delete_vital_signs_from_medical_record(doc):
def set_subject_field(doc):
subject = ''
if(doc.temperature):
subject += 'Temperature: \n'+ cstr(doc.temperature)+'. '
subject += _('Temperature: ') + '\n'+ cstr(doc.temperature) + '. '
if(doc.pulse):
subject += 'Pulse: \n'+ cstr(doc.pulse)+'. '
subject += _('Pulse: ') + '\n' + cstr(doc.pulse) + '. '
if(doc.respiratory_rate):
subject += 'Respiratory Rate: \n'+ cstr(doc.respiratory_rate)+'. '
subject += _('Respiratory Rate: ') + '\n' + cstr(doc.respiratory_rate) + '. '
if(doc.bp):
subject += 'BP: \n'+ cstr(doc.bp)+'. '
subject += _('BP: ') + '\n' + cstr(doc.bp) + '. '
if(doc.bmi):
subject += 'BMI: \n'+ cstr(doc.bmi)+'. '
subject += _('BMI: ') + '\n' + cstr(doc.bmi) + '. '
if(doc.nutrition_note):
subject += 'Note: \n'+ cstr(doc.nutrition_note)+'. '
subject += _('Note: ') + '\n' + cstr(doc.nutrition_note) + '. '
return subject

View File

@ -109,22 +109,34 @@ def get_lab_tests_to_invoice(patient):
filters={'patient': patient.name, 'invoiced': False, 'docstatus': 1}
)
for lab_test in lab_tests:
if frappe.db.get_value('Lab Test Template', lab_test.template, 'is_billable'):
item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.lab_test_code, ['item', 'is_billable'])
if is_billable:
lab_tests_to_invoice.append({
'reference_type': 'Lab Test',
'reference_name': lab_test.name,
'service': frappe.db.get_value('Lab Test Template', lab_test.template, 'item')
'service': item
})
lab_prescriptions = frappe.db.sql('''select lp.name, lp.lab_test_code from `tabPatient Encounter` et, `tabLab Prescription` lp
where et.patient=%s and lp.parent=et.name and lp.lab_test_created=0 and lp.invoiced=0''', (patient.name), as_dict=1)
lab_prescriptions = frappe.db.sql(
'''
SELECT
lp.name, lp.lab_test_code
FROM
`tabPatient Encounter` et, `tabLab Prescription` lp
WHERE
et.patient=%s
and lp.parent=et.name
and lp.lab_test_created=0
and lp.invoiced=0
''', (patient.name), as_dict=1)
for prescription in lab_prescriptions:
if prescription.lab_test_code and frappe.db.get_value('Lab Test Template', prescription.lab_test_code, 'is_billable'):
item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable'])
if prescription.lab_test_code and is_billable:
lab_tests_to_invoice.append({
'reference_type': 'Lab Prescription',
'reference_name': prescription.name,
'service': frappe.db.get_value('Lab Test Template', prescription.lab_test_code, 'item')
'service': item
})
return lab_tests_to_invoice
@ -139,11 +151,12 @@ def get_clinical_procedures_to_invoice(patient):
)
for procedure in procedures:
if not procedure.appointment:
if procedure.procedure_template and frappe.db.get_value('Clinical Procedure Template', procedure.procedure_template, 'is_billable'):
item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable'])
if procedure.procedure_template and is_billable:
clinical_procedures_to_invoice.append({
'reference_type': 'Clinical Procedure',
'reference_name': procedure.name,
'service': frappe.db.get_value('Clinical Procedure Template', procedure.procedure_template, 'item')
'service': item
})
# consumables
@ -164,16 +177,27 @@ def get_clinical_procedures_to_invoice(patient):
'description': procedure.consumption_details
})
procedure_prescriptions = frappe.db.sql('''select pp.name, pp.procedure from `tabPatient Encounter` et,
`tabProcedure Prescription` pp where et.patient=%s and pp.parent=et.name and
pp.procedure_created=0 and pp.invoiced=0 and pp.appointment_booked=0''', (patient.name), as_dict=1)
procedure_prescriptions = frappe.db.sql(
'''
SELECT
pp.name, pp.procedure
FROM
`tabPatient Encounter` et, `tabProcedure Prescription` pp
WHERE
et.patient=%s
and pp.parent=et.name
and pp.procedure_created=0
and pp.invoiced=0
and pp.appointment_booked=0
''', (patient.name), as_dict=1)
for prescription in procedure_prescriptions:
if frappe.db.get_value('Clinical Procedure Template', prescription.procedure, 'is_billable'):
item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable'])
if is_billable:
clinical_procedures_to_invoice.append({
'reference_type': 'Procedure Prescription',
'reference_name': prescription.name,
'service': frappe.db.get_value('Clinical Procedure Template', prescription.procedure, 'item')
'service': item
})
return clinical_procedures_to_invoice
@ -181,13 +205,22 @@ def get_clinical_procedures_to_invoice(patient):
def get_inpatient_services_to_invoice(patient):
services_to_invoice = []
inpatient_services = frappe.db.sql('''select io.* from `tabInpatient Record` ip,
`tabInpatient Occupancy` io where ip.patient=%s and io.parent=ip.name and
io.left=1 and io.invoiced=0''', (patient.name), as_dict=1)
inpatient_services = frappe.db.sql(
'''
SELECT
io.*
FROM
`tabInpatient Record` ip, `tabInpatient Occupancy` io
WHERE
ip.patient=%s
and io.parent=ip.name
and io.left=1
and io.invoiced=0
''', (patient.name), as_dict=1)
for inpatient_occupancy in inpatient_services:
service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
service_unit_type = frappe.get_doc('Healthcare Service Unit Type', service_unit_type)
service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type)
if service_unit_type and service_unit_type.is_billable:
hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
qty = 0.5
@ -231,9 +264,9 @@ def get_service_item_and_practitioner_charge(doc):
def throw_config_service_item(is_inpatient):
service_item_label = 'Out Patient Consulting Charge Item'
service_item_label = _('Out Patient Consulting Charge Item')
if is_inpatient:
service_item_label = 'Inpatient Visit Charge Item'
service_item_label = _('Inpatient Visit Charge Item')
msg = _(('Please Configure {0} in ').format(service_item_label) \
+ '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>''')
@ -241,9 +274,9 @@ def throw_config_service_item(is_inpatient):
def throw_config_practitioner_charge(is_inpatient, practitioner):
charge_name = 'OP Consulting Charge'
charge_name = _('OP Consulting Charge')
if is_inpatient:
charge_name = 'Inpatient Visit Charge'
charge_name = _('Inpatient Visit Charge')
msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
+ ''' <b><a href='#Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
@ -425,11 +458,17 @@ def get_children(doctype, parent, company, is_root=False):
if each["expandable"] == 1:
occupied = False
vacant = False
child_list = frappe.db.sql("""
select name, occupancy_status from `tabHealthcare Service Unit`
where inpatient_occupancy = 1 and
lft > %s and rgt < %s""",
(each["lft"], each["rgt"]))
child_list = frappe.db.sql(
'''
SELECT
name, occupancy_status
FROM
`tabHealthcare Service Unit`
WHERE
inpatient_occupancy = 1
and lft > %s and rgt < %s
''', (each['lft'], each['rgt']))
for child in child_list:
if not occupied:
occupied = 0