From f1b5f3481850006beed3c6ed0dd810726cb26ed2 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Thu, 1 May 2025 14:36:39 +0530 Subject: [PATCH] fix: added logic to update amount, net amount, total and net total (cherry picked from commit a6323f42afadec3777a2663155350ac12baeef2b) --- crm/fcrm/doctype/crm_deal/crm_deal.js | 76 +++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.js b/crm/fcrm/doctype/crm_deal/crm_deal.js index 944ac6d4..a7f26fb5 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.js +++ b/crm/fcrm/doctype/crm_deal/crm_deal.js @@ -5,4 +5,80 @@ frappe.ui.form.on("CRM Deal", { refresh(frm) { frm.add_web_link(`/crm/deals/${frm.doc.name}`, __("Open in Portal")); }, + update_total: function (frm) { + let total = 0; + let total_qty = 0; + let net_total = 0; + frm.doc.products.forEach((d) => { + total += d.amount; + total_qty += d.qty; + net_total += d.net_amount || d.amount; + }); + + if (total) { + frappe.model.set_value(frm.doctype, frm.docname, "total", total); + } + if (total_qty) { + frappe.model.set_value( + frm.doctype, + frm.docname, + "total_qty", + total_qty + ); + } + frappe.model.set_value( + frm.doctype, + frm.docname, + "net_total", + net_total || total + ); + } +}); + +frappe.ui.form.on("CRM Products", { + products_add: function (frm, cdt, cdn) { + frm.trigger("update_total"); + }, + products_remove: function (frm, cdt, cdn) { + frm.trigger("update_total"); + }, + product_code: function (frm, cdt, cdn) { + let d = frappe.get_doc(cdt, cdn); + if (!d.product_name) { + frappe.model.set_value(cdt, cdn, "product_name", d.product_code); + } + }, + rate: function (frm, cdt, cdn) { + let d = frappe.get_doc(cdt, cdn); + if (d.rate && d.qty) { + frappe.model.set_value(cdt, cdn, "amount", d.rate * d.qty); + } + frm.trigger("update_total"); + }, + qty: function (frm, cdt, cdn) { + let d = frappe.get_doc(cdt, cdn); + if (d.rate && d.qty) { + frappe.model.set_value(cdt, cdn, "amount", d.rate * d.qty); + } + frm.trigger("update_total"); + }, + discount_percentage: function (frm, cdt, cdn) { + let d = frappe.get_doc(cdt, cdn); + if (d.discount_percentage && d.amount) { + discount_amount = (d.discount_percentage / 100) * d.amount; + frappe.model.set_value( + cdt, + cdn, + "discount_amount", + discount_amount + ); + frappe.model.set_value( + cdt, + cdn, + "net_amount", + d.amount - discount_amount + ); + } + frm.trigger("update_total"); + } });