Skip to content

Commit

Permalink
Fix rounding and conversion issues on statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite committed Nov 12, 2023
1 parent cce3b0f commit 74d851e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion endpoints/subscriptions/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$print[$id]['payment_method_name'] = $payment_methods[$paymentMethodId]['name'];
$print[$id]['category_id'] = $subscription['category_id'];
$print[$id]['payer_user_id'] = $subscription['payer_user_id'];
$print[$id]['price'] = $subscription['price'];
$print[$id]['price'] = floatval($subscription['price']);

if (isset($_COOKIE['convertCurrency']) && $_COOKIE['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) {
$print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db);
Expand Down
13 changes: 11 additions & 2 deletions includes/list_subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ function getPriceConverted($price, $currency, $database) {
return $price;
} else {
$fromRate = $exchangeRate['rate'];
return number_format($price / $fromRate, 2);
return $price / $fromRate;
}
}

function printSubscriptons($subscriptions, $sort, $categories) {
function printSubscriptons($subscriptions, $sort, $categories, $members) {
if ($sort === "price") {
usort($subscriptions, function($a, $b) {
return $a['price'] < $b['price'] ? 1 : -1;
});
}

$currentCategory = 0;
$currentPayerUserId = 0;
foreach ($subscriptions as $subscription) {
if ($sort == "category_id" && $subscription['category_id'] != $currentCategory) {
?>
Expand All @@ -71,6 +72,14 @@ function printSubscriptons($subscriptions, $sort, $categories) {
<?php
$currentCategory = $subscription['category_id'];
}
if ($sort == "payer_user_id" && $subscription['payer_user_id'] != $currentPayerUserId) {
?>
<div class="subscription-list-title">
<?= $members[$subscription['payer_user_id']]['name'] ?>
</div>
<?php
$currentPayerUserId = $subscription['payer_user_id'];
}
?>
<div class="subscription">
<span class="logo <?= $subscription['hidelogo'] ?>"><img src="<?= $subscription['logo'] ?>"></span>
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
$print[$id]['payment_method_name'] = $payment_methods[$paymentMethodId]['name'];
$print[$id]['category_id'] = $subscription['category_id'];
$print[$id]['payer_user_id'] = $subscription['payer_user_id'];
$print[$id]['price'] = $subscription['price'];
$print[$id]['price'] = floatval($subscription['price']);

if (isset($_COOKIE['convertCurrency']) && $_COOKIE['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) {
$print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db);
Expand Down
4 changes: 2 additions & 2 deletions stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function getPriceConverted($price, $currency, $database) {
return $price;
} else {
$fromRate = $exchangeRate['rate'];
return number_format($price * $fromRate, 2, ".", "");
return $price / $fromRate;
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ function getPriceConverted($price, $currency, $database) {
<div class="title">Most Expensive Subscription Cost</div>
</div>
<div class="statistic">
<span><?= $amountDueThisMonth ?><?= $symbol ?></span>
<span><?= number_format($amountDueThisMonth, 2, ".", "") ?><?= $symbol ?></span>
<div class="title">Amount due this month</div>
</div>
<?php
Expand Down

0 comments on commit 74d851e

Please sign in to comment.