Hi!
I need your help.
Doctype: Sales Invoice.
In the Items child table (sales invoice Item) I have a custom field called (serial_number_final_weight).
I need to custom the calculation of the Amount in the sales invoice item to calculate ( rate* serial_number_final_weight )
Currently, The Amount is calculated ( rate * qty )
the serial_number_final_weight is automated when I select a Serial no
What would you advice me to do?
1 Like
Hello, for example you can do before_save event that will loop on each item and does (amount = rate * my_custom_field)
not tested example:
for(var i = 0; i < cur_frm.doc.items.length;i++) {
cur_frm.doc.items[i].amount = cur_frm.doc.items[i].rate * my_custom_field
}
would this be in the client script ?
yes, here is an example of how to use with before_save event, not tested
frappe.ui.form.on('Sales Invoice', {
before_save: function(frm) {
for(var i = 0; i < cur_frm.doc.items.length;i++) {
if(frm.doc.items[i].serial_number_final_weight){
frm.doc.items[i].amount = (frm.doc.items[i].rate * frm.doc.items[i].serial_number_final_weight);
}
}
}
})
I will Give it a try and tell you if it works
It worked for me I just did a little adjustment to your code :
frappe.ui.form.on('Sales Invoice', {
after_save: function(frm) {
$.each(frm.doc.items, function(i, d) {
if(d.serial_number_final_weight){
d.amount = (d.rate * d.serial_number_final_weight);
}
});
}
});
I had to do it after_save in order to override the core function in:
erpnext/controllers/taxes_and_totals.py → calculate_item_values();
as this function calculate the rate * qty directly
However, I am facing an Issue in calculating the grand total and taxes and Discount after the above action. Meaning that only the Amount is changed but the Total for example is still calculated on the sum of (rate * qty)
any recommendation? as I think that I need to override that method of calculation in core file of erpnext
2 Likes
hello, you can create global counter and add to it the total of your rate*my_custom_field so after the loop end you update the grand total. this may not be ideal because there can be other calculations too
you can also check cur_frm.script_manager.trigger
with this you can trigger table field which will recalculate everything for you,