Skip to content

Exchange Rates, Sync & History

Currency separates live spot rates (stored on currency-currencies) from locked historical snapshots (stored on currency-rate-history) so historical accounting lookups remain deterministic even after future rate updates.

Provider Key Class Notes
ecb EcbProvider Fetches European Central Bank daily XML (eurofxref-daily.xml) and 90-day historical XML (eurofxref-hist-90d.xml).
tcmb TcmbProvider Fetches Central Bank of Turkey daily XML (today.xml) supporting forex_selling (and buying/banknote types), automatically skipping weekends and looking back up to 5 business days on public holidays (HTTP 404).
manual ManualProvider Snapshots manually entered rates from currency-currencies into currency-rate-history without external network calls.
<?php namespace ProcessWire;
use Totoglu\Currency\Value\Money;
$currency = Currency::requireInstance($wire);
// Spot conversion with currency spread markup applied
$usd = $currency->convert(Money::fromMajor('14700.00', 'EUR'), 'USD', date: null, applySpread: true);
// $usd->converted, $usd->fxRate, $usd->provider, $usd->effectiveDate, $usd->spreadApplied
// Exact 8-decimal rate lookup on or before a historical date (cross-rate aware)
$rate = $currency->exchangeRate('USD', 'TRY', date: '2026-06-01', applySpread: false);
// Direct RateHistoryRepository queries
$hist = $currency->rateHistory();
$direct = $hist->findRateOnOrBefore('EUR', 'USD', '2026-06-01');
$cross = $hist->findCrossRateOnOrBefore('GBP', 'TRY', '2026-06-01');

Synchronizing Rates & Atomic Base Currency Switching

Section titled “Synchronizing Rates & Atomic Base Currency Switching”
  • Live Sync (syncRates()): Performs the HTTPS fetch outside database locks, then acquires CurrencyWriteLock::run() to persist currency-rate-history snapshots and update currencies whose rate_source is auto or matches the synced provider key (leaving manual and other provider-pinned currencies untouched).
  • Historical Backfill (syncRates($provider, '2026-06-01')): Writes locked snapshots to currency-rate-history without overwriting live spot rates or last_sync_* timestamps. Future dates (> today) are rejected.
  • Atomic Base Currency Switch ($currency->converter()->setBaseCurrency('USD', rebaseExistingRates: true)): Runs inside CurrencyWriteLock::run() via Entries::saveMany(), promoting the target currency to 1.00000000 and rebasing all sibling rates atomically.
Every sync persists immutable rate snapshots that power point-in-time historical conversions and cross-rate resolution.

Implement ExchangeRateProviderInterface (key(), label(), fetchRates()) using WireHttpTransport and register it via $currency->registerExchangeRateProvider($provider) (provider keys must match [a-z0-9_-]{1,32}):

<?php namespace ProcessWire;
use Totoglu\Currency\Http\WireHttpTransport;
$currency = Currency::requireInstance($wire);
$currency->registerExchangeRateProvider(new AegeanTreasuryRateProvider(new WireHttpTransport()));

See site/modules/Currency/examples/real-world/custom-rate-provider.php for a complete implementation.

Step-by-Step: Adding a New Currency & Syncing Rates (Admin UI)

Section titled “Step-by-Step: Adding a New Currency & Syncing Rates (Admin UI)”
  1. Open Setup -> Currencies -> Currencies and click + New Currency.
  2. Enter the ISO 4217 code (for example GBP), decimal places (2), optional spread markup percentage (1.5), and leave Rate Source set to auto.
  3. Save the currency, then return to Setup -> Currencies -> Overview and click Sync Rates Now (or run php vendor/bin/wire currency:sync --json).
{
"ok": true,
"provider": "ecb",
"baseCurrency": "EUR",
"effectiveDate": "2026-12-05",
"updated": ["USD", "GBP", "TRY"],
"skippedManual": []
}

Next steps