Extend js script and overwrite method

Good Day, everyone, I have been having an issue with extending a method located in
erpnext> public> js> controllers> taxes_and_totals.js
Very similar to what was done here I have written my function like

frappe.provide("erpnext.public");
frappe.provide("erpnext.controllers");

console.log("123:::::::");

erpnext.taxes_and_totals = erpnext.taxes_and_totals.extend({
	setup: function(){
		;	
	},
	calculate_item_values:function() {
		var me = this;
		me.offline_pay()
		console.log(":::::CUSTOM:::::me:::::",me);
	},
	offline_pay:function(){
		var me=this;
		console.log("Called 123!!")
	},
		
})

I have also linked the file in the Hooks.py under app_include_js

I am sure the script loads as I see the first console.log message but I have not been able to override any function with my own code.
I would really appreciate any assistance I get in pointing me towards the right direction
Thanks In advance

Did you find a solution ?

Hi, Mine was a peculiar case, I found a way to wrap the function and extend it in another script.

I’ve been working off of my custom app, I found a way to overwrite core py methods, and also figured how to use new python methods within the custom scripts . I can send you my code if you want

Sounds interesting,can you overwrite only whitelisted methods?

So my use case scenario is that we have Assets. We do not do any distribution, manufacturing or anything like that, but we have equipment that we get an issue out to our personell. IE (guns, rifles, tasers, body cameras) Most of our equipment have control numbers, but they also have the manufacture serial number that we also want to track.

The problem I was facing is that I wasn’t able to track these assets that had serial numbers.

So, i found that you can override the core python methods by using a method called Monkey Patching. It took a while to play around with, but i was able to finally override core methods.

For Instance, in asset.py there is a method called validate_item

from erpnext.assets.doctype.asset.asset import Asset

# validate extended from core method
def validate(doc, method):
    validate_item(doc)

def validate_item(self):

    item = frappe.get_cached_value("Item", self.item_code,
                                   ["is_fixed_asset", "is_stock_item", "disabled"], as_dict=1)
    if not item:
        frappe.throw(_("Item {0} does not exist").format(self.item_code))
    elif item.disabled:
        frappe.throw(_("Item {0} has been disabled").format(self.item_code))

# override core validate_item attribute
def validate_item_override(doc, method):
    Asset.validate_item = validate_item

in hooks.py

    "Asset": {
# inserts the override function replacing the core with the overrided one
       "before_insert": "asset_management.asset_management.validations.asset.validate_item_override",
# extends the validate function,
       "validate": "asset_management.asset_management.validations.asset.validate"
}
2 Likes

Wonderful!, this will definitely come in handy, thanks!

1 Like