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-2012 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 ********************************************************************************/
48 require_once("include/charts/Charts.php");
49 require_once("modules/Charts/code/Chart_pipeline_by_sales_stage.php");
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');
57 if (isset($_REQUEST['mypbss_refresh'])) { $refresh = $_REQUEST['mypbss_refresh']; }
58 else { $refresh = false; }
60 //get the dates to display
61 $user_date_start = $current_user->getPreference('mypbss_date_start');
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);
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'));
77 $date_start = $timedate->nowDate();
79 $user_date_end = $current_user->getPreference('mypbss_date_end');
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);
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'));
95 $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
96 $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
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);
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);
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'));
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);
135 $datax = $app_list_strings['sales_stage_dom'];
136 $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
138 $GLOBALS['log']->debug("datax is:");
139 $GLOBALS['log']->debug($datax);
141 $ids = array($current_user->id);
142 //create unique prefix based on selected users for image files
146 $id_hash = crc32(implode('',$ids));
149 $id_hash = $id_hash * -1;
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";
160 $GLOBALS['log']->debug("cache file name is: $cache_file_name");
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']).' '.$current_module_strings['LBL_REFRESH'].'</a> <a href="javascript: toggleDisplay(\'my_pipeline_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','border="0" align="absmiddle"',null,null,'.gif',$mod_strings['LBL_EDIT']).' '. $current_module_strings['LBL_EDIT'].'</a></div>';
166 <?php echo get_form_header($mod_strings['LBL_PIPELINE_FORM_TITLE'], $tools , false);?>
171 $cal_dateformat = $timedate->get_cal_date_format();
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">
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>
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>
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>
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']; ?>" 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>
200 <script type="text/javascript">
202 inputField : "date_start", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_start_trigger", singleClick : true, step : 1, weekNumbers:false
205 inputField : "date_end", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_end_trigger", singleClick : true, step : 1, weekNumbers:false
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>";
217 if (file_exists($cache_file_name)) {
218 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($cache_file_name)));
225 <span class='chartFootnote'>
226 <p align="right"><i><?php echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
230 echo get_validate_chart_js();
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): ______________________________________..
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;
242 $kDelim = $current_user->getPreference('num_grp_sep');
245 if (!file_exists($cache_file_name) || $refresh == true) {
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;
255 //build the where clause for the query that matches $user
256 $count = count($user_id);
258 $user_list = get_user_array(false);
259 foreach ($user_id as $key) {
260 $new_ids[$key] = $user_list[$key];
263 foreach ($new_ids as $the_id=>$the_name) {
264 $id[] = "'".$the_id."'";
266 $ids = join(",",$id);
267 $where .= "opportunities.assigned_user_id IN ($ids) ";
270 //build the where clause for the query that matches $datax
271 $count = count($datax);
275 foreach ($datax as $key=>$value) {
276 $dataxArr[] = "'".$key."'";
278 $dataxArr = join(",",$dataxArr);
279 $where .= "AND opportunities.sales_stage IN ($dataxArr) ";
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 ";
287 //Now do the db queries
288 //query for opportunity data that matches $datax and $user
289 $query = " SELECT opportunities.sales_stage,
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";
298 $result = $opp->db->query($query, true);
299 //build pipeline by sales stage data
302 global $sugar_config;
303 $symbol = $sugar_config['default_currency_symbol'];
304 global $current_user;
305 if($current_user->getPreference('currency') ){
307 $currency = new Currency();
308 $currency->retrieve($current_user->getPreference('currency'));
309 $div = $currency->conversion_rate;
310 $symbol = $currency->symbol;
312 // cn: adding user-pref date handling
313 $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
314 $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
316 $fileContents = ' <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
318 $usernameArr = array();
319 $rowTotalArr = array();
321 while($row = $opp->db->fetchByAssoc($result, false))
323 if($row['total']*$div<=100){
324 $sum = round($row['total']*$div, 2);
326 $sum = round($row['total']*$div);
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;
334 $usernameArr[$row['assigned_user_id']] = $row['user_name'];
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']);
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";
352 $fileContents .= ' </dataRow>'."\n";
354 $fileContents .= ' </yData>'."\n";
355 $max = get_max($rowTotalArr);
356 if($chart_size=='hBarF'){
361 $fileContents .= ' <xData min="0" max="'.$max.'" length="'.$length.'" kDelim="'.$kDelim.'" prefix="'.$symbol.'" suffix=""/>'."\n";
362 $fileContents .= ' <colorLegend status="on">'."\n";
365 foreach ($new_ids as $key=>$value) {
366 $color = generate_graphcolor($key,$i);
367 $fileContents .= ' <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
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.'" ';
378 $fileContents .= ' />'."\n";
379 $fileContents .= '</graphData>'."\n";
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;
384 save_xml_file($cache_file_name, $fileContents);
387 if($chart_size=='hBarF'){
394 $return = create_chart($chart_size,$cache_file_name,$width,$height);
398 function constructQuery(){
399 global $current_user;
402 //get the dates to display
403 $user_date_start = $current_user->getPreference('mypbss_date_start');
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);
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'));
419 $date_start = $timedate->nowDate();
421 $user_date_end = $current_user->getPreference('mypbss_date_end');
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);
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'));
437 $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
438 $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
441 $user_id = array($current_user->id);
443 $opp = new Opportunity;
445 //build the where clause for the query that matches $user
446 $count = count($user_id);
448 $user_list = get_user_array(false);
449 foreach ($user_id as $key) {
450 $new_ids[$key] = $user_list[$key];
453 foreach ($new_ids as $the_id=>$the_name) {
454 $id[] = "'".$the_id."'";
456 $ids = join(",",$id);
457 $where .= "opportunities.assigned_user_id IN ($ids) ";
459 //build the where clause for the query that matches $datax
460 $count = count($datax);
463 foreach ($datax as $key=>$value) {
464 $dataxArr[] = "'".$key."'";
466 $dataxArr = join(",",$dataxArr);
467 $where .= "AND opportunities.sales_stage IN ($dataxArr) ";
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 ";
475 //Now do the db queries
476 //query for opportunity data that matches $datax and $user
477 $query = " SELECT opportunities.sales_stage,
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";
490 function constructGroupBy(){
491 return array('sales_stage');