]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/code/Chart_my_pipeline_by_sales_stage.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Charts / code / Chart_my_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 require_once("include/charts/Charts.php");
49 require_once("modules/Charts/code/Chart_pipeline_by_sales_stage.php");
50
51 global $app_list_strings, $current_language, $sugar_config, $currentModule, $action, $current_user, $theme, $timedate;
52 $user_dateFormat = $timedate->get_date_format();
53 $current_module_strings = return_module_language($current_language, 'Charts');
54
55 global $timedate;
56
57 if (isset($_REQUEST['mypbss_refresh'])) { $refresh = $_REQUEST['mypbss_refresh']; }
58 else { $refresh = false; }
59
60 //get the dates to display
61 $user_date_start = $current_user->getPreference('mypbss_date_start');
62
63 if (!empty($user_date_start) && !isset($_REQUEST['mypbss_date_start'])) {
64         $date_start = $user_date_start;
65         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
66         $GLOBALS['log']->debug($user_date_start);
67 }
68 elseif (isset($_REQUEST['mypbss_date_start']) && $_REQUEST['mypbss_date_start'] != '') {
69         $date_start = $_REQUEST['mypbss_date_start'];
70         $current_user->setPreference('mypbss_date_start', $_REQUEST['mypbss_date_start']);
71         $GLOBALS['log']->debug("_REQUEST['mypbss_date_start'] is:");
72         $GLOBALS['log']->debug($_REQUEST['mypbss_date_start']);
73         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
74         $GLOBALS['log']->debug($current_user->getPreference('mypbss_date_start'));
75 }
76 else {
77         $date_start = $timedate->nowDate();
78 }
79 $user_date_end = $current_user->getPreference('mypbss_date_end');
80
81 if (!empty($user_date_end) && !isset($_REQUEST['mypbss_date_end'])) {
82         $date_end = $user_date_end;
83         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
84         $GLOBALS['log']->debug($user_date_end);
85 }
86 elseif (isset($_REQUEST['mypbss_date_end']) && $_REQUEST['mypbss_date_end'] != '') {
87         $date_end = $_REQUEST['mypbss_date_end'];
88         $current_user->setPreference('mypbss_date_end', $_REQUEST['mypbss_date_end']);
89         $GLOBALS['log']->debug("_REQUEST['mypbss_date_end'] is:");
90         $GLOBALS['log']->debug($_REQUEST['mypbss_date_end']);
91         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
92         $GLOBALS['log']->debug( $current_user->getPreference('mypbss_date_end'));
93 }
94 else {
95         $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
96         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
97 }
98
99 // cn: format date_start|end to user's preferred
100 $dateStartDisplay = strftime($timedate->get_user_date_format(), strtotime($date_start));
101 $dateEndDisplay         = strftime($timedate->get_user_date_format(), strtotime($date_end));
102 $seps                           = array("-", "/");
103 $dates                          = array($date_start, $date_end);
104 $dateFileNameSafe       = str_replace($seps, "_", $dates);
105 $dateXml[0]                     = $timedate->swap_formats($date_start, $user_dateFormat, $timedate->dbDayFormat);
106 $dateXml[1]                     = $timedate->swap_formats($date_end, $user_dateFormat, $timedate->dbDayFormat);
107
108 $tempx = array();
109 $datax = array();
110 $selected_datax = array();
111 //get list of sales stage keys to display
112 $user_sales_stage = $current_user->getPreference('mypbss_sales_stages');
113 if (!empty($user_sales_stage) && count($user_sales_stage) > 0 && !isset($_REQUEST['mypbss_sales_stages'])) {
114         $tempx = $user_sales_stage;
115         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_sales_stages'] is:");
116         $GLOBALS['log']->debug($user_sales_stage);
117 }
118 elseif (isset($_REQUEST['mypbss_sales_stages']) && count($_REQUEST['mypbss_sales_stages']) > 0) {
119         $tempx = $_REQUEST['mypbss_sales_stages'];
120         $current_user->setPreference('mypbss_sales_stages', $_REQUEST['mypbss_sales_stages']);
121         $GLOBALS['log']->debug("_REQUEST['mypbss_sales_stages'] is:");
122         $GLOBALS['log']->debug($_REQUEST['mypbss_sales_stages']);
123         $GLOBALS['log']->debug("USER PREFRENCES['mypbss_sales_stages'] is:");
124         $GLOBALS['log']->debug($current_user->getPreference('mypbss_sales_stages'));
125 }
126
127 //set $datax using selected sales stage keys
128 if (count($tempx) > 0) {
129         foreach ($tempx as $key) {
130                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
131                 array_push($selected_datax, $key);
132         }
133 }
134 else {
135         $datax = $app_list_strings['sales_stage_dom'];
136         $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
137 }
138 $GLOBALS['log']->debug("datax is:");
139 $GLOBALS['log']->debug($datax);
140
141 $ids = array($current_user->id);
142 //create unique prefix based on selected users for image files
143 $id_hash = '1';
144 if (isset($ids)) {
145         sort($ids);
146         $id_hash = crc32(implode('',$ids));
147         if($id_hash < 0)
148         {
149         $id_hash = $id_hash * -1;
150         }
151 }
152 $GLOBALS['log']->debug("ids is:");
153 $GLOBALS['log']->debug($ids);
154 $id_md5 = substr(md5($current_user->id),0,9);
155 $seps                           = array("-", "/");
156 $dates                          = array($dateStartDisplay, $dateEndDisplay);
157 $dateFileNameSafe       = str_replace($seps, "_", $dates);
158 $cache_file_name = sugar_cached("xml/").$current_user->getUserPrivGuid()."_".$theme."_my_pipeline_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
159
160 $GLOBALS['log']->debug("cache file name is: $cache_file_name");
161
162
163 $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&mypbss_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(\'my_pipeline_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></div>';
164
165 ?>
166         <?php echo get_form_header($mod_strings['LBL_PIPELINE_FORM_TITLE'], $tools , false);?>
167
168 <?php
169         global $timedate;
170         $cal_lang = "en";
171         $cal_dateformat = $timedate->get_cal_date_format();
172 ?>
173 <p>
174 <div id='my_pipeline_edit' style='display: none;'>
175 <form name='my_pipeline' action="index.php" method="post" >
176 <input type="hidden" name="module" value="<?php echo $currentModule;?>">
177 <input type="hidden" name="action" value="<?php echo $action;?>">
178 <input type="hidden" name="mypbss_refresh" value="true">
179 <table cellpadding="0" cellspacing="0" border="0" class="edit view" align="center">
180 <tr>
181         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_START']?> </b><br><i><?php echo $timedate->get_user_date_format();?></i></td>
182         <td valign='top' ><input onblur="parseDate(this, '<?php echo $cal_dateformat ?>');" class="text" name="mypbss_date_start" size='12' maxlength='10' id='date_start' value='<?php echo $date_start; ?>'> <?php echo SugarThemeRegistry::current()->getImage('jscalendar', 'id="date_start_trigger" align="absmiddle"', null, null, ".gif", $app_strings['LBL_ENTER_DATE']); ?> </td>
183 </tr>
184
185 <tr>
186         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_END'];?></b><br><i><?php echo $timedate->get_user_date_format();?></i></td>
187         <td valign='top' ><input onblur="parseDate(this, '<?php echo $cal_dateformat ?>');" class="text" name="mypbss_date_end" size='12' maxlength='10' id='date_end' value='<?php echo $date_end; ?>'>  <?php echo SugarThemeRegistry::current()->getImage('jscalendar', 'id="date_end_trigger" align="absmiddle"', null, null, ".gif", $app_strings['LBL_ENTER_DATE']); ?> </td>
188 </tr>
189
190         <tr>
191         <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_SALES_STAGES'];?></b></td>
192         <td valign='top' ><select name="mypbss_sales_stages[]" multiple size='3'><?php echo get_select_options_with_id($app_list_strings['sales_stage_dom'],$selected_datax); ?></select></td>
193         </tr>
194
195 <tr>
196         <td align="right" colspan="2"><input class="button" onclick="return verify_chart_data(my_pipeline);" 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('my_pipeline_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>
197 </tr>
198 </table>
199 </form>
200 <script type="text/javascript">
201 Calendar.setup ({
202         inputField : "date_start", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_start_trigger", singleClick : true, step : 1, weekNumbers:false
203 });
204 Calendar.setup ({
205         inputField : "date_end", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_end_trigger", singleClick : true, step : 1, weekNumbers:false
206 });
207 </script>
208 </div>
209 </p>
210
211 <?php
212
213 echo "<p align='center'>".gen_xml_pipeline_by_sales_stage($datax, $dateXml[0], $dateXml[1], $ids, $cache_file_name, $refresh,'hBarS',$current_module_strings)."</p>";
214 echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_PIPELINE_FORM_TITLE_DESC']."</span></P>";
215
216
217         if (file_exists($cache_file_name)) {
218                 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($cache_file_name)));
219         }
220         else {
221                 $file_date = '';
222         }
223
224 ?>
225 <span class='chartFootnote'>
226 <p align="right"><i><?php  echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
227 </span>
228
229 <?php
230 echo get_validate_chart_js();
231
232         /**
233         * Creates opportunity pipeline image as a HORIZONTAL accumlated BAR GRAPH for multiple users.
234         * param $datax- the sales stage data to display in the x-axis
235         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
236         * All Rights Reserved..
237         * Contributor(s): ______________________________________..
238         */
239         function gen_xml_pipeline_by_sales_stage($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) {
240                 global $app_strings, $charset, $lang, $barChartColors, $current_user;
241
242                 $kDelim = $current_user->getPreference('num_grp_sep');
243                 global $timedate;
244
245                 if (!file_exists($cache_file_name) || $refresh == true) {
246
247                         $GLOBALS['log']->debug("starting pipeline chart");
248                         $GLOBALS['log']->debug("datax is:");
249                         $GLOBALS['log']->debug($datax);
250                         $GLOBALS['log']->debug("user_id is: ");
251                         $GLOBALS['log']->debug($user_id);
252                         $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
253                         $opp = new Opportunity;
254                         $where="";
255                         //build the where clause for the query that matches $user
256                         $count = count($user_id);
257                         $id = array();
258                         $user_list = get_user_array(false);
259                         foreach ($user_id as $key) {
260                                 $new_ids[$key] = $user_list[$key];
261                         }
262                         if ($count>0) {
263                                 foreach ($new_ids as $the_id=>$the_name) {
264                                         $id[] = "'".$the_id."'";
265                                 }
266                                 $ids = join(",",$id);
267                                 $where .= "opportunities.assigned_user_id IN ($ids) ";
268
269                         }
270                         //build the where clause for the query that matches $datax
271                         $count = count($datax);
272                         $dataxArr = array();
273                         if ($count>0) {
274
275                                 foreach ($datax as $key=>$value) {
276                                         $dataxArr[] = "'".$key."'";
277                                 }
278                                 $dataxArr = join(",",$dataxArr);
279                                 $where .= "AND opportunities.sales_stage IN     ($dataxArr) ";
280                         }
281
282                         //build the where clause for the query that matches $date_start and $date_end
283                         $where .= "     AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
284                                                 AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
285                         $where .= "     AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
286
287                         //Now do the db queries
288                         //query for opportunity data that matches $datax and $user
289                         $query = "      SELECT opportunities.sales_stage,
290                                                         users.user_name,
291                                                         opportunities.assigned_user_id,
292                                                         count( * ) AS opp_count,
293                                                         sum(amount_usdollar/1000) AS total
294                                                 FROM users,opportunities  ";
295                         $query .= "WHERE " .$where;
296                         $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
297
298                         $result = $opp->db->query($query, true);
299                         //build pipeline by sales stage data
300                         $total = 0;
301                         $div = 1;
302                         global $sugar_config;
303                         $symbol = $sugar_config['default_currency_symbol'];
304                         global $current_user;
305                         if($current_user->getPreference('currency') ){
306
307                                 $currency = new Currency();
308                                 $currency->retrieve($current_user->getPreference('currency'));
309                                 $div = $currency->conversion_rate;
310                                 $symbol = $currency->symbol;
311                         }
312                         // cn: adding user-pref date handling
313                         $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
314                         $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
315
316                         $fileContents = '     <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
317                         $stageArr = array();
318                         $usernameArr = array();
319                         $rowTotalArr = array();
320                         $rowTotalArr[] = 0;
321                         while($row = $opp->db->fetchByAssoc($result, false))
322                         {
323                                 if($row['total']*$div<=100){
324                                         $sum = round($row['total']*$div, 2);
325                                 } else {
326                                         $sum = round($row['total']*$div);
327                                 }
328                                 if(!isset($stageArr[$row['sales_stage']]['row_total'])) {$stageArr[$row['sales_stage']]['row_total']=0;}
329                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['opp_count'] = $row['opp_count'];
330                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['total'] = $sum;
331                                 $stageArr[$row['sales_stage']]['people'][$row['assigned_user_id']] = $row['user_name'];
332                                 $stageArr[$row['sales_stage']]['row_total'] += $sum;
333
334                                 $usernameArr[$row['assigned_user_id']] = $row['user_name'];
335                                 $total += $sum;
336                         }
337                         foreach ($datax as $key=>$translation) {
338                                 if(isset($stageArr[$key]['row_total'])){$rowTotalArr[]=$stageArr[$key]['row_total'];}
339                                 if(isset($stageArr[$key]['row_total']) && $stageArr[$key]['row_total']>100) {
340                                         $stageArr[$key]['row_total'] = round($stageArr[$key]['row_total']);
341                                 }
342                                 $fileContents .= '     <dataRow title="'.$translation.'" endLabel="';
343                                 if(isset($stageArr[$key]['row_total'])){$fileContents .= $stageArr[$key]['row_total'];}
344                                 $fileContents .= '">'."\n";
345                                 if(isset($stageArr[$key]['people'])){
346                                         asort($stageArr[$key]['people']);
347                                         reset($stageArr[$key]['people']);
348                                         foreach ($stageArr[$key]['people'] as $nameKey=>$nameValue) {
349                                                 $fileContents .= '          <bar id="'.$nameKey.'" totalSize="'.$stageArr[$key][$nameKey]['total'].'" altText="'.$nameValue.': '.$stageArr[$key][$nameKey]['opp_count'].' '.$current_module_strings['LBL_OPPS_WORTH'].' '.currency_format_number($stageArr[$key][$nameKey]['total'], array('currency_symbol'=>true)).$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";
350                                         }
351                                 }
352                                 $fileContents .= '     </dataRow>'."\n";
353                         }
354                         $fileContents .= '     </yData>'."\n";
355                         $max = get_max($rowTotalArr);
356                         if($chart_size=='hBarF'){
357                                 $length = "10";
358                         }else{
359                                 $length = "4";
360                         }
361                         $fileContents .= '     <xData min="0" max="'.$max.'" length="'.$length.'" kDelim="'.$kDelim.'" prefix="'.$symbol.'" suffix=""/>'."\n";
362                         $fileContents .= '     <colorLegend status="on">'."\n";
363                         $i=0;
364                         asort($new_ids);
365                         foreach ($new_ids as $key=>$value) {
366                         $color = generate_graphcolor($key,$i);
367                         $fileContents .= '          <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
368                         $i++;
369                         }
370                         $fileContents .= '     </colorLegend>'."\n";
371                         $fileContents .= '     <graphInfo>'."\n";
372                         $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";
373                         $fileContents .= '     </graphInfo>'."\n";
374                         $fileContents .= '     <chartColors ';
375                         foreach ($barChartColors as $key => $value) {
376                                 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
377                         }
378                         $fileContents .= ' />'."\n";
379                         $fileContents .= '</graphData>'."\n";
380                         $total = $total;
381                         $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
382                         $fileContents = $title.$fileContents;
383
384                         save_xml_file($cache_file_name, $fileContents);
385                 }
386
387                 if($chart_size=='hBarF'){
388                         $width = "800";
389                         $height = "400";
390                 } else {
391                         $width = "350";
392                         $height = "400";
393                 }
394                 $return = create_chart($chart_size,$cache_file_name,$width,$height);
395                 return $return;
396         }
397
398         function constructQuery(){
399                 global $current_user;
400                 global $timedate;
401
402                 //get the dates to display
403                 $user_date_start = $current_user->getPreference('mypbss_date_start');
404
405                 if (!empty($user_date_start) && !isset($_REQUEST['mypbss_date_start'])) {
406                         $date_start = $user_date_start;
407                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
408                         $GLOBALS['log']->debug($user_date_start);
409                 }
410                 elseif (isset($_REQUEST['mypbss_date_start']) && $_REQUEST['mypbss_date_start'] != '') {
411                         $date_start = $_REQUEST['mypbss_date_start'];
412                         $current_user->setPreference('mypbss_date_start', $_REQUEST['mypbss_date_start']);
413                         $GLOBALS['log']->debug("_REQUEST['mypbss_date_start'] is:");
414                         $GLOBALS['log']->debug($_REQUEST['mypbss_date_start']);
415                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
416                         $GLOBALS['log']->debug($current_user->getPreference('mypbss_date_start'));
417                 }
418                 else {
419                         $date_start = $timedate->nowDate();
420                 }
421                 $user_date_end = $current_user->getPreference('mypbss_date_end');
422
423                 if (!empty($user_date_end) && !isset($_REQUEST['mypbss_date_end'])) {
424                         $date_end = $user_date_end;
425                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
426                         $GLOBALS['log']->debug($user_date_end);
427                 }
428                 elseif (isset($_REQUEST['mypbss_date_end']) && $_REQUEST['mypbss_date_end'] != '') {
429                         $date_end = $_REQUEST['mypbss_date_end'];
430                         $current_user->setPreference('mypbss_date_end', $_REQUEST['mypbss_date_end']);
431                         $GLOBALS['log']->debug("_REQUEST['mypbss_date_end'] is:");
432                         $GLOBALS['log']->debug($_REQUEST['mypbss_date_end']);
433                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
434                         $GLOBALS['log']->debug( $current_user->getPreference('mypbss_date_end'));
435                 }
436                 else {
437                         $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
438                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
439                 }
440
441                 $user_id = array($current_user->id);
442
443                 $opp = new Opportunity;
444                 $where="";
445                 //build the where clause for the query that matches $user
446                 $count = count($user_id);
447                 $id = array();
448                 $user_list = get_user_array(false);
449                 foreach ($user_id as $key) {
450                         $new_ids[$key] = $user_list[$key];
451                 }
452                 if ($count>0) {
453                         foreach ($new_ids as $the_id=>$the_name) {
454                                 $id[] = "'".$the_id."'";
455                         }
456                         $ids = join(",",$id);
457                         $where .= "opportunities.assigned_user_id IN ($ids) ";
458                 }
459                 //build the where clause for the query that matches $datax
460                 $count = count($datax);
461                 $dataxArr = array();
462                 if ($count>0) {
463                         foreach ($datax as $key=>$value) {
464                                 $dataxArr[] = "'".$key."'";
465                         }
466                         $dataxArr = join(",",$dataxArr);
467                         $where .= "AND opportunities.sales_stage IN     ($dataxArr) ";
468                 }
469
470                 //build the where clause for the query that matches $date_start and $date_end
471                 $where .= "     AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
472                                         AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
473                 $where .= "     AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
474
475                 //Now do the db queries
476                 //query for opportunity data that matches $datax and $user
477                 $query = "      SELECT opportunities.sales_stage,
478                                                 users.user_name,
479                                                 opportunities.assigned_user_id,
480                                                 count( * ) AS opp_count,
481                                                 sum(amount_usdollar/1000) AS total
482                                         FROM users,opportunities  ";
483                 $query .= "WHERE " .$where;
484                 $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
485
486                 return $query;
487
488         }
489
490         function constructGroupBy(){
491                 return array('sales_stage');
492         }
493
494 ?>