PNG IHDR x sBIT|d pHYs + tEXtSoftware www.inkscape.org< ,tEXtComment
<?php
require_once "../config.php";
session_start();
if (!isset($_SESSION['auth'])) {
header("Location: ../login.php");
exit;
}
$user_id = $_SESSION['auth'];
// Fetch user info
$stmt = $pdo->prepare("SELECT fullname FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Fetch user accounts
$stmt = $pdo->prepare("SELECT id, account_number, currency, balance FROM accounts WHERE user_id = ?");
$stmt->execute([$user_id]);
$accounts = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Handle transaction (deposit / withdrawal)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$account_id = hex2bin($_POST['account_id']);
$type = $_POST['type'];
$amount = floatval($_POST['amount']);
// Fetch account details
$stmt = $pdo->prepare("SELECT balance, currency FROM accounts WHERE id = ? AND user_id = ?");
$stmt->execute([$account_id, $user_id]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$account) {
$error = "Invalid account.";
} elseif ($amount <= 0) {
$error = "Enter a valid amount.";
} else {
$balance = $account['balance'];
$currency = $account['currency'];
$interest_rate = 2.0; // 2% daily interest for demo
$pdo->beginTransaction();
try {
if ($type === 'deposit') {
if ($balance < $amount) {
throw new Exception("Insufficient funds for deposit.");
}
// Deduct from account
$pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE id = ?")
->execute([$amount, $account_id]);
// Insert savings record
$pdo->prepare("
INSERT INTO savings (user_id, account_id, amount, interest_rate, type)
VALUES (?, ?, ?, ?, 'deposit')
")->execute([$user_id, $account_id, $amount, $interest_rate]);
} elseif ($type === 'withdrawal') {
// Find total deposits and withdrawals
$stmt = $pdo->prepare("
SELECT
(SELECT COALESCE(SUM(amount + interest_accumulated),0) FROM savings WHERE user_id=? AND account_id=? AND type='deposit') -
(SELECT COALESCE(SUM(amount),0) FROM savings WHERE user_id=? AND account_id=? AND type='withdrawal')
");
$stmt->execute([$user_id, $account_id, $user_id, $account_id]);
$available = $stmt->fetchColumn();
if ($available < $amount) {
throw new Exception("Insufficient savings balance to withdraw.");
}
// Add back to account
$pdo->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?")
->execute([$amount, $account_id]);
// Log withdrawal
$pdo->prepare("
INSERT INTO savings (user_id, account_id, amount, interest_rate, type)
VALUES (?, ?, ?, ?, 'withdrawal')
")->execute([$user_id, $account_id, $amount, $interest_rate]);
}
$pdo->commit();
header("Location: savings.php?success");
exit;
} catch (Exception $e) {
$pdo->rollBack();
$error = $e->getMessage();
}
}
}
// Auto-update daily interest for deposits (simulated simple compounding)
$pdo->exec("
UPDATE savings
SET interest_accumulated = interest_accumulated + (amount * (interest_rate / 100))
WHERE type='deposit'
");
// Fetch all savings with account info
$stmt = $pdo->prepare("
SELECT s.*, a.account_number, a.currency
FROM savings s
JOIN accounts a ON s.account_id = a.id
WHERE s.user_id = ?
ORDER BY s.id DESC
");
$stmt->execute([$user_id]);
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Calculate total savings and interest
$stmt = $pdo->prepare("
SELECT
COALESCE(SUM(CASE WHEN type='deposit' THEN amount + interest_accumulated ELSE -amount END),0) as total_savings,
COALESCE(SUM(interest_accumulated),0) as total_interest
FROM savings WHERE user_id=?
");
$stmt->execute([$user_id]);
$summary = $stmt->fetch(PDO::FETCH_ASSOC);
$total_savings = $summary['total_savings'];
$total_interest = $summary['total_interest'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Savings - Evergreen Bank</title>
<link rel="stylesheet" href="user_style.css">
<style>
body { font-family: "Segoe UI", sans-serif; background: #f4f7fb; margin: 0; }
.dashboard-container { display: flex; min-height: 100vh; }
.sidebar {
width: 250px; background: #0a2342; color: #fff; display: flex;
flex-direction: column; justify-content: space-between; padding: 20px;
}
.sidebar a {
color: #cbd5e1; text-decoration: none; padding: 10px 0;
border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar a:hover, .sidebar a.active { color: #fff; font-weight: bold; }
.main-content { flex: 1; padding: 30px; background: white; border-radius: 20px 0 0 20px; }
.summary {
display: flex; gap: 20px; margin-bottom: 30px;
}
.summary-card {
flex: 1; background: linear-gradient(145deg, #0b274d, #143b7a);
color: #fff; padding: 20px; border-radius: 10px; text-align: center;
}
form {
background: #f8f9fc; padding: 20px; border-radius: 10px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1); margin-bottom: 30px;
}
form select, form input, form button {
width: 100%; padding: 10px; margin-top: 10px;
border-radius: 6px; border: 1px solid #ccc;
}
form button {
background: #0070f3; color: white; border: none;
cursor: pointer; font-weight: 600; transition: 0.3s;
}
form button:hover { background: #005ad1; }
table {
width: 100%; border-collapse: collapse;
}
th, td { padding: 10px; border-bottom: 1px solid #ddd; text-align: left; }
.success { color: green; }
.error { color: red; }
</style>
</head>
<body>
<div class="dashboard-container">
<aside class="sidebar">
<div>
<h2>Evergreen Bank</h2>
<a href="dashboard.php">🏠 Dashboard</a>
<a href="savings.php" class="active">💰 Savings</a>
<a href="investment.php">📈 Investment</a>
<a href="business.php">🏢 Business</a>
<a href="mortgage.php">🏡 Mortgage</a>
<a href="equity.php">📊 Equity</a>
<a href="cds.php">💼 CDs</a>
</div>
<a href="logout.php">🚪 Logout</a>
</aside>
<main class="main-content">
<h1>My Savings</h1>
<div class="summary">
<div class="summary-card">
<h3>Total Savings</h3>
<h2><?= number_format($total_savings, 2) ?></h2>
</div>
<div class="summary-card">
<h3>Total Interest Earned</h3>
<h2><?= number_format($total_interest, 2) ?></h2>
</div>
</div>
<?php if (!empty($error)): ?>
<p class="error"><?= htmlspecialchars($error) ?></p>
<?php elseif (isset($_GET['success'])): ?>
<p class="success">✅ Transaction successful!</p>
<?php endif; ?>
<?php if ($accounts): ?>
<form method="POST">
<label>Select Account</label>
<select name="account_id" id="accountSelect" required>
<option value="">-- Select Account --</option>
<?php foreach ($accounts as $acc): ?>
<option value="<?= bin2hex($acc['id']) ?>" data-currency="<?= htmlspecialchars($acc['currency']) ?>">
<?= $acc['account_number'] ?> — <?= htmlspecialchars($acc['currency']) ?> <?= number_format($acc['balance'],2) ?>
</option>
<?php endforeach; ?>
</select>
<label>Transaction Type</label>
<select name="type" required>
<option value="deposit">Deposit to Savings</option>
<option value="withdrawal">Withdraw from Savings</option>
</select>
<label>Amount</label>
<input type="number" step="0.01" name="amount" placeholder="Enter amount" required>
<button type="submit">Submit</button>
</form>
<?php endif; ?>
<h2>Transaction History</h2>
<table>
<thead>
<tr><th>Date</th><th>Account</th><th>Type</th><th>Amount</th><th>Interest</th><th>Currency</th></tr>
</thead>
<tbody>
<?php foreach ($transactions as $t): ?>
<tr>
<td><?= date('d M Y, h:i A', strtotime($t['created_at'])) ?></td>
<td><?= htmlspecialchars($t['account_number']) ?></td>
<td><?= ucfirst($t['type']) ?></td>
<td><?= number_format($t['amount'], 2) ?></td>
<td><?= number_format($t['interest_accumulated'], 2) ?></td>
<td><?= htmlspecialchars($t['currency']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</main>
</div>
</body>
</html>
b IDATxytVսϓ22 A@IR:hCiZ[v*E:WũZA ^dQeQ @ !jZ'>gsV仿$|?g)&x-E