Debug v17 - Fix the report on custom groups, column previous period

This commit is contained in:
Laurent Destailleur 2022-12-29 20:25:53 +01:00
parent f4476b905c
commit bcd5c207cf
2 changed files with 100 additions and 94 deletions

View File

@ -678,6 +678,22 @@ class AccountancyCategory // extends CommonObject
$this->sdc = 0;
$this->sdcpermonth = array();
if (is_array($cpt)) {
$listofaccount = '';
foreach ($cpt as $cptcursor) {
if (! is_null($cptcursor)) {
if ($listofaccount) {
$listofaccount .= ",";
}
$listofaccount .= "'".$cptcursor."'";
}
}
if (empty($listofaccount)) {
// List of account is empty, so we do no try sql request, we can say result is empty.
return 0;
}
}
$sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
if (is_array($cpt)) {
$sql .= ", t.numero_compte as accountancy_account";
@ -686,13 +702,6 @@ class AccountancyCategory // extends CommonObject
//if (in_array($this->db->type, array('mysql', 'mysqli'))) $sql.=' USE INDEX idx_accounting_bookkeeping_doc_date';
$sql .= " WHERE t.entity = ".$conf->entity;
if (is_array($cpt)) {
$listofaccount = '';
foreach ($cpt as $cptcursor) {
if ($listofaccount) {
$listofaccount .= ",";
}
$listofaccount .= "'".$cptcursor."'";
}
$sql .= " AND t.numero_compte IN (".$this->db->sanitize($listofaccount, 1).")";
} else {
$sql .= " AND t.numero_compte = '".$this->db->escape($cpt)."'";
@ -709,22 +718,28 @@ class AccountancyCategory // extends CommonObject
if (is_array($cpt)) {
$sql .= " GROUP BY t.numero_compte";
}
//print $sql;
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
if ($num) {
$obj = $this->db->fetch_object($resql);
if ($sens == 1) {
$this->sdc = $obj->debit - $obj->credit;
} else {
$this->sdc = $obj->credit - $obj->debit;
}
if (is_array($cpt)) {
$this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
if ($obj) {
if ($sens == 1) {
$this->sdc = $obj->debit - $obj->credit;
} else {
$this->sdc = $obj->credit - $obj->debit;
}
if (is_array($cpt)) {
$this->sdcperaccount[$obj->accountancy_account] = $this->sdc;
}
}
$i++;
}
}
return $num;
} else {
$this->error = "Error ".$this->db->lasterror();

View File

@ -184,8 +184,8 @@ llxheader('', $langs->trans('ReportInOut'));
$formaccounting = new FormAccounting($db);
$form = new Form($db);
$textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year - 1).'">'.img_previous().'</a>';
$textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year + 1).'">'.img_next().'</a>';
$textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year - 1).'&showaccountdetail='.urlencode($showaccountdetail).'">'.img_previous().'</a>';
$textnextyear = ' &nbsp; <a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year + 1).'&showaccountdetail='.urlencode($showaccountdetail).'">'.img_next().'</a>';
@ -309,8 +309,8 @@ if ($modecompta == 'CREANCES-DETTES') {
if (!is_array($cats) && $cats < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
} elseif (is_array($cats) && count($cats) > 0) {
// Loop on each custom group of accounts
foreach ($cats as $cat) {
// Loop on each group
if (!empty($cat['category_type'])) {
// category calculed
// When we enter here, $sommes was filled by group of accounts
@ -319,10 +319,10 @@ if ($modecompta == 'CREANCES-DETTES') {
print '<tr class="liste_total">';
// Year NP
print '<td class="liste_total width200">';
// Code and Label
print '<td class="liste_total tdoverflowmax100" title="'.dol_escape_htmltag($cat['code']).'">';
print dol_escape_htmltag($cat['code']);
print '</td><td>';
print '</td><td class="tdoverflowmax250" title="'.dol_escape_htmltag($cat['label']).'">';
print dol_escape_htmltag($cat['label']);
print '</td>';
@ -345,83 +345,71 @@ if ($modecompta == 'CREANCES-DETTES') {
$r = 'Error bad formula: '.$result;
$rshort = 'Err';
print '<td class="liste_total right"><span class="amount" title="'.dol_escape_htmltag($r).'">'.$rshort.'</span></td>';
print '<td class="liste_total right"><span class="amount" title="'.dol_escape_htmltag($r).'">'.$rshort.'</span></td>';
// Detail by month
foreach ($months as $k => $v) {
if (($k + 1) >= $date_startmonth) {
print '<td class="liste_total right"><span class="amount" title="'.dol_escape_htmltag($r).'">'.$rshort.'</span></td>';
}
}
foreach ($months as $k => $v) {
if (($k + 1) < $date_startmonth) {
print '<td class="liste_total right"><span class="amount" title="'.dol_escape_htmltag($r).'">'.$rshort.'</span></td>';
}
}
} else {
//var_dump($result);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, '1');
//var_dump($r);
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
}
// Year N
$code = $cat['code']; // code of categorie ('VTE', 'MAR', ...)
$sommes[$code]['NP'] += $r;
// Year N
$code = $cat['code']; // code of categorie ('VTE', 'MAR', ...)
$sommes[$code]['NP'] += $r;
// Current fiscal year (N)
if (is_array($sommes) && !empty($sommes)) {
// Current fiscal year (N)
if (is_array($sommes) && !empty($sommes)) {
foreach ($sommes as $code => $det) {
$vars[$code] = $det['N'];
}
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, '1');
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
$sommes[$code]['N'] += $r;
// Detail by month
foreach ($months as $k => $v) {
if (($k + 1) >= $date_startmonth) {
foreach ($sommes as $code => $det) {
$vars[$code] = $det['N'];
$vars[$code] = $det['M'][$k];
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, '1');
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
$sommes[$code]['M'][$k] += $r;
}
}
$result = strtr($formula, $vars);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, 1);
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
$sommes[$code]['N'] += $r;
// Detail by month
foreach ($months as $k => $v) {
if (($k + 1) >= $date_startmonth) {
foreach ($sommes as $code => $det) {
$vars[$code] = $det['M'][$k];
}
$result = strtr($formula, $vars);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, 1);
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
$sommes[$code]['M'][$k] += $r;
foreach ($months as $k => $v) {
if (($k + 1) < $date_startmonth) {
foreach ($sommes as $code => $det) {
$vars[$code] = $det['M'][$k];
}
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, '1');
foreach ($months as $k => $v) {
if (($k + 1) < $date_startmonth) {
foreach ($sommes as $code => $det) {
$vars[$code] = $det['M'][$k];
}
$result = strtr($formula, $vars);
//$r = $AccCat->calculate($result);
$r = dol_eval($result, 1, 1, 1);
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
$sommes[$code]['M'][$k] += $r;
}
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
$sommes[$code]['M'][$k] += $r;
}
}
print "</tr>\n";
//var_dump($sommes);
} else // normal category
{
} else { // normal category
$code = $cat['code']; // Category code we process
$totCat = array();
@ -435,23 +423,25 @@ if ($modecompta == 'CREANCES-DETTES') {
// Set $cpts with array of accounts in the category/group
$cpts = $AccCat->getCptsCat($cat['rowid']);
// We should loop over empty $cpts array, else the category _code_ is used in the formula, which leads to wrong result if the code is a number.
if (empty($cpts)) $cpts[] = array();
if (empty($cpts)) {
$cpts[] = array();
}
$arrayofaccountforfilter = array();
foreach ($cpts as $i => $cpt) { // Loop on each account.
$arrayofaccountforfilter[] = $cpt['account_number'];
if (!is_null($cpt['account_number'])) {
$arrayofaccountforfilter[] = $cpt['account_number'];
}
}
// N-1
if (!empty($arrayofaccountforfilter)) {
$return = $AccCat->getSumDebitCredit($arrayofaccountforfilter, $date_start_previous, $date_end_previous, $cat['dc'] ? $cat['dc'] : 0);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultNP = 0;
} else {
foreach ($cpts as $i => $cpt) { // Loop on each account.
foreach ($cpts as $i => $cpt) { // Loop on each account found
$resultNP = empty($AccCat->sdcperaccount[$cpt['account_number']]) ? 0 : $AccCat->sdcperaccount[$cpt['account_number']];
$totCat['NP'] += $resultNP;
@ -499,37 +489,38 @@ if ($modecompta == 'CREANCES-DETTES') {
// Now output columns for row $code ('VTE', 'MAR', ...)
print "<tr>";
print '<tr'.($showaccountdetail != 'no' ? ' class="trforbreak"' : '').'>';
// Column group
print '<td class="width200">';
print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($cat['code']).'">';
print dol_escape_htmltag($cat['code']);
print '</td>';
// Label of group
print '<td>';
print dol_escape_htmltag($cat['label']);
$labeltoshow = dol_escape_htmltag($cat['label']);
if (count($cpts) > 0 && !empty($cpts[0])) { // Show example of 5 first accounting accounts
$i = 0;
foreach ($cpts as $cpt) {
if ($i > 5) {
print '...)';
$labeltoshow .= '...)';
break;
}
if ($i > 0) {
print ', ';
$labeltoshow .= ', ';
} else {
print ' (';
$labeltoshow .= ' (';
}
print dol_escape_htmltag($cpt['account_number']);
$labeltoshow .= dol_escape_htmltag($cpt['account_number']);
$i++;
}
if ($i <= 5) {
print ')';
$labeltoshow .= ')';
}
} else {
print ' - <span class="warning">'.$langs->trans("GroupIsEmptyCheckSetup").'</span>';
$labeltoshow .= ' - <span class="warning">'.$langs->trans("GroupIsEmptyCheckSetup").'</span>';
}
print '<td class="tdoverflowmax250" title="'.dol_escape_htmltag(dol_string_nohtmltag($labeltoshow)).'">';
print $labeltoshow;
print '</td>';
print '<td class="right"><span class="amount">'.price($totCat['NP']).'</span></td>';
@ -558,7 +549,7 @@ if ($modecompta == 'CREANCES-DETTES') {
if ($showaccountdetail == 'all' || $resultN != 0) {
print '<tr>';
print '<td></td>';
print '<td class="tdoverflowmax200">';
print '<td class="tdoverflowmax250">';
print ' &nbsp; &nbsp; '.length_accountg($cpt['account_number']);
print ' - ';
print $cpt['account_label'];