Skip to content

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

feat: make the dashboard upcoming payment count configurable (#1191)

周耕毅(Ryan Chou)4fc931057165
diff --git a/api/settings/get_settings.php b/api/settings/get_settings.php
index b172830..4d97dd8 100644
--- a/api/settings/get_settings.php
+++ b/api/settings/get_settings.php
@@ -26,6 +26,7 @@ Example response:
"mobile_nav": 1,
"show_subscription_progress": 0,
"week_starts_sunday": 0,
+ "upcoming_payments_limit": 3,
"square_icons": 0,
"custom_colors": {
"main_color": "#0000ff",
@@ -129,4 +130,4 @@ if ($_SERVER["REQUEST_METHOD"] === "POST" || $_SERVER["REQUEST_METHOD"] === "GET
exit;
}
-?>
\ No newline at end of file
+?>
diff --git a/api/settings/set_settings.php b/api/settings/set_settings.php
index 8c5a732..95122a3 100644
--- a/api/settings/set_settings.php
+++ b/api/settings/set_settings.php
@@ -11,6 +11,7 @@ It receives the following parameters:
- mobile_nav: (optional) '1' or '0' (use mobile navigation menu).
- show_subscription_progress: (optional) '1' or '0' (show subscription progress bars).
- week_starts_sunday: (optional) '1' or '0' (start calendar weeks on Sunday).
+- upcoming_payments_limit: (optional) '3', '5', '10', or '20' (number of upcoming payments on the dashboard).
- disabled_to_bottom: (optional) '1' or '0' (move disabled subscriptions to bottom).
- hide_disabled: (optional) '1' or '0' (hide disabled subscriptions).
- remove_background: (optional) '1' or '0' (remove background from logos).
@@ -35,6 +36,7 @@ Example response:
require_once '../../includes/connect_endpoint.php';
require_once '../../includes/inputvalidation.php';
+require_once '../../includes/upcoming_payments.php';
header('Content-Type: application/json; charset=UTF-8');
@@ -216,6 +218,25 @@ if (isset($_POST['color_theme'])) {
}
}
+if (isset($_POST['upcoming_payments_limit'])) {
+ $upcomingPaymentsLimit = parse_upcoming_payments_limit($_POST['upcoming_payments_limit']);
+
+ if ($upcomingPaymentsLimit === null) {
+ echo json_encode([
+ 'success' => false,
+ 'title' => 'Invalid parameter',
+ 'message' => "Parameter 'upcoming_payments_limit' must be 3, 5, 10, or 20."
+ ]);
+ exit;
+ }
+
+ $updateFields[] = '`upcoming_payments_limit` = :upcoming_payments_limit';
+ $params['upcoming_payments_limit'] = [
+ 'val' => $upcomingPaymentsLimit,
+ 'type' => SQLITE3_INTEGER
+ ];
+}
+
// Perform update if fields were specified
if (!empty($updateFields)) {
$sqlUpdate = "UPDATE settings SET " . implode(', ', $updateFields) . " WHERE user_id = :userId";
diff --git a/endpoints/settings/upcoming_payments_limit.php b/endpoints/settings/upcoming_payments_limit.php
new file mode 100644
index 0000000..ce0fe7c
--- /dev/null
+++ b/endpoints/settings/upcoming_payments_limit.php
@@ -0,0 +1,32 @@
+<?php
+
+require_once '../../includes/connect_endpoint.php';
+require_once '../../includes/validate_endpoint.php';
+require_once '../../includes/upcoming_payments.php';
+
+$postData = file_get_contents('php://input');
+$data = json_decode($postData, true);
+$limit = parse_upcoming_payments_limit($data['value'] ?? null);
+
+if ($limit === null) {
+ die(json_encode([
+ 'success' => false,
+ 'message' => translate('error', $i18n),
+ ]));
+}
+
+$stmt = $db->prepare('UPDATE settings SET upcoming_payments_limit = :limit WHERE user_id = :userId');
+$stmt->bindValue(':limit', $limit, SQLITE3_INTEGER);
+$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+
+if ($stmt->execute()) {
+ die(json_encode([
+ 'success' => true,
+ 'message' => translate('success', $i18n),
+ ]));
+}
+
+die(json_encode([
+ 'success' => false,
+ 'message' => translate('error', $i18n),
+]));
diff --git a/includes/upcoming_payments.php b/includes/upcoming_payments.php
new file mode 100644
index 0000000..a24b26d
--- /dev/null
+++ b/includes/upcoming_payments.php
@@ -0,0 +1,61 @@
+<?php
+
+const UPCOMING_PAYMENTS_LIMITS = [3, 5, 10, 20];
+
+/**
+ * Parse a dashboard limit and return null for unsupported values.
+ *
+ * @param mixed $limit
+ * @return int|null
+ */
+function parse_upcoming_payments_limit($limit)
+{
+ if (is_string($limit) && ctype_digit($limit)) {
+ $limit = (int) $limit;
+ }
+
+ return is_int($limit) && in_array($limit, UPCOMING_PAYMENTS_LIMITS, true) ? $limit : null;
+}
+
+/**
+ * Return a supported dashboard limit, falling back to the current default.
+ *
+ * @param mixed $limit
+ * @return int
+ */
+function normalize_upcoming_payments_limit($limit)
+{
+ return parse_upcoming_payments_limit($limit) ?? 3;
+}
+
+/**
+ * Fetch the upcoming subscriptions shown on the dashboard.
+ *
+ * @param SQLite3 $db
+ * @param int $userId
+ * @param mixed $limit
+ * @return array
+ */
+function get_upcoming_payments($db, $userId, $limit)
+{
+ $limit = normalize_upcoming_payments_limit($limit);
+
+ $stmt = $db->prepare("SELECT id, logo, logo_text_color, logo_variant, name, price, currency_id, next_payment, inactive
+ FROM subscriptions
+ WHERE user_id = :userId
+ AND next_payment >= date('now')
+ AND inactive = 0
+ AND cycle != 5
+ ORDER BY next_payment ASC
+ LIMIT :limit");
+ $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+ $stmt->bindValue(':limit', $limit, SQLITE3_INTEGER);
+ $result = $stmt->execute();
+
+ $subscriptions = [];
+ while ($result && ($row = $result->fetchArray(SQLITE3_ASSOC))) {
+ $subscriptions[] = $row;
+ }
+
+ return $subscriptions;
+}
diff --git a/index.php b/index.php
index cc4b8eb..6f2db63 100644
--- a/index.php
+++ b/index.php
@@ -3,6 +3,7 @@
require_once 'includes/header.php';
require_once 'includes/getdbkeys.php';
require_once 'includes/logo_theme_variant.php';
+require_once 'includes/upcoming_payments.php';
function formatPrice($price, $currencyCode, $currencies)
{
@@ -72,14 +73,12 @@ $result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
$first_name = $user['firstname'] ?? $user['username'] ?? '';
-// Fetch the next 3 enabled subscriptions up for payment
-$stmt = $db->prepare("SELECT id, logo, logo_text_color, logo_variant, name, price, currency_id, next_payment, inactive FROM subscriptions WHERE user_id = :userId AND next_payment >= date('now') AND inactive = 0 AND cycle != 5 ORDER BY next_payment ASC LIMIT 3");
-$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
-$result = $stmt->execute();
-$upcomingSubscriptions = [];
-while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
- $upcomingSubscriptions[] = $row;
-}
+// Fetch the enabled subscriptions up for payment using the user's dashboard setting.
+$upcomingSubscriptions = get_upcoming_payments(
+ $db,
+ $userId,
+ $settings['upcoming_payments_limit'] ?? 3
+);
// Fetch enabled subscriptions with manual renewal that are overdue
$stmt = $db->prepare("SELECT id, logo, logo_text_color, logo_variant, name, price, currency_id, next_payment, inactive, auto_renew FROM subscriptions WHERE user_id = :userId AND next_payment < date('now') AND auto_renew = 0 AND inactive = 0 AND cycle != 5 ORDER BY next_payment ASC");
@@ -463,4 +462,4 @@ require_once 'includes/subscription_details_popup.php';
<?php
require_once 'includes/footer.php';
-?>
\ No newline at end of file
+?>
diff --git a/migrations/000057.php b/migrations/000057.php
new file mode 100644
index 0000000..eeb04bd
--- /dev/null
+++ b/migrations/000057.php
@@ -0,0 +1,14 @@
+<?php
+
+// This migration lets each user choose how many upcoming payments appear on
+// the dashboard (3, 5, 10, or 20); existing users keep the old default.
+
+$columnQuery = $db->query("SELECT * FROM pragma_table_info('settings') WHERE name='upcoming_payments_limit'");
+if ($columnQuery->fetchArray(SQLITE3_ASSOC) === false) {
+ $db->exec('ALTER TABLE settings ADD COLUMN upcoming_payments_limit INTEGER DEFAULT 3');
+}
+
+// Be defensive about databases that may already contain an invalid value.
+$db->exec('UPDATE settings SET upcoming_payments_limit = 3
+ WHERE upcoming_payments_limit IS NULL
+ OR upcoming_payments_limit NOT IN (3, 5, 10, 20)');
diff --git a/scripts/settings.js b/scripts/settings.js
index 706a8e5..79a24a7 100644
--- a/scripts/settings.js
+++ b/scripts/settings.js
@@ -1075,6 +1075,13 @@ function setShowOriginalPrice() {
storeSettingsOnDB('show_original_price', value);
}
+function setUpcomingPaymentsLimit() {
+ const upcomingPaymentsLimit = document.querySelector("#upcomingpaymentslimit");
+ const value = Number(upcomingPaymentsLimit.value);
+
+ storeSettingsOnDB('upcoming_payments_limit', value);
+}
+
function setMobileNavigation() {
const mobileNavigationCheckbox = document.querySelector("#mobilenavigation");
const value = mobileNavigationCheckbox.checked;
diff --git a/settings.php b/settings.php
index d740bf2..020fe0a 100644
--- a/settings.php
+++ b/settings.php
@@ -1,5 +1,6 @@
<?php
require_once 'includes/header.php';
+require_once 'includes/upcoming_payments.php';
$currencies = array();
$query = "SELECT * FROM currencies WHERE user_id = :userId";
@@ -16,6 +17,7 @@ $budgetPeriodAnchorDate = $userData['budget_period_anchor_date'] ?? date('Y-m-d'
if ($budgetPeriodAnchorDate === '1970-01-01' || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $budgetPeriodAnchorDate)) {
$budgetPeriodAnchorDate = date('Y-m-d');
}
+$upcomingPaymentsLimit = normalize_upcoming_payments_limit($settings['upcoming_payments_limit'] ?? 3);
?>
@@ -1604,6 +1606,17 @@ if ($budgetPeriodAnchorDate === '1970-01-01' || !preg_match('/^\d{4}-\d{2}-\d{2}
<label for="showoriginalprice"><?= translate('show_original_price', $i18n) ?></label>
</div>
</div>
+ <div>
+ <div class="form-group-inline">
+ <label for="upcomingpaymentslimit"><?= translate('upcoming_payments', $i18n) ?></label>
+ <select id="upcomingpaymentslimit" name="upcomingpaymentslimit" onChange="setUpcomingPaymentsLimit()">
+ <option value="3" <?= $upcomingPaymentsLimit === 3 ? 'selected' : '' ?>>3</option>
+ <option value="5" <?= $upcomingPaymentsLimit === 5 ? 'selected' : '' ?>>5</option>
+ <option value="10" <?= $upcomingPaymentsLimit === 10 ? 'selected' : '' ?>>10</option>
+ <option value="20" <?= $upcomingPaymentsLimit === 20 ? 'selected' : '' ?>>20</option>
+ </select>
+ </div>
+ </div>
<h3><?= translate('experience', $i18n) ?></h3>
<div>
<div class="form-group-inline">
@@ -1679,4 +1692,4 @@ if ($budgetPeriodAnchorDate === '1970-01-01' || !preg_match('/^\d{4}-\d{2}-\d{2}
<?php
require_once 'includes/footer.php';
-?>
\ No newline at end of file
+?>
diff --git a/tests/cases/subscription_index_test.php b/tests/cases/subscription_index_test.php
index 03958dd..e0317b9 100644
--- a/tests/cases/subscription_index_test.php
+++ b/tests/cases/subscription_index_test.php
@@ -4,6 +4,8 @@
than scanning the table.
*/
+require_once WALLOS_ROOT . '/includes/upcoming_payments.php';
+
/**
* Returns the query plan of a statement as one string.
*
@@ -70,3 +72,71 @@ wallos_test('the migration can run twice', function () {
$db->close();
});
+
+wallos_test('the dashboard limit migration defaults existing users to three', function () {
+ $db = wallos_test_open_database();
+
+ $row = $db->query('SELECT upcoming_payments_limit FROM settings WHERE user_id = 1')
+ ->fetchArray(SQLITE3_ASSOC);
+
+ assert_same(3, (int) $row['upcoming_payments_limit'], 'existing users keep the default limit');
+
+ $db->exec('UPDATE settings SET upcoming_payments_limit = 4 WHERE user_id = 1');
+ require WALLOS_ROOT . '/migrations/000057.php';
+ $row = $db->query('SELECT upcoming_payments_limit FROM settings WHERE user_id = 1')
+ ->fetchArray(SQLITE3_ASSOC);
+ assert_same(3, (int) $row['upcoming_payments_limit'], 'invalid stored values are reset to the default');
+
+ require WALLOS_ROOT . '/migrations/000057.php';
+ assert_same(3, (int) $db->querySingle('SELECT upcoming_payments_limit FROM settings WHERE user_id = 1'),
+ 'the dashboard limit migration is safe to run twice');
+
+ $db->close();
+});
+
+wallos_test('dashboard limit parsing accepts only supported values', function () {
+ assert_same(20, parse_upcoming_payments_limit('20'), 'a supported string value is accepted');
+ assert_same(5, parse_upcoming_payments_limit(5), 'a supported numeric value is accepted');
+ assert_same(null, parse_upcoming_payments_limit(0), 'zero is no longer a supported value');
+ assert_same(null, parse_upcoming_payments_limit(4), 'unsupported numeric values are rejected');
+ assert_same(null, parse_upcoming_payments_limit(true), 'boolean values are rejected');
+});
+
+wallos_test('the dashboard keeps the legacy default of three upcoming payments', function () {
+ $db = wallos_test_open_database();
+ wallos_test_create_user($db, 1, 'alice');
+
+ $stmt = $db->prepare('INSERT INTO subscriptions (user_id, name, price, currency_id, next_payment, cycle, inactive)
+ VALUES (1, :name, 9.99, :currencyId, :nextPayment, 3, 0)');
+ for ($i = 1; $i <= 5; $i++) {
+ $stmt->bindValue(':name', 'payment-' . $i, SQLITE3_TEXT);
+ $stmt->bindValue(':currencyId', wallos_test_currency_id(1, 0), SQLITE3_INTEGER);
+ $stmt->bindValue(':nextPayment', date('Y-m-d', strtotime('+' . $i . ' days')), SQLITE3_TEXT);
+ $stmt->execute();
+ }
+
+ assert_same(3, count(get_upcoming_payments($db, 1, 3)), 'the default limit remains three');
+
+ $db->close();
+});
+
+wallos_test('the dashboard supports the configured limits', function () {
+ $db = wallos_test_open_database();
+ wallos_test_create_user($db, 1, 'alice');
+
+ $stmt = $db->prepare('INSERT INTO subscriptions (user_id, name, price, currency_id, next_payment, cycle, inactive)
+ VALUES (1, :name, 9.99, :currencyId, :nextPayment, 3, 0)');
+ for ($i = 1; $i <= 25; $i++) {
+ $stmt->bindValue(':name', 'payment-' . $i, SQLITE3_TEXT);
+ $stmt->bindValue(':currencyId', wallos_test_currency_id(1, 0), SQLITE3_INTEGER);
+ $stmt->bindValue(':nextPayment', date('Y-m-d', strtotime('+' . $i . ' days')), SQLITE3_TEXT);
+ $stmt->execute();
+ }
+
+ assert_same(5, count(get_upcoming_payments($db, 1, 5)), 'the five-payment option is honored');
+ assert_same(10, count(get_upcoming_payments($db, 1, 10)), 'the ten-payment option is honored');
+ assert_same(20, count(get_upcoming_payments($db, 1, 20)), 'the twenty-payment option is honored');
+ assert_same(3, count(get_upcoming_payments($db, 1, 7)), 'unsupported values fall back to three');
+
+ $db->close();
+});