66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Fetches the PLN to EUR conversion rate from the database.
|
|
*
|
|
* @param PDO $db The database connection object.
|
|
* @return float The conversion rate. Defaults to 0.23 if not set or invalid.
|
|
*/
|
|
function get_pln_to_eur_rate(PDO $db): float {
|
|
static $rate = null;
|
|
|
|
if ($rate === null) {
|
|
try {
|
|
$stmt = $db->prepare("SELECT value FROM settings WHERE `key` = 'pln_to_eur_rate'");
|
|
$stmt->execute();
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($result && is_numeric($result['value']) && $result['value'] > 0) {
|
|
$rate = (float)$result['value'];
|
|
} else {
|
|
$rate = 0.23; // Fallback default
|
|
}
|
|
} catch (PDOException $e) {
|
|
// Log error if possible, for now just use fallback
|
|
error_log('Could not fetch PLN to EUR rate: ' . $e->getMessage());
|
|
$rate = 0.23;
|
|
}
|
|
}
|
|
|
|
return $rate;
|
|
}
|
|
|
|
/**
|
|
* Converts a PLN amount to EUR.
|
|
*
|
|
* @param float $pln The amount in PLN.
|
|
* @param float $rate The PLN to EUR conversion rate.
|
|
* @return float The amount in EUR, rounded to 2 decimal places.
|
|
*/
|
|
function to_eur(float $pln, float $rate): float {
|
|
return round($pln * $rate, 2);
|
|
}
|
|
|
|
/**
|
|
* Formats a money amount based on the language and converts to EUR if necessary.
|
|
*
|
|
* @param float|null $amountPLN The amount in PLN. Can be null.
|
|
* @param string $lang The current language code ('en', 'pl', etc.).
|
|
* @param PDO $db The database connection object.
|
|
* @return string The formatted money string. Returns an empty string or placeholder if amount is null.
|
|
*/
|
|
function format_money(?float $amountPLN, string $lang, PDO $db): string {
|
|
if ($amountPLN === null) {
|
|
return '-'; // Or some other placeholder for null values
|
|
}
|
|
|
|
if ($lang === 'en') {
|
|
$rate = get_pln_to_eur_rate($db);
|
|
$eur = to_eur($amountPLN, $rate);
|
|
return '€' . number_format($eur, 2, '.', ',');
|
|
} else {
|
|
// Default to Polish PLN format
|
|
return number_format($amountPLN, 2, ',', ' ') . ' zł';
|
|
}
|
|
}
|