Completed
Branch develop (cfcedc)
by
unknown
42:44 queued 19s
created

ProjectStats   C

Complexity

Total Complexity 58

Size/Duplication

Total Lines 477
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 477
rs 6.3005
c 0
b 0
f 0
wmc 58
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getAllProjectByStatus() 0 59 7
B getAllByYear() 0 24 2
F buildWhere() 0 36 10
A getNbByMonth() 0 21 1
A getAmountByMonth() 0 20 1
D getWeightedAmountByMonthWithPrevYear() 0 84 16
A getWeightedAmountByMonth() 0 20 2
C getTransformRateByMonthWithPrevYear() 0 73 15
A getTransformRateByMonth() 0 47 3

How to fix   Complexity   

Complex Class

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
2
/* Lead
3
 * Copyright (C) 2014-2015 Florian HENRY <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
19
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
20
21
22
/**
23
 * Class to manage statistics on projects
24
 */
25
class ProjectStats extends Stats
26
{
27
	private $project;
28
	public $userid;
29
	public $socid;
30
	public $year;
31
32
	function __construct($db)
33
	{
34
		global $conf, $user;
35
36
		$this->db = $db;
37
38
		require_once 'project.class.php';
39
		$this->project = new Project($this->db);
40
	}
41
42
43
	/**
44
	 * Return all leads grouped by opportunity status.
45
	 * Warning: There is no filter on WON/LOST because we want this for statistics.
46
	 *
47
	 * @param  int             $limit Limit results
48
	 * @return array|int       Array with value or -1 if error
49
	 * @throws Exception
50
	 */
51
	function getAllProjectByStatus($limit = 5)
52
	{
53
		global $conf, $user, $langs;
54
55
		$datay = array ();
56
57
		$sql = "SELECT";
58
		$sql .= " SUM(t.opp_amount), t.fk_opp_status, cls.code, cls.label";
59
		$sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
60
		// No check is done on company permission because readability is managed by public status of project and assignement.
61
		//if (! $user->rights->societe->client->voir && ! $user->socid)
62
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
63
		$sql .= ", ".MAIN_DB_PREFIX."c_lead_status as cls";
64
		$sql .= $this->buildWhere();
65
		// For external user, no check is done on company permission because readability is managed by public status of project and assignement.
66
		//if ($socid > 0) $sql.= " AND t.fk_soc = ".$socid;
67
		// No check is done on company permission because readability is managed by public status of project and assignement.
68
		//if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id.") OR (s.rowid IS NULL))";
69
		$sql .= " AND t.fk_opp_status = cls.rowid";
70
		$sql .= " AND t.fk_statut <> 0";     // We want historic also, so all projects not draft
71
		$sql .= " GROUP BY t.fk_opp_status, cls.code, cls.label";
72
73
		$result = array ();
74
		$res = array ();
75
76
		dol_syslog(get_class($this) . '::' . __METHOD__ . "", LOG_DEBUG);
77
		$resql = $this->db->query($sql);
78
		if ($resql) {
79
			$num = $this->db->num_rows($resql);
80
			$i = 0;
81
			$other = 0;
82
			while ( $i < $num ) {
83
				$row = $this->db->fetch_row($resql);
84
				if ($i < $limit || $num == $limit)
85
				{
86
					$label = (($langs->trans("OppStatus".$row[2]) != "OppStatus".$row[2]) ? $langs->trans("OppStatus".$row[2]) : $row[2]);
87
					$result[$i] = array(
88
					$label. ' (' . price(price2num($row[0], 'MT'), 1, $langs, 1, -1, -1, $conf->currency) . ')',
89
					$row[0]
90
					);
91
				}
92
				else
93
					$other += $row[1];
94
					$i++;
95
			}
96
			if ($num > $limit)
97
				$result[$i] = array (
98
				$langs->transnoentitiesnoconv("Other"),
99
				$other
100
				);
101
				$this->db->free($resql);
102
		} else {
103
			$this->error = "Error " . $this->db->lasterror();
104
			dol_syslog(get_class($this) . '::' . __METHOD__ . ' ' . $this->error, LOG_ERR);
105
			return -1;
106
		}
107
108
		return $result;
109
	}
110
111
	/**
112
	 * Return count, and sum of products
113
	 *
114
	 * @return array of values
115
	 */
116
	function getAllByYear()
117
	{
118
		global $conf, $user, $langs;
119
120
		$datay = array ();
121
122
		$wonlostfilter=0; // No filter on status WON/LOST
123
124
		$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,";
125
		$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";
126
		$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";
127
		// No check is done on company permission because readability is managed by public status of project and assignement.
128
		//if (! $user->rights->societe->client->voir && ! $user->soc_id)
129
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
130
		$sql.= $this->buildWhere();
131
		// For external user, no check is done on company permission because readability is managed by public status of project and assignement.
132
		//if ($socid > 0) $sql.= " AND t.fk_soc = ".$socid;
133
		// No check is done on company permission because readability is managed by public status of project and assignement.
134
		//if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id.") OR (s.rowid IS NULL))";
135
		$sql.= " GROUP BY year";
136
		$sql.= $this->db->order('year', 'DESC');
137
138
		return $this->_getAllByYear($sql);
139
	}
140
141
142
	/**
143
	 * Build the where part
144
	 *
145
	 * @return string
146
	 */
147
	public function buildWhere()
148
	{
149
		global $user;
150
151
		$sqlwhere_str = '';
152
		$sqlwhere = array();
153
154
		// Get list of project id allowed to user (in a string list separated by coma)
155
		$object = new Project($this->db);
156
		$projectsListId='';
157
		if (! $user->rights->projet->all->lire) $projectsListId = $object->getProjectsAuthorizedForUser($user,0,1,$user->socid);
158
159
		$sqlwhere[] = ' t.entity IN (' . getEntity('project') . ')';
160
161
		if (! empty($this->userid))
162
			$sqlwhere[] = ' t.fk_user_resp=' . $this->userid;
163
164
		// Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
165
		if (! empty($this->socid))
166
			$sqlwhere[] = ' t.fk_soc=' . $this->socid;
167
		if (! empty($this->year) && empty($this->yearmonth))
168
			$sqlwhere[] = " date_format(t.datec,'%Y')='" . $this->db->escape($this->year) . "'";
169
		if (! empty($this->yearmonth))
170
			$sqlwhere[] = " t.datec BETWEEN '" . $this->db->idate(dol_get_first_day($this->yearmonth)) . "' AND '" . $this->db->idate(dol_get_last_day($this->yearmonth)) . "'";
171
172
		if (! empty($this->status))
173
			$sqlwhere[] = " t.fk_opp_status IN (" . $this->status . ")";
174
175
		if (! $user->rights->projet->all->lire) $sqlwhere[] = " AND p.rowid IN (".$projectsListId.")";     // public and assigned to, or restricted to company for external users
176
177
		if (count($sqlwhere) > 0) {
178
			$sqlwhere_str = ' WHERE ' . implode(' AND ', $sqlwhere);
179
		}
180
181
		return $sqlwhere_str;
182
	}
183
184
	/**
185
	 * Return Project number by month for a year
186
	 *
187
	 * @param 	int 	$year 		Year to scan
188
	 * @param	int		$format		0=Label of absiss is a translated text, 1=Label of absiss is month number, 2=Label of absiss is first letter of month
189
	 * @return 	array 				Array of values
190
	 */
191
	function getNbByMonth($year, $format=0)
192
	{
193
		global $user;
194
195
		$this->yearmonth = $year;
196
197
		$sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(*) as nb";
198
		$sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
199
		// No check is done on company permission because readability is managed by public status of project and assignement.
200
		//if (! $user->rights->societe->client->voir && ! $user->soc_id)
201
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
202
		$sql .= $this->buildWhere();
203
		$sql .= " GROUP BY dm";
204
		$sql .= $this->db->order('dm', 'DESC');
205
206
		$this->yearmonth=0;
207
208
		$res = $this->_getNbByMonth($year, $sql, $format);
209
		// var_dump($res);print '<br>';
210
		return $res;
211
	}
212
213
	/**
214
	 * Return the Project amount by month for a year
215
	 *
216
	 * @param 	int 	$year 		Year to scan
217
	 * @param	int		$format		0=Label of absiss is a translated text, 1=Label of absiss is month number, 2=Label of absiss is first letter of month
218
	 * @return 	array 				Array with amount by month
219
	 */
220
	function getAmountByMonth($year, $format=0)
221
	{
222
		global $user;
223
224
		$this->yearmonth = $year;
225
226
		$sql = "SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount)";
227
		$sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
228
		// No check is done on company permission because readability is managed by public status of project and assignement.
229
		//if (! $user->rights->societe->client->voir && ! $user->soc_id)
230
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
231
		$sql .= $this->buildWhere();
232
		$sql .= " GROUP BY dm";
233
		$sql .= $this->db->order('dm', 'DESC');
234
		$this->yearmonth=0;
235
236
		$res = $this->_getAmountByMonth($year, $sql, $format);
237
		// var_dump($res);print '<br>';
238
		return $res;
239
	}
240
241
242
	/**
243
	 * Return amount of elements by month for several years
244
	 *
245
	 * @param	int		$endyear		Start year
246
	 * @param	int		$startyear		End year
247
	 * @param	int		$cachedelay		Delay we accept for cache file (0=No read, no save of cache, -1=No read but save)
248
	 * @param   int     $wonlostfilter  Add a filter on status won/lost
249
	 * @return 	array					Array of values
250
	 */
251
	function getWeightedAmountByMonthWithPrevYear($endyear,$startyear,$cachedelay=0,$wonlostfilter=1)
252
	{
253
		global $conf,$user,$langs;
254
255
		if ($startyear > $endyear) return -1;
256
257
		$datay=array();
258
259
		// Search into cache
260
		if (! empty($cachedelay))
261
		{
262
			include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
263
			include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php';
264
		}
265
266
		$newpathofdestfile=$conf->user->dir_temp.'/'.get_class($this).'_'.__FUNCTION__.'_'.(empty($this->cachefilesuffix)?'':$this->cachefilesuffix.'_').$langs->defaultlang.'_user'.$user->id.'.cache';
267
		$newmask='0644';
268
269
		$nowgmt = dol_now();
270
271
		$foundintocache=0;
272
		if ($cachedelay > 0)
273
		{
274
			$filedate=dol_filemtime($newpathofdestfile);
275
			if ($filedate >= ($nowgmt - $cachedelay))
276
			{
277
				$foundintocache=1;
278
279
				$this->_lastfetchdate[get_class($this).'_'.__FUNCTION__]=$filedate;
280
			}
281
			else
282
			{
283
				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.");
284
			}
285
		}
286
287
		// Load file into $data
288
		if ($foundintocache)    // Cache file found and is not too old
289
		{
290
			dol_syslog(get_class($this).'::'.__FUNCTION__." read data from cache file ".$newpathofdestfile." ".$filedate.".");
0 ignored issues
show
Bug introduced by
The variable $filedate does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
291
			$data = json_decode(file_get_contents($newpathofdestfile), true);
292
		}
293
		else
294
		{
295
			$year=$startyear;
296
			while($year <= $endyear)
297
			{
298
				$datay[$year] = $this->getWeightedAmountByMonth($year,$wonlostfilter);
299
				$year++;
300
			}
301
302
			$data = array();
303
			// $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...)
304
			for ($i = 0 ; $i < 12 ; $i++)
305
			{
306
				$data[$i][]=$datay[$endyear][$i][0];	// set label
307
				$year=$startyear;
308
				while($year <= $endyear)
309
				{
310
					$data[$i][]=$datay[$year][$i][1];	// set yval for x=i
311
					$year++;
312
				}
313
			}
314
		}
315
316
		// Save cache file
317
		if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1))
