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.
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 ********************************************************************************/
47 require_once('include/charts/Charts.php');
51 class Chart_lead_source_by_outcome
53 var $modules = array('Opportunities');
55 function Chart_lead_source_by_outcome()
60 function draw($extra_tools)
65 global $app_list_strings, $current_language, $sugar_config, $currentModule, $action,$theme;
66 $current_module_strings = return_module_language($current_language, 'Charts');
69 if (isset($_REQUEST['lsbo_refresh'])) { $refresh = $_REQUEST['lsbo_refresh']; }
70 else { $refresh = false; }
74 $selected_datax = array();
75 //get list of sales stage keys to display
78 $tempx = $current_user->getPreference('lsbo_lead_sources');
79 if (!empty($lsbo_lead_sources) && count($lsbo_lead_sources) > 0 && !isset($_REQUEST['lsbo_lead_sources'])) {
80 $GLOBALS['log']->fatal("user->getPreference('lsbo_lead_sources') is:");
81 $GLOBALS['log']->fatal($tempx);
83 elseif (isset($_REQUEST['lsbo_lead_sources']) && count($_REQUEST['lsbo_lead_sources']) > 0) {
84 $tempx = $_REQUEST['lsbo_lead_sources'];
85 $current_user->setPreference('lsbo_lead_sources', $_REQUEST['lsbo_lead_sources']);
86 $GLOBALS['log']->fatal("_REQUEST['lsbo_lead_sources'] is:");
87 $GLOBALS['log']->fatal($_REQUEST['lsbo_lead_sources']);
88 $GLOBALS['log']->fatal("user->getPreference('lsbo_lead_sources') is:");
89 $GLOBALS['log']->fatal($current_user->getPreference('lsbo_lead_sources'));
91 //set $datax using selected sales stage keys
92 if (!empty($tempx) && sizeof($tempx) > 0) {
93 foreach ($tempx as $key) {
94 $datax[$key] = $app_list_strings['lead_source_dom'][$key];
95 array_push($selected_datax,$key);
99 $datax = $app_list_strings['lead_source_dom'];
100 $selected_datax = array_keys($app_list_strings['lead_source_dom']);
103 $ids =$current_user->getPreference('lsbo_ids');
104 //get list of user ids for which to display data
105 if (!empty($ids) && count($ids) != 0 && !isset($_REQUEST['lsbo_ids'])) {
106 $GLOBALS['log']->debug("_SESSION['lsbo_ids'] is:");
107 $GLOBALS['log']->debug($ids);
109 elseif (isset($_REQUEST['lsbo_ids']) && count($_REQUEST['lsbo_ids']) > 0) {
110 $ids = $_REQUEST['lsbo_ids'];
111 $current_user->setPreference('lsbo_ids', $_REQUEST['lsbo_ids']);
112 $GLOBALS['log']->debug("_REQUEST['lsbo_ids'] is:");
113 $GLOBALS['log']->debug($_REQUEST['lsbo_ids']);
114 $GLOBALS['log']->debug("user->getPreference('lsbo_ids') is:");
115 $GLOBALS['log']->debug($current_user->getPreference('lsbo_ids'));
118 $ids = get_user_array(false);
119 $ids = array_keys($ids);
122 //create unique prefix based on selected users for image files
126 $id_hash = crc32(implode('',$ids));
129 $id_hash = $id_hash * -1;
133 $GLOBALS['log']->debug("ids is:");
134 $GLOBALS['log']->debug($ids);
135 $id_md5 = substr(md5($current_user->id),0,9);
138 $seps = array("-", "/");
139 $dates = array(date($GLOBALS['timedate']->dbDayFormat), $GLOBALS['timedate']->dbDayFormat);
140 $dateFileNameSafe = str_replace($seps, "_", $dates);
141 $cache_file_name = $current_user->getUserPrivGuid()."_lead_source_by_outcome_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
142 $GLOBALS['log']->debug("cache file name is: $cache_file_name");
145 $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&lsbo_refresh=true" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('refresh','alt="Refresh" border="0" align="absmiddle"').' '.$current_module_strings['LBL_REFRESH'].'</a> <a href="javascript: toggleDisplay(\'lsbo_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','alt="Edit" border="0" align="absmiddle"').' '. $current_module_strings['LBL_EDIT'].'</a> '.$extra_tools.'</div>';
149 echo '<span onmouseover="this.style.cursor=\'move\'" id="chart_handle_' . $this->order . '">' . get_form_header($current_module_strings['LBL_LEAD_SOURCE_BY_OUTCOME'],$tools,false) . '</span>';
151 if (empty($_SESSION['lsbo_ids'])) $_SESSION['lsbo_ids'] = "";
155 <div id='lsbo_edit' style='display: none;'>
156 <form action="index.php" method="post" >
157 <input type="hidden" name="module" value="<?php echo $currentModule;?>">
158 <input type="hidden" name="action" value="<?php echo $action;?>">
159 <input type="hidden" name="lsbo_refresh" value="true">
160 <table cellpadding="0" cellspacing="0" border="0" class="edit view" align="center">
162 <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_LEAD_SOURCES'];?></b></td>
163 <td valign='top'><select name="lsbo_lead_sources[]" multiple size='3'><?php echo get_select_options_with_id($app_list_strings['lead_source_dom'],$selected_datax); ?></select></td>
167 <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_USERS'];?></b></td>
168 <td valign='top'><select name="lsbo_ids[]" multiple size='3'><?php echo get_select_options_with_id(get_user_array(false),$ids); ?></select></td>
175 <td align="right" colspan="2"> <input class="button" 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('lsbo_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>
183 echo "<p align='center'>".$this->gen_xml($datax, $ids, $sugar_config['tmp_dir'].$cache_file_name, $refresh,$current_module_strings)."</p>";
184 echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_LEAD_SOURCE_BY_OUTCOME_DESC']."</span></P>";
187 if (file_exists($sugar_config['tmp_dir'].$cache_file_name)) {
189 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($sugar_config['tmp_dir'].$cache_file_name)));
195 <span class='chartFootnote'>
196 <p align="right"><i><?php echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
206 * Creates lead_source_by_outcome pipeline image as a HORIZONAL accumlated bar graph for multiple users.
207 * param $datay- the lead source data to display in the x-axis
208 * param $ids - list of assigned users of opps to find
209 * param $cache_file_name - file name to write image to
210 * param $refresh - boolean whether to rebuild image if exists
211 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
212 * All Rights Reserved..
213 * Contributor(s): ______________________________________..
215 function gen_xml($datay=array('foo','bar'), $user_id=array('1'), $cache_file_name='a_file', $refresh=false,$current_module_strings) {
216 global $app_strings, $charset, $lang, $barChartColors,$app_list_strings, $current_user;
218 $kDelim = $current_user->getPreference('num_grp_sep');
220 if (!file_exists($cache_file_name) || $refresh == true) {
221 $GLOBALS['log']->debug("datay is:");
222 $GLOBALS['log']->debug($datay);
223 $GLOBALS['log']->debug("user_id is: ");
224 $GLOBALS['log']->debug($user_id);
225 $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
226 $opp = new Opportunity();
228 //build the where clause for the query that matches $user
229 $count = count($user_id);
232 foreach ($user_id as $the_id) {
233 $id[] = "'".$the_id."'";
235 $ids = join(",",$id);
236 $where .= "opportunities.assigned_user_id IN ($ids) ";
240 //build the where clause for the query that matches $datay
241 $count = count($datay);
245 foreach ($datay as $key=>$value) {
246 $datayArr[] = "'".$key."'";
248 $datayArr = join(",",$datayArr);
249 $where .= "AND opportunities.lead_source IN ($datayArr) ";
251 $query = "SELECT lead_source,sales_stage,sum(amount_usdollar/1000) as total,count(*) as opp_count FROM opportunities ";
252 $query .= "WHERE " .$where." AND opportunities.deleted=0 ";
253 $query .= " GROUP BY sales_stage,lead_source ORDER BY lead_source,sales_stage";
254 //Now do the db queries
255 //query for opportunity data that matches $datay and $user
257 $result = $opp->db->query($query)
258 or sugar_die("Error selecting sugarbean: ".mysql_error());
259 //build pipeline by sales stage data
262 global $sugar_config;
263 $symbol = $sugar_config['default_currency_symbol'];
264 $other = $current_module_strings['LBL_LEAD_SOURCE_OTHER'];
265 $rowTotalArr = array();
267 global $current_user;
268 $salesStages = array("Closed Lost"=>$app_list_strings['sales_stage_dom']["Closed Lost"],"Closed Won"=>$app_list_strings['sales_stage_dom']["Closed Won"],"Other"=>$other);
269 if($current_user->getPreference('currency') ){
271 $currency = new Currency();
272 $currency->retrieve($current_user->getPreference('currency'));
273 $div = $currency->conversion_rate;
274 $symbol = $currency->symbol;
276 $fileContents = ' <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
277 $leadSourceArr = array();
278 while($row = $opp->db->fetchByAssoc($result, -1, false))
280 if($row['total']*$div<=100){
281 $sum = round($row['total']*$div, 2);
283 $sum = round($row['total']*$div);
285 if($row['lead_source'] == ''){
286 $row['lead_source'] = $current_module_strings['NTC_NO_LEGENDS'];
288 if($row['sales_stage'] == 'Closed Won' || $row['sales_stage'] == 'Closed Lost'){
289 $salesStage = $row['sales_stage'];
290 $salesStageT = $app_list_strings['sales_stage_dom'][$row['sales_stage']];
292 $salesStage = "Other";
293 $salesStageT = $other;
295 if(!isset($leadSourceArr[$row['lead_source']]['row_total'])) {$leadSourceArr[$row['lead_source']]['row_total']=0;}
296 $leadSourceArr[$row['lead_source']][$salesStage]['opp_count'][] = $row['opp_count'];
297 $leadSourceArr[$row['lead_source']][$salesStage]['total'][] = $sum;
298 $leadSourceArr[$row['lead_source']]['outcome'][$salesStage]=$salesStageT;
299 $leadSourceArr[$row['lead_source']]['row_total'] += $sum;
303 foreach ($datay as $key=>$translation) {
305 $key = $current_module_strings['NTC_NO_LEGENDS'];
306 $translation = $current_module_strings['NTC_NO_LEGENDS'];
308 if(!isset($leadSourceArr[$key])){
309 $leadSourceArr[$key] = $key;
311 if(isset($leadSourceArr[$key]['row_total'])){$rowTotalArr[]=$leadSourceArr[$key]['row_total'];}
312 if(isset($leadSourceArr[$key]['row_total']) && $leadSourceArr[$key]['row_total']>100){
313 $leadSourceArr[$key]['row_total'] = round($leadSourceArr[$key]['row_total']);
315 $fileContents .= ' <dataRow title="'.$translation.'" endLabel="'.currency_format_number($leadSourceArr[$key]['row_total'], array('currency_symbol' => true)) . '">'."\n";
316 if(is_array($leadSourceArr[$key]['outcome'])){
317 foreach ($leadSourceArr[$key]['outcome'] as $outcome=>$outcome_translation){
318 $fileContents .= ' <bar id="'.$outcome.'" totalSize="'.array_sum($leadSourceArr[$key][$outcome]['total']).'" altText="'.format_number(array_sum($leadSourceArr[$key][$outcome]['opp_count']),0,0).' '.$current_module_strings['LBL_OPPS_WORTH'].' '.currency_format_number(array_sum($leadSourceArr[$key][$outcome]['total']),array('currency_symbol' => true)).$current_module_strings['LBL_OPP_THOUSANDS'].' '.$current_module_strings['LBL_OPPS_OUTCOME'].' '.$outcome_translation.'" url="index.php?module=Opportunities&action=index&lead_source='.$key.'&sales_stage='.urlencode($outcome).'&query=true&searchFormTab=advanced_search"/>'."\n";
321 $fileContents .= ' </dataRow>'."\n";
323 $fileContents .= ' </yData>'."\n";
324 $max = get_max($rowTotalArr);
325 $fileContents .= ' <xData min="0" max="'.$max.'" length="10" kDelim="'.$kDelim.'" prefix="'.$symbol.'" suffix=""/>' . "\n";
326 $fileContents .= ' <colorLegend status="on">'."\n";
329 foreach ($salesStages as $outcome=>$outcome_translation) {
330 $color = generate_graphcolor($outcome,$i);
331 $fileContents .= ' <mapping id="'.$outcome.'" name="'.$outcome_translation.'" color="'.$color.'"/>'."\n";
334 $fileContents .= ' </colorLegend>'."\n";
335 $fileContents .= ' <graphInfo>'."\n";
336 $fileContents .= ' <![CDATA['.$current_module_strings['LBL_OPP_SIZE'].' '.$symbol.'1'.$current_module_strings['LBL_OPP_THOUSANDS'].']]>'."\n";
337 $fileContents .= ' </graphInfo>'."\n";
338 $fileContents .= ' <chartColors ';
339 foreach ($barChartColors as $key => $value) {
340 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
342 $fileContents .= ' />'."\n";
343 $fileContents .= '</graphData>'."\n";
344 $total = round($total, 2);
345 $title = '<graphData title="'.$current_module_strings['LBL_ALL_OPPORTUNITIES'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
346 $fileContents = $title.$fileContents;
348 save_xml_file($cache_file_name, $fileContents);
350 $return = create_chart('hBarF',$cache_file_name);
355 function constructQuery(){
356 global $current_user;
357 global $app_list_strings;
361 $selected_datax = array();
362 //get list of sales stage keys to display
364 $tempx = $current_user->getPreference('lsbo_lead_sources');
365 if (!empty($lsbo_lead_sources) && count($lsbo_lead_sources) > 0 && !isset($_REQUEST['lsbo_lead_sources'])) {
366 $GLOBALS['log']->fatal("user->getPreference('lsbo_lead_sources') is:");
367 $GLOBALS['log']->fatal($tempx);
369 elseif (isset($_REQUEST['lsbo_lead_sources']) && count($_REQUEST['lsbo_lead_sources']) > 0) {
370 $tempx = $_REQUEST['lsbo_lead_sources'];
371 $current_user->setPreference('lsbo_lead_sources', $_REQUEST['lsbo_lead_sources']);
372 $GLOBALS['log']->fatal("_REQUEST['lsbo_lead_sources'] is:");
373 $GLOBALS['log']->fatal($_REQUEST['lsbo_lead_sources']);
374 $GLOBALS['log']->fatal("user->getPreference('lsbo_lead_sources') is:");
375 $GLOBALS['log']->fatal($current_user->getPreference('lsbo_lead_sources'));
377 //set $datax using selected sales stage keys
378 if (!empty($tempx) && sizeof($tempx) > 0) {
379 foreach ($tempx as $key) {
380 $datax[$key] = $app_list_strings['lead_source_dom'][$key];
381 array_push($selected_datax,$key);
385 $datax = $app_list_strings['lead_source_dom'];
386 $selected_datax = array_keys($app_list_strings['lead_source_dom']);
391 $ids =$current_user->getPreference('lsbo_ids');
392 //get list of user ids for which to display data
393 if (!empty($ids) && count($ids) != 0 && !isset($_REQUEST['lsbo_ids'])) {
394 $GLOBALS['log']->debug("_SESSION['lsbo_ids'] is:");
395 $GLOBALS['log']->debug($ids);
397 elseif (isset($_REQUEST['lsbo_ids']) && count($_REQUEST['lsbo_ids']) > 0) {
398 $ids = $_REQUEST['lsbo_ids'];
399 $current_user->setPreference('lsbo_ids', $_REQUEST['lsbo_ids']);
400 $GLOBALS['log']->debug("_REQUEST['lsbo_ids'] is:");
401 $GLOBALS['log']->debug($_REQUEST['lsbo_ids']);
402 $GLOBALS['log']->debug("user->getPreference('lsbo_ids') is:");
403 $GLOBALS['log']->debug($current_user->getPreference('lsbo_ids'));
406 $ids = get_user_array(false);
407 $ids = array_keys($ids);
412 $opp = new Opportunity();
414 //build the where clause for the query that matches $user
415 $count = count($user_id);
418 foreach ($user_id as $the_id) {
419 $id[] = "'".$the_id."'";
421 $ids = join(",",$id);
422 $where .= "opportunities.assigned_user_id IN ($ids) ";
426 //build the where clause for the query that matches $datay
427 $count = count($datay);
431 foreach ($datay as $key=>$value) {
432 $datayArr[] = "'".$key."'";
434 $datayArr = join(",",$datayArr);
435 $where .= "AND opportunities.lead_source IN ($datayArr) ";
437 $query = "SELECT lead_source,sales_stage,sum(amount_usdollar/1000) as total,count(*) as opp_count FROM opportunities ";
438 $query .= "WHERE " .$where." AND opportunities.deleted=0 ";
439 $query .= " GROUP BY sales_stage,lead_source ORDER BY lead_source,sales_stage";
444 function constructGroupBy(){
445 return array( 'lead_source', 'sales_stage' );