34 protected $lastfetchdate = array();
35 public $cachefilesuffix =
'';
97 global $conf, $user, $langs;
99 if ($startyear > $endyear) {
107 if (!empty($cachedelay)) {
108 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
109 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
112 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
118 if ($cachedelay > 0) {
120 if ($filedate >= ($nowgmt - $cachedelay)) {
123 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
125 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.");
129 if ($foundintocache) {
130 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
131 $data = json_decode(file_get_contents($newpathofdestfile),
true);
132 '@phan-var-force array<int<0,11>,array{0:int<1,12>,1:int}> $data';
135 $sm = $startmonth - 1;
139 while ($year <= $endyear) {
144 for ($i = 0; $i < 12; $i++) {
145 $data[$i][] = $datay[$endyear][($i + $sm) % 12][0];
147 while ($year <= $endyear) {
149 $data[$i][] = $datay[$year - (1 - (int) floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
157 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
158 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
162 $fp = @fopen($newpathofdestfile,
'w');
164 fwrite($fp, json_encode($data));
167 dol_syslog(
"Failed to save cache file ".$newpathofdestfile, LOG_ERR);
171 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
199 global $conf, $user, $langs;
201 if ($startyear > $endyear) {
209 if (!empty($cachedelay)) {
210 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
211 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
214 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
220 if ($cachedelay > 0) {
222 if ($filedate >= ($nowgmt - $cachedelay)) {
225 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
227 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.");
232 if ($foundintocache) {
233 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
234 $data = json_decode(file_get_contents($newpathofdestfile),
true);
237 $sm = $startmonth - 1;
241 while ($year <= $endyear) {
247 for ($i = 0; $i < 12; $i++) {
248 $data[$i][] = isset($datay[$endyear][($i + $sm) % 12][
'label']) ? $datay[$endyear][($i + $sm) % 12][
'label'] : $datay[$endyear][($i + $sm) % 12][0];
250 while ($year <= $endyear) {
252 $data[$i][] = $datay[$year - (1 - floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
259 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
260 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
264 $fp = @fopen($newpathofdestfile,
'w');
266 fwrite($fp, json_encode($data));
269 dol_syslog(
"Failed to save cache file ".$newpathofdestfile, LOG_ERR);
273 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
294 if ($startyear > $endyear) {
302 while ($year <= $endyear) {
307 for ($i = 0; $i < 12; $i++) {
308 $data[$i][] = $datay[$endyear][$i][0];
310 while ($year <= $endyear) {
311 $data[$i][] = $datay[$year][$i][1];
329 global $conf, $user, $langs;
334 if (!empty($cachedelay)) {
335 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
336 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
339 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
345 if ($cachedelay > 0) {
347 if ($filedate >= ($nowgmt - $cachedelay)) {
350 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
352 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.");
357 if ($foundintocache) {
358 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
359 $data = json_decode(file_get_contents($newpathofdestfile),
true);
360 '@phan-var-force array<int<0,11>,array{0:int<1,12>,1:int|float}> $data';
364 $data = $this->getAllByProduct($year, $limit);
368 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
369 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
373 $fp = @fopen($newpathofdestfile,
'w');
375 fwrite($fp, json_encode($data));
378 dol_syslog(
"Failed to save cache file ".$newpathofdestfile, LOG_ERR);
382 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
404 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
405 $resql = $this->db->query($sql);
407 $num = $this->db->num_rows($resql);
410 $row = $this->db->fetch_row($resql);
414 $this->db->free($resql);
433 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
434 $resql = $this->db->query($sql);
436 $num = $this->db->num_rows($resql);
439 $row = $this->db->fetch_object($resql);
440 $result[$i][
'year'] = $row->year;
441 $result[$i][
'nb'] = $row->nb;
442 if ($i > 0 && $row->nb > 0) {
443 $result[$i - 1][
'nb_diff'] = ($result[$i - 1][
'nb'] - $row->nb) / $row->nb * 100;
446 if (property_exists($row,
'total')) {
447 $result[$i][
'total'] = $row->total;
448 if ($i > 0 && $row->total > 0) {
449 $result[$i - 1][
'total_diff'] = ($result[$i - 1][
'total'] - $row->total) / $row->total * 100;
453 if (property_exists($row,
'total')) {
454 $result[$i][
'avg'] = $row->avg;
455 if ($i > 0 && $row->avg > 0) {
456 $result[$i - 1][
'avg_diff'] = ($result[$i - 1][
'avg'] - $row->avg) / $row->avg * 100;
460 if (property_exists($row,
'weighted')) {
461 $result[$i][
'weighted'] = $row->weighted;
462 if ($i > 0 && $row->weighted > 0) {
463 $result[$i - 1][
'avg_weighted'] = ($result[$i - 1][
'weighted'] - $row->weighted) / $row->weighted * 100;
468 $this->db->free($resql);
493 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
494 $resql = $this->db->query($sql);
496 $num = $this->db->num_rows($resql);
500 $row = $this->db->fetch_row($resql);
502 $result[$j] = $row[1];
505 $this->db->free($resql);
510 for ($i = 1; $i < 13; $i++) {
511 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
516 for ($i = 1; $i < 13; $i++) {
519 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
520 } elseif ($format == 1) {
522 } elseif ($format == 2) {
523 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
527 $data[$i - 1] = array($month, $res[$i]);
550 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
552 $resql = $this->db->query($sql);
554 $num = $this->db->num_rows($resql);
557 $row = $this->db->fetch_row($resql);
559 $result[$j] = $row[1];
562 $this->db->free($resql);
567 for ($i = 1; $i < 13; $i++) {
568 $res[$i] = (int) round((isset($result[$i]) ? $result[$i] : 0));
573 for ($i = 1; $i < 13; $i++) {
576 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
577 } elseif ($format == 1) {
579 } elseif ($format == 2) {
580 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
584 $data[$i - 1] = array($month, $res[$i]);
607 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
608 $resql = $this->db->query($sql);
610 $num = $this->db->num_rows($resql);
614 $row = $this->db->fetch_row($resql);
616 $result[$j] = $row[1];
619 $this->db->free($resql);
624 for ($i = 1; $i < 13; $i++) {
625 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
630 for ($i = 1; $i < 13; $i++) {
633 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
634 } elseif ($format == 1) {
636 } elseif ($format == 2) {
637 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
641 $data[$i - 1] = array($month, $res[$i]);
663 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
664 $resql = $this->db->query($sql);
666 $num = $this->db->num_rows($resql);
670 $row = $this->db->fetch_row($resql);
671 if ($i < $limit || $num == $limit) {
672 $result[$i] = array($row[0], $row[1]);
679 $result[$i] = array($langs->transnoentitiesnoconv(
"Other"), $other);
681 $this->db->free($resql);
698 $resql = $this->db->query($sql);
700 $num = $this->db->num_rows($resql);
703 $row = $this->db->fetch_row($resql);
710 $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)
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)
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.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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)