# Invoice Flow - Visual Diagram

## Complete Flow from Quotation to Payment

```
┌─────────────────────────────────────────────────────────────────┐
│                    STEP 1: CUSTOMER ACCEPTS                     │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Customer (Portal)                                              │
│       │                                                         │
│       │ Clicks "Accept Quotation"                              │
│       ↓                                                         │
│  QuotationController.accept()                                  │
│       │                                                         │
│       ├─→ Creates SalesOrder (local DB)                        │
│       │                                                         │
│       └─→ Sends to Odoo: convertQuotationToSaleOrder()        │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│                    STEP 2: ODOO CONFIRMS                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Odoo ERP                                                       │
│       │                                                         │
│       │ Quotation confirmed                                    │
│       ↓                                                         │
│  Sales Order created (state: 'sale')                           │
│       │                                                         │
│       │ Odoo Order ID: 12345                                   │
│       │                                                         │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 3: SALES TEAM GENERATES INVOICE               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Sales Team (in Odoo)                                           │
│       │                                                         │
│       │ Opens Sales Order #12345                               │
│       ↓                                                         │
│  Clicks "Create Invoice"                                        │
│       │                                                         │
│       ↓                                                         │
│  Invoice Created in Odoo                                        │
│       │                                                         │
│       ├─→ Invoice Number: INV/2024/0001                        │
│       ├─→ Amount: $500.00                                      │
│       ├─→ State: 'posted'                                      │
│       └─→ Odoo Invoice ID: 789                                 │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
                    ┌───────┴───────┐
                    │               │
                    ↓               ↓
┌──────────────────────────┐  ┌──────────────────────────┐
│   OPTION A: WEBHOOK      │  │  OPTION B: SCHEDULER     │
│   (Instant)              │  │  (Every 5 minutes)       │
├──────────────────────────┤  ├──────────────────────────┤
│                          │  │                          │
│  Odoo                    │  │  Docker Scheduler        │
│    │                     │  │    │                     │
│    │ Sends webhook       │  │    │ Runs every 5 min   │
│    ↓                     │  │    ↓                     │
│  POST /api/odoo/webhook  │  │  php artisan sync:data  │
│    │                     │  │  invoices               │
│    │ Event:              │  │    │                     │
│    │ 'invoice.created'   │  │    │ Fetches invoices   │
│    │                     │  │    │ from Odoo API       │
│    ↓                     │  │    ↓                     │
│  Portal receives         │  │  Portal receives        │
│  immediately             │  │  within 5 minutes       │
│                          │  │                          │
└──────────────────────────┘  └──────────────────────────┘
                    │               │
                    └───────┬───────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 4: PORTAL SYNCS INVOICE                       │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  OdooController.syncInvoice($invoiceData)                      │
│       │                                                         │
│       ├─→ Find Customer (by odoo_partner_id)                   │
│       │                                                         │
│       ├─→ Find SalesOrder (by order_number)                    │
│       │                                                         │
│       ├─→ Create/Update Invoice in Database:                   │
│       │   Invoice::updateOrCreate([                            │
│       │     'odoo_invoice_id' => 789,                          │
│       │     'customer_id' => 1,                                │
│       │     'sales_order_id' => 5,                             │
│       │     'invoice_number' => 'INV/2024/0001',               │
│       │     'total_amount' => 500.00,                          │
│       │     'status' => 'pending',                             │
│       │     ...                                                │
│       │   ])                                                   │
│       │                                                         │
│       └─→ Send Email Notification:                             │
│           Mail::to($customer->email)                           │
│               ->send(new InvoiceCreated($invoice))             │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 5: CUSTOMER RECEIVES EMAIL                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Customer Email Inbox                                           │
│       │                                                         │
│       │ Subject: "Invoice Generated - INV/2024/0001"           │
│       ↓                                                         │
│  Email Content:                                                 │
│    - Invoice Number: INV/2024/0001                             │
│    - Amount Due: $500.00                                       │
│    - Due Date: 2024-02-28                                      │
│    - [View Invoice] button                                     │
│    - [Pay Now] button                                          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 6: CUSTOMER VIEWS INVOICE                     │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Customer (Portal)                                              │
│       │                                                         │
│       │ Logs into portal                                       │
│       ↓                                                         │
│  Navigates to Invoices page                                    │
│       │                                                         │
│       ↓                                                         │
│  Portal queries database:                                      │
│  Invoice::where('customer_id', 1)->get()                       │
│       │                                                         │
│       ↓                                                         │
│  Displays invoice:                                             │
│    ┌──────────────────────────────────────┐                   │
│    │ Invoice #INV/2024/0001               │                   │
│    │ Amount: $500.00                      │                   │
│    │ Due Date: 2024-02-28                 │                   │
│    │ Status: Pending                      │                   │
│    │                                      │                   │
│    │ [Pay Now] [Download PDF]            │                   │
│    └──────────────────────────────────────┘                   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 7: CUSTOMER PAYS INVOICE                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Customer clicks "Pay Now"                                      │
│       │                                                         │
│       ↓                                                         │
│  PaymentController.processInvoicePayment()                     │
│       │                                                         │
│       ├─→ Validates invoice                                    │
│       │                                                         │
│       ├─→ Initiates payment (Pesepay/Bank Transfer)            │
│       │                                                         │
│       ├─→ Payment confirmed                                    │
│       │                                                         │
│       ├─→ Creates Payment record                               │
│       │                                                         │
│       ├─→ Updates Invoice status to 'paid'                     │
│       │                                                         │
│       └─→ Sends payment to Odoo                                │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 8: ODOO MARKS INVOICE PAID                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Odoo ERP                                                       │
│       │                                                         │
│       │ Receives payment record                                │
│       ↓                                                         │
│  Marks invoice as paid                                         │
│       │                                                         │
│       ├─→ Invoice state: 'paid'                                │
│       │                                                         │
│       └─→ Sends webhook: 'invoice.paid'                        │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────────┐
│              STEP 9: PORTAL SYNCS PAYMENT STATUS                │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  OdooController.syncInvoicePayment($invoiceData)               │
│       │                                                         │
│       ├─→ Updates Invoice status to 'paid'                     │
│       │                                                         │
│       ├─→ Sets paid_date                                       │
│       │                                                         │
│       └─→ Updates customer balance                             │
│                                                                 │
│  COMPLETE ✓                                                     │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

## Data Flow Summary

```
┌──────────┐     ┌──────────┐     ┌──────────┐     ┌──────────┐
│Quotation │ ──→ │  Sales   │ ──→ │ Invoice  │ ──→ │ Payment  │
│          │     │  Order   │     │          │     │          │
└──────────┘     └──────────┘     └──────────┘     └──────────┘
   Portal           Portal           Odoo            Portal
   Creates          Creates          Creates         Creates
      ↓                ↓                ↓                ↓
   Sent to          Sent to          Synced to       Sent to
   Odoo             Odoo             Portal          Odoo
