Scheduler Event Document - Server Script

I just add 3 custom field for Doc Customer and create a client script to validate expired date each customer.

Here the code and its working well… :smiley:

frappe.ui.form.on("Customer", {
        custom_created_date: function(frm) {
        if (frm.doc.custom_created_date) {
            var newExpireDate = frappe.datetime.add_days(frm.doc.custom_created_date, 365);
            frm.set_value("custom_expired_date", newExpireDate);

            if (new Date(newExpireDate) > new Date()) { 
                frm.set_value("is_frozen", 0);
                frappe.msgprint(__("Pembuatan Member Berhasil"));
            } else {
                frm.set_value("is_frozen", 1);
                frappe.msgprint(__("Member Expired"));
            }
            frm.refresh_field("custom_expired_date");
            frm.refresh_field("is_frozen");
        }
    },
    custom_start_over: function(frm) {
        if (frm.doc.custom_start_over) {
            var newExpireDate = frappe.datetime.add_days(frm.doc.custom_start_over, 365);
            frm.set_value("custom_expired_date", newExpireDate);

            if (new Date(newExpireDate) > new Date()) { 
                frm.set_value("is_frozen", 0);
                frappe.msgprint(__("Member Berhasil Di Update"));
            } else {
                frm.set_value("is_frozen", 1);
                frappe.msgprint(__("Member Expired"));
            }
            frm.refresh_field("custom_expired_date");
            frm.refresh_field("is_frozen");
        }
    },
    validate: function(frm) {
        if (new Date(frm.doc.custom_expired_date) < new Date()) {
            frappe.validated = false;
            frappe.msgprint(__("Member Expired"));
        }
    }
});

Now my problem is, I want to add server script with Scheduler Event to check expired date each Customer.

I try with this script :

# Loop through the customers and check if trade or custom is expired
for customer in customers:
    # Get the trade expiry date and custom expiry date
    expired = customer.custom_expired_date
    # Compare with today's date
    if expired < today():
        # Trade is expired, set status as "Expired"
        frappe.db.set_value("Customer", customer.name, "status", "Frozen")
        # Send an email notification to the customer
        frappe.sendmail(
            recipients=customer.email_id,
            subject="Your trade has expired",
            message="Dear {0},\n\nWe regret to inform you that your trade with us has expired on {1}. Please contact us to renew your trade or settle your account.\n\nThank you for your cooperation.\n\nSincerely,\nYour ERPNext Team".format(customer.customer_name, trade_expiry_date)
        )

Any advice…?

@Gembira_IT_Tech

def update_customer_status():
    customers = frappe.db.get_all("Customer", fields=['name', 'custom_expired_date', 'customer_name', 
    'status', 'email_id'])

    for customer in customers:
        expired = getdate(customer.custom_expired_date)
        if expired < today():
            trade_expiry_date = {any_value or today()}
            frappe.db.set_value("Customer", customer.name, "status", "Frozen")
            frappe.sendmail(
                recipients=customer.email_id,
                subject="Your trade has been expired",
                message="Dear {0},\n\nWe regret to inform you that your trade with us has expired on {1}. 
                Please contact us to renew your trade or settle your account.\n\nThank you for your 
                cooperation.\n\nSincerely,\nYour ERPNext Team".format(customer.customer_name, 
                trade_expiry_date)
            )
cust_list = frappe.get_all('Customer', filters={'disabled':0}, fields=['name', 'customer_name', 'custom_expired_date', 'is_frozen'])
now_date = str(frappe.utils.nowdate())

for row in cust_list:
    ex_date = str(row.custom_expired_date)
    
    if row.custom_expired_date == None:
        this_cust = frappe.get_doc('Customer', row.name)
        this_cust.customer_details = "Customer ini belum di isi Tanggal Created / Expired"
        this_cust.is_frozen = 1
        this_cust.save()
        
    elif ex_date < now_date:
        this_cust = frappe.get_doc('Customer', row.name)
        this_cust.is_frozen = 1
        
        frappe.sendmail(
            recipients=this_cust.email_id,
            subject="Member Expired",
            message="Dear {0},\n\nMember Gembira anda berakhir hari ini. Dapatkan potongan 50% untuk pembuatan member baru jika anda melakukan daftar ulang dalam waktu 3 hari setelah pesan ini di terima.\n\nThank you for your cooperation.\n\nSincerely,\nYour Gembira Team")
        
        this_cust.save()
        


Thanks for help mate.
This code work for me..