Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add balance sheet report #734

Merged
merged 34 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9551399
Update add and edit to accomodate type
psi-iota Oct 7, 2023
ccb67d8
Add type dropdown, and update opening balance star
psi-iota Oct 7, 2023
db5eee0
Add type dropdown
psi-iota Oct 7, 2023
bc95bb4
Update Accounts screen to show type
psi-iota Oct 7, 2023
29e2e5e
Update DB, and added comments for next new guy.
psi-iota Oct 7, 2023
844a85c
Update DB
psi-iota Oct 7, 2023
f8bc2ee
Add "account_type" to accounts table
psi-iota Oct 7, 2023
5d964c9
New Balance sheet report screen
psi-iota Oct 7, 2023
3a87908
Add balance sheet to side nav
psi-iota Oct 7, 2023
d63da07
Updated setup - Cash Account: current asset type
psi-iota Oct 7, 2023
3e96751
Bug - Liabilities is showing Assets total
psi-iota Oct 7, 2023
6a485a9
Allows for negative numbers for credit cards
psi-iota Oct 7, 2023
1455e20
Added Total Equities and Liabilities
psi-iota Oct 7, 2023
2ee87f0
Switched to DB controlled Account Types
psi-iota Oct 9, 2023
f190e10
Switched to DB controlled Account Types
psi-iota Oct 9, 2023
8b6c909
Switched to DB controlled Account Types
psi-iota Oct 9, 2023
749281a
Switched to DB controlled Account Types
psi-iota Oct 9, 2023
81f34e2
Updated Version
psi-iota Oct 9, 2023
03c8f47
added account types table
psi-iota Oct 9, 2023
69272a8
added account type post handling
psi-iota Oct 9, 2023
a8e03d4
Switched to DB controlled Account Types
psi-iota Oct 9, 2023
3ef045e
New Modal
psi-iota Oct 9, 2023
72da87e
New Settings Screen
psi-iota Oct 9, 2023
e4fc7bc
New settings menu
psi-iota Oct 9, 2023
31043da
add more default account types
psi-iota Oct 9, 2023
aef4ec0
Post handling for add and edit
psi-iota Oct 9, 2023
d5e03f1
fixed what @wrongecho suggested.
psi-iota Oct 9, 2023
9c099f7
Account Types bug
psi-iota Oct 9, 2023
bb559a4
Fixed account type not showing by default on edit
psi-iota Oct 9, 2023
4fbf448
Updated balance sheet to use DB types
psi-iota Oct 9, 2023
96e493d
Removed a testcase
psi-iota Oct 9, 2023
7b772cf
Something is broken with the confirm-link
psi-iota Oct 9, 2023
22a072f
Merge branch 'itflow-org:master' into balance-sheet
o-psi Oct 10, 2023
74e2866
Adjusted the type dropdown
psi-iota Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion account_add_modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@
</div>

<div class="form-group">
<label>Opening Balance</label>
<label>Account Type <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-list"></i></span>
</div>
<select class="form-control select2" name="type" required>
<option value="">- Select -</option>
<option value="11">Current Assets</option>
<option value="12">Fixed Assets</option>
<option value="13">Other Assets</option>
<option value="21">Current Liabilities</option>
<option value="22">Long Term Liabilities</option>
<option value="23">Other Liabilities</option>
<option value="30">Equity</option>
</select>
</div>
</div>

<div class="form-group">
<label>Opening Balance <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-dollar-sign"></i></span>
Expand Down
19 changes: 19 additions & 0 deletions account_edit_modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
</div>
</div>

