Leave Deduction Based on Server Script

Hi! I am creating a Server Script to deduct Leave based on Attendance. Like if A employee is Absent his Leave Should be Deducted. I used the following script first it was working fine

create_leave_application = False

if doc.status == "Absent":
    # Check if there are available casual or sick leaves quota
    casual_sick_leave_quota = frappe.db.get_value("Leave Allocation", {"employee": doc.employee, "leave_type": "Casual Leave & Sick Leaves"}, "total_leaves_allocated")
    annual_leave_quota = frappe.db.get_value("Leave Allocation", {"employee": doc.employee, "leave_type": "Annual Leave"}, "total_leaves_allocated")

    if casual_sick_leave_quota > 0:
        leave_type = "Casual Leave & Sick Leaves"
        create_leave_application = True
    elif annual_leave_quota > 0:
        leave_type = "Annual Leave"  # Deduct from annual leave if casual and sick leaves quota are finished
        create_leave_application = True
        frappe.msgprint(_("No leave quota available for the employee. Please contact the administrator."))

if create_leave_application:
    # Create leave application if there's available leave quota
    leave_app = frappe.get_doc({
        "doctype": "Leave Application",
        "employee": doc.employee,
        "leave_type": leave_type,
        "from_date": doc.attendance_date,
        "to_date": doc.attendance_date,
        "leave_approver": 'Administrator',
        "status": 'Approved',
        "description": 'Automatic Deduction based on Absent',
        "follow_via_email": 0

except Exception as e:
frappe.log_error(f"Error in processing leave application: {str(e)}")
frappe.msgprint(_(“An error occurred while processing leave application. Please contact the administrator. Error details have been logged.”))
now the problem i am facing is if the Casual and Sick Leaves Qouta is consumed it doesn’t deduct leaves from annual leaves instead it marks attendance as Absent.
Any Help in this regard will be appreciated.