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 ********************************************************************************/
48 require_once('include/charts/Charts.php');
52 class Chart_pipeline_by_lead_source
55 var $modules = array('Opportunities');
57 function Chart_pipeline_by_lead_source()
61 function draw($extra_tools)
65 global $app_list_strings, $current_language, $ids, $sugar_config ,$theme;
66 $current_module_strings = return_module_language($current_language, 'Charts');
69 if (isset($_REQUEST['pbls_refresh'])) { $refresh = $_REQUEST['pbls_refresh']; }
70 else { $refresh = false; }
74 $selected_datax = array();
75 //get list of sales stage keys to display
77 $user_tempx = $current_user->getPreference('pbls_lead_sources');
78 if (!empty($user_tempx) && count($user_tempx) > 0 && !isset($_REQUEST['pbls_lead_sources'])) {
80 $GLOBALS['log']->debug("USER PREFERENCES['pbls_lead_sources'] is:");
81 $GLOBALS['log']->debug($user_tempx);
83 elseif (isset($_REQUEST['pbls_lead_sources']) && count($_REQUEST['pbls_lead_sources']) > 0) {
84 $tempx = $_REQUEST['pbls_lead_sources'];
85 $current_user->setPreference('pbls_lead_sources', $_REQUEST['pbls_lead_sources']);
86 $GLOBALS['log']->debug("_REQUEST['pbls_lead_sources'] is:");
87 $GLOBALS['log']->debug($_REQUEST['pbls_lead_sources']);
88 $GLOBALS['log']->debug("USER PREFERENCES['pbls_lead_sources'] is:");
89 $GLOBALS['log']->debug($current_user->getPreference('pbls_lead_sources'));
92 //set $datax using selected sales stage keys
93 if (count($tempx) > 0) {
94 foreach ($tempx as $key) {
95 $datax[$key] = $app_list_strings['lead_source_dom'][$key];
96 array_push($selected_datax,$key);
100 $datax = $app_list_strings['lead_source_dom'];
101 $selected_datax = array_keys($app_list_strings['lead_source_dom']);
103 $GLOBALS['log']->debug("datax is:");
104 $GLOBALS['log']->debug($datax);
107 $user_ids = $current_user->getPreference('pbls_ids');
108 //get list of user ids for which to display data
109 if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbls_ids'])) {
110 if(isset($_SESSION['pbls_ids'])) {$ids = $_SESSION['pbls_ids'];}
111 $GLOBALS['log']->debug("USER PREFERENCES['pbls_ids'] is:");
112 $GLOBALS['log']->debug($user_ids);
114 elseif (isset($_REQUEST['pbls_ids']) && count($_REQUEST['pbls_ids']) > 0) {
115 $ids = $_REQUEST['pbls_ids'];
116 $current_user->setPreference('pbls_ids', $ids);
117 $GLOBALS['log']->debug("_REQUEST['pbls_ids'] is:");
118 $GLOBALS['log']->debug($_REQUEST['pbls_ids']);
119 $GLOBALS['log']->debug("USER PREFERENCES['pbls_ids'] is:");
120 $GLOBALS['log']->debug($current_user->getPreference('pbls_ids'));
123 $ids = get_user_array(false);
124 $ids = array_keys($ids);
127 //create unique prefix based on selected users for image files
129 if (isset($ids) && is_array($ids)) {
131 $id_hash = crc32(implode('',$ids));
134 $id_hash = $id_hash * -1;
137 $GLOBALS['log']->debug("ids is:");
138 $GLOBALS['log']->debug($ids);
139 $id_md5 = substr(md5($current_user->id),0,9);
142 $seps = array("-", "/");
143 $dates = array(date($GLOBALS['timedate']->dbDayFormat), $GLOBALS['timedate']->dbDayFormat);
144 $dateFileNameSafe = str_replace($seps, "_", $dates);
145 $cache_file_name = $current_user->getUserPrivGuid()."_pipeline_by_lead_source_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
147 $GLOBALS['log']->debug("cache file name is: $cache_file_name");
148 global $currentModule,$action;
149 $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&pbls_refresh=true" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('refresh','alt="Refresh" border="0" align="absmiddle"').' '.$current_module_strings['LBL_REFRESH'].'</a> <a href="javascript: toggleDisplay(\'pbls_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','alt="Edit" border="0" align="absmiddle"').' '. $current_module_strings['LBL_EDIT'].'</a> '.$extra_tools.'</div>';
153 echo '<span onmouseover="this.style.cursor=\'move\'" id="chart_handle_' . $this->order . '">' . get_form_header($current_module_strings['LBL_LEAD_SOURCE_FORM_TITLE'],$tools,false) . '</span>';
154 if (empty($_SESSION['pbls_lead_sources'])) $_SESSION['pbls_lead_sources'] = "";
155 if (empty($_SESSION['pbls_ids'])) $_SESSION['pbls_ids'] = "";
159 <div id='pbls_edit' style='display: none;'>
160 <form action="index.php" method="post" >
161 <input type="hidden" name="module" value="<?php echo $currentModule;?>">
162 <input type="hidden" name="action" value="<?php echo $action;?>">
163 <input type="hidden" name="pbls_refresh" value="true">
164 <table cellpadding="0" cellspacing="0" border="0" class="edit view" align="center">
166 <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_LEAD_SOURCES'];?></b></td>
167 <td valign='top'><select name="pbls_lead_sources[]" multiple size='3'><?php echo get_select_options_with_id($app_list_strings['lead_source_dom'],$selected_datax); ?></select></td>
168 <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_USERS'];?></b></td>
169 <td valign='top'><select name="pbls_ids[]" multiple size='3'><?php $allUsers = get_user_array(false); echo get_select_options_with_id($allUsers,$ids); ?></select></td>
173 <td align="right" valign="top"><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('pbls_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>
181 echo "<p align='center'>".$this->gen_xml($datax, $ids, $sugar_config['tmp_dir'].$cache_file_name, $refresh,$current_module_strings)."</p>";
182 echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_LEAD_SOURCE_FORM_DESC']."</span></P>";
184 if (file_exists($sugar_config['tmp_dir'].$cache_file_name)) {
186 $file_date = date($timedate->get_date_format()." ".$timedate->get_time_format(), filemtime($sugar_config['tmp_dir'].$cache_file_name));
192 <span class='chartFootnote'>
193 <p align="right"><i><?php echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
201 * Creates PIE CHART image of opportunities by lead_source.
202 * param $datax- the sales stage data to display in the x-axis
203 * param $datay- the sum of opportunity amounts for each opportunity in each sales stage
204 * to display in the y-axis
205 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
206 * All Rights Reserved..
207 * Contributor(s): ______________________________________..
209 function gen_xml($legends=array('foo','bar'), $user_id=array('1'), $cache_file_name='a_file', $refresh=true,$current_module_strings) {
210 global $app_strings, $charset, $lang, $pieChartColors, $current_user;
212 $kDelim = $current_user->getPreference('num_grp_sep');
214 if (!file_exists($cache_file_name) || $refresh == true) {
216 $GLOBALS['log']->debug("starting pipeline chart");
217 $GLOBALS['log']->debug("legends is:");
218 $GLOBALS['log']->debug($legends);
219 $GLOBALS['log']->debug("user_id is: ");
220 $GLOBALS['log']->debug($user_id);
221 $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
223 $opp = new Opportunity;
224 //Now do the db queries
225 //query for opportunity data that matches $legends and $user
227 //build the where clause for the query that matches $user
229 $count = count($user_id);
231 if ($count > 0 && !empty($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) ";
239 if(!empty($where)) $where .= 'AND';
240 //build the where clause for the query that matches $datax
241 $count = count($legends);
242 $legendItem = array();
243 if ($count > 0 && !empty($legends)) {
245 foreach ($legends as $key=>$value) {
246 $legendItem[] = "'".$key."'";
248 $legendItems = join(",",$legendItem);
249 $where .= " opportunities.lead_source IN ($legendItems) ";
251 $query = "SELECT lead_source,sum(amount_usdollar/1000) as total,count(*) as opp_count FROM opportunities ";
252 $query .= "WHERE ".$where." AND opportunities.deleted=0 ";
253 $query .= "GROUP BY lead_source ORDER BY total DESC";
255 //build pipeline by lead source data
258 global $sugar_config;
259 $symbol = $sugar_config['default_currency_symbol'];
260 global $current_user;
261 if($current_user->getPreference('currency') ) {
262 $currency = new Currency();
263 $currency->retrieve($current_user->getPreference('currency'));
264 $div = $currency->conversion_rate;
265 $symbol = $currency->symbol;
267 $subtitle = $current_module_strings['LBL_OPP_SIZE'].' '.$symbol.'1'.$current_module_strings['LBL_OPP_THOUSANDS'];
269 $fileContents .= ' <pie defaultAltText="'.$current_module_strings['LBL_ROLLOVER_WEDGE_DETAILS'].'" legendStatus="on">'."\n";
270 $result = $opp->db->query($query)
271 or sugar_die("Error selecting sugarbean: ".mysql_error());
272 $leadSourceArr = array();
273 while($row = $opp->db->fetchByAssoc($result, -1, false))
275 if($row['lead_source'] == ''){
276 $leadSource = $current_module_strings['NTC_NO_LEGENDS'];
278 $leadSource = $row['lead_source'];
280 if($row['total']*$div<=100){
281 $sum = round($row['total']*$div, 2);
283 $sum = round($row['total']*$div);
286 $leadSourceArr[$leadSource]['opp_count'] = $row['opp_count'];
287 $leadSourceArr[$leadSource]['sum'] = $sum;
290 foreach ($legends as $lead_source_key=>$translation) {
291 if ($lead_source_key == '') {
292 $lead_source_key = $current_module_strings['NTC_NO_LEGENDS'];
293 $translation = $current_module_strings['NTC_NO_LEGENDS'];
295 if(!isset($leadSourceArr[$lead_source_key])) {
296 $leadSourceArr[$lead_source_key] = $lead_source_key;
297 $leadSourceArr[$lead_source_key]['sum'] = 0;
299 $color = generate_graphcolor($lead_source_key,$i);
300 $fileContents .= ' <wedge title="'.$translation.'" kDelim="'.$kDelim.'" value="'.$leadSourceArr[$lead_source_key]['sum'].'" color="'.$color.'" labelText="'.currency_format_number($leadSourceArr[$lead_source_key]['sum'], array('currency_symbol' => true)).'" url="index.php?module=Opportunities&action=index&lead_source='.urlencode($lead_source_key).'&query=true&searchFormTab=advanced_search" altText="'.format_number($leadSourceArr[$lead_source_key]['opp_count'], 0, 0).' '.$current_module_strings['LBL_OPPS_IN_LEAD_SOURCE'].' '.$translation.'"/>'."\n";
301 if(isset($leadSourceArr[$lead_source_key])){$total += $leadSourceArr[$lead_source_key]['sum'];}
305 $fileContents .= ' </pie>'."\n";
306 $fileContents .= ' <graphInfo>'."\n";
307 $fileContents .= ' <![CDATA[]]>'."\n";
308 $fileContents .= ' </graphInfo>'."\n";
309 $fileContents .= ' <chartColors ';
310 foreach ($pieChartColors as $key => $value) {
311 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
313 $fileContents .= ' />'."\n";
314 $fileContents .= '</graphData>'."\n";
315 $total = round($total, 2);
316 $title = $current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'];
317 $fileContents = '<graphData title="'.$title.'" subtitle="'.$subtitle.'">'."\n" . $fileContents;
318 $GLOBALS['log']->debug("total is: $total");
320 return ($current_module_strings['ERR_NO_OPPS']);
323 save_xml_file($cache_file_name, $fileContents);
326 $return = create_chart('pieF',$cache_file_name);
331 function constructQuery(){
332 global $current_user;
333 global $app_list_strings;
337 $selected_datax = array();
338 //get list of sales stage keys to display
339 global $current_user;
340 $user_tempx = $current_user->getPreference('pbls_lead_sources');
341 if (!empty($user_tempx) && count($user_tempx) > 0 && !isset($_REQUEST['pbls_lead_sources'])) {
342 $tempx = $user_tempx;
343 $GLOBALS['log']->debug("USER PREFERENCES['pbls_lead_sources'] is:");
344 $GLOBALS['log']->debug($user_tempx);
346 elseif (isset($_REQUEST['pbls_lead_sources']) && count($_REQUEST['pbls_lead_sources']) > 0) {
347 $tempx = $_REQUEST['pbls_lead_sources'];
348 $current_user->setPreference('pbls_lead_sources', $_REQUEST['pbls_lead_sources']);
349 $GLOBALS['log']->debug("_REQUEST['pbls_lead_sources'] is:");
350 $GLOBALS['log']->debug($_REQUEST['pbls_lead_sources']);
351 $GLOBALS['log']->debug("USER PREFERENCES['pbls_lead_sources'] is:");
352 $GLOBALS['log']->debug($current_user->getPreference('pbls_lead_sources'));
355 //set $datax using selected sales stage keys
356 if (count($tempx) > 0) {
357 foreach ($tempx as $key) {
358 $datax[$key] = $app_list_strings['lead_source_dom'][$key];
359 array_push($selected_datax,$key);
363 $datax = $app_list_strings['lead_source_dom'];
364 $selected_datax = array_keys($app_list_strings['lead_source_dom']);
370 $user_ids = $current_user->getPreference('pbls_ids');
371 //get list of user ids for which to display data
372 if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbls_ids'])) {
373 if(isset($_SESSION['pbls_ids'])) {$ids = $_SESSION['pbls_ids'];}
374 $GLOBALS['log']->debug("USER PREFERENCES['pbls_ids'] is:");
375 $GLOBALS['log']->debug($user_ids);
377 elseif (isset($_REQUEST['pbls_ids']) && count($_REQUEST['pbls_ids']) > 0) {
378 $ids = $_REQUEST['pbls_ids'];
379 $current_user->setPreference('pbls_ids', $ids);
380 $GLOBALS['log']->debug("_REQUEST['pbls_ids'] is:");
381 $GLOBALS['log']->debug($_REQUEST['pbls_ids']);
382 $GLOBALS['log']->debug("USER PREFERENCES['pbls_ids'] is:");
383 $GLOBALS['log']->debug($current_user->getPreference('pbls_ids'));
386 $ids = get_user_array(false);
387 $ids = array_keys($ids);
392 $opp = new Opportunity;
393 //Now do the db queries
394 //query for opportunity data that matches $legends and $user
396 //build the where clause for the query that matches $user
398 $count = count($user_id);
400 if ($count > 0 && !empty($user_id)) {
401 foreach ($user_id as $the_id) {
402 $id[] = "'".$the_id."'";
404 $ids = join(",",$id);
405 $where .= "opportunities.assigned_user_id IN ($ids) ";
408 if(!empty($where)) $where .= 'AND';
409 //build the where clause for the query that matches $datax
410 $count = count($legends);
411 $legendItem = array();
412 if ($count > 0 && !empty($legends)) {
414 foreach ($legends as $key=>$value) {
415 $legendItem[] = "'".$key."'";
417 $legendItems = join(",",$legendItem);
418 $where .= " opportunities.lead_source IN ($legendItems) ";
420 $query = "SELECT lead_source,sum(amount_usdollar/1000) as total,count(*) as opp_count FROM opportunities ";
421 $query .= "WHERE ".$where." AND opportunities.deleted=0 ";
422 $query .= "GROUP BY lead_source ORDER BY total DESC";
427 function constructGroupBy(){
428 return array( 'lead_source', );