forked from itflow-org/itflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_balance_sheet.php
110 lines (96 loc) · 3.85 KB
/
report_balance_sheet.php
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
<?php
require_once "inc_all_reports.php";
validateAccountantRole();
// Fetch accounts data
$sql = "SELECT accounts.*, account_types.account_type_parent
FROM accounts
LEFT JOIN account_types ON accounts.account_type = account_types.account_type_id
WHERE account_archived_at IS NULL
ORDER BY account_name ASC;";
$result = mysqli_query($mysqli, $sql);
$accounts = [];
$total_assets = 0;
$total_liabilities = 0;
$total_equity = 0;
$currency_code = '';
while ($row = mysqli_fetch_assoc($result)) {
$account_id = $row['account_id'];
// Fetch and calculate balances
$balance = calculateAccountBalance($mysqli, $account_id);
// Categorize account based on type
if ($row['account_type_parent'] == 1) {
$total_assets += $balance;
} elseif ($row['account_type_parent'] == 2) {
$total_liabilities += $balance;
} elseif ($row['account_type_parent'] == 3) {
$total_equities += $balance;
}
// Add account to array
$accounts[$row['account_type_parent']][] = [
'id' => $account_id,
'name' => $row['account_name'],
'type' => $row['account_type_name'],
'balance' => $balance
];
}
function display_account_section($mysqli, $accounts, $type) {
foreach ($accounts[$type] as $account) {
global $currency_format;
global $currency_code;
$currency_code = getAccountCurrencyCode($mysqli, $account['id']);
echo "<tr>";
echo "<td>{$account['type']}</td>";
echo "<td>{$account['name']}</td>";
echo "<td class='text-right'>" . numfmt_format_currency($currency_format, $account['balance'], $currency_code) . "</td>";
echo "</tr>";
}
}
?>
<div class="card card-dark">
<div class="card-header py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-balance-scale mr-2"></i>Balance Sheet</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary d-print-none" onclick="window.print();"><i class="fas fa-fw fa-print mr-2"></i>Print</button>
</div>
</div>
<div class="card-body p-0">
<div class="table-responsive-sm">
<div class="text-center">
<h2 class="text-dark">
<?php echo nullable_htmlentities($session_company_name); ?>
</h2>
<h3 class="text-dark">Balance Sheet</h3>
<h5 class="text-dark">As of <?php echo date("F j, Y"); ?></h5>
</div>
<div>
<table class="table table-sm">
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<!-- Assets Section -->
<?php display_account_section($mysqli, $accounts, 1); ?>
<tr>
<th></th>
<th class="text-uppercase">Total Assets</th>
<th class="text-right"><?= numfmt_format_currency($currency_format, $total_assets, $currency_code); ?></th>
</tr>
<!-- Liabilities Section -->
<?php display_account_section($mysqli, $accounts, 2); ?>
<tr>
<th></th>
<th class="text-uppercase">Total Liabilities</th>
<th class="text-right"><?= numfmt_format_currency($currency_format, $total_liabilities, $currency_code); ?></th>
</tr>
<!-- Equities Section -->
<?php display_account_section($mysqli, $accounts, 3); ?>
<tr>
<th></th>
<th class="text-uppercase">Total Equities</th>
<th class="text-right"><?= numfmt_format_currency($currency_format, $total_equities, $currency_code); ?></th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<?php require_once "footer.php"; ?>