]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/code/Chart_my_pipeline_by_sales_stage.php
Release 6.1.4
[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 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 = date($timedate->get_date_format(), time());
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 = date($timedate->get_date_format(), strtotime('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 = $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','alt="Refresh"  border="0" align="absmiddle"').'&nbsp;'.$current_module_strings['LBL_REFRESH'].'</a>&nbsp;&nbsp;<a href="javascript: toggleDisplay(\'my_pipeline_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','alt="Edit"  border="0"  align="absmiddle"').'&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; ?>'>  <img src="<?php echo SugarThemeRegistry::current()->getImageURL('jscalendar.gif'); ?>" alt="<?php echo $app_strings['LBL_ENTER_DATE']; ?>"  id="date_start_trigger" align="absmiddle"> </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; ?>'>  <img src="<?php echo SugarThemeRegistry::current()->getImageURL('jscalendar.gif'); ?>" alt="<?php echo $app_strings['LBL_ENTER_DATE']; ?>"  id="date_end_trigger" align="absmiddle"> </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, $sugar_config['tmp_dir'].$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($sugar_config['tmp_dir'].$cache_file_name)) {
218                 $file_date = date($timedate->get_date_format()." ".$timedate->get_time_format(), filemtime($sugar_config['tmp_dir'].$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)
299                         or sugar_die("Error selecting sugarbean: ".mysql_error());
300                         //build pipeline by sales stage data
301                         $total = 0;
302                         $div = 1;
303                         global $sugar_config;
304                         $symbol = $sugar_config['default_currency_symbol'];
305                         global $current_user;
306                         if($current_user->getPreference('currency') ){
307                                 
308                                 $currency = new Currency();
309                                 $currency->retrieve($current_user->getPreference('currency'));
310                                 $div = $currency->conversion_rate;
311                                 $symbol = $currency->symbol;
312                         }
313                         // cn: adding user-pref date handling
314                         $dateStartDisplay = date($timedate->get_date_format(), strtotime($date_start));
315                         $dateEndDisplay = date($timedate->get_date_format(), strtotime($date_end));
316                         
317                         $fileContents = '     <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
318                         $stageArr = array();
319                         $usernameArr = array();
320                         $rowTotalArr = array();
321                         $rowTotalArr[] = 0;
322                         while($row = $opp->db->fetchByAssoc($result, -1, false))
323                         {
324                                 if($row['total']*$div<=100){
325                                         $sum = round($row['total']*$div, 2);
326                                 } else {
327                                         $sum = round($row['total']*$div);
328                                 }
329                                 if(!isset($stageArr[$row['sales_stage']]['row_total'])) {$stageArr[$row['sales_stage']]['row_total']=0;}
330                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['opp_count'] = $row['opp_count'];
331                                 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['total'] = $sum;
332                                 $stageArr[$row['sales_stage']]['people'][$row['assigned_user_id']] = $row['user_name'];
333                                 $stageArr[$row['sales_stage']]['row_total'] += $sum;
334
335                                 $usernameArr[$row['assigned_user_id']] = $row['user_name'];
336                                 $total += $sum;
337                         }
338                         foreach ($datax as $key=>$translation) {
339                                 if(isset($stageArr[$key]['row_total'])){$rowTotalArr[]=$stageArr[$key]['row_total'];}
340                                 if(isset($stageArr[$key]['row_total']) && $stageArr[$key]['row_total']>100) {
341                                         $stageArr[$key]['row_total'] = round($stageArr[$key]['row_total']);
342                                 }
343                                 $fileContents .= '     <dataRow title="'.$translation.'" endLabel="';
344                                 if(isset($stageArr[$key]['row_total'])){$fileContents .= $stageArr[$key]['row_total'];}
345                                 $fileContents .= '">'."\n";
346                                 if(isset($stageArr[$key]['people'])){
347                                         asort($stageArr[$key]['people']);
348                                         reset($stageArr[$key]['people']);
349                                         foreach ($stageArr[$key]['people'] as $nameKey=>$nameValue) {
350                                                 $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";
351                                         }
352                                 }
353                                 $fileContents .= '     </dataRow>'."\n";
354                         }
355                         $fileContents .= '     </yData>'."\n";
356                         $max = get_max($rowTotalArr);
357                         if($chart_size=='hBarF'){
358                                 $length = "10";
359                         }else{
360                                 $length = "4";
361                         }
362                         $fileContents .= '     <xData min="0" max="'.$max.'" length="'.$length.'" kDelim="'.$kDelim.'" prefix="'.$symbol.'" suffix=""/>'."\n";
363                         $fileContents .= '     <colorLegend status="on">'."\n";
364                         $i=0;
365                         asort($new_ids);
366                         foreach ($new_ids as $key=>$value) {
367                         $color = generate_graphcolor($key,$i);
368                         $fileContents .= '          <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
369                         $i++;
370                         }
371                         $fileContents .= '     </colorLegend>'."\n";
372                         $fileContents .= '     <graphInfo>'."\n";
373                         $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";
374                         $fileContents .= '     </graphInfo>'."\n";
375                         $fileContents .= '     <chartColors ';
376                         foreach ($barChartColors as $key => $value) {
377                                 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
378                         }
379                         $fileContents .= ' />'."\n";
380                         $fileContents .= '</graphData>'."\n";
381                         $total = $total;
382                         $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
383                         $fileContents = $title.$fileContents;
384
385                         save_xml_file($cache_file_name, $fileContents);
386                 }
387
388                 if($chart_size=='hBarF'){
389                         $width = "800";
390                         $height = "400";
391                 } else {
392                         $width = "350";
393                         $height = "400";
394                 }
395                 $return = create_chart($chart_size,$cache_file_name,$width,$height);
396                 return $return;
397         }
398         
399         function constructQuery(){
400                 global $current_user;
401                 global $time_date;
402                 
403                 //get the dates to display
404                 $user_date_start = $current_user->getPreference('mypbss_date_start');
405                 
406                 if (!empty($user_date_start) && !isset($_REQUEST['mypbss_date_start'])) {
407                         $date_start = $user_date_start;
408                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
409                         $GLOBALS['log']->debug($user_date_start);
410                 }
411                 elseif (isset($_REQUEST['mypbss_date_start']) && $_REQUEST['mypbss_date_start'] != '') {
412                         $date_start = $_REQUEST['mypbss_date_start'];
413                         $current_user->setPreference('mypbss_date_start', $_REQUEST['mypbss_date_start']);
414                         $GLOBALS['log']->debug("_REQUEST['mypbss_date_start'] is:");
415                         $GLOBALS['log']->debug($_REQUEST['mypbss_date_start']);
416                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
417                         $GLOBALS['log']->debug($current_user->getPreference('mypbss_date_start'));
418                 }
419                 else {
420                         $date_start = date($timedate->get_date_format(), time());
421                 }
422                 $user_date_end = $current_user->getPreference('mypbss_date_end');
423                 
424                 if (!empty($user_date_end) && !isset($_REQUEST['mypbss_date_end'])) {
425                         $date_end = $user_date_end;
426                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
427                         $GLOBALS['log']->debug($user_date_end);
428                 }
429                 elseif (isset($_REQUEST['mypbss_date_end']) && $_REQUEST['mypbss_date_end'] != '') {
430                         $date_end = $_REQUEST['mypbss_date_end'];
431                         $current_user->setPreference('mypbss_date_end', $_REQUEST['mypbss_date_end']);
432                         $GLOBALS['log']->debug("_REQUEST['mypbss_date_end'] is:");
433                         $GLOBALS['log']->debug($_REQUEST['mypbss_date_end']);
434                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
435                         $GLOBALS['log']->debug( $current_user->getPreference('mypbss_date_end'));
436                 }
437                 else {
438                         $date_end = date($timedate->get_date_format(), strtotime('2010-01-01'));
439                         $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
440                 }
441                 
442                 $user_id = array($current_user->id);
443                                 
444                 $opp = new Opportunity;
445                 $where="";
446                 //build the where clause for the query that matches $user
447                 $count = count($user_id);
448                 $id = array();
449                 $user_list = get_user_array(false);
450                 foreach ($user_id as $key) {
451                         $new_ids[$key] = $user_list[$key];
452                 }
453                 if ($count>0) {
454                         foreach ($new_ids as $the_id=>$the_name) {
455                                 $id[] = "'".$the_id."'";
456                         }
457                         $ids = join(",",$id);
458                         $where .= "opportunities.assigned_user_id IN ($ids) ";
459                 }
460                 //build the where clause for the query that matches $datax
461                 $count = count($datax);
462                 $dataxArr = array();
463                 if ($count>0) {
464                         foreach ($datax as $key=>$value) {
465                                 $dataxArr[] = "'".$key."'";
466                         }
467                         $dataxArr = join(",",$dataxArr);
468                         $where .= "AND opportunities.sales_stage IN     ($dataxArr) ";
469                 }
470
471                 //build the where clause for the query that matches $date_start and $date_end
472                 $where .= "     AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). " 
473                                         AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
474                 $where .= "     AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
475
476                 //Now do the db queries
477                 //query for opportunity data that matches $datax and $user
478                 $query = "      SELECT opportunities.sales_stage,
479                                                 users.user_name,
480                                                 opportunities.assigned_user_id,
481                                                 count( * ) AS opp_count,
482                                                 sum(amount_usdollar/1000) AS total
483                                         FROM users,opportunities  ";
484                 $query .= "WHERE " .$where;
485                 $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
486
487                 return $query;
488         
489         }
490         
491         function constructGroupBy(){
492                 return array('sales_stage');
493         }
494
495 ?>