]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/code/Chart_pipeline_by_sales_stage.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Charts / code / Chart_pipeline_by_sales_stage.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  returns HTML for client-side image map.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47
48
49 require_once('include/charts/Charts.php');
50
51
52
53 class Chart_pipeline_by_sales_stage
54 {
55           var $modules = array('Opportunities');
56           var $order = 0;
57 function Chart_pipeline_by_sales_stage()
58 {
59 }
60
61 function draw($extra_tools)
62 {
63
64
65
66 global $action;
67 global $app_list_strings;
68 global $app_strings;
69 global $current_language;
70 global $current_user;
71 global $currentModule;
72 global $sugar_config;
73 global $theme;
74 global $timedate;
75
76 $user_dateFormat = $timedate->get_date_format();
77 $current_module_strings = return_module_language($current_language, 'Charts');
78
79
80 if (isset($_REQUEST['pbss_refresh'])) { $refresh = $_REQUEST['pbss_refresh']; }
81 else { $refresh = false; }
82
83 //get the dates to display
84 $user_date_start = $current_user->getPreference('pbss_date_start');
85
86 if (!empty($user_date_start) && !isset($_REQUEST['pbss_date_start'])) {
87         $date_start = $timedate->to_display_date($user_date_start, false);
88         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
89         $GLOBALS['log']->debug($user_date_start);
90 }
91 elseif (isset($_REQUEST['pbss_date_start']) && $_REQUEST['pbss_date_start'] != '') {
92         $date_start = $_REQUEST['pbss_date_start'];
93         $ds = $timedate->to_db_date($date_start, false);
94         $current_user->setPreference('pbss_date_start', $ds);
95         $GLOBALS['log']->debug("_REQUEST['pbss_date_start'] is:");
96         $GLOBALS['log']->debug($_REQUEST['pbss_date_start']);
97         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
98         $GLOBALS['log']->debug($current_user->getPreference('pbss_date_start'));
99 }
100 else {
101         $date_start = $timedate->nowDate();
102 }
103
104 $user_date_end = $current_user->getPreference('pbss_date_end');
105 if (!empty($user_date_end) && !isset($_REQUEST['pbss_date_end'])) {
106         $date_end = $timedate->to_display_date($user_date_end, false);
107         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
108         $GLOBALS['log']->debug($user_date_end);
109 }
110 elseif (isset($_REQUEST['pbss_date_end']) && $_REQUEST['pbss_date_end'] != '') {
111         $date_end = $_REQUEST['pbss_date_end'];
112         $de = $timedate->to_db_date($date_end, false);
113     $current_user->setPreference('pbss_date_end', $de);
114         $GLOBALS['log']->debug("_REQUEST['pbss_date_end'] is:");
115         $GLOBALS['log']->debug($_REQUEST['pbss_date_end']);
116         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
117         $GLOBALS['log']->debug( $current_user->getPreference('pbss_date_end'));
118 }
119 else {
120         $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
121         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] not found. Using: ".$date_end);
122 }
123
124 // cn: format date_start|end to user's preferred
125 $dateDisplayStart       = strftime($timedate->get_user_date_format(), strtotime($date_start));
126 $dateDisplayEnd         = strftime($timedate->get_user_date_format(), strtotime($date_end));
127 $seps                           = array("-", "/");
128 $dates                          = array($date_start, $date_end);
129 $dateFileNameSafe       = str_replace($seps, "_", $dates);
130 $dateXml[0]                     = $timedate->swap_formats($date_start, $user_dateFormat, $timedate->dbDayFormat);
131 $dateXml[1]                     = $timedate->swap_formats($date_end, $user_dateFormat, $timedate->dbDayFormat);
132
133 $tempx = array();
134 $datax = array();
135 $datax_selected= array();
136 $user_tempx = $current_user->getPreference('pbss_sales_stages');
137 //get list of sales stage keys to display
138 if (!empty($user_tempx) && count($user_tempx) > 0 && !isset($_REQUEST['pbss_sales_stages'])) {
139         $tempx = $user_tempx ;
140         $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
141         $GLOBALS['log']->debug($user_tempx );
142 }
143 elseif (isset($_REQUEST['pbss_sales_stages']) && count($_REQUEST['pbss_sales_stages']) > 0) {
144         $tempx = $_REQUEST['pbss_sales_stages'];
145         $current_user->setPreference('pbss_sales_stages', $_REQUEST['pbss_sales_stages']);
146         $GLOBALS['log']->debug("_REQUEST['pbss_sales_stages'] is:");
147         $GLOBALS['log']->debug($_REQUEST['pbss_sales_stages']);
148         $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
149         $GLOBALS['log']->debug($current_user->getPreference('pbss_sales_stages'));
150 }
151
152 //set $datax using selected sales stage keys
153 if (count($tempx) > 0) {
154         foreach ($tempx as $key) {
155                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
156                 array_push($datax_selected,$key);
157         }
158 }
159 else {
160         $datax = $app_list_strings['sales_stage_dom'];
161         $datax_selected = array_keys($app_list_strings['sales_stage_dom']);
162 }
163 $GLOBALS['log']->debug("datax is:");
164 $GLOBALS['log']->debug($datax);
165
166 $ids = array();
167 $new_ids = array();
168 $user_ids = $current_user->getPreference('pbss_ids');
169 //get list of user ids for which to display data
170 if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbss_ids'])) {
171         $ids = $user_ids;
172
173         $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
174         $GLOBALS['log']->debug($user_ids);
175 }
176 elseif (isset($_REQUEST['pbss_ids']) && count($_REQUEST['pbss_ids']) > 0) {
177         $ids = $_REQUEST['pbss_ids'];
178         $current_user->setPreference('pbss_ids', $_REQUEST['pbss_ids']);
179         $GLOBALS['log']->debug("_REQUEST['pbss_ids'] is:");
180         $GLOBALS['log']->debug($_REQUEST['pbss_ids']);
181         $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
182         $GLOBALS['log']->debug($current_user->getPreference('pbss_ids'));
183 }
184 else {
185         $ids = get_user_array(false);
186         $ids = array_keys($ids);
187
188 }
189
190 //create unique prefix based on selected users for image files
191 $id_hash = '1';
192 if (isset($ids)) {
193         sort($ids);
194         $id_hash = crc32(implode('',$ids));
195         if($id_hash < 0)
196         {
197         $id_hash = $id_hash * -1;
198         }
199 }
200 $GLOBALS['log']->debug("ids is:");
201 $GLOBALS['log']->debug($ids);
202
203 $cache_file_name        = $current_user->getUserPrivGuid()."_lead_source_by_outcome_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
204
205 $GLOBALS['log']->debug("cache file name is: $cache_file_name");
206
207 $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&pbss_refresh=true" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('refresh','alt="Refresh"  border="0" align="absmiddle"').'&nbsp;'.$current_module_strings['LBL_REFRESH'].'</a>&nbsp;&nbsp;<a href="javascript: toggleDisplay(\'pipeline_by_sales_stage_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','alt="Edit"  border="0"  align="absmiddle"').'&nbsp;'. $current_module_strings['LBL_EDIT'].'</a>&nbsp;&nbsp;'.$extra_tools.'</div>';
208
209 echo '<span onmouseover="this.style.cursor=\'move\'" id="chart_handle_' . $this->order . '">' . get_form_header($current_module_strings['LBL_SALES_STAGE_FORM_TITLE'], $tools , false) . '</span>'; ?>
210
211 <?php
212         $cal_lang = "en";
213         $cal_dateformat = $timedate->get_cal_date_format();
214 if (empty($_SESSION['pbss_sales_stages'])) $_SESSION['pbss_sales_stages'] = "";
215 if (empty($_SESSION['pbss_ids'])) $_SESSION['pbss_ids'] = "";
216
217
218 // set populate values
219 $puser_date_start = $current_user->getPreference('user_date_start');
220
221 ?>
222 <p>
223         <div id='pipeline_by_sales_stage_edit' style='display: none;'>
224 <form name='pipeline_by_sales_stage' action="index.php" method="post" >
225 <input type="hidden" name="module" value="<?php echo $currentModule;?>">
226 <input type="hidden" name="action" value="<?php echo $action;?>">
227 <input type="hidden" name="pbss_refresh" value="true">
228 <table cellpadding="0" cellspacing="0" border="0" class="edit view" align="center">
229 <tr>
230         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_START']?></b> <br><span class="dateFormat"><?php echo "(".$timedate->get_user_date_format().")"; ?></span></td>
231         <td valign='top' ><input class="text" name="pbss_date_start" size='12' maxlength='10' id='date_start' value='<?php if (isset($date_start)) echo $date_start; ?>'>  <img src="<?php echo SugarThemeRegistry::current()->getImageURL('jscalendar.gif'); ?>" alt="<?php echo $app_strings['LBL_ENTER_DATE']; ?>"  id="date_start_trigger" align="absmiddle"> </td>
232 </tr>
233 <tr>
234         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_END'];?></b><br><span class="dateFormat"><?php echo "(".$timedate->get_user_date_format().")"; ?></span></td>
235         <td valign='top' ><input class="text" name="pbss_date_end" size='12' maxlength='10' id='date_end' value='<?php if (isset($date_end)) echo $date_end; ?>'>  <img src="<?php echo SugarThemeRegistry::current()->getImageURL('jscalendar.gif'); ?>" alt="<?php echo $app_strings['LBL_ENTER_DATE']; ?>"  id="date_end_trigger" align="absmiddle"> </td>
236 </tr>
237 <tr>
238         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_SALES_STAGES'];?></b></td>
239         <td valign='top' ><select name="pbss_sales_stages[]" multiple size='3'><?php echo get_select_options_with_id($app_list_strings['sales_stage_dom'],$datax_selected); ?></select></td>
240 </tr>
241 <tr>
242         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_USERS'];?></b></td>
243         <td valign='top' ><select name="pbss_ids[]" multiple size='3'><?php echo  get_select_options_with_id(get_user_array(false),$ids); ?></select></td>
244 </tr>
245 <tr>
246 <?php
247 global $app_strings;
248 ?>
249         <td align="right" colspan="2"><input class="button" onclick="return verify_chart_data(pipeline_by_sales_stage);" type="submit" title="<?php echo $app_strings['LBL_SELECT_BUTTON_TITLE']; ?>" accessKey="<?php echo $app_strings['LBL_SELECT_BUTTON_KEY']; ?>" value="<?php echo $app_strings['LBL_SELECT_BUTTON_LABEL']?>" /><input class="button" onClick="javascript: toggleDisplay('pipeline_by_sales_stage_edit');" type="button" title="<?php echo $app_strings['LBL_CANCEL_BUTTON_TITLE']; ?>" accessKey="<?php echo $app_strings['LBL_CANCEL_BUTTON_KEY'];?>" value="<?php echo $app_strings['LBL_CANCEL_BUTTON_LABEL']?>"/></td>
250 </tr>
251 </table>
252 </form>
253 <script type="text/javascript">
254 Calendar.setup ({
255         inputField : "date_start", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_start_trigger", singleClick : true, step : 1, weekNumbers:false
256 });
257 Calendar.setup ({
258         inputField : "date_end", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_end_trigger", singleClick : true, step : 1, weekNumbers:false
259 });
260 </script>
261 </div>
262 </p>
263         <?php
264
265 // draw table
266 echo "<P align='center'>".$this->gen_xml($datax, $dateXml[0], $dateXml[1], $ids, $sugar_config['tmp_dir'].$cache_file_name, $refresh,'hBarF',$current_module_strings)."</P>";
267 echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_SALES_STAGE_FORM_DESC']."</span></P>";
268
269         if (file_exists($sugar_config['tmp_dir'].$cache_file_name)) {
270                 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($sugar_config['tmp_dir'].$cache_file_name)));
271         }
272         else {
273                 $file_date = '';
274         }
275 ?>
276
277 <span class='chartFootnote'>
278 <p align="right"><i><?php  echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
279 </span>
280 <?php
281 echo get_validate_chart_js();
282
283 }
284
285
286
287         /**
288         * Creates opportunity pipeline image as a HORIZONTAL accumlated BAR GRAPH for multiple users.
289         * param $datax- the sales stage data to display in the x-axis
290         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
291         * All Rights Reserved..
292         * Contributor(s): ______________________________________..
293         */
294         function gen_xml($datax=array('foo','bar'), $date_start='2071-10-15', $date_end='2071-10-15', $user_id=array('1'), $cache_file_name='a_file', $refresh=false,$chart_size='hBarF',$current_module_strings) {
295                 global $app_strings, $charset, $lang, $barChartColors, $current_user;
296
297                 $kDelim = $current_user->getPreference('num_grp_sep');
298
299                 global $timedate;
300
301                 if (!file_exists($cache_file_name) || $refresh == true) {
302
303                         $GLOBALS['log']->debug("starting pipeline chart");
304                         $GLOBALS['log']->debug("datax is:");
305                         $GLOBALS['log']->debug($datax);
306                         $GLOBALS['log']->debug("user_id is: ");
307                         $GLOBALS['log']->debug($user_id);
308                         $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
309                         $opp = new Opportunity;
310                         $where="";
311                         //build the where clause for the query that matches $user
312                         $count = count($user_id);
313                         $id = array();
314                         $user_list = get_user_array(false);
315                         foreach ($user_id as $key) {
316                                 $new_ids[$key] = $user_list[$key];
317                         }
318                         if ($count>0) {
319                                 foreach ($new_ids as $the_id=>$the_name) {
320                                         $id[] = "'".$the_id."'";
321                                 }
322                                 $ids = join(",",$id);
323                                 $where .= "opportunities.assigned_user_id IN ($ids) ";
324
325                         }
326                         //build the where clause for the query that matches $datax
327                         $count = count($datax);
328                         $dataxArr = array();
329                         if ($count>0) {
330
331                                 foreach ($datax as $key=>$value) {
332                                         $dataxArr[] = "'".$key."'";
333                                 }
334                                 $dataxArr = join(",",$dataxArr);
335                                 $where .= "AND opportunities.sales_stage IN     ($dataxArr) ";
336                         }
337
338                         //build the where clause for the query that matches $date_start and $date_end
339                         $where .= "     AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
340                                                 AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
341                         $where .= "     AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
342
343                         //Now do the db queries
344                         //query for opportunity data that matches $datax and $user
345                         $query = "      SELECT opportunities.sales_stage,
346                                                         users.user_name,
347                                                         opportunities.assigned_user_id,
348                                                         count( * ) AS opp_count,
349                                                         sum(amount_usdollar/1000) AS total
350                                                 FROM users,opportunities  ";
351                         $query .= "WHERE " .$where;
352                         $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
353
354
355
356                         $result = $opp->db->query($query)
357                         or sugar_die("Error selecting sugarbean: ".mysql_error());
358                         //build pipeline by sales stage data
359                         $total = 0;
360                         $div = 1;
361                         global $sugar_config;
362                         $symbol = $sugar_config['default_currency_symbol'];
363                         global $current_user;
364                         if($current_user->getPreference('currency') ){
365
366                                 $currency = new Currency();
367                                 $currency->retrieve($current_user->getPreference('currency'));
368                                 $div = $currency->conversion_rate;
369                                 $symbol = $currency->symbol;
370                         }
371                         // cn: adding user-pref date handling
372                         $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
373                         $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
374
375                         $fileContents = '     <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
376                         $stageArr = array();
377                         $usernameArr = array();
378                         $rowTotalArr = array();
379                         $rowTotalArr[] = 0;
380                         while($row = $opp->db->fetchByAssoc($result, -1, false))
381                         {
382                                 if($row['total']*$div<=100){
383                                         $sum = round($row['total']*$div, 2);
384                                 } else {
385                                         $sum = round($row['total']*$div);
386                                 }
387                                 if(!isset($stageArr[$row['sales_stage']]['row_total'])) {$stageArr[$row['sales_stage']]['row_total']=0;}
388                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['opp_count'] = $row['opp_count'];
389                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['total'] = $sum;
390                                 $stageArr[$row['sales_stage']]['people'][$row['assigned_user_id']] = $row['user_name'];
391                                 $stageArr[$row['sales_stage']]['row_total'] += $sum;
392
393                                 $usernameArr[$row['assigned_user_id']] = $row['user_name'];
394                                 $total += $sum;
395                         }
396                         foreach ($datax as $key=>$translation) {
397                                 if(isset($stageArr[$key]['row_total'])){$rowTotalArr[]=$stageArr[$key]['row_total'];}
398                                 if(isset($stageArr[$key]['row_total']) && $stageArr[$key]['row_total']>100) {
399                                         $stageArr[$key]['row_total'] = round($stageArr[$key]['row_total']);
400                                 }
401                                 $fileContents .= '     <dataRow title="'.$translation.'" endLabel="';
402                                 if(isset($stageArr[$key]['row_total'])){$fileContents .= currency_format_number($stageArr[$key]['row_total'], array('currency_symbol' => true));}
403                                 $fileContents .= '">'."\n";
404                                 if(isset($stageArr[$key]['people'])){
405                                         asort($stageArr[$key]['people']);
406                                         reset($stageArr[$key]['people']);
407                                         foreach ($stageArr[$key]['people'] as $nameKey=>$nameValue) {
408                                                 $fileContents .= '          <bar id="'.$nameKey.'" totalSize="'.$stageArr[$key][$nameKey]['total'].'" altText="'.$nameValue.': '.format_number($stageArr[$key][$nameKey]['opp_count'], 0, 0).' '.$current_module_strings['LBL_OPPS_WORTH'].' '.format_number($stageArr[$key][$nameKey]['total'], 2, 2).$current_module_strings['LBL_OPP_THOUSANDS'].' '.$current_module_strings['LBL_OPPS_IN_STAGE'].' '.$translation.'" url="index.php?module=Opportunities&action=index&assigned_user_id[]='.$nameKey.'&sales_stage='.urlencode($key).'&date_start='.$date_start.'&date_closed='.$date_end.'&query=true&searchFormTab=advanced_search"/>'."\n";
409                                         }
410                                 }
411                                 $fileContents .= '     </dataRow>'."\n";
412                         }
413                         $fileContents .= '     </yData>'."\n";
414                         $max = get_max($rowTotalArr);
415                         if($chart_size=='hBarF'){
416                                 $length = "10";
417                         }else{
418                                 $length = "4";
419                         }
420                         $fileContents .= '     <xData min="0" max="'.$max.'" length="'.$length.'" prefix="'.$symbol.'" suffix="" kDelim="'.$kDelim.'" />'."\n";
421                         $fileContents .= '     <colorLegend status="on">'."\n";
422                         $i=0;
423                         asort($new_ids);
424                         foreach ($new_ids as $key=>$value) {
425                         $color = generate_graphcolor($key,$i);
426                         $fileContents .= '          <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
427                         $i++;
428                         }
429                         $fileContents .= '     </colorLegend>'."\n";
430                         $fileContents .= '     <graphInfo>'."\n";
431                         $fileContents .= '          <![CDATA['.$current_module_strings['LBL_DATE_RANGE'].' '.$dateStartDisplay.' '.$current_module_strings['LBL_DATE_RANGE_TO'].' '.$dateEndDisplay.'<BR/>'.$current_module_strings['LBL_OPP_SIZE'].' '.$symbol.'1'.$current_module_strings['LBL_OPP_THOUSANDS'].']]>'."\n";
432                         $fileContents .= '     </graphInfo>'."\n";
433                         $fileContents .= '     <chartColors ';
434                         foreach ($barChartColors as $key => $value) {
435                                 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
436                         }
437                         $fileContents .= ' />'."\n";
438                         $fileContents .= '</graphData>'."\n";
439                         $total = $total;
440                         $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
441                         $fileContents = $title.$fileContents;
442
443                         save_xml_file($cache_file_name, $fileContents);
444                 }
445
446                 if($chart_size=='hBarF'){
447                         $width = "800";
448                         $height = "400";
449                 } else {
450                         $width = "350";
451                         $height = "400";
452                 }
453                 $return = create_chart($chart_size,$cache_file_name,$width,$height);
454                 return $return;
455         }
456
457         function constructQuery(){
458                 global $current_user;
459                 global $timedate;
460                 global $app_list_strings;
461
462                 //get the dates to display
463                 $user_date_start = $current_user->getPreference('pbss_date_start');
464
465                 if (!empty($user_date_start) && !isset($_REQUEST['pbss_date_start'])) {
466                         $date_start = $timedate->to_display_date($user_date_start, false);
467                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
468                         $GLOBALS['log']->debug($user_date_start);
469                 }
470                 elseif (isset($_REQUEST['pbss_date_start']) && $_REQUEST['pbss_date_start'] != '') {
471                         $date_start = $_REQUEST['pbss_date_start'];
472                         $ds = $timedate->to_db_date($date_start, false);
473                         $current_user->setPreference('pbss_date_start', $ds);
474                         $GLOBALS['log']->debug("_REQUEST['pbss_date_start'] is:");
475                         $GLOBALS['log']->debug($_REQUEST['pbss_date_start']);
476                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
477                         $GLOBALS['log']->debug($current_user->getPreference('pbss_date_start'));
478                 }
479                 else {
480                         $date_start = $timedate->nowDate();
481                 }
482
483                 $user_date_end = $current_user->getPreference('pbss_date_end');
484                 if (!empty($user_date_end) && !isset($_REQUEST['pbss_date_end'])) {
485                         $date_end = $timedate->to_display_date($user_date_end, false);
486                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
487                         $GLOBALS['log']->debug($user_date_end);
488                 }
489                 elseif (isset($_REQUEST['pbss_date_end']) && $_REQUEST['pbss_date_end'] != '') {
490                         $date_end = $_REQUEST['pbss_date_end'];
491                         $de = $timedate->to_db_date($date_end, false);
492                     $current_user->setPreference('pbss_date_end', $de);
493                         $GLOBALS['log']->debug("_REQUEST['pbss_date_end'] is:");
494                         $GLOBALS['log']->debug($_REQUEST['pbss_date_end']);
495                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
496                         $GLOBALS['log']->debug( $current_user->getPreference('pbss_date_end'));
497                 }
498                 else {
499                         $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
500                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] not found. Using: ".$date_end);
501                 }
502
503 $tempx = array();
504 $datax = array();
505 $datax_selected= array();
506 $user_tempx = $current_user->getPreference('pbss_sales_stages');
507 //get list of sales stage keys to display
508 if (!empty($user_tempx) && count($user_tempx) > 0 && !isset($_REQUEST['pbss_sales_stages'])) {
509         $tempx = $user_tempx ;
510         $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
511         $GLOBALS['log']->debug($user_tempx );
512 }
513 elseif (isset($_REQUEST['pbss_sales_stages']) && count($_REQUEST['pbss_sales_stages']) > 0) {
514         $tempx = $_REQUEST['pbss_sales_stages'];
515         $current_user->setPreference('pbss_sales_stages', $_REQUEST['pbss_sales_stages']);
516         $GLOBALS['log']->debug("_REQUEST['pbss_sales_stages'] is:");
517         $GLOBALS['log']->debug($_REQUEST['pbss_sales_stages']);
518         $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
519         $GLOBALS['log']->debug($current_user->getPreference('pbss_sales_stages'));
520 }
521
522 //set $datax using selected sales stage keys
523 if (count($tempx) > 0) {
524         foreach ($tempx as $key) {
525                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
526                 array_push($datax_selected,$key);
527         }
528 }
529 else {
530         $datax = $app_list_strings['sales_stage_dom'];
531         $datax_selected = array_keys($app_list_strings['sales_stage_dom']);
532 }
533 $GLOBALS['log']->debug("datax is:");
534 $GLOBALS['log']->debug($datax);
535
536
537
538                 $ids = array();
539                 $new_ids = array();
540                 $user_ids = $current_user->getPreference('pbss_ids');
541                 //get list of user ids for which to display data
542                 if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbss_ids'])) {
543                         $ids = $user_ids;
544
545                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
546                         $GLOBALS['log']->debug($user_ids);
547                 }
548                 elseif (isset($_REQUEST['pbss_ids']) && count($_REQUEST['pbss_ids']) > 0) {
549                         $ids = $_REQUEST['pbss_ids'];
550                         $current_user->setPreference('pbss_ids', $_REQUEST['pbss_ids']);
551                         $GLOBALS['log']->debug("_REQUEST['pbss_ids'] is:");
552                         $GLOBALS['log']->debug($_REQUEST['pbss_ids']);
553                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
554                         $GLOBALS['log']->debug($current_user->getPreference('pbss_ids'));
555                 }
556                 else {
557                         $ids = get_user_array(false);
558                         $ids = array_keys($ids);
559
560                 }
561
562                 $user_id = $ids;
563                 $opp = new Opportunity;
564                 $where="";
565                 //build the where clause for the query that matches $user
566                 $count = count($user_id);
567                 $id = array();
568                 $user_list = get_user_array(false);
569                 foreach ($user_id as $key) {
570                         $new_ids[$key] = $user_list[$key];
571                 }
572                 if ($count>0) {
573                         foreach ($new_ids as $the_id=>$the_name) {
574                                 $id[] = "'".$the_id."'";
575                         }
576                         $ids = join(",",$id);
577                         $where .= "opportunities.assigned_user_id IN ($ids) ";
578
579                 }
580                 //build the where clause for the query that matches $datax
581                 $count = count($datax);
582                 $dataxArr = array();
583                 if ($count>0) {
584
585                         foreach ($datax as $key=>$value) {
586                                 $dataxArr[] = "'".$key."'";
587                         }
588                         $dataxArr = join(",",$dataxArr);
589                         $where .= "AND opportunities.sales_stage IN     ($dataxArr) ";
590                 }
591
592                 //build the where clause for the query that matches $date_start and $date_end
593                 $where .= "     AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
594                                         AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
595                 $where .= "     AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
596
597                 //Now do the db queries
598                 //query for opportunity data that matches $datax and $user
599                 $query = "      SELECT opportunities.sales_stage,
600                                                 users.user_name,
601                                                 opportunities.assigned_user_id,
602                                                 count( * ) AS opp_count,
603                                                 sum(amount_usdollar/1000) AS total
604                                         FROM users,opportunities  ";
605                 $query .= "WHERE " .$where;
606                 $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
607
608                 return $query;
609         }
610
611         function constructGroupBy(){
612                 return array( 'sales_stage', 'user_name' );
613         }
614
615 }
616
617 ?>