# Product Discount Feature - Implementation Diagram

## Architecture Overview

```
┌─────────────────────────────────────────────────────────────────┐
│                       USER INTERFACE (Dashboard)                 │
│                                                                   │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │              Products Module (sql/modules.sql)            │  │
│  │                                                            │  │
│  │  Header:                                                   │  │
│  │  [Add Product] [Auto Orders] [Import] [Export]           │  │
│  │                                                            │  │
│  │  ┌────────────────────────────────────────────────────┐  │  │
│  │  │ Products Table (products)                          │  │  │
│  │  │                                                     │  │  │
│  │  │ Name    | Price  | Brand    | Tax Rates | Actions│  │  │
│  │  │ ───────────────────────────────────────────────────│  │  │
│  │  │ Widget  | $19.99 | BrandA   | {...}     | [Edit] │  │  │
│  │  │                                           [Delete]│  │  │
│  │  │                                           [Discount]◄─┐  │
│  │  └────────────────────────────────────────────────────┘  │  │
│  └──────────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────────┘
                                  │
                                  │ Click "Discount" button
                                  ↓
┌─────────────────────────────────────────────────────────────────┐
│                   Product Discount Form Modal                    │
│                         (Form ID: 134)                           │
│                                                                   │
│  ┌────────────────────────────────────────────────────────────┐│
│  │ Product Discount                                   [Close] ││
│  │────────────────────────────────────────────────────────────││
│  │                                                             ││
│  │  Product:       [Widget (read-only)               ]       ││
│  │                    ↑ Type 1: Read-only text field          ││
│  │                    ↑ Pre-filled with selected product      ││
│  │  (Hidden field stores product_id)                          ││
│  │                                                             ││
│  │  Discount Amount: [______________________________]        ││
│  │                    ↑ Type 2: Numeric input                 ││
│  │                                                             ││
│  │  Start Date:     [📅 MM/DD/YYYY                  ]        ││
│  │                    ↑ Type 3: Date picker                   ││
│  │                                                             ││
│  │  End Date:       [📅 MM/DD/YYYY                  ]        ││
│  │                    ↑ Type 3: Date picker                   ││
│  │                                                             ││
│  │  [Save]  [Cancel]                                          ││
│  └────────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────────┘
                                  │
                                  │ Submit form
                                  ↓
┌─────────────────────────────────────────────────────────────────┐
│                    DATA LAYER (Backend)                          │
│                                                                   │
│  ┌───────────────────────────────────────────────────────────┐ │
│  │  FormRenderer → GenericController → /api/sync             │ │
│  │                                                             │ │
│  │  Form data validated and sent to generic sync endpoint    │ │
│  └───────────────────────────────────────────────────────────┘ │
│                                  │                               │
│                                  ↓                               │
│  ┌───────────────────────────────────────────────────────────┐ │
│  │              PostgreSQL Database (TAFDB.pgsql)            │ │
│  │                                                             │ │
│  │  ┌─────────────────────────────────────────────────────┐ │ │
│  │  │ product_discounts table                             │ │ │
│  │  │                                                       │ │ │
│  │  │ • discount_id (uuid PK)                             │ │ │
│  │  │ • product_id (uuid FK → products.product_id)        │ │ │
│  │  │ • discount_amount (numeric 10,2)                    │ │ │
│  │  │ • start_date (date)                                 │ │ │
│  │  │ • end_date (date)                                   │ │ │
│  │  │ • org_id (uuid FK → orgs.org_id)                   │ │ │
│  │  │ • isDeleted (boolean)                               │ │ │
│  │  │ • updatedAt (timestamptz)                           │ │ │
│  │  └─────────────────────────────────────────────────────┘ │ │
│  └───────────────────────────────────────────────────────────┘ │
│                                  │                               │
│                                  ↓                               │
│  ┌───────────────────────────────────────────────────────────┐ │
│  │           tenant_tables (Schema Registration)             │ │
│  │                                                             │ │
│  │  Registers: product_discounts                             │ │
│  │  Definition: Column metadata for IndexedDB                │ │
│  └───────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
                                  │
                                  │ Sync via LocalSyncManager
                                  ↓
┌─────────────────────────────────────────────────────────────────┐
│               CLIENT-SIDE STORAGE (IndexedDB)                    │
│                                                                   │
│  ┌───────────────────────────────────────────────────────────┐ │
│  │  Store: product_discounts                                 │ │
│  │  KeyPath: id (discount_id)                                │ │
│  │                                                             │ │
│  │  Auto-synced via LocalSyncManager                         │ │
│  │  Available offline for read operations                    │ │
│  │  Queued writes sync when online                           │ │
│  └───────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```

## Data Flow Sequence

```
1. User clicks "Discount" button on product row
       ↓
2. ModalBuilder reads config from modules table
       ↓
3. FormRenderer loads form definition (ID 134) from forms table
       ↓
4. Form displays with:
   - Product selector (pre-filled with selected product)
   - Discount amount input
   - Date range pickers
       ↓
5. User fills in discount details and clicks Save
       ↓
6. FormRenderer validates required fields
       ↓
7. Data sent to /api/sync endpoint (GenericController)
       ↓
8. Data saved to product_discounts table
       ↓
9. LocalSyncManager syncs to IndexedDB
       ↓
10. Modal closes, success notification shown
```

## File Structure

```
TAF/
├── TAFDB.pgsql                           # Database schema
│   └── product_discounts table (line ~422)
│
├── sql/
│   ├── defaults.sql                      # Form definitions
│   │   └── Product Discount form (ID 134, line ~3102)
│   │
│   └── modules.sql                       # Module configurations
│       └── Products module with Discount button (line ~1300)
│
├── migrations/
│   └── 20251004_product_discounts.sql   # Migration script
│
├── docs/
│   └── ProductDiscounts.md              # Documentation
│
└── deploy_product_discounts.sh          # Deployment script
```

## Technology Stack

```
Frontend:
├── ModalBuilder (core/modalbuilder.js)
├── FormRenderer (core/FormRenderer.js)
├── DataLoader (core/DataLoader.js)
├── LocalSyncManager (core/localsyncmanager.js)
└── DaisyUI components (buttons, forms, modals)

Backend:
├── GenericController (api/controllers/GenericController.php)
├── SyncController (api/controllers/SyncController.php)
├── FormBuilderController (api/controllers/FormBuilderController.php)
└── PostgreSQL database

Sync Layer:
├── /api/sync endpoints (CRUD operations)
├── IndexedDB (client-side storage)
├── LocalSyncManager (sync orchestration)
└── ServiceWorker (offline support)
```

## Key Features

✅ **Zero Custom Code**: Uses existing infrastructure
✅ **Offline-First**: Works offline via IndexedDB
✅ **Row Actions**: Discount button per product
✅ **Form Validation**: Required fields enforced
✅ **Date Range**: Start and end date pickers
✅ **Multi-Tenant**: org_id support
✅ **Soft Deletes**: isDeleted flag
✅ **Audit Trail**: updatedAt timestamp
✅ **No Extra Modules**: Integrated into Products module
