# Leave Application Restructure Guide

This document explains the new dynamic workflow-based leave application system.

## Overview

The leave application system has been restructured to be fully dynamic and workflow-based. Instead of hardcoded approval columns (supervisor, HR), the system now uses a flexible approval system that works with any workflow configuration.

## New Table Structure

### 1. `leave_application` (Simplified)
- **Removed columns:** supervisor, supervisor_status, supervisor_comment, supervisor_date, hr_staff, hr_status, hr_comment, hr_date
- **Kept columns:** relief_staff, relief_status (still needed for relief acceptance)
- **New columns:** workflow_id, status
- **Core columns:** emp_no, type, date_from, date_to, additional_information, token, date_log, year

### 2. `leave_approvals` (New Table)
- Tracks each approval step dynamically
- Columns: id, application_token, workflow_id, step_order, position_id, approver_emp_no, status, comment, date_approved, date_created
- One record per workflow step per application

## How It Works

1. **Employee Applies for Leave:**
   - System checks employee's department
   - Finds workflow assigned to that department
   - Gets workflow steps in order
   - For each step:
     - Finds line manager from `staff_position` table with matching position_id and department_id
     - Creates approval record in `leave_approvals` table
     - Sends notification to line manager

2. **Approval Process:**
   - Each line manager receives notification for their step
   - Approvals must happen in order (step 1, then step 2, etc.)
   - When all required steps are approved, application status changes to 'approved'

3. **Relief Staff:**
   - Relief staff acceptance/decline still works separately
   - Uses relief_staff and relief_status columns in leave_application

## SQL Scripts to Run

1. **leave_application_restructure.sql**
   - Creates `leave_approvals` table
   - Removes old columns from `leave_application`
   - Adds workflow_id and status columns

2. **staff_position_table.sql** (if table doesn't exist)
   - Creates `staff_position` table
   - Links employees to positions and departments

## Staff Position Table

The `staff_position` table structure:
- id (primary key)
- emp_no (employee number)
- position_id (foreign key to positions table)
- department_id (foreign key to departments table)

This table is used to find line managers for each workflow step.

## Finding Line Managers

When a leave application is submitted:
1. System gets workflow steps for the employee's department
2. For each step, finds a line manager by:
   - Matching position_id from workflow step
   - Matching department_id from employee's department
   - Ensuring employee is active
   - Excluding the applicant

## Notifications

- Relief staff: Receives notification to accept/decline relief request
- Line managers: Receive notifications for their approval step
- Notifications include link to approval page with token and step number

## Migration Notes

⚠️ **Important:** Before running the restructure script:
1. Backup your `leave_application` table
2. If you have existing leave applications, you may need to migrate data
3. The script will drop columns - make sure you don't need the old data

## Benefits

- **Flexible:** Works with any workflow configuration
- **Scalable:** Can add/remove approval steps without code changes
- **Department-specific:** Each department can have different approval chains
- **Position-based:** Uses actual positions from staff_position table
- **Dynamic:** No hardcoded approval roles

