Performance enhancement in Accounting Personalized report

This commit is contained in:
Laurent Destailleur 2018-11-22 10:40:15 +01:00
parent 5e9abe6aad
commit 21e9c02b11
3 changed files with 35 additions and 18 deletions

View File

@ -713,27 +713,36 @@ class AccountancyCategory // extends CommonObject
* Function to show result of an accounting account from the ledger with a direction and a period
*
* @param int $cpt Id accounting account
* @param string $month Specifig month - Can be empty
* @param string $date_start Date start
* @param string $date_end Date end
* @param int $sens Sens of the account: 0: credit - debit, 1: debit - credit
* @param string $thirdparty_code Thirdparty code
* @param string $month Specifig month - Can be empty
* @param string $year Specifig year - Can be empty
* @return integer Result in table
*/
public function getResult($cpt, $month, $date_start, $date_end, $sens, $thirdparty_code='nofilter')
public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code='nofilter', $month=0, $year=0)
{
$sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
$sql .= " WHERE t.numero_compte = '" . $cpt."'";
$sql .= " WHERE t.numero_compte = '" . $this->db->escape($cpt) . "'";
/*
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND t.doc_date >= '".$this->db->idate($date_start)."' AND t.doc_date <= '".$this->db->idate($date_end)."'";
if (! empty($month)) {
$sql .= " AND MONTH(t.doc_date) = " . $month;
}
*/
if (! empty($date_start) && ! empty($date_end))
$sql .= " AND t.doc_date BETWEEN '".$this->db->idate($date_start)."' AND '".$this->db->idate($date_end)."'";
if (! empty($month) && ! empty($year)) {
$sql .= " AND t.doc_date BETWEEN '".$this->db->idate(dol_get_first_day($year, $month))."' AND '".$this->db->idate(dol_get_last_day($year, $month))."'";
}
if ($thirdparty_code != 'nofilter')
{
$sql .= " AND thirdparty_code = '".$this->db->escape($thirdparty_code)."'";
}
//print $sql;
dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
$resql = $this->db->query($sql);
@ -856,13 +865,12 @@ class AccountancyCategory // extends CommonObject
if ($num) {
while ($obj = $this->db->fetch_object($resql))
{
$name_cat = $obj->name_cat;
$data[] = array (
'id' => $obj->rowid,
'account_number' => $obj->account_number,
'account_label' => $obj->account_label,
);
$i ++;
$i++;
}
}
return $data;

View File

@ -303,7 +303,7 @@ if ($modecompta == 'BOOKKEEPING')
foreach($cpts as $i => $cpt)
{
$return = $AccCat->getResult($cpt['account_number'], 0, $date_start, $date_end, $cpt['dc']);
$return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start, $date_end, $cpt['dc']);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultN=0;

View File

@ -78,7 +78,7 @@ if (empty($date_start) || empty($date_end)) // We define date_start and date_end
{
// We define date_start and date_end
$year_end=$year_start + ($nbofyear - 1);
$month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
$month_start=GETPOST("month",'int')?GETPOST("month",'int'):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
$date_startmonth = $month_start;
if (! GETPOST('month'))
{
@ -108,8 +108,10 @@ if (($date_start < dol_time_plus_duree($date_end, -1, 'y')) || ($date_start > $d
// $date_start and $date_end are defined. We force $start_year and $nbofyear
$tmps=dol_getdate($date_start);
$start_year = $tmps['year'];
$start_month = $tmps['mon'];
$tmpe=dol_getdate($date_end);
$year_end = $tmpe['year'];
$month_end = $tmpe['mon'];
$nbofyear = ($year_end - $start_year) + 1;
$date_start_previous = dol_time_plus_duree($date_start, -1, 'y');
@ -374,7 +376,6 @@ elseif ($modecompta=="BOOKKEEPING")
}
}
print "</tr>\n";
//var_dump($sommes);
@ -390,7 +391,7 @@ elseif ($modecompta=="BOOKKEEPING")
$totCat['M'][$k] = 0;
}
// Set $cpts of with array of accounts in the category/group
// Set $cpts with array of accounts in the category/group
$cpts = $AccCat->getCptsCat($cat['rowid']);
print "<tr>";
@ -428,13 +429,15 @@ elseif ($modecompta=="BOOKKEEPING")
$code = $cat['code'];
// Set value into column NPrevious, N and each month M ($totCat)
// This make 14 calls for each detail of account (NP, N and month m)
foreach($cpts as $i => $cpt)
// Set value into column N-1, N and each month M ($totCat)
// This make 14 calls for each detail of account (N-1, N and 12 monthes m)
foreach($cpts as $i => $cpt) // Loop on each account.
{
// N-1
$return = $AccCat->getResult($cpt['account_number'], 0, $date_start_previous, $date_end_previous, $cpt['dc']);
// We make 1 loop for each account because we may want detail.
// @TODO Optimize mode when $showaccountdetail == 'no'
// N-1
$return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start_previous, $date_end_previous, $cpt['dc']);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultNP=0;
@ -442,8 +445,8 @@ elseif ($modecompta=="BOOKKEEPING")
$resultNP=$AccCat->sdc;
}
//N
$return = $AccCat->getResult($cpt['account_number'], 0, $date_start, $date_end, $cpt['dc']);
// N
$return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start, $date_end, $cpt['dc']);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultN=0;
@ -458,9 +461,15 @@ elseif ($modecompta=="BOOKKEEPING")
$totPerAccount[$cpt['account_number']]['NP'] = $resultNP;
$totPerAccount[$cpt['account_number']]['N'] = $resultN;
// Each month
foreach($months as $k => $v)
{
$return = $AccCat->getResult($cpt['account_number'], $k+1, $date_start, $date_end, $cpt['dc']);
$monthtoprocess = $k+1; // ($k+1) is month 1, 2, ..., 12
$yeartoprocess = $start_year;
if (($k+1) < $start_month) $yeartoprocess++;
//var_dump($monthtoprocess.'_'.$yeartoprocess);
$return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start, $date_end, $cpt['dc'], 'nofilter', $monthtoprocess, $yeartoprocess);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultM=0;
@ -488,7 +497,7 @@ elseif ($modecompta=="BOOKKEEPING")
print "</tr>\n";
// Loop on detail of all accounts
// This make 14 calls for each detail of account (NP, N and month m)
// This make 14 calls for each detail of account (N-1, N and 12 monthes m)
if ($showaccountdetail != 'no')
{
foreach($cpts as $i => $cpt)