# Exchange Rate System

## Overview
The exchange rate system is centrally managed through the admin settings and affects all currency conversions throughout the application.

## Admin Configuration
Admins can set the exchange rate at: `/admin/settings`

The exchange rate is stored in the `settings` table with key `exchange_rate`.

## Where the Exchange Rate is Used

### 1. Home Page (`/`)
- **Live Rate Display**: Shows current rate in the hero section
- **Calculator**: Real-time GHS to CNY conversion calculator
- **Controller**: `HomeController@index`

### 2. Exchange Page (`/exchange`)
- **Rate Display**: Shows current exchange rate
- **Calculator**: Calculates CNY amount based on GHS input
- **Controller**: `ExchangeController@index` and `ExchangeController@preview`

### 3. User Dashboard (`/dashboard`)
- **Rate Strip**: Displays live rate at the top
- **Quick Exchange**: Mini calculator for quick conversions
- **Controller**: `DashboardController@index`

### 4. Order Creation
- **Exchange Calculation**: Used when creating new orders
- **Stored Value**: Each order stores the exchange rate used at the time of creation
- **Controller**: `ExchangeController@preview` and `ExchangeController@create`

## How It Works

### Getting the Exchange Rate
The system uses a fallback mechanism:
1. First, tries to get rate from database (`settings` table)
2. If not found, falls back to config file (`config/exchange.php`)
3. If config not set, uses default value of `0.57`

### Code Example
```php
use App\Models\Setting;

// Get current exchange rate
$rate = Setting::get('exchange_rate', config('exchange.rate_ghs_to_cny', 0.57));
```

### Using the Helper Class
```php
use App\Helpers\ExchangeHelper;

// Get current rate
$rate = ExchangeHelper::getExchangeRate();

// Convert GHS to CNY
$cnyAmount = ExchangeHelper::convertGhsToCny(1000); // 1000 GHS to CNY

// Convert CNY to GHS
$ghsAmount = ExchangeHelper::convertCnyToGhs(570); // 570 CNY to GHS
```

## Database Schema

### Settings Table
```sql
CREATE TABLE settings (
    id BIGINT PRIMARY KEY,
    key VARCHAR(255) UNIQUE,
    value TEXT,
    created_at TIMESTAMP,
    updated_at TIMESTAMP
);
```

### Example Record
```sql
INSERT INTO settings (key, value) VALUES ('exchange_rate', '0.57');
```

## Updating the Exchange Rate

### Via Admin Panel
1. Login as admin
2. Navigate to `/admin/settings`
3. Update the "GHS to CNY Exchange Rate" field
4. Click "Save Settings"
5. Changes take effect immediately across all pages

### Via Code
```php
use App\Models\Setting;

Setting::set('exchange_rate', 0.58);
```

### Via Database
```sql
UPDATE settings SET value = '0.58' WHERE key = 'exchange_rate';
```

## Important Notes

1. **Immediate Effect**: Changes to the exchange rate take effect immediately without requiring cache clearing or server restart.

2. **Historical Orders**: Each order stores the exchange rate used at the time of creation, so historical orders are not affected by rate changes.

3. **Precision**: The exchange rate supports up to 4 decimal places (e.g., 0.5712).

4. **Validation**: The admin settings form validates that the exchange rate is a positive number.

5. **Default Value**: If no rate is set in the database, the system falls back to the configured default (0.57).

## Files Modified

- `app/Http/Controllers/HomeController.php`
- `app/Http/Controllers/ExchangeController.php`
- `app/Http/Controllers/DashboardController.php`
- `app/Http/Controllers/Admin/AdminController.php`
- `app/Helpers/ExchangeHelper.php`
- `resources/views/home.blade.php`
- `resources/views/exchange.blade.php`
- `resources/views/dashboard.blade.php`
- `resources/views/admin/settings.blade.php`
