33 protected $lastfetchdate = array();
34 public $cachefilesuffix =
'';
96 global $conf, $user, $langs;
98 if ($startyear > $endyear) {
105 if (!empty($cachedelay)) {
106 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
107 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
110 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
116 if ($cachedelay > 0) {
118 if ($filedate >= ($nowgmt - $cachedelay)) {
121 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
123 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.");
127 if ($foundintocache) {
128 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
129 $data = json_decode(file_get_contents($newpathofdestfile),
true);
132 $sm = $startmonth - 1;
136 while ($year <= $endyear) {
143 for ($i = 0; $i < 12; $i++) {
144 $data[$i][] = $datay[$endyear][($i + $sm) % 12][0];
146 while ($year <= $endyear) {
148 $data[$i][] = $datay[$year - (1 - floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
155 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
156 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
160 $fp = fopen($newpathofdestfile,
'w');
162 fwrite($fp, json_encode($data));
165 dol_syslog(
"Failed to save cache file ".$newpathofdestfile);
169 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
198 global $conf, $user, $langs;
200 if ($startyear > $endyear) {
207 if (!empty($cachedelay)) {
208 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
209 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
212 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
218 if ($cachedelay > 0) {
220 if ($filedate >= ($nowgmt - $cachedelay)) {
223 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
225 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.");
230 if ($foundintocache) {
231 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
232 $data = json_decode(file_get_contents($newpathofdestfile),
true);
235 $sm = $startmonth - 1;
239 while ($year <= $endyear) {
246 for ($i = 0; $i < 12; $i++) {
247 $data[$i][] = isset($datay[$endyear][($i + $sm) % 12][
'label']) ? $datay[$endyear][($i + $sm) % 12][
'label'] : $datay[$endyear][($i + $sm) % 12][0];
249 while ($year <= $endyear) {
251 $data[$i][] = $datay[$year - (1 - floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
258 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
259 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
263 $fp = fopen($newpathofdestfile,
'w');
265 fwrite($fp, json_encode($data));
269 dol_syslog(
"Failed to write cache file", LOG_ERR);
271 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
292 if ($startyear > $endyear) {
299 while ($year <= $endyear) {
306 for ($i = 0; $i < 12; $i++) {
307 $data[$i][] = $datay[$endyear][$i][0];
309 while ($year <= $endyear) {
310 $data[$i][] = $datay[$year][$i][1];
328 global $conf, $user, $langs;
333 if (!empty($cachedelay)) {
334 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
335 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
338 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
344 if ($cachedelay > 0) {
346 if ($filedate >= ($nowgmt - $cachedelay)) {
349 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
351 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.");
356 if ($foundintocache) {
357 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
358 $data = json_decode(file_get_contents($newpathofdestfile),
true);
362 $data = $this->getAllByProduct($year, $limit);
366 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
367 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
371 $fp = fopen($newpathofdestfile,
'w');
373 fwrite($fp, json_encode($data));
377 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
399 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
400 $resql = $this->db->query($sql);
402 $num = $this->db->num_rows($resql);
405 $row = $this->db->fetch_row($resql);
409 $this->db->free($resql);
428 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
429 $resql = $this->db->query($sql);
431 $num = $this->db->num_rows($resql);
434 $row = $this->db->fetch_object($resql);
435 $result[$i][
'year'] = $row->year;
436 $result[$i][
'nb'] = $row->nb;
437 if ($i > 0 && $row->nb > 0) {
438 $result[$i - 1][
'nb_diff'] = ($result[$i - 1][
'nb'] - $row->nb) / $row->nb * 100;
441 if (property_exists($row,
'total')) {
442 $result[$i][
'total'] = $row->total;
443 if ($i > 0 && $row->total > 0) {
444 $result[$i - 1][
'total_diff'] = ($result[$i - 1][
'total'] - $row->total) / $row->total * 100;
448 if (property_exists($row,
'total')) {
449 $result[$i][
'avg'] = $row->avg;
450 if ($i > 0 && $row->avg > 0) {
451 $result[$i - 1][
'avg_diff'] = ($result[$i - 1][
'avg'] - $row->avg) / $row->avg * 100;
455 if (property_exists($row,
'weighted')) {
456 $result[$i][
'weighted'] = $row->weighted;
457 if ($i > 0 && $row->weighted > 0) {
458 $result[$i - 1][
'avg_weighted'] = ($result[$i - 1][
'weighted'] - $row->weighted) / $row->weighted * 100;
463 $this->db->free($resql);
488 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
489 $resql = $this->db->query($sql);
491 $num = $this->db->num_rows($resql);
495 $row = $this->db->fetch_row($resql);
497 $result[$j] = $row[1];
500 $this->db->free($resql);
505 for ($i = 1; $i < 13; $i++) {
506 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
511 for ($i = 1; $i < 13; $i++) {
514 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
515 } elseif ($format == 1) {
517 } elseif ($format == 2) {
518 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
522 $data[$i - 1] = array($month, $res[$i]);
545 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
547 $resql = $this->db->query($sql);
549 $num = $this->db->num_rows($resql);
552 $row = $this->db->fetch_row($resql);
554 $result[$j] = $row[1];
557 $this->db->free($resql);
562 for ($i = 1; $i < 13; $i++) {
563 $res[$i] = (int) round((isset($result[$i]) ? $result[$i] : 0));
568 for ($i = 1; $i < 13; $i++) {
571 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
572 } elseif ($format == 1) {
574 } elseif ($format == 2) {
575 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
579 $data[$i - 1] = array($month, $res[$i]);
603 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
604 $resql = $this->db->query($sql);
606 $num = $this->db->num_rows($resql);
610 $row = $this->db->fetch_row($resql);
612 $result[$j] = $row[1];
615 $this->db->free($resql);
620 for ($i = 1; $i < 13; $i++) {
621 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
626 for ($i = 1; $i < 13; $i++) {
629 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
630 } elseif ($format == 1) {
632 } elseif ($format == 2) {
633 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
637 $data[$i - 1] = array($month, $res[$i]);
659 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
660 $resql = $this->db->query($sql);
662 $num = $this->db->num_rows($resql);
666 $row = $this->db->fetch_row($resql);
667 if ($i < $limit || $num == $limit) {
668 $result[$i] = array($row[0], $row[1]);
675 $result[$i] = array($langs->transnoentitiesnoconv(
"Other"), $other);
677 $this->db->free($resql);
694 $resql = $this->db->query($sql);
696 $num = $this->db->num_rows($resql);
699 $row = $this->db->fetch_row($resql);
707 $this->db->free($resql);
Parent class of statistics class.
getNbByMonth($year, $format=0)
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)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
_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)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_filemtime($pathoffile)
Return time of a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
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)