fix: check fee validity in a single function

This commit is contained in:
Rucha Mahabal 2020-03-13 15:39:31 +05:30
parent 200cb34ecf
commit cd31996b81
3 changed files with 57 additions and 56 deletions

View File

@ -108,6 +108,14 @@ frappe.ui.form.on('Patient Appointment', {
}, __('Create')); }, __('Create'));
} }
frm.events.toggle_payment_fields(frm);
},
get_procedure_from_encounter: function(frm) {
get_prescribed_procedure(frm);
},
toggle_payment_fields: function(frm) {
frappe.db.get_value('Healthcare Settings', {name: 'Healthcare Settings'}, ['automate_appointment_invoicing'], (settings) => { frappe.db.get_value('Healthcare Settings', {name: 'Healthcare Settings'}, ['automate_appointment_invoicing'], (settings) => {
if (settings.automate_appointment_invoicing == 1) { if (settings.automate_appointment_invoicing == 1) {
frm.set_df_property('mode_of_payment', 'hidden', 0); frm.set_df_property('mode_of_payment', 'hidden', 0);
@ -121,10 +129,6 @@ frappe.ui.form.on('Patient Appointment', {
frm.set_df_property('paid_amount', 'reqd', 0); frm.set_df_property('paid_amount', 'reqd', 0);
} }
}); });
},
get_procedure_from_encounter: function(frm) {
get_prescribed_procedure(frm);
} }
}); });

View File

