From 6f21ac81d54d093d0ff06f8c79f6c3f1e9240429 Mon Sep 17 00:00:00 2001 From: Mohammed Irfan Date: Tue, 17 Oct 2023 15:56:56 +0530 Subject: [PATCH] fix: Add Discount Amount field to show payment dialog --- .../patient_appointment.js | 40 +++++++++++++------ .../patient_appointment.py | 12 ++++-- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/healthcare/healthcare/doctype/patient_appointment/patient_appointment.js b/healthcare/healthcare/doctype/patient_appointment/patient_appointment.js index 10e3bd44dc..534886793d 100644 --- a/healthcare/healthcare/doctype/patient_appointment/patient_appointment.js +++ b/healthcare/healthcare/doctype/patient_appointment/patient_appointment.js @@ -896,6 +896,12 @@ let make_payment = function (frm, automate_invoicing) { fieldname: "consultation_charge", fieldtype: "Currency", read_only: true, + }, + { + label: "Discount Amount", + fieldname: "discount_amount", + fieldtype: "Currency", + default: 0, } ]; @@ -936,21 +942,29 @@ let make_payment = function (frm, automate_invoicing) { fields: fields, primary_action_label: "Create Invoice", primary_action(values) { - frm.set_value("mode_of_payment", values.mode_of_payment) - frm.save(); - frappe.call({ - method: "healthcare.healthcare.doctype.patient_appointment.patient_appointment.invoice_appointment", - args: { "appointment_name": frm.doc.name }, - callback: async function (data) { - if (!data.exc) { - await frm.reload_doc(); - if (frm.doc.ref_sales_invoice) { - d.get_primary_btn().attr("disabled", true); - d.get_secondary_btn().attr("disabled", false); + if (values.consultation_charge >= values.discount_amount) { + frappe.call({ + method: "healthcare.healthcare.doctype.patient_appointment.patient_appointment.invoice_appointment", + args: { + "appointment_name": frm.doc.name, + "mode_of_payment": values.mode_of_payment, + "discount_amount": values.discount_amount + }, + callback: async function (data) { + if (!data.exc) { + await frm.reload_doc(); + if (frm.doc.ref_sales_invoice) { + d.get_field("mode_of_payment").$input.prop("disabled", true); + d.get_field("discount_amount").$input.prop("disabled", true); + d.get_primary_btn().attr("disabled", true); + d.get_secondary_btn().attr("disabled", false); + } } } - } - }); + }); + } else { + frappe.throw(__("Discount Amount should be less than or equal to Consultation Charge")) + } }, secondary_action_label: __(` diff --git a/healthcare/healthcare/doctype/patient_appointment/patient_appointment.py b/healthcare/healthcare/doctype/patient_appointment/patient_appointment.py index b38b64dfc5..a7092adf01 100755 --- a/healthcare/healthcare/doctype/patient_appointment/patient_appointment.py +++ b/healthcare/healthcare/doctype/patient_appointment/patient_appointment.py @@ -394,8 +394,9 @@ def check_payment_reqd(patient): @frappe.whitelist() -def invoice_appointment(appointment_name): +def invoice_appointment(appointment_name, mode_of_payment, discount_amount): appointment_doc = frappe.get_doc("Patient Appointment", appointment_name) + appointment_doc.mode_of_payment = mode_of_payment show_payment_popup = frappe.db.get_single_value("Healthcare Settings", "show_payment_popup") free_follow_ups = frappe.db.get_single_value("Healthcare Settings", "enable_free_follow_ups") @@ -411,11 +412,11 @@ def invoice_appointment(appointment_name): fee_validity = None if show_payment_popup and not appointment_doc.invoiced and not fee_validity: - create_sales_invoice(appointment_doc) + create_sales_invoice(appointment_doc, discount_amount) update_fee_validity(appointment_doc) -def create_sales_invoice(appointment_doc): +def create_sales_invoice(appointment_doc, discount_amount): sales_invoice = frappe.new_doc("Sales Invoice") sales_invoice.patient = appointment_doc.patient sales_invoice.customer = frappe.get_value("Patient", appointment_doc.patient, "customer") @@ -426,6 +427,7 @@ def create_sales_invoice(appointment_doc): item = sales_invoice.append("items", {}) item = get_appointment_item(appointment_doc, item) + appointment_doc.paid_amount = flt(item.amount) - flt(discount_amount) # Add payments if payment details are supplied else proceed to create invoice as Unpaid if appointment_doc.mode_of_payment and appointment_doc.paid_amount: @@ -434,6 +436,9 @@ def create_sales_invoice(appointment_doc): payment.mode_of_payment = appointment_doc.mode_of_payment payment.amount = appointment_doc.paid_amount + # Set discount amount in invoice equal to discount amount entered in payment popup + sales_invoice.discount_amount = flt(discount_amount) + sales_invoice.set_missing_values(for_validate=True) sales_invoice.flags.ignore_mandatory = True sales_invoice.save(ignore_permissions=True) @@ -446,6 +451,7 @@ def create_sales_invoice(appointment_doc): "invoiced": 1, "ref_sales_invoice": sales_invoice.name, "paid_amount": appointment_doc.paid_amount, + "mode_of_payment": appointment_doc.mode_of_payment, }, ) appointment_doc.reload()