# Visual Diagrams Documentation

## Process Flow Diagrams

### 1. Main System Flow
```mermaid
graph TD
    A[Start] --> B[Login]
    B --> C{User Role}
    C -->|Admin| D[Admin Dashboard]
    C -->|Sales| E[Sales Dashboard]
    C -->|Accountant| F[Accountant Dashboard]
    
    D --> G[User Management]
    D --> H[System Settings]
    D --> I[Reports]
    
    E --> J[Dealer Management]
    E --> K[Order Management]
    E --> L[Inventory View]
    
    F --> M[Financial Reports]
    F --> N[Voucher Management]
    F --> O[Payment Tracking]
```

### 2. Order Processing Flow
```mermaid
sequenceDiagram
    participant Sales
    participant System
    participant Inventory
    participant Finance
    
    Sales->>System: Create Order
    System->>Inventory: Check Stock
    Inventory-->>System: Stock Available
    System->>Finance: Check Credit Limit
    Finance-->>System: Credit Approved
    System->>System: Generate Order
    System->>Inventory: Update Stock
    System->>Finance: Update Accounts
    System-->>Sales: Order Confirmed
```

### 3. Financial Transaction Flow
```mermaid
graph LR
    A[Create Voucher] --> B[Select Type]
    B --> C[Enter Details]
    C --> D[Validate]
    D --> E[Save Transaction]
    E --> F[Update Accounts]
    F --> G[Generate Report]
```

## Database Schema Diagrams

### 1. Core Entities Relationship
```mermaid
erDiagram
    users ||--o{ orders : creates
    dealers ||--o{ orders : places
    products ||--o{ order_items : contains
    orders ||--o{ order_items : has
    orders ||--o{ payments : receives
    dealers ||--o{ payments : makes
```

### 2. Financial Management Schema
```mermaid
erDiagram
    account_heads ||--o{ voucher_items : contains
    vouchers ||--o{ voucher_items : has
    vouchers ||--|| users : created_by
    account_heads ||--o{ account_heads : parent_of
```

### 3. Vehicle Management Schema
```mermaid
erDiagram
    vehicles ||--o{ vehicle_expenses : has
    vehicles ||--o{ deliveries : assigned_to
    orders ||--o{ deliveries : has
    vehicle_expenses ||--|| users : created_by
```

### 4. Complete Database Schema
```mermaid
erDiagram
    users {
        bigint id PK
        string name
        string email
        string password
        timestamp created_at
        timestamp updated_at
    }
    
    roles {
        bigint id PK
        string name
        string guard_name
        timestamp created_at
        timestamp updated_at
    }
    
    dealers {
        bigint id PK
        string name
        string contact_person
        string email
        string phone
        text address
        decimal credit_limit
        decimal outstanding_balance
        enum status
        timestamp created_at
        timestamp updated_at
    }
    
    products {
        bigint id PK
        string name
        string sku
        text description
        decimal unit_price
        int reorder_level
        int current_stock
        enum status
        timestamp created_at
        timestamp updated_at
    }
    
    orders {
        bigint id PK
        string order_number
        bigint dealer_id FK
        decimal total_amount
        enum status
        date delivery_date
        text notes
        timestamp created_at
        timestamp updated_at
    }
    
    order_items {
        bigint id PK
        bigint order_id FK
        bigint product_id FK
        int quantity
        decimal unit_price
        decimal total_amount
        timestamp created_at
        timestamp updated_at
    }
    
    account_heads {
        bigint id PK
        bigint parent_id FK
        string name
        string code
        enum type
        decimal opening_balance
        timestamp created_at
        timestamp updated_at
    }
    
    vouchers {
        bigint id PK
        string voucher_number
        enum type
        date date
        text description
        decimal total_amount
        bigint created_by FK
        timestamp created_at
        timestamp updated_at
    }
    
    voucher_items {
        bigint id PK
        bigint voucher_id FK
        bigint account_head_id FK
        decimal debit_amount
        decimal credit_amount
        text description
        timestamp created_at
        timestamp updated_at
    }
    
    vehicles {
        bigint id PK
        string registration_number
        string model
        decimal capacity
        enum status
        timestamp created_at
        timestamp updated_at
    }
    
    vehicle_expenses {
        bigint id PK
        bigint vehicle_id FK
        date expense_date
        text description
        decimal amount
        bigint created_by FK
        timestamp created_at
        timestamp updated_at
    }
    
    deliveries {
        bigint id PK
        bigint order_id FK
        bigint vehicle_id FK
        date delivery_date
        enum status
        text notes
        timestamp created_at
        timestamp updated_at
    }
    
    payments {
        bigint id PK
        string payment_number
        enum payer_type
        bigint payer_id
        decimal amount
        date payment_date
        enum payment_method
        string reference_number
        text notes
        bigint created_by FK
        timestamp created_at
        timestamp updated_at
    }
    
    payment_items {
        bigint id PK
        bigint payment_id FK
        bigint order_id FK
        decimal amount
        timestamp created_at
        timestamp updated_at
    }
```

### 5. User Authentication Flow
```mermaid
sequenceDiagram
    participant User
    participant Frontend
    participant Backend
    participant Database
    
    User->>Frontend: Enter Credentials
    Frontend->>Backend: POST /api/auth/login
    Backend->>Database: Validate Credentials
    Database-->>Backend: Return User Data
    Backend->>Backend: Generate Token
    Backend-->>Frontend: Return Token & User Data
    Frontend-->>User: Show Dashboard
```

### 6. Inventory Management Flow
```mermaid
graph TD
    A[Stock Entry] --> B[Stock Movement]
    B --> C[Stock Monitoring]
    C --> D{Stock Level}
    D -->|Low| E[Generate Alert]
    D -->|Normal| F[Regular Update]
    D -->|High| G[Excess Stock Alert]
    E --> H[Reorder Process]
    F --> I[Regular Reports]
    G --> J[Stock Adjustment]
```

### 7. Payment Processing Flow
```mermaid
sequenceDiagram
    participant User
    participant System
    participant Finance
    participant Database
    
    User->>System: Initiate Payment
    System->>Finance: Validate Payment
    Finance->>Database: Check Balance
    Database-->>Finance: Return Balance
    Finance->>System: Confirm Payment
    System->>Database: Record Payment
    Database-->>System: Confirm Record
    System-->>User: Show Receipt
```

### 8. Report Generation Flow
```mermaid
graph LR
    A[Select Report Type] --> B[Set Parameters]
    B --> C[Fetch Data]
    C --> D[Process Data]
    D --> E[Generate Report]
    E --> F[Format Report]
    F --> G[Export/Save]
```

These diagrams provide a visual representation of:
1. System processes and workflows
2. Database relationships and structure
3. User interactions and authentication flow
4. Business logic and data flow
5. System architecture and components

The diagrams are created using Mermaid syntax, which can be rendered by most Markdown viewers and documentation tools. They provide a clear visual understanding of the system's structure and processes. 
