blob: cae3008ca829c0d952535f09bb2bf8fb1b9c63f1 [file] [log] [blame]
Jamsheerba119722018-07-06 15:58:13 +05301# -*- coding: utf-8 -*-
2# Copyright (c) 2018, earthians and contributors
3# For license information, please see license.txt
4
5from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05306
Rucha Mahabalccc80922021-02-18 16:41:10 +05307import json
Chillar Anand915b3432021-09-02 16:44:59 +05308import math
9
10import frappe
Jamsheerba119722018-07-06 15:58:13 +053011from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +053012from frappe.utils import cstr, rounded, time_diff_in_hours
Rucha Mahabal5e3c51b2020-11-30 13:35:00 +053013from frappe.utils.formatters import format_value
Chillar Anand915b3432021-09-02 16:44:59 +053014
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +053015from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity
Chillar Anand915b3432021-09-02 16:44:59 +053016from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account
Jamsheer0ae100b2018-08-01 14:29:43 +053017from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple
Jamsheerba119722018-07-06 15:58:13 +053018
Chillar Anand915b3432021-09-02 16:44:59 +053019
Jamsheerba119722018-07-06 15:58:13 +053020@frappe.whitelist()
anoop93d0c782020-04-13 16:42:03 +053021def get_healthcare_services_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +053022 patient = frappe.get_doc('Patient', patient)
anoop93d0c782020-04-13 16:42:03 +053023 items_to_invoice = []
Jamsheerba119722018-07-06 15:58:13 +053024 if patient:
Rucha Mahabal27512c82020-03-09 17:29:23 +053025 validate_customer_created(patient)
anoop93d0c782020-04-13 16:42:03 +053026 # Customer validated, build a list of billable services
27 items_to_invoice += get_appointments_to_invoice(patient, company)
28 items_to_invoice += get_encounters_to_invoice(patient, company)
29 items_to_invoice += get_lab_tests_to_invoice(patient, company)
30 items_to_invoice += get_clinical_procedures_to_invoice(patient, company)
31 items_to_invoice += get_inpatient_services_to_invoice(patient, company)
Rucha Mahabal434791e2020-10-24 14:20:38 +053032 items_to_invoice += get_therapy_plans_to_invoice(patient, company)
Rucha Mahabald7304512020-04-23 00:52:47 +053033 items_to_invoice += get_therapy_sessions_to_invoice(patient, company)
Jamsheerba119722018-07-06 15:58:13 +053034
Rucha Mahabal27512c82020-03-09 17:29:23 +053035 return items_to_invoice
Jamsheerba119722018-07-06 15:58:13 +053036
anoop93d0c782020-04-13 16:42:03 +053037
Rucha Mahabal27512c82020-03-09 17:29:23 +053038def validate_customer_created(patient):
39 if not frappe.db.get_value('Patient', patient.name, 'customer'):
40 msg = _("Please set a Customer linked to the Patient")
Rushabh Mehta542bc012020-11-18 15:00:34 +053041 msg += " <b><a href='/app/Form/Patient/{0}'>{0}</a></b>".format(patient.name)
Rucha Mahabal27512c82020-03-09 17:29:23 +053042 frappe.throw(msg, title=_('Customer Not Found'))
Jamsheer8da6f4e2018-07-26 21:03:17 +053043
Rucha Mahabal434791e2020-10-24 14:20:38 +053044
anoop93d0c782020-04-13 16:42:03 +053045def get_appointments_to_invoice(patient, company):
46 appointments_to_invoice = []
47 patient_appointments = frappe.get_list(
48 'Patient Appointment',
49 fields = '*',
anoop59030022020-07-28 21:15:54 +053050 filters = {'patient': patient.name, 'company': company, 'invoiced': 0, 'status': ['not in', 'Cancelled']},
anoop93d0c782020-04-13 16:42:03 +053051 order_by = 'appointment_date'
52 )
53
Rucha Mahabal27512c82020-03-09 17:29:23 +053054 for appointment in patient_appointments:
anoop93d0c782020-04-13 16:42:03 +053055 # Procedure Appointments
Rucha Mahabal27512c82020-03-09 17:29:23 +053056 if appointment.procedure_template:
57 if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'):
anoop93d0c782020-04-13 16:42:03 +053058 appointments_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +053059 'reference_type': 'Patient Appointment',
60 'reference_name': appointment.name,
61 'service': appointment.procedure_template
62 })
anoop93d0c782020-04-13 16:42:03 +053063 # Consultation Appointments, should check fee validity
Jamsheerba119722018-07-06 15:58:13 +053064 else:
anoop93d0c782020-04-13 16:42:03 +053065 if frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups') and \
66 frappe.db.exists('Fee Validity Reference', {'appointment': appointment.name}):
67 continue # Skip invoicing, fee validty present
68 practitioner_charge = 0
69 income_account = None
70 service_item = None
71 if appointment.practitioner:
Rucha Mahabalccc80922021-02-18 16:41:10 +053072 details = get_service_item_and_practitioner_charge(appointment)
73 service_item = details.get('service_item')
74 practitioner_charge = details.get('practitioner_charge')
anoop93d0c782020-04-13 16:42:03 +053075 income_account = get_income_account(appointment.practitioner, appointment.company)
76 appointments_to_invoice.append({
77 'reference_type': 'Patient Appointment',
78 'reference_name': appointment.name,
79 'service': service_item,
80 'rate': practitioner_charge,
81 'income_account': income_account
82 })
Rucha Mahabal27512c82020-03-09 17:29:23 +053083
anoop93d0c782020-04-13 16:42:03 +053084 return appointments_to_invoice
Rucha Mahabal27512c82020-03-09 17:29:23 +053085
86
anoop93d0c782020-04-13 16:42:03 +053087def get_encounters_to_invoice(patient, company):
Rucha Mahabal0f059252021-01-13 09:46:33 +053088 if not isinstance(patient, str):
89 patient = patient.name
Rucha Mahabal27512c82020-03-09 17:29:23 +053090 encounters_to_invoice = []
91 encounters = frappe.get_list(
92 'Patient Encounter',
93 fields=['*'],
Rucha Mahabal0f059252021-01-13 09:46:33 +053094 filters={'patient': patient, 'company': company, 'invoiced': False, 'docstatus': 1}
Rucha Mahabal27512c82020-03-09 17:29:23 +053095 )
96 if encounters:
97 for encounter in encounters:
98 if not encounter.appointment:
99 practitioner_charge = 0
100 income_account = None
101 service_item = None
102 if encounter.practitioner:
Rucha Mahabal13541972021-01-13 09:12:50 +0530103 if encounter.inpatient_record and \
104 frappe.db.get_single_value('Healthcare Settings', 'do_not_bill_inpatient_encounters'):
105 continue
106
Rucha Mahabalccc80922021-02-18 16:41:10 +0530107 details = get_service_item_and_practitioner_charge(encounter)
108 service_item = details.get('service_item')
109 practitioner_charge = details.get('practitioner_charge')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530110 income_account = get_income_account(encounter.practitioner, encounter.company)
111
112 encounters_to_invoice.append({
113 'reference_type': 'Patient Encounter',
114 'reference_name': encounter.name,
115 'service': service_item,
116 'rate': practitioner_charge,
117 'income_account': income_account
118 })
119
120 return encounters_to_invoice
121
122
anoop93d0c782020-04-13 16:42:03 +0530123def get_lab_tests_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530124 lab_tests_to_invoice = []
125 lab_tests = frappe.get_list(
126 'Lab Test',
127 fields=['name', 'template'],
anoop93d0c782020-04-13 16:42:03 +0530128 filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
Rucha Mahabal27512c82020-03-09 17:29:23 +0530129 )
130 for lab_test in lab_tests:
Rucha Mahabal131452c2020-04-27 10:52:38 +0530131 item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.template, ['item', 'is_billable'])
Rucha Mahabalced978e2020-04-02 18:45:53 +0530132 if is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530133 lab_tests_to_invoice.append({
134 'reference_type': 'Lab Test',
135 'reference_name': lab_test.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530136 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530137 })
138
Rucha Mahabalced978e2020-04-02 18:45:53 +0530139 lab_prescriptions = frappe.db.sql(
140 '''
141 SELECT
142 lp.name, lp.lab_test_code
143 FROM
144 `tabPatient Encounter` et, `tabLab Prescription` lp
145 WHERE
146 et.patient=%s
147 and lp.parent=et.name
148 and lp.lab_test_created=0
149 and lp.invoiced=0
150 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530151
152 for prescription in lab_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530153 item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable'])
154 if prescription.lab_test_code and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530155 lab_tests_to_invoice.append({
156 'reference_type': 'Lab Prescription',
157 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530158 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530159 })
160
161 return lab_tests_to_invoice
162
163
anoop93d0c782020-04-13 16:42:03 +0530164def get_clinical_procedures_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530165 clinical_procedures_to_invoice = []
166 procedures = frappe.get_list(
167 'Clinical Procedure',
168 fields='*',
anoop93d0c782020-04-13 16:42:03 +0530169 filters={'patient': patient.name, 'company': company, 'invoiced': False}
Rucha Mahabal27512c82020-03-09 17:29:23 +0530170 )
171 for procedure in procedures:
172 if not procedure.appointment:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530173 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable'])
174 if procedure.procedure_template and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530175 clinical_procedures_to_invoice.append({
176 'reference_type': 'Clinical Procedure',
177 'reference_name': procedure.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530178 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530179 })
180
181 # consumables
182 if procedure.invoice_separately_as_consumables and procedure.consume_stock \
183 and procedure.status == 'Completed' and not procedure.consumption_invoiced:
184
Rucha Mahabalccc80922021-02-18 16:41:10 +0530185 service_item = frappe.db.get_single_value('Healthcare Settings', 'clinical_procedure_consumable_item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530186 if not service_item:
Rucha Mahabal16824022021-08-30 18:26:56 +0530187 frappe.throw(_('Please configure Clinical Procedure Consumable Item in {0}').format(
188 frappe.utils.get_link_to_form('Healthcare Settings', 'Healthcare Settings')),
189 title=_('Missing Configuration'))
Rucha Mahabal27512c82020-03-09 17:29:23 +0530190
191 clinical_procedures_to_invoice.append({
192 'reference_type': 'Clinical Procedure',
193 'reference_name': procedure.name,
194 'service': service_item,
195 'rate': procedure.consumable_total_amount,
196 'description': procedure.consumption_details
197 })
198
Rucha Mahabalced978e2020-04-02 18:45:53 +0530199 procedure_prescriptions = frappe.db.sql(
200 '''
201 SELECT
202 pp.name, pp.procedure
203 FROM
204 `tabPatient Encounter` et, `tabProcedure Prescription` pp
205 WHERE
206 et.patient=%s
207 and pp.parent=et.name
208 and pp.procedure_created=0
209 and pp.invoiced=0
210 and pp.appointment_booked=0
211 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530212
213 for prescription in procedure_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530214 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable'])
215 if is_billable:
Rucha Mahabal197165f2020-03-26 17:29:50 +0530216 clinical_procedures_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +0530217 'reference_type': 'Procedure Prescription',
218 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530219 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530220 })
221
222 return clinical_procedures_to_invoice
223
224
anoop93d0c782020-04-13 16:42:03 +0530225def get_inpatient_services_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530226 services_to_invoice = []
Rucha Mahabalced978e2020-04-02 18:45:53 +0530227 inpatient_services = frappe.db.sql(
228 '''
229 SELECT
230 io.*
231 FROM
232 `tabInpatient Record` ip, `tabInpatient Occupancy` io
233 WHERE
234 ip.patient=%s
anoop93d0c782020-04-13 16:42:03 +0530235 and ip.company=%s
Rucha Mahabalced978e2020-04-02 18:45:53 +0530236 and io.parent=ip.name
237 and io.left=1
238 and io.invoiced=0
anoop93d0c782020-04-13 16:42:03 +0530239 ''', (patient.name, company), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530240
241 for inpatient_occupancy in inpatient_services:
242 service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
Rucha Mahabalced978e2020-04-02 18:45:53 +0530243 service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530244 if service_unit_type and service_unit_type.is_billable:
245 hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
246 qty = 0.5
247 if hours_occupied > 0:
248 actual_qty = hours_occupied / service_unit_type.no_of_hours
249 floor = math.floor(actual_qty)
250 decimal_part = actual_qty - floor
251 if decimal_part > 0.5:
252 qty = rounded(floor + 1, 1)
253 elif decimal_part < 0.5 and decimal_part > 0:
254 qty = rounded(floor + 0.5, 1)
255 if qty <= 0:
256 qty = 0.5
257 services_to_invoice.append({
258 'reference_type': 'Inpatient Occupancy',
259 'reference_name': inpatient_occupancy.name,
260 'service': service_unit_type.item, 'qty': qty
261 })
262
263 return services_to_invoice
264
Jamsheerba119722018-07-06 15:58:13 +0530265
Rucha Mahabal434791e2020-10-24 14:20:38 +0530266def get_therapy_plans_to_invoice(patient, company):
267 therapy_plans_to_invoice = []
268 therapy_plans = frappe.get_list(
269 'Therapy Plan',
270 fields=['therapy_plan_template', 'name'],
271 filters={
272 'patient': patient.name,
273 'invoiced': 0,
274 'company': company,
275 'therapy_plan_template': ('!=', '')
276 }
277 )
278 for plan in therapy_plans:
279 therapy_plans_to_invoice.append({
280 'reference_type': 'Therapy Plan',
281 'reference_name': plan.name,
282 'service': frappe.db.get_value('Therapy Plan Template', plan.therapy_plan_template, 'linked_item')
283 })
284
285 return therapy_plans_to_invoice
286
287
Rucha Mahabald7304512020-04-23 00:52:47 +0530288def get_therapy_sessions_to_invoice(patient, company):
Rucha Mahabaleaa956b2020-04-22 13:07:12 +0530289 therapy_sessions_to_invoice = []
Rucha Mahabal434791e2020-10-24 14:20:38 +0530290 therapy_plans = frappe.db.get_all('Therapy Plan', {'therapy_plan_template': ('!=', '')})
291 therapy_plans_created_from_template = []
292 for entry in therapy_plans:
293 therapy_plans_created_from_template.append(entry.name)
294
Rucha Mahabaleaa956b2020-04-22 13:07:12 +0530295 therapy_sessions = frappe.get_list(
296 'Therapy Session',
297 fields='*',
Rucha Mahabal434791e2020-10-24 14:20:38 +0530298 filters={
299 'patient': patient.name,
300 'invoiced': 0,
301 'company': company,
302 'therapy_plan': ('not in', therapy_plans_created_from_template)
303 }
Rucha Mahabaleaa956b2020-04-22 13:07:12 +0530304 )
305 for therapy in therapy_sessions:
306 if not therapy.appointment:
307 if therapy.therapy_type and frappe.db.get_value('Therapy Type', therapy.therapy_type, 'is_billable'):
308 therapy_sessions_to_invoice.append({
309 'reference_type': 'Therapy Session',
310 'reference_name': therapy.name,
311 'service': frappe.db.get_value('Therapy Type', therapy.therapy_type, 'item')
312 })
313
314 return therapy_sessions_to_invoice
315
Rucha Mahabalccc80922021-02-18 16:41:10 +0530316@frappe.whitelist()
Rucha Mahabal24055e12020-02-24 19:09:50 +0530317def get_service_item_and_practitioner_charge(doc):
Rucha Mahabal16824022021-08-30 18:26:56 +0530318 if isinstance(doc, str):
Rucha Mahabalccc80922021-02-18 16:41:10 +0530319 doc = json.loads(doc)
320 doc = frappe.get_doc(doc)
321
322 service_item = None
323 practitioner_charge = None
324 department = doc.medical_department if doc.doctype == 'Patient Encounter' else doc.department
325
Rucha Mahabal27512c82020-03-09 17:29:23 +0530326 is_inpatient = doc.inpatient_record
Rucha Mahabalccc80922021-02-18 16:41:10 +0530327
328 if doc.get('appointment_type'):
329 service_item, practitioner_charge = get_appointment_type_service_item(doc.appointment_type, department, is_inpatient)
330
331 if not service_item and not practitioner_charge:
332 service_item, practitioner_charge = get_practitioner_service_item(doc.practitioner, is_inpatient)
Jamsheeree5f9c72018-07-30 12:42:06 +0530333 if not service_item:
Rucha Mahabalccc80922021-02-18 16:41:10 +0530334 service_item = get_healthcare_service_item(is_inpatient)
335
Jamsheer8da6f4e2018-07-26 21:03:17 +0530336 if not service_item:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530337 throw_config_service_item(is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530338
Jamsheer8da6f4e2018-07-26 21:03:17 +0530339 if not practitioner_charge:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530340 throw_config_practitioner_charge(is_inpatient, doc.practitioner)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530341
Rucha Mahabalccc80922021-02-18 16:41:10 +0530342 return {'service_item': service_item, 'practitioner_charge': practitioner_charge}
343
344
345def get_appointment_type_service_item(appointment_type, department, is_inpatient):
Chillar Anand915b3432021-09-02 16:44:59 +0530346 from erpnext.healthcare.doctype.appointment_type.appointment_type import (
347 get_service_item_based_on_department,
348 )
Rucha Mahabalccc80922021-02-18 16:41:10 +0530349
350 item_list = get_service_item_based_on_department(appointment_type, department)
351 service_item = None
352 practitioner_charge = None
353
354 if item_list:
355 if is_inpatient:
356 service_item = item_list.get('inpatient_visit_charge_item')
357 practitioner_charge = item_list.get('inpatient_visit_charge')
358 else:
359 service_item = item_list.get('op_consulting_charge_item')
360 practitioner_charge = item_list.get('op_consulting_charge')
361
Jamsheer8da6f4e2018-07-26 21:03:17 +0530362 return service_item, practitioner_charge
363
Jamsheer8da6f4e2018-07-26 21:03:17 +0530364
Rucha Mahabal27512c82020-03-09 17:29:23 +0530365def throw_config_service_item(is_inpatient):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530366 service_item_label = _('Out Patient Consulting Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530367 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530368 service_item_label = _('Inpatient Visit Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530369
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530370 msg = _(('Please Configure {0} in ').format(service_item_label) \
Rushabh Mehta542bc012020-11-18 15:00:34 +0530371 + '''<b><a href='/app/Form/Healthcare Settings'>Healthcare Settings</a></b>''')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530372 frappe.throw(msg, title=_('Missing Configuration'))
373
Jamsheer8da6f4e2018-07-26 21:03:17 +0530374
Rucha Mahabal24055e12020-02-24 19:09:50 +0530375def throw_config_practitioner_charge(is_inpatient, practitioner):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530376 charge_name = _('OP Consulting Charge')
Rucha Mahabal24055e12020-02-24 19:09:50 +0530377 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530378 charge_name = _('Inpatient Visit Charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530379
Rucha Mahabal27512c82020-03-09 17:29:23 +0530380 msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
Rushabh Mehta542bc012020-11-18 15:00:34 +0530381 + ''' <b><a href='/app/Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
Rucha Mahabal27512c82020-03-09 17:29:23 +0530382 frappe.throw(msg, title=_('Missing Configuration'))
383
Jamsheer8da6f4e2018-07-26 21:03:17 +0530384
Rucha Mahabalccc80922021-02-18 16:41:10 +0530385def get_practitioner_service_item(practitioner, is_inpatient):
386 service_item = None
387 practitioner_charge = None
388
389 if is_inpatient:
390 service_item, practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, ['inpatient_visit_charge_item', 'inpatient_visit_charge'])
391 else:
392 service_item, practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, ['op_consulting_charge_item', 'op_consulting_charge'])
393
394 return service_item, practitioner_charge
Rucha Mahabal27512c82020-03-09 17:29:23 +0530395
Jamsheeree5f9c72018-07-30 12:42:06 +0530396
Rucha Mahabalccc80922021-02-18 16:41:10 +0530397def get_healthcare_service_item(is_inpatient):
398 service_item = None
399
400 if is_inpatient:
401 service_item = frappe.db.get_single_value('Healthcare Settings', 'inpatient_visit_charge_item')
402 else:
403 service_item = frappe.db.get_single_value('Healthcare Settings', 'op_consulting_charge_item')
404
405 return service_item
Jamsheer8da6f4e2018-07-26 21:03:17 +0530406
Jamsheer8da6f4e2018-07-26 21:03:17 +0530407
Rucha Mahabal24055e12020-02-24 19:09:50 +0530408def get_practitioner_charge(practitioner, is_inpatient):
409 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530410 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530411 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530412 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge')
Jamsheerba119722018-07-06 15:58:13 +0530413 if practitioner_charge:
414 return practitioner_charge
Jamsheer8da6f4e2018-07-26 21:03:17 +0530415 return False
Jamsheerba119722018-07-06 15:58:13 +0530416
Rucha Mahabal27512c82020-03-09 17:29:23 +0530417
Jamsheerba119722018-07-06 15:58:13 +0530418def manage_invoice_submit_cancel(doc, method):
419 if doc.items:
420 for item in doc.items:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530421 if item.get('reference_dt') and item.get('reference_dn'):
422 if frappe.get_meta(item.reference_dt).has_field('invoiced'):
Jamsheer146683b2018-07-25 11:30:30 +0530423 set_invoiced(item, method, doc.name)
Jamsheerba119722018-07-06 15:58:13 +0530424
Rucha Mahabal27512c82020-03-09 17:29:23 +0530425 if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'):
426 create_multiple('Sales Invoice', doc.name)
427
Jamsheer0ae100b2018-08-01 14:29:43 +0530428
Jamsheer146683b2018-07-25 11:30:30 +0530429def set_invoiced(item, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530430 invoiced = False
Rucha Mahabal27512c82020-03-09 17:29:23 +0530431 if method=='on_submit':
Jamsheerba119722018-07-06 15:58:13 +0530432 validate_invoiced_on_submit(item)
433 invoiced = True
434
Jamsheer8da6f4e2018-07-26 21:03:17 +0530435 if item.reference_dt == 'Clinical Procedure':
Rucha Mahabalccc80922021-02-18 16:41:10 +0530436 service_item = frappe.db.get_single_value('Healthcare Settings', 'clinical_procedure_consumable_item')
437 if service_item == item.item_code:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530438 frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530439 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530440 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530441 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530442 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530443
Jamsheerba119722018-07-06 15:58:13 +0530444 if item.reference_dt == 'Patient Appointment':
445 if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530446 dt_from_appointment = 'Clinical Procedure'
Jamsheerba119722018-07-06 15:58:13 +0530447 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530448 dt_from_appointment = 'Patient Encounter'
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530449 manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced)
Jamsheerba119722018-07-06 15:58:13 +0530450
451 elif item.reference_dt == 'Lab Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530452 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created')
Jamsheerba119722018-07-06 15:58:13 +0530453
454 elif item.reference_dt == 'Procedure Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530455 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created')
456
Jamsheerba119722018-07-06 15:58:13 +0530457
458def validate_invoiced_on_submit(item):
Rucha Mahabalccc80922021-02-18 16:41:10 +0530459 if item.reference_dt == 'Clinical Procedure' and \
460 frappe.db.get_single_value('Healthcare Settings', 'clinical_procedure_consumable_item') == item.item_code:
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530461 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530462 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530463 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced')
464 if is_invoiced:
Rucha Mahabal434791e2020-10-24 14:20:38 +0530465 frappe.throw(_('The item referenced by {0} - {1} is already invoiced').format(
466 item.reference_dt, item.reference_dn))
Jamsheerba119722018-07-06 15:58:13 +0530467
Rucha Mahabal27512c82020-03-09 17:29:23 +0530468
Jamsheerba119722018-07-06 15:58:13 +0530469def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
470 created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530471 if created:
Jamsheerba119722018-07-06 15:58:13 +0530472 # Fetch the doc created for the prescription
Jamsheereafb0462018-07-25 13:15:12 +0530473 doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
Jamsheerba119722018-07-06 15:58:13 +0530474 frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
475
Rucha Mahabal27512c82020-03-09 17:29:23 +0530476
Rucha Mahabalcd319962020-03-13 15:39:31 +0530477def check_fee_validity(appointment):
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530478 if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
479 return
480
Rucha Mahabalcd319962020-03-13 15:39:31 +0530481 validity = frappe.db.exists('Fee Validity', {
482 'practitioner': appointment.practitioner,
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530483 'patient': appointment.patient,
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530484 'valid_till': ('>=', appointment.appointment_date)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530485 })
486 if not validity:
487 return
488
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530489 validity = frappe.get_doc('Fee Validity', validity)
490 return validity
491
Rucha Mahabal27512c82020-03-09 17:29:23 +0530492
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530493def manage_fee_validity(appointment):
494 fee_validity = check_fee_validity(appointment)
anoop93d0c782020-04-13 16:42:03 +0530495
Rucha Mahabalcd319962020-03-13 15:39:31 +0530496 if fee_validity:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530497 if appointment.status == 'Cancelled' and fee_validity.visited > 0:
498 fee_validity.visited -= 1
499 frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name})
Rucha Mahabal2cec6bd2020-03-26 14:38:12 +0530500 elif fee_validity.status == 'Completed':
501 return
Rucha Mahabalcd319962020-03-13 15:39:31 +0530502 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530503 fee_validity.visited += 1
504 fee_validity.append('ref_appointments', {
505 'appointment': appointment.name
506 })
507 fee_validity.save(ignore_permissions=True)
Jamsheerba119722018-07-06 15:58:13 +0530508 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530509 fee_validity = create_fee_validity(appointment)
510 return fee_validity
Rucha Mahabal27512c82020-03-09 17:29:23 +0530511
Jamsheerba119722018-07-06 15:58:13 +0530512
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530513def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced):
Rucha Mahabalc4b2dce2020-03-09 23:57:00 +0530514 dn_from_appointment = frappe.db.get_value(
515 dt_from_appointment,
Rucha Mahabal27512c82020-03-09 17:29:23 +0530516 filters={'appointment': appointment}
Jamsheerba119722018-07-06 15:58:13 +0530517 )
518 if dn_from_appointment:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530519 frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced)
520
Jamsheere82f27a2018-07-30 11:28:37 +0530521
522@frappe.whitelist()
523def get_drugs_to_invoice(encounter):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530524 encounter = frappe.get_doc('Patient Encounter', encounter)
Jamsheere82f27a2018-07-30 11:28:37 +0530525 if encounter:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530526 patient = frappe.get_doc('Patient', encounter.patient)
527 if patient:
528 if patient.customer:
529 items_to_invoice = []
Jamsheere82f27a2018-07-30 11:28:37 +0530530 for drug_line in encounter.drug_prescription:
531 if drug_line.drug_code:
532 qty = 1
Rucha Mahabal27512c82020-03-09 17:29:23 +0530533 if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos':
Jamsheere82f27a2018-07-30 11:28:37 +0530534 qty = drug_line.get_quantity()
Rucha Mahabal27512c82020-03-09 17:29:23 +0530535
536 description = ''
537 if drug_line.dosage and drug_line.period:
538 description = _('{0} for {1}').format(drug_line.dosage, drug_line.period)
539
540 items_to_invoice.append({
541 'drug_code': drug_line.drug_code,
542 'quantity': qty,
543 'description': description
544 })
545 return items_to_invoice
546 else:
547 validate_customer_created(patient)
548
Jamsheer4371c7e2018-08-01 18:40:05 +0530549
550@frappe.whitelist()
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530551def get_children(doctype, parent=None, company=None, is_root=False):
552 parent_fieldname = 'parent_' + doctype.lower().replace(' ', '_')
Jamsheer4371c7e2018-08-01 18:40:05 +0530553 fields = [
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530554 'name as value',
555 'is_group as expandable',
556 'lft',
557 'rgt'
Jamsheer4371c7e2018-08-01 18:40:05 +0530558 ]
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530559
560 filters = [["ifnull(`{0}`,'')".format(parent_fieldname),
561 '=', '' if is_root else parent]]
Jamsheer4371c7e2018-08-01 18:40:05 +0530562
563 if is_root:
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530564 fields += ['service_unit_type'] if doctype == 'Healthcare Service Unit' else []
565 filters.append(['company', '=', company])
Jamsheer4371c7e2018-08-01 18:40:05 +0530566 else:
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530567 fields += ['service_unit_type', 'allow_appointments', 'inpatient_occupancy',
568 'occupancy_status'] if doctype == 'Healthcare Service Unit' else []
569 fields += [parent_fieldname + ' as parent']
Jamsheer4371c7e2018-08-01 18:40:05 +0530570
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530571 service_units = frappe.get_list(doctype, fields=fields, filters=filters)
572 for each in service_units:
573 if each['expandable'] == 1: # group node
574 available_count = frappe.db.count('Healthcare Service Unit', filters={
575 'parent_healthcare_service_unit': each['value'],
576 'inpatient_occupancy': 1})
Jamsheer4371c7e2018-08-01 18:40:05 +0530577
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530578 if available_count > 0:
579 occupied_count = frappe.db.count('Healthcare Service Unit', {
580 'parent_healthcare_service_unit': each['value'],
581 'inpatient_occupancy': 1,
582 'occupancy_status': 'Occupied'})
583 # set occupancy status of group node
584 each['occupied_of_available'] = str(
585 occupied_count) + ' Occupied of ' + str(available_count)
Rucha Mahabalced978e2020-04-02 18:45:53 +0530586
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530587 return service_units
Jamsheer5073ac42019-07-12 12:28:34 +0530588
Rucha Mahabal27512c82020-03-09 17:29:23 +0530589
Jamsheer5073ac42019-07-12 12:28:34 +0530590@frappe.whitelist()
591def get_patient_vitals(patient, from_date=None, to_date=None):
592 if not patient: return
Rucha Mahabal27512c82020-03-09 17:29:23 +0530593
Rucha Mahabal4dd6b992020-05-25 18:42:01 +0530594 vitals = frappe.db.get_all('Vital Signs', filters={
Rucha Mahabal27512c82020-03-09 17:29:23 +0530595 'docstatus': 1,
596 'patient': patient
Rucha Mahabal4dd6b992020-05-25 18:42:01 +0530597 }, order_by='signs_date, signs_time', fields=['*'])
Rucha Mahabal27512c82020-03-09 17:29:23 +0530598
599 if len(vitals):
Jamsheer5073ac42019-07-12 12:28:34 +0530600 return vitals
Rucha Mahabal27512c82020-03-09 17:29:23 +0530601 return False
602
Jamsheer5073ac42019-07-12 12:28:34 +0530603
604@frappe.whitelist()
605def render_docs_as_html(docs):
606 # docs key value pair {doctype: docname}
607 docs_html = "<div class='col-md-12 col-sm-12 text-muted'>"
608 for doc in docs:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530609 docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>'
Jamsheer5073ac42019-07-12 12:28:34 +0530610 return {'html': docs_html}
611
Rucha Mahabal27512c82020-03-09 17:29:23 +0530612
Jamsheer5073ac42019-07-12 12:28:34 +0530613@frappe.whitelist()
614def render_doc_as_html(doctype, docname, exclude_fields = []):
Rucha Mahabal16824022021-08-30 18:26:56 +0530615 """
616 Render document as HTML
617 """
618
Jamsheer5073ac42019-07-12 12:28:34 +0530619 doc = frappe.get_doc(doctype, docname)
620 meta = frappe.get_meta(doctype)
Rucha Mahabal16824022021-08-30 18:26:56 +0530621 doc_html = section_html = section_label = html = ""
622 sec_on = has_data = False
Jamsheer5073ac42019-07-12 12:28:34 +0530623 col_on = 0
Rucha Mahabal16824022021-08-30 18:26:56 +0530624
Jamsheer5073ac42019-07-12 12:28:34 +0530625 for df in meta.fields:
Rucha Mahabal16824022021-08-30 18:26:56 +0530626 # on section break append previous section and html to doc html
Jamsheer5073ac42019-07-12 12:28:34 +0530627 if df.fieldtype == "Section Break":
628 if has_data and col_on and sec_on:
629 doc_html += section_html + html + "</div>"
Rucha Mahabal16824022021-08-30 18:26:56 +0530630
Jamsheer5073ac42019-07-12 12:28:34 +0530631 elif has_data and not col_on and sec_on:
Rucha Mahabal16824022021-08-30 18:26:56 +0530632 doc_html += """
633 <br>
634 <div class='row'>
635 <div class='col-md-12 col-sm-12'>
636 <b>{0}</b>
637 </div>
638 </div>
639 <div class='row'>
640 <div class='col-md-12 col-sm-12'>
641 {1} {2}
642 </div>
643 </div>
644 """.format(section_label, section_html, html)
645
646 # close divs for columns
Jamsheer5073ac42019-07-12 12:28:34 +0530647 while col_on:
648 doc_html += "</div>"
649 col_on -= 1
Rucha Mahabal16824022021-08-30 18:26:56 +0530650
Jamsheer5073ac42019-07-12 12:28:34 +0530651 sec_on = True
Rucha Mahabal16824022021-08-30 18:26:56 +0530652 has_data = False
Jamsheer5073ac42019-07-12 12:28:34 +0530653 col_on = 0
Rucha Mahabal16824022021-08-30 18:26:56 +0530654 section_html = html = ""
655
Jamsheer5073ac42019-07-12 12:28:34 +0530656 if df.label:
657 section_label = df.label
658 continue
Rucha Mahabal16824022021-08-30 18:26:56 +0530659
660 # on column break append html to section html or doc html
Jamsheer5073ac42019-07-12 12:28:34 +0530661 if df.fieldtype == "Column Break":
Rucha Mahabal16824022021-08-30 18:26:56 +0530662 if sec_on and not col_on and has_data:
663 section_html += """
664 <br>
665 <div class='row'>
666 <div class='col-md-12 col-sm-12'>
667 <b>{0}</b>
668 </div>
669 </div>
670 <div class='row'>
671 <div class='col-md-4 col-sm-4'>
672 {1}
673 </div>
674 """.format(section_label, html)
675 elif col_on == 1 and has_data:
676 section_html += "<div class='col-md-4 col-sm-4'>" + html + "</div>"
677 elif col_on > 1 and has_data:
678 doc_html += "<div class='col-md-4 col-sm-4'>" + html + "</div>"
679 else:
680 doc_html += """
681 <div class='row'>
682 <div class='col-md-12 col-sm-12'>
683 {0}
684 </div>
685 </div>
686 """.format(html)
687
688 html = ""
Jamsheer5073ac42019-07-12 12:28:34 +0530689 col_on += 1
Rucha Mahabal16824022021-08-30 18:26:56 +0530690
Jamsheer5073ac42019-07-12 12:28:34 +0530691 if df.label:
Rucha Mahabal16824022021-08-30 18:26:56 +0530692 html += "<br>" + df.label
Jamsheer5073ac42019-07-12 12:28:34 +0530693 continue
Rucha Mahabal16824022021-08-30 18:26:56 +0530694
695 # on table iterate through items and create table
696 # based on the in_list_view property
697 # append to section html or doc html
698 if df.fieldtype == "Table":
Jamsheer5073ac42019-07-12 12:28:34 +0530699 items = doc.get(df.fieldname)
Rucha Mahabal16824022021-08-30 18:26:56 +0530700 if not items:
701 continue
Jamsheer5073ac42019-07-12 12:28:34 +0530702 child_meta = frappe.get_meta(df.options)
Rucha Mahabal16824022021-08-30 18:26:56 +0530703
704 if not has_data:
705 has_data = True
706 table_head = table_row = ""
Jamsheer5073ac42019-07-12 12:28:34 +0530707 create_head = True
Rucha Mahabal16824022021-08-30 18:26:56 +0530708
Jamsheer5073ac42019-07-12 12:28:34 +0530709 for item in items:
Rucha Mahabal16824022021-08-30 18:26:56 +0530710 table_row += "<tr>"
Jamsheer5073ac42019-07-12 12:28:34 +0530711 for cdf in child_meta.fields:
712 if cdf.in_list_view:
713 if create_head:
Rucha Mahabal16824022021-08-30 18:26:56 +0530714 table_head += "<th class='text-muted'>" + cdf.label + "</th>"
Jamsheer5073ac42019-07-12 12:28:34 +0530715 if item.get(cdf.fieldname):
Rucha Mahabal16824022021-08-30 18:26:56 +0530716 table_row += "<td>" + cstr(item.get(cdf.fieldname)) + "</td>"
Jamsheer5073ac42019-07-12 12:28:34 +0530717 else:
Rucha Mahabal16824022021-08-30 18:26:56 +0530718 table_row += "<td></td>"
719
Jamsheer5073ac42019-07-12 12:28:34 +0530720 create_head = False
Rucha Mahabal16824022021-08-30 18:26:56 +0530721 table_row += "</tr>"
722
Jamsheer5073ac42019-07-12 12:28:34 +0530723 if sec_on:
Rucha Mahabal16824022021-08-30 18:26:56 +0530724 section_html += """
725 <table class='table table-condensed bordered'>
726 {0} {1}
727 </table>
728 """.format(table_head, table_row)
Jamsheer5073ac42019-07-12 12:28:34 +0530729 else:
Rucha Mahabal16824022021-08-30 18:26:56 +0530730 html += """
731 <table class='table table-condensed table-bordered'>
732 {0} {1}
733 </table>
734 """.format(table_head, table_row)
Jamsheer5073ac42019-07-12 12:28:34 +0530735 continue
Rucha Mahabal5e3c51b2020-11-30 13:35:00 +0530736
Rucha Mahabal16824022021-08-30 18:26:56 +0530737 # on any other field type add label and value to html
Jamsheer5073ac42019-07-12 12:28:34 +0530738 if not df.hidden and not df.print_hide and doc.get(df.fieldname) and df.fieldname not in exclude_fields:
Rucha Mahabal16824022021-08-30 18:26:56 +0530739 formatted_value = format_value(doc.get(df.fieldname), meta.get_field(df.fieldname), doc)
740 html += "<br>{0} : {1}".format(df.label or df.fieldname, formatted_value)
Rucha Mahabal5e3c51b2020-11-30 13:35:00 +0530741
Jamsheer5073ac42019-07-12 12:28:34 +0530742 if not has_data : has_data = True
Rucha Mahabal5e3c51b2020-11-30 13:35:00 +0530743
Jamsheer5073ac42019-07-12 12:28:34 +0530744 if sec_on and col_on and has_data:
Rucha Mahabal16824022021-08-30 18:26:56 +0530745 doc_html += section_html + html + "</div></div>"
Jamsheer5073ac42019-07-12 12:28:34 +0530746 elif sec_on and not col_on and has_data:
Rucha Mahabal16824022021-08-30 18:26:56 +0530747 doc_html += """
748 <div class='col-md-12 col-sm-12'>
749 <div class='col-md-12 col-sm-12'>
750 {0} {1}
751 </div>
752 </div>
753 """.format(section_html, html)
Jamsheer5073ac42019-07-12 12:28:34 +0530754
Rucha Mahabal16824022021-08-30 18:26:56 +0530755 return {"html": doc_html}
Rucha Mahabal212eb4b2021-08-30 13:10:18 +0530756
757
758def update_address_links(address, method):
759 '''
760 Hook validate Address
761 If Patient is linked in Address, also link the associated Customer
762 '''
763 if 'Healthcare' not in frappe.get_active_domains():
764 return
765
766 patient_links = list(filter(lambda link: link.get('link_doctype') == 'Patient', address.links))
767
768 for link in patient_links:
769 customer = frappe.db.get_value('Patient', link.get('link_name'), 'customer')
770 if customer and not address.has_link('Customer', customer):
771 address.append('links', dict(link_doctype = 'Customer', link_name = customer))
772
773
774def update_patient_email_and_phone_numbers(contact, method):
775 '''
776 Hook validate Contact
777 Update linked Patients' primary mobile and phone numbers
778 '''
779 if 'Healthcare' not in frappe.get_active_domains():
780 return
781
782 if contact.is_primary_contact and (contact.email_id or contact.mobile_no or contact.phone):
783 patient_links = list(filter(lambda link: link.get('link_doctype') == 'Patient', contact.links))
784
785 for link in patient_links:
786 contact_details = frappe.db.get_value('Patient', link.get('link_name'), ['email', 'mobile', 'phone'], as_dict=1)
787 if contact.email_id and contact.email_id != contact_details.get('email'):
788 frappe.db.set_value('Patient', link.get('link_name'), 'email', contact.email_id)
789 if contact.mobile_no and contact.mobile_no != contact_details.get('mobile'):
790 frappe.db.set_value('Patient', link.get('link_name'), 'mobile', contact.mobile_no)
791 if contact.phone and contact.phone != contact_details.get('phone'):
792 frappe.db.set_value('Patient', link.get('link_name'), 'phone', contact.phone)