33 protected $lastfetchdate = array();
34 public $cachefilesuffix =
'';
41 protected abstract function getNbByMonth($year, $format = 0);
55 global $conf, $user, $langs;
57 if ($startyear > $endyear) {
64 if (!empty($cachedelay)) {
65 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
66 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
69 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
75 if ($cachedelay > 0) {
77 if ($filedate >= ($nowgmt - $cachedelay)) {
80 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
82 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.");
86 if ($foundintocache) {
87 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
88 $data = json_decode(file_get_contents($newpathofdestfile),
true);
91 $sm = $startmonth - 1;
95 while ($year <= $endyear) {
102 for ($i = 0; $i < 12; $i++) {
103 $data[$i][] = $datay[$endyear][($i + $sm) % 12][0];
105 while ($year <= $endyear) {
107 $data[$i][] = $datay[$year - (1 - floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
114 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
115 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
119 $fp = fopen($newpathofdestfile,
'w');
120 fwrite($fp, json_encode($data));
124 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
153 global $conf, $user, $langs;
155 if ($startyear > $endyear) {
162 if (!empty($cachedelay)) {
163 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
164 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
167 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
173 if ($cachedelay > 0) {
175 if ($filedate >= ($nowgmt - $cachedelay)) {
178 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
180 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.");
185 if ($foundintocache) {
186 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
187 $data = json_decode(file_get_contents($newpathofdestfile),
true);
190 $sm = $startmonth - 1;
194 while ($year <= $endyear) {
201 for ($i = 0; $i < 12; $i++) {
202 $data[$i][] = isset($datay[$endyear][($i + $sm) % 12][
'label']) ? $datay[$endyear][($i + $sm) % 12][
'label'] : $datay[$endyear][($i + $sm) % 12][0];
204 while ($year <= $endyear) {
206 $data[$i][] = $datay[$year - (1 - floor(($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
213 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
214 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
218 $fp = fopen($newpathofdestfile,
'w');
220 fwrite($fp, json_encode($data));
224 dol_syslog(
"Failed to write cache file", LOG_ERR);
226 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
247 if ($startyear > $endyear) {
254 while ($year <= $endyear) {
261 for ($i = 0; $i < 12; $i++) {
262 $data[$i][] = $datay[$endyear][$i][0];
264 while ($year <= $endyear) {
265 $data[$i][] = $datay[$year][$i][1];
283 global $conf, $user, $langs;
288 if (!empty($cachedelay)) {
289 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
290 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
293 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
299 if ($cachedelay > 0) {
301 if ($filedate >= ($nowgmt - $cachedelay)) {
304 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
306 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.");
311 if ($foundintocache) {
312 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
313 $data = json_decode(file_get_contents($newpathofdestfile),
true);
315 $data = $this->getAllByProduct($year, $limit);
320 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
321 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
325 $fp = fopen($newpathofdestfile,
'w');
327 fwrite($fp, json_encode($data));
331 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
353 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
354 $resql = $this->db->query(
$sql);
356 $num = $this->db->num_rows($resql);
359 $row = $this->db->fetch_row($resql);
363 $this->db->free($resql);
382 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
383 $resql = $this->db->query(
$sql);
385 $num = $this->db->num_rows($resql);
388 $row = $this->db->fetch_object($resql);
389 $result[$i][
'year'] = $row->year;
390 $result[$i][
'nb'] = $row->nb;
391 if ($i > 0 && $row->nb > 0) {
392 $result[$i - 1][
'nb_diff'] = ($result[$i - 1][
'nb'] - $row->nb) / $row->nb * 100;
395 if (property_exists($row,
'total')) {
396 $result[$i][
'total'] = $row->total;
397 if ($i > 0 && $row->total > 0) {
398 $result[$i - 1][
'total_diff'] = ($result[$i - 1][
'total'] - $row->total) / $row->total * 100;
402 if (property_exists($row,
'total')) {
403 $result[$i][
'avg'] = $row->avg;
404 if ($i > 0 && $row->avg > 0) {
405 $result[$i - 1][
'avg_diff'] = ($result[$i - 1][
'avg'] - $row->avg) / $row->avg * 100;
409 if (property_exists($row,
'weighted')) {
410 $result[$i][
'weighted'] = $row->weighted;
411 if ($i > 0 && $row->weighted > 0) {
412 $result[$i - 1][
'avg_weighted'] = ($result[$i - 1][
'weighted'] - $row->weighted) / $row->weighted * 100;
417 $this->db->free($resql);
442 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
443 $resql = $this->db->query(
$sql);
445 $num = $this->db->num_rows($resql);
449 $row = $this->db->fetch_row($resql);
451 $result[$j] = $row[1];
454 $this->db->free($resql);
459 for ($i = 1; $i < 13; $i++) {
460 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
465 for ($i = 1; $i < 13; $i++) {
468 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
469 } elseif ($format == 1) {
471 } elseif ($format == 2) {
472 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
476 $data[$i - 1] = array($month, $res[$i]);
499 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
501 $resql = $this->db->query(
$sql);
503 $num = $this->db->num_rows($resql);
506 $row = $this->db->fetch_row($resql);
508 $result[$j] = $row[1];
511 $this->db->free($resql);
516 for ($i = 1; $i < 13; $i++) {
517 $res[$i] = (int) round((isset($result[$i]) ? $result[$i] : 0));
522 for ($i = 1; $i < 13; $i++) {
525 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
526 } elseif ($format == 1) {
528 } elseif ($format == 2) {
529 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
533 $data[$i - 1] = array($month, $res[$i]);
557 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
558 $resql = $this->db->query(
$sql);
560 $num = $this->db->num_rows($resql);
564 $row = $this->db->fetch_row($resql);
566 $result[$j] = $row[1];
569 $this->db->free($resql);
574 for ($i = 1; $i < 13; $i++) {
575 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
580 for ($i = 1; $i < 13; $i++) {
583 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
584 } elseif ($format == 1) {
586 } elseif ($format == 2) {
587 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
591 $data[$i - 1] = array($month, $res[$i]);
613 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
614 $resql = $this->db->query(
$sql);
616 $num = $this->db->num_rows($resql);
620 $row = $this->db->fetch_row($resql);
621 if ($i < $limit || $num == $limit) {
622 $result[$i] = array($row[0], $row[1]);
629 $result[$i] = array($langs->transnoentitiesnoconv(
"Other"), $other);
631 $this->db->free($resql);
648 $resql = $this->db->query(
$sql);
650 $num = $this->db->num_rows($resql);
653 $row = $this->db->fetch_row($resql);
661 $this->db->free($resql);