Currency Documentation
Currency Controller Documentation
File: /controllers/currencyController.php
Purpose: Manages multi-currency support, exchange rates, and currency conversion settings
Last Updated: December 19, 2024
Total Functions: 5
Lines of Code: 201
---
๐ Overview
The Currency Controller manages the multi-currency functionality in the ERP system. It handles:
- โข Creating and managing different currencies
- โข Setting conversion factors and exchange rates
- โข Primary currency management
- โข Currency symbols and display formats
- โข Soft deletion of unused currencies
- โข Integration with program settings for main currency
Primary Functions
- โ Add new currencies with conversion rates
- โ View all active currencies in listing
- โ Edit existing currency information
- โ Update currency conversion factors
- โ Soft delete (deactivate) currencies
- โ Special handling for primary currency updates
- โ Program settings integration
Related Controllers
- โข programsettingsController.md - Main currency settings
- โข sellbillController.md - Multi-currency sales
- โข buyBillController.md - Multi-currency purchases
- โข supplierController.md - Supplier currency preferences
- โข clientController.md - Customer currency preferences
- โข balancereportController.md - Currency conversion in reports
- โข accountstree.md - Account currency settings
- โข dailyentry.md - Multi-currency accounting entries
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| **currency** | Currency definitions | id, name, symbol, conversionFactor, otherconversionFactor, conditions, userid, sysDate |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **programsettings** | System main currency | programsettings.currancy linked to currency.name | |
| **buybillcurr** | Purchase currency details | References currency.id | |
| **sellbillcurr** | Sales currency details | References currency.id | |
| **youtubelink** | Associated help content | General system integration |
| Column | Type | Purpose | Notes | |
|---|---|---|---|---|
| **id** | int | Primary key | Auto-increment | |
| **name** | varchar | Currency name | e.g., "US Dollar", "Euro" | |
| **symbol** | varchar | Currency symbol | e.g., "$", "โฌ", "ยฃ" | |
| **conversionFactor** | decimal | Rate to main currency | Main currency = 1.0 | |
| **otherconversionFactor** | decimal | Alternative rate | For complex conversions | |
| **conditions** | int | Status flag | 0=active, 1=deleted | |
| **userid** | int | User who created/modified | Foreign key to user table | |
| **sysDate** | datetime | Last modification timestamp | Auto-updated |
๐ง Key Functions
1. add()
Purpose: Create a new currency with conversion factors
Called By: Form submission with ?do=add
Line: 132
Parameters (via $_POST):
- โข
name(string) - Currency name (e.g., "US Dollar") - โข
symbol(string) - Currency symbol (e.g., "$") - โข
conversionFactor(float) - Exchange rate to main currency - โข
othercFactor(float) - Alternative conversion factor
Process Flow:
Form Data Collection
โ
Input Validation & Filtering
โ
Currency Object Creation
โ
Set Default Properties
โ
Database Insert
โ
Redirect to Success
Business Logic:
1. Filters and validates input using filter_input()
2. Creates currency object with automatic properties:
- conditions = 0 (active status)
- userid from current session
- sysDate as current timestamp
3. Inserts into database via CurrencyDAO
Code Example:
$currency->name = $name;
$currency->symbol = $symbol;
$currency->conversionFactor = $conversionFactor;
$currency->otherconversionFactor = $otherconversionFactor;
$currency->conditions = 0; // Active
$currency->userid = $_SESSION['userid'];
$currency->sysDate = date('Y-m-d H:i:s');
2. show()
Purpose: Display all active currencies in listing format
Called By: Navigation to ?do=show
Line: 153
Database Query: $currencyDAO->queryByConditions(0) - Only active currencies
Process Flow:
Query Active Currencies (conditions=0)
โ
Assign to Smarty Template
โ
Display Currency Listing
โ
Include YouTube Links for Help
Business Logic:
1. Queries only active currencies (conditions = 0)
2. Excludes soft-deleted currencies
3. Integrates help content via YouTube links
4. Assigns data to template for display
3. edit()
Purpose: Load currency data for editing form
Called By: Edit action with ?do=edit&id=X
Line: 161
Parameters (via $_GET):
- โข
id(int) - Currency ID to edit
Return Value: Currency object with all current data
Process Flow:
Get Currency ID from URL
โ
Validate ID (integer cast)
โ
Load Currency from Database
โ
Return Currency Object
โ
Populate Edit Form
Business Logic:
1. Safely casts ID to integer for security
2. Loads complete currency record
3. Returns object for form population
4. update()
Purpose: Update existing currency information with special main currency handling
Called By: Form submission with ?do=update
Line: 169
Parameters (via $_POST):
- โข
id(int) - Currency ID to update - โข
name(string) - Updated currency name - โข
symbol(string) - Updated currency symbol - โข
conversionFactor(float) - Updated exchange rate - โข
othercFactor(float) - Updated alternative rate
Process Flow:
Business Logic:
1. Loads existing currency to preserve system fields
2. Updates modifiable fields with new values
3. Updates timestamp and user who modified
4. Special Case: If currency ID = 1 (main currency):
- Updates programsettings.currancy field
- Keeps system-wide currency name in sync
Main Currency Update Code:
if ($id == 1) {
$ProgramsettingEX->runSqlQuery(
"UPDATE programsettings SET currancy = '" . $currency->name . "' WHERE programsettingsid=1"
);
}
5. delete()
Purpose: Soft delete currency (mark as inactive)
Called By: Delete action with ?do=delete&id=X
Line: 193
Parameters (via $_GET):
- โข
id(int) - Currency ID to delete
Process Flow:
Get Currency ID
โ
Load Currency Record
โ
Set conditions = 1 (deleted)
โ
Update Database
โ
Currency Hidden from Lists
Business Logic:
1. Implements soft delete - doesn't remove from database
2. Sets conditions = 1 to mark as deleted
3. Preserves data integrity for historical transactions
4. Currency becomes invisible in active listings
Why Soft Delete:
- โข Historical bills may reference this currency
- โข Conversion calculations remain valid
- โข Audit trail preserved
- โข Can be reactivated if needed
---
๐ Business Logic Flow
Currency Creation Workflow
Main Currency Update Workflow
Currency Conversion Process
---
โ ๏ธ Common Issues
1. Main Currency Synchronization
Symptoms: Currency name mismatch between currency table and program settings
Causes: Direct database edits or failed updates
Solutions:
- โข Always update main currency through controller
- โข Check programsettings.currancy matches currency.name where id=1
- โข Run synchronization query if needed:
UPDATE programsettings SET currancy = (
SELECT name FROM currency WHERE id = 1
) WHERE programsettingsid = 1;
2. Conversion Factor Errors
Symptoms: Incorrect currency calculations in bills
Causes:
- โข Zero or negative conversion factors
- โข Inverted conversion rates
- โข Missing conversion factors
Solutions:
- โข Validate conversion factor > 0 before saving
- โข Document conversion direction clearly
- โข Test calculations with known amounts
3. Orphaned Currency References
Symptoms: Bills showing "unknown currency" or errors
Causes: Currency soft-deleted but still referenced in old bills
Solutions:
- โข Check bill dependencies before deletion
- โข Consider archiving instead of deletion
- โข Implement cascade handling for dependent records
4. Performance Issues with Currency Lookups
Symptoms: Slow loading of bills with multiple currencies
Causes: Frequent currency table queries without optimization
Solutions:
- โข Cache frequently used currency data
- โข Use JOIN queries instead of multiple lookups
- โข Index currency table properly
---
๐ Dependencies
Required Files
- โข
../public/impOpreation.php- Core system operations - โข
../public/config.php- Database configuration - โข
../public/include_dao.php- Data access layer includes
Required DAOs
- โข
CurrencyDAO.class.php- Currency data access interface - โข
Currency.class.php- Currency data transfer object - โข
CurrencyMySqlDAO.class.php- MySQL currency operations - โข
CurrencyMySqlExtDAO.class.php- Extended currency operations - โข
ProgramsettingsDAO.class.php- Program settings access - โข
Programsetting.class.php- Settings DTO - โข
ProgramsettingsMySqlDAO.class.php- Settings MySQL operations - โข
ProgramsettingsMySqlExtDAO.class.php- Extended settings operations
Template Files
- โข
currencyview/add.html- Currency creation form - โข
currencyview/show.html- Currency listing display - โข
currencyview/edit.html- Currency editing form - โข
header.html- Standard page header - โข
footer.html- Standard page footer - โข
succes.html- Success message template - โข
error.html- Error message template
Integration Dependencies
- โข Program Settings: Main currency name synchronization
- โข Bill Controllers: Multi-currency transaction support
- โข Report Controllers: Currency conversion for reporting
- โข Client/Supplier Controllers: Preferred currency settings
---
๐ฏ Usage Examples
Adding a New Currency
// Form data for adding Euro
$_POST = [
'name' => 'Euro',
'symbol' => 'โฌ',
'conversionFactor' => 0.85, // 1 USD = 0.85 EUR
'othercFactor' => 1.18 // 1 EUR = 1.18 USD
];
Conversion Calculation Example
// If main currency is USD and rate is 0.85 EUR per USD
$usd_amount = 100.00;
$eur_rate = 0.85;
$eur_amount = $usd_amount * $eur_rate; // 85.00 EUR
Main Currency Check
// Always check if updating main currency
if ($currency_id == 1) {
// Update both currency table and program settings
// This ensures system-wide consistency
}
---
๐ก Best Practices
Currency Management
1. Always validate conversion factors before saving
2. Test currency calculations with known amounts
3. Document conversion direction clearly
4. Use soft delete to preserve historical data
5. Keep main currency in sync with program settings
Performance Optimization
1. Cache currency data for frequent lookups
2. Index currency queries for better performance
3. Use batch operations for bulk currency updates
4. Monitor conversion calculations for accuracy
Data Integrity
1. Check dependencies before deleting currencies
2. Validate historical references remain intact
3. Backup before major changes to currency settings
4. Test multi-currency scenarios thoroughly