19include_once DOL_DOCUMENT_ROOT.
'/core/class/stats.class.php';
20include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
75 require_once
'project.class.php';
76 $this->project =
new Project($this->db);
78 $this->from = MAIN_DB_PREFIX.$this->project->table_element;
79 $this->field =
'opp_amount';
80 $this->where =
" entity = ".$conf->entity;
81 if ($this->socid > 0) {
82 $this->where .=
" AND fk_soc = ".((int) $this->socid);
84 if (is_array($this->userid) && count($this->userid) > 0) {
85 $this->where .=
' AND fk_user IN ('.$this->db->sanitize(implode(
',', $this->userid)).
')';
86 } elseif ($this->userid > 0) {
87 $this->where .=
" AND fk_user = ".((int) $this->userid);
102 global
$conf, $user, $langs;
107 $sql .=
" SUM(t.opp_amount), t.fk_opp_status, cls.code, cls.label";
108 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
112 $sql .=
", ".MAIN_DB_PREFIX.
"c_lead_status as cls";
118 $sql .=
" AND t.fk_opp_status = cls.rowid";
119 $sql .=
" AND t.fk_statut <> 0";
120 $sql .=
" GROUP BY t.fk_opp_status, cls.code, cls.label";
124 dol_syslog(get_class($this).
'::'.__METHOD__, LOG_DEBUG);
125 $resql = $this->db->query($sql);
127 $num = $this->db->num_rows($resql);
131 $row = $this->db->fetch_row($resql);
132 if ($i < $limit || $num == $limit) {
133 $label = (($langs->trans(
"OppStatus".$row[2]) !=
"OppStatus".$row[2]) ? $langs->trans(
"OppStatus".$row[2]) : $row[2]);
145 $langs->transnoentitiesnoconv(
"Other"),
149 $this->db->free($resql);
151 $this->error =
"Error ".$this->db->lasterror();
152 dol_syslog(get_class($this).
'::'.__METHOD__.
' '.$this->error, LOG_ERR);
166 global
$conf, $user, $langs;
172 $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,";
173 $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";
174 $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";
183 $sql .=
" GROUP BY year";
184 $sql .= $this->db->order(
'year',
'DESC');
204 $projectsListId =
'';
205 if (!$user->hasRight(
'projet',
'all',
'lire')) {
206 $projectsListId =
$object->getProjectsAuthorizedForUser($user, 0, 1, $user->socid);
209 $sqlwhere[] =
' t.entity IN ('.getEntity(
'project').
')';
211 if (!empty($this->userid)) {
212 $sqlwhere[] =
' t.fk_user_resp = '.((int) $this->userid);
216 if (!empty($this->socid)) {
217 $sqlwhere[] =
' t.fk_soc = '.((int) $this->socid);
219 if (!empty($this->year) && empty($this->month)) {
222 if (!empty($this->year) && !empty($this->month)) {
223 $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)).
"'";
226 if (!empty($this->
status)) {
227 $sqlwhere[] =
" t.fk_statut IN (".$this->db->sanitize($this->
status).
")";
230 if (!empty($this->opp_status)) {
231 if (is_numeric($this->opp_status) && $this->opp_status > 0) {
232 $sqlwhere[] =
" t.fk_opp_status = ".((int) $this->opp_status);
234 if ($this->opp_status ==
'all') {
235 $sqlwhere[] =
" (t.fk_opp_status IS NOT NULL AND t.fk_opp_status <> -1)";
237 if ($this->opp_status ==
'openedopp') {
238 $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')))";
240 if ($this->opp_status ==
'notopenedopp') {
241 $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'))";
243 if ($this->opp_status ==
'none') {
244 $sqlwhere[] =
" (t.fk_opp_status IS NULL OR t.fk_opp_status = -1)";
248 if (!$user->hasRight(
'projet',
'all',
'lire')) {
249 $sqlwhere[] =
" t.rowid IN (".$this->db->sanitize($projectsListId).
")";
252 if (count($sqlwhere) > 0) {
253 $sqlwhere_str =
' WHERE '.implode(
' AND ', $sqlwhere);
256 return $sqlwhere_str;
270 $sql =
"SELECT date_format(t.datec,'%m') as dm, COUNT(*) as nb";
271 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
276 $sql .=
" GROUP BY dm";
277 $sql .= $this->db->order(
'dm',
'DESC');
295 $sql =
"SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount)";
296 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
301 $sql .=
" GROUP BY dm";
302 $sql .= $this->db->order(
'dm',
'DESC');
321 global
$conf, $user, $langs;
323 if ($startyear > $endyear) {
330 if (!empty($cachedelay)) {
331 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
334 $newpathofdestfile =
$conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_user'.$user->id.
'.cache';
341 if ($cachedelay > 0) {
343 if ($filedate >= ($nowgmt - $cachedelay)) {
346 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
348 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.");
353 if ($foundintocache) {
354 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
355 $data = json_decode(file_get_contents($newpathofdestfile),
true);
356 '@phan-var-force array $data';
359 while ($year <= $endyear) {
366 for ($i = 0; $i < 12; $i++) {
367 $data[$i][] = $datay[$endyear][$i][0];
369 while ($year <= $endyear) {
370 $data[$i][] = $datay[$year][$i][1];
377 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
378 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
382 $fp = fopen($newpathofdestfile,
'w');
384 fwrite($fp, json_encode($data));
388 dol_syslog(
"Failed to write cache file", LOG_ERR);
390 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
408 $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)";
409 $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';
414 $sql .=
" GROUP BY dm";
415 $sql .= $this->db->order(
'dm',
'DESC');
432 global
$conf, $user, $langs;
434 if ($startyear > $endyear) {
441 if (!empty($cachedelay)) {
442 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
445 $newpathofdestfile =
$conf->user->dir_temp.
'/'.get_class($this).
'_'.__FUNCTION__.
'_'.(empty($this->cachefilesuffix) ?
'' : $this->cachefilesuffix.
'_').$langs->defaultlang.
'_user'.$user->id.
'.cache';
452 if ($cachedelay > 0) {
454 if ($filedate >= ($nowgmt - $cachedelay)) {
457 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $filedate;
459 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.");
464 if ($foundintocache) {
465 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" read data from cache file ".$newpathofdestfile.
" ".$filedate.
".");
466 $data = json_decode(file_get_contents($newpathofdestfile),
true);
467 '@phan-var-force array $data';
470 while ($year <= $endyear) {
477 for ($i = 0; $i < 12; $i++) {
478 $data[$i][] = $datay[$endyear][$i][0];
480 while ($year <= $endyear) {
481 $data[$i][] = $datay[$year][$i][1];
488 if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == - 1)) {
489 dol_syslog(get_class($this).
'::'.__FUNCTION__.
" save cache file ".$newpathofdestfile.
" onto disk.");
493 $fp = fopen($newpathofdestfile,
'w');
495 fwrite($fp, json_encode($data));
500 $this->lastfetchdate[get_class($this).
'_'.__FUNCTION__] = $nowgmt;
517 $sql =
"SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
518 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
523 $sql .=
" GROUP BY dm";
524 $sql .= $this->db->order(
'dm',
'DESC');
530 $sql =
"SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
531 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
536 $sql .=
" GROUP BY dm";
537 $sql .= $this->db->order(
'dm',
'DESC');
541 $res_only_wined = $this->
_getNbByMonth($year, $sql, $format);
545 foreach ($res_total as $key => $total_row) {
547 if (!empty($total_row[1])) {
548 $res[$key] = array($total_row[0], (100 * $res_only_wined[$key][1]) / $total_row[1]);
550 $res[$key] = array($total_row[0], 0);
564 $sql =
"SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.
")";
565 $sql .=
" FROM ".$this->from;
567 $sql .=
" AND ".$this->where;
568 $sql .=
" GROUP BY dm";
569 $sql .= $this->db->order(
'dm',
'DESC');
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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)
Return the amount average par month for a given year.
_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...
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
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)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...