diff --git a/functions.php b/functions.php index 7c3f19a0..bf2abf41 100644 --- a/functions.php +++ b/functions.php @@ -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; } \ No newline at end of file diff --git a/report_balance_sheet.php b/report_balance_sheet.php index f384d8a3..bdfd2583 100644 --- a/report_balance_sheet.php +++ b/report_balance_sheet.php @@ -1,33 +1,58 @@ 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 ""; + echo "{$account['type']}"; + echo "{$account['name']}"; + echo "" . numfmt_format_currency($currency_format, $account['balance'], $currency_code) . ""; + echo ""; + } +} - $total_assets = 0; - $total_liabilities = 0; - $total_equity = 0; - $currency = $session_company_currency; ?>
@@ -40,131 +65,46 @@
-
-

- -

-

Balance Sheet

-
As of
-
+
+

+ +

+

Balance Sheet

+
As of
+
+
- - - - - - - - - - - - - = 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']); - } - - } - ?> - - - - - + + + + + + + + + + - - - - - = 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']); - } - } - ?> - - - - - + + + + + + + - - - - - = 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']); - } - } - ?> - - - - - - + + + + + + + + +
Account TypeAccount NameAccount Balance
Assets
Total Assets
Total Assets
Liabilities
Total Liabilities
Total Liabilities
Equity
Total Equity
Total Equities
- - - - - Total Liabilities and Equity - - - - - - Unbalanced: -
-
- - - - +
-
-"; - echo ""; - echo "$account_name"; - echo "$formatted_balance"; - echo ""; -} - - -?> + \ No newline at end of file