Math custom script

Hello, I have 2 fields in a Child Table( “unit” and “packing”) and the field " total" in doctype “factor_table”.
What I am trying to do is to have the following formula cantitate (unit * packing) = total.
And then sum of all total Cells Display in a row named (Sum total) in doctype “factor”.

Thank you for your help

Hi,
I’m newbie to erpnext and frappe framework also.
but I think your problem could be fix via client script. I just test code below and it works as expected.

frappe.ui.form.on('Your Child Table Name', {
	unit(frm,cdt, cdn) {
		let doc=   locals[cdt][cdn];
		doc.total=doc.unit*doc.packing;
	    frm.refresh_field('Your child table field name');
	}

    packing(frm,cdt, cdn) {
		let doc=   locals[cdt][cdn];
		doc.total=doc.unit*doc.packing;
	    frm.refresh_field('Your child table field name');
	}
})

here document I read.
(203) Assigning a Value to a Child Table Field in Frappe or ERPNext - YouTube

Form Scripts (frappeframework.com)

3 Likes

@erfaneh_sahraii First, Welcome to the forum. Second, the way provided by @Pheakdey_Tes1 is right. :slightly_smiling_face:

Hello, thank you dear friend , I wrote the script but not worked :upside_down_face:
Do I have to type something in the doctype?

Please make sure you write client script in factor doctype and make sure you change child table name and child table field name correctly.

Should I have written this script for doctype or its child?

In doctype, not in child table.

it did not work :smiling_face_with_tear:

Hi @erfaneh_sahraii,

Please apply it.

frappe.ui.form.on("Child Table Name", {
// Example: Child Table name like if Sales Invoice have a child table like Sales Invoice Item then set a custom/client script child table name.
// frappe.ui.form.on("Sales Invoice Item", {
    unit: function(frm,cdt,cdn) {
        var doc = locals[cdt][cdn];
        frappe.model.set_value(cdt, cdn, "total", (doc.unit * doc.packing));
        // above script does not work then apply below script.
        // frappe.model.set_value(cdt, cdn, "total", parseFloat(doc.unit * doc.packing));
    },
    packing: function(frm,cdt,cdn) {
        var doc = locals[cdt][cdn];
         frappe.model.set_value(cdt, cdn, "total", (doc.unit * doc.packing));
        // above script does not work then apply below script.
        // frappe.model.set_value(cdt, cdn, "total", parseFloat(doc.unit * doc.packing)));
    }
    
});

How to apply script for check below image for just example.

Please set your table name and field name according.
Then reload and check it, please.

Thank You!

2 Likes

@NCP I learn a lot from your previous post and solution. I try my best to learn from official document site, but I cannot find some syntax like frappe.model.set_value.

Could you please provide me link that I can learn more about this kind of feature?

Thanks,

Hello, thank you for your help.
I wrote the script you said. When I enter the selling department and create our first invoice, it calculates and displays the total in the column, but when I save, all the data in the column will be deleted. How can I solve this problem?
Then, how can I write a script that displays the sum of the total column in the sum total row.

I think this document will help you

Client Script (frappeframework.com) Calculate incentives on a Sales Invoice

@Pheakdey_Tes1
I saw This link
https://frappeframework.com/docs/v13/user/en/desk/scripting/client-script#34-customize-field-properties
But I think it is only related to incentives.
Do I have to write a parent script in Ddoctype or child to calculate the sum of the total column and show in the sum totla row?

@erfaneh_sahraii The client script should be on parent doctype.
Lets assume that the sum field name is total_sum and child table name is items, then the code be

frappe.ui.form.on('Sales Invoice',  {
    validate: function(frm) {
        let total_sum = 0;
        $.each(frm.doc.items, function(i, d) {
            total_sum += flt(d.total)
        });
        frm.doc.total_sum = total_sum;
    }
});

Note I haven’t tested the code yet.
I hope that I was helpful.

Hello , my parent doctype name is (Factor) and my child table name is (Factor_table).
the sum field name is (sum_total). So I wrote my script like this
frappe.ui.form.on(‘Factor’, {
validate: function(frm) {
let sum_total = 0;
$.each(frm.doc.Factor_table, function(i, d) {
sum_total += flt(d.total)
});
frm.doc.sum_total = sum_total;
}
});
but it didn’t work. :slightly_frowning_face:

Hi,
You code look fine for me. Event validate is raise when you save your document.
You must save your document to get update to field sub_total.

@erfaneh_sahraii What @Pheakdey_Tes1 said is right.
But if you want it to be immediate then the code must be changed, assuming the child table title is Factor Table and it’s name is Factor_table

function updateSumTotal(frm) {
    let sum_total = 0;
    $.each(frm.doc.Factor_table, function(i, d) {
        sum_total += flt(d.total)
    });
    frm.doc.set_value('sum_total', sum_total);
}
frappe.ui.form.on(‘Factor’, {
    sum_total: updateSumTotal,
    validate: updateSumTotal,
});
frappe.ui.form.on(‘Factor Table’, {
    Factor_table_add: updateSumTotal,
    Factor_table_remove: updateSumTotal,
});

This code will update the sum_total field immediately, before save and after adding and remmoving child table rows.

2 Likes