Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

calculate next Payment in Creation Form (#828)

noomise06d4f1b21d9
diff --git a/includes/i18n/de.php b/includes/i18n/de.php
index d98cb40..9358c38 100644
--- a/includes/i18n/de.php
+++ b/includes/i18n/de.php
@@ -77,6 +77,7 @@ $i18n = [
"no_matching_subscriptions" => "Keine passenden Abonnements gefunden",
"clone" => "Klonen",
"renew" => "Verlängern",
+ "autofill_next_payment" => "Nächste Zahlung automatisch ausfüllen anhand von Startdatum und Interval",
// Subscription form
"add_subscription" => "Abonnement hinzufügen",
"edit_subscription" => "Abonnement editieren",
diff --git a/includes/i18n/en.php b/includes/i18n/en.php
index 87afd2d..208e328 100644
--- a/includes/i18n/en.php
+++ b/includes/i18n/en.php
@@ -77,6 +77,7 @@ $i18n = [
"no_matching_subscriptions" => "No matching subscriptions",
"clone" => "Clone",
"renew" => "Renew",
+ "autofill_next_payment" => "Autofill Next Payment by Interval and Start Date",
// Subscription form
"add_subscription" => "Add subscription",
"edit_subscription" => "Edit subscription",
diff --git a/index.php b/index.php
index d806892..1288b42 100644
--- a/index.php
+++ b/index.php
@@ -387,6 +387,9 @@ $headerClass = count($subscriptions) > 0 ? "main-actions" : "main-actions hidden
<label for="start_date"><?= translate('start_date', $i18n) ?></label>
<input type="date" id="start_date" name="start_date">
</div>
+ <div id="autofill-next-payment-button" class="image-button medium disabled autofill-next-payment-button" title="<?= translate('autofill_next_payment', $i18n) ?>" onClick="autoFillNextPaymentDate()">
+ <i class="fa-solid fa-wand-magic-sparkles"></i>
+ </div>
<div class="split50">
<label for="next_payment"><?= translate('next_payment', $i18n) ?></label>
<input type="date" id="next_payment" name="next_payment" required>
diff --git a/scripts/dashboard.js b/scripts/dashboard.js
index b53f721..64960bb 100644
--- a/scripts/dashboard.js
+++ b/scripts/dashboard.js
@@ -799,6 +799,63 @@ function swipeHintAnimation() {
}
}
+function autoFillNextPaymentDate() {
+ const frequencySelect = document.querySelector("#frequency");
+ const cycleSelect = document.querySelector("#cycle");
+ const startDate = document.querySelector("#start_date");
+ const nextPayment = document.querySelector("#next_payment");
+
+ const today = new Date();
+ const cycle = cycleSelect.value;
+ const frequency = Number(frequencySelect.value);
+
+ const nextDate = new Date(startDate.value);
+ let safetyCounter = 0;
+ const maxIterations = 1000;
+
+ while (nextDate <= today && safetyCounter < maxIterations) {
+ switch (cycle) {
+ case '1': // Days
+ nextDate.setDate(nextDate.getDate() + frequency);
+ break;
+ case '2': // Weeks
+ nextDate.setDate(nextDate.getDate() + 7 * frequency);
+ break;
+ case '3': // Months
+ nextDate.setMonth(nextDate.getMonth() + frequency);
+ break;
+ case '4': // Years
+ nextDate.setFullYear(nextDate.getFullYear() + frequency);
+ break;
+ default:
+ }
+ safetyCounter++;
+ }
+
+if (safetyCounter === maxIterations) {
+ return;
+}
+
+nextPayment.value = toISOStringWithTimezone(nextDate).substring(0, 10);
+}
+
+function toISOStringWithTimezone(date) {
+ const pad = n => String(Math.floor(Math.abs(n))).padStart(2, '0');
+ const tzOffset = -date.getTimezoneOffset();
+ const sign = tzOffset >= 0 ? '+' : '-';
+ const hoursOffset = pad(tzOffset / 60);
+ const minutesOffset = pad(tzOffset % 60);
+
+ return date.getFullYear() +
+ '-' + pad(date.getMonth() + 1) +
+ '-' + pad(date.getDate()) +
+ 'T' + pad(date.getHours()) +
+ ':' + pad(date.getMinutes()) +
+ ':' + pad(date.getSeconds()) +
+ sign + hoursOffset +
+ ':' + minutesOffset;
+}
+
window.addEventListener('load', () => {
if (document.querySelector('.subscription')) {
swipeHintAnimation();
diff --git a/styles/styles.css b/styles/styles.css
index 3b2dab3..1bc5ef7 100644
--- a/styles/styles.css
+++ b/styles/styles.css
@@ -2830,4 +2830,8 @@ input[type="radio"]:checked+label::after {
.mobile-navigation .mobileNavigationHideOnMobile {
display: none !important;
}
+}
+
+.autofill-next-payment-button {
+ margin-top: 20px;
}
\ No newline at end of file