318
		{
319
			dol_syslog(get_class($this).'::'.__FUNCTION__." save cache file ".$newpathofdestfile." onto disk.");
320
			if (! dol_is_dir($conf->user->dir_temp)) dol_mkdir($conf->user->dir_temp);
321
			$fp = fopen($newpathofdestfile, 'w');
322
			if ($fp)
323
			{
324
				fwrite($fp, json_encode($data));
325
				fclose($fp);
326
				if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK;
327
				@chmod($newpathofdestfile, octdec($newmask));
328
			}
329
			else dol_syslog("Failed to write cache file", LOG_ERR);
330
			$this->_lastfetchdate[get_class($this).'_'.__FUNCTION__]=$nowgmt;
331
		}
332
333
		return $data;
334
	}
335
336
337
	/**
338
	 * Return the Project weighted opp amount by month for a year.
339
	 *
340
	 * @param  int $year               Year to scan
341
	 * @param  int $wonlostfilter      Add a filter on status won/lost
342
	 * @return array                   Array with amount by month
343
	 */
344
	function getWeightedAmountByMonth($year, $wonlostfilter=1)
345
	{
346
		global $user;
347
348
		$this->yearmonth = $year;
349
350
		$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)";
351
		$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';
352
		// No check is done on company permission because readability is managed by public status of project and assignement.
