Skip to content

Commit

Permalink
fix: linking existing customer to new Paitient overwrites customer na…
Browse files Browse the repository at this point in the history
…me and other details (#306)

test for the same

(cherry picked from commit c5d9de2)

# Conflicts:
#	healthcare/healthcare/doctype/patient/patient.py
  • Loading branch information
akurungadam authored and mergify[bot] committed Nov 1, 2023
1 parent ae5f6e6 commit 849e530
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
67 changes: 54 additions & 13 deletions healthcare/healthcare/doctype/patient/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def onload(self):
def validate(self):
self.set_full_name()
self.flags.is_new_doc = self.is_new()
self.flags.existing_customer = self.is_new() and bool(self.customer)

def before_insert(self):
self.set_missing_customer_details()
Expand All @@ -46,18 +47,13 @@ def after_insert(self):
def on_update(self):
if frappe.db.get_single_value("Healthcare Settings", "link_customer_to_patient"):
if self.customer:
customer = frappe.get_doc("Customer", self.customer)
if self.customer_group:
customer.customer_group = self.customer_group
if self.territory:
customer.territory = self.territory
customer.customer_name = self.patient_name
customer.default_price_list = self.default_price_list
customer.default_currency = self.default_currency
customer.language = self.language
customer.image = self.image
customer.ignore_mandatory = True
customer.save(ignore_permissions=True)
if self.flags.existing_customer or frappe.db.exists(
{"doctype": "Patient", "name": ["!=", self.name], "customer": self.customer}
):
self.update_patient_based_on_existing_customer()
else:
self.update_linked_customer()

else:
create_customer(self)

Expand Down Expand Up @@ -253,6 +249,51 @@ def update_contact(self, contact):
contact.flags.skip_patient_update = True
contact.save(ignore_permissions=True)

<<<<<<< HEAD
=======
def calculate_age(self, ref_date=None):
if self.dob:
if not ref_date:
ref_date = frappe.utils.nowdate()
diff = frappe.utils.date_diff(ref_date, self.dob)
years = diff // 365
months = (diff - (years * 365)) // 30
days = (diff - (years * 365)) - (months * 30)
return {
"age_in_string": f'{str(years)} {_("Year(s)")} {str(months)} {_("Month(s)")} {str(days)} {_("Day(s)")}',
"age_in_days": diff,
}

def update_linked_customer(self):
customer = frappe.get_doc("Customer", self.customer)
if self.customer_group:
customer.customer_group = self.customer_group
if self.territory:
customer.territory = self.territory
customer.customer_name = self.patient_name
customer.default_price_list = self.default_price_list
customer.default_currency = self.default_currency
customer.language = self.language
customer.image = self.image
customer.ignore_mandatory = True
customer.save(ignore_permissions=True)

frappe.msgprint(_("Customer {0} updated").format(customer.name), alert=True)

def update_patient_based_on_existing_customer(self):
customer = frappe.get_doc("Customer", self.customer)
self.db_set(
{
"customer_group": customer.customer_group,
"territory": customer.territory,
"default_price_list": customer.default_price_list,
"default_currency": customer.default_currency,
"language": customer.language,
}
)
self.notify_update()

>>>>>>> c5d9de2 (fix: linking existing customer to new Paitient overwrites customer name and other details (#306))

def create_customer(doc):
customer = frappe.get_doc(
Expand All @@ -271,7 +312,7 @@ def create_customer(doc):
).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)
frappe.msgprint(_("Customer {0} created and linked to Patient").format(customer.name), alert=True)


def make_invoice(patient, company):
Expand Down
16 changes: 16 additions & 0 deletions healthcare/healthcare/doctype/patient/test_patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,19 @@ def test_patient_image_update_should_update_customer_image(self):

customer = frappe.get_doc("Customer", patient.customer)
self.assertEqual(customer.image, patient.image)

def test_multiple_paients_linked_with_same_customer(self):
frappe.db.sql("""delete from `tabPatient`""")
frappe.db.set_single_value("Healthcare Settings", "link_customer_to_patient", 1)

patient_name_1 = create_patient(patient_name="John Doe")
p1_customer_name = frappe.get_value("Patient", patient_name_1, "customer")
p1_customer = frappe.get_doc("Customer", p1_customer_name)
self.assertEqual(p1_customer.customer_name, "John Doe")

patient_name_2 = create_patient(patient_name="Jane Doe", customer=p1_customer.name)
p2_customer_name = frappe.get_value("Patient", patient_name_2, "customer")
p2_customer = frappe.get_doc("Customer", p2_customer_name)

self.assertEqual(p1_customer_name, p2_customer_name)
self.assertEqual(p2_customer.customer_name, "John Doe")

0 comments on commit 849e530

Please sign in to comment.