The following files exists in this folder. Click to view.
index.php235 lines UTF-8 Unix (LF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
<?php
session_start();
$CURRENCY = "kr";
if (!isset($_SESSION['transactions'])) {
$_SESSION['transactions'] = [];
// Startbelopp 1000 kr
$_SESSION['transactions'][] = [
'time' => time(),
'type' => 'insättning',
'amount' => 1000,
'comment' => 'Startbelopp'
];
}
$transactions = &$_SESSION['transactions'];
function calculate_balance(array $transactions): int {
$balance = 0;
foreach ($transactions as $t) {
if (isset($t['type']) && isset($t['amount'])) {
$amt = (int)$t['amount'];
if ($t['type'] === 'insättning') {
$balance += $amt;
} else {
$balance -= $amt;
}
}
}
return $balance;
}
$errors = [];
$messages = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
// det måste finnas ett tal och det får inte vara negativt
$rawAmount = $_POST['amount'] ?? '';
$comment = trim((string)($_POST['comment'] ?? ''));
if ($rawAmount === '' || !is_numeric($rawAmount)) {
$errors[] = "Ange ett giltigt belopp i hela kronor.";
} else {
// kontrollerar hela kronor
$amount = (int)round(floatval($rawAmount));
if ($amount < 0) {
$errors[] = "Beloppet måste vara 0 eller högre.";
}
}
if (empty($errors)) {
$balanceNow = calculate_balance($transactions);
if ($action === 'deposit') {
// Insättning: lägg till transaktion
$transactions[] = [
'time' => time(),
'type' => 'insättning',
'amount' => $amount,
'comment' => $comment === '' ? 'Insättning via formulär' : $comment
];
$messages[] = "Insättning på " . number_format($amount, 0, ',', ' ') . " $CURRENCY genomförd.";
} elseif ($action === 'withdraw') {
// Uttag kontrollera så saldo inte blir negativt
if ($balanceNow - $amount < 0) {
$errors[] = "Uttaget nekades saldot skulle bli negativt.";
} else {
$transactions[] = [
'time' => time(),
'type' => 'uttag',
'amount' => $amount,
'comment' => $comment === '' ? 'Uttag via formulär' : $comment
];
$messages[] = "Uttag på " . number_format($amount, 0, ',', ' ') . " $CURRENCY genomfört.";
}
} else {
$errors[] = "Okänd åtgärd.";
}
}
}
$balance = calculate_balance($transactions);
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>P01 Bank</title>
<link rel="stylesheet" href="bulma.css">
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title has-text-centered">P01 Banken</h1>
<div class="box" style="display: flex; justify-content: space-between; align-items: center;">
<h2 class="title is-4 m-0">
Saldo: <?= number_format($balance, 0, ',', ' ') ?> <?= htmlspecialchars($CURRENCY) ?>
</h2>
<a href="logout.php" class="button is-danger">Återsäll sidan</a>
</div>
<?php if (!empty($errors)): ?>
<div class="notification is-danger">
<button class="delete" onclick="this.parentElement.style.display='none'"></button>
<ul>
<?php foreach ($errors as $e): ?>
<li><?= htmlspecialchars($e) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if (!empty($messages)): ?>
<div class="notification is-success">
<button class="delete" onclick="this.parentElement.style.display='none'"></button>
<ul>
<?php foreach ($messages as $m): ?>
<li><?= htmlspecialchars($m) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if (isset($_GET['logout'])): ?>
<div class="notification is-success">
<button class="delete" onclick="this.parentElement.style.display='none'"></button>
Sidan har återställts.
</div>
<?php endif; ?>
<div class="columns">
<div class="column is-half">
<div class="box">
<h3 class="title is-5 has-text-centered">Insättning</h3>
<form method="post">
<div class="field">
<label class="label">Belopp</label>
<div class="control">
<input type="number" class="input" name="amount" min="0" step="1" required placeholder="ange hur mycket du vill sätta in...">
</div>
</div>
<div class="field">
<label class="label">Kommentar</label>
<div class="control">
<input type="text" class="input" name="comment" maxlength="255" placeholder="Valfritt">
</div>
</div>
<input type="hidden" name="action" value="deposit">
<div class="control">
<button class="button is-primary" type="submit">Sätt in</button>
</div>
</form>
</div>
</div>
<div class="column is-half">
<div class="box">
<h3 class="title is-5 has-text-centered">Uttag</h3>
<form method="post">
<div class="field">
<label class="label">Belopp</label>
<div class="control">
<input type="number" class="input" name="amount" min="0" step="1" required placeholder="ange hur mycket du vill ta ut...">
</div>
</div>
<div class="field">
<label class="label">Kommentar</label>
<div class="control">
<input type="text" class="input" name="comment" maxlength="255" placeholder="valfritt">
</div>
</div>
<input type="hidden" name="action" value="withdraw">
<div class="control">
<button class="button is-danger" type="submit">Ta ut</button>
</div>
</form>
</div>
</div>
</div>
<div class="box">
<h3 class="title is-5 has-text-centered">Transaktioner</h3>
<?php if (empty($transactions)): ?>
<p>Inga transaktioner</p>
<?php else: ?>
<table class="table is-fullwidth is-striped">
<thead>
<tr>
<th>Tid</th>
<th>Typ</th>
<th>Belopp</th>
<th>Kommentar</th>
</tr>
</thead>
<tbody>
<?php
// Visa senaste först
$rev = array_reverse($transactions);
foreach ($rev as $t):
$timeStr = date('Y-m-d H:i:s', $t['time']);
$type = htmlspecialchars($t['type']);
$amountStr = number_format((int)$t['amount'], 0, ',', ' ') . ' ' . htmlspecialchars($CURRENCY);
$comment = htmlspecialchars($t['comment'] ?? '');
?>
<tr>
<td><?= $timeStr ?></td>
<td><?= $type ?></td>
<td><?= $amountStr ?></td>
<td><?= $comment ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</section>
</body>
</html>