# Deployment Guide — Shared Hosting + Subdomain

## Quick Start

1. **Upload the zip file** to your hosting account (e.g., into `public_html/` or a subdirectory)
2. **Extract** the zip — it will create an `erp/` folder
3. **Create a database** in cPanel (MySQL Databases) — note the DB name, user, and password
4. **Copy `.env.example` to `.env`** and edit with your hosting details (see below)
5. **Run setup commands** via SSH or cPanel Terminal (see below)
6. **Configure subdomain** in cPanel to point to `erp/public`

---

## Step-by-Step

### 1. Upload & Extract

- Upload `erp-deploy.zip` to your hosting account
- Extract it — you'll get an `erp/` folder
- The folder structure should be: `public_html/erp/` (or wherever your subdomain root is)

### 2. Create Database (cPanel)

1. Go to **cPanel > MySQL Database Wizard**
2. Create a database (e.g., `youruser_erp`)
3. Create a user and grant **ALL PRIVILEGES**
4. Note: database name, username, password

### 3. Configure .env

Copy `.env.example` to `.env` and edit:

```env
APP_NAME="ERP"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://erp.yourdomain.com

DB_DATABASE=youruser_erp
DB_USERNAME=youruser_dbuser
DB_PASSWORD=YourStrongPassword

MAIL_MAILER=smtp
MAIL_HOST=mail.yourdomain.com
MAIL_PORT=587
MAIL_USERNAME=noreply@yourdomain.com
MAIL_PASSWORD=YourEmailPassword
MAIL_FROM_ADDRESS=noreply@yourdomain.com
```

### 4. Run Setup Commands

If your hosting has **SSH access** or **cPanel Terminal**:

```bash
cd public_html/erp

# Generate app key
php artisan key:generate

# Run migrations + seed
php artisan migrate --force
php artisan db:seed --force

# Build assets (if not already built)
# (Assets are pre-built in the zip, skip if you don't have Node.js)

# Cache for production
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

# Set permissions
chmod -R 775 storage
chmod -R 775 bootstrap/cache
```

If **no SSH access**, use the **alternative method** below.

### 5. Configure Subdomain (cPanel)

1. Go to **cPanel > Subdomains**
2. Create subdomain: `erp.yourdomain.com`
3. Set **Document Root** to: `public_html/erp/public`
4. Save

### 6. SSL Certificate

1. Go to **cPanel > SSL/TLS > Manage SSL sites**
2. Enable **Let's Encrypt** for `erp.yourdomain.com`
3. Auto-renewal should be enabled

### 7. Login

- Visit: `https://erp.yourdomain.com`
- Login: `admin@erp.test` / `password`
- **CHANGE PASSWORD IMMEDIATELY** after first login

---

## Alternative: No SSH Access

If your shared hosting doesn't have SSH or Terminal:

### Database Migration
1. Open `phpMyAdmin` from cPanel
2. Select your database
3. Click **Import**
4. Upload the file `database/sql/erp_schema.sql` (included in the zip)
5. Click **Go** to import all tables

### App Key
1. Visit `https://erp.yourdomain.com` — if you see an error about APP_KEY
2. Use an online base64 key generator, or
3. Add any random 32-character base64 string to `.env`:
   ```
   APP_KEY=base64:your_random_key_here
   ```

### Seed Initial Data
If you imported the schema but have no data, visit:
```
https://erp.yourdomain.com/setup-seed.php
```
(This script is included and runs the seeder — delete it after use)

---

## Post-Deployment

### Change Admin Password
1. Log in as `admin@erp.test`
2. Go to **Setup > Users**
3. Change the password immediately

### Configure Company
1. Go to **Setup > Company**
2. Edit company name, address, tax number, currency

### Add Your Data
1. **Accounting > Chart of Accounts** — verify the chart of accounts
2. **Inventory > Products** — add your products
3. **Sales > Customers** — add your customers
4. **Purchases > Suppliers** — add your suppliers
5. **Banking > Bank Accounts** — add your bank accounts
6. **Setup > Users** — create user accounts for your staff

### Set Up Backups
1. Go to **Setup > Backups**
2. Click **Create Backup** to test
3. Optionally connect Google Drive for automatic cloud sync

### Set Up Cron (if available)
Add to cPanel > Cron Jobs:
```
* * * * * cd /home/youruser/public_html/erp && php artisan schedule:run >> /dev/null 2>&1
```

---

## Troubleshooting

### 500 Internal Server Error
- Check `storage/logs/laravel.log`
- Ensure `storage/` and `bootstrap/cache/` are writable (chmod 775)
- Verify `.env` exists and has correct DB credentials
- Run `php artisan config:clear` then `php artisan config:cache`

### Blank Page
- Check if APP_KEY is set in `.env`
- Check PHP version (needs 8.2+)
- Check error logs in cPanel > Errors

### "No application encryption key found"
Run `php artisan key:generate` or set APP_KEY manually in `.env`

### Database connection refused
- Verify DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD in `.env`
- Ensure the database user has privileges to the database

### Assets not loading (CSS/JS missing)
- Run `npm run build` if you have Node.js
- Or ensure `public/build/` folder exists in the upload (it should be in the zip)

### Permission denied errors
```bash
chmod -R 775 storage bootstrap/cache
chown -R youruser:youruser storage bootstrap/cache
```
