Hello,
My use case is that I want to calculate a selling price based on the shipping terms (exworks, CIP, DDP) for each item in my items table in quotation. And I do that successfully for each item using the following code. The script is linked to Quotation doctype.
function calculateSellingPrice(exworks, freight, shippingTerms) {
var cip = (exworks * 1.01) + freight;
var ddp = cip * 1.05;
if (shippingTerms == "EXWORKS") {
return exworks;
} else if (shippingTerms == "CIP") {
return cip;
} else {
return ddp;
}
}
frappe.ui.form.on("Quotation Item", "qty", function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});
frappe.ui.form.on("Quotation Item", "price_list_rate", function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});
frappe.ui.form.on("Quotation Item", "discount_percentage", function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});
frappe.ui.form.on("Quotation Item", "total_margin", function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});
frappe.ui.form.on("Quotation Item", "rate", function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});
frappe.ui.form.on("Quotation Item", "freight", function(frm,cdt,cdn) {
var d = locals[cdt][cdn];
var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});
Now, I want the calculation to trigger also when I press a button on the parent doctype (quotation), or every time the grand_total field updates.
I have been searching in the form a lot but didn’t find something that helped with this one.
Looking forward for the forum’s support.
By the way, this is my first script in erpnext, and my first attempt with javascript, so please keep it simple and clear.
Regards,
Ahmed