# IBF and ERM Billing Codes Implementation Guide

## Overview
Two new billing codes have been added to the wire transfer system:
1. **IBF (Intermediary Bank Fees)** - Charges applied by correspondent banks
2. **ERM (Exchange Rate Margins)** - Currency conversion cost margins

## Files Created/Modified

### 1. Database Changes
**File:** `add_ibf_erm_columns.sql`
- Adds `ibf` and `erm` columns to the `account` table
- Run this SQL in your database before using the new features

```sql
ALTER TABLE `account` 
ADD COLUMN `ibf` VARCHAR(20) DEFAULT NULL AFTER `imf`,
ADD COLUMN `erm` VARCHAR(20) DEFAULT NULL AFTER `ibf`;
```

### 2. New Verification Pages

#### IBF Code Verification Page
**File:** `dashboard/ibfcode.php`
- Validates Intermediary Bank Fees code
- Redirects to ERM code page upon successful validation
- Shows progress indicator during validation

#### ERM Code Verification Page
**File:** `dashboard/ermcode.php`
- Validates Exchange Rate Margins code
- Redirects to OTP page upon successful validation (final step)
- Shows 90% progress indicator

### 3. Updated Files

#### Billing Configuration
**File:** `dashboard/billing.php`
- Added two new billing code options:
  - Option 5: MOVE-TO-IBF-PAGE
  - Option 6: MOVE-TO-ERM-PAGE
- Updated description to include all billing codes
- Added documentation for new options

#### Account Editor
**File:** `dashboard/edit_account.php`
- Added IBF and ERM input fields in the form
- Updated SQL query to include IBF and ERM columns
- Form now captures and saves IBF and ERM codes

## Billing Code Flow

### Current Flow Options:
1. **Value 0:** Deactivate (OTP only) - No billing codes required
2. **Value 1:** Active with ALL billing codes (COT → IMF → TAX → IBF → ERM → OTP)
3. **Value 2:** Move to COT page only
4. **Value 3:** Move to TAX page only
5. **Value 4:** Move to IMF page only
6. **Value 5:** Move to IBF page only (NEW)
7. **Value 6:** Move to ERM page only (NEW)

### Complete Transfer Flow (When All Codes Active):
```
Wire Transfer Form
    ↓
COT Code Verification
    ↓
IMF Code Verification
    ↓
TAX Code Verification
    ↓
IBF Code Verification (NEW)
    ↓
ERM Code Verification (NEW)
    ↓
OTP Verification
    ↓
Transfer Complete
```

## Setup Instructions

### Step 1: Update Database
Run the SQL script to add new columns:
```bash
# In phpMyAdmin or MySQL client, run:
mysql -u your_username -p your_database < add_ibf_erm_columns.sql
```

Or manually execute:
```sql
ALTER TABLE `account` 
ADD COLUMN `ibf` VARCHAR(20) DEFAULT NULL AFTER `imf`,
ADD COLUMN `erm` VARCHAR(20) DEFAULT NULL AFTER `ibf`;
```

### Step 2: Set IBF and ERM Codes for Users
1. Go to Admin Panel → Edit Account
2. Find the user account
3. Enter IBF code (e.g., "12345")
4. Enter ERM code (e.g., "67890")
5. Save changes

### Step 3: Configure Billing Code
1. Go to Admin Panel → Billing Configuration
2. Select the user
3. Choose billing option:
   - "ACTIVE WITH ALL BILLING CODES" - Requires all codes including IBF and ERM
   - "MOVE-TO-IBF-PAGE" - Skip to IBF verification only
   - "MOVE-TO-ERM-PAGE" - Skip to ERM verification only
4. Save settings

### Step 4: Test the Flow
1. Login as the test user
2. Initiate a wire transfer
3. Verify each billing code page appears in sequence
4. Confirm successful transfer completion

## Code Descriptions

### IBF (Intermediary Bank Fees)
"Intermediary Bank Fees (IBF) are charges applied by correspondent or intermediary banks that facilitate international wire transfers. These fees cover the cost of processing and routing your funds through multiple banking networks to reach the beneficiary's bank."

### ERM (Exchange Rate Margins)
"Exchange Rate Margins (ERM) represent the difference between the interbank exchange rate and the rate applied to your international transfer. This margin covers currency conversion costs and market fluctuations."

## Admin Features

### Setting Individual Codes
Admins can set specific codes for each user in the Edit Account page:
- COT Code
- TAX Code
- IMF Code
- IBF Code (NEW)
- ERM Code (NEW)

### Billing Control
Admins have granular control over which billing codes are required:
- Enable/disable all codes
- Require specific codes only
- Skip directly to specific verification pages

## Technical Notes

### Session Variables
The following session variables are maintained throughout the transfer:
- `$_SESSION['amount']`
- `$_SESSION['acc_name']`
- `$_SESSION['bank_name']`
- `$_SESSION['swift']`
- `$_SESSION['cout']`
- `$_SESSION['transtype']`
- `$_SESSION['routing']`
- `$_SESSION['acctype']`
- `$_SESSION['remarks']`

### Validation Logic
Each code page:
1. Retrieves the stored code from the database
2. Compares user input with stored code
3. Shows error if mismatch
4. Redirects to next page if match
5. Displays progress indicator during validation

### Progress Indicators
- COT: 30%
- IMF: 30%
- TAX: (varies)
- IBF: 30%
- ERM: 90% (final billing code)

## Security Considerations

1. All codes are stored in the database per user
2. Codes are validated server-side
3. Session management prevents unauthorized access
4. Input sanitization prevents SQL injection
5. Account status checks (DORMANT, ON HOLD) are enforced

## Troubleshooting

### Issue: IBF/ERM columns not found
**Solution:** Run the SQL script to add the columns to the database

### Issue: Codes not saving
**Solution:** Check that the edit_account.php file includes IBF and ERM in the UPDATE query

### Issue: Redirect not working
**Solution:** Verify that ibfcode.php redirects to ermcode.php and ermcode.php redirects to otp.php

### Issue: Billing code option not showing
**Solution:** Clear browser cache and verify billing.php has been updated with options 5 and 6

## Future Enhancements

Potential improvements:
1. Add code expiration dates
2. Implement code generation system
3. Add email notifications for code requests
4. Create audit log for code usage
5. Add multi-factor authentication options

## Support

For issues or questions:
1. Check error logs in the server
2. Verify database structure matches requirements
3. Ensure all files are uploaded correctly
4. Test with a sample account first

---

**Implementation Date:** February 2026
**Version:** 1.0
**Status:** Ready for Production
