]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/code/Chart_pipeline_by_sales_stage.php
Release 6.4.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        = sugar_cached("xml/").$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','border="0" align="absmiddle"',null,null,'.gif',$mod_strings['LBL_REFRESH']).'&nbsp;'.$current_module_strings['LBL_REFRESH'].'</a>&nbsp;&nbsp;<a href="javascript: toggleDisplay(\'pipeline_by_sales_stage_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','border="0" align="absmiddle"',null,null,'.gif',$mod_strings['LBL_EDIT']).'&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; ?>'>  <?php echo SugarThemeRegistry::current()->getImage('jscalendar', 'id="date_start_trigger" align="absmiddle"', null, null, ".gif", $app_strings['LBL_ENTER_DATE']); ?> </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; ?>'>  <?php echo SugarThemeRegistry::current()->getImage('jscalendar', 'id="date_end_trigger" align="absmiddle"', null, null, ".gif", $app_strings['LBL_ENTER_DATE']); ?> </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, $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($cache_file_name)) {
270                 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($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, true);
357                         //build pipeline by sales stage data
358                         $total = 0;
359                         $div = 1;
360                         global $sugar_config;
361                         $symbol = $sugar_config['default_currency_symbol'];
362                         global $current_user;
363                         if($current_user->getPreference('currency') ){
364
365                                 $currency = new Currency();
366                                 $currency->retrieve($current_user->getPreference('currency'));
367                                 $div = $currency->conversion_rate;
368                                 $symbol = $currency->symbol;
369                         }
370                         // cn: adding user-pref date handling
371                         $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
372                         $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
373
374                         $fileContents = '     <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
375                         $stageArr = array();
376                         $usernameArr = array();
377                         $rowTotalArr = array();
378                         $rowTotalArr[] = 0;
379                         while($row = $opp->db->fetchByAssoc($result, false))
380                         {
381                                 if($row['total']*$div<=100){
382                                         $sum = round($row['total']*$div, 2);
383                                 } else {
384                                         $sum = round($row['total']*$div);
385                                 }
386                                 if(!isset($stageArr[$row['sales_stage']]['row_total'])) {$stageArr[$row['sales_stage']]['row_total']=0;}
387                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['opp_count'] = $row['opp_count'];
388                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['total'] = $sum;
389                                 $stageArr[$row['sales_stage']]['people'][$row['assigned_user_id']] = $row['user_name'];
390                                 $stageArr[$row['sales_stage']]['row_total'] += $sum;
391
392                                 $usernameArr[$row['assigned_user_id']] = $row['user_name'];
393                                 $total += $sum;
394                         }
395                         foreach ($datax as $key=>$translation) {
396                                 if(isset($stageArr[$key]['row_total'])){$rowTotalArr[]=$stageArr[$key]['row_total'];}
397                                 if(isset($stageArr[$key]['row_total']) && $stageArr[$key]['row_total']>100) {
398                                         $stageArr[$key]['row_total'] = round($stageArr[$key]['row_total']);
399                                 }
400                                 $fileContents .= '     <dataRow title="'.$translation.'" endLabel="';
401                                 if(isset($stageArr[$key]['row_total'])){$fileContents .= currency_format_number($stageArr[$key]['row_total'], array('currency_symbol' => true));}
402                                 $fileContents .= '">'."\n";
403                                 if(isset($stageArr[$key]['people'])){
404                                         asort($stageArr[$key]['people']);
405                                         reset($stageArr[$key]['people']);
406                                         foreach ($stageArr[$key]['people'] as $nameKey=>$nameValue) {
407                                                 $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";
408                                         }
409                                 }
410                                 $fileContents .= '     </dataRow>'."\n";
411                         }
412                         $fileContents .= '     </yData>'."\n";
413                         $max = get_max($rowTotalArr);
414                         if($chart_size=='hBarF'){
415                                 $length = "10";
416                         }else{
417                                 $length = "4";
418                         }
419                         $fileContents .= '     <xData min="0" max="'.$max.'" length="'.$length.'" prefix="'.$symbol.'" suffix="" kDelim="'.$kDelim.'" />'."\n";
420                         $fileContents .= '     <colorLegend status="on">'."\n";
421                         $i=0;
422                         asort($new_ids);
423                         foreach ($new_ids as $key=>$value) {
424                         $color = generate_graphcolor($key,$i);
425                         $fileContents .= '          <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
426                         $i++;
427                         }
428                         $fileContents .= '     </colorLegend>'."\n";
429                         $fileContents .= '     <graphInfo>'."\n";
430                         $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";
431                         $fileContents .= '     </graphInfo>'."\n";
432                         $fileContents .= '     <chartColors ';
433                         foreach ($barChartColors as $key => $value) {
434                                 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
435                         }
436                         $fileContents .= ' />'."\n";
437                         $fileContents .= '</graphData>'."\n";
438                         $total = $total;
439                         $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
440                         $fileContents = $title.$fileContents;
441
442                         save_xml_file($cache_file_name, $fileContents);
443                 }
444
445                 if($chart_size=='hBarF'){
446                         $width = "800";
447                         $height = "400";
448                 } else {
449                         $width = "350";
450                         $height = "400";
451                 }
452                 $return = create_chart($chart_size,$cache_file_name,$width,$height);
453                 return $return;
454         }
455
456         function constructQuery(){
457                 global $current_user;
458                 global $timedate;
459                 global $app_list_strings;
460
461                 //get the dates to display
462                 $user_date_start = $current_user->getPreference('pbss_date_start');
463
464                 if (!empty($user_date_start) && !isset($_REQUEST['pbss_date_start'])) {
465                         $date_start = $timedate->to_display_date($user_date_start, false);
466                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
467                         $GLOBALS['log']->debug($user_date_start);
468                 }
469                 elseif (isset($_REQUEST['pbss_date_start']) && $_REQUEST['pbss_date_start'] != '') {
470                         $date_start = $_REQUEST['pbss_date_start'];
471                         $ds = $timedate->to_db_date($date_start, false);
472                         $current_user->setPreference('pbss_date_start', $ds);
473                         $GLOBALS['log']->debug("_REQUEST['pbss_date_start'] is:");
474                         $GLOBALS['log']->debug($_REQUEST['pbss_date_start']);
475                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
476                         $GLOBALS['log']->debug($current_user->getPreference('pbss_date_start'));
477                 }
478                 else {
479                         $date_start = $timedate->nowDate();
480                 }
481
482                 $user_date_end = $current_user->getPreference('pbss_date_end');
483                 if (!empty($user_date_end) && !isset($_REQUEST['pbss_date_end'])) {
484                         $date_end = $timedate->to_display_date($user_date_end, false);
485                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
486                         $GLOBALS['log']->debug($user_date_end);
487                 }
488                 elseif (isset($_REQUEST['pbss_date_end']) && $_REQUEST['pbss_date_end'] != '') {
489                         $date_end = $_REQUEST['pbss_date_end'];
490                         $de = $timedate->to_db_date($date_end, false);
491                     $current_user->setPreference('pbss_date_end', $de);
492                         $GLOBALS['log']->debug("_REQUEST['pbss_date_end'] is:");
493                         $GLOBALS['log']->debug($_REQUEST['pbss_date_end']);
494                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
495                         $GLOBALS['log']->debug( $current_user->getPreference('pbss_date_end'));
496                 }
497                 else {
498                         $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
499                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] not found. Using: ".$date_end);
500                 }
501
502 $tempx = array();
503 $datax = array();
504 $datax_selected= array();
505 $user_tempx = $current_user->getPreference('pbss_sales_stages');
506 //get list of sales stage keys to display
507 if (!empty($user_tempx) && count($user_tempx) > 0 && !isset($_REQUEST['pbss_sales_stages'])) {
508         $tempx = $user_tempx ;
509         $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
510         $GLOBALS['log']->debug($user_tempx );
511 }
512 elseif (isset($_REQUEST['pbss_sales_stages']) && count($_REQUEST['pbss_sales_stages']) > 0) {
513         $tempx = $_REQUEST['pbss_sales_stages'];
514         $current_user->setPreference('pbss_sales_stages', $_REQUEST['pbss_sales_stages']);
515         $GLOBALS['log']->debug("_REQUEST['pbss_sales_stages'] is:");
516         $GLOBALS['log']->debug($_REQUEST['pbss_sales_stages']);
517         $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
518         $GLOBALS['log']->debug($current_user->getPreference('pbss_sales_stages'));
519 }
520
521 //set $datax using selected sales stage keys
522 if (count($tempx) > 0) {
523         foreach ($tempx as $key) {
524                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
525                 array_push($datax_selected,$key);
526         }
527 }
528 else {
529         $datax = $app_list_strings['sales_stage_dom'];
530         $datax_selected = array_keys($app_list_strings['sales_stage_dom']);
531 }
532 $GLOBALS['log']->debug("datax is:");
533 $GLOBALS['log']->debug($datax);
534
535
536
537                 $ids = array();
538                 $new_ids = array();
539                 $user_ids = $current_user->getPreference('pbss_ids');
540                 //get list of user ids for which to display data
541                 if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbss_ids'])) {
542                         $ids = $user_ids;
543
544                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
545                         $GLOBALS['log']->debug($user_ids);
546                 }
547                 elseif (isset($_REQUEST['pbss_ids']) && count($_REQUEST['pbss_ids']) > 0) {
548                         $ids = $_REQUEST['pbss_ids'];
549                         $current_user->setPreference('pbss_ids', $_REQUEST['pbss_ids']);
550                         $GLOBALS['log']->debug("_REQUEST['pbss_ids'] is:");
551                         $GLOBALS['log']->debug($_REQUEST['pbss_ids']);
552                         $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
553                         $GLOBALS['log']->debug($current_user->getPreference('pbss_ids'));
554                 }
555                 else {
556                         $ids = get_user_array(false);
557                         $ids = array_keys($ids);
558
559                 }
560
561                 $user_id = $ids;
562                 $opp = new Opportunity;
563                 $where="";
564                 //build the where clause for the query that matches $user
565                 $count = count($user_id);
566                 $id = array();
567                 $user_list = get_user_array(false);
568                 foreach ($user_id as $key) {
569                         $new_ids[$key] = $user_list[$key];
570                 }
571                 if ($count>0) {
572                         foreach ($new_ids as $the_id=>$the_name) {
573                                 $id[] = "'".$the_id."'";
574                         }
575                         $ids = join(",",$id);
576                         $where .= "opportunities.assigned_user_id IN ($ids) ";
577
578                 }
579                 //build the where clause for the query that matches $datax
580                 $count = count($datax);
581                 $dataxArr = array();
582                 if ($count>0) {
583
584                         foreach ($datax as $key=>$value) {
585                                 $dataxArr[] = "'".$key."'";
586                         }
587                         $dataxArr = join(",",$dataxArr);
588                         $where .= "AND opportunities.sales_stage IN     ($dataxArr) ";
589                 }
590
591                 //build the where clause for the query that matches $date_start and $date_end
592                 $where .= "     AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
593                                         AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
594                 $where .= "     AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
595
596                 //Now do the db queries
597                 //query for opportunity data that matches $datax and $user
598                 $query = "      SELECT opportunities.sales_stage,
599                                                 users.user_name,
600                                                 opportunities.assigned_user_id,
601                                                 count( * ) AS opp_count,
602                                                 sum(amount_usdollar/1000) AS total
603                                         FROM users,opportunities  ";
604                 $query .= "WHERE " .$where;
605                 $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
606
607                 return $query;
608         }
609
610         function constructGroupBy(){
611                 return array( 'sales_stage', 'user_name' );
612         }
613
614 }
615
616 ?>