# Warehouse Management System - Deployment Guide for cPanel

## Prerequisites

- cPanel hosting account with:
  - PHP 8.2 or higher
  - MySQL 5.7 or higher / MariaDB 10.3 or higher
  - Node.js 18 or higher (for frontend build)
  - Composer (for Laravel dependencies)

## Project Structure

```
gudang_strange/
├── backend/          # Laravel API backend
└── frontend/         # React + Vite frontend
```

## Deployment Steps

### 1. Database Setup

1. Log in to cPanel
2. Go to "MySQL Database Wizard"
3. Create a new database (e.g., `wms_db`)
4. Create a new database user and set a strong password
5. Assign the user to the database with all privileges
6. Note down:
   - Database name
   - Database username
   - Database password
   - Database host (usually `localhost`)

### 2. Backend Deployment

#### Step 2.1: Prepare Backend Files

1. Upload the entire `backend/` folder to your cPanel account (preferably outside `public_html` for security)
2. In cPanel File Manager, navigate to the backend folder
3. Copy `.env.example` to `.env`
4. Edit `.env` and update the following:

```env
APP_NAME=WMS
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_wms_db
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password
```

#### Step 2.2: Install Dependencies

1. In cPanel, go to "Terminal"
2. Navigate to the backend folder:
   ```bash
   cd /home/yourusername/backend
   ```
3. Install Composer dependencies:
   ```bash
   composer install --no-dev --optimize-autoloader
   ```
4. Generate application key:
   ```bash
   php artisan key:generate
   ```
5. Run migrations:
   ```bash
   php artisan migrate --force
   ```
6. (Optional) Seed the database with sample data:
   ```bash
   php artisan db:seed --force
   ```

#### Step 2.3: Configure Backend in Public HTML

1. Create a subdomain or use your main domain
2. Create a symlink from `public_html` to the backend's `public` folder, or:
3. Copy the contents of `backend/public/` to `public_html/`
4. Update `public_html/index.php` to point to the correct backend folder:

```php
// Change these lines:
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

// To:
require __DIR__.'/../../backend/vendor/autoload.php';
$app = require_once __DIR__.'/../../backend/bootstrap/app.php';
```

#### Step 2.4: Set Up Backend .htaccess

Create `public_html/.htaccess` for the backend:

```apache
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
```

### 3. Frontend Deployment

#### Step 3.1: Build Frontend

1. On your local machine, navigate to the `frontend/` folder
2. Install dependencies:
   ```bash
   npm install
   ```
3. Create a `.env.production` file in the frontend folder:
   ```env
   VITE_API_BASE_URL=https://yourdomain.com/api
   ```
4. Build the frontend:
   ```bash
   npm run build
   ```

#### Step 3.2: Deploy Frontend

1. Upload the contents of `frontend/dist/` to `public_html/` (or a subfolder if using subdomain)
2. Create `.htaccess` in the frontend folder:

```apache
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>
```

### 4. Folder Permissions

Set correct permissions in cPanel File Manager:

- `backend/storage/` and `backend/bootstrap/cache/` should be writable (755 or 775)
- `public_html/` should be readable (755)

## Accessing the Application

- Frontend: `https://yourdomain.com`
- API: `https://yourdomain.com/api`

## Default Credentials

If you ran the seeders, you can log in with:

- Username: `admin`
- Password: `password`

## Troubleshooting

### 500 Internal Server Error

- Check PHP version (needs 8.2+)
- Verify file permissions
- Check `.env` file exists and is configured correctly
- Check Laravel logs in `backend/storage/logs/`

### Database Connection Error

- Verify database credentials in `.env`
- Make sure database user has correct privileges

### Frontend Routing Issues

- Ensure `.htaccess` is present and correct
- Check mod_rewrite is enabled in Apache
