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;
394 $result[$i][
'total'] = $row->total;
395 if ($i > 0 && $row->total > 0) {
396 $result[$i - 1][
'total_diff'] = ($result[$i - 1][
'total'] - $row->total) / $row->total * 100;
398 $result[$i][
'avg'] = $row->avg;
399 if ($i > 0 && $row->avg > 0) {
400 $result[$i - 1][
'avg_diff'] = ($result[$i - 1][
'avg'] - $row->avg) / $row->avg * 100;
403 if (isset($row->weighted)) {
404 $result[$i][
'weighted'] = $row->weighted;
405 if ($i > 0 && $row->weighted > 0) {
406 $result[$i - 1][
'avg_weighted'] = ($result[$i - 1][
'weighted'] - $row->weighted) / $row->weighted * 100;
411 $this->
db->free($resql);
436 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
437 $resql = $this->
db->query(
$sql);
439 $num = $this->
db->num_rows($resql);
443 $row = $this->
db->fetch_row($resql);
445 $result[$j] = $row[1];
448 $this->
db->free($resql);
453 for ($i = 1; $i < 13; $i++) {
454 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
459 for ($i = 1; $i < 13; $i++) {
462 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
463 } elseif ($format == 1) {
465 } elseif ($format == 2) {
466 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
470 $data[$i - 1] = array($month, $res[$i]);
493 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
495 $resql = $this->
db->query(
$sql);
497 $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] = (int) round((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]);
551 dol_syslog(get_class($this).
'::'.__FUNCTION__, LOG_DEBUG);
552 $resql = $this->
db->query(
$sql);
554 $num = $this->
db->num_rows($resql);
558 $row = $this->
db->fetch_row($resql);
560 $result[$j] = $row[1];
563 $this->
db->free($resql);
568 for ($i = 1; $i < 13; $i++) {
569 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
574 for ($i = 1; $i < 13; $i++) {
577 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
578 } elseif ($format == 1) {
580 } elseif ($format == 2) {
581 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
585 $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);
615 if ($i < $limit || $num == $limit) {
616 $result[$i] = array($row[0], $row[1]);
623 $result[$i] = array($langs->transnoentitiesnoconv(
"Other"), $other);
625 $this->
db->free($resql);
642 $resql = $this->
db->query(
$sql);
644 $num = $this->
db->num_rows($resql);
647 $row = $this->
db->fetch_row($resql);
655 $this->
db->free($resql);