# Phase 3–5 Update Guide

## Overview

This package upgrades your live Phase 1+2 installation with:

| Phase | What's Added |
|-------|-------------|
| **Phase 3** | Chart of Accounts, 7 Dimensions, Customers, Vendors, Currencies, Tax Codes |
| **Phase 4** | Items, Item Categories, Warehouses, UOMs, Price Levels |
| **Phase 5** | Accounting Vouchers (6 types), Ledger Entries, Approval Workflow |

---

## What This Package Contains

```
saas-accounting-phase3-to-5/
├── app/
│   ├── Http/Controllers/Company/
│   │   ├── Accounting/   (COA, Dimensions, Vouchers controllers — NEW)
│   │   ├── Inventory/    (Items, Categories, Warehouses, UOMs — NEW)
│   │   └── Masters/      (Customers, Vendors, Currencies, Tax, PriceLevels — NEW)
│   ├── Models/           (All new models: Item, Voucher, LedgerEntry, etc.)
│   └── Services/         (VoucherPostingService — NEW)
├── database/
│   ├── migrations/
│   │   └── 2024_01_02_000001_phase3_to_phase5_tables.php  ← NEW MIGRATION
│   └── seeders/
│       └── DatabaseSeeder.php  ← UPDATED (adds Phase 3-5 demo data)
├── resources/views/
│   ├── accounting/       (COA, Dimensions, Vouchers views — NEW)
│   ├── inventory/        (Items, Categories, Warehouses views — NEW)
│   └── masters/          (Customers, Vendors, Currencies, Tax views — NEW)
└── routes/
    └── web.php           ← UPDATED (Phase 3-5 routes added)
```

> **Note:** The original Phase 1+2 mega-migration is NOT re-run.
> A new dedicated migration `2024_01_02_*` adds only the new tables using
> `Schema::hasTable()` guards — safe to run on your existing database.

---

## Step-by-Step Update Process

### Step 1 — Take a Database Backup

**In cPanel → phpMyAdmin:**
1. Select your database (`duiclick_erp`)
2. Click **Export** → Quick → Format: SQL → **Go**
3. Save the `.sql` file somewhere safe

Or via SSH:
```bash
mysqldump -u duiclick_hafij -p duiclick_erp > backup_before_phase35_$(date +%Y%m%d).sql
```

---

### Step 2 — Upload the Files

**Option A — cPanel File Manager (easiest):**
1. Open **File Manager** → navigate to `public_html/saas-accounting/`
2. Click **Upload** → upload `saas-accounting-phase3-to-5.zip`
3. Right-click the zip → **Extract** → extract to `public_html/saas-accounting/`
4. When asked about overwriting, click **Yes to All**

**Option B — FTP:**
Upload and overwrite all files from this package into `public_html/saas-accounting/`

---

### Step 3 — Run the Migration

Connect via SSH (or cPanel Terminal):

```bash
cd ~/public_html/saas-accounting

# Run ONLY the new migration (safe — uses hasTable() guards)
php artisan migrate --force

# You should see:
#   Migrating: 2024_01_02_000001_phase3_to_phase5_tables
#   Migrated:  2024_01_02_000001_phase3_to_phase5_tables
```

---

### Step 4 — Run the Seeder (adds Phase 3-5 demo data)

```bash
php artisan db:seed --force

# This uses firstOrCreate() throughout — safe on existing data.
# Adds: Dimensions, Price Levels, Tax Codes, Sample Customers,
#       Vendors, UOMs, Warehouses, Items, Voucher permissions
```

---

### Step 5 — Clear Cache

```bash
php artisan optimize:clear
php artisan optimize

# Or individually:
# php artisan config:clear
# php artisan route:clear
# php artisan view:clear
# php artisan cache:clear
```

---

### Step 6 — Verify

Open your browser and check:

| URL | Expected |
|-----|----------|
| `duiclick.com/login` | Login page loads |
| After login → Sidebar | See: Chart of Accounts, Dimensions, Customers, Vendors, Items, Vouchers |
| `duiclick.com/accounting/accounts` | Chart of Accounts tree |
| `duiclick.com/masters/customers` | Customer list |
| `duiclick.com/inventory/items` | Items list |
| `duiclick.com/accounting/vouchers/cash_payment` | Cash Payment voucher list |

---

## Credentials (unchanged)

| Role | Login URL | Company | Username | Password |
|------|-----------|---------|----------|----------|
| Super Admin | `/sadmin/login` | — | `admin@saasaccounting.com` | `Admin@1234` |
| Demo Admin | `/login` | `DEMO` | `admin` | `Demo@1234` |
| Demo Accountant | `/login` | `DEMO` | `accountant` | `Demo@1234` |

---

## Troubleshooting

**"Migration already ran" error:**
The new migration has `hasTable()` guards. If you see this, the tables were
already created by a previous run — run `php artisan migrate:status` to check.

**"Class not found" error:**
Run `php artisan optimize` to rebuild the class autoloader.

**Blank page / 500 error:**
```bash
cat storage/logs/laravel.log | tail -50
```

**Routes not working:**
```bash
php artisan route:clear && php artisan route:cache
```

**Views not updating:**
```bash
php artisan view:clear
```

---

## Rollback (if needed)

```bash
# Rollback the Phase 3-5 migration only
php artisan migrate:rollback --step=1

# This drops: dimensions, dimension_values, vouchers, voucher_lines,
# voucher_approvals, ledger_entries, stock_movements, item_price_levels,
# item_serial_numbers, item_lot_numbers, transaction_attachments,
# audit_logs (if created here), and all AR/AP sub-tables
```

Then restore your database backup from Step 1.

---

*Generated by SaaS Accounting Builder — Phase 3-5 Package*
