Calculate a value for custom field

Hi, guys. Thanks everybody in this thread for valuable discussion. Hope my case is not very off-topic here. I’ve modified Quality Inspection doctype so that it calculates repair rate with the help of following script:

frappe.ui.form.on("Quality Inspection", "repaired_qty", function(frm, cdt, cdn) {
       var d = locals[cdt][cdn];
       frappe.model.set_value(cdt, cdn, "repair_rate", d.repaired_qty * 100 / d.inspected_qty);
});

frappe.ui.form.on("Quality Inspection", "inspected_qty", function(frm, cdt, cdn) {
       var d = locals[cdt][cdn];
       frappe.model.set_value(cdt, cdn, "repair_rate", d.repaired_qty * 100 / d.inspected_qty);
});

Everything works great, but the problem is that I created repair_rate custom field as Data type and resulting numbers are shown like 10.256410256410257.
Is there any way to set precision in custom script? Or the only solution would be to create another custom field with Float type?

You can do this I guess

frappe.model.set_value(cdt, cdn, "repair_rate", flt(d.repaired_qty * 100 / d.inspected_qty, precision('repair_rate')));

flt(2.xxx, precision('fieldname')) should round it for you
3 Likes

Thank you, it worked!

I’m trying to calculate the total billing hrs for Sales Invoice, using this post: Sum fields amount From Child Table V7 - #25 by Randy_Lowery

But it doesn’t work…where is the problem please?

frappe.ui.form.on("Sales Invoice Timesheet", {
    billing_hours: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        var total = 0;
        frm.doc.timesheets.forEach(function(d) { total += d.billing_hours; });
        frm.set_value('total_hrs', total);
    }
});

Hello;
How I can access (get and set) the value of field in previous row of the changed row in the child table?
Regards
Bilal

Try this… Not sure if it helps
var d = locals[cdt][cdn]
var previous row = frm.doc.items[d.idx-1];

I try same code to change the rate of item in quotation but it doesnt work
pls reply

In Quotation, the item rate is automatically fetched when selecting the item code …what are you trying to accomplish?

Please explain thoroughly so we can help you

Thank you. I was looking for the script and it works for me.