Skip to content

Commit

Permalink
feat: add payment cycle to csv/json export
Browse files Browse the repository at this point in the history
feat: run db migration after restoring database
feat: run db migration after importing db
feat: store weekly the total yearly cost of subscriptions
fix: double encoding in statistics labels
  • Loading branch information
ellite authored Dec 11, 2024
1 parent 66ad4d8 commit 5e6bc90
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Use the php:8.0.5-fpm-alpine base image
# Use the php:8.2-fpm-alpine base image
FROM php:8.2-fpm-alpine

# Set working directory to /var/www/html
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ See instructions to run Wallos below.
*/2 * * * * php /var/www/html/endpoints/cronjobs/sendverificationemails.php >> /var/log/cron/sendverificationemail.log 2>&1
*/2 * * * * php /var/www/html/endpoints/cronjobs/sendresetpasswordemails.php >> /var/log/cron/sendresetpasswordemails.log 2>&1
0 */6 * * * php /var/www/html/endpoints/cronjobs/checkforupdates.php >> /var/log/cron/checkforupdates.log 2>&1
30 1 * * 1 php /var/www/html/endpoints/cronjobs/storetotalyearlycost.php >> /var/log/cron/storetotalyearlycost.log 2>&1
```

5. If your web root is not `/var/www/html/` adjust the cronjobs above accordingly.
Expand Down
1 change: 1 addition & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ class="one-third" value="<?= $settings['smtp_port'] ?>" />
<input type="button" value="Send Verification Emails" class="button tiny mobile-grow" onclick="executeCronJob('sendverificationemails')">
<input type="button" value="Update Exchange Rates" class="button tiny mobile-grow" onclick="executeCronJob('updateexchange')">
<input type="button" value="Update Next Payments" class="button tiny mobile-grow" onclick="executeCronJob('updatenextpayment')">
<input type="button" value="Store Total Yearly Cost" class="button tiny mobile-grow" onclick="executeCronJob('storetotalyearlycost')">
</div>
<div class="inline-row">
<textarea id="cronjobResult" class="thin" readonly></textarea>
Expand Down
1 change: 1 addition & 0 deletions cronjobs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/2 * * * * /usr/local/bin/php /var/www/html/endpoints/cronjobs/sendverificationemails.php >> /var/log/cron/sendverificationemails.log 2>&1
*/2 * * * * /usr/local/bin/php /var/www/html/endpoints/cronjobs/sendresetpasswordemails.php >> /var/log/cron/sendresetpasswordemails.log 2>&1
0 */6 * * * /usr/local/bin/php /var/www/html/endpoints/cronjobs/checkforupdates.php >> /var/log/cron/checkforupdates.log 2>&1
30 1 * * 1 /usr/local/bin/php /var/www/html/endpoints/cronjobs/storetotalyearlycost.php >> /var/log/cron/storetotalyearlycost.log 2>&1
72 changes: 72 additions & 0 deletions endpoints/cronjobs/storetotalyearlycost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

require_once __DIR__ . '/../../includes/connect_endpoint_crontabs.php';

if (php_sapi_name() == 'cli') {
$date = new DateTime('now');
echo "\n" . $date->format('Y-m-d') . " " . $date->format('H:i:s') . "<br />\n";
}

$currentDate = new DateTime();
$currentDateString = $currentDate->format('Y-m-d');

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;
}
}

// Get all users

$query = "SELECT id, main_currency FROM user";
$stmt = $db->prepare($query);
$result = $stmt->execute();

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$userId = $row['id'];
$userCurrencyId = $row['main_currency'];
$totalYearlyCost = 0;

$query = "SELECT * FROM subscriptions WHERE user_id = :userId";
$stmt = $db->prepare($query);
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$resultSubscriptions = $stmt->execute();

while ($rowSubscriptions = $resultSubscriptions->fetchArray(SQLITE3_ASSOC)) {
$price = getPriceConverted($rowSubscriptions['price'], $rowSubscriptions['currency_id'], $db, $userId);
$totalYearlyCost += $price;
}

$query = "INSERT INTO total_yearly_cost (user_id, date, cost, currency) VALUES (:userId, :date, :cost, :currency)";
$stmt = $db->prepare($query);
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$stmt->bindParam(':date', $currentDateString, SQLITE3_TEXT);
$stmt->bindParam(':cost', $totalYearlyCost, SQLITE3_FLOAT);
$stmt->bindParam(':currency', $userCurrencyId, SQLITE3_INTEGER);

if ($stmt->execute()) {
echo "Inserted total yearly cost for user " . $userId . " with cost " . $totalYearlyCost . "<br />\n";
} else {
echo "Error inserting total yearly cost for user " . $userId . "<br />\n";
}
}








?>
17 changes: 17 additions & 0 deletions endpoints/subscriptions/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,25 @@
$result = $stmt->execute();

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$cycle = $cycles[$row['cycle']]['name'];
$frequency =$row['frequency'];

$cyclesMap = array(
'Daily' => 'Days',
'Weekly' => 'Weeks',
'Monthly' => 'Months',
'Yearly' => 'Years'
);

if ($frequency == 1) {
$cyclePrint = $cycle;
} else {
$cyclePrint = "Every " . $frequency . " " . $cyclesMap[$cycle];
}

$subscriptionDetails = array(
'Name' => str_replace(',', ' ', $row['name']),
'Payment Cycle' => $cyclePrint,
'Next Payment' => $row['next_payment'],
'Renewal' => $row['auto_renew'] ? 'Automatic' : 'Manual',
'Category' => str_replace(',', ' ', $categories[$row['category_id']]['name']),
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.40.0";
$version = "v2.41.0";
?>
11 changes: 9 additions & 2 deletions scripts/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ function restoreDB() {
.then(response => response.json())
.then(data => {
if (data.success) {
showSuccessMessage(data.message)
window.location.href = 'logout.php';
showSuccessMessage(data.message);
fetch('endpoints/db/migrate.php')
.then(response => response.text())
.then(() => {
window.location.href = 'logout.php';
})
.catch(error => {
window.location.href = 'logout.php';
});
} else {
showErrorMessage(data.message);
}
Expand Down
Loading

0 comments on commit 5e6bc90

Please sign in to comment.