In my current project we need to modify the default calculation of ‘Purchase Order Item’ table, which is a Child table of ‘Purchase Order’. By default each ‘Amount’ field of the purchase order item is calculated by multiplying the ‘Quantity’ field and ‘Rate’ field of that row. Here is the default calculation - Amount = Quantity * Rate
But in our project we had to introduce some custom fields, like Kg Per Drum, Conversion Rate, Utility etc. And we need all of them to be involved on the final amount calculation for every item row of the table. This is our Purchase Order Item Table. I am giving the Expected Calculations Below -
from __future__ import unicode_literals
from pydoc import doc
def items_table_calculation(doc, action):
final_amount = 0
for row in doc.items:
row.total_weight = row.qty * row.kg_per_drum
row.inmon = row.total_weight / row.conversion
row.amount = (row.inmon * row.rate) + row.utility
final_amount += row.amount
The problem is, after saving, child tables calculation shows ok but in the Total and Grand Total show qty*rate calculations result. How can I show my customized calculated amount in these fields?
And I figured it out that this calculation is handled by the taxes and totals.py and taxes and totals.js file. But if we change the taxes and totals.py file, the change doesn’t show immediately. And we really don’t want to change the core files here, so we need to know how we can override the functions here?
There aren’t hooks to override arbitrary python, so you’d have to find where in the Purchase Order controller those methods from taxes_and_totals.py are getting called. It’ll get messy, as there’s a fair bit of inheritance going on, but it’s doable.
Like I said in my last post, there’s no way to override arbitrary functions. Your options are:
Edit the source code of ERPNext directly, which is straightforward but requires you to manually merge changes on every version update.
Use the override_doctype_class hook to create a custom controller for Purchase Orders, which you can load from a custom app. This has the advantage of not getting overwritten on every update. The disadvantage is that you won’t be able to edit taxes_and_totals.py directly, but you can find the places where purchase_order.py calls taxes_and_totals.py and replace those calls with calls to other methods.