Wallos v0.9
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
|
||||
$databaseFile = '/var/www/html/db/wallos.db';
|
||||
|
||||
if (!file_exists($databaseFile)) {
|
||||
echo "Database does not exist. Creating it...";
|
||||
$db = new SQLite3($databaseFile, SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
|
||||
$db->busyTimeout(5000);
|
||||
|
||||
$db->exec('CREATE TABLE user (
|
||||
id INTEGER PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
main_currency INTEGER NOT NULL,
|
||||
avatar TEXT,
|
||||
FOREIGN KEY(main_currency) REFERENCES currencies(id)
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE payment_methods (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
icon TEXT
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE subscriptions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
logo TEXT,
|
||||
price REAL NOT NULL,
|
||||
currency_id INTEGER,
|
||||
next_payment DATE,
|
||||
cycle INTEGER,
|
||||
frequency INTEGER,
|
||||
notes TEXT,
|
||||
payment_method_id INTEGER,
|
||||
payer_user_id INTEGER,
|
||||
category_id INTEGER,
|
||||
FOREIGN KEY(currency_id) REFERENCES currencies(id),
|
||||
FOREIGN KEY(cycle) REFERENCES cycles(id),
|
||||
FOREIGN KEY(frequency) REFERENCES frequencies(id),
|
||||
FOREIGN KEY(payment_method_id) REFERENCES payment_methods(id),
|
||||
FOREIGN KEY(payer_user_id) REFERENCES user(id)
|
||||
FOREIGN KEY(category_id) REFERENCES categories(id)
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE categories (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE currencies (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
code TEXT NOT NULL,
|
||||
rate TEXT NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE household (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE login_tokens (
|
||||
user_id INTEGER NOT NULL,
|
||||
token TEXT NOT NULL,
|
||||
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE cycles (
|
||||
id INTEGER PRIMARY KEY,
|
||||
days INTEGER NOT NULL,
|
||||
name TEXT NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE frequencies (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name INTEGER NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE fixer (
|
||||
api_key TEXT NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE last_exchange_update (
|
||||
date DATE NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec('CREATE TABLE last_update_next_payment_date (
|
||||
date DATE NOT NULL
|
||||
)');
|
||||
|
||||
$db->exec("INSERT INTO categories (id, name) VALUES
|
||||
(1, 'No category'),
|
||||
(2, 'Entertainment'),
|
||||
(3, 'Music'),
|
||||
(4, 'Utilities'),
|
||||
(5, 'Food & Beverages'),
|
||||
(6, 'Health & Wellbeing'),
|
||||
(7, 'Productivity'),
|
||||
(8, 'Banking'),
|
||||
(9, 'Transport'),
|
||||
(10, 'Education'),
|
||||
(11, 'Insurance'),
|
||||
(12, 'Gaming'),
|
||||
(13, 'News & Magazines'),
|
||||
(14, 'Productivity'),
|
||||
(15, 'Technology'),
|
||||
(16, 'Cloud Services'),
|
||||
(17, 'Charity & Donations')");
|
||||
|
||||
$db->exec("INSERT INTO cycles (id, days, name) VALUES
|
||||
(1, 1, 'Daily'),
|
||||
(2, 7, 'Weekly'),
|
||||
(3, 30, 'Monthly'),
|
||||
(4, 365, 'Yearly')");
|
||||
|
||||
$db->exec("INSERT INTO frequencies (id, name) VALUES
|
||||
(1, 1),
|
||||
(2, 2),
|
||||
(3, 3),
|
||||
(4, 4),
|
||||
(5, 5),
|
||||
(6, 6),
|
||||
(7, 7),
|
||||
(8, 8),
|
||||
(9, 9),
|
||||
(10, 10),
|
||||
(11, 11),
|
||||
(12, 12),
|
||||
(13, 13),
|
||||
(14, 14),
|
||||
(15, 15),
|
||||
(16, 16),
|
||||
(17, 17),
|
||||
(18, 18),
|
||||
(19, 19),
|
||||
(20, 20),
|
||||
(21, 21),
|
||||
(22, 22),
|
||||
(23, 23),
|
||||
(24, 24),
|
||||
(25, 25),
|
||||
(26, 26),
|
||||
(27, 27),
|
||||
(28, 28),
|
||||
(29, 29),
|
||||
(30, 30),
|
||||
(31, 31)");
|
||||
|
||||
$db->exec("INSERT INTO currencies (name, symbol, code, rate) VALUES
|
||||
('Euro', '€', 'EUR', 1),
|
||||
('US Dollar', '$', 'USD', 1),
|
||||
('Japanese Yen', '¥', 'JPY', 1),
|
||||
('Bulgarian Lev', 'лв', 'BGN', 1),
|
||||
('Czech Republic Koruna', 'Kč', 'CZK', 1),
|
||||
('Danish Krone', 'kr', 'DKK', 1),
|
||||
('British Pound Sterling', '£', 'GBP', 1),
|
||||
('Hungarian Forint', 'Ft', 'HUF', 1),
|
||||
('Polish Zloty', 'zł', 'PLN', 1),
|
||||
('Romanian Leu', 'lei', 'RON', 1),
|
||||
('Swedish Krona', 'kr', 'SEK', 1),
|
||||
('Swiss Franc', 'Fr', 'CHF', 1),
|
||||
('Icelandic Króna', 'kr', 'ISK', 1),
|
||||
('Norwegian Krone', 'kr', 'NOK', 1),
|
||||
('Croatian Kuna', 'kn', 'HRK', 1),
|
||||
('Russian Ruble', '₽', 'RUB', 1),
|
||||
('Turkish Lira', '₺', 'TRY', 1),
|
||||
('Australian Dollar', '$', 'AUD', 1),
|
||||
('Brazilian Real', 'R$', 'BRL', 1),
|
||||
('Canadian Dollar', '$', 'CAD', 1),
|
||||
('Chinese Yuan', '¥', 'CNY', 1),
|
||||
('Hong Kong Dollar', 'HK$', 'HKD', 1),
|
||||
('Indonesian Rupiah', 'Rp', 'IDR', 1),
|
||||
('Israeli New Sheqel', '₪', 'ILS', 1),
|
||||
('Indian Rupee', '₹', 'INR', 1),
|
||||
('South Korean Won', '₩', 'KRW', 1),
|
||||
('Mexican Peso', 'Mex$', 'MXN', 1),
|
||||
('Malaysian Ringgit', 'RM', 'MYR', 1),
|
||||
('New Zealand Dollar', 'NZ$', 'NZD', 1),
|
||||
('Philippine Peso', '₱', 'PHP', 1),
|
||||
('Singapore Dollar', 'S$', 'SGD', 1),
|
||||
('Thai Baht', '฿', 'THB', 1),
|
||||
('South African Rand', 'R', 'ZAR', 1)");
|
||||
|
||||
$db->exec("INSERT INTO payment_methods (id, name, icon) VALUES
|
||||
(1, 'PayPal', 'paypal.png'),
|
||||
(2, 'Credit Card', 'creditcard.png'),
|
||||
(3, 'Bank Transfer', 'banktransfer.png'),
|
||||
(4, 'Direct Debit', 'directdebit.png'),
|
||||
(5, 'Money', 'money.png'),
|
||||
(6, 'Google Pay', 'googlepay.png'),
|
||||
(7, 'Samsung Pay', 'samsungpay.png'),
|
||||
(8, 'Apple Pay', 'applepay.png'),
|
||||
(9, 'Crypto', 'crypto.png'),
|
||||
(10, 'Klarna', 'klarna.png'),
|
||||
(11, 'Amazon Pay', 'amazonpay.png'),
|
||||
(12, 'SEPA', 'sepa.png'),
|
||||
(13, 'Skrill', 'skrill.png'),
|
||||
(14, 'Sofort', 'sofort.png'),
|
||||
(15, 'Stripe', 'stripe.png'),
|
||||
(16, 'Affirm', 'affirm.png'),
|
||||
(17, 'AliPay', 'alipay.png'),
|
||||
(18, 'Elo', 'elo.png'),
|
||||
(19, 'Facebook Pay', 'facebookpay.png'),
|
||||
(20, 'GiroPay', 'giropay.png'),
|
||||
(21, 'iDeal', 'ideal.png'),
|
||||
(22, 'Union Pay', 'unionpay.png'),
|
||||
(23, 'Interac', 'interac.png'),
|
||||
(24, 'WeChat', 'wechat.png'),
|
||||
(25, 'Paysafe', 'paysafe.png'),
|
||||
(26, 'Poli', 'poli.png'),
|
||||
(27, 'Qiwi', 'qiwi.png'),
|
||||
(28, 'ShopPay', 'shoppay.png'),
|
||||
(29, 'Venmo', 'venmo.png'),
|
||||
(30, 'VeriFone', 'verifone.png'),
|
||||
(31, 'WebMoney', 'webmoney.png')");
|
||||
|
||||
} else {
|
||||
echo "Database already exist. Skipping...";
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
require_once '/var/www/html/includes/connect_endpoint_crontabs.php';
|
||||
|
||||
$query = "SELECT api_key FROM fixer";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($result) {
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
if ($row) {
|
||||
$apiKey = $row['api_key'];
|
||||
|
||||
$codes = "";
|
||||
$query = "SELECT id, name, symbol, code FROM currencies";
|
||||
$result = $db->query($query);
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$codes .= $row['code'].",";
|
||||
}
|
||||
$codes = rtrim($codes, ',');
|
||||
$query = "SELECT u.main_currency, c.code FROM user u LEFT JOIN currencies c ON u.main_currency = c.id WHERE u.id = 1";
|
||||
$stmt = $db->prepare($query);
|
||||
$result = $stmt->execute();
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
$mainCurrencyCode = $row['code'];
|
||||
$mainCurrencyId = $row['main_currency'];
|
||||
|
||||
$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
|
||||
$response = file_get_contents($api_url);
|
||||
$apiData = json_decode($response, true);
|
||||
|
||||
$mainCurrencyToEUR = $apiData['rates'][$mainCurrencyCode];
|
||||
|
||||
if ($apiData !== null && isset($apiData['rates'])) {
|
||||
foreach ($apiData['rates'] as $currencyCode => $rate) {
|
||||
if ($currencyCode === $mainCurrencyCode) {
|
||||
$exchangeRate = 1.0;
|
||||
} else {
|
||||
$exchangeRate = $rate / $mainCurrencyToEUR;
|
||||
}
|
||||
$updateQuery = "UPDATE currencies SET rate = :rate WHERE code = :code";
|
||||
$updateStmt = $db->prepare($updateQuery);
|
||||
$updateStmt->bindParam(':rate', $exchangeRate, SQLITE3_TEXT);
|
||||
$updateStmt->bindParam(':code', $currencyCode, SQLITE3_TEXT);
|
||||
$updateResult = $updateStmt->execute();
|
||||
|
||||
if (!$updateResult) {
|
||||
echo "Error updating rate for currency: $currencyCode";
|
||||
}
|
||||
}
|
||||
$currentDate = new DateTime();
|
||||
$formattedDate = $currentDate->format('Y-m-d');
|
||||
|
||||
$deleteQuery = "DELETE FROM last_exchange_update";
|
||||
$deleteStmt = $db->prepare($deleteQuery);
|
||||
$deleteResult = $deleteStmt->execute();
|
||||
|
||||
$query = "INSERT INTO last_exchange_update (date) VALUES (:formattedDate)";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(':formattedDate', $formattedDate, SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
|
||||
$db->close();
|
||||
echo "Rates updated successfully!";
|
||||
}
|
||||
} else {
|
||||
echo "Exchange rates update skipped. No fixer.io api key provided";
|
||||
$apiKey = null;
|
||||
}
|
||||
} else {
|
||||
echo "Exchange rates update skipped. No fixer.io api key provided";
|
||||
$apiKey = null;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
require_once '/var/www/html/includes/connect_endpoint_crontabs.php';
|
||||
|
||||
$currentDate = new DateTime();
|
||||
$currentDateString = $currentDate->format('Y-m-d');
|
||||
|
||||
$cycles = array();
|
||||
$query = "SELECT * FROM cycles";
|
||||
$result = $db->query($query);
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$cycleId = $row['id'];
|
||||
$cycles[$cycleId] = $row;
|
||||
}
|
||||
|
||||
$query = "SELECT id, next_payment, frequency, cycle FROM subscriptions WHERE next_payment < :currentDate";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':currentDate', $currentDate->format('Y-m-d'));
|
||||
$result = $stmt->execute();
|
||||
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$subscriptionId = $row['id'];
|
||||
$nextPaymentDate = new DateTime($row['next_payment']);
|
||||
$frequency = $row['frequency'];
|
||||
$cycle = $cycles[$row['cycle']]['name'];
|
||||
|
||||
// Calculate the interval to add based on the cycle
|
||||
$intervalSpec = "P";
|
||||
if ($cycle == 'Daily') {
|
||||
$intervalSpec .= "{$frequency}D";
|
||||
} elseif ($cycle === 'Weekly') {
|
||||
$intervalSpec .= "{$frequency}W";
|
||||
} elseif ($cycle === 'Monthly') {
|
||||
$intervalSpec .= "{$frequency}M";
|
||||
} elseif ($cycle === 'Yearly') {
|
||||
$intervalSpec .= "{$frequency}Y";
|
||||
}
|
||||
|
||||
$interval = new DateInterval($intervalSpec);
|
||||
|
||||
// Add intervals until the next payment date is in the future
|
||||
while ($nextPaymentDate < $currentDate) {
|
||||
$nextPaymentDate->add($interval);
|
||||
}
|
||||
|
||||
// Update the subscription's next_payment date
|
||||
$updateQuery = "UPDATE subscriptions SET next_payment = :nextPaymentDate WHERE id = :subscriptionId";
|
||||
$updateStmt = $db->prepare($updateQuery);
|
||||
$updateStmt->bindValue(':nextPaymentDate', $nextPaymentDate->format('Y-m-d'));
|
||||
$updateStmt->bindValue(':subscriptionId', $subscriptionId);
|
||||
$updateStmt->execute();
|
||||
}
|
||||
|
||||
$formattedDate = $currentDate->format('Y-m-d');
|
||||
|
||||
$deleteQuery = "DELETE FROM last_update_next_payment_date";
|
||||
$deleteStmt = $db->prepare($deleteQuery);
|
||||
$deleteResult = $deleteStmt->execute();
|
||||
|
||||
$query = "INSERT INTO last_update_next_payment_date (date) VALUES (:formattedDate)";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(':formattedDate', $currentDateString, SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
|
||||
echo "Updated next payment dates";
|
||||
?>
|
||||
Reference in New Issue
Block a user