ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

How to override method in frappe?


#1

Hello,
I am doing some customisation in POS?
I want to add vehicle number and customer type in POS.

I have successfully updated popup and javascript using javascript prototype and javascript class extend method. I have maintained this in another custom app using page_js hook.

Now, I am stuck at erpnext python code overriding?
In some cases we can use hooks before insert, after insert to write own python code, but this is not possible for current case as data is coming from local storage.

Is it possible to override exiting python methods?
How to access local storage in python?


#2

Why not start contributing fixes instead :smile:


#3

@rmehta The complete feature will be enable bold fields in pos.
or
For this current fix, we just need to update below code. and loop data (data is local storage customer list here)
Please advise on it.

def add_customer(data):
	customer_doc = frappe.new_doc('Customer')
	customer_doc.customer_name = data.get('full_name') or data.get('customer')
	customer_doc.customer_pos_id = data.get('customer_pos_id')
	customer_doc.customer_type = 'Company'
	customer_doc.vehicle_no = data.get('vehicle_no')
	customer_doc.customer_group = get_customer_group(data)
	customer_doc.territory = get_territory(data)
	customer_doc.flags.ignore_mandatory = True
	customer_doc.save(ignore_permissions = True)
	frappe.db.commit()
return customer_doc.name

#4

In custom_app module_name.py

import frappe
from erpnext.accounts.doctype.sales_invoice.sales_invoice import SalesInvoice

def shoutout(self):
	print("Yay!")

def before_cancel(self):
	self.shoutout()
	self.update_time_sheet(None)

def build_my_thing():
	SalesInvoice.shoutout = shoutout
	SalesInvoice.before_cancel = before_cancel

:hammer: python console :hammer:

In [1]: from custom_app.module_name import build_my_thing

In [2]: build_my_thing()

In [3]: sales_invoice = frappe.get_doc("Sales Invoice", "SINV-00165")

In [4]: sales_invoice.shoutout()
Yay!

In [5]: sales_invoice.cancel()
Yay!

more:


Override Standard validate method of any doctype
How to override method in erpnext
How to Override py method in erpnext v9
Best approach to edit ERPNext python doctypes
#5

@revant_one
Thanks for the solution. I have marked this as solution as its working fine for overriding class method.

However this will not work in my case, as I need to override method which doesn’t belong to any class.


#6

@revant_one I have been looking for such a method for a while. This is great pointer.
Quick Question - How is this loaded in the ERPNext context? Would you call build_my_thing() function on load of the core doctype? I’m no able to grasp when the bench would load the overridden function…


#7

call build_my_thing just before you need to do overridden action.
onload, before frappe.get_doc or frappe.new_doc.

also, just like classes same thing is possible with modules.
Try it in bench console


#8

Thanks @revant_one I tried using befoe_insert and onload. It works, but works inconsistently. Sometimes the custom method is called, but then other times, original method is called…doctype needs to be reloaded to call the custom method again…I dont know when it happens/ any thoughts?
Thanks.


#9

Continued here


#10