18include_once DOL_DOCUMENT_ROOT.
'/core/class/stats.class.php';
19include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
52 require_once
'project.class.php';
53 $this->project =
new Project($this->db);
55 $this->from = MAIN_DB_PREFIX.$this->project->table_element;
56 $this->field =
'opp_amount';
57 $this->where =
" entity = ".$conf->entity;
58 if ($this->socid > 0) {
59 $this->where .=
" AND fk_soc = ".((int) $this->socid);
61 if (is_array($this->userid) && count($this->userid) > 0) {
62 $this->where .=
' AND fk_user IN ('.$this->db->sanitize(join(
',', $this->userid)).
')';
63 } elseif ($this->userid > 0) {
64 $this->where .=
" AND fk_user = ".((int) $this->userid);
79 global $conf, $user, $langs;
84 $sql .=
" SUM(t.opp_amount), t.fk_opp_status, cls.code, cls.label";
85 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
89 $sql .=
", ".MAIN_DB_PREFIX.
"c_lead_status as cls";
95 $sql .=
" AND t.fk_opp_status = cls.rowid";
96 $sql .=
" AND t.fk_statut <> 0";
97 $sql .=
" GROUP BY t.fk_opp_status, cls.code, cls.label";
101 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
102 $resql = $this->db->query($sql);
104 $num = $this->db->num_rows($resql);
108 $row = $this->db->fetch_row($resql);
109 if ($i < $limit || $num == $limit) {
110 $label = (($langs->trans(
"OppStatus".$row[2]) !=
"OppStatus".$row[2]) ? $langs->trans(
"OppStatus".$row[2]) : $row[2]);
112 $label.
' ('.
price(
price2num($row[0],
'MT'), 1, $langs, 1, -1, -1, $conf->currency).
')',
122 $langs->transnoentitiesnoconv(
"Other"),
126 $this->db->free($resql);
128 $this->error =
"Error ".$this->db->lasterror();
129 dol_syslog(get_class($this).
'::'.__METHOD__.
' '.$this->error, LOG_ERR);
143 global $conf, $user, $langs;
149 $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,";
150 $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";
151 $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";
160 $sql .=
" GROUP BY year";
161 $sql .= $this->db->order(
'year',
'DESC');
180 $object =
new Project($this->db);
181 $projectsListId =
'';
182 if (empty($user->rights->projet->all->lire)) {
183 $projectsListId = $object->getProjectsAuthorizedForUser($user, 0, 1, $user->socid);
186 $sqlwhere[] =
' t.entity IN ('.getEntity(
'project').
')';
188 if (!empty($this->userid)) {
189 $sqlwhere[] =
' t.fk_user_resp = '.((int) $this->userid);
193 if (!empty($this->socid)) {
194 $sqlwhere[] =
' t.fk_soc = '.((int) $this->socid);
196 if (!empty($this->year) && empty($this->month)) {
199 if (!empty($this->year) && !empty($this->month)) {
200 $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)).
"'";
203 if (!empty($this->status)) {
204 $sqlwhere[] =
" t.fk_statut IN (".$this->db->sanitize($this->status).
")";
207 if (!empty($this->opp_status)) {
208 if (is_numeric($this->opp_status) && $this->opp_status > 0) {
209 $sqlwhere[] =
" t.fk_opp_status = ".((int) $this->opp_status);
211 if ($this->opp_status ==
'all') {
212 $sqlwhere[] =
" (t.fk_opp_status IS NOT NULL AND t.fk_opp_status <> -1)";
214 if ($this->opp_status ==
'openedopp') {
215 $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')))";
217 if ($this->opp_status ==
'notopenedopp') {
218 $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'))";
220 if ($this->opp_status ==
'none') {
221 $sqlwhere[] =
" (t.fk_opp_status IS NULL OR t.fk_opp_status = -1)";
225 if (empty($user->rights->projet->all->lire)) {
226 $sqlwhere[] =
" t.rowid IN (".$this->db->sanitize($projectsListId).
")";
229 if (count($sqlwhere) > 0) {
230 $sqlwhere_str =
' WHERE '.implode(
' AND ', $sqlwhere);
233 return $sqlwhere_str;
247 $sql =
"SELECT date_format(t.datec,'%m') as dm, COUNT(*) as nb";
248 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
253 $sql .=
" GROUP BY dm";
254 $sql .= $this->db->order(
'dm',
'DESC');
272 $sql =
"SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount)";
273 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
278 $sql .=
" GROUP BY dm";
279 $sql .= $this->db->order(
'dm',
'DESC');
298 global $conf, $user, $langs;
300 if ($startyear > $endyear) {
307 if (!empty($cachedelay)) {
308 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
309 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
312 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_user'.$user->id.
'.cache';
318 if ($cachedelay > 0) {
320 if ($filedate >= ($nowgmt - $cachedelay)) {
323 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
325 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.");
330 if ($foundintocache) {
331 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
332 $data = json_decode(file_get_contents($newpathofdestfile),
true);
335 while ($year <= $endyear) {
342 for ($i = 0; $i < 12; $i++) {
343 $data[$i][] = $datay[$endyear][$i][0];
345 while ($year <= $endyear) {
346 $data[$i][] = $datay[$year][$i][1];
353 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
354 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
358 $fp = fopen($newpathofdestfile,
'w');
360 fwrite($fp, json_encode($data));
364 dol_syslog(
"Failed to write cache file", LOG_ERR);
366 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
384 $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)";
385 $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';
390 $sql .=
" GROUP BY dm";
391 $sql .= $this->db->order(
'dm',
'DESC');
408 global $conf, $user, $langs;
410 if ($startyear > $endyear) {
417 if (!empty($cachedelay)) {
418 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
419 include_once DOL_DOCUMENT_ROOT.
'/core/lib/json.lib.php';
422 $newpathofdestfile = $conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_user'.$user->id.
'.cache';
428 if ($cachedelay > 0) {
430 if ($filedate >= ($nowgmt - $cachedelay)) {
433 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
435 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.");
440 if ($foundintocache) {
441 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
442 $data = json_decode(file_get_contents($newpathofdestfile),
true);
445 while ($year <= $endyear) {
452 for ($i = 0; $i < 12; $i++) {
453 $data[$i][] = $datay[$endyear][$i][0];
455 while ($year <= $endyear) {
456 $data[$i][] = $datay[$year][$i][1];
463 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == - 1)) {
464 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
468 $fp = fopen($newpathofdestfile,
'w');
470 fwrite($fp, json_encode($data));
475 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
492 $sql =
"SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
493 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
498 $sql .=
" GROUP BY dm";
499 $sql .= $this->db->order(
'dm',
'DESC');
505 $sql =
"SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
506 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
511 $sql .=
" GROUP BY dm";
512 $sql .= $this->db->order(
'dm',
'DESC');
516 $res_only_wined = $this->
_getNbByMonth($year, $sql, $format);
520 foreach ($res_total as $key => $total_row) {
522 if (!empty($total_row[1])) {
523 $res[$key] = array($total_row[0], (100 * $res_only_wined[$key][1]) / $total_row[1]);
525 $res[$key] = array($total_row[0], 0);
539 $sql =
"SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.
")";
540 $sql .=
" FROM ".$this->from;
542 $sql .=
" AND ".$this->where;
543 $sql .=
" GROUP BY dm";
544 $sql .= $this->db->order(
'dm',
'DESC');
Class to manage projects.
Class to manage statistics on projects.
getAmountByMonth($year, $format=0)
Return the Project amount by month for a year.
__construct($db)
Constructor.
buildWhere()
Build the where part.
getAllByYear()
Return count, and sum of products.
getTransformRateByMonth($year, $format=0)
Return the Project transformation rate by month for a year.
getAllProjectByStatus($limit=5)
Return all leads grouped by opportunity status.
getNbByMonth($year, $format=0)
Return Project number by month for a year.
getWeightedAmountByMonth($year, $wonlostfilter=1)
Return the Project weighted opp amount by month for a year.
getAverageByMonth($year)
Return average of entity by month.
getTransformRateByMonthWithPrevYear($endyear, $startyear, $cachedelay=0)
Return amount of elements by month for several years.
getWeightedAmountByMonthWithPrevYear($endyear, $startyear, $cachedelay=0, $wonlostfilter=1)
Return amount of elements by month for several years.
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
dol_filemtime($pathoffile)
Return time of a file.
dol_is_dir($folder)
Test if filename is a directory.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
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)