<div class="form-group">
<label>Account Type <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-list"></i></span>
</div>
<select class="form-control select" name="type" required>
o-psi marked this conversation as resolved.
Show resolved Hide resolved
<option value="">- Select -</option>
<option value="11" <?php if ($account_type == 'Current Assets') echo 'selected'; ?>>Current Assets</option>
<option value="12" <?php if ($account_type == 'Fixed Assets') echo 'selected'; ?>>Fixed Assets</option>
<option value="13" <?php if ($account_type == 'Other Assets') echo 'selected'; ?>>Other Assets</option>
<option value="21" <?php if ($account_type == 'Current Liabilities') echo 'selected'; ?>>Current Liabilities</option>
<option value="22" <?php if ($account_type == 'Long Term Liabilities') echo 'selected'; ?>>Long Term Liabilities</option>
<option value="23" <?php if ($account_type == 'Other Liabilities') echo 'selected'; ?>>Other Liabilities</option>
<option value="30" <?php if ($account_type == 'Equity') echo 'selected'; ?>>Equity</option>
</select>
</div>
</div>

<div class="form-group">
<label>Notes</label>
<textarea class="form-control" rows="5" placeholder="Enter some notes" name="notes"><?php echo $account_notes; ?></textarea>
Expand Down
31 changes: 31 additions & 0 deletions accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<thead class="text-dark <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
<tr>
<th><a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=account_name&order=<?php echo $disp; ?>">Name</a></th>
<th class="text-center">Type</th>
<th><a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=account_currency_code&order=<?php echo $disp; ?>">Currency</a></th>
<th class="text-right">Balance</th>
<th class="text-center">Action</th>
Expand All @@ -57,6 +58,7 @@
$opening_balance = floatval($row['opening_balance']);
$account_currency_code = nullable_htmlentities($row['account_currency_code']);
$account_notes = nullable_htmlentities($row['account_notes']);
$account_type = intval($row['account_type']);

$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);
Expand All @@ -75,6 +77,35 @@

<tr>
<td><a class="text-dark" href="#" data-toggle="modal" data-target="#editAccountModal<?php echo $account_id; ?>"><?php echo $account_name; ?></a></td>
<td class="text-center">
<?php
switch ($account_type) {
case 11:
echo "Current Assets";
break;
case 12:
echo "Fixed Assets";
break;
case 13:
echo "Other Assets";
break;
case 21:
echo "Current Liabilities";
break;
case 22:
echo "Long Term Liabilities";
break;
case 23:
echo "Other Liabilities";
break;
case 30:
echo "Equity";
break;
default:
echo "Unknown Account Type";
}
?>
</td>
<td><?php echo $account_currency_code; ?></td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $balance, $account_currency_code); ?></td>
<td>
Expand Down
18 changes: 16 additions & 2 deletions database_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,12 +1374,26 @@
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.8.6'");
}

//if (CURRENT_DATABASE_VERSION == '0.8.6') {
// Update DB to 0.8.7
if (CURRENT_DATABASE_VERSION == '0.8.6') {
// Insert queries here required to update to DB version 0.8.7
mysqli_query($mysqli, "ALTER TABLE `accounts` ADD `account_type` int(6) DEFAULT NULL AFTER `account_notes`");
o-psi marked this conversation as resolved.
Show resolved Hide resolved

// Then, update the database to the next sequential version
//mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.8.7'");
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.8.7'");
}

