Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

fix: calculation of monthly cost progress graph (#747)

Miguel Ribeiro77486ec92c44
diff --git a/endpoints/cronjobs/storetotalyearlycost.php b/endpoints/cronjobs/storetotalyearlycost.php
index 2fead74..9277605 100644
--- a/endpoints/cronjobs/storetotalyearlycost.php
+++ b/endpoints/cronjobs/storetotalyearlycost.php
@@ -10,6 +10,24 @@ if (php_sapi_name() == 'cli') {
$currentDate = new DateTime();
$currentDateString = $currentDate->format('Y-m-d');
+function getPricePerMonth($cycle, $frequency, $price)
+{
+ switch ($cycle) {
+ case 1:
+ $numberOfPaymentsPerMonth = (30 / $frequency);
+ return $price * $numberOfPaymentsPerMonth;
+ case 2:
+ $numberOfPaymentsPerMonth = (4.35 / $frequency);
+ return $price * $numberOfPaymentsPerMonth;
+ case 3:
+ $numberOfPaymentsPerMonth = (1 / $frequency);
+ return $price * $numberOfPaymentsPerMonth;
+ case 4:
+ $numberOfMonths = (12 * $frequency);
+ return $price / $numberOfMonths;
+ }
+}
+
function getPriceConverted($price, $currency, $database, $userId)
{
$query = "SELECT rate FROM currencies WHERE id = :currency AND user_id = :userId";
@@ -38,13 +56,14 @@ while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$userCurrencyId = $row['main_currency'];
$totalYearlyCost = 0;
- $query = "SELECT * FROM subscriptions WHERE user_id = :userId";
+ $query = "SELECT * FROM subscriptions WHERE user_id = :userId AND inactive = 0";
$stmt = $db->prepare($query);
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$resultSubscriptions = $stmt->execute();
while ($rowSubscriptions = $resultSubscriptions->fetchArray(SQLITE3_ASSOC)) {
- $price = getPriceConverted($rowSubscriptions['price'], $rowSubscriptions['currency_id'], $db, $userId);
+ $originalSubscriptionPrice = getPriceConverted($rowSubscriptions['price'], $rowSubscriptions['currency_id'], $db, $userId);
+ $price = getPricePerMonth($rowSubscriptions['cycle'], $rowSubscriptions['frequency'], $originalSubscriptionPrice) * 12;
$totalYearlyCost += $price;
}
diff --git a/includes/version.php b/includes/version.php
index 81a4134..f68e0b1 100644
--- a/includes/version.php
+++ b/includes/version.php
@@ -1,3 +1,3 @@
<?php
-$version = "v2.46.0";
+$version = "v2.46.1";
?>
\ No newline at end of file
diff --git a/migrations/000035.php b/migrations/000035.php
new file mode 100644
index 0000000..28c0a34
--- /dev/null
+++ b/migrations/000035.php
@@ -0,0 +1,4 @@
+<?php
+// This migration clears the total_yearly_cost table as the calculation up to this point was incorrect
+
+$db->exec('DELETE FROM total_yearly_cost');
diff --git a/stats.php b/stats.php
index 76fa4c4..65ea780 100644
--- a/stats.php
+++ b/stats.php
@@ -535,7 +535,7 @@ $showTotalMonthlyCostGraph = count($totalMonthlyCostDataPoints) > 1;
}
$showPaymentMethodsGraph = count($paymentMethodDataPoints) > 1;
- if ($showCategoryCostGraph || $showMemberCostGraph || $showPaymentMethodsGraph) {
+ if ($showCategoryCostGraph || $showMemberCostGraph || $showPaymentMethodsGraph || $showTotalMonthlyCostGraph) {
?>
<h2><?= translate('split_views', $i18n) ?></h2>
<div class="graphs">