Skip to content

Commit

Permalink
feat: add some statistics to the calendar page
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ribeiro committed Nov 14, 2024
1 parent 37a14ee commit c329979
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 5 deletions.
103 changes: 98 additions & 5 deletions calendar.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
<?php
require_once 'includes/header.php';

function getPriceConverted($price, $currency, $database, $userId)
{
$query = "SELECT rate FROM currencies WHERE id = :currency AND user_id = :userId";
$stmt = $database->prepare($query);
$stmt->bindParam(':currency', $currency, SQLITE3_INTEGER);
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();

$exchangeRate = $result->fetchArray(SQLITE3_ASSOC);
if ($exchangeRate === false) {
return $price;
} else {
$fromRate = $exchangeRate['rate'];
return $price / $fromRate;
}
}

$currentMonth = date('m');
$currentYear = date('Y');
$sameAsCurrent = false;
Expand Down Expand Up @@ -30,19 +47,62 @@
$sameAsCurrent = true;
}

$currenciesInUse = [];
$numberOfSubscriptionsToPayThisMonth = 0;
$totalCostThisMonth = 0;
$amountDueThisMonth = 0;

$query = "SELECT * FROM subscriptions WHERE user_id = :user_id AND inactive = 0";
$stmt = $db->prepare($query);
$stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$subscriptions = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$subscriptions[] = $row;
$currenciesInUse[] = $row['currency_id'];
}

$currenciesInUse = array_unique($currenciesInUse);
$usesMultipleCurrencies = count($currenciesInUse) > 1;

$showCantConverErrorMessage = false;
if ($usesMultipleCurrencies) {
$query = "SELECT api_key FROM fixer WHERE user_id = :userId";
$stmt = $db->prepare($query);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($result->fetchArray(SQLITE3_ASSOC) === false) {
$showCantConverErrorMessage = true;
}
}

// Get code of main currency to display on statistics
$query = "SELECT c.code
FROM currencies c
INNER JOIN user u ON c.id = u.main_currency
WHERE u.id = :userId";
$stmt = $db->prepare($query);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$row = $result->fetchArray(SQLITE3_ASSOC);
$code = $row['code'];

$yearsToLoad = $calendarYear - $currentYear + 1;
?>

<section class="contain">
<?php
if ($showCantConverErrorMessage) {
?>
<div class="error-box">
<div class="error-message">
<i class="fa-solid fa-exclamation-circle"></i>
<?= translate('cant_convert_currency', $i18n) ?>
</div>
</div>
<?php
}
?>
<div class="split-header">
<h2>Calendar</h2>
<div class="calendar-nav">
Expand Down Expand Up @@ -78,6 +138,7 @@ class="fa-solid fa-chevron-right"></i></button>
$today = $todayYear . '-' . $todayMonth . '-' . $todayDay;
$today = strtotime($today);
?>

<div class="calendar">
<div class="calendar-header">
<div class="calendar-cell"><?= translate('mon', $i18n) ?></div>
Expand Down Expand Up @@ -115,9 +176,9 @@ class="fa-solid fa-chevron-right"></i></button>
$nextPaymentDate = strtotime($subscription['next_payment']);
$cycle = $subscription['cycle']; // Integer from 1 to 4
$frequency = $subscription['frequency'];

$endDate = strtotime("+" . $yearsToLoad . " years", $nextPaymentDate);

