# Order Amount Sync Guide

## Overview

The portal now automatically syncs order amounts from Odoo every hour. This ensures that when staff add order lines in Odoo, the portal database stays in sync.

## How It Works

### Automatic Sync (Every Hour)
The scheduler runs `orders:sync-amounts` every hour at the top of the hour (e.g., 1:00, 2:00, 3:00).

**What it does:**
- Fetches recent orders (last 30 days) from Odoo
- Compares Odoo `amount_total` with portal `total_amount`
- Updates portal if Odoo amount is greater than 0 and different
- Keeps portal amount if Odoo is still $0.00 (no order lines yet)

### Manual Sync (When Needed)

You can also run the sync manually:

```bash
# Sync recent orders (last 30 days)
php artisan orders:sync-amounts

# Sync all orders with Odoo IDs
php artisan orders:sync-amounts --all

# Sync specific order
php artisan orders:sync-amounts --order-id=3
```

## Workflow Example

### Scenario: Staff adds order lines in Odoo

1. **Customer places order** (10:00 AM)
   - Portal: $857.87 ✅
   - Odoo: $0.00 (no lines)
   - Dashboard: $857.87 (uses portal fallback) ✅

2. **Staff adds order lines in Odoo** (10:30 AM)
   - Portal: $857.87 (not synced yet)
   - Odoo: $857.87 (now has lines) ✅
   - Dashboard: $857.87 (uses Odoo amount) ✅

3. **Automatic sync runs** (11:00 AM)
   - Portal: $857.87 (synced from Odoo) ✅
   - Odoo: $857.87 ✅
   - Dashboard: $857.87 ✅

### Scenario: Staff changes pricing in Odoo

1. **Staff updates order line price** (2:15 PM)
   - Portal: $857.87 (old amount)
   - Odoo: $950.00 (new amount) ✅
   - Dashboard: $950.00 (uses Odoo amount) ✅

2. **Automatic sync runs** (3:00 PM)
   - Portal: $950.00 (synced from Odoo) ✅
   - Odoo: $950.00 ✅
   - Dashboard: $950.00 ✅

## Monitoring

### Check Scheduled Tasks
```bash
php artisan schedule:list
```

Expected output:
```
0 * * * * php artisan orders:sync-amounts  Next Due: X minutes from now
```

### View Sync Logs
Check `storage/logs/laravel.log` for sync activity:
```
[timestamp] Synced order amount from Odoo
  order_id: 3
  order_number: AFINET-SO-699C2E7046573
  old_amount: 857.87
  new_amount: 950.00
```

### Test Sync Manually
```bash
php artisan orders:sync-amounts
```

Expected output:
```
🔄 Syncing order amounts from Odoo...

Found 3 recent orders (last 30 days)

  ✅ AFINET-SO-699C2E7046573: $857.87 → $950.00
  ⏭️  AFINET-SO-699C2E20AFF48: No change ($857.87)
  ⏭️  AFINET-SO-699C2DF59FE0D: Still $0.00 in Odoo (keeping portal amount: $857.87)

Synced: 3

✅ Sync complete!
```

## Important Notes

1. **Dashboard always shows correct price** - Even before sync runs, the dashboard uses Odoo amount if available, or falls back to portal amount

2. **Sync only updates if Odoo > 0** - If Odoo still has $0.00 (no order lines), portal keeps its original amount

3. **No data loss** - Portal amounts are only updated if Odoo has a valid amount (> 0)

4. **Sync is one-way** - Portal → Odoo (when creating orders), Odoo → Portal (when syncing amounts)

## Scheduler Setup

The scheduler is already configured in `routes/console.php`:

```php
// Scheduled task: Sync order amounts from Odoo every hour
Schedule::command('orders:sync-amounts')->hourly()->withoutOverlapping();
```

Make sure your cron job is running:
```bash
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
```

Or on Windows, use Task Scheduler to run:
```
php artisan schedule:run
```
Every minute.

## Troubleshooting

### Sync not running automatically
1. Check if scheduler cron job is set up
2. Run `php artisan schedule:list` to verify task is registered
3. Check `storage/logs/laravel.log` for errors

### Orders not syncing
1. Verify order has `odoo_order_id` set
2. Check Odoo connection: `php artisan odoo:test-connection`
3. Run manual sync with verbose output: `php artisan orders:sync-amounts`

### Wrong amounts after sync
1. Check Odoo order has order lines
2. Verify order lines have correct `price_unit`
3. Check if order is confirmed in Odoo (state = 'sale')

## Future Enhancements

- **Webhook integration**: Real-time sync when Odoo orders change
- **Bi-directional sync**: Sync more fields (status, delivery, etc.)
- **Conflict resolution**: Handle cases where both systems have different amounts
