Skip to content

Commit

Permalink
Merge pull request ellite#53 from ttam/feature/format-currency
Browse files Browse the repository at this point in the history
Format currency in users locale
  • Loading branch information
ellite committed Nov 16, 2023
2 parents 0cd6f64 + a14bd55 commit 732aed3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 48 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ RUN apk add --no-cache sqlite-dev \
&& docker-php-ext-enable pdo pdo_sqlite

# Install additional PHP extensions and dependencies
RUN apk add --no-cache libpng libpng-dev libjpeg-turbo libjpeg-turbo-dev freetype freetype-dev curl autoconf libgomp \
RUN apk add --no-cache libpng libpng-dev libjpeg-turbo libjpeg-turbo-dev freetype freetype-dev curl autoconf libgomp icu-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install intl

# Install Imagick extension
RUN apk add --no-cache imagemagick imagemagick-dev \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ See instructions to run Wallos below.
- curl
- gd
- imagick
- intl
- openssl
- sqlite3

Expand Down
8 changes: 4 additions & 4 deletions endpoints/subscriptions/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'];
Expand All @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions includes/currency_formatter.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions includes/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions includes/list_subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function getPriceConverted($price, $currency, $database) {
}
}

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;
Expand Down Expand Up @@ -89,7 +89,7 @@ function printSubscriptons($subscriptions, $sort, $categories, $members) {
<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">
Expand Down
7 changes: 3 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions scripts/stats.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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: {
Expand Down
44 changes: 10 additions & 34 deletions stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ function getPriceConverted($price, $currency, $database) {
$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
Expand Down Expand Up @@ -113,16 +113,6 @@ function getPriceConverted($price, $currency, $database) {
$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');
Expand All @@ -144,26 +134,12 @@ function getPriceConverted($price, $currency, $database) {
}

}
$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;
Expand All @@ -179,23 +155,23 @@ function getPriceConverted($price, $currency, $database) {
<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
Expand Down Expand Up @@ -267,8 +243,8 @@ function getPriceConverted($price, $currency, $database) {
<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
Expand Down

0 comments on commit 732aed3

Please sign in to comment.