From c57e0ae34bc35b658f0405054b48ea058a673076 Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Thu, 22 Feb 2024 02:47:22 +0100 Subject: [PATCH] feat: add statistics about inactive subscriptions --- includes/i18n/de.php | 1 + includes/i18n/el.php | 1 + includes/i18n/en.php | 1 + includes/i18n/es.php | 1 + includes/i18n/fr.php | 1 + includes/i18n/jp.php | 1 + includes/i18n/pt.php | 1 + includes/i18n/tr.php | 1 + includes/i18n/zh_cn.php | 1 + includes/i18n/zh_tw.php | 1 + stats.php | 86 ++++++++++++++++++++++++----------------- 11 files changed, 61 insertions(+), 35 deletions(-) diff --git a/includes/i18n/de.php b/includes/i18n/de.php index f58720dd0..419dbea8b 100644 --- a/includes/i18n/de.php +++ b/includes/i18n/de.php @@ -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", diff --git a/includes/i18n/el.php b/includes/i18n/el.php index 1c8b37776..cd75e425d 100644 --- a/includes/i18n/el.php +++ b/includes/i18n/el.php @@ -77,6 +77,7 @@ 'average_monthly' => "Μέσο μηνιαίο κόστος συνδρομής", 'most_expensive' => "Πιο ακριβό κόστος συνδρομής", 'amount_due' => "Ποσό που οφείλεται αυτόν τον μήνα", + 'monthly_savings' => "Μηνιαίες εξοικονομήσεις (σε ανενεργές συνδρομές)", 'split_views' => "Διαχωρισμένες προβολές", 'category_split' => "Διαχωρισμός κατηγορίας", 'household_split' => "Διαχωρισμός νοικοκυριού", diff --git a/includes/i18n/en.php b/includes/i18n/en.php index bd6521510..f3d6e2e13 100644 --- a/includes/i18n/en.php +++ b/includes/i18n/en.php @@ -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", diff --git a/includes/i18n/es.php b/includes/i18n/es.php index 15b325dc7..62d287728 100644 --- a/includes/i18n/es.php +++ b/includes/i18n/es.php @@ -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", diff --git a/includes/i18n/fr.php b/includes/i18n/fr.php index 0d36ab83e..6f832d925 100644 --- a/includes/i18n/fr.php +++ b/includes/i18n/fr.php @@ -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", diff --git a/includes/i18n/jp.php b/includes/i18n/jp.php index ca0091591..70eb54ed2 100644 --- a/includes/i18n/jp.php +++ b/includes/i18n/jp.php @@ -77,6 +77,7 @@ 'average_monthly' => "月額平均費用", 'most_expensive' => "最も高額な定期購入費用", 'amount_due' => "今月の支払額", + 'monthly_savings' => "月間節約 (非アクティブな定期購入)", 'split_views' => "分割表示", 'category_split' => "カテゴリ別", 'household_split' => "世帯別", diff --git a/includes/i18n/pt.php b/includes/i18n/pt.php index c28a133eb..896e1a226 100644 --- a/includes/i18n/pt.php +++ b/includes/i18n/pt.php @@ -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", diff --git a/includes/i18n/tr.php b/includes/i18n/tr.php index 90be24b24..2fa03e150 100644 --- a/includes/i18n/tr.php +++ b/includes/i18n/tr.php @@ -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ü", diff --git a/includes/i18n/zh_cn.php b/includes/i18n/zh_cn.php index bdf621b9c..3cd309ee7 100644 --- a/includes/i18n/zh_cn.php +++ b/includes/i18n/zh_cn.php @@ -82,6 +82,7 @@ 'average_monthly' => "平均每月订阅费用", 'most_expensive' => "最昂贵订阅费用", 'amount_due' => "本月应付金额", + 'monthly_savings' => "每月节省", 'split_views' => "拆分视图", 'category_split' => "分类视图", 'household_split' => "家庭视图", diff --git a/includes/i18n/zh_tw.php b/includes/i18n/zh_tw.php index b41c61ec4..845a8e6b2 100644 --- a/includes/i18n/zh_tw.php +++ b/includes/i18n/zh_tw.php @@ -82,6 +82,7 @@ 'average_monthly' => "平均每月訂閱費用", 'most_expensive' => "最高的訂閱費用", 'amount_due' => "本月應付金額", + 'monthly_savings' => "每月節省", 'split_views' => "分割表示", 'category_split' => "類別表示", 'household_split' => "家庭表示", diff --git a/stats.php b/stats.php index c5c1441fe..3a90c5ce1 100644 --- a/stats.php +++ b/stats.php @@ -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)) { @@ -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; } } @@ -185,6 +187,20 @@ function getPriceConverted($price, $currency, $database) {
0) { + $numberOfElements = 8; + ?> +
+ +
+
+
+ +
+
+