# Income & Expense – Laravel Migration Summary

## 1. Configuration

- **`.env`** – Configured for MySQL: `DB_CONNECTION=mysql`, `DB_DATABASE=im_db`, `DB_USERNAME=root`, `DB_PASSWORD=` (empty)

## 2. Database Migrations (25 tables)

Migrations in `database/migrations/` ordered by foreign key dependencies:

| Order | Migration | Table |
|-------|-----------|-------|
| 1 | 0001_01_01_000000 | password_reset_tokens, sessions (users table removed from default) |
| 2 | 2025_03_05_100001 | companies |
| 3 | 2025_03_05_100002 | branches |
| 4 | 2025_03_05_100003 | zones |
| 5 | 2025_03_05_100004 | sub_zones |
| 6 | 2025_03_05_100005 | employees |
| 7 | 2025_03_05_100006 | income_categories |
| 8 | 2025_03_05_100007 | expense_categories |
| 9 | 2025_03_05_100008 | accounts |
| 10 | 2025_03_05_100009 | users (password_hash, role enum, branch_id) |
| 11 | 2025_03_05_100010 | permissions |
| 12 | 2025_03_05_100011 | user_permissions |
| 13 | 2025_03_05_100012 | payment_types |
| 14 | 2025_03_05_100013 | borrower_profiles |
| 15 | 2025_03_05_100014 | loan_receivables |
| 16 | 2025_03_05_100015 | incomes |
| 17 | 2025_03_05_100016 | expenses |
| 18 | 2025_03_05_100017 | balance_transfers |
| 19 | 2025_03_05_100018 | loans |
| 20 | 2025_03_05_100019 | loan_payments |
| 21 | 2025_03_05_100020 | loan_collections |
| 22 | 2025_03_05_100021 | income_receive_employees |
| 23 | 2025_03_05_100022 | income_transfer_to_admins |
| 24 | 2025_03_05_100023 | menus |

## 3. Users Table

- `name`, `email`, `password_hash` (replaces `password`), `role` enum (`admin`/`employee`), `branch_id`, `created_at`
- No `email_verified_at`, `remember_token`, or `updated_at`

## 4. Eloquent Models (22)

- **Company**, **Branch**, **Zone**, **SubZone**, **Employee**
- **IncomeCategory**, **ExpenseCategory**, **Account**
- **User** (custom auth via `password_hash`, `getAuthPassword()`)
- **Permission**, **UserPermission**
- **Income**, **Expense**, **BalanceTransfer**
- **Loan**, **LoanPayment**, **LoanCollection**
- **PaymentType**, **BorrowerProfile**, **LoanReceivable**
- **IncomeReceiveEmployee**, **IncomeTransferToAdmin**
- **Menu**

All relationships are defined.

## 5. Auth System

- **LoginController** – `showLoginForm`, `login`, `logout`
- Session-based auth using Laravel’s default guard
- Custom `User` model using `password_hash` (bcrypt)
- **Login view** – `resources/views/auth/login.blade.php`
- Credentials: `admin@example.com` / `password` (from seeder)

## 6. Middleware

- **EnsureUserHasPermission** – Checks role or `user_permissions` for given permission codes
- **EnsureAdmin** – Restricts access to `role === 'admin'`
- Aliases: `permission`, `admin`

## 7. Layout & Menu

- **`resources/views/layouts/app.blade.php`** – Sidebar layout with user badge
- Sidebar built from **menus** table via `MenuService`
- `MenuHelper` maps legacy `.php` URLs to Laravel routes
- Admin-only and permission-based visibility respected

## 8. Controllers

| Controller | Routes |
|------------|--------|
| DashboardController | `/dashboard` |
| IncomeController | `/incomes`, `/incomes/create` |
| ExpenseController | `/expenses`, `/expenses/create` |
| TransferController | `/transfers`, `/transfers/create` |
| AccountController | `/accounts`, `/accounts/create` |
| CompanyController | `/companies` |
| IncomeCategoryController | `/income-categories` |
| ExpenseCategoryController | `/expense-categories` |
| BranchController | `/branches` |
| ZoneController | `/zones` |
| SubZoneController | `/sub-zones` |
| EmployeeController | `/employees`, `/employees/create` |
| UserController | `/users` |
| LoanReceivableController | `/loan-receivables`, `/loan-receivables/create` |
| LoanCollectionController | `/loan-collections` |
| LoanPaymentController | `/loan-payments` |
| LoanCollectionReportController | `/loan-collection-report` |
| IncomeReceiveEmployeeController | `/income-receive-employees` |
| IncomeTransferToAdminController | `/income-transfer-to-admins` |
| MenuController | `/menus` |
| PermissionController | `/permissions` |

## 9. Routes

- **`routes/web.php`** – All routes protected with `auth` middleware
- Permission-based access via `permission:` middleware
- Admin-only routes via `admin` middleware
- Login: `GET/POST /login`, Logout: `GET/POST /auth/logout`

## 10. Seeders

- **DataSeeder** – Branches, income/expense categories, companies, accounts, admin user
- **PermissionSeeder** – 24 permissions from `db_permissions_migration.sql`
- **MenuSeeder** – 27 menu items from `db_menu_seed.sql`

## Commands

```bash
php artisan migrate:fresh --drop-views   # Fresh migrations
php artisan db:seed                       # Seed data
php artisan serve                         # Run dev server
```

## Login

- **Email:** admin@example.com  
- **Password:** password
