# Testing Odoo Workflow - Complete Guide

## Current Status: ✅ CONNECTION IS SOLID

The portal can READ all Odoo statuses. The Odoo team will manage status updates manually.

## Complete Workflow (Portal ↔ Odoo)

### Step 1: Customer Requests Quotation
**Portal Action:**
```
Customer fills quotation form → Portal creates quotation
```

**Odoo Result:**
```
Quotation created with:
- state = 'draft'
- amount_total = $0.00
- Product details in notes
```

**Test Command:**
```bash
# Check quotation was created in Odoo
php check-odoo-statuses.php
# Look for recent draft orders with your reference number
```

---

### Step 2: Odoo Team Adds Products & Pricing
**Odoo Team Action:**
```
1. Open quotation in Odoo (state=draft)
2. Read product details from notes
3. Add appropriate products
4. Set pricing
5. Save quotation
```

**Odoo Result:**
```
Quotation now has:
- state = 'draft' (still)
- amount_total = $XXX.XX (actual price)
- Order lines with products
```

**Test Command:**
```bash
# Check quotation now has amount
php -r "
require 'vendor/autoload.php';
\$app = require 'bootstrap/app.php';
\$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
\$odoo = new App\Services\OdooService();
\$quot = \$odoo->call('sale.order', 'search_read', [['id', '=', YOUR_ODOO_ID]], ['name', 'amount_total', 'state']);
print_r(\$quot);
"
```

---

### Step 3: Customer Accepts Quotation
**Portal Action:**
```
Customer clicks "Accept" → Portal converts to sales order
```

**Odoo Result:**
```
Same Odoo record, state changes:
- state = 'sale' (confirmed)
- amount_total = $XXX.XX (unchanged)
- invoice_status = 'to invoice'
```

**Test Command:**
```bash
# Check state changed to 'sale'
php check-odoo-statuses.php
# Look for orders with state='sale'
```

---

### Step 4: Customer Makes Payment
**Portal Action:**
```
Customer pays → Portal records payment
```

**Odoo Result:**
```
Order remains:
- state = 'sale' (unchanged)
- Invoice generated (if successful)
- Payment recorded (if successful)
```

**Test Command:**
```bash
# Check if invoice was generated
php -r "
require 'vendor/autoload.php';
\$app = require 'bootstrap/app.php';
\$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
\$odoo = new App\Services\OdooService();
\$invoices = \$odoo->call('account.move', 'search_read', 
    [['invoice_origin', '=', 'DFAZ-SO-XXXX']], 
    ['name', 'state', 'amount_total', 'payment_state']
);
print_r(\$invoices);
"
```

---

### Step 5: Installation/Delivery Happens
**Portal Action:**
```
Installation team completes work → Portal updates internally
```

**Odoo Result:**
```
Order remains:
- state = 'sale' (unchanged - waiting for Odoo team)
```

**Manual Step Required:**
```
Odoo team should manually update:
- state = 'done'
- delivery_status = 'full'
```

---

### Step 6: Odoo Team Marks Complete
**Odoo Team Action:**
```
1. Open order in Odoo
2. Click "Lock" or set state to 'done'
3. Update delivery_status to 'full'
```

**Odoo Result:**
```
Order now shows:
- state = 'done' (completed)
- delivery_status = 'full'
```

**Test Command:**
```bash
# Verify portal can read the 'done' state
php test-odoo-status-sync.php
```

---

## Testing Checklist

### ✅ Before Odoo Team Involvement
- [ ] Portal creates quotation in Odoo (state=draft)
- [ ] Product details appear in Odoo notes
- [ ] Reference number is correct
- [ ] Customer is linked (partner_id)

### ⏳ Requires Odoo Team
- [ ] Odoo team adds products to quotation
- [ ] Amount updates from $0.00 to actual price
- [ ] Portal can read updated amount

### ✅ After Customer Accepts
- [ ] Portal converts quotation to order (state=sale)
- [ ] State changes from 'draft' to 'sale' in Odoo
- [ ] Portal can read 'sale' state

### ✅ After Payment
- [ ] Invoice generated in Odoo (if successful)
- [ ] Payment recorded in Odoo (if successful)
- [ ] Portal can read invoice status

### ⏳ Requires Odoo Team (Final Step)
- [ ] Odoo team marks order as 'done'
- [ ] Portal can read 'done' state
- [ ] Order tracking shows completed

---

## Quick Test Commands

### Check Recent Quotations
```bash
php check-odoo-statuses.php
```

### Check Specific Order
```bash
php -r "
require 'vendor/autoload.php';
\$app = require 'bootstrap/app.php';
\$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
\$odoo = new App\Services\OdooService();
\$order = \$odoo->call('sale.order', 'search_read', 
    [['id', '=', YOUR_ODOO_ID]], 
    ['name', 'state', 'amount_total', 'invoice_status', 'delivery_status']
);
print_r(\$order);
"
```

### Test Full Sync
```bash
php test-odoo-status-sync.php
```

### Create Test Quotation
```bash
php test-quotation-fix.php
```

---

## What You Can Test NOW (Without Odoo Team)

1. ✅ **Quotation Creation**
   ```bash
   php test-quotation-fix.php
   ```
   - Creates quotation in Odoo
   - Verifies product details in notes
   - Confirms state='draft'

2. ✅ **Reading Odoo States**
   ```bash
   php test-odoo-status-sync.php
   ```
   - Reads all order states
   - Shows portal can handle all states
   - Confirms connection works

3. ✅ **Status Conversion**
   - Create quotation in portal
   - Accept it (converts to order)
   - Check Odoo shows state='sale'

## What Requires Odoo Team

1. ⏳ **Adding Products/Pricing**
   - Only Odoo team can add products
   - Portal will show $0.00 until they do

2. ⏳ **Marking Complete**
   - Only Odoo team can set state='done'
   - Portal will show 'sale' until they do

3. ⏳ **Cancellation**
   - Only Odoo team can set state='cancel'
   - Portal tracks cancellation internally

---

## Future Automation (Optional)

If you want to automate status updates later:

### Auto-Complete Orders
```php
// Add to InstallationController after marking complete
if ($salesOrder->odoo_order_id) {
    $odooService->call('sale.order', 'write', 
        [$salesOrder->odoo_order_id], 
        ['state' => 'done']
    );
}
```

### Auto-Cancel Orders
```php
// Add to OrderController when cancelling
if ($salesOrder->odoo_order_id) {
    $odooService->call('sale.order', 'write', 
        [$salesOrder->odoo_order_id], 
        ['state' => 'cancel']
    );
}
```

### Periodic Sync
```php
// Create scheduled job to sync Odoo states back to portal
// Run every hour to pull latest states from Odoo
```

---

## Summary

**✅ What's Working:**
- Portal → Odoo: Quotation creation (draft)
- Portal → Odoo: Order conversion (sale)
- Portal → Odoo: Payment recording
- Portal ← Odoo: Reading all states
- Portal ← Odoo: Reading invoices
- Portal ← Odoo: Reading payments

**⏳ What's Manual (By Design):**
- Odoo team adds products/pricing
- Odoo team marks orders complete (done)
- Odoo team handles cancellations

**🔗 Connection Status:**
- ✅ SOLID - Portal can read any status Odoo sets
- ✅ TESTED - All read operations work
- ✅ READY - For Odoo team to manage statuses

**📝 Next Steps:**
1. Test quotation creation (you can do this now)
2. Ask Odoo team to add products to a test quotation
3. Accept the quotation in portal
4. Make test payment
5. Ask Odoo team to mark it 'done'
6. Verify portal shows completed status

The connection is solid and ready for production! 🎉
