FIX option "Show subtotal" on the balance report

This commit is contained in:
Laurent Destailleur 2020-11-26 16:12:55 +01:00
commit 52cc08f5da
2 changed files with 24 additions and 16 deletions

View File

@ -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 .= $form->selectDate($search_date_end ? $search_date_end : -1, 'date_end', 0, 0, 1, '', 1, 0);
$moreforfilter .= ' - '; $moreforfilter .= ' - ';
$moreforfilter .= $langs->trans('ShowSubtotalByGroup').': '; $moreforfilter .= '<label for="show_subgroup">'.$langs->trans('ShowSubtotalByGroup').'</label>: ';
$moreforfilter .= '<input type="checkbox" name="show_subgroup" value="show_subgroup"'.($show_subgroup == 'show_subgroup' ? ' checked' : '').'>'; $moreforfilter .= '<input type="checkbox" name="show_subgroup" id="show_subgroup" value="show_subgroup"'.($show_subgroup == 'show_subgroup' ? ' checked' : '').'>';
$moreforfilter .= '</div>'; $moreforfilter .= '</div>';
@ -272,6 +272,8 @@ if ($action != 'export_csv')
$total_credit = 0; $total_credit = 0;
$sous_total_debit = 0; $sous_total_debit = 0;
$sous_total_credit = 0; $sous_total_credit = 0;
$total_opening_balance = 0;
$sous_total_opening_balance = 0;
$displayed_account = ""; $displayed_account = "";
$accountingaccountstatic = new AccountingAccount($db); $accountingaccountstatic = new AccountingAccount($db);
@ -302,7 +304,13 @@ if ($action != 'export_csv')
$link = ''; $link = '';
$total_debit += $line->debit; $total_debit += $line->debit;
$total_credit += $line->credit; $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)) { if (empty($accountingaccountstatic->account_number)) {
$link = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/card.php?action=create&accountingaccount='.length_accountg($line->numero_compte).'">'.img_edit_add().'</a>'; $link = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/card.php?action=create&accountingaccount='.length_accountg($line->numero_compte).'">'.img_edit_add().'</a>';
} }
@ -311,14 +319,14 @@ if ($action != 'export_csv')
if (!empty($show_subgroup)) if (!empty($show_subgroup))
{ {
// Show accounting account // 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 // Show subtotal per accounting account
if ($displayed_account != "") { if ($displayed_account != "") {
print '<tr class="liste_total">'; print '<tr class="liste_total">';
print '<td class="right" colspan="2">'.$langs->trans("SubTotal").':</td>'; print '<td class="right" colspan="2">'.$langs->trans("SubTotal").':</td>';
print '<td class="nowrap right">'.price($sous_total_debit).'</td>'; print '<td class="nowrap right">'.price($sous_total_debit).'</td>';
print '<td class="nowrap right">'.price($sous_total_credit).'</td>'; print '<td class="nowrap right">'.price($sous_total_credit).'</td>';
print '<td class="nowrap right">'.price(price2num($sous_total_credit - $sous_total_debit)).'</td>'; print '<td class="nowrap right">'.price(price2num($sous_total_opening_balance + $sous_total_credit - $sous_total_debit)).'</td>';
print "<td></td>\n"; print "<td></td>\n";
print '</tr>'; print '</tr>';
} }
@ -328,18 +336,18 @@ if ($action != 'export_csv')
print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">'.$line->numero_compte.($root_account_description ? ' - '.$root_account_description : '').'</td>'; print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">'.$line->numero_compte.($root_account_description ? ' - '.$root_account_description : '').'</td>';
print '</tr>'; print '</tr>';
$displayed_account = $root_account_description; $displayed_account = $root_account_number;
$sous_total_debit = 0; $sous_total_debit = 0;
$sous_total_credit = 0; $sous_total_credit = 0;
$sous_total_opening_balance = 0;
} }
} }
// $object->get_compte_racine($line->numero_compte);
print '<td>'.$accounting_account.'</td>'; print '<td>'.$accounting_account.'</td>';
print '<td class="nowraponall right">'.price($opening_balances["'".$line->numero_compte."'"]).'</td>'; print '<td class="nowraponall right">'.price($opening_balance).'</td>';
print '<td class="nowraponall right">'.price($line->debit).'</td>'; print '<td class="nowraponall right">'.price($line->debit).'</td>';
print '<td class="nowraponall right">'.price($line->credit).'</td>'; print '<td class="nowraponall right">'.price($line->credit).'</td>';
print '<td class="nowraponall right">'.price(price2num($line->debit - $line->credit, 'MT')).'</td>'; print '<td class="nowraponall right">'.price(price2num($opening_balance + $line->debit - $line->credit, 'MT')).'</td>';
print '<td class="center">'.$link; print '<td class="center">'.$link;
print '</td>'; print '</td>';
print "</tr>\n"; print "</tr>\n";
@ -347,6 +355,7 @@ if ($action != 'export_csv')
// Records the sub-total // Records the sub-total
$sous_total_debit += $line->debit; $sous_total_debit += $line->debit;
$sous_total_credit += $line->credit; $sous_total_credit += $line->credit;
$sous_total_opening_balance += $opening_balance;
} }
if (!empty($show_subgroup)) if (!empty($show_subgroup))

View File

@ -1898,23 +1898,22 @@ class BookKeeping extends CommonObject
return $out; 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 * @param string $account Accounting account
* @return string Root account * @return string Root account
*/ */
public function get_compte_racine($account = null) public function getRootAccount($account = null)
{ {
// phpcs:enable
global $conf; global $conf;
$pcgver = $conf->global->CHARTOFACCOUNTS; $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 .= " 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 .= " 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 parent ON aa.account_parent = parent.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as root ON parent.account_parent = root.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)."'"; $sql .= " WHERE aa.account_number = '".$this->db->escape($account)."'";
@ -1930,7 +1929,7 @@ class BookKeeping extends CommonObject
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
} }
return $obj->label; return array('id'=>$obj->rowid, 'account_number'=>$obj->account_number, 'label'=>$obj->label);
} else { } else {
$this->error = "Error ".$this->db->lasterror(); $this->error = "Error ".$this->db->lasterror();
dol_syslog(__METHOD__." ".$this->error, LOG_ERR); dol_syslog(__METHOD__." ".$this->error, LOG_ERR);