From 551256b77936fc4d24b5f3b7fd93c91603ae1eb0 Mon Sep 17 00:00:00 2001
From: Muhammad Aboelfotoh <>
Date: Wed, 25 Nov 2020 15:11:09 +0000
Subject: [PATCH] FIX #13067 including opening balance in calculation of
displayed balance
---
htdocs/accountancy/bookkeeping/balance.php | 30 ++++++++++++++--------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/htdocs/accountancy/bookkeeping/balance.php b/htdocs/accountancy/bookkeeping/balance.php
index 456da29d954..ef19a6ae330 100644
--- a/htdocs/accountancy/bookkeeping/balance.php
+++ b/htdocs/accountancy/bookkeeping/balance.php
@@ -265,10 +265,16 @@ if ($action != 'export_csv')
$total_credit = 0;
$sous_total_debit = 0;
$sous_total_credit = 0;
+ $total_opening_balance = 0;
+ $sous_total_opening_balance = 0;
$displayed_account = "";
- $sql = "select t.numero_compte, (SUM(t.debit) - SUM(t.credit)) as opening_balance from ".MAIN_DB_PREFIX."accounting_bookkeeping as t where entity in ".$conf->entity;
- $sql .= " AND t.doc_date < '".$db->idate($search_date_start)."' GROUP BY t.numero_compte";
+ $sql = "SELECT t.numero_compte, (SUM(t.debit) - SUM(t.credit)) as opening_balance";
+ $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as t";
+ $sql .= " WHERE t.entity = ".$conf->entity; // Never do sharing into accounting features
+ $sql .= " AND t.doc_date < '".$db->idate($search_date_start)."'";
+ $sql .= " GROUP BY t.numero_compte";
+
$resql = $db->query($sql);
$nrows = $resql->num_rows;
$opening_balances = array();
@@ -284,17 +290,19 @@ if ($action != 'export_csv')
$total_credit += $line->credit;
$description = $object->get_compte_desc($line->numero_compte); // Search description of the account
$root_account_description = $object->get_compte_racine($line->numero_compte);
+ $opening_balance = isset($opening_balances["'".$line->numero_compte."'"]) ? $opening_balances["'".$line->numero_compte."'"] : 0;
+ $total_opening_balance += $opening_balance;
if (empty($description)) {
$link = ''.img_edit_add().'';
}
print '
';
- // Permet d'afficher le compte comptable
+ // Display the accounting account
if (empty($displayed_account) || $root_account_description != $displayed_account)
{
- // Affiche un Sous-Total par compte comptable
+ // Display a sub-total per account
if ($displayed_account != "") {
- print '
| '.$langs->trans("SubTotal").': | '.price($sous_total_debit).' | '.price($sous_total_credit).' | '.price(price2num($sous_total_credit - $sous_total_debit)).' | ';
+ print '
| '.$langs->trans("SubTotal").': | '.price($sous_total_opening_balance).' | '.price($sous_total_debit).' | '.price($sous_total_credit).' | '.price(price2num($sous_total_credit - $sous_total_debit)).' | ';
print " | \n";
print '
';
}
@@ -307,30 +315,32 @@ if ($action != 'export_csv')
$displayed_account = $root_account_description;
$sous_total_debit = 0;
$sous_total_credit = 0;
+ $sous_total_opening_balance = 0;
}
// $object->get_compte_racine($line->numero_compte);
print ''.length_accountg($line->numero_compte).' | ';
print ''.$description.' | ';
- print ''.price($opening_balances["'".$line->numero_compte."'"]).' | ';
+ print ''.price($opening_balance).' | ';
print ''.price($line->debit).' | ';
print ''.price($line->credit).' | ';
- print ''.price($line->debit - $line->credit).' | ';
+ print ''.price($opening_balance + $line->debit - $line->credit).' | ';
print ''.$link;
print ' | ';
print "\n";
- // Comptabilise le sous-total
+ // Records the sub-total
$sous_total_debit += $line->debit;
$sous_total_credit += $line->credit;
+ $sous_total_opening_balance += $opening_balance;
}
- print '| '.$langs->trans("SubTotal").': | '.price($sous_total_debit).' | '.price($sous_total_credit).' | '.price(price2num($sous_total_debit - $sous_total_credit)).' | ';
+ print '
| '.$langs->trans("SubTotal").': | '.price($sous_total_opening_balance).' | '.price($sous_total_debit).' | '.price($sous_total_credit).' | '.price(price2num($sous_total_opening_balance + $sous_total_debit - $sous_total_credit)).' | ';
print " | \n";
print '
';
- print '| '.$langs->trans("AccountBalance").': | '.price($total_debit).' | '.price($total_credit).' | '.price(price2num($total_debit - $total_credit)).' | ';
+ print '
| '.$langs->trans("AccountBalance").': | '.price($total_opening_balance).' | '.price($total_debit).' | '.price($total_credit).' | '.price(price2num($total_opening_balance + $total_debit - $total_credit)).' | ';
print " | \n";
print '
';