# Order Status Logic - Real vs Simulation

## Two Scenarios

### Scenario 1: Real Production (With Odoo Integration)
Orders are created and managed in Odoo, synced to portal.

### Scenario 2: Simulation Mode (Portal Only)
Orders are created directly in portal without Odoo integration.

## Status Flow Comparison

### Real Production Flow (Odoo)
```
1. Order Created in Odoo
   ↓
   Status: 'sale' (confirmed/approved in Odoo)
   Display: "Approved" (blue)
   
2. Payment Received
   ↓
   invoice_status: 'invoiced'
   Status still: 'sale'
   Display: "Completed" (green) - because payment received
   
3. Subscription Created
   ↓
   subscription.state: 'open'/'active'
   Display: "Active" (green) - service is running
```

### Simulation Flow (Portal)
```
1. Order Created in Portal
   ↓
   Status: 'pending_payment'
   Display: "Pending Payment" (yellow)
   
2. Payment Received
   ↓
   Status: 'completed'
   payment_status: 'paid'
   Display: "Completed" (green)
   
3. No Subscription Checking
   ↓
   Portal orders don't have Odoo subscriptions
   Display remains: "Completed" (green)
```

## Status Determination Logic

### Priority Order
1. **Cancelled** - If order is cancelled
2. **Active** - If subscription exists (Odoo only)
3. **Completed** - If status is 'completed' OR (status is 'sale' AND payment is 'invoiced')
4. **Approved** - If status is 'sale' AND payment is NOT 'invoiced'
5. **Pending Payment** - If status is 'pending_payment'
6. **Draft** - If status is 'draft'

### Frontend Logic

```javascript
const getCustomerStatus = (order) => {
  const status = order.order_status || order.status || order.state;
  const paymentStatus = order.payment_status || order.invoice_status;
  const hasSubscription = order.subscription || order.subscription_id;
  
  // Cancelled
  if (status === 'cancelled' || status === 'cancel') {
    return { status: 'cancelled', label: 'Cancelled', color: 'red' };
  }
  
  // Active (subscription exists - Odoo only)
  if (hasSubscription) {
    return { status: 'active', label: 'Active', color: 'green' };
  }
  
  // Completed
  if (status === 'completed' || status === 'done') {
    return { status: 'completed', label: 'Completed', color: 'green' };
  }
  
  // Odoo 'sale' status - check payment
  if (status === 'sale' || status === 'confirmed') {
    // If payment completed, show as completed
    if (paymentStatus === 'invoiced' || paymentStatus === 'paid') {
      return { status: 'completed', label: 'Completed', color: 'green' };
    }
    // Otherwise, just approved awaiting payment
    return { status: 'approved', label: 'Approved', color: 'blue' };
  }
  
  // Pending Payment
  if (status === 'pending_payment') {
    return { status: 'pending_payment', label: 'Pending Payment', color: 'yellow' };
  }
  
  // Draft
  if (status === 'draft') {
    return { status: 'draft', label: 'Draft', color: 'gray' };
  }
  
  return { status: status, label: status, color: 'gray' };
};
```

## Backend Status Mapping

### Odoo to Portal Status Mapping

```php
private function mapOdooOrderStatus($odooState)
{
    return match($odooState) {
        'draft', 'sent' => 'pending',
        'sale' => 'completed',  // Odoo 'sale' = confirmed order
        'done' => 'completed',
        'cancel' => 'cancelled',
        default => 'pending'
    };
}
```

**Note**: We map Odoo's `'sale'` to `'completed'` in the backend, but the frontend checks payment status to determine if it should display as "Completed" (paid) or "Approved" (not yet paid).

## Payment Button Logic

Show payment button when:
1. Order status is one of: `'completed'`, `'sale'`, `'confirmed'`, `'pending_payment'`
2. Payment status is NOT: `'paid'`, `'completed'`, `'invoiced'`
3. No subscription exists
4. Order is not cancelled

```javascript
const canPay = ['completed', 'sale', 'confirmed', 'pending_payment'].includes(order.order_status) &&
               paymentStatus && 
               paymentStatus !== 'paid' && 
               paymentStatus !== 'completed' &&
               paymentStatus !== 'invoiced' &&
               !hasSubscription;
```

## Tracking Steps Logic

### Step 2: Order Approved
- **Completed** if: `order_status === 'completed'` OR `order_status === 'sale'`
- **In Progress** if: `order_status === 'pending'`
- **Pending** otherwise

### Step 3: Payment
- **Completed** if: `payment_status === 'paid'` OR `payment_status === 'invoiced'` OR `hasSubscription`
- **In Progress** if: Order is approved but payment not received
- **Pending** otherwise

### Step 4-6: Installation & Service
- **Completed** if: `hasSubscription` (Odoo) OR `actual_installation_date` exists
- **In Progress/Pending** based on dates

## Examples

### Example 1: Odoo Order - Just Approved
```json
{
  "order_status": "sale",
  "payment_status": "no",
  "invoice_status": "to invoice",
  "subscription": null
}
```
**Display**: "Approved" (blue) - Awaiting payment

### Example 2: Odoo Order - Paid
```json
{
  "order_status": "sale",
  "payment_status": "paid",
  "invoice_status": "invoiced",
  "subscription": null
}
```
**Display**: "Completed" (green) - Payment received

### Example 3: Odoo Order - Active Subscription
```json
{
  "order_status": "sale",
  "payment_status": "paid",
  "invoice_status": "invoiced",
  "subscription": {"id": "odoo_subscription", "status": "active"}
}
```
**Display**: "Active" (green) - Service running

### Example 4: Portal Order - Pending Payment
```json
{
  "order_status": "pending_payment",
  "payment_status": "pending",
  "subscription": null
}
```
**Display**: "Pending Payment" (yellow)

### Example 5: Portal Order - Completed
```json
{
  "order_status": "completed",
  "payment_status": "paid",
  "subscription": null
}
```
**Display**: "Completed" (green)

## Key Differences

| Aspect | Real (Odoo) | Simulation (Portal) |
|--------|-------------|---------------------|
| Order Creation | Odoo first, then synced | Portal database only |
| Status Field | `'sale'` when confirmed | `'pending_payment'` → `'completed'` |
| Payment Check | `invoice_status` field | `payment_status` field |
| Subscription | Checked in Odoo | Not applicable |
| Completion | Subscription = Active | Status = Completed |

## Testing Checklist

- [ ] Odoo order without payment shows "Approved"
- [ ] Odoo order with payment shows "Completed"
- [ ] Odoo order with subscription shows "Active"
- [ ] Portal order pending payment shows "Pending Payment"
- [ ] Portal order after payment shows "Completed"
- [ ] Payment button shows/hides correctly in all scenarios
- [ ] Tracking steps reflect correct progress in all scenarios
