Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: linking existing customer to new Patient overwrites Customer Name (backport #306) #309

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 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,35 @@ def update_contact(self, contact):
contact.flags.skip_patient_update = True
contact.save(ignore_permissions=True)

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 @@ -271,7 +296,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")
Loading