| Total Complexity | 70 |
| Total Lines | 522 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProjectStats often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProjectStats, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class ProjectStats extends Stats |
||
| 33 | { |
||
| 34 | private $project; |
||
| 35 | public $userid; |
||
| 36 | public $socid; |
||
| 37 | public $status; |
||
| 38 | public $opp_status; |
||
| 39 | |||
| 40 | //SQL stat |
||
| 41 | public $field; |
||
| 42 | public $from; |
||
| 43 | public $where; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * |
||
| 49 | * @param DoliDB $db Database handler |
||
|
|
|||
| 50 | */ |
||
| 51 | public function __construct($db) |
||
| 52 | { |
||
| 53 | global $conf, $user; |
||
| 54 | |||
| 55 | $this->db = $db; |
||
| 56 | |||
| 57 | require_once 'project.class.php'; |
||
| 58 | $this->project = new Project($this->db); |
||
| 59 | |||
| 60 | $this->from = MAIN_DB_PREFIX . $this->project->table_element; |
||
| 61 | $this->field = 'opp_amount'; |
||
| 62 | $this->where = " entity = " . $conf->entity; |
||
| 63 | if ($this->socid > 0) { |
||
| 64 | $this->where .= " AND fk_soc = " . ((int) $this->socid); |
||
| 65 | } |
||
| 66 | if (is_array($this->userid) && count($this->userid) > 0) { |
||
| 67 | $this->where .= ' AND fk_user IN (' . $this->db->sanitize(implode(',', $this->userid)) . ')'; |
||
| 68 | } elseif ($this->userid > 0) { |
||
| 69 | $this->where .= " AND fk_user = " . ((int) $this->userid); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Return all leads grouped by opportunity status. |
||
| 76 | * Warning: There is no filter on WON/LOST because we want this for statistics. |
||
| 77 | * |
||
| 78 | * @param int $limit Limit results |
||
| 79 | * @return array|int Array with value or -1 if error |
||
| 80 | * @throws Exception |
||
| 81 | */ |
||
| 82 | public function getAllProjectByStatus($limit = 5) |
||
| 83 | { |
||
| 84 | global $conf, $user, $langs; |
||
| 85 | |||
| 86 | $datay = array(); |
||
| 87 | |||
| 88 | $sql = "SELECT"; |
||
| 89 | $sql .= " SUM(t.opp_amount), t.fk_opp_status, cls.code, cls.label"; |
||
| 90 | $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t"; |
||
| 91 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 92 | //if (! $user->rights->societe->client->voir && ! $user->socid) |
||
| 93 | // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id); |
||
| 94 | $sql .= ", " . MAIN_DB_PREFIX . "c_lead_status as cls"; |
||
| 95 | $sql .= $this->buildWhere(); |
||
| 96 | // For external user, no check is done on company permission because readability is managed by public status of project and assignment. |
||
| 97 | //if ($socid > 0) $sql.= " AND t.fk_soc = ".((int) $socid); |
||
| 98 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 99 | //if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))"; |
||
| 100 | $sql .= " AND t.fk_opp_status = cls.rowid"; |
||
| 101 | $sql .= " AND t.fk_statut <> 0"; // We want historic also, so all projects not draft |
||
| 102 | $sql .= " GROUP BY t.fk_opp_status, cls.code, cls.label"; |
||
| 103 | |||
| 104 | $result = array(); |
||
| 105 | |||
| 106 | dol_syslog(get_class($this) . '::' . __METHOD__, LOG_DEBUG); |
||
| 107 | $resql = $this->db->query($sql); |
||
| 108 | if ($resql) { |
||
| 109 | $num = $this->db->num_rows($resql); |
||
| 110 | $i = 0; |
||
| 111 | $other = 0; |
||
| 112 | while ($i < $num) { |
||
| 113 | $row = $this->db->fetch_row($resql); |
||
| 114 | if ($i < $limit || $num == $limit) { |
||
| 115 | $label = (($langs->trans("OppStatus" . $row[2]) != "OppStatus" . $row[2]) ? $langs->trans("OppStatus" . $row[2]) : $row[2]); |
||
| 116 | $result[$i] = array( |
||
| 117 | $label . ' (' . price(price2num($row[0], 'MT'), 1, $langs, 1, -1, -1, $conf->currency) . ')', |
||
| 118 | $row[0] |
||
| 119 | ); |
||
| 120 | } else { |
||
| 121 | $other += $row[1]; |
||
| 122 | } |
||
| 123 | $i++; |
||
| 124 | } |
||
| 125 | if ($num > $limit) { |
||
| 126 | $result[$i] = array( |
||
| 127 | $langs->transnoentitiesnoconv("Other"), |
||
| 128 | $other |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | $this->db->free($resql); |
||
| 132 | } else { |
||
| 133 | $this->error = "Error " . $this->db->lasterror(); |
||
| 134 | dol_syslog(get_class($this) . '::' . __METHOD__ . ' ' . $this->error, LOG_ERR); |
||
| 135 | return -1; |
||
| 136 | } |
||
| 137 | |||
| 138 | return $result; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return count, and sum of products |
||
| 143 | * |
||
| 144 | * @return array of values |
||
| 145 | */ |
||
| 146 | public function getAllByYear() |
||
| 147 | { |
||
| 148 | global $conf, $user, $langs; |
||
| 149 | |||
| 150 | $datay = array(); |
||
| 151 | |||
| 152 | $wonlostfilter = 0; // No filter on status WON/LOST |
||
| 153 | |||
| 154 | $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb, SUM(t.opp_amount) as total, AVG(t.opp_amount) as avg,"; |
||
| 155 | $sql .= " SUM(t.opp_amount * " . $this->db->ifsql("t.opp_percent IS NULL" . ($wonlostfilter ? " OR cls.code IN ('WON','LOST')" : ""), '0', 't.opp_percent') . " / 100) as weighted"; |
||
| 156 | $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t LEFT JOIN " . MAIN_DB_PREFIX . "c_lead_status as cls ON cls.rowid = t.fk_opp_status"; |
||
| 157 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 158 | //if (! $user->rights->societe->client->voir && ! $user->soc_id) |
||
| 159 | // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id); |
||
| 160 | $sql .= $this->buildWhere(); |
||
| 161 | // For external user, no check is done on company permission because readability is managed by public status of project and assignment. |
||
| 162 | //if ($socid > 0) $sql.= " AND t.fk_soc = ".((int) $socid); |
||
| 163 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 164 | //if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))"; |
||
| 165 | $sql .= " GROUP BY year"; |
||
| 166 | $sql .= $this->db->order('year', 'DESC'); |
||
| 167 | |||
| 168 | return $this->_getAllByYear($sql); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Build the where part |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function buildWhere() |
||
| 178 | { |
||
| 179 | global $user; |
||
| 180 | |||
| 181 | $sqlwhere_str = ''; |
||
| 182 | $sqlwhere = array(); |
||
| 183 | |||
| 184 | // Get list of project id allowed to user (in a string list separated by coma) |
||
| 185 | $object = new Project($this->db); |
||
| 186 | $projectsListId = ''; |
||
| 187 | if (!$user->hasRight('projet', 'all', 'lire')) { |
||
| 188 | $projectsListId = $object->getProjectsAuthorizedForUser($user, 0, 1, $user->socid); |
||
| 189 | } |
||
| 190 | |||
| 191 | $sqlwhere[] = ' t.entity IN (' . getEntity('project') . ')'; |
||
| 192 | |||
| 193 | if (!empty($this->userid)) { |
||
| 194 | $sqlwhere[] = ' t.fk_user_resp = ' . ((int) $this->userid); |
||
| 195 | } |
||
| 196 | |||
| 197 | // Forced filter on socid is similar to forced filter on project. TODO Use project assignment to allow to not use filter on project |
||
| 198 | if (!empty($this->socid)) { |
||
| 199 | $sqlwhere[] = ' t.fk_soc = ' . ((int) $this->socid); |
||
| 200 | } |
||
| 201 | if (!empty($this->year) && empty($this->month)) { |
||
| 202 | $sqlwhere[] = " t.datec BETWEEN '" . $this->db->idate(dol_get_first_day($this->year, 1)) . "' AND '" . $this->db->idate(dol_get_last_day($this->year, 12)) . "'"; |
||
| 203 | } |
||
| 204 | if (!empty($this->year) && !empty($this->month)) { |
||
| 205 | $sqlwhere[] = " t.datec BETWEEN '" . $this->db->idate(dol_get_first_day($this->year, $this->month)) . "' AND '" . $this->db->idate(dol_get_last_day($this->year, $this->month)) . "'"; |
||
| 206 | } |
||
| 207 | |||
| 208 | if (!empty($this->status)) { |
||
| 209 | $sqlwhere[] = " t.fk_statut IN (" . $this->db->sanitize($this->status) . ")"; |
||
| 210 | } |
||
| 211 | |||
| 212 | if (!empty($this->opp_status)) { |
||
| 213 | if (is_numeric($this->opp_status) && $this->opp_status > 0) { |
||
| 214 | $sqlwhere[] = " t.fk_opp_status = " . ((int) $this->opp_status); |
||
| 215 | } |
||
| 216 | if ($this->opp_status == 'all') { |
||
| 217 | $sqlwhere[] = " (t.fk_opp_status IS NOT NULL AND t.fk_opp_status <> -1)"; |
||
| 218 | } |
||
| 219 | if ($this->opp_status == 'openedopp') { |
||
| 220 | $sqlwhere[] = " (t.fk_opp_status IS NOT NULL AND t.fk_opp_status <> -1 AND t.fk_opp_status NOT IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "c_lead_status WHERE code IN ('WON','LOST')))"; |
||
| 221 | } |
||
| 222 | if ($this->opp_status == 'notopenedopp') { |
||
| 223 | $sqlwhere[] = " (t.fk_opp_status IS NULL OR t.fk_opp_status = -1 OR t.fk_opp_status IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "c_lead_status WHERE code = 'WON'))"; |
||
| 224 | } |
||
| 225 | if ($this->opp_status == 'none') { |
||
| 226 | $sqlwhere[] = " (t.fk_opp_status IS NULL OR t.fk_opp_status = -1)"; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | if (!$user->hasRight('projet', 'all', 'lire')) { |
||
| 231 | $sqlwhere[] = " t.rowid IN (" . $this->db->sanitize($projectsListId) . ")"; // public and assigned to, or restricted to company for external users |
||
| 232 | } |
||
| 233 | |||
| 234 | if (count($sqlwhere) > 0) { |
||
| 235 | $sqlwhere_str = ' WHERE ' . implode(' AND ', $sqlwhere); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $sqlwhere_str; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return Project number by month for a year |
||
| 243 | * |
||
| 244 | * @param int $year Year to scan |
||
| 245 | * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month |
||
| 246 | * @return array Array of values |
||
| 247 | */ |
||
| 248 | public function getNbByMonth($year, $format = 0) |
||
| 249 | { |
||
| 250 | $this->year = $year; |
||
| 251 | |||
| 252 | $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(*) as nb"; |
||
| 253 | $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t"; |
||
| 254 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 255 | //if (! $user->rights->societe->client->voir && ! $user->soc_id) |
||
| 256 | // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id); |
||
| 257 | $sql .= $this->buildWhere(); |
||
| 258 | $sql .= " GROUP BY dm"; |
||
| 259 | $sql .= $this->db->order('dm', 'DESC'); |
||
| 260 | |||
| 261 | $res = $this->_getNbByMonth($year, $sql, $format); |
||
| 262 | // var_dump($res);print '<br>'; |
||
| 263 | return $res; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Return the Project amount by month for a year |
||
| 268 | * |
||
| 269 | * @param int $year Year to scan |
||
| 270 | * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month |
||
| 271 | * @return array Array with amount by month |
||
| 272 | */ |
||
| 273 | public function getAmountByMonth($year, $format = 0) |
||
| 274 | { |
||
| 275 | $this->year = $year; |
||
| 276 | |||
| 277 | $sql = "SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount)"; |
||
| 278 | $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t"; |
||
| 279 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 280 | //if (! $user->rights->societe->client->voir && ! $user->soc_id) |
||
| 281 | // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id); |
||
| 282 | $sql .= $this->buildWhere(); |
||
| 283 | $sql .= " GROUP BY dm"; |
||
| 284 | $sql .= $this->db->order('dm', 'DESC'); |
||
| 285 | |||
| 286 | $res = $this->_getAmountByMonth($year, $sql, $format); |
||
| 287 | // var_dump($res);print '<br>'; |
||
| 288 | return $res; |
||
| 289 | } |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Return amount of elements by month for several years |
||
| 294 | * |
||
| 295 | * @param int $endyear Start year |
||
| 296 | * @param int $startyear End year |
||
| 297 | * @param int $cachedelay Delay we accept for cache file (0=No read, no save of cache, -1=No read but save) |
||
| 298 | * @param int $wonlostfilter Add a filter on status won/lost |
||
| 299 | * @return array|int<-1,-1> Array of values or <0 if error |
||
| 300 | */ |
||
| 301 | public function getWeightedAmountByMonthWithPrevYear($endyear, $startyear, $cachedelay = 0, $wonlostfilter = 1) |
||
| 302 | { |
||
| 303 | global $conf, $user, $langs; |
||
| 304 | |||
| 305 | if ($startyear > $endyear) { |
||
| 306 | return -1; |
||
| 307 | } |
||
| 308 | |||
| 309 | $datay = array(); |
||
| 310 | |||
| 311 | // Search into cache |
||
| 312 | if (!empty($cachedelay)) { |
||
| 313 | include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 314 | include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php'; |
||
| 315 | } |
||
| 316 | |||
| 317 | $newpathofdestfile = $conf->user->dir_temp . '/' . get_class($this) . '_' . __FUNCTION__ . '_' . (empty($this->cachefilesuffix) ? '' : $this->cachefilesuffix . '_') . $langs->defaultlang . '_user' . $user->id . '.cache'; |
||
| 318 | $newmask = '0644'; |
||
| 319 | |||
| 320 | $nowgmt = dol_now(); |
||
| 321 | |||
| 322 | $foundintocache = 0; |
||
| 323 | if ($cachedelay > 0) { |
||
| 324 | $filedate = dol_filemtime($newpathofdestfile); |
||
| 325 | if ($filedate >= ($nowgmt - $cachedelay)) { |
||
| 326 | $foundintocache = 1; |
||
| 327 | |||
| 328 | $this->lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $filedate; |
||
| 329 | } else { |
||
| 330 | 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."); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | // Load file into $data |
||
| 335 | if ($foundintocache) { // Cache file found and is not too old |
||
| 336 | dol_syslog(get_class($this) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . "."); |
||
| 337 | $data = json_decode(file_get_contents($newpathofdestfile), true); |
||
| 338 | '@phan-var-force array $data'; // Phan can not interpret json_decode |
||
| 339 | } else { |
||
| 340 | $year = $startyear; |
||
| 341 | while ($year <= $endyear) { |
||
| 342 | $datay[$year] = $this->getWeightedAmountByMonth($year, $wonlostfilter); |
||
| 343 | $year++; |
||
| 344 | } |
||
| 345 | |||
| 346 | $data = array(); |
||
| 347 | // $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...) |
||
| 348 | for ($i = 0; $i < 12; $i++) { |
||
| 349 | $data[$i][] = $datay[$endyear][$i][0]; // set label |
||
| 350 | $year = $startyear; |
||
| 351 | while ($year <= $endyear) { |
||
| 352 | $data[$i][] = $datay[$year][$i][1]; // set yval for x=i |
||
| 353 | $year++; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | // Save cache file |
||
| 359 | if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) { |
||
| 360 | dol_syslog(get_class($this) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk."); |
||
| 361 | if (!dol_is_dir($conf->user->dir_temp)) { |
||
| 362 | dol_mkdir($conf->user->dir_temp); |
||
| 363 | } |
||
| 364 | $fp = fopen($newpathofdestfile, 'w'); |
||
| 365 | if ($fp) { |
||
| 366 | fwrite($fp, json_encode($data)); |
||
| 367 | fclose($fp); |
||
| 368 | dolChmod($newpathofdestfile); |
||
| 369 | } else { |
||
| 370 | dol_syslog("Failed to write cache file", LOG_ERR); |
||
| 371 | } |
||
| 372 | $this->lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $nowgmt; |
||
| 373 | } |
||
| 374 | |||
| 375 | return $data; |
||
| 376 | } |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Return the Project weighted opp amount by month for a year. |
||
| 381 | * |
||
| 382 | * @param int $year Year to scan |
||
| 383 | * @param int $wonlostfilter Add a filter on status won/lost |
||
| 384 | * @return array Array with amount by month |
||
| 385 | */ |
||
| 386 | public function getWeightedAmountByMonth($year, $wonlostfilter = 1) |
||
| 387 | { |
||
| 388 | $this->year = $year; |
||
| 389 | |||
| 390 | $sql = "SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount * " . $this->db->ifsql("t.opp_percent IS NULL" . ($wonlostfilter ? " OR cls.code IN ('WON','LOST')" : ""), '0', 't.opp_percent') . " / 100)"; |
||
| 391 | $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t LEFT JOIN " . MAIN_DB_PREFIX . 'c_lead_status as cls ON t.fk_opp_status = cls.rowid'; |
||
| 392 | // No check is done on company permission because readability is managed by public status of project and assignment. |
||
| 393 | //if (! $user->rights->societe->client->voir && ! $user->soc_id) |
||
| 394 | // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id); |
||
| 395 | $sql .= $this->buildWhere(); |
||
| 396 | $sql .= " GROUP BY dm"; |
||
| 397 | $sql .= $this->db->order('dm', 'DESC'); |
||
| 398 | |||
| 399 | $res = $this->_getAmountByMonth($year, $sql); |
||
| 400 | // var_dump($res);print '<br>'; |
||
| 401 | return $res; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Return amount of elements by month for several years |
||
| 406 | * |
||
| 407 | * @param int $endyear End year |
||
| 408 | * @param int $startyear Start year |
||
| 409 | * @param int $cachedelay accept for cache file (0=No read, no save of cache, -1=No read but save) |
||
| 410 | * @return array|int Array of values or <0 if error |
||
| 411 | */ |
||
| 412 | public function getTransformRateByMonthWithPrevYear($endyear, $startyear, $cachedelay = 0) |
||
| 413 | { |
||
| 414 | global $conf, $user, $langs; |
||
| 415 | |||
| 416 | if ($startyear > $endyear) { |
||
| 417 | return -1; |
||
| 418 | } |
||
| 419 | |||
| 420 | $datay = array(); |
||
| 421 | |||
| 422 | // Search into cache |
||
| 423 | if (!empty($cachedelay)) { |
||
| 424 | include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 425 | include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php'; |
||
| 426 | } |
||
| 427 | |||
| 428 | $newpathofdestfile = $conf->user->dir_temp . '/' . get_class($this) . '_' . __FUNCTION__ . '_' . (empty($this->cachefilesuffix) ? '' : $this->cachefilesuffix . '_') . $langs->defaultlang . '_user' . $user->id . '.cache'; |
||
| 429 | $newmask = '0644'; |
||
| 430 | |||
| 431 | $nowgmt = dol_now(); |
||
| 432 | |||
| 433 | $foundintocache = 0; |
||
| 434 | if ($cachedelay > 0) { |
||
| 435 | $filedate = dol_filemtime($newpathofdestfile); |
||
| 436 | if ($filedate >= ($nowgmt - $cachedelay)) { |
||
| 437 | $foundintocache = 1; |
||
| 438 | |||
| 439 | $this->lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $filedate; |
||
| 440 | } else { |
||
| 441 | 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."); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | // Load file into $data |
||
| 446 | if ($foundintocache) { // Cache file found and is not too old |
||
| 447 | dol_syslog(get_class($this) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . "."); |
||
| 448 | $data = json_decode(file_get_contents($newpathofdestfile), true); |
||
| 449 | '@phan-var-force array $data'; // Phan can not interpret json_decode |
||
| 450 | } else { |
||
| 451 | $year = $startyear; |
||
| 452 | while ($year <= $endyear) { |
||
| 453 | $datay[$year] = $this->getTransformRateByMonth($year); |
||
| 454 | $year++; |
||
| 455 | } |
||
| 456 | |||
| 457 | $data = array(); |
||
| 458 | // $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...) |
||
| 459 | for ($i = 0; $i < 12; $i++) { |
||
| 460 | $data[$i][] = $datay[$endyear][$i][0]; // set label |
||
| 461 | $year = $startyear; |
||
| 462 | while ($year <= $endyear) { |
||
| 463 | $data[$i][] = $datay[$year][$i][1]; // set yval for x=i |
||
| 464 | $year++; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | // Save cache file |
||
| 470 | if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == - 1)) { |
||
| 471 | dol_syslog(get_class($this) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk."); |
||
| 472 | if (!dol_is_dir($conf->user->dir_temp)) { |
||
| 473 | dol_mkdir($conf->user->dir_temp); |
||
| 474 | } |
||
| 475 | $fp = fopen($newpathofdestfile, 'w'); |
||
| 476 | if ($fp) { |
||
| 477 | fwrite($fp, json_encode($data)); |
||
| 478 | fclose($fp); |
||
| 479 | dolChmod($newpathofdestfile); |
||
| 480 | } |
||
| 481 | |||
| 482 | $this->lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $nowgmt; |
||
| 483 | } |
||
| 484 | |||
| 485 | return $data; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Return the Project transformation rate by month for a year |
||
| 490 | * |
||
| 491 | * @param int $year Year to scan |
||
| 492 | * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month |
||
| 493 | * @return array Array with amount by month |
||
| 494 | */ |
||
| 495 | public function getTransformRateByMonth($year, $format = 0) |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Return average of entity by month |
||
| 541 | * @param int $year year number |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | protected function getAverageByMonth($year) |
||
| 554 | } |
||
| 555 | } |
||
| 556 |