View sourcecode

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

index.php

235 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?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($amount0','' ') . $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($amount0','' ') . $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($balance0','' '?> <?= 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>