The code for client script but it does not work as my description

there are field name previous_copy_count, current_copy_count, free_copy, total_copy, qty for (Billed Qty), rate, amount, negative_amount and monthly_rental_charge

i want in starting or 1st time or for first invoice ‘previous copy count’ will be zero and next time it take input from ‘current copy count’ of first time and so loop going on 2.‘total copy’ =‘current copy count’ - ‘precious copy count’ 3. there are a field name ‘free copy’ which is provide first time as a fixed input.
if user use less than ‘free copy’ then ‘billed’ will be zero and ‘amount’=‘monthly rental charge’ and ‘negative amount’= ‘free copy’ - ‘total copy’ it going on until when ‘negative amount’ become zero it will not be negative
if user use more than ‘free copy’ then ‘billed’= ‘total copies’ - ‘negative amount’ then ‘amount’=‘billed’ * ‘rate’

Blockquote
frappe.ui.form.on(‘Sales Invoice Rental’, {
refresh(frm) {
// Initialize Previous copy Count to zero for the first invoice
if (!frm.doc.__islocal && frm.doc[‘previous_copy_count’] === 0) {
frm.set_value(‘previous_copy_count’, frm.doc[‘current_copy_count’]);
}

	// Calculate Total Copies as the difference between Current Copy Count and Previous Copy Count
	frm.set_value('total_copies', frm.doc['current_copy_count'] - frm.doc['previous_copy_count']);

	// Calculate Monthly Rental Charge based on Total Copies and Rate (per)
	frm.set_value('monthly_rental_charge', frm.doc['total_copies'] * frm.doc['rate']);

	// Check if user used less than Free Copy
	if (frm.doc['total_copies'] <= frm.doc['free_copy']) {
		// Billed is zero, Amount is Monthly Rental Charge, and Negative amount is Free Copy - Total Copies
		frm.set_value('billed', 0);
		frm.set_value('amount', frm.doc['monthly_rental_charge']);
		frm.set_value('negative_amount', frm.doc['free_copy'] - frm.doc['total_copies']);
	} else {
		// Billed is Total Copies - Negative amount
		frm.set_value('Billed', frm.doc['total_copies'] - frm.doc['negative_amount']);
		// Amount is Billed * Rate (per)
		frm.set_value('amount', frm.doc['billed'] * frm.doc['rate']);
		// Reset Negative amount to zero
		frm.set_value('negative_amount', 0);
	}

}});