diff --git a/htdocs/accountancy/class/accountancycategory.class.php b/htdocs/accountancy/class/accountancycategory.class.php index 68662ccb610..300ccff9c09 100644 --- a/htdocs/accountancy/class/accountancycategory.class.php +++ b/htdocs/accountancy/class/accountancycategory.class.php @@ -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(); diff --git a/htdocs/compta/resultat/result.php b/htdocs/compta/resultat/result.php index 856eb8a5fe8..8615e595200 100644 --- a/htdocs/compta/resultat/result.php +++ b/htdocs/compta/resultat/result.php @@ -184,8 +184,8 @@ llxheader('', $langs->trans('ReportInOut')); $formaccounting = new FormAccounting($db); $form = new Form($db); -$textprevyear = ''.img_previous().''; -$textnextyear = ' '.img_next().''; +$textprevyear = ''.img_previous().''; +$textnextyear = '   '.img_next().''; @@ -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 ''; - // Year NP - print ''; + // Code and Label + print ''; print dol_escape_htmltag($cat['code']); - print ''; + print ''; print dol_escape_htmltag($cat['label']); print ''; @@ -345,83 +345,71 @@ if ($modecompta == 'CREANCES-DETTES') { $r = 'Error bad formula: '.$result; $rshort = 'Err'; print ''.$rshort.''; - print ''.$rshort.''; - // Detail by month - foreach ($months as $k => $v) { - if (($k + 1) >= $date_startmonth) { - print ''.$rshort.''; - } - } - foreach ($months as $k => $v) { - if (($k + 1) < $date_startmonth) { - print ''.$rshort.''; - } - } } else { //var_dump($result); //$r = $AccCat->calculate($result); $r = dol_eval($result, 1, 1, '1'); - //var_dump($r); print ''.price($r).''; + } - // 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 ''.price($r).''; + $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 ''.price($r).''; + $sommes[$code]['M'][$k] += $r; } + } - $result = strtr($formula, $vars); - - //$r = $AccCat->calculate($result); - $r = dol_eval($result, 1, 1, 1); - - print ''.price($r).''; - $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 ''.price($r).''; - $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 ''.price($r).''; - $sommes[$code]['M'][$k] += $r; - } + print ''.price($r).''; + $sommes[$code]['M'][$k] += $r; } } print "\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 ""; + print ''; // Column group - print ''; + print ''; print dol_escape_htmltag($cat['code']); print ''; // Label of group - print ''; - 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 ' - '.$langs->trans("GroupIsEmptyCheckSetup").''; + $labeltoshow .= ' - '.$langs->trans("GroupIsEmptyCheckSetup").''; } + print ''; + print $labeltoshow; print ''; print ''.price($totCat['NP']).''; @@ -558,7 +549,7 @@ if ($modecompta == 'CREANCES-DETTES') { if ($showaccountdetail == 'all' || $resultN != 0) { print ''; print ''; - print ''; + print ''; print '     '.length_accountg($cpt['account_number']); print ' - '; print $cpt['account_label'];