Try ERPNext Buy Support Partners Foundation

Override amount calculation

Hi,

I want to override calculation of amount field (purchase order item, etc)
Default calculation as I know is amount = rate * qty

Is it possible to add a custom field and use that as additional variable for calculating amount. For example, I add handling field and the amount is calculated using rate * qty + handling.

Other answer tells to use Doc Events, but the posting date is a bit old (2016). If I must use Doc Events hook, do I still need to recalculate other fields (taxes, grand total, etc) ?

Thank you.

I think it can be done by custom script :slight_smile:

https://erpnext.org/docs/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples

I have tried something like this:

// bussiness logic here
final_amount = item.amount + handling;
frappe.model.set_value(cdt, cdn, "amount", final_amount);

But when I do save or submit document, amount is reverted back to original value (qty*rate).

On which doctype you are doing calculations?

Purchase Order (Purchase Order Item). If it works, I will do on Purchase Invoice too.

then it is Override by controllers .
taxes_and _totals.js
taxes_and _totals.py

1 Like

Thank you, it works.

Hello! How you did it with js script? (before saving)

On public/js/controller/taxes_and_totals.js, there is a calculate_item_values function that calculates rate*qty. You can override it (via custom script).

so how to do that could you give me code example?