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) {
106 $data[$i][] = $datay[$year - (1 - ((int) ($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
113 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
114 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
118 $fp = fopen($newpathofdestfile,
'w');
119 fwrite($fp, json_encode($data));
121 if (!empty($conf->global->MAIN_UMASK)) {
122 $newmask = $conf->global->MAIN_UMASK;
124 @chmod($newpathofdestfile, octdec($newmask));
126 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
155 global $conf, $user, $langs;
157 if ($startyear > $endyear) {
164 if (!empty($cachedelay)) {
165 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
166 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
169 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
175 if ($cachedelay > 0) {
177 if ($filedate >= ($nowgmt - $cachedelay)) {
180 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
182 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.");
187 if ($foundintocache) {
188 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
189 $data = json_decode(file_get_contents($newpathofdestfile),
true);
192 $sm = $startmonth - 1;
196 while ($year <= $endyear) {
203 for ($i = 0; $i < 12; $i++) {
204 $data[$i][] = isset($datay[$endyear][($i + $sm) % 12][
'label']) ? $datay[$endyear][($i + $sm) % 12][
'label'] : $datay[$endyear][($i + $sm) % 12][0];
206 while ($year <= $endyear) {
207 $data[$i][] = $datay[$year - (1 - ((int) ($i + $sm) / 12)) + ($sm == 0 ? 1 : 0)][($i + $sm) % 12][1];
214 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
215 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
219 $fp = fopen($newpathofdestfile,
'w');
221 fwrite($fp, json_encode($data));
223 if (!empty($conf->global->MAIN_UMASK)) {
224 $newmask = $conf->global->MAIN_UMASK;
226 @chmod($newpathofdestfile, octdec($newmask));
228 dol_syslog(
"Failed to write cache file", LOG_ERR);
230 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
251 if ($startyear > $endyear) {
258 while ($year <= $endyear) {
265 for ($i = 0; $i < 12; $i++) {
266 $data[$i][] = $datay[$endyear][$i][0];
268 while ($year <= $endyear) {
269 $data[$i][] = $datay[$year][$i][1];
287 global $conf, $user, $langs;
292 if (!empty($cachedelay)) {
293 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
294 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
297 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_entity.'.$conf->entity.
'_user'.$user->id.
'.cache';
303 if ($cachedelay > 0) {
305 if ($filedate >= ($nowgmt - $cachedelay)) {
308 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
310 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.");
315 if ($foundintocache) {
316 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
317 $data = json_decode(file_get_contents($newpathofdestfile),
true);
319 $data = $this->getAllByProduct($year, $limit);
324 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
325 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
329 $fp = fopen($newpathofdestfile,
'w');
331 fwrite($fp, json_encode($data));
333 if (!empty($conf->global->MAIN_UMASK)) {
334 $newmask = $conf->global->MAIN_UMASK;
336 @chmod($newpathofdestfile, octdec($newmask));
338 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
360 dol_syslog(get_class($this).
'::'.__FUNCTION__.
"", LOG_DEBUG);
366 $row = $this->
db->fetch_row(
$resql);
389 dol_syslog(get_class($this).
'::'.__FUNCTION__.
"", LOG_DEBUG);
395 $row = $this->
db->fetch_object(
$resql);
396 $result[$i][
'year'] = $row->year;
397 $result[$i][
'nb'] = $row->nb;
398 if ($i > 0 && $row->nb > 0) {
399 $result[$i - 1][
'nb_diff'] = ($result[$i - 1][
'nb'] - $row->nb) / $row->nb * 100;
401 $result[$i][
'total'] = $row->total;
402 if ($i > 0 && $row->total > 0) {
403 $result[$i - 1][
'total_diff'] = ($result[$i - 1][
'total'] - $row->total) / $row->total * 100;
405 $result[$i][
'avg'] = $row->avg;
406 if ($i > 0 && $row->avg > 0) {
407 $result[$i - 1][
'avg_diff'] = ($result[$i - 1][
'avg'] - $row->avg) / $row->avg * 100;
410 if (isset($row->weighted)) {
411 $result[$i][
'weighted'] = $row->weighted;
412 if ($i > 0 && $row->weighted > 0) {
413 $result[$i - 1][
'avg_weighted'] = ($result[$i - 1][
'weighted'] - $row->weighted) / $row->weighted * 100;
443 dol_syslog(get_class($this).
'::'.__FUNCTION__.
"", LOG_DEBUG);
450 $row = $this->
db->fetch_row(
$resql);
452 $result[$j] = $row[1];
460 for ($i = 1; $i < 13; $i++) {
461 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
466 for ($i = 1; $i < 13; $i++) {
469 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
470 } elseif ($format == 1) {
472 } elseif ($format == 2) {
473 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
477 $data[$i - 1] = array($month, $res[$i]);
500 dol_syslog(get_class($this).
'::'.__FUNCTION__.
"", LOG_DEBUG);
507 $row = $this->
db->fetch_row(
$resql);
509 $result[$j] = $row[1];
517 for ($i = 1; $i < 13; $i++) {
518 $res[$i] = (int) round((isset($result[$i]) ? $result[$i] : 0));
523 for ($i = 1; $i < 13; $i++) {
526 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
527 } elseif ($format == 1) {
529 } elseif ($format == 2) {
530 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
534 $data[$i - 1] = array($month, $res[$i]);
558 dol_syslog(get_class($this).
'::'.__FUNCTION__.
"", LOG_DEBUG);
565 $row = $this->
db->fetch_row(
$resql);
567 $result[$j] = $row[1];
575 for ($i = 1; $i < 13; $i++) {
576 $res[$i] = (isset($result[$i]) ? $result[$i] : 0);
581 for ($i = 1; $i < 13; $i++) {
584 $month = $langs->transnoentitiesnoconv(
'MonthShort'.sprintf(
"%02d", $i));
585 } elseif ($format == 1) {
587 } elseif ($format == 2) {
588 $month = $langs->transnoentitiesnoconv(
'MonthVeryShort'.sprintf(
"%02d", $i));
592 $data[$i - 1] = array($month, $res[$i]);
614 dol_syslog(get_class($this).
'::'.__FUNCTION__.
"", LOG_DEBUG);
621 $row = $this->
db->fetch_row(
$resql);
622 if ($i < $limit || $num == $limit) {
623 $result[$i] = array($row[0], $row[1]);
630 $result[$i] = array($langs->transnoentitiesnoconv(
"Other"), $other);
654 $row = $this->
db->fetch_row(
$resql);