# ✅ Green Zone Automation - Setup Complete

## Configuration Applied

Your `.env` file has been configured with:

```env
# Generic product for portal orders (Shared Broadband - New Circuit)
ODOO_GENERIC_PRODUCT_ID=107
ODOO_VAT_TAX_ID=1

# Green Zone Automation
AUTO_APPROVE_HIGH_FEASIBILITY=true
AUTO_CREATE_ORDER_LINES=true
AUTO_APPROVE_CREDIT_THRESHOLD=0.8
```

## Product Mappings

### Starlink Package
- **Portal Package**: Starlink Residential (ID: 21)
- **Odoo Product**: Starlink Package - 1 TB Priority Access (ID: 2481, $155.00)
- ✅ **Status**: Linked and ready

### Generic Product (for packages without specific Odoo product)
- **Odoo Product**: Shared Broadband (MBB)-New Circuit (ID: 107, $1.00)
- **Usage**: Fallback for all other packages
- **Note**: Price will be overridden with actual package price from portal

## How It Works

### When a customer places an order with HIGH feasibility:

1. **Auto-Approval Check** (`UnifiedOrderService::shouldAutoApprove()`)
   - ✅ Feasibility = HIGH
   - ✅ Account status = active
   - ✅ Outstanding balance < 80% of credit limit
   - ✅ Package doesn't require custom quote

2. **If approved automatically:**
   - Order marked as `auto_approved = true`
   - `approved_at` timestamp set
   - `requires_manual_review = false`
   - Email sent to customer (order-auto-approved.blade.php)

3. **Order synced to Odoo** (`UnifiedOrderService::prepareOdooOrderData()`)
   - Creates sale order in Odoo
   - Adds order line with:
     - Product: Package-specific `odoo_product_id` OR generic product (107)
     - Description: Package name + speed
     - Price: Actual monthly subscription from portal
     - Quantity: 1
   - Adds detailed pricing breakdown to order notes:
     - Monthly subscription
     - Installation fee
     - Equipment cost
     - Extension cost
     - Router cost
     - Total amount

## Example Order Flow

### Starlink Order
```
Customer orders: Starlink Residential ($155/month)
Feasibility: HIGH
↓
Auto-approved immediately
↓
Email sent to customer
↓
Odoo order created with:
- Product: Starlink Package - 1 TB Priority Access (2481)
- Price: $155.00
- Description: "Starlink Residential"
- Notes: Full pricing breakdown
```

### Shared Broadband Order
```
Customer orders: Business 50Mbps ($89/month)
Feasibility: HIGH
↓
Auto-approved immediately
↓
Email sent to customer
↓
Odoo order created with:
- Product: Shared Broadband (MBB)-New Circuit (107)
- Price: $89.00 (overrides $1.00 default)
- Description: "Business 50Mbps - 50 Mbps"
- Notes: Full pricing breakdown
```

## Testing

### Test with a new order:

1. Create a customer account (or use existing)
2. Run coverage check → ensure HIGH feasibility result
3. Add a package to cart
4. Complete checkout
5. Check results:
   - Order should show `auto_approved = true` in database
   - Customer receives auto-approval email
   - Order appears in Odoo with order line and pricing
   - Dashboard shows correct pricing

### Manual test in Tinker:

```bash
php artisan tinker
```

```php
// Check if an order qualifies for auto-approval
$order = \App\Models\SalesOrder::where('feasibility_result', 'HIGH')->first();
$service = app(\App\Services\UnifiedOrderService::class);
$shouldApprove = $service->shouldAutoApprove($order);
var_dump($shouldApprove); // Should return true if all conditions met

// Check Odoo order data preparation
$odooData = $service->prepareOdooOrderData($order);
print_r($odooData); // Should show order_line array with product and pricing
```

## Monitoring

### Check auto-approved orders:

```sql
SELECT order_number, customer_id, total_amount, auto_approved, approved_at, odoo_order_id
FROM sales_orders
WHERE auto_approved = 1
ORDER BY approved_at DESC
LIMIT 10;
```

### Check orders pending manual review:

```sql
SELECT order_number, customer_id, total_amount, feasibility_result, requires_manual_review
FROM sales_orders
WHERE requires_manual_review = 1
AND status = 'pending'
ORDER BY created_at DESC;
```

## Email Template

Location: `afinet-portal-backend/resources/views/emails/order-auto-approved.blade.php`

Includes:
- Order number and amount
- Package details
- Next steps (payment, installation)
- Support contact info
- Professional AFINET branding

## Future Enhancements (Not Yet Implemented)

### Phase 3: Auto-Installation Scheduling
- Automatically schedule installation 7-14 days from order date
- Create installation task in Odoo
- Send installation confirmation email

### Phase 4: Auto-Service Activation
- Activate service automatically after payment confirmed
- Update subscription status
- Send welcome email with service details

## Troubleshooting

### Orders not auto-approving?

Check:
1. `AUTO_APPROVE_HIGH_FEASIBILITY=true` in `.env`
2. Feasibility result is exactly "HIGH" (case-sensitive)
3. Customer account status is "active"
4. Outstanding balance < 80% of credit limit
5. Package doesn't have `requires_custom_quote = true`

### Orders not creating lines in Odoo?

Check:
1. `AUTO_CREATE_ORDER_LINES=true` in `.env`
2. `ODOO_GENERIC_PRODUCT_ID=107` is set
3. Product 107 exists in Odoo (Shared Broadband MBB-New Circuit)
4. For Starlink: Package has `odoo_product_id = 2481`

### Pricing showing $0.00 in Odoo?

This is expected! The automation creates order lines with pricing, but:
- Staff may need to adjust pricing in Odoo
- The portal dashboard will always show correct pricing (uses portal data as fallback)
- Hourly sync updates portal with Odoo amounts when staff add/modify lines

## Support

For issues or questions:
- Check Laravel logs: `storage/logs/laravel.log`
- Check Odoo integration logs in database: `odoo_logs` table
- Review order notes in Odoo for detailed pricing breakdown
- Contact: tawona@quatrohaus.com

---

**Status**: ✅ Ready for Production Testing
**Last Updated**: 2026-02-25
