How To make a custom script for addition , multiplication and division

Hello all,
I want to make a custom script for calculating total cost of all selected features in the table plus the additional cost.


So it should go like this first all the features cost should be converted for monthly unit (if it is yearly divide cost by 12, if it is weekly multiply cost by 4.3)
and then sum them all up and show them in Feature sum cost field.
And finally add the additional cost and put it in total cost field.
If any one can help I would appreciate it.
I am beginner in ERPnext, learning it and also not good in java script so it’s a bit hard for me but still trying my best.
Thank you for your time.

1 Like

I just tried this code for simply adding all feature cost.
frappe.ui.form.on(“Feature list”, “feature_cost”, function(frm, cdt, cdn) {

var material_details = frm.doc.feature_id;
var total = 0
for(var i in material_details) {
total = total + material_details[i].feature_cost
}

frm.set_value("sum_cost",total)

});
but it is showing like this 120100015000 instead of summing them.
what should I do in it?

Hi @Devershi_Vashistha,

Please try it.

frappe.ui.form.on('Plan',  {
    validate: function(frm) {
        total = 0;
        $.each(frm.doc.feature_id,  function(i,  d) {
            total += flt(d.feature_cost);
        });
        frm.doc.sum_cost = total;
    } 
});

Set your field name and table field name according to.
Then reload and check it, please.

Thank You!

1 Like

Hello @NCP I tried it and nothing is changing in Feature sum cost field (sum_cost).


And also for some reason save button is not working in plan when I am creating new plan to test it.
After disabling the script it is working.

Hi @Devershi_Vashistha,

Please again check your variable name or field name.

For reference code:

frappe.ui.form.on('Sales Invoice',  {
    validate: function(frm) {
        // calculate incentives for each person on the deal
        total_incentive = 0
        $.each(frm.doc.sales_team,  function(i,  d) {
            // calculate incentive
            var incentive_percent = 2;
            if(frm.doc.base_grand_total > 400) incentive_percent = 4;
            // actual incentive
            d.incentives = flt(frm.doc.base_grand_total) * incentive_percent / 100;
            total_incentive += flt(d.incentives)
        });
        frm.doc.total_incentive = total_incentive;
    } 
})

Thanks.

Do i need to enter child table dcotype or main in starting?

No. Please check reference code.

Thank you @NCP for that but I am not able to understand much from it.
I have created
child table doctype = Feature list
Child table field names = feature_name, feature_cost, feature_currency, feature_unit
current doctype where i use child table = Plan
table filed name in Plan form = feature_id
field to get sum in = sum_cost

Hi @Devershi_Vashistha,

Please apply and check it.

frappe.ui.form.on('Plan', {
    validate: function(frm,cdt,cdn) {
        set_sum_cost(frm);
    }
});

function set_sum_cost(frm) {
    var doc = locals[frm.doc.doctype][frm.doc.name];
    var sum_cost = 0;
    $.each(doc.feature_id, function(i, d) {
        sum_cost += flt(d.feature_cost);
    });

frm.set_value("sum_cost",sum_cost);
}

I tested and it’s working on my side.

When clicking the save button, the total set in sum_code (parent field) automatically.

Thank You!

Thanks @NCP
It is working for me now.
I have other queries as well
I also want to be able to convert the feature_cost to monthly unit (check feature_unit if it is yearly divide feature_ cost by 12, if it is weekly multiply cost by 4.3)
And then do the adding of cost as you mentioned above
How can that be calculated?

I think, first add more custom field feature_id table like final_feature_ cost.

Set a calculation in the child table like when click feature_unit then divide calculation set in final_feature_ cost and then final_feature_ cost total set in sum_cost.

So can try it.

Ok I am trying it
If I face any difficulties I will ask you again.
Thanks.

Hi @NCP I tried it I added script for child table and added a new field monthly_cost
But It is showing value 0
This is the script
frappe.ui.form.on(‘Feature list’, function(frm, cdt, cdn) {

var unit, cost;
unit = frm.doc.feature_unit;
cost = frm.doc.feature_cost;

if (unit === “weekly”) {
cost = cost * 4.3;
}

if (unit === “yearly”) {
cost = cost / 12;
}

frm.set_value(“monthly_cost”,cost)
});
Can you help?

Hi @Devershi_Vashistha,

Please apply and check it.

frappe.ui.form.on("Feature list", "feature_unit", function(frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    if(d.feature_unit == "Yearly"){
        d.monthly_cost = flt(d.feature_cost / 12);
    }
    else if(d.feature_unit == "Weekly"){
        d.monthly_cost = flt(d.feature_cost / 4.3);
    }
    else{
        d.monthly_cost = flt(d.feature_cost);
    }
    frm.refresh_fields('feature_id');
});

When click feature_unit then automatically set value in the monthly_cost field.
Set your variable or field name according.

Then after in the above script some changes like:

sum_cost += flt(d.feature_cost);

//Replace
sum_cost += flt(d.monthly_cost);

Then reload and check it.

Thank You!

Thank you so much @NCP
Everything is working now.
Is there a way to update the sum_cost while we are adding features because the field is not visible while we are editing and is only visible once we hit save.

No Option @Devershi_Vashistha.

Only try. if comfortable.
Otherwise, please set read-only for sum_cost because anyone cannot change the value.

Thanks.

Ok Thank you @NCP