Make_autoname series

I have developed code for automatically naming purchase orders based on specified fields. When a purchase order is created with “Service Order” selected as the order type, the naming series updates accordingly, such as “SER-ORD-2024-”. If the order type is later changed to “Work Order” for the same record, the naming series should then automatically switch to the work order series.

How can I resolve this ?

In hooks.py
doc_events = {
“Purchase Order”: {
“autoname”: “simple.simple.custom_scripts.custom_scripts.set_doc_name”,
}
}

import frappe
from frappe.model.naming import make_autoname

def set_doc_name(doc, method):
if doc.doctype == “Purchase Order”:
if doc.custom_order_type == ‘Service Order’:
new_name = make_autoname(“SER-ORD-.YYYY.-”)
doc.name = new_name
elif doc.custom_order_type == ‘Work Order’:
new_name = make_autoname(“WOR-ORD-.YYYY.-”)
doc.name = new_name

Reference:

It’s working fine sir,Thank you for your assistance