Custom Script Refresh on Duplicate Document


I use the following script to calculate the sum of quantities in that sales invoice.

When I start a new Sales Invoice, it does the calculation all right. But not when an existing sales invoice is duplicated. Can I make the script to work even when a document is duplicated? Total Qty is a custom field I have added for this purpose.



var calculate_total_qty = function(frm) {
    var total_qty = frappe.utils.sum(
        (frm.doc.items || []).map(function(i) { return i.qty; })
    frm.set_value("total_qty", total_qty);

frappe.ui.form.on("Sales Invoice Item", "qty", function(frm, cdt, cdn) {
    calculate_total_qty(frm, cdt, cdn);

Add this, so that the qty is recalculated on every refresh.

frappe.ui.form.on("Sales Invoice", "refresh", function(frm) {

Yep. Worked like a charm!

Thanks much!!