# Equipment Maintenance System (EMS) - Project Context & Documentation

This document serves as the central knowledge base for the Equipment Maintenance System. It outlines the core architecture, user roles, business logic, key files, and the recent development progress to ensure we have a unified context moving forward.

## 1. Project Overview
The **Equipment Maintenance System (EMS)** is a web application built using **Laravel** (Backend) and **Vue.js with Inertia.js** (Frontend). It is designed to manage hardware and software maintenance requests, equipment inventory, and employee assignments across various branches and directorates.

The core functionality revolves around a structured **Ticket Approval Workflow**, where a complaint or demand passes through multiple authorization layers before being resolved.

---

## 2. Roles, Permissions & Abilities System

Instead of checking hardcoded roles (e.g., `if (role == 'Clerk')`), the application uses a dynamic **Abilities (Middleware & Gates)** system. This allows the Super Admin to grant or revoke specific capabilities to users regardless of their exact role title.

### Core Abilities
- `create-tickets`: Can create new maintenance tickets (Typically **Clerks**).
- `approve-tickets`: Can approve or reject tickets forwarded to them (Typically **Directors, HR, Secretary, Addl IT Directors, IT Directors**).
- `process-tickets`: Can view all tickets, route them, manage inventory, and generate minute sheets (Typically **IT System Admins**).
- `resolve-tickets`: Can view assigned tasks and mark them as resolved (Typically **Technicians**).
- `manage-users`: Full system control, branch management, and user management (Typically **Super Admins**).

---

## 3. The Core Workflow (Ticket Lifecycle)

The system handles both **Complaints/Services** (repairing existing assets) and **New Demands** (requesting new assets).

1. **Ticket Creation (Clerk)**
   - A Clerk logs into the portal and creates a "New Performa" for a specific employee.
   - They specify the issue category, asset details, and description.
   - Initial Status: `Pending_Director`

2. **Branch Approval (Director / HR)**
   - The respective Directorate's Director logs in and reviews the ticket.
   - They can Approve or Reject.
   - If Approved, Status becomes: `Pending_IT_Admin` (or `Pending_ICT`).

3. **IT Processing & Routing (IT Admin)**
   - IT Admin reviews the approved ticket.
   - Based on the requirement and estimated cost, the IT Admin routes the ticket:
     - **Direct to Technician:** If there is no financial cost involved.
     - **Secretary Approval (Petty Cash):** If the estimated cost is ≤ 10,000 PKR, it is sent to the Secretary for financial sanction.
     - **Minute Sheet (Major Expense):** If the estimated cost is > 10,000 PKR, a Minute Sheet is initiated. It goes to the **Addl Dir (ICT)** for recommendation and then to the **IT Director** for final approval.

4. **Approval & Execution**
   - Once financial approval is secured (from Secretary or IT Director), the task is assigned to a Technician.

5. **Technician Resolution (Technician)**
   - Technicians view their assigned tasks.
   - They perform the repair/installation, log their remarks, and mark it as Resolved.
   - Status becomes: `Pending_Verification`

5. **Final Verification (Clerk / IT Admin)**
   - The creator (Clerk) verifies that the issue has been successfully resolved.
   - Once verified, the ticket is officially closed.
   - Final Status: `Resolved`

---

## 4. Key Files and Architecture

### Backend (Laravel)
- **`routes/web.php`**: The central routing file. It groups routes based on the dynamic abilities (`can:create-tickets`, `can:approve-tickets`, etc.) rather than hardcoded roles.
- **`app/Providers/AppServiceProvider.php`**: Configures the `Gate::before` logic, which checks the `user_abilities` column in the database to grant access dynamically.
- **Controllers:**
  - `TicketController.php`: Handles the bulk of the ticket lifecycle (creation, approvals, processing by IT Admin, verification).
  - `TechnicianController.php`: Handles Technician specific logic (viewing assigned tasks, logging emergency tasks, marking resolved).
  - `SuperAdminController.php`: Handles CRUD operations for users, employees, assets, branches, and directorates.
- **Database Seeders (`DatabaseSeeder.php`)**: Contains the foundational data (Roles, Branches, Directorates, Assets) and all existing system users (Super Admin, Clerks, Directors, Technicians, IT Admin, etc.) so that the environment can be easily rebuilt (`php artisan migrate:fresh --seed`).

### Frontend (Vue.js / Inertia / TailwindCSS)
- **`resources/js/Layouts/AuthenticatedLayout.vue`**: The global layout containing the Sidebar Navigation. The sidebar links are conditionally rendered based on the user's abilities (e.g., `can('create-tickets')`).
- **Dashboard Views:**
  - `Clerk/Dashboard.vue`: Where clerks create tickets and view pending actions.
  - `Approver/Dashboard.vue`: Generic approval queue for Directors, IT Directors, and HR.
  - `ItAdmin/Dashboard.vue` & `ProcessTicket.vue`: Complex UI for IT Admin to route tickets, assign technicians, and generate minute sheets.
  - `Technician/Dashboard.vue`: Task list for technicians.
  - `SuperAdmin/Dashboard.vue`: The "System Control Center" for managing branches, directorates, users, and assets.

---

## 5. Recent Changes & Fixes (Where we are now)

In the most recent sessions, we performed significant stabilization and refactoring:

1. **Dynamic Abilities Refactor:**
   - Moved away from rigid role-based routing (which caused overlapping route issues) to an abilities-based middleware system (`can:approve-tickets`, etc.).
   - Unified the "Approvers" flow so that HR, Branch Directors, and IT Directors share the same foundational approval routes and histories.

2. **White Screen (JS Error) Fixes:**
   - Users (Clerks, Directors) were facing blank white screens due to frontend crashes.
   - **Fix 1:** Added optional chaining (`|| ''`) to computed search functions in Vue files (e.g., `t.case_no.toLowerCase().includes()`) to prevent crashes when a value was null.
   - **Fix 2:** Updated `AuthenticatedLayout.vue` to use the correct route names (`clerk.history` and `director.history`) because invalid `route()` calls were causing Ziggy to crash the entire Vue layout on load.

3. **Super Admin UI Formatting:**
   - Modified the "System Users" and "Employees" tables in `SuperAdmin/Dashboard.vue` to prominently display the **Directorate Name** with the **Branch Name** directly beneath it in smaller parentheses.

4. **Data Consistency & Seeding:**
   - Identified missing users and inconsistencies (e.g., Addl IT Director's email).
   - Updated `DatabaseSeeder.php` to include all currently active users (M Amjad, Faheem, Dilawar, Secretary, etc.) and reset all default passwords to `password` for easy testing environments.
   - Pushed all cleaned-up code (`routes/web.php` formatting) to GitHub.

---

## 6. Next Steps
The core foundation is now completely stable, routing is secure, UI glitches are resolved, and the database is perfectly seeded. We are now ready to tackle the **"Major Changes"** phase of the project.
