feat: add stats about inactive subscriptions (#146)
feat: sort disabled subscription at the bottom
This commit is contained in:
parent
66cd5c2d2f
commit
ccac17a6f2
@ -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'];
|
||||
@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'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",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'average_monthly' => "Μέσο μηνιαίο κόστος συνδρομής",
|
||||
'most_expensive' => "Πιο ακριβό κόστος συνδρομής",
|
||||
'amount_due' => "Ποσό που οφείλεται αυτόν τον μήνα",
|
||||
'monthly_savings' => "Μηνιαίες εξοικονομήσεις (σε ανενεργές συνδρομές)",
|
||||
'split_views' => "Διαχωρισμένες προβολές",
|
||||
'category_split' => "Διαχωρισμός κατηγορίας",
|
||||
'household_split' => "Διαχωρισμός νοικοκυριού",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'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",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'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",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'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",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'average_monthly' => "月額平均費用",
|
||||
'most_expensive' => "最も高額な定期購入費用",
|
||||
'amount_due' => "今月の支払額",
|
||||
'monthly_savings' => "月間節約 (非アクティブな定期購入)",
|
||||
'split_views' => "分割表示",
|
||||
'category_split' => "カテゴリ別",
|
||||
'household_split' => "世帯別",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'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",
|
||||
|
||||
@ -77,6 +77,7 @@ $i18n = [
|
||||
'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ü",
|
||||
|
||||
@ -82,6 +82,7 @@ $i18n = [
|
||||
'average_monthly' => "平均每月订阅费用",
|
||||
'most_expensive' => "最昂贵订阅费用",
|
||||
'amount_due' => "本月应付金额",
|
||||
'monthly_savings' => "每月节省",
|
||||
'split_views' => "拆分视图",
|
||||
'category_split' => "分类视图",
|
||||
'household_split' => "家庭视图",
|
||||
|
||||
@ -82,6 +82,7 @@ $i18n = [
|
||||
'average_monthly' => "平均每月訂閱費用",
|
||||
'most_expensive' => "最高的訂閱費用",
|
||||
'amount_due' => "本月應付金額",
|
||||
'monthly_savings' => "每月節省",
|
||||
'split_views' => "分割表示",
|
||||
'category_split' => "類別表示",
|
||||
'household_split' => "家庭表示",
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
<?php
|
||||
$version = "v1.5.0";
|
||||
$version = "v1.6.0";
|
||||
?>
|
||||
@ -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'];
|
||||
@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
86
stats.php
86
stats.php
@ -81,21 +81,15 @@ $result = $stmt->execute();
|
||||
$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 @@ if ($result) {
|
||||
$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 @@ $numberOfElements = 6;
|
||||
<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>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user