Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.
Format currency in users locale
Matt Bannon7529daab3e75
diff --git a/endpoints/subscriptions/get.php b/endpoints/subscriptions/get.phpindex 5c47754..bb6f256 100644--- a/endpoints/subscriptions/get.php+++ b/endpoints/subscriptions/get.php@@ -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) {diff --git a/includes/currency_formatter.php b/includes/currency_formatter.phpnew file mode 100644index 0000000..a1b3268--- /dev/null+++ b/includes/currency_formatter.php@@ -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);+ }+}diff --git a/includes/header.php b/includes/header.phpindex f81e87d..5fa2a26 100644--- a/includes/header.php+++ b/includes/header.php@@ -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();diff --git a/includes/list_subscriptions.php b/includes/list_subscriptions.phpindex a8893cc..697a262 100644--- a/includes/list_subscriptions.php+++ b/includes/list_subscriptions.php@@ -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">diff --git a/index.php b/index.phpindex efbeddf..5dec75d 100644--- a/index.php+++ b/index.php@@ -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();diff --git a/scripts/stats.js b/scripts/stats.jsindex a74f153..478e3ae 100644--- a/scripts/stats.js+++ b/scripts/stats.js@@ -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: {diff --git a/stats.php b/stats.phpindex 1843073..00f0d29 100644--- a/stats.php+++ b/stats.php@@ -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.codeFROM currencies cINNER JOIN user u ON c.id = u.main_currencyWHERE 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