How can i input items that are zero cost or its free

how can i input items that are zero cost or its free… it has serial numbers too where should i put them?
i have a code right here please check it below it seems it does work. i am confuse also where should i put this code in purchase receipt item or purchase invoice item?

frappe.ui.form.on(“Purchase Invoice Item”,“rate”, function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
var v_item=frappe.get_doc(cdt,cdn);
if(d.rate ==0){
frappe.model.set_value(“Purchase Invoice Item”, d.v_item, “item_code”, “FREEBIES”);
}
});