Format currency in users locale

This commit is contained in:
Matt Bannon 2023-11-16 17:03:29 +11:00
parent 0cd6f64978
commit 7529daab3e
No known key found for this signature in database
GPG Key ID: 187033B38F7D0527
7 changed files with 42 additions and 46 deletions

View File

@ -2,6 +2,7 @@
require_once '../../includes/connect_endpoint.php';
session_start();
require_once '../../includes/currency_formatter.php';
require_once '../../includes/getdbkeys.php';
include_once '../../includes/list_subscriptions.php';
@ -44,7 +45,7 @@
$frequency = $subscription['frequency'];
$print[$id]['billing_cycle'] = getBillingCycle($cycle, $frequency);
$paymentMethodId = $subscription['payment_method_id'];
$print[$id]['currency'] = $currencies[$subscription['currency_id']]['symbol'];
$print[$id]['currency_code'] = $currencies[$subscription['currency_id']]['code'];
$currencyId = $subscription['currency_id'];
$print[$id]['next_payment'] = date('M d, Y', strtotime($subscription['next_payment']));
$print[$id]['payment_method_icon'] = "images/uploads/icons/" . $payment_methods[$paymentMethodId]['icon'];
@ -55,16 +56,15 @@
if (isset($_COOKIE['convertCurrency']) && $_COOKIE['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) {
$print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db);
$print[$id]['currency'] = $currencies[$mainCurrencyId]['symbol'];
$print[$id]['currency_code'] = $currencies[$mainCurrencyId]['code'];
}
if (isset($_COOKIE['showMonthlyPrice']) && $_COOKIE['showMonthlyPrice'] === 'true') {
$print[$id]['price'] = getPricePerMonth($cycle, $frequency, $print[$id]['price']);
}
$print[$id]['price'] = number_format($print[$id]['price'], 2, ".", "");
}
if (isset($print)) {
printSubscriptons($print, $sort, $categories, $members);
printSubscriptions($print, $sort, $categories, $members);
}
if (count($subscriptions) == 0) {

View File

@ -0,0 +1,20 @@
<?php
final class CurrencyFormatter
{
private static $instance;
private static function getInstance()
{
if (self::$instance === null) {
self::$instance = new NumberFormatter(Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']), NumberFormatter::CURRENCY);
}
return self::$instance;
}
public static function format($amount, $currency)
{
return self::getInstance()->formatCurrency($amount, $currency);
}
}

View File

@ -2,6 +2,7 @@
require_once 'connect.php';
require_once 'checkuser.php';
require_once 'checksession.php';
require_once 'currency_formatter.php';
if ($userCount == 0) {
$db->close();

View File

@ -54,7 +54,7 @@
}
}
function printSubscriptons($subscriptions, $sort, $categories, $members) {
function printSubscriptions($subscriptions, $sort, $categories, $members) {
if ($sort === "price") {
usort($subscriptions, function($a, $b) {
return $a['price'] < $b['price'] ? 1 : -1;
@ -89,7 +89,7 @@
<span class="next"><?= $subscription['next_payment'] ?></span>
<span class="price">
<img src="<?= $subscription['payment_method_icon'] ?>" title="Payment Method: <?= $subscription['payment_method_name'] ?>"/>
<?= $subscription['price'] ?><?= $subscription['currency'] ?>
<?= CurrencyFormatter::format($subscription['price'], $subscription['currency_code']) ?>
</span>
<span class="actions">
<button class="image-button medium" onClick="openEditSubscription(event, <?= $subscription['id'] ?>)" name="edit">

View File

@ -72,7 +72,7 @@
$frequency = $subscription['frequency'];
$print[$id]['billing_cycle'] = getBillingCycle($cycle, $frequency);
$paymentMethodId = $subscription['payment_method_id'];
$print[$id]['currency'] = $currencies[$subscription['currency_id']]['symbol'];
$print[$id]['currency_code'] = $currencies[$subscription['currency_id']]['code'];
$currencyId = $subscription['currency_id'];
$print[$id]['next_payment'] = date('M d, Y', strtotime($subscription['next_payment']));
$print[$id]['payment_method_icon'] = "images/uploads/icons/" . $payment_methods[$paymentMethodId]['icon'];
@ -83,16 +83,15 @@
if (isset($_COOKIE['convertCurrency']) && $_COOKIE['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) {
$print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db);
$print[$id]['currency'] = $currencies[$mainCurrencyId]['symbol'];
$print[$id]['currency_code'] = $currencies[$mainCurrencyId]['code'];
}
if (isset($_COOKIE['showMonthlyPrice']) && $_COOKIE['showMonthlyPrice'] === 'true') {
$print[$id]['price'] = getPricePerMonth($cycle, $frequency, $print[$id]['price']);
}
$print[$id]['price'] = number_format($print[$id]['price'], 2, ".", "");
}
if (isset($print)) {
printSubscriptons($print, $sort, $categories, $members);
printSubscriptions($print, $sort, $categories, $members);
}
$db->close();

View File

@ -1,4 +1,4 @@
function loadGraph(container, dataPoints, symbol, run) {
function loadGraph(container, dataPoints, currency, run) {
if (run) {
var ctx = document.getElementById(container).getContext('2d');
@ -8,7 +8,7 @@ function loadGraph(container, dataPoints, symbol, run) {
datasets: [{
data: dataPoints.map(point => point.y),
}],
labels: dataPoints.map(point => `${point.label} (${point.y}${symbol})`),
labels: dataPoints.map(point => `(${new Intl.NumberFormat(navigator.language, { style: 'currency', currency }).format(point.y)})`),
},
options: {
animation: {

View File

@ -64,15 +64,15 @@ while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$categoryCost[$categoryId]['name'] = $row['name'];
}
// Get symbol of main currency to display on statistics
$query = "SELECT c.symbol
// Get code of main currency to display on statistics
$query = "SELECT c.code
FROM currencies c
INNER JOIN user u ON c.id = u.main_currency
WHERE u.id = 1";
$stmt = $db->prepare($query);
$result = $stmt->execute();
$row = $result->fetchArray(SQLITE3_ASSOC);
$symbol = $row['symbol'];
$code = $row['code'];
// Calculate active subscriptions
@ -113,16 +113,6 @@ if ($result) {
$mostExpensiveSubscription = $price;
}
$memberCost[$payerId]['cost'] = number_format($memberCost[$payerId]['cost'], 2, ".", "");
if ((int)$memberCost[$payerId]['cost'] == $memberCost[$payerId]['cost']) {
$memberCost[$payerId]['cost'] = (int)$memberCost[$payerId]['cost'];
}
$categoryCost[$categoryId]['cost'] = number_format($categoryCost[$categoryId]['cost'], 2, ".", "");
if ((int)$categoryCost[$categoryId]['cost'] == $categoryCost[$categoryId]['cost']) {
$categoryCost[$categoryId]['cost'] = (int)$categoryCost[$categoryId]['cost'];
}
// Calculate ammount due this month
$nextPaymentDate = DateTime::createFromFormat('Y-m-d', trim($next_payment));
$tomorrow = new DateTime('tomorrow');
@ -144,26 +134,12 @@ if ($result) {
}
}
$mostExpensiveSubscription = number_format($mostExpensiveSubscription, 2, ".", "");
// Calculate yearly price
$totalCostPerYear = $totalCostPerMonth * 12;
$totalCostPerYear = number_format($totalCostPerYear, 2, ".", "");
if ((int)$totalCostPerYear == $totalCostPerYear) {
$totalCostPerYear = (int)$totalCostPerYear;
}
// Calculate average subscription monthly cost
$averageSubscriptionCost = $totalCostPerMonth / $activeSubscriptions;
$averageSubscriptionCost = number_format($averageSubscriptionCost, 2, ".", "");
if ((int)$averageSubscriptionCost == $averageSubscriptionCost) {
$averageSubscriptionCost = (int)$averageSubscriptionCost;
}
$totalCostPerMonth = number_format($totalCostPerMonth, 2, ".", "");
if ((int)$totalCostPerMonth == $totalCostPerMonth) {
$totalCostPerMonth = (int)$totalCostPerMonth;
}
} else {
$totalCostPerYear = 0;
$averageSubscriptionCost = 0;
@ -179,23 +155,23 @@ if ($result) {
<div class="title">Active Subscriptions</div>
</div>
<div class="statistic">
<span><?= $totalCostPerMonth ?><?= $symbol ?></span>
<span><?= CurrencyFormatter::format($totalCostPerMonth, $code) ?></span>
<div class="title">Monthly Cost</div>
</div>
<div class="statistic">
<span><?= $totalCostPerYear ?><?= $symbol ?></span>
<span><?= CurrencyFormatter::format($totalCostPerYear, $code) ?></span>
<div class="title">Yearly Cost</div>
</div>
<div class="statistic">
<span><?= $averageSubscriptionCost ?><?= $symbol ?></span>
<span><?= CurrencyFormatter::format($averageSubscriptionCost, $code) ?></span>
<div class="title">Average Monthly Subscription Cost</div>
</div>
<div class="statistic">
<span><?= $mostExpensiveSubscription ?><?= $symbol ?></span>
<span><?= CurrencyFormatter::format($mostExpensiveSubscription, $code) ?></span>
<div class="title">Most Expensive Subscription Cost</div>
</div>
<div class="statistic">
<span><?= number_format($amountDueThisMonth, 2, ".", "") ?><?= $symbol ?></span>
<span><?= CurrencyFormatter::format($amountDueThisMonth, $code) ?></span>
<div class="title">Amount due this month</div>
</div>
<?php
@ -267,8 +243,8 @@ if ($result) {
<script src="scripts/libs/chart.js"></script>
<script type="text/javascript">
window.onload = function() {
loadGraph("categorySplitChart", <?php echo json_encode($categoryDataPoints, JSON_NUMERIC_CHECK); ?>, "<?= $symbol ?>", <?= $showCategoryCostGraph ?>);
loadGraph("memberSplitChart", <?php echo json_encode($memberDataPoints, JSON_NUMERIC_CHECK); ?>, "<?= $symbol ?>", <?= $showMemberCostGraph ?>);
loadGraph("categorySplitChart", <?php echo json_encode($categoryDataPoints, JSON_NUMERIC_CHECK); ?>, "<?= $code ?>", <?= $showCategoryCostGraph ?>);
loadGraph("memberSplitChart", <?php echo json_encode($memberDataPoints, JSON_NUMERIC_CHECK); ?>, "<?= $code ?>", <?= $showMemberCostGraph ?>);
}
</script>
<?php