feat: subscriptions have personalized notification times (#334)

This commit is contained in:
Miguel Ribeiro
2024-05-20 13:07:40 +02:00
committed by GitHub
parent f1815d7335
commit c7146dfd08
25 changed files with 105 additions and 28 deletions
+28 -21
View File
@@ -134,15 +134,21 @@
$notify = []; $i = 0;
$currentDate = new DateTime('now');
while ($rowSubscription = $resultSubscriptions->fetchArray(SQLITE3_ASSOC)) {
if ($rowSubscription['notify_days_before'] !== 0) {
$daysToCompare = $rowSubscription['notify_days_before'];
} else {
$daysToCompare = $days;
}
$nextPaymentDate = new DateTime($rowSubscription['next_payment']);
$difference = $currentDate->diff($nextPaymentDate)->days + 1;
if ($difference === $days) {
if ($difference === $daysToCompare) {
$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'];
$notify[$rowSubscription['payer_user_id']][$i]['category'] = $categories[$rowSubscription['category_id']]['name'];
$notify[$rowSubscription['payer_user_id']][$i]['payer'] = $household[$rowSubscription['payer_user_id']]['name'];
$notify[$rowSubscription['payer_user_id']][$i]['date'] = $rowSubscription['next_payment'];
$notify[$rowSubscription['payer_user_id']][$i]['days'] = $daysToCompare;
$i++;
}
}
@@ -163,11 +169,11 @@
$defaultName = $defaultUser['username'];
foreach ($notify as $userId => $perUser) {
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
$message = "The following subscriptions are up for renewal:\n";
foreach ($perUser as $subscription) {
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}
$mail = new PHPMailer(true);
@@ -214,15 +220,15 @@
$title = translate('wallos_notification', $i18n);
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
if ($user['name']) {
$message = $user['name'] . ", the following subscriptions are up for renewal " . $dayText . ":\n";
$message = $user['name'] . ", the following subscriptions are up for renewal:\n";
} else {
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
$message = "The following subscriptions are up for renewal:\n";
}
foreach ($perUser as $subscription) {
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}
$postfields = [
@@ -267,15 +273,15 @@
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
if ($user['name']) {
$message = $user['name'] . ", the following subscriptions are up for renewal " . $dayText . ":\n";
$message = $user['name'] . ", the following subscriptions are up for renewal:\n";
} else {
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
$message = "The following subscriptions are up for renewal:\n";
}
foreach ($perUser as $subscription) {
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}
$data = array(
@@ -312,15 +318,15 @@
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
if ($user['name']) {
$message = $user['name'] . ", the following subscriptions are up for renewal " . $dayText . ":\n";
$message = $user['name'] . ", the following subscriptions are up for renewal:\n";
} else {
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
$message = "The following subscriptions are up for renewal:\n";
}
foreach ($perUser as $subscription) {
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}
$data = array(
@@ -357,15 +363,15 @@
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
$dayText = $days == 1 ? "tomorrow" : "in " . $days . " days";
if ($user['name']) {
$message = $user['name'] . ", the following subscriptions are up for renewal " . $dayText . ":\n";
$message = $user['name'] . ", the following subscriptions are up for renewal:\n";
} else {
$message = "The following subscriptions are up for renewal " . $dayText . ":\n";
$message = "The following subscriptions are up for renewal:\n";
}
foreach ($perUser as $subscription) {
$message .= $subscription['name'] . " for " . $subscription['price'] . "\n";
$dayText = $subscription['days'] == 1 ? "Tomorrow" : "In " . $subscription['days'] . " days";
$message .= $subscription['name'] . " for " . $subscription['price'] . " (" . $dayText . ")\n";
}
$ch = curl_init();
@@ -394,7 +400,7 @@
if ($webhookNotificationsEnabled) {
// Get webhook payload and turn it into a json object
$payload = str_replace("{{days_until}}", $days, $webhook['payload']);
$payload = str_replace("{{days_until}}", $days, $webhook['payload']); // The default value for all subscriptions
$payload_json = json_decode($payload, true);
$subscription_template = $payload_json["{{subscriptions}}"];
@@ -422,6 +428,7 @@
$temp_subscription[$key] = str_replace("{{subscription_category}}", $subscription['category'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_payer}}", $subscription['payer'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_date}}", $subscription['date'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]); // The de facto value for this subscription
}
}
$subscriptions[] = $temp_subscription;
+11 -4
View File
@@ -166,6 +166,7 @@
$logoUrl = validate($_POST['logo-url']);
$logo = "";
$notify = isset($_POST['notifications']) ? true : false;
$notifyDaysBefore = $_POST['notify_days_before'];
$inactive = isset($_POST['inactive']) ? true : false;
if($logoUrl !== "") {
@@ -183,15 +184,20 @@
if (!$isEdit) {
$sql = "INSERT INTO subscriptions (name, logo, price, currency_id, next_payment, cycle, frequency, notes,
payment_method_id, payer_user_id, category_id, notify, inactive, url)
payment_method_id, payer_user_id, category_id, notify, inactive, url, notify_days_before)
VALUES (:name, :logo, :price, :currencyId, :nextPayment, :cycle, :frequency, :notes,
:paymentMethodId, :payerUserId, :categoryId, :notify, :inactive, :url)";
:paymentMethodId, :payerUserId, :categoryId, :notify, :inactive, :url, :notifyDaysBefore)";
} else {
$id = $_POST['id'];
if ($logo != "") {
$sql = "UPDATE subscriptions SET name = :name, logo = :logo, price = :price, currency_id = :currencyId, next_payment = :nextPayment, cycle = :cycle, frequency = :frequency, notes = :notes, payment_method_id = :paymentMethodId, payer_user_id = :payerUserId, category_id = :categoryId, notify = :notify, inactive = :inactive, url = :url WHERE id = :id";
$sql = "UPDATE subscriptions SET name = :name, logo = :logo, price = :price, currency_id = :currencyId,
next_payment = :nextPayment, cycle = :cycle, frequency = :frequency, notes = :notes, payment_method_id = :paymentMethodId,
payer_user_id = :payerUserId, category_id = :categoryId, notify = :notify, inactive = :inactive,
url = :url, notify_days_before = :notifyDaysBefore WHERE id = :id";
} else {
$sql = "UPDATE subscriptions SET name = :name, price = :price, currency_id = :currencyId, next_payment = :nextPayment, cycle = :cycle, frequency = :frequency, notes = :notes, payment_method_id = :paymentMethodId, payer_user_id = :payerUserId, category_id = :categoryId, notify = :notify, inactive = :inactive, url = :url WHERE id = :id";
$sql = "UPDATE subscriptions SET name = :name, price = :price, currency_id = :currencyId, next_payment = :nextPayment,
cycle = :cycle, frequency = :frequency, notes = :notes, payment_method_id = :paymentMethodId, payer_user_id = :payerUserId,
category_id = :categoryId, notify = :notify, inactive = :inactive, url = :url,notify_days_before = :notifyDaysBefore WHERE id = :id";
}
}
@@ -215,6 +221,7 @@
$stmt->bindParam(':notify', $notify, SQLITE3_INTEGER);
$stmt->bindParam(':inactive', $inactive, SQLITE3_INTEGER);
$stmt->bindParam(':url', $url, SQLITE3_TEXT);
$stmt->bindParam(':notifyDaysBefore', $notifyDaysBefore, SQLITE3_INTEGER);
if ($stmt->execute()) {
$success['status'] = "Success";
+1
View File
@@ -27,6 +27,7 @@
$subscriptionData['notify'] = $row['notify'];
$subscriptionData['inactive'] = $row['inactive'];
$subscriptionData['url'] = htmlspecialchars_decode($row['url']);
$subscriptionData['notify_days_before'] = $row['notify_days_before'];
$subscriptionJson = json_encode($subscriptionData);
header('Content-Type: application/json');