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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
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 ********************************************************************************/
49 require_once('include/charts/Charts.php');
53 class Chart_pipeline_by_sales_stage
55 var $modules = array('Opportunities');
57 function Chart_pipeline_by_sales_stage()
61 function draw($extra_tools)
67 global $app_list_strings;
69 global $current_language;
71 global $currentModule;
76 $user_dateFormat = $timedate->get_date_format();
77 $current_module_strings = return_module_language($current_language, 'Charts');
80 if (isset($_REQUEST['pbss_refresh'])) { $refresh = $_REQUEST['pbss_refresh']; }
81 else { $refresh = false; }
83 //get the dates to display
84 $user_date_start = $current_user->getPreference('pbss_date_start');
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);
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'));
101 $date_start = $timedate->nowDate();
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);
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'));
120 $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
121 $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] not found. Using: ".$date_end);
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);
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 );
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'));
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);
160 $datax = $app_list_strings['sales_stage_dom'];
161 $datax_selected = array_keys($app_list_strings['sales_stage_dom']);
163 $GLOBALS['log']->debug("datax is:");
164 $GLOBALS['log']->debug($datax);
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'])) {
173 $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
174 $GLOBALS['log']->debug($user_ids);
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'));
185 $ids = get_user_array(false);
186 $ids = array_keys($ids);
190 //create unique prefix based on selected users for image files
194 $id_hash = crc32(implode('',$ids));
197 $id_hash = $id_hash * -1;
200 $GLOBALS['log']->debug("ids is:");
201 $GLOBALS['log']->debug($ids);
203 $cache_file_name = $current_user->getUserPrivGuid()."_lead_source_by_outcome_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
205 $GLOBALS['log']->debug("cache file name is: $cache_file_name");
207 $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&pbss_refresh=true" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('refresh','alt="Refresh" border="0" align="absmiddle"').' '.$current_module_strings['LBL_REFRESH'].'</a> <a href="javascript: toggleDisplay(\'pipeline_by_sales_stage_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','alt="Edit" border="0" align="absmiddle"').' '. $current_module_strings['LBL_EDIT'].'</a> '.$extra_tools.'</div>';
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>'; ?>
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'] = "";
218 // set populate values
219 $puser_date_start = $current_user->getPreference('user_date_start');
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">
230 <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_START']?></b> <br><span class="dateFormat"><?php echo "(".$timedate->get_user_date_format().")"; ?></span></td>
231 <td valign='top' ><input class="text" name="pbss_date_start" size='12' maxlength='10' id='date_start' value='<?php if (isset($date_start)) echo $date_start; ?>'> <img src="<?php echo SugarThemeRegistry::current()->getImageURL('jscalendar.gif'); ?>" alt="<?php echo $app_strings['LBL_ENTER_DATE']; ?>" id="date_start_trigger" align="absmiddle"> </td>
234 <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_END'];?></b><br><span class="dateFormat"><?php echo "(".$timedate->get_user_date_format().")"; ?></span></td>
235 <td valign='top' ><input class="text" name="pbss_date_end" size='12' maxlength='10' id='date_end' value='<?php if (isset($date_end)) echo $date_end; ?>'> <img src="<?php echo SugarThemeRegistry::current()->getImageURL('jscalendar.gif'); ?>" alt="<?php echo $app_strings['LBL_ENTER_DATE']; ?>" id="date_end_trigger" align="absmiddle"> </td>
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>
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>
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>
253 <script type="text/javascript">
255 inputField : "date_start", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_start_trigger", singleClick : true, step : 1, weekNumbers:false
258 inputField : "date_end", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_end_trigger", singleClick : true, step : 1, weekNumbers:false
266 echo "<P align='center'>".$this->gen_xml($datax, $dateXml[0], $dateXml[1], $ids, $sugar_config['tmp_dir'].$cache_file_name, $refresh,'hBarF',$current_module_strings)."</P>";
267 echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_SALES_STAGE_FORM_DESC']."</span></P>";
269 if (file_exists($sugar_config['tmp_dir'].$cache_file_name)) {
270 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($sugar_config['tmp_dir'].$cache_file_name)));
277 <span class='chartFootnote'>
278 <p align="right"><i><?php echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
281 echo get_validate_chart_js();
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): ______________________________________..
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;
297 $kDelim = $current_user->getPreference('num_grp_sep');
301 if (!file_exists($cache_file_name) || $refresh == true) {
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;
311 //build the where clause for the query that matches $user
312 $count = count($user_id);
314 $user_list = get_user_array(false);
315 foreach ($user_id as $key) {
316 $new_ids[$key] = $user_list[$key];
319 foreach ($new_ids as $the_id=>$the_name) {
320 $id[] = "'".$the_id."'";
322 $ids = join(",",$id);
323 $where .= "opportunities.assigned_user_id IN ($ids) ";
326 //build the where clause for the query that matches $datax
327 $count = count($datax);
331 foreach ($datax as $key=>$value) {
332 $dataxArr[] = "'".$key."'";
334 $dataxArr = join(",",$dataxArr);
335 $where .= "AND opportunities.sales_stage IN ($dataxArr) ";
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 ";
343 //Now do the db queries
344 //query for opportunity data that matches $datax and $user
345 $query = " SELECT opportunities.sales_stage,
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";
356 $result = $opp->db->query($query)
357 or sugar_die("Error selecting sugarbean: ".mysql_error());
358 //build pipeline by sales stage data
361 global $sugar_config;
362 $symbol = $sugar_config['default_currency_symbol'];
363 global $current_user;
364 if($current_user->getPreference('currency') ){
366 $currency = new Currency();
367 $currency->retrieve($current_user->getPreference('currency'));
368 $div = $currency->conversion_rate;
369 $symbol = $currency->symbol;
371 // cn: adding user-pref date handling
372 $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
373 $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
375 $fileContents = ' <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
377 $usernameArr = array();
378 $rowTotalArr = array();
380 while($row = $opp->db->fetchByAssoc($result, -1, false))
382 if($row['total']*$div<=100){
383 $sum = round($row['total']*$div, 2);
385 $sum = round($row['total']*$div);
387 if(!isset($stageArr[$row['sales_stage']]['row_total'])) {$stageArr[$row['sales_stage']]['row_total']=0;}
388 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['opp_count'] = $row['opp_count'];
389 $stageArr[$row['sales_stage']][$row['assigned_user_id']]['total'] = $sum;
390 $stageArr[$row['sales_stage']]['people'][$row['assigned_user_id']] = $row['user_name'];
391 $stageArr[$row['sales_stage']]['row_total'] += $sum;
393 $usernameArr[$row['assigned_user_id']] = $row['user_name'];
396 foreach ($datax as $key=>$translation) {
397 if(isset($stageArr[$key]['row_total'])){$rowTotalArr[]=$stageArr[$key]['row_total'];}
398 if(isset($stageArr[$key]['row_total']) && $stageArr[$key]['row_total']>100) {
399 $stageArr[$key]['row_total'] = round($stageArr[$key]['row_total']);
401 $fileContents .= ' <dataRow title="'.$translation.'" endLabel="';
402 if(isset($stageArr[$key]['row_total'])){$fileContents .= currency_format_number($stageArr[$key]['row_total'], array('currency_symbol' => true));}
403 $fileContents .= '">'."\n";
404 if(isset($stageArr[$key]['people'])){
405 asort($stageArr[$key]['people']);
406 reset($stageArr[$key]['people']);
407 foreach ($stageArr[$key]['people'] as $nameKey=>$nameValue) {
408 $fileContents .= ' <bar id="'.$nameKey.'" totalSize="'.$stageArr[$key][$nameKey]['total'].'" altText="'.$nameValue.': '.format_number($stageArr[$key][$nameKey]['opp_count'], 0, 0).' '.$current_module_strings['LBL_OPPS_WORTH'].' '.format_number($stageArr[$key][$nameKey]['total'], 2, 2).$current_module_strings['LBL_OPP_THOUSANDS'].' '.$current_module_strings['LBL_OPPS_IN_STAGE'].' '.$translation.'" url="index.php?module=Opportunities&action=index&assigned_user_id[]='.$nameKey.'&sales_stage='.urlencode($key).'&date_start='.$date_start.'&date_closed='.$date_end.'&query=true&searchFormTab=advanced_search"/>'."\n";
411 $fileContents .= ' </dataRow>'."\n";
413 $fileContents .= ' </yData>'."\n";
414 $max = get_max($rowTotalArr);
415 if($chart_size=='hBarF'){
420 $fileContents .= ' <xData min="0" max="'.$max.'" length="'.$length.'" prefix="'.$symbol.'" suffix="" kDelim="'.$kDelim.'" />'."\n";
421 $fileContents .= ' <colorLegend status="on">'."\n";
424 foreach ($new_ids as $key=>$value) {
425 $color = generate_graphcolor($key,$i);
426 $fileContents .= ' <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
429 $fileContents .= ' </colorLegend>'."\n";
430 $fileContents .= ' <graphInfo>'."\n";
431 $fileContents .= ' <![CDATA['.$current_module_strings['LBL_DATE_RANGE'].' '.$dateStartDisplay.' '.$current_module_strings['LBL_DATE_RANGE_TO'].' '.$dateEndDisplay.'<BR/>'.$current_module_strings['LBL_OPP_SIZE'].' '.$symbol.'1'.$current_module_strings['LBL_OPP_THOUSANDS'].']]>'."\n";
432 $fileContents .= ' </graphInfo>'."\n";
433 $fileContents .= ' <chartColors ';
434 foreach ($barChartColors as $key => $value) {
435 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
437 $fileContents .= ' />'."\n";
438 $fileContents .= '</graphData>'."\n";
440 $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
441 $fileContents = $title.$fileContents;
443 save_xml_file($cache_file_name, $fileContents);
446 if($chart_size=='hBarF'){
453 $return = create_chart($chart_size,$cache_file_name,$width,$height);
457 function constructQuery(){
458 global $current_user;
460 global $app_list_strings;
462 //get the dates to display
463 $user_date_start = $current_user->getPreference('pbss_date_start');
465 if (!empty($user_date_start) && !isset($_REQUEST['pbss_date_start'])) {
466 $date_start = $timedate->to_display_date($user_date_start, false);
467 $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
468 $GLOBALS['log']->debug($user_date_start);
470 elseif (isset($_REQUEST['pbss_date_start']) && $_REQUEST['pbss_date_start'] != '') {
471 $date_start = $_REQUEST['pbss_date_start'];
472 $ds = $timedate->to_db_date($date_start, false);
473 $current_user->setPreference('pbss_date_start', $ds);
474 $GLOBALS['log']->debug("_REQUEST['pbss_date_start'] is:");
475 $GLOBALS['log']->debug($_REQUEST['pbss_date_start']);
476 $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_start'] is:");
477 $GLOBALS['log']->debug($current_user->getPreference('pbss_date_start'));
480 $date_start = $timedate->nowDate();
483 $user_date_end = $current_user->getPreference('pbss_date_end');
484 if (!empty($user_date_end) && !isset($_REQUEST['pbss_date_end'])) {
485 $date_end = $timedate->to_display_date($user_date_end, false);
486 $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
487 $GLOBALS['log']->debug($user_date_end);
489 elseif (isset($_REQUEST['pbss_date_end']) && $_REQUEST['pbss_date_end'] != '') {
490 $date_end = $_REQUEST['pbss_date_end'];
491 $de = $timedate->to_db_date($date_end, false);
492 $current_user->setPreference('pbss_date_end', $de);
493 $GLOBALS['log']->debug("_REQUEST['pbss_date_end'] is:");
494 $GLOBALS['log']->debug($_REQUEST['pbss_date_end']);
495 $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] is:");
496 $GLOBALS['log']->debug( $current_user->getPreference('pbss_date_end'));
499 $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
500 $GLOBALS['log']->debug("USER PREFERENCES['pbss_date_end'] not found. Using: ".$date_end);
505 $datax_selected= array();
506 $user_tempx = $current_user->getPreference('pbss_sales_stages');
507 //get list of sales stage keys to display
508 if (!empty($user_tempx) && count($user_tempx) > 0 && !isset($_REQUEST['pbss_sales_stages'])) {
509 $tempx = $user_tempx ;
510 $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
511 $GLOBALS['log']->debug($user_tempx );
513 elseif (isset($_REQUEST['pbss_sales_stages']) && count($_REQUEST['pbss_sales_stages']) > 0) {
514 $tempx = $_REQUEST['pbss_sales_stages'];
515 $current_user->setPreference('pbss_sales_stages', $_REQUEST['pbss_sales_stages']);
516 $GLOBALS['log']->debug("_REQUEST['pbss_sales_stages'] is:");
517 $GLOBALS['log']->debug($_REQUEST['pbss_sales_stages']);
518 $GLOBALS['log']->debug("USER PREFERENCES['pbss_sales_stages'] is:");
519 $GLOBALS['log']->debug($current_user->getPreference('pbss_sales_stages'));
522 //set $datax using selected sales stage keys
523 if (count($tempx) > 0) {
524 foreach ($tempx as $key) {
525 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
526 array_push($datax_selected,$key);
530 $datax = $app_list_strings['sales_stage_dom'];
531 $datax_selected = array_keys($app_list_strings['sales_stage_dom']);
533 $GLOBALS['log']->debug("datax is:");
534 $GLOBALS['log']->debug($datax);
540 $user_ids = $current_user->getPreference('pbss_ids');
541 //get list of user ids for which to display data
542 if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbss_ids'])) {
545 $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
546 $GLOBALS['log']->debug($user_ids);
548 elseif (isset($_REQUEST['pbss_ids']) && count($_REQUEST['pbss_ids']) > 0) {
549 $ids = $_REQUEST['pbss_ids'];
550 $current_user->setPreference('pbss_ids', $_REQUEST['pbss_ids']);
551 $GLOBALS['log']->debug("_REQUEST['pbss_ids'] is:");
552 $GLOBALS['log']->debug($_REQUEST['pbss_ids']);
553 $GLOBALS['log']->debug("USER PREFERENCES['pbss_ids'] is:");
554 $GLOBALS['log']->debug($current_user->getPreference('pbss_ids'));
557 $ids = get_user_array(false);
558 $ids = array_keys($ids);
563 $opp = new Opportunity;
565 //build the where clause for the query that matches $user
566 $count = count($user_id);
568 $user_list = get_user_array(false);
569 foreach ($user_id as $key) {
570 $new_ids[$key] = $user_list[$key];
573 foreach ($new_ids as $the_id=>$the_name) {
574 $id[] = "'".$the_id."'";
576 $ids = join(",",$id);
577 $where .= "opportunities.assigned_user_id IN ($ids) ";
580 //build the where clause for the query that matches $datax
581 $count = count($datax);
585 foreach ($datax as $key=>$value) {
586 $dataxArr[] = "'".$key."'";
588 $dataxArr = join(",",$dataxArr);
589 $where .= "AND opportunities.sales_stage IN ($dataxArr) ";
592 //build the where clause for the query that matches $date_start and $date_end
593 $where .= " AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
594 AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
595 $where .= " AND opportunities.assigned_user_id = users.id AND opportunities.deleted=0 ";
597 //Now do the db queries
598 //query for opportunity data that matches $datax and $user
599 $query = " SELECT opportunities.sales_stage,
601 opportunities.assigned_user_id,
602 count( * ) AS opp_count,
603 sum(amount_usdollar/1000) AS total
604 FROM users,opportunities ";
605 $query .= "WHERE " .$where;
606 $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
611 function constructGroupBy(){
612 return array( 'sales_stage', 'user_name' );