@ -13,7 +13,7 @@ import datetime
from frappe.core.doctype.sms_settings.sms_settings import send_sms from frappe.core.doctype.sms_settings.sms_settings import send_sms
from erpnext.hr.doctype.employee.employee import is_holiday from erpnext.hr.doctype.employee.employee import is_holiday
from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account, get_income_account from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account, get_income_account
from erpnext.healthcare.utils import check_validity_exists, get_service_item_and_practitioner_charge from erpnext.healthcare.utils import check_fee_validity, get_service_item_and_practitioner_charge
class PatientAppointment(Document): class PatientAppointment(Document):
def validate(self): def validate(self):
@ -25,7 +25,7 @@ class PatientAppointment(Document):
def after_insert(self): def after_insert(self):
invoice_appointment(self) invoice_appointment(self)
self.update_prescription_details() self.update_prescription_details()
self.check_fee_validity() self.update_fee_validity()
send_confirmation_msg(self) send_confirmation_msg(self)
def set_status(self): def set_status(self):
@ -59,8 +59,8 @@ class PatientAppointment(Document):
self.appointment_time, end_time.time(), self.appointment_time, end_time.time(), self.appointment_time)) self.appointment_time, end_time.time(), self.appointment_time, end_time.time(), self.appointment_time))
if overlaps: if overlaps:
overlapping_details = _('Appointment overlaps with {0}').format(overlaps[0][0]) overlapping_details = _('Appointment overlaps with ')
overlapping_details += '<br>' overlapping_details += "<b><a href='#Form/Patient Appointment/{0}'>{0}</a></b><br>".format(overlaps[0][0])
overlapping_details += _('{0} has appointment scheduled with {1} at {2} having {3} minute(s) duration.').format( overlapping_details += _('{0} has appointment scheduled with {1} at {2} having {3} minute(s) duration.').format(
overlaps[0][1], overlaps[0][2], overlaps[0][3], overlaps[0][4]) overlaps[0][1], overlaps[0][2], overlaps[0][3], overlaps[0][4])
frappe.throw(overlapping_details, title=_('Appointments Overlapping')) frappe.throw(overlapping_details, title=_('Appointments Overlapping'))
@ -83,27 +83,18 @@ class PatientAppointment(Document):
if comments: if comments:
frappe.db.set_value('Patient Appointment', self.name, 'notes', comments) frappe.db.set_value('Patient Appointment', self.name, 'notes', comments)
def check_fee_validity(self): def update_fee_validity(self):
# Check fee validity exists fee_validity = check_fee_validity(self)
validity = check_validity_exists(self.practitioner, self.patient) if fee_validity:
if validity: visited = fee_validity.visited + 1
fee_validity = frappe.get_doc('Fee Validity', validity) frappe.db.set_value('Fee Validity', fee_validity.name, 'visited', visited)
if fee_validity.ref_invoice:
# Check if the validity is valid frappe.db.set_value('Patient Appointment', self.name, 'invoiced', True)
appointment_date = getdate(self.appointment_date) frappe.db.set_value('Patient Appointment', self.name, 'ref_sales_invoice', fee_validity.ref_invoice)
if (fee_validity.valid_till >= appointment_date) and (fee_validity.visited < fee_validity.max_visits): frappe.msgprint(_('{0} has fee validity till {1}').format(self.patient, fee_validity.valid_till))
visited = fee_validity.visited + 1
frappe.db.set_value('Fee Validity', fee_validity.name, 'visited', visited)
if fee_validity.ref_invoice:
frappe.db.set_value('Patient Appointment', self.name, 'invoiced', True)
frappe.db.set_value('Patient Appointment', self.name, 'ref_sales_invoice', fee_validity.ref_invoice)
frappe.msgprint(_('{0} has fee validity till {1}').format(self.patient, fee_validity.valid_till))
def invoice_appointment(appointment_doc): def invoice_appointment(appointment_doc):
if not appointment_doc.name:
return
if frappe.db.get_single_value('Healthcare Settings', 'automate_appointment_invoicing') and \ if frappe.db.get_single_value('Healthcare Settings', 'automate_appointment_invoicing') and \
not frappe.db.get_value('Patient Appointment', appointment_doc.name, 'invoiced'): not frappe.db.get_value('Patient Appointment', appointment_doc.name, 'invoiced'):
sales_invoice = frappe.new_doc('Sales Invoice') sales_invoice = frappe.new_doc('Sales Invoice')
@ -153,7 +144,7 @@ def cancel_appointment(appointment_id):
_('Appointment {0} and Sales Invoice {1} cancelled'.format(appointment.name, sales_invoice.name)) _('Appointment {0} and Sales Invoice {1} cancelled'.format(appointment.name, sales_invoice.name))
) )
else: else:
validity = check_validity_exists(appointment.practitioner, appointment.patient) validity = check_fee_validity(appointment.practitioner, appointment.patient)
if validity: if validity:
fee_validity = frappe.get_doc('Fee Validity', validity) fee_validity = frappe.get_doc('Fee Validity', validity)
if validate_appointment_in_fee_validity(appointment, fee_validity.valid_till, fee_validity.ref_invoice): if validate_appointment_in_fee_validity(appointment, fee_validity.valid_till, fee_validity.ref_invoice):
@ -235,7 +226,7 @@ def get_availability_data(date, practitioner):
check_employee_wise_availability(date, practitioner_doc) check_employee_wise_availability(date, practitioner_doc)
if practitioner_doc.practitioner_schedules: if practitioner_doc.practitioner_schedules:
slot_details = get_available_slots(practitioner_doc.practitioner_schedules, date) slot_details = get_available_slots(practitioner_doc, date)
else: else:
frappe.throw(_('{0} does not have a Healthcare Practitioner Schedule. Add it in Healthcare Practitioner master').format( frappe.throw(_('{0} does not have a Healthcare Practitioner Schedule. Add it in Healthcare Practitioner master').format(
practitioner), title=_('Practitioner Schedule Not Found')) practitioner), title=_('Practitioner Schedule Not Found'))
@ -271,13 +262,13 @@ def check_employee_wise_availability(date, practitioner_doc):
frappe.throw(_('{0} is on Leave on {1}').format(practitioner_doc.name, date), title=_('Not Available')) frappe.throw(_('{0} is on Leave on {1}').format(practitioner_doc.name, date), title=_('Not Available'))
def get_available_slots(practitioner_schedules, date): def get_available_slots(practitioner_doc, date):
available_slots = [] available_slots = []
slot_details = [] slot_details = []
weekday = date.strftime('%A') weekday = date.strftime('%A')
practitioner = None practitioner = practitioner_doc.name
for schedule_entry in practitioner_schedules: for schedule_entry in practitioner_doc.practitioner_schedules:
if schedule_entry.schedule: if schedule_entry.schedule:
practitioner_schedule = frappe.get_doc('Practitioner Schedule', schedule_entry.schedule) practitioner_schedule = frappe.get_doc('Practitioner Schedule', schedule_entry.schedule)
else: else:
@ -310,7 +301,7 @@ def get_available_slots(practitioner_schedules, date):
slot_name = schedule_entry.schedule slot_name = schedule_entry.schedule
# fetch all appointments to practitioner without service unit # fetch all appointments to practitioner without service unit
filters['practitioner'] = practitioner filters['practitioner'] = practitioner
filters['service_unit'] = '' filters.pop('service_unit')
appointments = frappe.get_all( appointments = frappe.get_all(
'Patient Appointment', 'Patient Appointment',

View File

@ -72,13 +72,12 @@ def get_fee_validity(patient_appointments):
if not practitioner_exist_in_list: if not practitioner_exist_in_list:
valid_till = appointment.appointment_date + datetime.timedelta(days=int(valid_days)) valid_till = appointment.appointment_date + datetime.timedelta(days=int(valid_days))
visits = 0 visits = 0
validity = check_validity_exists(appointment.practitioner, appointment.patient) fee_validity = check_fee_validity(appointment)
if validity: if fee_validity:
fee_validity = frappe.get_doc('Fee Validity', validity)
valid_till = fee_validity.valid_till valid_till = fee_validity.valid_till
visits = fee_validity.visited visits = fee_validity.visited
fee_validity_details.append({'practitioner': appointment.practitioner, fee_validity_details.append({'practitioner': appointment.practitioner,
'valid_till': valid_till, 'visits': visits}) 'valid_till': valid_till, 'visits': visits})
if not skip_invoice: if not skip_invoice:
practitioner_charge = 0 practitioner_charge = 0
@ -347,32 +346,39 @@ def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
frappe.db.set_value(dt, doc_created, 'invoiced', invoiced) frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
def check_validity_exists(practitioner, patient): def check_fee_validity(appointment):
return frappe.db.get_value('Fee Validity', {'practitioner': practitioner, 'patient': patient}, 'name') validity = frappe.db.exists('Fee Validity', {
'practitioner': appointment.practitioner,
'patient': appointment.patient
})
if not validity:
return
fee_validity = frappe.get_doc('Fee Validity', validity)
appointment_date = getdate(appointment.appointment_date)
if fee_validity.valid_till >= appointment_date and fee_validity.visited < fee_validity.max_visits:
return fee_validity
def manage_fee_validity(appointment_name, method, ref_invoice=None): def manage_fee_validity(appointment_name, method, ref_invoice=None):
appointment_doc = frappe.get_doc('Patient Appointment', appointment_name) appointment_doc = frappe.get_doc('Patient Appointment', appointment_name)
validity = check_validity_exists(appointment_doc.practitioner, appointment_doc.patient) fee_validity = check_fee_validity(appointment_doc)
do_not_update = False do_not_update = False
visited = 0 visited = 0
if validity: if fee_validity:
fee_validity = frappe.get_doc('Fee Validity', validity) if method == 'on_cancel' and appointment_doc.status != 'Closed':
# Check if the validity is valid if ref_invoice == fee_validity.ref_invoice:
if fee_validity.valid_till >= appointment_doc.appointment_date: visited = fee_validity.visited - 1
if method == 'on_cancel' and appointment_doc.status != 'Closed': if visited < 0:
if ref_invoice == fee_validity.ref_invoice: visited = 0
visited = fee_validity.visited - 1
if visited < 0:
visited = 0
frappe.db.set_value('Fee Validity', fee_validity.name, 'visited', visited)
do_not_update = True
elif method == 'on_submit' and fee_validity.visited < fee_validity.max_visits:
visited = fee_validity.visited + 1
frappe.db.set_value('Fee Validity', fee_validity.name, 'visited', visited) frappe.db.set_value('Fee Validity', fee_validity.name, 'visited', visited)
do_not_update = True do_not_update = True
else: elif method == 'on_submit' and fee_validity.visited < fee_validity.max_visits:
do_not_update = False visited = fee_validity.visited + 1
frappe.db.set_value('Fee Validity', fee_validity.name, 'visited', visited)
do_not_update = True
else:
do_not_update = False
if not do_not_update: if not do_not_update:
fee_validity = update_fee_validity(fee_validity, appointment_doc.appointment_date, ref_invoice) fee_validity = update_fee_validity(fee_validity, appointment_doc.appointment_date, ref_invoice)