Redo Balance Sheet Report

This commit is contained in:
Andrew Malsbury 2023-11-21 16:04:41 +00:00
parent dcd5103819
commit 230e649e2c
2 changed files with 121 additions and 146 deletions

View File

@ -833,4 +833,39 @@ function getTotalTax($tax_name, $year, $mysqli) {
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_assoc($result);
return $row['total_tax'] ?? 0;
}
//Get account currency code
function getAccountCurrencyCode($mysqli, $account_id) {
$sql = mysqli_query($mysqli, "SELECT account_currency_code FROM accounts WHERE account_id = $account_id");
$row = mysqli_fetch_array($sql);
$account_currency_code = nullable_htmlentities($row['account_currency_code']);
return $account_currency_code;
}
function calculateAccountBalance($mysqli, $account_id) {
$sql_account = mysqli_query($mysqli, "SELECT * FROM accounts LEFT JOIN account_types ON accounts.account_type = account_types.account_type_id WHERE account_archived_at IS NULL AND account_id = $account_id ORDER BY account_name ASC; ");
$row = mysqli_fetch_array($sql_account);
$opening_balance = floatval($row['opening_balance']);
$account_id = $row['account_id'];
$sql_payments = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS total_payments FROM payments WHERE payment_account_id = $account_id");
$row = mysqli_fetch_array($sql_payments);
$total_payments = floatval($row['total_payments']);
$sql_revenues = mysqli_query($mysqli, "SELECT SUM(revenue_amount) AS total_revenues FROM revenues WHERE revenue_account_id = $account_id");
$row = mysqli_fetch_array($sql_revenues);
$total_revenues = floatval($row['total_revenues']);
$sql_expenses = mysqli_query($mysqli, "SELECT SUM(expense_amount) AS total_expenses FROM expenses WHERE expense_account_id = $account_id");
$row = mysqli_fetch_array($sql_expenses);
$total_expenses = floatval($row['total_expenses']);
$balance = $opening_balance + $total_payments + $total_revenues - $total_expenses;
if ($balance == '') {
$balance = '0.00';
}
return $balance;
}

View File

@ -1,33 +1,58 @@
<?php
require_once "inc_all_reports.php";
require_once "inc_all_reports.php";
validateAccountantRole();
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);
// Fetch Accounts and their balances
$sql_accounts = "
SELECT
a.account_id,
a.account_name,
a.opening_balance,
a.account_currency_code,
a.account_notes,
a.account_type,
COALESCE(SUM(p.payment_amount), 0) AS total_payments,
COALESCE(SUM(CASE WHEN e.expense_vendor_id <> 0 THEN e.expense_amount ELSE 0 END), 0) AS total_expenses,
COALESCE(SUM(CASE WHEN r.revenue_category_id <> 0 THEN r.revenue_amount ELSE 0 END), 0) AS total_revenues
FROM accounts a
LEFT JOIN payments p ON a.account_id = p.payment_account_id
LEFT JOIN revenues r ON a.account_id = r.revenue_account_id
LEFT JOIN expenses e ON a.account_id = e.expense_account_id
GROUP BY a.account_id
ORDER BY a.account_type, a.account_name ASC";
$accounts = [];
$total_assets = 0;
$total_liabilities = 0;
$total_equity = 0;
$currency_code = '';
$result_accounts = mysqli_query($mysqli, $sql_accounts);
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>";
}
}
$total_assets = 0;
$total_liabilities = 0;
$total_equity = 0;
$currency = $session_company_currency;
?>
<div class="card card-dark">
@ -40,131 +65,46 @@
<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 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">
<thead class="text-dark">
<tr>
<th>Account Type</th>
<th>Account Name</th>
<th class="text-right">Account Balance</th>
</tr>
</thead>
<tbody>
<!-- Assets Section -->
<tr>
<th colspan="3" >Assets</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result_accounts)) {
$account_type = $row['account_type'];
if ($account_type >= 11 && $account_type <= 19) {
$balance = $row['opening_balance'] + $row['total_payments'] + $row['total_revenues'] - $row['total_expenses'];
print_row($row, $balance, $currency_format);
$total_assets += $balance;
$formatted_total_assets = numfmt_format_currency($currency_format, $total_assets, $row['account_currency_code']);
}
}
?>
<tr>
<th></th>
<th class="text-uppercase">Total Assets</th>
<th class="text-right"><?php echo $formatted_total_assets; ?></th>
</tr>
<!-- 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 -->
<tr>
<th colspan="3" >Liabilities</th>
</tr>
<?php
mysqli_data_seek($result_accounts, 0); // Reset the result pointer to the start
while ($row = mysqli_fetch_array($result_accounts)) {
$balance = $row['opening_balance'] + $row['total_payments'] + $row['total_revenues'] - $row['total_expenses'];
$account_type = $row['account_type'];
if ($account_type >= 21 && $account_type <= 29) {
$balance = $row['opening_balance'] + $row['total_payments'] + $row['total_revenues'] - $row['total_expenses'];
print_row($row, $balance, $currency_format);
$total_liabilities += $balance;
$formatted_total_liabilities = numfmt_format_currency($currency_format, $total_liabilities, $row['account_currency_code']);
}
}
?>
<tr>
<th></th>
<th class="text-uppercase">Total Liabilities</th>
<th class="text-right"><?php echo $formatted_total_liabilities; ?></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>
<!-- Equity Section -->
<tr>
<th colspan="3" >Equity</th>
</tr>
<?php
mysqli_data_seek($result_accounts, 0); // Reset the result pointer to the start
while ($row = mysqli_fetch_array($result_accounts)) {
$balance = $row['opening_balance'] + $row['total_payments'] + $row['total_revenues'] - $row['total_expenses'];
$account_type = $row['account_type'];
if ($account_type >= 30) {
$balance = $row['opening_balance'] + $row['total_payments'] + $row['total_revenues'] - $row['total_expenses'];
print_row($row, $balance, $currency_format);
$total_equity += $balance;
$formatted_total_equity = numfmt_format_currency($currency_format, $total_equity, $row['account_currency_code']);
}
}
?>
<tr>
<th></th>
<th class="text-uppercase">Total Equity</th>
<th class="text-right"><?php echo $formatted_total_equity; ?></th>
</tr>
<!-- Total Equity and Liabilities -->
<!-- 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>
<?php
$total_liabilities_and_equity = $total_liabilities + $total_equity;
$formatted_total_liabilities_and_equity = numfmt_format_currency($currency_format, $total_liabilities_and_equity, $currency);
?>
<tr>
<th></th>
<th class="text-uppercase">Total Liabilities and Equity</th>
<th class="text-right"><?php echo $formatted_total_liabilities_and_equity; ?></th>
</tr>
<tr>
<th>
Unbalanced:
<div><?php
$unbalanced = $total_assets + $total_liabilities_and_equity;
echo numfmt_format_currency($currency_format, $unbalanced, $currency);
?>
</div>
</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php require_once "footer.php";
function print_row($row, $balance, $currency_format) {
$account_name = nullable_htmlentities($row['account_name']);
$formatted_balance = numfmt_format_currency($currency_format, $balance, $row['account_currency_code']);
echo "<tr>";
echo "<td></td>";
echo "<td>$account_name</td>";
echo "<td class='text-right'>$formatted_balance</td>";
echo "</tr>";
}
?>
<?php require_once "footer.php"; ?>