Event listener in erpnext

Hey, I have a customized the function that calculates amount in quotation since I added other fields, but when it comes Sales Taxes and Charges part in quotation it doesn’t change. I have tried some solutions so I want to know where can I find event listener or something equivalent? so the function can be triggered when I change those fields (width, height)
this is my code (client script)

function calculateAmount(frm, cdt, cdn) {
    setTimeout(function() {
        var item = locals[cdt][cdn];
        var length = item.length;
        var width = item.width;
        var quantity = item.qty;
        var rate = item.rate;

        // Calculate the item amount
        var amount = length * rate * width * quantity;
        frappe.model.set_value(cdt, cdn, 'amount', amount);

        // Calculate the total of items
        var total = 0;
        frm.doc.items.forEach(function(item) {
            total += item.amount;
        });
        frappe.model.set_value('Quotation', frm.doc.name, 'total', total);

        // Calculate the total tax amount based on tax rates
        var taxTotal = 0;
        for (var i = 0; i < frm.doc.taxes.length; i++) {
            var tax = frm.doc.taxes[i];
            var currentTaxAmount = 0;

             if (tax.charge_type === "Actual") {
                // Distribute the tax amount proportionally to each item row
                var actual = tax.tax_amount;
                currentTaxAmount = (total * actual) / frm.doc.net_total || 0.0;
            } else if (tax.charge_type === "On Net Total") {
                currentTaxAmount = (tax.rate / 100.0) * total;
            } else if (tax.charge_type === "On Previous Row Amount") {
                currentTaxAmount = (tax.rate / 100.0) * frm.doc.taxes[i - 1].base_tax_amount;
            } else if (tax.charge_type === "On Previous Row Total") {
                currentTaxAmount = (tax.rate / 100.0) * frm.doc.taxes[cint(tax.row_id) - 1].base_total;
            } else if (tax.charge_type === "On Item Quantity") {
                currentTaxAmount = tax.rate * frm.doc.total_qty;
            }
            
            console.log(currentTaxAmount)

            frappe.model.set_value('Sales Taxes and Charges', tax.name, 'tax_amount', currentTaxAmount);
            taxTotal += currentTaxAmount;
        }

        // Calculate total related fields
        var grandTotal = total + taxTotal;
        var baseGrandTotal = grandTotal * frm.doc.conversion_rate;
        var roundedTotal = Math.round(grandTotal);

        // Update the document fields
        frappe.model.set_value('Quotation', frm.doc.name, 'grand_total', grandTotal);
        frappe.model.set_value('Quotation', frm.doc.name, 'base_grand_total', baseGrandTotal);
        frappe.model.set_value('Quotation', frm.doc.name, 'total_taxes_and_charges', taxTotal);
        frappe.model.set_value('Quotation', frm.doc.name, 'rounded_total', roundedTotal);

        // Log the calculated values
        console.log('total', total, 'grandTotal', grandTotal, 'baseGrandTotal', baseGrandTotal, 'taxTotal', taxTotal, 'rounded total', roundedTotal);
    }, 1000);
}

frappe.ui.form.on('Quotation Item', {
    length: function(frm, cdt, cdn) {
        calculateAmount(frm, cdt, cdn);
    },
    width: function(frm, cdt, cdn) {
        calculateAmount(frm, cdt, cdn);
    },
    qty: function(frm, cdt, cdn) {
        calculateAmount(frm, cdt, cdn);
    },
    rate: function(frm, cdt, cdn) {
        calculateAmount(frm, cdt, cdn);
    },
    refresh: function(frm, cdt, cdn) {
        calculateAmount(frm, cdt, cdn);
    },
});

Hi @imo,

First thing, you can’t override the base amount field calculation because the amount field is connected with lots of methods like total tax calculation, shipping cost, etc.

It will need a hard-core customization

Thank You!

1 Like

but I did override the method calculate_item_values (the method that calculates the amount) on the server side and the client side. My only problem is on the client side of Sales Taxes and Charges. When saving the document, it does save the correct amount and calculations, but before saving, changing in items fields does change in all other fields except for Sales Taxes and Charges fields. That’s where I’m stuck; it is completely working only for this part.