# Employee Exit Workflow

This document explains how the employee exit process is handled in TAF. It describes workflow initiation, required approvals, the clearance checklist, final settlement actions, and knowledge transfer steps.

## Initiation
1. HR or the employee's manager submits a record in the `employee_exits` table.
2. When the record is created the controller triggers the `employee.exit_initiated` event. The `WorkflowEngine` listens for this event and can start department notifications or other automated tasks.

## Approvals
1. Approval rows are created in `exit_approvals` for each department or manager that must sign off.
2. Approvers update their row status to `approved` or `declined` via the API.
3. The exit cannot progress to clearance until all required approvals are complete.

## Clearance Checklist
1. Items assigned to the employee are tracked in `exit_clearances`.
2. Each department confirms assets are returned and accounts are revoked.
3. The outstanding asset list can be retrieved via `GET /api/employee_exits/{id}/assets`. If no clearance rows exist, the endpoint pulls assignments from `employee_items` to prefill the list.
4. Once every clearance item is marked as returned and access revoked, HR calls the `completeClearance` endpoint. This triggers `employee.exit_completed` through the `WorkflowEngine`.

## Final Settlement
1. When clearance is completed the system calculates any outstanding salary, unused leave payouts and deductions.
2. Results are saved to the `final_settlements` table and a `employee.exit.final_settlement` workflow event notifies Finance.
3. Payroll can reference the record to process payment and then archive the employee.

## Knowledge Transfer
1. A meeting may be scheduled using the `exit_interviews` table.
2. Interview questions and responses are stored in `exit_feedback` to capture lessons learned and ensure handover of responsibilities.

## Setup Steps
1. Run the database scripts in `TAFDB.sql` to create the following tables:
   - `employee_exits`
   - `exit_approvals`
   - `exit_clearances`
   - `exit_interviews`
   - `exit_feedback`
2. Ensure these tables are accessible to the application user.
3. Grant permissions to HR managers, department heads, IT, Admin, and Legal as outlined in `docs/RoleMapping.md`.

## Workflow Engine Integration
- `employee.exit_initiated` is emitted when an exit record is created. The `WorkflowEngine` uses this event to notify HR, IT, Admin, Finance, and Legal.
- `employee.exit_completed` is emitted after clearance is finished. The engine updates any outstanding tasks and can run custom workflows defined in the `workflows` table.
