Skip to content

Commit

Permalink
fix: Add Discount Amount field to show payment dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Irfan778899 committed Nov 1, 2023
1 parent 472d1e0 commit 6f21ac8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
];

Expand Down Expand Up @@ -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: __(`<svg class="icon icon-sm" style="">
<use class="" href="#icon-printer"></use>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 6f21ac8

Please sign in to comment.