Need guidance with server script

Actually we want validation in server script, but we don’t familiar where such type of coding should be done in the erpnext ?

So kindly guide us on how shall we write the validation code as our below requirement details.

if a user has not allowed the role of " System Manager " then,we need to stop or validate that target warehouse should not be ( “CANTEEN SHANTIKUNJ (Production) 30121 - UK-TMD”, “CANTEEN SRIRAM PURAM (Production) 30121 - UK-TMD”, “CANTEEN GAYATRIKUNJ (Production) 30121 - UK-TMD”, “JAL PAN GRUH RAW MATERIAL - UK-TMD” ) . When processing it from " stock summary " for transferring or moving the stock from the source into target warehouse(sale counter).

Code:

where should we write this code properly?

i=0
roles = frappe.get_all(“Has Role”, fields = [“role”], filters={“parent”: frappe.session.user, “role”: (“not in”, [“All”, “Guest”])})
user_roles = [r.role for r in roles]
warehouse_list = [“CANTEEN SHANTIKUNJ (Production) 30121 - UK-TMD”,
“CANTEEN SRIRAM PURAM (Production) 30121 - UK-TMD”, “CANTEEN GAYATRIKUNJ (Production) 30121 - UK-TMD”,
“JAL PAN GRUH RAW MATERIAL - UK-TMD”]

while i < len(doc.items):
if “System Manager” not in user_roles and doc.items[i].t_warehouse in warehouse_list:
msg = "You don’t have permission to make stock transfer entry against warehouse: " + doc.items[i].t_warehouse
frappe.msgprint(msg, raise_exception=True)
i = i + 1

Thanks and Regards,
Shivam Shukla

can you format the code well so if anyone wants to help you can read well written question

We are trying below code in doctype “stock entry”, but it does not work. May be we are not aware with server script.

So what am i doing wrong?


@frappe.whitelist()
def validate_stock_summary():
        i=0
        roles = frappe.get_all("Has Role", fields = ["role"], filters={"parent": frappe.session.user, "role": ("not in", ["All", “Guest”])})
        user_roles = [r.role for r in roles]
        warehouse_list = ["CANTEEN SHANTIKUNJ (Production) 30121 - UK-TMD","CANTEEN SRIRAM PURAM (Production) 30121 - UK-TMD",  “CANTEEN GAYATRIKUNJ (Production) 30121 - UK-TMD”,“JAL PAN GRUH RAW MATERIAL - UK-TMD”]

        while i < len(doc.items):
                if "System Manager" not in user_roles and doc.items[i].t_warehouse in warehouse_list:
                        msg = "You don't have permission to make stock transfer entry against warehouse: " + doc.items[i].t_warehouse
                        frappe.msgprint(msg, raise_exception=True)
        i = i + 1

if you want to validate anything on particular doctype(model) there is std trigger frappe provides validate you can wite those conditions under and throw from there. you can check any doctype code

eg sales_invoice.py