Override amount calculation


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:


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?

Continuing the discussion from Override amount calculation:

Hello, brother this link is not working now… I am beginner in erpnext

will you please help to solve the similar issue I am facing …
my scenario is that I want to calculate
amount = rate * stock_qty
in sales order as well as purchase order …

how we can override calc_item_value() function using custom app

You may review this How to override method in frappe? - #4 by revant_one

This solution talked about doctype class what I need it a core function override