From 743425e7110f637e5f6def0381a00f6f0f771949 Mon Sep 17 00:00:00 2001 From: Sajin SR Date: Tue, 22 Oct 2024 08:17:37 +0530 Subject: [PATCH] fix(Treatment Counselling): Add payment entry on_cancel and paid amount calculation --- .../custom_doctype/payment_entry.py | 22 ++++++++++++++----- .../treatment_counselling.py | 14 ++++++------ healthcare/hooks.py | 3 ++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/healthcare/healthcare/custom_doctype/payment_entry.py b/healthcare/healthcare/custom_doctype/payment_entry.py index a376cf5e71..6a6fd53d9d 100644 --- a/healthcare/healthcare/custom_doctype/payment_entry.py +++ b/healthcare/healthcare/custom_doctype/payment_entry.py @@ -4,9 +4,19 @@ @frappe.whitelist() def set_paid_amount_in_treatment_counselling(doc, method): if doc.treatment_counselling and doc.paid_amount: - treatment_counselling_doc = frappe.get_doc("Treatment Counselling", doc.treatment_counselling) - treatment_counselling_doc.paid_amount = doc.paid_amount - treatment_counselling_doc.outstanding_amount = ( - treatment_counselling_doc.amount - treatment_counselling_doc.paid_amount - ) - treatment_counselling_doc.save() + on_cancel = True if method == "on_cancel" else False + validate_treatment_counselling(doc, on_cancel) + + +def validate_treatment_counselling(doc, on_cancel=False): + treatment_counselling_doc = frappe.get_doc("Treatment Counselling", doc.treatment_counselling) + + paid_amount = treatment_counselling_doc.paid_amount + doc.paid_amount + if on_cancel: + paid_amount = treatment_counselling_doc.paid_amount - doc.paid_amount + + treatment_counselling_doc.paid_amount = paid_amount + treatment_counselling_doc.outstanding_amount = ( + treatment_counselling_doc.amount - treatment_counselling_doc.paid_amount + ) + treatment_counselling_doc.save() diff --git a/healthcare/healthcare/doctype/treatment_counselling/treatment_counselling.py b/healthcare/healthcare/doctype/treatment_counselling/treatment_counselling.py index 37cdd7cc66..dcb6dfce66 100644 --- a/healthcare/healthcare/doctype/treatment_counselling/treatment_counselling.py +++ b/healthcare/healthcare/doctype/treatment_counselling/treatment_counselling.py @@ -139,7 +139,12 @@ def set_treatment_plan_template_items(doc): if su_item and su_rate: doc.append( "treatment_plan_template_items", - {"type": "Item", "template": su_item, "qty": doc.expected_length_of_stay, "amount": su_rate}, + { + "type": "Item", + "template": su_item, + "qty": (doc.expected_length_of_stay or 1), + "amount": su_rate, + }, ) @@ -147,7 +152,7 @@ def set_total_amount(doc): total_price = 0 for item in doc.treatment_plan_template_items: if item.amount: - total_price += item.amount + total_price += item.qty * item.amount doc.amount = total_price @@ -164,11 +169,6 @@ def create_ip_from_treatment_counselling(admission_order, treatment_counselling) @frappe.whitelist() def create_payment_entry(treatment_counselling): - payment_entry = frappe.db.exists( - "Payment Entry", {"treatment_counselling": treatment_counselling, "docstatus": ["!=", 2]} - ) - if payment_entry: - return payment_entry treatment_counselling_doc = frappe.get_doc("Treatment Counselling", treatment_counselling) payment_entry_doc = frappe.new_doc("Payment Entry") payment_entry_doc.update( diff --git a/healthcare/hooks.py b/healthcare/hooks.py index 3951eba3a3..7d8909318b 100644 --- a/healthcare/hooks.py +++ b/healthcare/hooks.py @@ -136,7 +136,8 @@ "after_insert": "healthcare.regional.india.abdm.utils.set_consent_attachment_details" }, "Payment Entry": { - "on_submit": "healthcare.healthcare.custom_doctype.payment_entry.set_paid_amount_in_treatment_counselling" + "on_submit": "healthcare.healthcare.custom_doctype.payment_entry.set_paid_amount_in_treatment_counselling", + "on_cancel": "healthcare.healthcare.custom_doctype.payment_entry.set_paid_amount_in_treatment_counselling", }, }