# Make Rates work EVERYWHERE (one-time setup)

## 1) Add Service Provider
Add to `config/app.php` providers array:
```php
App\Providers\RatesServiceProvider::class,
```

## 2) (Optional) Autoload helpers
Either require the file in `composer.json`:
```json
"autoload": {
  "files": [
    "app/helpers/rates.php"
  ]
}
```
Then run:
```
composer dump-autoload
```

## 3) Use in Controllers
```php
use App\Models\AppRate;
use App\Services\Pricing;

$r = AppRate::current();
$pricing = new Pricing($r);

// examples
$unrealAed = $pricing->unrealizedAed($qty_ttb, $entry_usd, $mark_usd, $side);
$netAed    = $pricing->netBalanceAed($balance_aed, $unrealAed);
$bidAsk    = $pricing->bidAskFromMid($mid_usd);
```

## 4) Use in Blades
All blades now have `$appRates` shared:
```blade
FX: {{ number_format($appRates->fx_usd_aed, 6) }}
@rate('fx_usd_aed') {{-- Blade directive --}}
```

Or fetch JSON:
```js
import { getRates } from '/resources/js/rates.js'
const r = await getRates();
```

## 5) Update your endpoints
- Make `/api/price` or `livePrice()` honor manual price + spread via `AppRate::current()` (see PATCHES).

## 6) Clear caches
```
php artisan optimize:clear
```

## 7) Result
Changing anything at **/settings/rates** immediately affects:
- FX conversions, P/L, Net Balance, Margin calculators
- Gold price endpoints (if you wired livePrice/price per patch)
- All Blade UIs via `$appRates` + `/api/rates`
