# Pricing Display Fix - NaN and $0.00 Issues

## Problem
User reported two issues:
1. Order detail page showing $0.00 for order prices
2. Cart showing NaN in some places

## Root Causes

### Frontend Issue (NaN in Cart)
- Null or undefined pricing values were being used in arithmetic operations before being passed to `formatCurrency()`
- `parseFloat(null)` or `parseFloat(undefined)` returns `NaN`
- Arithmetic operations with `NaN` propagate the `NaN` value
- While `formatCurrency()` handles null/undefined by returning '$0.00', it doesn't handle `NaN` values

### Backend Issue ($0.00 in Order Details)
- When fetching Odoo orders, the backend was hardcoding pricing fields to 0:
  ```php
  'monthly_subscription' => 0, // Not available from Odoo
  'installation_fee' => 0,
  'equipment_cost' => 0,
  'extension_cost' => 0,
  ```
- This happened because Odoo orders don't have these fields in the same format
- However, orders created in the portal DO have a local database record with correct pricing

## Solutions Applied

### 1. Frontend - Order Detail Page (`afinet-portal/src/app/(portal)/orders/[order_number]/page.js`)
Added null coalescing operators (`|| 0`) to all pricing fields:
- Line 323: `order.monthly_subscription || 0`
- Line 398: `order.monthly_subscription || 0`
- Line 411: `order.installation_fee || 0`
- Line 419: `order.equipment_cost || 0`
- Line 428: `order.extension_cost || 0`
- Line 439: `order.total_amount || 0`

### 2. Frontend - Cart Page (`afinet-portal/src/app/(portal)/cart/page.js`)
Fixed all arithmetic operations to handle null/undefined values:

**In totals calculation (lines 67-88):**
- `parseFloat(item.monthly_price) || 0`
- `parseFloat(item.one_time_fee) || 0`
- `parseFloat(s.price) || 0` (for value-added services)
- `item.quantity || 1`

**In item display (lines 202-217):**
- `hasMonthly = Number(item.monthly_price || 0) > 0`
- `hasOneTime = Number(item.one_time_fee || 0) > 0`
- `formatCurrency(parseFloat(item.monthly_price) || 0)`
- `formatCurrency(parseFloat(item.one_time_fee) || 0)`
- `item.quantity || 1`

### 3. Backend - SalesOrderController (`afinet-portal-backend/app/Http/Controllers/API/SalesOrderController.php`)
Modified the `show()` method to fetch pricing from local database when available:

**Before:**
```php
'monthly_subscription' => 0, // Not available from Odoo
'installation_fee' => 0,
'equipment_cost' => 0,
'extension_cost' => 0,
```

**After:**
```php
// Check if there's a local order record with pricing info
$localOrder = SalesOrder::where('odoo_order_id', $odooOrder['id'])
    ->orWhere('order_number', $odooOrder['name'])
    ->with(['productPackage'])
    ->first();

// Use local order pricing if available, otherwise use Odoo total or 0
$monthlySubscription = $localOrder ? ($localOrder->monthly_subscription ?? 0) : 0;
$installationFee = $localOrder ? ($localOrder->installation_fee ?? 0) : 0;
$equipmentCost = $localOrder ? ($localOrder->equipment_cost ?? 0) : 0;
$extensionCost = $localOrder ? ($localOrder->extension_cost ?? 0) : 0;
$totalAmount = $localOrder ? ($localOrder->total_amount ?? 0) : ($odooOrder['amount_total'] ?? 0);
```

This ensures that when viewing an Odoo order that was originally created in the portal, the correct pricing from the local database is displayed.

### 4. Other Pages Already Safe
- **Orders table page**: Already uses `formatCurrency(order.total_amount)` which handles null
- **Dashboard page**: Already uses `order.amount || order.amount_total || 0`
- **Checkout page**: Already uses `parseFloat(item.monthly_price) || 0` pattern

## How It Works Now

1. **For portal-created orders**: Pricing is stored in local database and displayed correctly
2. **For Odoo orders with local record**: Backend fetches pricing from local database
3. **For pure Odoo orders**: Backend uses Odoo's `amount_total` or falls back to $0.00
4. **Frontend safety**: All arithmetic operations have null/undefined guards to prevent NaN

## Testing Recommendations
1. Test order detail page with orders that have null pricing fields
2. Test cart with items that have null/undefined monthly_price or one_time_fee
3. Verify no NaN displays anywhere in the UI
4. Verify $0.00 displays correctly for zero-value items
5. Test viewing Odoo orders that were created in the portal (should show correct pricing)
6. Test viewing pure Odoo orders (should show Odoo total or $0.00)

## Files Modified
1. `afinet-portal/src/app/(portal)/orders/[order_number]/page.js` (Frontend)
2. `afinet-portal/src/app/(portal)/cart/page.js` (Frontend)
3. `afinet-portal-backend/app/Http/Controllers/API/SalesOrderController.php` (Backend)

## Status
✅ Fixed - All pricing displays now handle null/undefined values gracefully
✅ Backend now fetches pricing from local database when available for Odoo orders
