Skip to content

Commit

Permalink
feat: replacement for disabled subscriptions, to more accurately calc…
Browse files Browse the repository at this point in the history
…ulate savings
  • Loading branch information
Miguel Ribeiro committed Oct 29, 2024
1 parent cb0659d commit 29eef51
Show file tree
Hide file tree
Showing 32 changed files with 219 additions and 55 deletions.
1 change: 1 addition & 0 deletions api/subscriptions/get_subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"category_name": "Entertainment",
"payer_user_name": "Jane Doe",
"payment_method_name": "Credit Card"
"replacement_subscription_id": 1
}
],
"notes": []
Expand Down
57 changes: 40 additions & 17 deletions endpoints/subscription/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ function resizeAndUploadLogo($uploadedFile, $uploadDir, $name, $settings)
$newHeight = $height;

if ($width > $targetWidth) {
$newWidth = (int)$targetWidth;
$newHeight = (int)(($targetWidth / $width) * $height);
$newWidth = (int) $targetWidth;
$newHeight = (int) (($targetWidth / $width) * $height);
}

if ($newHeight > $targetHeight) {
$newWidth = (int)(($targetHeight / $newHeight) * $newWidth);
$newHeight = (int)$targetHeight;
$newWidth = (int) (($targetHeight / $newHeight) * $newWidth);
$newHeight = (int) $targetHeight;
}

$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
Expand Down Expand Up @@ -178,6 +178,11 @@ function resizeAndUploadLogo($uploadedFile, $uploadDir, $name, $settings)
$notifyDaysBefore = $_POST['notify_days_before'];
$inactive = isset($_POST['inactive']) ? true : false;
$cancellationDate = $_POST['cancellation_date'] ?? null;
$replacementSubscriptionId = $_POST['replacement_subscription_id'];

if ($replacementSubscriptionId == 0 || $inactive == 0) {
$replacementSubscriptionId = null;
}

if ($logoUrl !== "") {
$logo = getLogoFromUrl($logoUrl, '../../images/uploads/logos/', $name, $settings, $i18n);
Expand All @@ -193,23 +198,40 @@ function resizeAndUploadLogo($uploadedFile, $uploadDir, $name, $settings)
}

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, notify_days_before, user_id, cancellation_date)
VALUES (:name, :logo, :price, :currencyId, :nextPayment, :cycle, :frequency, :notes,
:paymentMethodId, :payerUserId, :categoryId, :notify, :inactive, :url, :notifyDaysBefore, :userId, :cancellationDate)";
$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,
notify_days_before, user_id, cancellation_date, replacement_subscription_id
) VALUES (
:name, :logo, :price, :currencyId, :nextPayment, :cycle, :frequency, :notes,
:paymentMethodId, :payerUserId, :categoryId, :notify, :inactive, :url,
:notifyDaysBefore, :userId, :cancellationDate, :replacement_subscription_id
)";
} else {
$id = $_POST['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,
cancellation_date = :cancellationDate,
replacement_subscription_id = :replacement_subscription_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, notify_days_before = :notifyDaysBefore, cancellation_date = :cancellationDate WHERE id = :id AND user_id = :userId";
} 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, notify_days_before = :notifyDaysBefore, cancellation_date = :cancellationDate WHERE id = :id AND user_id = :userId";
$sql .= ", logo = :logo";
}

$sql .= " WHERE id = :id AND user_id = :userId";
}

$stmt = $db->prepare($sql);
Expand All @@ -235,6 +257,7 @@ function resizeAndUploadLogo($uploadedFile, $uploadDir, $name, $settings)
$stmt->bindParam(':id', $id, SQLITE3_INTEGER);
}
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$stmt->bindParam(':replacement_subscription_id', $replacementSubscriptionId, SQLITE3_INTEGER);

if ($stmt->execute()) {
$success['status'] = "Success";
Expand Down
3 changes: 2 additions & 1 deletion endpoints/subscription/clone.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
]));
}

$query = "INSERT INTO subscriptions (name, logo, price, currency_id, next_payment, cycle, frequency, notes, payment_method_id, payer_user_id, category_id, notify, url, inactive, notify_days_before, user_id, cancellation_date) VALUES (:name, :logo, :price, :currency_id, :next_payment, :cycle, :frequency, :notes, :payment_method_id, :payer_user_id, :category_id, :notify, :url, :inactive, :notify_days_before, :user_id, :cancellation_date)";
$query = "INSERT INTO subscriptions (name, logo, price, currency_id, next_payment, cycle, frequency, notes, payment_method_id, payer_user_id, category_id, notify, url, inactive, notify_days_before, user_id, cancellation_date, replacement_subscription_id) VALUES (:name, :logo, :price, :currency_id, :next_payment, :cycle, :frequency, :notes, :payment_method_id, :payer_user_id, :category_id, :notify, :url, :inactive, :notify_days_before, :user_id, :cancellation_date, :replacement_subscription_id)";
$cloneStmt = $db->prepare($query);
$cloneStmt->bindValue(':name', $subscriptionToClone['name'], SQLITE3_TEXT);
$cloneStmt->bindValue(':logo', $subscriptionToClone['logo'], SQLITE3_TEXT);
Expand All @@ -36,6 +36,7 @@
$cloneStmt->bindValue(':notify_days_before', $subscriptionToClone['notify_days_before'], SQLITE3_INTEGER);
$cloneStmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$cloneStmt->bindValue(':cancellation_date', $subscriptionToClone['cancellation_date'], SQLITE3_TEXT);
$cloneStmt->bindValue(':replacement_subscription_id', $subscriptionToClone['id'], SQLITE3_INTEGER);