// Be sure to change database_version.php to reflect the version you are updating to here
// Please add this same comment block to the bottom of this file, and update the version number.
// Uncomment Below Lines, to add additional database updates
//
//if (CURRENT_DATABASE_VERSION == '0.8.7') {
// Insert queries here required to update to DB version 0.8.9
//
// Then, update the database to the next sequential version
//mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.8.8'");
//}
//

} else {
// Up-to-date
Expand Down
2 changes: 1 addition & 1 deletion database_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* It is used in conjunction with database_updates.php
*/

DEFINE("LATEST_DATABASE_VERSION", "0.8.6");
DEFINE("LATEST_DATABASE_VERSION", "0.8.7");
1 change: 1 addition & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE `accounts` (
`opening_balance` decimal(15,2) NOT NULL DEFAULT 0.00,
`account_currency_code` varchar(200) NOT NULL,
`account_notes` text DEFAULT NULL,
`account_type` int(6) NOT NULL DEFAULT 11,
`account_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`account_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`account_archived_at` datetime DEFAULT NULL,
Expand Down
6 changes: 4 additions & 2 deletions post/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
$opening_balance = floatval($_POST['opening_balance']);
$currency_code = sanitizeInput($_POST['currency_code']);
$notes = sanitizeInput($_POST['notes']);
$type = intval($_POST['type']);

mysqli_query($mysqli,"INSERT INTO accounts SET account_name = '$name', opening_balance = $opening_balance, account_currency_code = '$currency_code', account_notes = '$notes'");
mysqli_query($mysqli,"INSERT INTO accounts SET account_name = '$name', opening_balance = $opening_balance, account_currency_code = '$currency_code', account_type ='$type', account_notes = '$notes'");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Create', log_description = '$name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
Expand All @@ -26,9 +27,10 @@

$account_id = intval($_POST['account_id']);
$name = sanitizeInput($_POST['name']);
$type = intval($_POST['type']);
$notes = sanitizeInput($_POST['notes']);

mysqli_query($mysqli,"UPDATE accounts SET account_name = '$name', account_notes = '$notes' WHERE account_id = $account_id");
mysqli_query($mysqli,"UPDATE accounts SET account_name = '$name',account_type = '$type', account_notes = '$notes' WHERE account_id = $account_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Modify', log_description = '$name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
Expand Down
155 changes: 155 additions & 0 deletions report_balance_sheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
require_once("inc_all_reports.php");
validateAccountantRole();

// 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(r.revenue_amount), 0) AS total_revenues,
COALESCE(SUM(e.expense_amount), 0) AS total_expenses
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";

$result_accounts = mysqli_query($mysqli, $sql_accounts);

$total_assets = 0;
$total_liabilities = 0;
$total_equity = 0;
?>

<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>
<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)) {
$balance = $row['opening_balance'] + $row['total_payments'] + $row['total_revenues'] - $row['total_expenses'];
$account_type = $row['account_type'];
if ($account_type >= 11 && $account_type <= 19) {
// Display assets account row
echoAccountRow($row, $balance);
$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>

<!-- 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) {
// Display liabilities account row
echoAccountRow($row, $balance);
$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_assets; ?></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) {
// Display equity account row
echoAccountRow($row, $balance);
$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>
</tbody>
</table>
</div>
</div>
</div>

<?php require_once("footer.php"); ?>

<?php
function echoAccountRow($accountRow, $balance) {
global $currency_format;
$account_type_strings = [
11 => "Current Assets",
12 => "Fixed Assets",
13 => "Other Assets",
21 => "Current Liabilities",
22 => "Long Term Liabilities",
23 => "Other Liabilities",
30 => "Equity"
];
$account_type_string = $account_type_strings[$accountRow['account_type']] ?? "Unknown";
$account_name_encoded = urlencode($accountRow['account_name']);
echo "
<tr>
<td>$account_type_string</td>
<td><a class=\"text-dark\" href=\"account_details.php?account_name=$account_name_encoded\">{$accountRow['account_name']}</a></td>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • All non-int variables should be echoed/assigned using nullable_htmlentities to prevent XSS

  • Ideally (where possible) we try to assign the variable once (e.g. as $account_type and $account_name and then just re-use as required), rather than constantly reusing $accountRow['account_type'] - its much cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been resolved. Looking over the rest of the code now.

<td class=\"text-right\">" . numfmt_format_currency($currency_format, $balance, $accountRow['account_currency_code']) . "</td>
</tr>
";
}

?>
6 changes: 6 additions & 0 deletions reports_side_nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<p>Profit & Loss</p>
</a>
</li>
<li class="nav-item">
<a href="report_balance_sheet.php" class="nav-link <?php if (basename($_SERVER["PHP_SELF"]) == "report_balance_sheet.php") { echo "active"; } ?>">
<i class="fas fa-balance-scale nav-icon"></i>
<p>Balance Sheet</p>
</a>
</li>
<?php } // End financial reports IF statement ?>

<?php if ($session_user_role == 2 || $session_user_role == 3) { ?>
Expand Down