# Leave Management System - Setup Guide

## Overview
A complete Leave Management System built with Laravel Blade (MVC architecture only). This system manages multi-level leave request approvals with role-based access control.

## ✨ Features
- **Multi-level Approval Workflow**: Student → Training Officer → Warden → Assistant Director
- **Role-based Access**: 5 different user roles (student, teacher, training_officer, warden, assistant_director, director)
- **Complete Leave Tracking**: Apply, track, and view leave request status in real-time
- **Approval Remarks**: Every approver can add remarks at their level
- **Notifications**: Database and in-app notifications for all status changes
- **Admin Dashboard**: Complete overview of all leave requests

## 🚀 Setup Instructions

### Prerequisites
- PHP 8.1+
- MySQL 5.7+
- Composer
- Laravel 10+

### Step 1: Install Dependencies
```bash
composer install
npm install
```

### Step 2: Setup Environment
```bash
cp .env.example .env
php artisan key:generate
```

Configure your database in `.env`:
```
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=leave_management
DB_USERNAME=root
DB_PASSWORD=
```

### Step 3: Run Migrations
```bash
php artisan migrate
```

### Step 4: Seed Database
```bash
php artisan db:seed
```

This creates test users with the following credentials:
- **Student**: student@example.com / password
- **Training Officer**: training_officer@example.com / password
- **Warden**: warden@example.com / password
- **Assistant Director**: asst_director@example.com / password
- **Director**: director@example.com / password

### Step 5: Run Development Server
```bash
php artisan serve
```

Visit: http://localhost:8000

## 📁 Project Structure

```
app/
├── Http/
│   ├── Controllers/
│   │   ├── DashboardController.php      # Dashboard routing
│   │   ├── StudentLeaveController.php   # Student leave operations
│   │   ├── ApprovalController.php       # Approval workflow
│   │   └── NotificationController.php   # Notification management
│   ├── Middleware/
│   │   └── RoleMiddleware.php           # Role-based access control
│   └── Kernel.php                       # Middleware configuration
├── Models/
│   ├── User.php                         # User model with roles
│   ├── Student.php                      # Student profile
│   ├── Teacher.php                      # Teacher profile
│   ├── LeaveRequest.php                 # Leave request model
│   ├── ApprovalLog.php                  # Approval history
│   └── Notification.php                 # Notifications
├── Services/
│   └── LeaveApprovalService.php         # Core approval logic
└── Providers/
    └── AppServiceProvider.php

database/
├── migrations/
│   ├── *_add_role_to_users_table.php
│   ├── *_create_students_table.php
│   ├── *_create_teachers_table.php
│   ├── *_create_leave_requests_table.php
│   ├── *_create_approval_logs_table.php
│   └── *_create_notifications_table.php
└── seeders/
    └── DatabaseSeeder.php               # Test data

resources/
├── views/
│   ├── layouts/
│   │   └── app.blade.php               # Main layout
│   ├── dashboard/
│   │   ├── student.blade.php
│   │   ├── approver.blade.php
│   │   └── admin.blade.php
│   ├── student/
│   │   ├── apply-leave.blade.php
│   │   ├── leave-history.blade.php
│   │   └── leave-detail.blade.php
│   ├── approval/
│   │   └── review.blade.php
│   └── notifications/
│       └── index.blade.php
└── js/
    └── app.js

routes/
└── web.php                              # All application routes
```

## 👥 User Roles & Permissions

### 1. **Student**
- ✅ Apply for leave
- ✅ View own leave requests
- ✅ View approval status
- ✅ View remarks from approvers

### 2. **Training Officer (Level 1)**
- ✅ View pending leave requests
- ✅ Approve/Reject leaves
- ✅ Forward to Warden
- ✅ Add remarks

### 3. **Warden (Level 2)**
- ✅ View requests from Training Officer
- ✅ Approve/Reject leaves
- ✅ Forward to Assistant Director
- ✅ Add remarks

### 4. **Assistant Director (Level 3)**
- ✅ View requests from Warden
- ✅ Final approval/rejection
- ✅ Add remarks
- ✅ Notify students

### 5. **Director (Admin)**
- ✅ View all leave requests
- ✅ View system reports
- ✅ Manage users

## 🔄 Leave Approval Workflow

