Discount table on sales invoice

i want to make discount table, which can calculate item discount based on their choice, one item can be discounted multiple times. the discount can be by amount or by percentage. and i need to calculate that. is there anyone can help ? i already make the code, but it doesn’t work.

frappe.ui.form.on(‘Sales Invoice’, {
refresh: function(frm) {
frm.fields_dict[‘details’].grid.get_field(‘discount_type’).get_query = function(doc, cdt, cdn) {
return {
filters: [
[‘name’, ‘in’, [‘Percentage’, ‘Amount’]]
]
};
};
}
});

frappe.ui.form.on(‘Discount Detail Table’, {
add_discount_amount: async function(frm, cdt, cdn) {
var child = locals[cdt][cdn]
if (child.discount_type === ‘Percentage’) {
var curr_disc = frm.doc.discount_amount;
var discount_percentage = child.add_discount_amount;
var percentage_amount = await calculatePercentageDiscount(frm, discount_percentage);
frm.set_value(“discount_amount”, percentage_amount + curr_disc);
} else if (child.discount_type === ‘Amount’) {
var curr_disc = frm.doc.discount_amount;
var amount_discount = child.add_discount_amount + curr_disc;
frm.set_value(“discount_amount”, amount_discount);
}
},

details_remove: function(frm, cdt, cdn) {
    // Get the removed row
    var removed_row = frappe.get_doc(cdt, cdn);
    
    // Reset discount values only if the removed row is a percentage discount
    if (removed_row && removed_row.discount_type === 'Percentage') {
        frm.doc.total_discount_percentage -= removed_row.add_discount_amount;
        frm.set_value("discount_amount", frm.doc.discount_amount - removed_row.percentage_amount);
    } else if (removed_row && removed_row.discount_type === 'Amount') {
        frm.doc.total_discount_amount -= removed_row.add_discount_amount;
        frm.set_value("discount_amount", frm.doc.discount_amount - removed_row.add_discount_amount);
    }
    calculateDiscount(frm);
}

});

function calculateDiscount(frm) {
// var grands_total = frm.doc.total;
// var total_discount = 0;

// $.each(frm.doc.details, function(i, d){
//     if (d.discount_type === 'Amount') {
//         var curr_disc = frm.doc.discount_amount;
//         total_discount = grands_total - curr_disc
//         total_discount += frm.doc.discount_amount;
//     } else if (d.discount_type === 'Percentage') {
//         var curr_disc = frm.doc.discount_amount;
//         total_discount = d.add_discount_amount + curr_disc;
//         frm.set_value("discount_amount", amount_discount);
//     } 
// })
var amount_discount = 0;
var percentage_amount = 0;

$.each(frm.doc.details, async function(i, d) {
    if (d.discount_type === 'Percentage') {
        percentage_amount += await calculatePercentageDiscount(frm, d.add_discount_amount);
    } else if (d.discount_type === 'Amount') {
        amount_discount += d.add_discount_amount;
    }
});

// Set the total discount amount
frm.set_value("total_discount_amount", amount_discount + percentage_amount);
// Set the discount amount including both percentage and amount discounts
frm.set_value("discount_amount", amount_discount + percentage_amount);

// Update grand_total with the new discount amount
var net_total = frm.doc.total || 0;
var tax_total = frm.doc.total_taxes_and_charges || 0;
frm.set_value("grand_total", (net_total + tax_total) - (amount_discount + percentage_amount));

}

function calculatePercentageDiscount(frm, percentage) {
var grand_total = 0;

if (frm.doc.apply_discount_on === 'Grand Total') {
    grand_total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
} else if (frm.doc.apply_discount_on === 'Net Total') {
    grand_total = flt(frm.doc.base_net_total);
}

return (grand_total * percentage) / 100;

}

function updateTotals(frm) {
var total_discount = frm.doc.discount_amount || 0;
var net_total = frm.doc.total || 0;
var tax_total = frm.doc.total_taxes_and_charges || 0;
frm.set_value(“grand_total”, (net_total+tax_total)-total_discount);
frm.set_value(“net_total”, (net_total+tax_total)-total_discount);
frm.refresh_field(“grand_total”);
frm.refresh_field(“net_total”);
}


the table was look like this, i need to calculate whether the type is amount or percentage, and it also re-calculate when i keep changing the position or even deleting the data