ReferenceError: total_packets is not defined at validate

Dear Users,
The below custom script was working till version 10 and when it was upgraded to v12 it gives error. Please help.

frappe.ui.form.on("Sales Invoice", {
	validate: function(frm) {
        total_packets = 0;
        total_actual= 0;		
        total_qty = 0;
        total_charged=0;
        total_gr_ch = 0;
        total_loading = 0;
    	total_door_delivery = 0;
        total_barrier_charges = 0;
        total_green_tax =0;
        total_toll_tax = 0;
        total_unloading_charges = 0;
        total_door_pickup = 0;
        total_other_charges = 0;
        total_misc_charges = 0;
        if (frm.doc.naming_series.substr(0,4)==="ALPL" || frm.doc.naming_series.substr(0,4)==="TEST"){

//-----------------------------Green Tax/Loading/GR Charges------------------
        $.each(frm.doc.items || [], function (i, d) {
               
                d.gr_ch = flt(d.gr_applicable_charges);
                d.barrier_charges=flt(d.barrier_applicable_charges);
                d.door_delivery = flt(d.dd_applicable_charges);
                d.door_pickup = flt(d.dp_applicable_charges);
                if (d.lloading_applicable) {
                    if (d.calculation_type === "FTL") {
                        d.labour_loading = flt(d.gr_actual_qty * d.labour_rate_per_kg);
                    } else if (d.calculation_type === "Packets") {
                        d.labour_loading = flt(d.qty * d.labour_rate_per_packet);
                    } else if (d.calculation_type === "Weight") {
                        d.labour_loading = flt(d.qty * d.labour_rate_per_kg);
                    }
                }
                if (d.unloading_applicable) {
                    if (d.calculation_type === "FTL") {
                        d.unloading_charges = flt(d.gr_actual_qty * d.labour_rate_per_kg);
                    } else if (d.calculation_type === "Packets") {
                        d.unloading_charges = flt(d.qty * d.labour_rate_per_packet);
                    } else if (d.calculation_type === "Weight") {
                        d.unloading_charges = flt(d.qty * d.labour_rate_per_kg);
                    }
                }

                if (d.green_tax_applicable) {
                    if (d.calculation_type === "FTL") {
                        d.green_tax = flt(d.qty * 1400);
                    } else if (d.calculation_type === "Packets") {
                        if (d.qty >= 1 && d.qty <= 50) {
                            d.green_tax = 100;
                        } else if (d.qty >= 51 && d.qty <= 100) {
                            d.green_tax = 250;
                        } else if (d.qty >= 101 && d.qty <= 200) {
                            d.green_tax = 500;
                        } else if (d.qty > 200) {
                            d.green_tax = 1400;
                        }
                    } else if (d.calculation_type === "Weight") {
                        if (d.qty >= 1 && d.qty <= 500) {
                            d.green_tax = 100;
                        } else if (d.qty >= 501 && d.qty <= 1000) {
                            d.green_tax = 250;
                        } else if (d.qty >= 1001 && d.qty <= 2000) {
                            d.green_tax = 500;
                        } else if (d.qty > 2000) {
                            d.green_tax = 1400;
                        }
                    }
                } else {
                   d.green_tax = d.gt_applicable_charges;
                }

        });