diff --git a/htdocs/accountancy/bookkeeping/balance.php b/htdocs/accountancy/bookkeeping/balance.php
index a930ed12852..55797290058 100644
--- a/htdocs/accountancy/bookkeeping/balance.php
+++ b/htdocs/accountancy/bookkeeping/balance.php
@@ -227,8 +227,8 @@ if ($action != 'export_csv')
$moreforfilter .= $form->selectDate($search_date_end ? $search_date_end : -1, 'date_end', 0, 0, 1, '', 1, 0);
$moreforfilter .= ' - ';
- $moreforfilter .= $langs->trans('ShowSubtotalByGroup').': ';
- $moreforfilter .= '';
+ $moreforfilter .= ': ';
+ $moreforfilter .= '';
$moreforfilter .= '';
@@ -272,6 +272,8 @@ 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 = "";
$accountingaccountstatic = new AccountingAccount($db);
@@ -302,7 +304,13 @@ if ($action != 'export_csv')
$link = '';
$total_debit += $line->debit;
$total_credit += $line->credit;
- $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;
+
+ $tmparrayforrootaccount = $object->getRootAccount($line->numero_compte);
+ $root_account_description = $tmparrayforrootaccount['label'];
+ $root_account_number = $tmparrayforrootaccount['account_number'];
+
if (empty($accountingaccountstatic->account_number)) {
$link = ''.img_edit_add().'';
}
@@ -311,14 +319,14 @@ if ($action != 'export_csv')
if (!empty($show_subgroup))
{
// Show accounting account
- if (empty($displayed_account) || $root_account_description != $displayed_account) {
+ if (empty($displayed_account) || $root_account_number != $displayed_account) {
// Show subtotal per accounting account
if ($displayed_account != "") {
print '
';
print '| '.$langs->trans("SubTotal").': | ';
print ''.price($sous_total_debit).' | ';
print ''.price($sous_total_credit).' | ';
- print ''.price(price2num($sous_total_credit - $sous_total_debit)).' | ';
+ print ''.price(price2num($sous_total_opening_balance + $sous_total_credit - $sous_total_debit)).' | ';
print " | \n";
print '
';
}
@@ -328,18 +336,18 @@ if ($action != 'export_csv')
print ''.$line->numero_compte.($root_account_description ? ' - '.$root_account_description : '').' | ';
print '';
- $displayed_account = $root_account_description;
+ $displayed_account = $root_account_number;
$sous_total_debit = 0;
$sous_total_credit = 0;
+ $sous_total_opening_balance = 0;
}
}
- // $object->get_compte_racine($line->numero_compte);
print ''.$accounting_account.' | ';
- print ''.price($opening_balances["'".$line->numero_compte."'"]).' | ';
+ print ''.price($opening_balance).' | ';
print ''.price($line->debit).' | ';
print ''.price($line->credit).' | ';
- print ''.price(price2num($line->debit - $line->credit, 'MT')).' | ';
+ print ''.price(price2num($opening_balance + $line->debit - $line->credit, 'MT')).' | ';
print ''.$link;
print ' | ';
print "\n";
@@ -347,6 +355,7 @@ if ($action != 'export_csv')
// Records the sub-total
$sous_total_debit += $line->debit;
$sous_total_credit += $line->credit;
+ $sous_total_opening_balance += $opening_balance;
}
if (!empty($show_subgroup))
diff --git a/htdocs/accountancy/class/bookkeeping.class.php b/htdocs/accountancy/class/bookkeeping.class.php
index 505c51d27fe..8312b155b8b 100644
--- a/htdocs/accountancy/class/bookkeeping.class.php
+++ b/htdocs/accountancy/class/bookkeeping.class.php
@@ -1898,23 +1898,22 @@ class BookKeeping extends CommonObject
return $out;
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
- * Description of a root accounting account
+ * Return id and description of a root accounting account.
+ * This function takes the parent of parent to get the root account !
*
* @param string $account Accounting account
* @return string Root account
*/
- public function get_compte_racine($account = null)
+ public function getRootAccount($account = null)
{
- // phpcs:enable
global $conf;
$pcgver = $conf->global->CHARTOFACCOUNTS;
- $sql = "SELECT root.account_number, root.label as label";
+ $sql = "SELECT root.rowid, root.account_number, root.label as label";
$sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa";
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
- $sql .= " AND asy.rowid = ".$pcgver;
+ $sql .= " AND asy.rowid = ".((int) $pcgver);
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as parent ON aa.account_parent = parent.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as root ON parent.account_parent = root.rowid";
$sql .= " WHERE aa.account_number = '".$this->db->escape($account)."'";
@@ -1930,7 +1929,7 @@ class BookKeeping extends CommonObject
$obj = $this->db->fetch_object($resql);
}
- return $obj->label;
+ return array('id'=>$obj->rowid, 'account_number'=>$obj->account_number, 'label'=>$obj->label);
} else {
$this->error = "Error ".$this->db->lasterror();
dol_syslog(__METHOD__." ".$this->error, LOG_ERR);