41 protected $lastfetchdate = array();
46 public $cachefilesuffix =
'';
108 global
$conf, $user, $langs;
110 if ($startyear > $endyear) {
118 if (!empty($cachedelay)) {
119 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
122 $newpathofdestfile =
$conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.
$conf->entity.
'_user'.$user->id.
'.cache';
129 if ($cachedelay > 0) {
131 if ($filedate >= ($nowgmt - $cachedelay)) {
134 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
136 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" cache file ".$newpathofdestfile.
" is not found or older than now - cachedelay (".$nowgmt.
" - ".$cachedelay.
") so we can't use it.");
140 if ($foundintocache) {
141 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
142 $data = json_decode(file_get_contents($newpathofdestfile),
true);
143 '@phan-var-force array<int<0,11>,array{0:int<1,12>,1:int}> $data';
146 $sm = $startmonth - 1;
150 while ($year <= $endyear) {
155 for ($i = 0; $i < 12; $i++) {
156 $data[$i][] = $datay[$endyear][($i + $sm) % 12][0];
158 while ($year <= $endyear) {
160 $data[$i][] = $datay[$year - (1 - (int) floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
168 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
169 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
173 $fp = @fopen($newpathofdestfile,
'w');
175 fwrite($fp, json_encode($data));
178 dol_syslog(
"Failed to save cache file ".$newpathofdestfile, LOG_ERR);
182 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
210 global
$conf, $user, $langs;
212 if ($startyear > $endyear) {
220 if (!empty($cachedelay)) {
221 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
224 $newpathofdestfile =
$conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.
$conf->entity.
'_user'.$user->id.
'.cache';
231 if ($cachedelay > 0) {
233 if ($filedate >= ($nowgmt - $cachedelay)) {
236 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
238 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" cache file ".$newpathofdestfile.
" is not found or older than now - cachedelay (".$nowgmt.
" - ".$cachedelay.
") so we can't use it.");
243 if ($foundintocache) {
244 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
245 $data = json_decode(file_get_contents($newpathofdestfile),
true);
248 $sm = $startmonth - 1;
252 while ($year <= $endyear) {
258 for ($i = 0; $i < 12; $i++) {
259 $data[$i][] = isset($datay[$endyear][($i + $sm) % 12][
'label']) ? $datay[$endyear][($i + $sm) % 12][
'label'] : $datay[$endyear][($i + $sm) % 12][0];
261 while ($year <= $endyear) {
263 $data[$i][] = $datay[$year - (1 - (int) floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
270 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
271 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
275 $fp = @fopen($newpathofdestfile,
'w');
277 fwrite($fp, json_encode($data));
280 dol_syslog(
"Failed to save cache file ".$newpathofdestfile, LOG_ERR);
284 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
305 if ($startyear > $endyear) {
313 while ($year <= $endyear) {
318 for ($i = 0; $i < 12; $i++) {
319 $data[$i][] = $datay[$endyear][$i][0];
321 while ($year <= $endyear) {
322 $data[$i][] = $datay[$year][$i][1];
340 global
$conf, $user, $langs;
345 if (!empty($cachedelay)) {
346 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
349 $newpathofdestfile =
$conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.
$conf->entity.
'_user'.$user->id.
'.cache';
356 if ($cachedelay > 0) {
358 if ($filedate >= ($nowgmt - $cachedelay)) {
361 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
363 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" cache file ".$newpathofdestfile.
" is not found or older than now - cachedelay (".$nowgmt.
" - ".$cachedelay.
") so we can't use it.");
368 if ($foundintocache) {
369 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
370 $data = json_decode(file_get_contents($newpathofdestfile),
true);
371 '@phan-var-force array<int<0,11>,array{0:int<1,12>,1:int|float}> $data';
377 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
378 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
382 $fp = @fopen($newpathofdestfile,
'w');
384 fwrite($fp, json_encode($data));
387 dol_syslog(
"Failed to save cache file ".$newpathofdestfile, LOG_ERR);
391 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
413 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
414 $resql = $this->db->query($sql);
416 $num = $this->db->num_rows($resql);
419 $row = $this->db->fetch_row($resql);
423 $this->db->free($resql);
425 dol_print_error($this->db);
442 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
443 $resql = $this->db->query($sql);
445 $num = $this->db->num_rows($resql);
454 $row = $this->db->fetch_object($resql);
455 $result[$i][
'year'] = (string) $row->year;
456 $result[$i][
'nb'] = (int) $row->nb;
458 $hasprevyear = ($i > 0 && $prevyear == (int) $row->year + 1);
459 if ($hasprevyear && $row->nb > 0) {
460 $result[$i - 1][
'nb_diff'] = ($prevnb - $row->nb) / $row->nb * 100;
463 if (property_exists($row,
'total')) {
464 $result[$i][
'total'] = (float) $row->total;
465 if ($hasprevyear && $row->total > 0) {
466 $result[$i - 1][
'total_diff'] = ($prevtotal - $row->total) / $row->total * 100;
468 $prevtotal = (float) $row->total;
471 if (property_exists($row,
'total')) {
472 $result[$i][
'avg'] = (float) $row->avg;
473 if ($hasprevyear && $row->avg > 0) {
474 $result[$i - 1][
'avg_diff'] = ($prevavg - $row->avg) / $row->avg * 100;
476 $prevavg = (float) $row->avg;
479 if (property_exists($row,
'weighted')) {
480 $result[$i][
'weighted'] = (float) $row->weighted;
481 if ($hasprevyear && $row->weighted > 0) {
482 $result[$i - 1][
'avg_weighted'] = ($prevweighted - $row->weighted) / $row->weighted * 100;
484 $prevweighted = (float) $row->weighted;
486 $prevyear = (int) $row->year;
487 $prevnb = (int) $row->nb;
490 $this->db->free($resql);
492 dol_print_error($this->db);
514 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
515 $resql = $this->db->query($sql);
517 $num = $this->db->num_rows($resql);
521 $row = $this->db->fetch_row($resql);
523 $result[$j] = $row[1];
526 $this->db->free($resql);
528 dol_print_error($this->db);
531 for ($i = 1; $i < 13; $i++) {
532 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
537 for ($i = 1; $i < 13; $i++) {
540 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
541 } elseif ($format == 1) {
543 } elseif ($format == 2) {
544 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
548 $data[$i - 1] = array($month, $res[$i]);
571 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
573 $resql = $this->db->query($sql);
575 $num = $this->db->num_rows($resql);
578 $row = $this->db->fetch_row($resql);
580 $result[$j] = $row[1];
583 $this->db->free($resql);
585 dol_print_error($this->db);
588 for ($i = 1; $i < 13; $i++) {
589 $res[$i] = (int) round((isset($result[$i]) ? $result[$i] : 0));
594 for ($i = 1; $i < 13; $i++) {
597 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
598 } elseif ($format == 1) {
600 } elseif ($format == 2) {
601 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
605 $data[$i - 1] = array($month, $res[$i]);
628 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
629 $resql = $this->db->query($sql);
631 $num = $this->db->num_rows($resql);
635 $row = $this->db->fetch_row($resql);
637 $result[$j] = $row[1];
640 $this->db->free($resql);
642 dol_print_error($this->db);
645 for ($i = 1; $i < 13; $i++) {
646 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
651 for ($i = 1; $i < 13; $i++) {
654 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
655 } elseif ($format == 1) {
657 } elseif ($format == 2) {
658 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
662 $data[$i - 1] = array($month, $res[$i]);
684 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
685 $resql = $this->db->query($sql);
687 $num = $this->db->num_rows($resql);
691 $row = $this->db->fetch_row($resql);
692 if ($i < $limit || $num == $limit) {
693 $result[$i] = array($row[0], $row[1]);
700 $result[$i] = array($langs->transnoentitiesnoconv(
"Other"), $other);
702 $this->db->free($resql);
704 dol_print_error($this->db);
719 $resql = $this->db->query($sql);
721 $num = $this->db->num_rows($resql);
724 $row = $this->db->fetch_row($resql);
731 $this->db->free($resql);
747 $msg = get_class($this).
"::".__FUNCTION__.
" not implemented";
750 return array($l,$l,$l,$l,$l,$l,$l,$l,$l,$l,$l,$l);
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Parent class of statistics class.
getNbByMonth($year, $format=0)
getAllByProduct($year, $limit=0)
Return nb, amount of predefined product for year.
getAmountByMonth($year, $format=0)
_getAmountByYear($sql)
Returns the summed amounts per year for a given number of past years ending now.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
getAmountByMonthWithPrevYear($endyear, $startyear, $cachedelay=0, $format=0, $startmonth=1)
Return amount of elements by month for several years.
getNbByMonthWithPrevYear($endyear, $startyear, $cachedelay=0, $format=0, $startmonth=1)
Return nb of elements by month for several years.
getAllByProductEntry($year, $cachedelay=0, $limit=10)
Return count, and sum of products.
getAverageByMonthWithPrevYear($endyear, $startyear)
Return average of entity by month for several years.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getNbByMonth($year, $sql, $format=0)
Return number of documents per month for a given year.
dol_filemtime($pathoffile)
Return time of a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_now($mode='gmt')
Return date for now.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)