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

V2 37 0 #624

Merged
merged 3 commits into from
Nov 15, 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
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
32 changes: 25 additions & 7 deletions endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
$stmt = $db->prepare($query);
$usersToNotify = $stmt->execute();

function getDaysText($days) {
if ($days == 0) {
return "Today";
} elseif ($days == 1) {
return "Tomorrow";
} else {
return "In " . $days . " days";
}
}

while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
$userId = $userToNotify['id'];
if (php_sapi_name() !== 'cli') {
Expand Down Expand Up @@ -213,8 +223,16 @@
$daysToCompare = $days;
}
$nextPaymentDate = new DateTime($rowSubscription['next_payment']);
$difference = $currentDate->diff($nextPaymentDate)->days + 1;

$difference = $currentDate->diff($nextPaymentDate)->days;
if ($nextPaymentDate > $currentDate) {
$difference += 1;
}

if ($difference === $daysToCompare) {
echo "Next payment date: " . $nextPaymentDate->format('Y-m-d') . "<br />";
echo "Current date: " . $currentDate->format('Y-m-d') . "<br />";
echo "Difference: " . $difference . "<br />";
$notify[$rowSubscription['payer_user_id']][$i]['name'] = $rowSubscription['name'];
$notify[$rowSubscription['payer_user_id']][$i]['price'] = $rowSubscription['price'] . $currencies[$rowSubscription['currency_id']]['symbol'];
$notify[$rowSubscription['payer_user_id']][$i]['currency'] = $currencies[$rowSubscription['currency_id']]['name'];
Expand Down Expand Up @@ -244,7 +262,7 @@
$message = "The following subscriptions are up for renewal:\n";

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -313,7 +331,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -366,7 +384,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -420,7 +438,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -469,7 +487,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

Expand Down Expand Up @@ -511,7 +529,7 @@
}

foreach ($perUser as $subscription) {
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$dayText = getDaysText($subscription['days']);
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}

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
Loading