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 ********************************************************************************/
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 = sugar_cached("xml/").$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','border="0" align="absmiddle"', null,null,'.gif',$mod_strings['LBL_REFRESH']).' '.$current_module_strings['LBL_REFRESH'].'</a> <a href="javascript: toggleDisplay(\'lsbo_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','border="0" align="absmiddle"',null,null,'.gif',$mod_strings['LBL_EDIT']).' '. $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']; ?>" 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, $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($cache_file_name)) {
189 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($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, true);
258 //build pipeline by sales stage data
261 global $sugar_config;
262 $symbol = $sugar_config['default_currency_symbol'];
263 $other = $current_module_strings['LBL_LEAD_SOURCE_OTHER'];
264 $rowTotalArr = array();
266 global $current_user;
267 $salesStages = array("Closed Lost"=>$app_list_strings['sales_stage_dom']["Closed Lost"],"Closed Won"=>$app_list_strings['sales_stage_dom']["Closed Won"],"Other"=>$other);
268 if($current_user->getPreference('currency') ){
270 $currency = new Currency();
271 $currency->retrieve($current_user->getPreference('currency'));
272 $div = $currency->conversion_rate;
273 $symbol = $currency->symbol;
275 $fileContents = ' <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
276 $leadSourceArr = array();
277 while($row = $opp->db->fetchByAssoc($result, false))
279 if($row['total']*$div<=100){
280 $sum = round($row['total']*$div, 2);
282 $sum = round($row['total']*$div);
284 if($row['lead_source'] == ''){
285 $row['lead_source'] = $current_module_strings['NTC_NO_LEGENDS'];
287 if($row['sales_stage'] == 'Closed Won' || $row['sales_stage'] == 'Closed Lost'){
288 $salesStage = $row['sales_stage'];
289 $salesStageT = $app_list_strings['sales_stage_dom'][$row['sales_stage']];
291 $salesStage = "Other";
292 $salesStageT = $other;
294 if(!isset($leadSourceArr[$row['lead_source']]['row_total'])) {$leadSourceArr[$row['lead_source']]['row_total']=0;}
295 $leadSourceArr[$row['lead_source']][$salesStage]['opp_count'][] = $row['opp_count'];
296 $leadSourceArr[$row['lead_source']][$salesStage]['total'][] = $sum;
297 $leadSourceArr[$row['lead_source']]['outcome'][$salesStage]=$salesStageT;
298 $leadSourceArr[$row['lead_source']]['row_total'] += $sum;
302 foreach ($datay as $key=>$translation) {
304 $key = $current_module_strings['NTC_NO_LEGENDS'];
305 $translation = $current_module_strings['NTC_NO_LEGENDS'];
307 if(!isset($leadSourceArr[$key])){
308 $leadSourceArr[$key] = $key;
310 if(isset($leadSourceArr[$key]['row_total'])){$rowTotalArr[]=$leadSourceArr[$key]['row_total'];}
311 if(isset($leadSourceArr[$key]['row_total']) && $leadSourceArr[$key]['row_total']>100){
312 $leadSourceArr[$key]['row_total'] = round($leadSourceArr[$key]['row_total']);
314 $fileContents .= ' <dataRow title="'.$translation.'" endLabel="'.currency_format_number($leadSourceArr[$key]['row_total'], array('currency_symbol' => true)) . '">'."\n";
315 if(is_array($leadSourceArr[$key]['outcome'])){
316 foreach ($leadSourceArr[$key]['outcome'] as $outcome=>$outcome_translation){
317 $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";
320 $fileContents .= ' </dataRow>'."\n";
322 $fileContents .= ' </yData>'."\n";
323 $max = get_max($rowTotalArr);
324 $fileContents .= ' <xData min="0" max="'.$max.'" length="10" kDelim="'.$kDelim.'" prefix="'.$symbol.'" suffix=""/>' . "\n";
325 $fileContents .= ' <colorLegend status="on">'."\n";
328 foreach ($salesStages as $outcome=>$outcome_translation) {
329 $color = generate_graphcolor($outcome,$i);
330 $fileContents .= ' <mapping id="'.$outcome.'" name="'.$outcome_translation.'" color="'.$color.'"/>'."\n";
333 $fileContents .= ' </colorLegend>'."\n";
334 $fileContents .= ' <graphInfo>'."\n";
335 $fileContents .= ' <![CDATA['.$current_module_strings['LBL_OPP_SIZE'].' '.$symbol.'1'.$current_module_strings['LBL_OPP_THOUSANDS'].']]>'."\n";
336 $fileContents .= ' </graphInfo>'."\n";
337 $fileContents .= ' <chartColors ';
338 foreach ($barChartColors as $key => $value) {
339 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
341 $fileContents .= ' />'."\n";
342 $fileContents .= '</graphData>'."\n";
343 $total = round($total, 2);
344 $title = '<graphData title="'.$current_module_strings['LBL_ALL_OPPORTUNITIES'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
345 $fileContents = $title.$fileContents;
347 save_xml_file($cache_file_name, $fileContents);
349 $return = create_chart('hBarF',$cache_file_name);
354 function constructQuery(){
355 global $current_user;
356 global $app_list_strings;
360 $selected_datax = array();
361 //get list of sales stage keys to display
363 $tempx = $current_user->getPreference('lsbo_lead_sources');
364 if (!empty($lsbo_lead_sources) && count($lsbo_lead_sources) > 0 && !isset($_REQUEST['lsbo_lead_sources'])) {
365 $GLOBALS['log']->fatal("user->getPreference('lsbo_lead_sources') is:");
366 $GLOBALS['log']->fatal($tempx);
368 elseif (isset($_REQUEST['lsbo_lead_sources']) && count($_REQUEST['lsbo_lead_sources']) > 0) {
369 $tempx = $_REQUEST['lsbo_lead_sources'];
370 $current_user->setPreference('lsbo_lead_sources', $_REQUEST['lsbo_lead_sources']);
371 $GLOBALS['log']->fatal("_REQUEST['lsbo_lead_sources'] is:");
372 $GLOBALS['log']->fatal($_REQUEST['lsbo_lead_sources']);
373 $GLOBALS['log']->fatal("user->getPreference('lsbo_lead_sources') is:");
374 $GLOBALS['log']->fatal($current_user->getPreference('lsbo_lead_sources'));
376 //set $datax using selected sales stage keys
377 if (!empty($tempx) && sizeof($tempx) > 0) {
378 foreach ($tempx as $key) {
379 $datax[$key] = $app_list_strings['lead_source_dom'][$key];
380 array_push($selected_datax,$key);
384 $datax = $app_list_strings['lead_source_dom'];
385 $selected_datax = array_keys($app_list_strings['lead_source_dom']);
390 $ids =$current_user->getPreference('lsbo_ids');
391 //get list of user ids for which to display data
392 if (!empty($ids) && count($ids) != 0 && !isset($_REQUEST['lsbo_ids'])) {
393 $GLOBALS['log']->debug("_SESSION['lsbo_ids'] is:");
394 $GLOBALS['log']->debug($ids);
396 elseif (isset($_REQUEST['lsbo_ids']) && count($_REQUEST['lsbo_ids']) > 0) {
397 $ids = $_REQUEST['lsbo_ids'];
398 $current_user->setPreference('lsbo_ids', $_REQUEST['lsbo_ids']);
399 $GLOBALS['log']->debug("_REQUEST['lsbo_ids'] is:");
400 $GLOBALS['log']->debug($_REQUEST['lsbo_ids']);
401 $GLOBALS['log']->debug("user->getPreference('lsbo_ids') is:");
402 $GLOBALS['log']->debug($current_user->getPreference('lsbo_ids'));
405 $ids = get_user_array(false);
406 $ids = array_keys($ids);
411 $opp = new Opportunity();
413 //build the where clause for the query that matches $user
414 $count = count($user_id);
417 foreach ($user_id as $the_id) {
418 $id[] = "'".$the_id."'";
420 $ids = join(",",$id);
421 $where .= "opportunities.assigned_user_id IN ($ids) ";
425 //build the where clause for the query that matches $datay
426 $count = count($datay);
430 foreach ($datay as $key=>$value) {
431 $datayArr[] = "'".$key."'";
433 $datayArr = join(",",$datayArr);
434 $where .= "AND opportunities.lead_source IN ($datayArr) ";
436 $query = "SELECT lead_source,sales_stage,sum(amount_usdollar/1000) as total,count(*) as opp_count FROM opportunities ";
437 $query .= "WHERE " .$where." AND opportunities.deleted=0 ";
438 $query .= " GROUP BY sales_stage,lead_source ORDER BY lead_source,sales_stage";
443 function constructGroupBy(){
444 return array( 'lead_source', 'sales_stage' );