The following files exists in this folder. Click to view.
bank.php174 lines UTF-8 Unix (LF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
<?php
$txFile = __DIR__ . "/transactions.txt";
function createAccountIfMissing(string $file): void {
if (!file_exists($file)) {
// Start-event: +1000
file_put_contents($file, "1000;" . time() . PHP_EOL);
}
}
function readTransactions(string $file): array {
if (!file_exists($file)) return [];
$rows = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$events = [];
foreach ($rows as $row) {
$parts = explode(";", trim($row));
if (count($parts) < 2) continue;
$events[] = [
"amount" => (int)$parts[0],
"time" => (int)$parts[1],
];
}
usort($events, fn($a, $b) => $a["time"] <=> $b["time"]);
return $events;
}
function saldo(array $events): int {
$sum = 0;
foreach ($events as $e) $sum += (int)$e["amount"];
return $sum;
}
function saveEvent(string $file, int $amount): void {
file_put_contents($file, $amount . ";" . time() . PHP_EOL, FILE_APPEND);
}
function formatAmount(int $amount): string {
$sign = $amount >= 0 ? "+" : "";
return $sign . $amount . " kr";
}
function amountColor(int $amount): string {
return $amount >= 0 ? "green" : "red";
}
function go(string $msg = ""): void {
$q = $msg !== "" ? "?mess=" . urlencode($msg) : "";
header("Location: bank.php{$q}");
exit;
}
createAccountIfMissing($txFile);
$mess = $_GET["mess"] ?? "";
$events = readTransactions($txFile);
$currentSaldo = saldo($events);
if (($_SERVER["REQUEST_METHOD"] ?? "") === "POST") {
$typ = $_POST["typ"] ?? "ins";
$amount = (int)($_POST["amount"] ?? 0);
if ($amount < 1) {
go("Belopp måste vara minst 1 kr.");
}
$eventsNow = readTransactions($txFile);
$saldoNow = saldo($eventsNow);
if ($typ === "utt") {
if ($saldoNow - $amount < 0) {
go("Uttag nekas. Saldot får inte bli negativt.");
}
saveEvent($txFile, -$amount);
go("Uttag genomfört.");
} else {
saveEvent($txFile, $amount);
go("Insättning genomförd.");
}
}
$events = readTransactions($txFile);
$currentSaldo = saldo($events);
?>
<!doctype html>
<html lang="sv">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Yamoos Bank</title>
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
</head>
<body>
<div class="container">
<div class="columns">
<div class="column col-10 col-mx-auto">
<h1>Yamoos Bank</h1>
<?php if ($mess): ?>
<p class="text-error"><?= htmlspecialchars($mess) ?></p>
<?php endif; ?>
<p><strong>Saldo:</strong> <?= $currentSaldo ?> kr</p>
<form method="post">
<div class="form-group">
<label class="form-label">Belopp (kr)</label>
<input class="form-input" type="number" name="amount" min="1" step="1" required>
<label class="form-radio">
<input type="radio" name="typ" value="ins" checked>
<i class="form-icon"></i> Insättning
</label>
<label class="form-radio">
<input type="radio" name="typ" value="utt">
<i class="form-icon"></i> Uttag
</label>
<button class="btn btn-primary" id="actionButton" type="submit">Sätt in</button>
</div>
</form>
<hr>
<h2>Transaktioner</h2>
<table class="table">
<thead>
<tr>
<th>Nr</th>
<th>Belopp</th>
<th>Datum</th>
<th>Saldo efter</th>
</tr>
</thead>
<tbody>
<?php $running = 0; $nr = 1; ?>
<?php foreach ($events as $e): ?>
<?php $running += (int)$e["amount"]; ?>
<tr>
<td><?= $nr++ ?></td>
<td style="color:<?= amountColor((int)$e["amount"]) ?>">
<?= htmlspecialchars(formatAmount((int)$e["amount"])) ?>
</td>
<td><?= htmlspecialchars(date("Y-m-d H:i", (int)$e["time"])) ?></td>
<td><?= $running ?> kr</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
const radios = document.querySelectorAll('input[name="typ"]');
const btn = document.getElementById('actionButton');
radios.forEach(r => {
r.addEventListener('change', () => {
btn.textContent = (r.value === 'ins') ? "Sätt in" : "Ta ut";
});
});
</script>
</body>
</html>