Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stats about inactive subscriptions #146

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions endpoints/subscriptions/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
$sort = "next_payment";
$sql = "SELECT * FROM subscriptions ORDER BY next_payment ASC";
$sql = "SELECT * FROM subscriptions ORDER BY next_payment ASC, inactive ASC";
if (isset($_COOKIE['sortOrder']) && $_COOKIE['sortOrder'] != "") {
$sort = $_COOKIE['sortOrder'];
$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id'];
Expand All @@ -25,7 +25,7 @@
$order = "DESC";
}
if (in_array($sort, $allowedSortCriteria)) {
$sql = "SELECT * FROM subscriptions ORDER BY $sort $order";
$sql = "SELECT * FROM subscriptions ORDER BY $sort $order, inactive ASC";
}
}

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Durchschnittliche monatliche Kosten",
'most_expensive' => "Kosten des teuersten Abonnements",
'amount_due' => "Diesen Monat fällige Summe",
'monthly_savings' => "Monatliche Ersparnisse (bei inaktiven Abonnements)",
'split_views' => "Aufgeteilte Ansichten",
'category_split' => "Kategorien",
'household_split' => "Haushalt",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Μέσο μηνιαίο κόστος συνδρομής",
'most_expensive' => "Πιο ακριβό κόστος συνδρομής",
'amount_due' => "Ποσό που οφείλεται αυτόν τον μήνα",
'monthly_savings' => "Μηνιαίες εξοικονομήσεις (σε ανενεργές συνδρομές)",
'split_views' => "Διαχωρισμένες προβολές",
'category_split' => "Διαχωρισμός κατηγορίας",
'household_split' => "Διαχωρισμός νοικοκυριού",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Average Monthly Subscription Cost",
'most_expensive' => "Most Expensive Subscription Cost",
'amount_due' => "Amount due this month",
'monthly_savings' => "Monthly Savings (on inactive subscriptions)",
'split_views' => "Split Views",
'category_split' => "Category Split",
'household_split' => "Household Split",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Costo Promedio Mensual de Suscripción",
'most_expensive' => "Costo de Suscripción Más Caro",
'amount_due' => "Monto a pagar este mes",
'monthly_savings' => "Ahorro Mensual (en suscripciones inactivas)",
'split_views' => "Vistas Divididas",
'category_split' => "División por Categoría",
'household_split' => "División por Hogar",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Coût moyen mensuel de l'abonnement",
'most_expensive' => "Coût d'abonnement le plus élevé",
'amount_due' => "Montant dû ce mois-ci",
'monthly_savings' => "Économies mensuelles (sur les abonnements inactifs)",
'split_views' => "Vues partagées",
'category_split' => "Répartition par catégorie",
'household_split' => "Répartition du ménage",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/jp.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "月額平均費用",
'most_expensive' => "最も高額な定期購入費用",
'amount_due' => "今月の支払額",
'monthly_savings' => "月間節約 (非アクティブな定期購入)",
'split_views' => "分割表示",
'category_split' => "カテゴリ別",
'household_split' => "世帯別",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Custo Mensal Médio das Subscrições",
'most_expensive' => "Custo da Subscrição Mais Cara",
'amount_due' => "Quantia em dívida este mês",
'monthly_savings' => "Poupança Mensal (em subscrições inactivas)",
'split_views' => "Vistas Divididas",
'category_split' => "Por Categoria",
'household_split' => "Por Membro",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'average_monthly' => "Ortalama Aylık Abonelik Maliyeti",
'most_expensive' => "En Pahalı Abonelik Maliyeti",
'amount_due' => "Bu ay ödenecek miktar",
'monthly_savings' => "Aylık Tasarruf (aktif olmayan aboneliklerde)",
'split_views' => "Bölünmüş Görünümler",
'category_split' => "Kategori Bölümü",
'household_split' => "Hane Bölümü",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/zh_cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'average_monthly' => "平均每月订阅费用",
'most_expensive' => "最昂贵订阅费用",
'amount_due' => "本月应付金额",
'monthly_savings' => "每月节省",
'split_views' => "拆分视图",
'category_split' => "分类视图",
'household_split' => "家庭视图",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/zh_tw.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'average_monthly' => "平均每月訂閱費用",
'most_expensive' => "最高的訂閱費用",
'amount_due' => "本月應付金額",
'monthly_savings' => "每月節省",
'split_views' => "分割表示",
'category_split' => "類別表示",
'household_split' => "家庭表示",
Expand Down
2 changes: 1 addition & 1 deletion includes/version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$version = "v1.5.0";
$version = "v1.6.0";
?>
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
include_once 'includes/list_subscriptions.php';

$sort = "next_payment";
$sql = "SELECT * FROM subscriptions ORDER BY next_payment ASC";
$sql = "SELECT * FROM subscriptions ORDER BY next_payment ASC, inactive ASC";
if (isset($_COOKIE['sortOrder']) && $_COOKIE['sortOrder'] != "") {
$sort = $_COOKIE['sortOrder'];
$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id'];
Expand All @@ -14,7 +14,7 @@
$order = "DESC";
}
if (in_array($sort, $allowedSortCriteria)) {
$sql = "SELECT * FROM subscriptions ORDER BY $sort $order";
$sql = "SELECT * FROM subscriptions ORDER BY $sort $order, inactive ASC";
}
}