```
┌─────────────────────────┐
│   Student Apply Leave   │
└────────────┬────────────┘
             │
             ▼
┌─────────────────────────────────────┐
│  Training Officer (Level 1)         │
│  [Pending → Forwarded/Rejected]     │
└────────────┬────────────────────────┘
             │
      If Approved
             │
             ▼
┌─────────────────────────────────────┐
│  Warden (Level 2)                   │
│  [Forwarded → Forwarded/Rejected]   │
└────────────┬────────────────────────┘
             │
      If Approved
             │
             ▼
┌─────────────────────────────────────┐
│  Assistant Director (Level 3)       │
│  [Forwarded → Approved/Rejected]    │
└────────────┬────────────────────────┘
             │
             ▼
        ✅ Leave Approved
        (Student Notified)
```

## 📊 Database Schema

### users table
- id, name, email, password, role, email_verified_at, remember_token, created_at, updated_at

### students table
- id, user_id, roll_no (unique), course, hostel_details, parent_contact, created_at, updated_at

### teachers table
- id, user_id, department, designation, created_at, updated_at

### leave_requests table
- id, student_id, leave_reason, from_date, to_date
- status (pending/forwarded/approved/rejected)
- current_level (1/2/3)
- applied_at, created_at, updated_at

### approval_logs table
- id, leave_request_id, level, approver_id, decision (approved/rejected)
- remarks, approved_at, created_at, updated_at

### notifications table
- id, user_id, leave_request_id, message, is_read, created_at, updated_at

## 🛣️ Key Routes

### Public
```
GET  /                    # Welcome page
```

### Authentication (Laravel Breeze)
```
GET  /login               # Login
POST /login               # Process login
GET  /register            # Register
POST /register            # Process registration
POST /logout              # Logout
```

### Dashboard
```
GET  /dashboard           # Auto-route to role-specific dashboard
```

### Student Routes
```
GET  /leave/apply         # Apply leave form
POST /leave               # Store leave request
GET  /leave/history       # View leave history
GET  /leave/{id}          # View leave details
```

### Approval Routes
```
GET  /approvals           # View pending requests
GET  /approvals/{id}      # Review specific request
POST /approvals/{id}/process  # Process decision
```

### Notification Routes
```
GET  /notifications                        # View all notifications
POST /notifications/{id}/mark-as-read      # Mark as read
POST /notifications/mark-all-as-read       # Mark all as read
GET  /notifications/unread-count           # Get unread count
DELETE /notifications/{id}                 # Delete notification
```

## 🔐 Security Features

✅ CSRF Protection (via `@csrf` in forms)
✅ Role-based Middleware (RoleMiddleware)
✅ Password Hashing (bcrypt)
✅ SQL Injection Prevention (Eloquent ORM)
✅ Authentication Checks (auth()->check())
✅ Authorization Checks (role verification)

## 🎨 UI Components

The system uses **Tailwind CSS** for styling with:
- Responsive grid layouts
- Color-coded status badges
- Professional forms
- Data tables with hover effects
- Modal-style cards
- Clear navigation

## 📝 Form Validations

### Leave Request Form
- **leave_reason**: Required, minimum 10 characters
- **from_date**: Required, must be today or later
- **to_date**: Required, must be on or after from_date

### Approval Form
- **decision**: Required (approved/rejected)
- **remarks**: Optional, max 500 characters

## 🔔 Notifications

Users receive notifications for:
1. Leave request approved to next level
2. Leave request rejected
3. Leave request finally approved
4. New pending leave request (for approvers)

## 🚨 Common Issues & Solutions

### Migration Errors
```bash
# Drop all tables and re-migrate
php artisan migrate:refresh --seed
```

### Routes Not Working
```bash
# Clear route cache
php artisan route:clear
```

### Still Getting Errors
```bash
# Full cache clear
php artisan cache:clear
php artisan config:clear
```

## 📚 Additional Commands

```bash
# Create new controller
php artisan make:controller ControllerName

# Create new model
php artisan make:model ModelName

# Create new migration
php artisan make:migration migration_name

# Run specific seeder
php artisan db:seed --class=DatabaseSeeder

# Show all routes
php artisan route:list

# Check logs
tail -f storage/logs/laravel.log
```

## 🎓 Learning Points

This system demonstrates:
- ✅ MVC Architecture
- ✅ Eloquent ORM Relationships
- ✅ Service Classes for business logic
- ✅ Middleware for authorization
- ✅ Request Validation
- ✅ Blade templating
- ✅ Database migrations
- ✅ Database seeding
- ✅ Routing (groups, middleware)
- ✅ Model relationships (HasOne, HasMany, BelongsTo)

## 📄 License

This project is open source and available under the MIT License.

## 🤝 Support

For issues or questions, please check the code comments or create an issue in the repository.

---

**Happy Coding!** 🚀