if ($cloneStmt->execute()) {
$response = [
Expand Down
44 changes: 25 additions & 19 deletions endpoints/subscription/delete.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
<?php
require_once '../../includes/connect_endpoint.php';

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
if ($_SERVER["REQUEST_METHOD"] === "DELETE") {
$subscriptionId = $_GET["id"];
$deleteQuery = "DELETE FROM subscriptions WHERE id = :subscriptionId AND user_id = :userId";
$deleteStmt = $db->prepare($deleteQuery);
$deleteStmt->bindParam(':subscriptionId', $subscriptionId, SQLITE3_INTEGER);
$deleteStmt->bindParam(':userId', $userId, SQLITE3_INTEGER);

if ($deleteStmt->execute()) {
http_response_code(204);
} else {
http_response_code(500);
echo json_encode(array("message" => translate('error_deleting_subscription', $i18n)));
}
require_once '../../includes/connect_endpoint.php';

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
if ($_SERVER["REQUEST_METHOD"] === "DELETE") {
$subscriptionId = $_GET["id"];
$deleteQuery = "DELETE FROM subscriptions WHERE id = :subscriptionId AND user_id = :userId";
$deleteStmt = $db->prepare($deleteQuery);
$deleteStmt->bindParam(':subscriptionId', $subscriptionId, SQLITE3_INTEGER);
$deleteStmt->bindParam(':userId', $userId, SQLITE3_INTEGER);

if ($deleteStmt->execute()) {
$query = "UPDATE subscriptions SET replacement_subscription_id = NULL WHERE replacement_subscription_id = :subscriptionId AND user_id = :userId";
$stmt = $db->prepare($query);
$stmt->bindParam(':subscriptionId', $subscriptionId, SQLITE3_INTEGER);
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$stmt->execute();

http_response_code(204);
} else {
http_response_code(405);
echo json_encode(array("message" => translate('invalid_request_method', $i18n)));
http_response_code(500);
echo json_encode(array("message" => translate('error_deleting_subscription', $i18n)));
}
} else {
http_response_code(405);
echo json_encode(array("message" => translate('invalid_request_method', $i18n)));
}
$db->close();
}
$db->close();
?>
1 change: 1 addition & 0 deletions endpoints/subscription/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$subscriptionData['url'] = htmlspecialchars_decode($row['url'] ?? "");
$subscriptionData['notify_days_before'] = $row['notify_days_before'];
$subscriptionData['cancellation_date'] = $row['cancellation_date'];
$subscriptionData['replacement_subscription_id'] = $row['replacement_subscription_id'];

$subscriptionJson = json_encode($subscriptionData);
header('Content-Type: application/json');
Expand Down
3 changes: 2 additions & 1 deletion endpoints/subscriptions/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
'URL' => $row['url'],
'State' => $row['inactive'] ? 'Disabled' : 'Enabled',
'Notifications' => $row['notify'] ? 'Enabled' : 'Disabled',
'Cancellation Date' => $row['cancellation_date']
'Cancellation Date' => $row['cancellation_date'],
'Active' => $row['inactive'] ? 'No' : 'Yes',
);

$subscriptions[] = $subscriptionDetails;
Expand Down
1 change: 1 addition & 0 deletions endpoints/subscriptions/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
$print[$id]['inactive'] = $subscription['inactive'];
$print[$id]['url'] = $subscription['url'] ?? "";
$print[$id]['notes'] = $subscription['notes'] ?? "";
$print[$id]['replacement_subscription_id'] = $subscription['replacement_subscription_id'];

if (isset($settings['convertCurrency']) && $settings['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) {
$print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db);
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Preis",
"next_payment" => "Nächste Zahlung",
"inactive" => "Abonnement deaktivieren",
"replaced_with" => "Ersetzt durch",
"none" => "Keine",
"member" => "Mitglied",
"category" => "Kategorie",
"payment_method" => "Zahlungsmethode",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Τιμή",
"next_payment" => "Επόμενη πληρωμή",
"inactive" => "Απενεργοποίηση συνδρομής",
"replaced_with" => "Αντικαταστάθηκε με",
"none" => "Κανένα",
"member" => "Χρήστης",
"category" => "Κατηγορία",
"payment_method" => "Τρόπος πληρωμής",
Expand Down
4 changes: 3 additions & 1 deletion includes/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Price",
"next_payment" => "Next Payment",
"inactive" => "Disable Subscription",
"replaced_with" => "Replaced with",
"none" => "None",
"member" => "Member",
"category" => "Category",
"payment_method" => "Payment Method",
Expand Down Expand Up @@ -86,7 +88,7 @@
"notes" => "Notes",
"enable_notifications" => "Enable Notifications for this subscription",
"default_value_from_settings" => "Default value from settings",
"cancellation_notification" => "cancellation Notification",
"cancellation_notification" => "Cancellation Notification",
"delete" => "Delete",
"cancel" => "Cancel",
"upload_logo" => "Upload Logo",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Precio",
"next_payment" => "Próximo Pago",
"inactive" => "Desactivar Suscripción",
"replaced_with" => "Reemplazada con",
"none" => "Ninguna",
"member" => "Miembro",
"category" => "Categoría",
"payment_method" => "Método de Pago",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Prix",
"next_payment" => "Prochain paiement",
"inactive" => "Désactiver l'abonnement",
"replaced_with" => "Remplacé par",
"none" => "Aucun",
"member" => "Membre",
"category" => "Catégorie",
"payment_method" => "Méthode de paiement",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"price" => 'Prezzo',
"next_payment" => 'Prossimo pagamento',
"inactive" => 'Disattiva abbonamento',
"replaced_with" => 'Sostituito con',
"none" => 'Nessuno',
"member" => 'Membro',
"category" => 'Categoria',
"payment_method" => 'Metodo di pagamento',
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/jp.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "金額",
"next_payment" => "次回支払い",
"inactive" => "サブスクリプションを無効にする",
"replaced_with" => "置き換えられた",
"none" => "なし",
"member" => "メンバー",
"category" => "カテゴリ",
"payment_method" => "支払い方法",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/ko.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "가격",
"next_payment" => "다음 결제일",
"inactive" => "구독 비활성화",
"replaced_with" => "다음 구독으로 대체됨",
"none" => "없음",
"member" => "구성원",
"category" => "카테고리",
"payment_method" => "지불 수단",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/pl.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Cena",
"next_payment" => "Następna płatność",
"inactive" => "Wyłącz subskrypcję",
"replaced_with" => "Zastąpione przez",
"none" => "Brak",
"member" => "Użytkownik",
"category" => "Kategoria",
"payment_method" => "Metoda płatności",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Preço",
"next_payment" => "Próximo Pagamento",
"inactive" => "Desactivar Subscrição",
"replaced_with" => "Substituída por",
"none" => "Nenhuma",
"member" => "Membro",
"category" => "Categoria",
"payment_method" => "Metodo de Pagamento",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/pt_br.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Preço",
"next_payment" => "Próximo pagamento",
"inactive" => "Assinatura inativa",
"replaced_with" => "Substituída por",
"none" => "Nenhuma",
"member" => "Membro",
"category" => "Categoria",
"payment_method" => "Método de Pagamento",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Стоимость",
"next_payment" => "Следующий платеж",
"inactive" => "Отключить подписку",
"replaced_with" => "Заменена на",
"none" => "Нет",
"member" => "Член семьи",
"category" => "Категория",
"payment_method" => "Способ оплаты",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/sl.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Cena",
"next_payment" => "Naslednje plačilo",
"inactive" => "Onemogoči naročnino",
"replaced_with" => "Zamenjano z",
"none" => "brez",
"member" => "Član",
"category" => "Kategorija",
"payment_method" => "Način plačila",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/sr.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Цена",
"next_payment" => "Следећа уплата",
"inactive" => "Онемогући претплату",
"replaced_with" => "Замењено са",
"none" => "Ништа",
"member" => "Члан",
"category" => "Категорија",
"payment_method" => "Начин плаћања",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/sr_lat.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Cena",
"next_payment" => "Sledeća uplata",
"inactive" => "Onemogući pretplatu",
"replaced_with" => "Zamenjeno sa",
"none" => "Nijedna",
"member" => "Član",
"category" => "Kategorija",
"payment_method" => "Način plaćanja",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Fiyat",
"next_payment" => "Sonraki Ödeme",
"inactive" => "Aboneliği Devre Dışı Bırak",
"replaced_with" => "Şununla değiştirildi",
"none" => "Yok",
"member" => "Üye",
"category" => "Kategori",
"payment_method" => "Ödeme Yöntemi",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/vi.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "Giá",
"next_payment" => "Thanh toán tiếp theo",
"inactive" => "Vô hiệu hóa đăng ký",
"replaced_with" => "Thay thế bằng",
"none" => "Không",
"member" => "Thành viên",
"category" => "Danh mục",
"payment_method" => "Phương thức thanh toán",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/zh_cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"price" => "价格",
"next_payment" => "下次支付时间",
"inactive" => "停用订阅",
"replaced_with" => "替换为",
"none" => "",
"member" => "成员",
"category" => "分类",
"payment_method" => "支付方式",
Expand Down
2 changes: 2 additions & 0 deletions includes/i18n/zh_tw.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"price" => "價格",
"next_payment" => "下次付款時間",
"inactive" => "停用訂閱",
"replaced_with" => "替換為",
"none" => "",
"member" => "成員",
"category" => "分類",
"payment_method" => "付款方式",
Expand Down
2 changes: 1 addition & 1 deletion includes/version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$version = "v2.32.0";
$version = "v2.33.0";
?>
Loading

0 comments on commit 29eef51

Please sign in to comment.