# System Analysis: Inventory & Vendor Management

## 1. Inventory System
The inventory system is designed as a **centralized, log-based tracking system** that supports both the Official Store (Admin-managed) and Third-Party Vendors.

### **Database Model**
- **Table**: `products`
    - Column: `stock_quantity` (Integer) - Holds the current real-time stock level.
    - Column: `vendor_id` - Determines ownership (NULL/0 = Official Store).
- **Table**: `inventory_logs` (Implied from code)
    - Records every stock change: `adjustment` (+/- amount), `previous_stock`, `current_stock`, `reason`, and `created_by`.

### **Features**
| Feature | Admin Portal | Vendor Portal |
| :--- | :--- | :--- |
| **Visibility** | Complete visibility of **ALL** products (Official + Vendors). Filterable by Vendor. | Restricted to **OWN** products only. |
| **Status Indicators** | Badges for "In Stock", "Low Stock" (<10), "Out of Stock" (0). | Same visual indicators. |
| **Stock Adjustment** | Manually Add/Remove stock via Modal. | Manually Add/Remove stock via Modal. |
| **Safety Layers** | Can adjust any stock. | **Ownership Check**: Can only adjust own products.<br>**Negative Protection**: Cannot reduce stock below 0. |

---

## 2. Vendor Management System
The vendor system allows third-party sellers to register, manage products, and track earnings, while giving Admins full oversight and control.

### **Database Model (`setup/create_vendor_tables.sql`)**
- **`vendors`**: Core profile (Store Name, Slug, Contact, Bank Details, Status: Active/Suspended/Pending).
- **`vendor_earnings`**: Financial record per order item (Order Amount, Platform Commission, Vendor Net Earning).
- **`vendor_payouts`**: Withdrawal request tracking.
- **`vendor_reviews`**: Customer ratings for vendors.

### **Admin Capabilities (`__admin__` files)**
- **Onboarding**: Review and Approve/Reject applications in `pending-vendors.php`.
- **Management**:
    - **View**: List all vendors with key metrics (Total Products, Total Earnings).
    - **Control**: Suspend or Activate vendor accounts instantly.
    - **Edit**: Modify vendor details (Contact info, Commission rate).
- **Inventory Oversight**: Admin can intervene and adjust stock for any vendor if necessary.

### **Vendor Capabilities (`vendorportal`)**
- **Dashboard**: Overview of sales and product status.
- **Product Management**:
    - Add/Edit products (Require Admin approval unless trusted).
    - **RESTRICTION**: Vendors **cannot** set their own Shipping Fees (Global/Admin controlled).
    - **RESTRICTION**: Vendors **must** select a valid "Location" (from Admin-defined list) for products.
- **Inventory**: Real-time stock updates.