```

## Database Records

```
┌─────────────────────────────────────────────────────────────────┐
│                    PORTAL DATABASE                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  quotations                                                     │
│  ├─ id: 1                                                       │
│  ├─ reference: QT-2024-ABC123                                   │
│  ├─ customer_id: 1                                              │
│  ├─ total_amount: 500.00                                        │
│  ├─ status: 'accepted'                                          │
│  └─ odoo_quotation_id: 456                                      │
│                                                                 │
│  sales_orders                                                   │
│  ├─ id: 5                                                       │
│  ├─ order_number: AFINET-SO-XYZ789                             │
│  ├─ customer_id: 1                                              │
│  ├─ quotation_id: 1                                             │
│  ├─ total_amount: 500.00                                        │
│  ├─ order_status: 'confirmed'                                   │
│  └─ odoo_order_id: 12345                                        │
│                                                                 │
│  invoices ← SYNCED FROM ODOO                                    │
│  ├─ id: 10                                                      │
│  ├─ invoice_number: INV/2024/0001                               │
│  ├─ customer_id: 1                                              │
│  ├─ sales_order_id: 5                                           │
│  ├─ total_amount: 500.00                                        │
│  ├─ status: 'pending' → 'paid'                                 │
│  ├─ odoo_invoice_id: 789 ← LINKS TO ODOO                       │
│  └─ odoo_synced: true                                           │
│                                                                 │
│  payments                                                       │
│  ├─ id: 20                                                      │
│  ├─ payment_reference: PAY-2024-001                            │
│  ├─ customer_id: 1                                              │
│  ├─ invoice_id: 10                                              │
│  ├─ amount: 500.00                                              │
│  ├─ status: 'completed'                                         │
│  └─ odoo_synced: true                                           │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

## Key Takeaways

1. **Invoice NEVER created by portal** - Always comes from Odoo
2. **Two sync methods** - Webhook (instant) or Scheduler (5 min)
3. **Saved to database** - Portal stores invoice locally
4. **Customer sees it** - Queries local database
5. **Customer pays** - Payment processed through portal
6. **Synced back to Odoo** - Payment recorded in both systems

## Timeline

```
T+0:00  Customer accepts quotation
T+0:01  Portal creates sales order, sends to Odoo
T+0:02  Odoo confirms sales order
T+2:00  Sales team generates invoice in Odoo
T+2:00  Webhook fires (instant) OR
T+2:05  Scheduler syncs (within 5 min)
T+2:01  Portal saves invoice to database
T+2:01  Customer receives email
T+2:05  Customer logs in and sees invoice
T+2:10  Customer pays invoice
T+2:11  Payment recorded in portal and Odoo
```
