View sourcecode

The following files exists in this folder. Click to view.

bank.php

174 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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($fileFILE_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 $fileint $amount): void {
  
file_put_contents($file$amount ";" time() . PHP_EOLFILE_APPEND);
}

function 
formatAmount(int $amount): string {
  
$sign $amount >= "+" "";
  return 
$sign $amount " kr";
}

function 
amountColor(int $amount): string {
  return 
$amount >= "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>