diff --git a/accounts.php b/accounts.php
new file mode 100644
index 0000000..1c2b153
--- /dev/null
+++ b/accounts.php
@@ -0,0 +1,181 @@
+query('SELECT id, name FROM users ORDER BY name');
+$users = $user_stmt->fetchAll();
+
+// Handle form submissions for adding/editing an account
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+ $name = trim($_POST['name']);
+ $user_id = $_POST['user_id'] ?? null;
+ $initial_balance = $_POST['initial_balance'] ?? 0;
+ $currency = $_POST['currency'] ?? 'USD';
+ $id = $_POST['id'] ?? null;
+
+ if (empty($name)) {
+ $error = 'Account name is required.';
+ } else {
+ try {
+ if ($id) {
+ // Update existing account
+ $stmt = $pdo->prepare('UPDATE accounts SET name = ?, user_id = ?, initial_balance = ?, currency = ? WHERE id = ?');
+ $stmt->execute([$name, $user_id, $initial_balance, $currency, $id]);
+ $message = 'Account updated successfully!';
+ } else {
+ // Insert new account
+ $stmt = $pdo->prepare('INSERT INTO accounts (name, user_id, initial_balance, currency) VALUES (?, ?, ?, ?)');
+ $stmt->execute([$name, $user_id, $initial_balance, $currency]);
+ $message = 'Account added successfully!';
+ }
+ } catch (PDOException $e) {
+ $error = 'Database error: ' . $e->getMessage();
+ }
+ }
+}
+
+// Handle deleting an account
+if (isset($_GET['delete'])) {
+ $id = $_GET['delete'];
+ try {
+ $stmt = $pdo->prepare('DELETE FROM accounts WHERE id = ?');
+ $stmt->execute([$id]);
+ $message = 'Account deleted successfully!';
+ } catch (PDOException $e) {
+ $error = 'Error deleting account. It might be associated with expenses.';
+ }
+}
+
+// Handle fetching an account for editing
+if (isset($_GET['edit'])) {
+ $id = $_GET['edit'];
+ $stmt = $pdo->prepare('SELECT * FROM accounts WHERE id = ?');
+ $stmt->execute([$id]);
+ $edit_account = $stmt->fetch();
+}
+
+// Fetch all accounts to display
+$stmt = $pdo->query('SELECT a.*, u.name as user_name FROM accounts a LEFT JOIN users u ON a.user_id = u.id ORDER BY a.name');
+$accounts = $stmt->fetchAll();
+
+$currencies = ['USD', 'EUR', 'COP'];
+
+?>
+
+
+
+
+
+ Manage Accounts
+
+
+
+
+
+
+
+ Manage Accounts
+ Back to Dashboard
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ | Name |
+ Owner |
+ Balance |
+ Currency |
+ Actions |
+
+
+
+
+
+ | No accounts found. |
+
+
+
+
+ |
+ |
+ |
+ |
+
+ Edit
+ Delete
+ |
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/add_expense.php b/add_expense.php
index 8424cf2..18540c2 100644
--- a/add_expense.php
+++ b/add_expense.php
@@ -1,3 +1,63 @@
+query('SELECT * FROM categories ORDER BY name')->fetchAll();
+$users = $pdo->query('SELECT * FROM users ORDER BY name')->fetchAll();
+$accounts = $pdo->query('SELECT * FROM accounts ORDER BY name')->fetchAll();
+
+$expense_types = ['expense', 'income', 'transfer'];
+$split_types = ['none', 'equally', 'parts', 'amounts'];
+$currencies = ['USD', 'EUR', 'COP'];
+
+if ($_SERVER["REQUEST_METHOD"] == "POST") {
+ try {
+ // Sanitize and validate input
+ $expense_date = filter_input(INPUT_POST, 'expense_date', FILTER_SANITIZE_STRING);
+ $title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
+ $amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT);
+ $currency = filter_input(INPUT_POST, 'currency', FILTER_SANITIZE_STRING);
+
+ $category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
+ $user_id = filter_input(INPUT_POST, 'user_id', FILTER_VALIDATE_INT);
+ $account_id = filter_input(INPUT_POST, 'account_id', FILTER_VALIDATE_INT);
+ $expense_type = filter_input(INPUT_POST, 'expense_type', FILTER_SANITIZE_STRING);
+ $split_type = filter_input(INPUT_POST, 'split_type', FILTER_SANITIZE_STRING);
+
+ // Handle file upload
+ $receipt_path = null;
+ if (isset($_FILES['receipt']) && $_FILES['receipt']['error'] == UPLOAD_ERR_OK) {
+ $upload_dir = __DIR__ . '/assets/uploads/';
+ if (!is_dir($upload_dir)) {
+ mkdir($upload_dir, 0775, true);
+ }
+ $filename = uniqid() . '-' . basename($_FILES['receipt']['name']);
+ $receipt_path = '/assets/uploads/' . $filename;
+ move_uploaded_file($_FILES['receipt']['tmp_name'], $upload_dir . $filename);
+ }
+
+ $sql = "INSERT INTO expenses (expense_date, title, amount, currency, expense_type, split_type, category_id, user_id, account_id, receipt_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+ $stmt = $pdo->prepare($sql);
+
+ if ($stmt->execute([$expense_date, $title, $amount, $currency, $expense_type, $split_type, $category_id, $user_id, $account_id, $receipt_path])) {
+ header("Location: index.php?message=success");
+ exit;
+ } else {
+ $error = "Error saving the expense.";
+ }
+ } catch (PDOException $e) {
+ error_log("DB Error: " . $e->getMessage());
+ $error = "Database error. Please try again later.";
+ } catch (Exception $e) {
+ error_log("File Upload Error: " . $e->getMessage());
+ $error = "Error uploading the file.";
+ }
+}
+?>
@@ -14,72 +74,90 @@