// Determine the strtotime increment string based on cycle
switch ($cycle) {
case 1: // Days
Expand All @@ -142,12 +203,17 @@ class="fa-solid fa-chevron-right"></i></button>
// Find the first payment date of the month by moving backwards
$startDate = $nextPaymentDate;
while ($startDate > $startOfMonth) {
$startDate = strtotime("-" . $incrementString, $startDate);
$startDate = strtotime("-" . $incrementString, $startDate);
}

for ($date = $startDate; $date <= $endDate; $date = strtotime($incrementString, $date)) {
if (date('Y-m', $date) == $calendarYear . '-' . str_pad($calendarMonth, 2, '0', STR_PAD_LEFT)) {
if (date('d', $date) == $day) {
$totalCostThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
$numberOfSubscriptionsToPayThisMonth++;
if ($date > $today) {
$amountDueThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
}
?>
<div class="calendar-subscription-title" onClick="openSubscriptionModal(<?= $subscription['id'] ?>)">
<?= htmlspecialchars($subscription['name']) ?>
Expand Down Expand Up @@ -210,12 +276,17 @@ class="fa-solid fa-chevron-right"></i></button>
// Find the first payment date of the month by moving backwards
$startDate = $nextPaymentDate;
while ($startDate > $startOfMonth) {
$startDate = strtotime("-" . $incrementString, $startDate);
$startDate = strtotime("-" . $incrementString, $startDate);
}

for ($date = $startDate; $date <= $endDate; $date = strtotime($incrementString, $date)) {
if (date('Y-m', $date) == $calendarYear . '-' . str_pad($calendarMonth, 2, '0', STR_PAD_LEFT)) {
if (date('d', $date) == $day) {
$totalCostThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
$numberOfSubscriptionsToPayThisMonth++;
if ($date > $today) {
$amountDueThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
}
?>
<div class="calendar-subscription-title" onClick="openSubscriptionModal(<?= $subscription['id'] ?>)">
<?= $subscription['name'] ?>
Expand Down Expand Up @@ -247,6 +318,28 @@ class="fa-solid fa-chevron-right"></i></button>
</div>
</div>
</div>

<div class="calendar-monthly-stats">
<div class="calendar-monthly-stats-header">
<h3><?= translate("stats", $i18n) ?></h3>
</div>
<div class="statistics">
<div class="statistic">
<span>
<?= $numberOfSubscriptionsToPayThisMonth ?></span>
<div class="title"><?= translate("active_subscriptions", $i18n) ?></div>
</div>
<div class="statistic">
<span><?= CurrencyFormatter::format($totalCostThisMonth, $code) ?></span>
<div class="title">Total cost</div>
</div>
<div class="statistic">
<span><?= CurrencyFormatter::format($amountDueThisMonth, $code) ?></span>
<div class="title"><?= translate("amount_due", $i18n) ?></div>
</div>
</div>
</div>

</section>

<div id="subscriptionModal" class="subscription-modal">
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Oktober",
"month-11" => "November",
"month-12" => "Dezember",
"total_cost" => "Gesamtkosten",
// TOTP Page
"insert_totp_code" => "Bitte geben Sie den TOTP-Code ein",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Οκτώβριος",
"month-11" => "Νοέμβριος",
"month-12" => "Δεκέμβριος",
"total_cost" => "Συνολικό κόστος",
// TOTP Page
"insert_totp_code" => "Εισάγετε τον κωδικό TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
"month-10" => "October",
"month-11" => "November",
"month-12" => "December",
"total_cost" => "Total Cost",
// TOTP Page
"insert_totp_code" => "Insert TOTP code",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Octubre",
"month-11" => "Noviembre",
"month-12" => "Diciembre",
"total_cost" => "Costo Total",
// TOTP Page
"insert_totp_code" => "Introduce el código TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Octobre",
"month-11" => "Novembre",
"month-12" => "Décembre",
"total_cost" => "Coût total",
// TOTP Page
"insert_totp_code" => "Veuillez insérer le code TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
"month-10" => "Ottobre",
"month-11" => "Novembre",
"month-12" => "Dicembre",
"total_cost" => "Costo totale",

// TOTP Page
"insert_totp_code" => "Inserisci il codice TOTP",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/jp.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
"month-10" => "10月",
"month-11" => "11月",
"month-12" => "12月",
"total_cost" => "合計費用",
// TOTP Page
"insert_totp_code" => "TOTPコードを入力してください",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/ko.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "10월",
"month-11" => "11월",
"month-12" => "12월",
"total_cost" => "총 비용",
// TOTP Page
"insert_totp_code" => "2단계 인증 코드를 입력하세요",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pl.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Październik",
"month-11" => "Listopad",
"month-12" => "Grudzień",
"total_cost" => "Całkowity koszt",
// TOTP Page
"insert_totp_code" => "Wprowadź kod TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Outubro",
"month-11" => "Novembro",
"month-12" => "Dezembro",
"total_cost" => "Custo Total",
// TOTP Page
"insert_totp_code" => "Insira o código TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt_br.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Outubro",
"month-11" => "Novembro",
"month-12" => "Dezembro",
"total_cost" => "Custo total",
// TOTP Page
"insert_totp_code" => "Insira o código TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Октябрь",
"month-11" => "Ноябрь",
"month-12" => "Декабрь",
"total_cost" => "Общая стоимость",
// TOTP Page
"insert_totp_code" => "Введите код TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sl.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
"month-10" => "Oktober",
"month-11" => "November",
"month-12" => "December",
"total_cost" => "Skupni stroški",
// TOTP Page
"insert_totp_code" => "Vnesite kodo TOTP",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sr.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Октобар",
"month-11" => "Новембар",
"month-12" => "Децембар",
"total_cost" => "Укупан трошак",
// TOTP Page
"insert_totp_code" => "Унесите ТОТП код",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sr_lat.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Oktobar",
"month-11" => "Novembar",
"month-12" => "Decembar",
"total_cost" => "Ukupan trošak",
// TOTP Page
"insert_totp_code" => "Unesite TOTP kod",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
"month-10" => "Ekim",
"month-11" => "Kasım",
"month-12" => "Aralık",
"total_cost" => "Toplam Maliyet",
// TOTP Page
"insert_totp_code" => "Lütfen TOTP kodunuzu girin",

Expand Down
1 change: 1 addition & 0 deletions includes/i18n/vi.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
"month-10" => "Tháng Mười",
"month-11" => "Tháng Mười Một",
"month-12" => "Tháng Mười Hai",
"total_cost" => "Tổng chi phí",
// TOTP Page
"insert_totp_code" => "Nhập mã TOTP",
];
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 @@ -384,6 +384,7 @@
"month-10" => "十月",
"month-11" => "十一月",
"month-12" => "十二月",
"total_cost" => "总费用",

// TOTP Page
"insert_totp_code" => "请输入 TOTP 代码",
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 @@ -364,6 +364,7 @@
"month-10" => "十月",
"month-11" => "十一月",
"month-12" => "十二月",
"total_cost" => "總費用",

// TOTP Page
"insert_totp_code" => "請輸入 TOTP 驗證碼",
Expand Down

0 comments on commit c329979

Please sign in to comment.