353
		//if (! $user->rights->societe->client->voir && ! $user->soc_id)
354
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
355
		$sql .= $this->buildWhere();
356
		$sql .= " GROUP BY dm";
357
		$sql .= $this->db->order('dm', 'DESC');
358
		$this->yearmonth=0;
359
360
		$res = $this->_getAmountByMonth($year, $sql);
361
		// var_dump($res);print '<br>';
362
		return $res;
363
	}
364
365
	/**
366
	 * Return amount of elements by month for several years
367
	 *
368
	 * @param int $endyear		End year
369
	 * @param int $startyear	Start year
370
	 * @param int $cachedelay accept for cache file (0=No read, no save of cache, -1=No read but save)
371
	 * @return array of values
372
	 */
373
	function getTransformRateByMonthWithPrevYear($endyear, $startyear, $cachedelay = 0)
374
	{
375
		global $conf, $user, $langs;
376
377
		if ($startyear > $endyear) return - 1;
378
379
		$datay = array();
380
381
		// Search into cache
382
		if (! empty($cachedelay))
383
		{
384
			include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
385
			include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php';
386
		}
387
388
		$newpathofdestfile = $conf->user->dir_temp . '/' . get_class($this) . '_' . __FUNCTION__ . '_' . (empty($this->cachefilesuffix) ? '' : $this->cachefilesuffix . '_') . $langs->defaultlang . '_user' . $user->id . '.cache';
389
		$newmask = '0644';
390
391
		$nowgmt = dol_now();
392
393
		$foundintocache = 0;
394
		if ($cachedelay > 0) {
395
			$filedate = dol_filemtime($newpathofdestfile);
396
			if ($filedate >= ($nowgmt - $cachedelay)) {
397
				$foundintocache = 1;
398
399
				$this->_lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $filedate;
400
			} else {
401
				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.");
402
			}
403
		}
404
405
		// Load file into $data
406
		if ($foundintocache) // Cache file found and is not too old
407
		{
408
			dol_syslog(get_class($this) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . ".");
0 ignored issues
show
Bug introduced by
The variable $filedate does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
409
			$data = json_decode(file_get_contents($newpathofdestfile), true);
410
		} else {
411
			$year = $startyear;
412
			while ( $year <= $endyear ) {
413
				$datay[$year] = $this->getTransformRateByMonth($year);
414
				$year ++;
415
			}
416
417
			$data = array ();
418
			// $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...)
419
			for($i = 0; $i < 12; $i ++) {
420
				$data[$i][] = $datay[$endyear][$i][0]; // set label
421
				$year = $startyear;
422
				while ( $year <= $endyear ) {
423
					$data[$i][] = $datay[$year][$i][1]; // set yval for x=i
424
					$year ++;
425
				}
426
			}
427
		}
428
429
		// Save cache file
430
		if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == - 1)) {
431
			dol_syslog(get_class($this) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk.");
432
			if (! dol_is_dir($conf->user->dir_temp))
433
				dol_mkdir($conf->user->dir_temp);
434
			$fp = fopen($newpathofdestfile, 'w');
435
			fwrite($fp, json_encode($data));
436
			fclose($fp);
437
			if (! empty($conf->global->MAIN_UMASK))
438
				$newmask = $conf->global->MAIN_UMASK;
439
			@chmod($newpathofdestfile, octdec($newmask));
440
441
			$this->_lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $nowgmt;
442
		}
443
444
		return $data;
445
	}
