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
  • Loading branch information
akurungadam authored Nov 1, 2023
1 parent 9d81901 commit c5d9de2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
51 changes: 38 additions & 13 deletions healthcare/healthcare/doctype/patient/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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 @@ -48,18 +49,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 @@ -268,6 +264,35 @@ def calculate_age(self, ref_date=None):
"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()


def create_customer(doc):
customer = frappe.get_doc(
Expand All @@ -286,7 +311,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 c5d9de2

Please sign in to comment.