# Stock Management System (SMS) - Entity Relationship Diagram (ERD)

## ERD Diagram

```mermaid
erDiagram
    PRODUCT ||--o{ STOCK_TRANSACTION : "has"
    WAREHOUSE ||--o{ STOCK_TRANSACTION : "has"

    PRODUCT {
        string productCode PK "Primary Key (unique)"
        string productName
        string category
        number quantityInStock
        number unitPrice
        string supplierName
        date dateReceived
        timestamp createdAt
        timestamp updatedAt
    }

    WAREHOUSE {
        objectid _id PK "Primary Key (MongoDB ObjectId)"
        string warehouseCode
        string warehouseName
        string warehouseLocation
        timestamp createdAt
        timestamp updatedAt
    }

    STOCK_TRANSACTION {
        objectid _id PK "Primary Key (MongoDB ObjectId)"
        date transactionDate
        number quantityMoved
        string transactionType "enum: Stock In, Stock Out"
        objectid product FK "Foreign Key -> Product"
        objectid warehouse FK "Foreign Key -> Warehouse"
        timestamp createdAt
        timestamp updatedAt
    }

    USER {
        objectid _id PK "Primary Key (MongoDB ObjectId)"
        string name
        string email "unique, lowercase"
        string password "hashed"
        timestamp createdAt
        timestamp updatedAt
    }
```

---

## ERD Explanation

### Entities & Attributes

#### **PRODUCT**
- **PK**: `productCode` (Unique String)
- **Attributes**:
  - `productName` - Name of the product
  - `category` - Product category
  - `quantityInStock` - Current stock level
  - `unitPrice` - Price per unit
  - `supplierName` - Supplier name
  - `dateReceived` - Date product was received
- **Relationship**: One Product can have Many StockTransactions

#### **WAREHOUSE**
- **PK**: `_id` (MongoDB ObjectId)
- **Attributes**:
  - `warehouseCode` - Unique warehouse identifier
  - `warehouseName` - Name of warehouse
  - `warehouseLocation` - Physical location
- **Relationship**: One Warehouse can have Many StockTransactions

#### **STOCK_TRANSACTION**
- **PK**: `_id` (MongoDB ObjectId)
- **FK1**: `product` → References Product._id
- **FK2**: `warehouse` → References Warehouse._id
- **Attributes**:
  - `transactionDate` - When transaction occurred
  - `quantityMoved` - Amount of stock moved
  - `transactionType` - "Stock In" or "Stock Out"
- **Relationship**: Bridge table connecting Product and Warehouse (Many-to-Many through StockTransaction)

#### **USER**
- **PK**: `_id` (MongoDB ObjectId)
- **Attributes**:
  - `name` - User's name
  - `email` - User's email (unique)
  - `password` - Hashed password
- **Note**: Authentication only, not directly related to stock data

---

## Cardinality & Relationships

| From | Relationship | To | Meaning |
|------|--------------|-----|---------|
| Product | 1 ← → Many | StockTransaction | One product can have many stock movements |
| Warehouse | 1 ← → Many | StockTransaction | One warehouse can have many transactions |
| Product ← StockTransaction → Warehouse | Many ← → Many | Junction | Products and Warehouses linked through transactions |

---

## Key Points

✅ **Primary Keys**:
- Product: `productCode` (String, Unique)
- Warehouse: `_id` (MongoDB ObjectId)
- StockTransaction: `_id` (MongoDB ObjectId)
- User: `_id` (MongoDB ObjectId)

✅ **Foreign Keys** (in StockTransaction):
- `product`: References Product ObjectId
- `warehouse`: References Warehouse ObjectId

✅ **Referential Integrity**:
- StockTransaction cannot exist without valid Product & Warehouse
- Validated in backend before insertion
- Data is auto-populated on retrieval

✅ **Cardinality**:
- Product (1) : StockTransaction (M)
- Warehouse (1) : StockTransaction (M)
- Implicit M:M relationship between Product and Warehouse through StockTransaction

✅ **Timestamps**:
- All entities include `createdAt` and `updatedAt` for audit trail
