Server Script - With Script Type - Schedular Event

I want to try to remind my employee who forgot to submit the timesheet for the working day. I am trying to achieve this functionality using a server script with a scheduler type. so it will run daily and send reminder emails who have not submitted the timesheet.

execute()

def execute():
    # send_timesheet_reminder()
    send_notification_to_self(
            subject='Timesheet Reminder Status',
            message= 'Message'
        )
    
def send_timesheet_reminder():
    frappe.log_error('Step 1')
    frappe.log('Step 1 Log')
    # Get today's date
    current_date = today()
    frappe.msgprint(current_date)
    
    # Check if today is a weekend or holiday
    if is_weekend_or_holiday(current_date):
        return

    # Get all active employees
    employees = frappe.db.get_all('Employee', filters={'status': 'Active', 'user_id': 'jaydeep@xyz.com'}, fields=['name', 'user_id'])

    for employee in employees:
        # Check if employee is on leave
        if is_employee_on_leave(employee['name'], current_date):
            continuesigzen

        # Check if timesheet is submitted for today
        if not is_timesheet_submitted(employee['name'], current_date):
            send_reminder_email(employee['user_id'])

def is_weekend_or_holiday(date):
    frappe.log_error('Step 2')
    # Check if date is a weekend (excluding 1st and 3rd Saturday)
    if frappe.utils.getdate(date).weekday() in [5, 6]:  # 5: Saturday, 6: Sunday
        # Get the day of the month
        day_of_month = frappe.utils.getdate(date).day
        if frappe.utils.getdate(date).weekday() == 5 and day_of_month not in [1, 15]:  # Exclude 1st and 3rd Saturday
            return True
        return True
    
    # Check if date is a holiday
    holiday_list = frappe.get_all('Holiday List', filters={'name': 'SZ_HL2024'}, fields=['name'])
    if holiday_list:
        holidays = frappe.get_all('Holidays', filters={'holiday_date': date, 'parent': holiday_list[0]['name']})
        if holidays:
            return True
    
    return False

def is_employee_on_leave(employee, date):
    leave_application = frappe.get_all('Leave Application', filters={
        'employee': employee,
        'from_date': ['<=', date],
        'to_date': ['>=', date],
        'status': 'Approved'
    })
    return bool(leave_application)

def is_timesheet_submitted(employee, date):
    timesheet = frappe.get_all('Timesheet', filters={
        'employee': employee,
        'start_date': ['<=', date],
        'end_date': ['>=', date],
        'docstatus': 1  # Submitted
    })
    return bool(timesheet)

def send_reminder_email(user_id):
    user = frappe.get_doc('User', user_id)
    if user.email:
        # frappe.msgprint(user.email)
        frappe.log(user.email)
        frappe.sendmail(
            recipients=[user.email],
            subject='Timesheet Submission Reminder',
            message='Please submit your timesheet for today.'
        )
        
        
def send_notification_to_self(subject, message):
    # Replace 'your_email@example.com' with your actual email address
    frappe.sendmail(
        recipients=['jaydeep@xyz.com'],
        subject=subject,
        message=message
    )

The script is executing successfully but I have not received emails. Please suggest to me how I can achieve or debug this.

1 Like

check the status of email in Email queue

Not have any list in the Email queue!

@neha This solution should work, right? but I am confused about the actual problem. Scheduled Job Type and Scheduled Job Log are created with staus completed.

Frappe Team/ Community - Please help me on this.