Child Table Calculation

This may help:

//The following three scripts calculates the rate/amount based on quantity.
frappe.ui.form.on("Material Request Item", {
      rate: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(d.doctype, d.name, 'amount', (d.qty * d.rate));
        var total = 0;
        frm.doc.items.forEach(function(d) {
            total += d.amount;
        });
        frm.set_value('total_amount', total);
    },
      qty: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(d.doctype, d.name, 'amount', (d.qty * d.rate));
        var total = 0;
        frm.doc.items.forEach(function(d) {
            total += d.amount;
        });
        frm.set_value('total_amount', total);
    },
      amount: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(d.doctype, d.name, 'rate', (d.amount / d.qty));
        var total = 0;
        frm.doc.items.forEach(function(d) {
            total += d.amount;
        });
        frm.set_value('total_amount', total);
    }
});
1 Like