# MobileAttend Overtime Workflow Update

This update changes only the attendance workflow. It does not replace or modify:

- `config.php`
- `config.local.php`
- Database credentials
- Check In or Check Out QR URLs
- Employee records or passwords
- Administrator accounts
- Existing attendance rows

## Back up first

1. In cPanel File Manager, compress the current application directory.
2. In phpMyAdmin, export the current database.
3. Keep both backups until the updated workflow has been tested.

## Step 1: Run the migration

1. Open cPanel **phpMyAdmin**.
2. Select the existing MobileAttend database.
3. Open **Import**.
4. Import:

```text
migrate_overtime_workflow.sql
```

The migration only adds these fields to the existing `attendance` table:

- `overtime_start_at`
- `overtime_end_at`
- `overtime_seconds`

It also calculates overtime for existing completed attendance records as:

```text
MAX(total_seconds - 28800, 0)
```

Run the migration once.

## Step 2: Replace existing files

Upload these files over the matching existing paths:

```text
src/functions.php
public/index.php
public/assets/app.css
templates/header.php
templates/attendance_success.php
templates/dashboard.php
templates/daily_report.php
templates/monthly_report.php
```

## Step 3: Add the new file

Create this new file:

```text
templates/overtime_offer.php
```

Do not replace any other project files.

## Step 4: Do not change QR codes

Keep the existing QR posters and URLs exactly as they are:

```text
/checkin
/checkout
```

New workflow:

```text
Check In QR + credentials
-> regular Check In

Check Out QR + credentials
-> regular Check Out

Check In QR + credentials again
-> Start Overtime button

Check Out QR + credentials
-> End Overtime

Any later Check In or Check Out
-> Attendance completed for today.
```

## Calculation rules

Standard working time is fixed at 8 hours:

```text
8 hours = 28,800 seconds
```

Total working time:

```text
regular attendance duration + optional overtime cycle duration
```

Total overtime:

```text
MAX(total working time - 8 hours, 0)
```

This means an employee who works longer than 8 hours during the regular cycle already receives overtime. An optional overtime cycle is added to total working time and overtime is recalculated.

## Automatic absence

No scheduled task or additional database row is required. The dashboard loads every active employee for the current day:

- Employees with a Check In record are shown as present.
- Employees without a Check In record are shown as absent.

The Present and Absent counters are calculated automatically whenever the dashboard opens.

## Verification

Use one test employee and complete these tests:

1. Check In succeeds.
2. A duplicate Check In is rejected.
3. Check Out succeeds.
4. Total working time appears on the dashboard.
5. Scan Check In again and confirm **Start Overtime** appears.
6. Select **Start Overtime**.
7. Scan Check Out and confirm overtime ends.
8. Confirm overtime start, overtime end, total hours, and overtime hours appear.
9. Scan either QR again and confirm:

```text
Attendance completed for today.
```

10. Confirm an employee who never checked in appears as **Absent**.
11. Confirm Daily Excel/PDF and Monthly Excel/PDF include overtime hours.