446
447
	/**
448
	 * Return the Project transformation rate by month for a year
449
	 *
450
	 * @param 	int 	$year 		Year to scan
451
	 * @param	int		$format		0=Label of absiss is a translated text, 1=Label of absiss is month number, 2=Label of absiss is first letter of month
452
	 * @return 	array 				Array with amount by month
453
	 */
454
	function getTransformRateByMonth($year, $format=0)
455
	{
456
		global $user;
457
458
		$this->yearmonth = $year;
459
460
		$sql = "SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
461
		$sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
462
		// No check is done on company permission because readability is managed by public status of project and assignement.
463
		//if (! $user->rights->societe->client->voir && ! $user->soc_id)
464
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
465
		$sql .= $this->buildWhere();
466
		$sql .= " GROUP BY dm";
467
		$sql .= $this->db->order('dm', 'DESC');
468
469
		$res_total = $this->_getNbByMonth($year, $sql, $format);
470
471
		$this->status=6;
472
473
		$sql = "SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
474
		$sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
475
		// No check is done on company permission because readability is managed by public status of project and assignement.
476
		//if (! $user->rights->societe->client->voir && ! $user->soc_id)
477
		//	$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user=" . $user->id;
478
		$sql .= $this->buildWhere();
479
		$sql .= " GROUP BY dm";
480
		$sql .= $this->db->order('dm', 'DESC');
481
482
		$this->status=0;
483
		$this->yearmonth=0;
484
485
		$res_only_wined = $this->_getNbByMonth($year, $sql, $format);
486
487
		$res=array();
488
489
		foreach($res_total as $key=>$total_row) {
490
			//var_dump($total_row);
491
			if (!empty($total_row[1])) {
492
				$res[$key]=array($total_row[0],(100*$res_only_wined[$key][1])/$total_row[1]);
493
			} else {
494
				$res[$key]=array($total_row[0],0);
495
			}
496
497
		}
498
		// var_dump($res);print '<br>';
499
		return $res;
500
	}
501
}
502