-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalance_sheet.php
More file actions
461 lines (425 loc) · 24.8 KB
/
Copy pathbalance_sheet.php
File metadata and controls
461 lines (425 loc) · 24.8 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
// report_balance_sheet.php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_login();
try {
$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$user_id = $_SESSION['user_id'];
// ── Filters ───────────────────────────────────────────────────────────────
$as_at_date = $_GET['date'] ?? date('Y-m-d');
function getBalances($pdo, $type, $date, $user_id)
{
if ($type === 'liability') {
$bt_debit_sign = -1;
$bt_credit_sign = 1;
$je_debit_sign = 1;
$je_credit_sign = -1;
} else {
$bt_debit_sign = 1;
$bt_credit_sign = 1;
$je_debit_sign = 1;
$je_credit_sign = 1;
}
$stmt = $pdo->prepare("
SELECT
c.id,
c.code,
c.name,
COALESCE(SUM(combined.signed_amount), 0) AS balance
FROM chart_of_accounts c
LEFT JOIN (
SELECT
bt.coa_id,
CASE
WHEN bt.type = 'debit' THEN bt.amount * {$bt_debit_sign}
WHEN bt.type = 'credit' THEN bt.amount * {$bt_credit_sign}
END AS signed_amount
FROM bank_transactions bt
WHERE bt.user_id = :user_id
AND bt.transaction_date <= :date_bt
UNION ALL
SELECT
jel.coa_id,
CASE
WHEN jel.type = 'debit' THEN jel.amount * {$je_debit_sign}
WHEN jel.type = 'credit' THEN jel.amount * {$je_credit_sign}
END AS signed_amount
FROM journal_entry_lines jel
JOIN journal_entries je ON jel.journal_entry_id = je.id
WHERE je.date <= :date_je
) AS combined ON combined.coa_id = c.id
WHERE c.type = :type
AND c.is_active = 1
GROUP BY c.id, c.code, c.name
ORDER BY c.code
");
$stmt->execute([
':user_id' => $user_id,
':date_bt' => $date,
':date_je' => $date,
':type' => $type,
]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$assets = getBalances($pdo, 'asset', $as_at_date, $user_id);
$liabilities = getBalances($pdo, 'liability', $as_at_date, $user_id);
$equity = getBalances($pdo, 'equity', $as_at_date, $user_id);
// Fix P&L — income credits positive, expense debits negative
$pl_stmt = $pdo->prepare("
SELECT COALESCE(SUM(net_amount), 0) AS net_profit
FROM (
SELECT
CASE
WHEN c.type = 'income' AND bt.type = 'credit' THEN bt.amount
WHEN c.type = 'income' AND bt.type = 'debit' THEN -bt.amount
WHEN c.type = 'expense' AND bt.type = 'debit' THEN -bt.amount
WHEN c.type = 'expense' AND bt.type = 'credit' THEN bt.amount
ELSE 0
END AS net_amount
FROM bank_transactions bt
JOIN chart_of_accounts c ON c.id = bt.coa_id
WHERE bt.user_id = :user_id
AND bt.transaction_date <= :date_bt
AND c.type IN ('income', 'expense')
UNION ALL
SELECT
CASE
WHEN c.type = 'income' AND jel.type = 'credit' THEN jel.amount
WHEN c.type = 'income' AND jel.type = 'debit' THEN -jel.amount
WHEN c.type = 'expense' AND jel.type = 'debit' THEN -jel.amount
WHEN c.type = 'expense' AND jel.type = 'credit' THEN jel.amount
ELSE 0
END AS net_amount
FROM journal_entry_lines jel
JOIN journal_entries je ON jel.journal_entry_id = je.id
JOIN chart_of_accounts c ON c.id = jel.coa_id
WHERE je.date <= :date_je
AND c.type IN ('income', 'expense')
) AS pl_combined
");
$pl_stmt->execute([
':user_id' => $user_id,
':date_bt' => $as_at_date,
':date_je' => $as_at_date,
]);
$current_period_profit = $pl_stmt->fetchColumn();
// Fix totals
$total_assets = array_sum(array_column($assets, 'balance'));
$total_liabilities = array_sum(array_column($liabilities, 'balance'));
$total_equity = array_sum(array_column($equity, 'balance')) + $current_period_profit;
$net_assets = $total_assets - $total_liabilities;
$difference = abs($net_assets - $total_equity);
$is_balanced = $difference < 0.01;
} catch (PDOException $e) {
$_SESSION['error'] = "Database error: " . $e->getMessage();
$assets = $liabilities = $equity = [];
$total_assets = $total_liabilities = $total_equity = $net_assets = $current_period_profit = 0;
$is_balanced = false;
}
$page_title = 'Balance Sheet';
require_once 'includes/header.php';
require_once 'includes/topbar.php';
require_once 'includes/sidebar.php';
?>
<div class="page-wrapper">
<div class="page-content">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box d-md-flex justify-content-md-between align-items-center">
<h4 class="page-title">Balance Sheet</h4>
<div class="">
<ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a>
</li><!--end nav-item-->
<li class="breadcrumb-item active">Balance Sheet</li>
</ol>
</div>
</div><!--end page-title-box-->
<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<span class="text-muted small">
As at <?php echo date('d F Y', strtotime($as_at_date)); ?>
</span>
</div>
<button class="btn btn-outline-secondary" onclick="window.print()">
<i class="bi bi-printer"></i> Print / PDF
</button>
</div>
<!-- Alerts -->
<?php if (isset($_SESSION['message'])): ?>
<div class="alert alert-success">
<?php echo $_SESSION['message'];
unset($_SESSION['message']); ?>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-danger">
<?php echo $_SESSION['error'];
unset($_SESSION['error']); ?>
</div>
<?php endif; ?>
<!-- Summary Cards -->
<div class="row g-3 mb-4">
<div class="col-sm-6 col-xl-3">
<div class="card border-0 shadow-sm h-100">
<div class="card-body">
<div class="text-muted small">Total Assets</div>
<div class="fs-4 fw-bold text-success">
£<?php echo number_format($total_assets, 2); ?>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-xl-3">
<div class="card border-0 shadow-sm h-100">
<div class="card-body">
<div class="text-muted small">Total Liabilities</div>
<div class="fs-4 fw-bold text-danger">
£<?php echo number_format($total_liabilities, 2); ?>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-xl-3">
<div class="card border-0 shadow-sm h-100">
<div class="card-body">
<div class="text-muted small">Net Assets</div>
<div class="fs-4 fw-bold text-primary">
£<?php echo number_format($net_assets, 2); ?>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-xl-3">
<div class="card border-0 shadow-sm h-100">
<div class="card-body">
<div class="text-muted small">Balanced?</div>
<div class="fs-4 fw-bold <?php echo $is_balanced ? 'text-success' : 'text-danger'; ?>">
<?php echo $is_balanced ? '✅ Yes' : '❌ No'; ?>
</div>
<?php if (!$is_balanced): ?>
<div class="text-muted small mt-1">
Difference: £<?php echo number_format($difference, 2); ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Date Filter -->
<div class="card shadow-sm mb-4">
<div class="card-body">
<form method="get" class="row g-2 align-items-end">
<div class="col-md-3">
<label class="form-label small">As at Date</label>
<input type="date" name="date" class="form-control form-control-sm"
value="<?php echo htmlspecialchars($as_at_date); ?>">
</div>
<div class="col-md-3 d-flex gap-2">
<button type="submit" class="btn btn-primary btn-sm">
<i class="bi bi-filter"></i> Update
</button>
<a href="report_balance_sheet.php" class="btn btn-outline-secondary btn-sm">
<i class="bi bi-x"></i> Clear
</a>
</div>
</form>
</div>
</div>
<!-- Assets & Liabilities Row -->
<div class="row g-4">
<!-- ASSETS -->
<div class="col-lg-6">
<div class="card shadow-sm h-100">
<div class="card-header bg-success text-white">
<strong><i class="bi bi-graph-up"></i> Assets</strong>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover table-sm mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Code</th>
<th>Account</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>
<?php foreach ($assets as $row): ?>
<tr>
<td class="text-muted small"><?php echo $row['code']; ?></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td class="text-end text-nowrap fw-semibold
<?php echo $row['balance'] >= 0 ? 'text-success' : 'text-danger'; ?>">
<?php if ($row['balance'] < 0): ?>
(£<?php echo number_format(abs($row['balance']), 2); ?>)
<?php else: ?>
£<?php echo number_format($row['balance'], 2); ?>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot class="table-success fw-bold">
<tr>
<td colspan="2">Total Assets</td>
<td class="text-end text-nowrap">
£<?php echo number_format($total_assets, 2); ?>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<!-- LIABILITIES & EQUITY -->
<div class="col-lg-6 d-flex flex-column gap-4">
<!-- Liabilities -->
<div class="card shadow-sm">
<div class="card-header bg-danger text-white">
<strong><i class="bi bi-graph-down"></i> Liabilities</strong>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover table-sm mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Code</th>
<th>Account</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>
<?php foreach ($liabilities as $row): ?>
<tr>
<td class="text-muted small"><?php echo $row['code']; ?></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td class="text-end text-nowrap fw-semibold
<?php echo $row['balance'] > 0 ? 'text-danger' : 'text-muted'; ?>">
<?php if ($row['balance'] < 0): ?>
(£<?php echo number_format(abs($row['balance']), 2); ?>)
<?php else: ?>
£<?php echo number_format($row['balance'], 2); ?>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot class="table-danger fw-bold">
<tr>
<td colspan="2">Total Liabilities</td>
<td class="text-end text-nowrap">
£<?php echo number_format($total_liabilities, 2); ?>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<!-- Equity -->
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<strong><i class="bi bi-bank"></i> Equity</strong>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover table-sm mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Code</th>
<th>Account</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>
<?php foreach ($equity as $row): ?>
<tr>
<td class="text-muted small"><?php echo $row['code']; ?></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td class="text-end text-nowrap fw-semibold text-primary">
£<?php echo number_format($row['balance'], 2); ?>
</td>
</tr>
<?php endforeach; ?>
<!-- Current Period Profit -->
<tr>
<td class="text-muted small">—</td>
<td>
Current Period Profit
<span class="text-muted small">(P&L)</span>
</td>
<td class="text-end text-nowrap fw-semibold
<?php echo $current_period_profit >= 0 ? 'text-success' : 'text-danger'; ?>">
<?php if ($current_period_profit < 0): ?>
(£<?php echo number_format(abs($current_period_profit), 2); ?>)
<?php else: ?>
£<?php echo number_format($current_period_profit, 2); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
<tfoot class="table-primary fw-bold">
<tr>
<td colspan="2">Total Equity</td>
<td class="text-end text-nowrap">
£<?php echo number_format($total_equity, 2); ?>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Balance Check Footer -->
<div class="card shadow-sm mt-4">
<div class="card-body">
<div class="row text-center">
<div class="col-md-4 border-end">
<div class="text-muted small">Net Assets</div>
<div class="fs-4 fw-bold text-primary">
£<?php echo number_format($net_assets, 2); ?>
</div>
<div class="text-muted small">Assets − Liabilities</div>
</div>
<div class="col-md-4 border-end">
<div class="text-muted small">Total Equity</div>
<div class="fs-4 fw-bold text-primary">
£<?php echo number_format($total_equity, 2); ?>
</div>
<div class="text-muted small">Capital + Retained + P&L</div>
</div>
<div class="col-md-4">
<div class="text-muted small">Status</div>
<div class="fs-4 fw-bold <?php echo $is_balanced ? 'text-success' : 'text-danger'; ?>">
<?php if ($is_balanced): ?>
✅ Balanced
<?php else: ?>
❌ Out by £<?php echo number_format($difference, 2); ?>
<?php endif; ?>
</div>
<div class="text-muted small">Net Assets should equal Equity</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<style>
@media print {
nav, .btn, form { display: none !important; }
.container { margin: 0 !important; }
.card { border: none !important; box-shadow: none !important; }
}
</style>
<?php require_once 'includes/footer.php'; ?>