182 lines
8.5 KiB
PHP
182 lines
8.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$pdo = db();
|
|
$message = '';
|
|
$error = '';
|
|
$edit_account = null;
|
|
|
|
// Fetch users for the dropdown
|
|
$user_stmt = $pdo->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'];
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Manage Accounts</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<header class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1>Manage Accounts</h1>
|
|
<a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
|
|
</header>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Add/Edit Account Form -->
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<h5 class="mb-0"><?php echo $edit_account ? 'Edit Account' : 'Add New Account'; ?></h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="accounts.php" method="POST">
|
|
<input type="hidden" name="id" value="<?php echo htmlspecialchars($edit_account['id'] ?? ''); ?>">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="name" class="form-label">Account Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($edit_account['name'] ?? ''); ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="user_id" class="form-label">Owner (User)</label>
|
|
<select class="form-select" id="user_id" name="user_id">
|
|
<option value="">None</option>
|
|
<?php foreach ($users as $user): ?>
|
|
<option value="<?php echo $user['id']; ?>" <?php echo (isset($edit_account['user_id']) && $edit_account['user_id'] == $user['id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($user['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="initial_balance" class="form-label">Initial Balance</label>
|
|
<input type="number" step="0.01" class="form-control" id="initial_balance" name="initial_balance" value="<?php echo htmlspecialchars($edit_account['initial_balance'] ?? '0.00'); ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="currency" class="form-label">Currency</label>
|
|
<select class="form-select" id="currency" name="currency" required>
|
|
<?php foreach ($currencies as $currency): ?>
|
|
<option value="<?php echo $currency; ?>" <?php echo (isset($edit_account['currency']) && $edit_account['currency'] == $currency) ? 'selected' : ''; ?>><?php echo $currency; ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary"><?php echo $edit_account ? 'Update Account' : 'Add Account'; ?></button>
|
|
<?php if ($edit_account): ?>
|
|
<a href="accounts.php" class="btn btn-secondary">Cancel Edit</a>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Accounts List -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="mb-0">Existing Accounts</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Owner</th>
|
|
<th class="text-end">Balance</th>
|
|
<th>Currency</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($accounts)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center">No accounts found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($accounts as $account): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($account['name']); ?></td>
|
|
<td><?php echo htmlspecialchars($account['user_name'] ?? 'N/A'); ?></td>
|
|
<td class="text-end font-monospace"><?php echo htmlspecialchars(number_format((float)$account['initial_balance'], 2)); ?></td>
|
|
<td><?php echo htmlspecialchars($account['currency']); ?></td>
|
|
<td class="text-end">
|
|
<a href="accounts.php?edit=<?php echo $account['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i> Edit</a>
|
|
<a href="accounts.php?delete=<?php echo $account['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this account?');"><i class="bi bi-trash"></i> Delete</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|