How to add custom field to Qutation child table and calculate the line amount

I would like to add a “No. of Days” column to the Items child table in ERPNext. This column will be used in the calculation of the line Amount field for Quotations. My business provides services and rentals, and the price of each item depends on both the quantity and the number of days for which it is rented or used.

Hi @girumtibebu,

I think you should add one more field like Line Amount.
Because you can’t override the amount field calculation.

So add two the Custom Field Like

  1. No of days
  2. Line amount

And calculation for, please apply the client script.

frappe.ui.form.on('Quotation Item', {
  no_of_days: function(frm, cdt, cdn) {
    var child = locals[cdt][cdn];
    var lm = // set your bussiness logic in lm like -> child.qty * child.no_of_days * child.rate;
    frappe.model.set_value(cdt, cdn, 'line_amount', lm);
  }
});

I hope this helps.
Thank You!

1 Like

@NCP Thanks for the information and the calculation is working. However Is there a way to update the native Amount filed with Line Amount field programatically. Because I may lose the other integrations like paymnet term, tax, coupon and other calculations that depend on the native Amount field.

Hmm :thinking:

@girumtibebu, I think it’s not possible.

But you can set a logic like if the no of the day is 10 then qty is 2 then the rate is set according to no of the day and qty.

If your logic worked in do not need to add a custom field of line amount.

Thank You!

1 Like

@NCP Interesting. Can you give me direction on how to do the logic. I’m curious how this use case is normally implemented. Since many business sectors need more than quntity to make calculations for quotations. Or would you advice me to adjust my pricing calculation approach?

Update … I was able to update the builtin Amount filed with this script but when i want to use the refresh event it starts to throw error …

frappe.ui.form.on(‘Quotation Item’, {
refresh: function(frm, cdt, cdn) {
var child = locals[cdt][cdn];
var lm = child.qty * child.days * child.rate;
frappe.model.set_value(cdt, cdn, ‘amount’, lm);
}
});

Here’s the error:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘fieldname’)
at grid.js:1192:56
at Array.find ()
at Ft.update_docfield_property (grid.js:1192:42)
at Ft.toggle_enable (grid.js:717:8)
at frappe.ui.form.Controller.toggle_conversion_factor (transaction.js:1135:36)
at transaction.js:502:18

Any recomendations?

Soo @NCP After rumbling around to update the amount filed but as you said that’s hard coded.
Updating the the quanity filed with two custom fileds (no. of days and another custom quanity filed) made the trick. I hide the default quantity field and users can add quantity and number of days fileds and all the calculations work accordingly.
Thank you so much for the help.