# Inventory System Implementation Plan

## Goal
Build a robust inventory management system for the Awesome Mall multi-vendor platform. This system will track stock levels, log all inventory changes (adjustments, sales, returns), and allow vendors to manage their stock efficiently.

## Phase 1: Database & Foundation
1.  **Create `inventory_logs` Table**:
    - Stores history of all stock changes.
    - Columns: `id`, `product_id`, `vendor_id`, `adjustment_qty` (e.g., +10, -5), `new_balance`, `reason` (e.g., "New Stock", "Order #123", "Damaged"), `created_at`, `created_by` (User ID).
2.  **Verify `products` Table**: Ensure `stock_quantity` is ready (Confirmed).

## Phase 2: Vendor Portal - Inventory Management
1.  **Inventory Dashboard (`vendorportal/inventory.php`)**:
    - A dedicated page to view all products with their current stock.
    - Status indicators (Low Stock, Out of Stock).
    - Quick actions: "Add Stock", "Remove Stock" (Adjustments).
2.  **Stock Adjustment Logic**:
    - Backend script to handle stock updates.
    - MUST update `products.stock_quantity`.
    - MUST insert record into `inventory_logs`.
3.  **Inventory History (`vendorportal/inventory-history.php`)**:
    - View logs for a specific product or all products.

## Phase 3: Order Integration (Stock Deduction)
1.  **Analyze Checkout Flow**: pinpoint exactly where orders are saved to the database (`orders` table).
2.  **Implement Deduction**:
    - When an order is strictly *placed* (or paid), decrement `stock_quantity`.
    - Insert log entry: "Order Placed - Order #XYZ".
3.  **Stock Validation**:
    - Prevent checkout if stock < requested quantity.

## Phase 4: Admin Overview (Optional/Next Step)
1.  Admin can view inventory logs in `edit-product.php`.

---
## Technical Details

### `inventory_logs` Schema
```sql
CREATE TABLE inventory_logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_id BIGINT UNSIGNED NOT NULL,
    vendor_id INT NOT NULL,
    adjustment INT NOT NULL,  -- Positive for add, Negative for remove
    previous_stock INT NOT NULL,
    current_stock INT NOT NULL,
    reason VARCHAR(255),
    reference_id VARCHAR(50), -- e.g. Order ID
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INT -- User ID who made the change (if manual)
);
```

### Next Steps (Immediate)
1.  Create `inventory_logs` table.
2.  Create `inventory.php` for vendors.