Expand Down
86 changes: 51 additions & 35 deletions stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,15 @@ function getPriceConverted($price, $currency, $database) {
$row = $result->fetchArray(SQLITE3_ASSOC);
$code = $row['code'];


// Calculate active subscriptions
$query = "SELECT COUNT(*) AS active_subscriptions FROM subscriptions WHERE inactive = 0";
$stmt = $db->prepare($query);
$stmt->bindParam(':criteria', $criteria, SQLITE3_INTEGER);
$result = $stmt->execute();
$row = $result->fetchArray(SQLITE3_ASSOC);
$activeSubscriptions = $row['active_subscriptions'];

$activeSubscriptions = 0;
$inactiveSubscriptions = 0;
// Calculate total monthly price
$mostExpensiveSubscription = 0;
$amountDueThisMonth = 0;
$totalCostPerMonth = 0;
$totalSavingsPerMonth = 0;

$query = "SELECT name, price, frequency, cycle, currency_id, next_payment, payer_user_id, category_id, payment_method_id FROM subscriptions WHERE inactive = 0";
$query = "SELECT name, price, frequency, cycle, currency_id, next_payment, payer_user_id, category_id, payment_method_id, inactive FROM subscriptions";
$result = $db->query($query);
if ($result) {
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
Expand All @@ -112,34 +106,42 @@ function getPriceConverted($price, $currency, $database) {
$payerId = $subscription['payer_user_id'];
$categoryId = $subscription['category_id'];
$paymentMethodId = $subscription['payment_method_id'];
$inactive = $subscription['inactive'];
$originalSubscriptionPrice = getPriceConverted($price, $currency, $db);
$price = getPricePerMonth($cycle, $frequency, $originalSubscriptionPrice);
$totalCostPerMonth += $price;
$memberCost[$payerId]['cost'] += $price;
$categoryCost[$categoryId]['cost'] += $price;
$paymentMethodCount[$paymentMethodId]['count'] += 1;
if ($price > $mostExpensiveSubscription) {
$mostExpensiveSubscription = $price;
}

// Calculate ammount due this month
$nextPaymentDate = DateTime::createFromFormat('Y-m-d', trim($next_payment));
$tomorrow = new DateTime('tomorrow');
$endOfMonth = new DateTime('last day of this month');

if ($nextPaymentDate >= $tomorrow && $nextPaymentDate <= $endOfMonth) {
$timesToPay = 1;
$daysInMonth = $endOfMonth->diff($tomorrow)->days + 1;
$daysRemaining = $endOfMonth->diff($nextPaymentDate)->days + 1;
if ($cycle == 1) {
$timesToPay = $daysRemaining / $frequency;
}
if ($cycle == 2) {
$weeksInMonth = ceil($daysInMonth / 7);
$weeksRemaining = ceil($daysRemaining / 7);
$timesToPay = $weeksRemaining / $frequency;
}
$amountDueThisMonth += $originalSubscriptionPrice * $timesToPay;
if ($inactive == 0) {
$activeSubscriptions++;
$totalCostPerMonth += $price;
$memberCost[$payerId]['cost'] += $price;
$categoryCost[$categoryId]['cost'] += $price;
$paymentMethodCount[$paymentMethodId]['count'] += 1;
if ($price > $mostExpensiveSubscription) {
$mostExpensiveSubscription = $price;
}

// Calculate ammount due this month
$nextPaymentDate = DateTime::createFromFormat('Y-m-d', trim($next_payment));
$tomorrow = new DateTime('tomorrow');
$endOfMonth = new DateTime('last day of this month');

if ($nextPaymentDate >= $tomorrow && $nextPaymentDate <= $endOfMonth) {
$timesToPay = 1;
$daysInMonth = $endOfMonth->diff($tomorrow)->days + 1;
$daysRemaining = $endOfMonth->diff($nextPaymentDate)->days + 1;
if ($cycle == 1) {
$timesToPay = $daysRemaining / $frequency;
}
if ($cycle == 2) {
$weeksInMonth = ceil($daysInMonth / 7);
$weeksRemaining = ceil($daysRemaining / 7);
$timesToPay = $weeksRemaining / $frequency;
}
$amountDueThisMonth += $originalSubscriptionPrice * $timesToPay;
}
} else {
$inactiveSubscriptions++;
$totalSavingsPerMonth += $price;
}

}
Expand Down Expand Up @@ -185,6 +187,20 @@ function getPriceConverted($price, $currency, $database) {
<div class="title"><?= translate('amount_due', $i18n) ?></div>
</div>
<?php
if ($inactiveSubscriptions > 0) {
$numberOfElements = 8;
?>
<div class="statistic">
<span><?= $inactiveSubscriptions ?></span>
<div class="title"><?= translate('inactive_subscriptions', $i18n) ?></div>
</div>
<div class="statistic">
<span><?= CurrencyFormatter::format($totalSavingsPerMonth, $code) ?></span>
<div class="title"><?= translate('monthly_savings', $i18n) ?></div>
</div>
<?php
}

if (($numberOfElements + 1) % 3 == 0) {
?>
<div class="statistic empty"></div>
Expand Down