]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/code/Chart_lead_source_by_outcome.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Charts / code / Chart_lead_source_by_outcome.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 /*********************************************************************************
39
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  ********************************************************************************/
45
46
47 require_once('include/charts/Charts.php');
48
49
50
51 class Chart_lead_source_by_outcome
52 {
53         var $modules = array('Opportunities');
54         var $order = 0;
55 function Chart_lead_source_by_outcome()
56 {
57
58 }
59
60 function draw($extra_tools)
61 {
62
63
64
65 global $app_list_strings, $current_language, $sugar_config, $currentModule, $action,$theme;
66 $current_module_strings = return_module_language($current_language, 'Charts');
67
68
69 if (isset($_REQUEST['lsbo_refresh'])) { $refresh = $_REQUEST['lsbo_refresh']; }
70 else { $refresh = false; }
71
72 $tempx = array();
73 $datax = array();
74 $selected_datax = array();
75 //get list of sales stage keys to display
76
77 global $current_user;
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);
82 }
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'));
90 }
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);
96         }
97 }
98 else {
99         $datax = $app_list_strings['lead_source_dom'];
100         $selected_datax = array_keys($app_list_strings['lead_source_dom']);
101 }
102
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);
108 }
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'));
116 }
117 else {
118         $ids = get_user_array(false);
119         $ids = array_keys($ids);
120 }
121
122 //create unique prefix based on selected users for image files
123 $id_hash = '1';
124 if (isset($ids)) {
125         sort($ids);
126         $id_hash = crc32(implode('',$ids));
127         if($id_hash < 0)
128         {
129         $id_hash = $id_hash * -1;
130         }
131
132 }
133 $GLOBALS['log']->debug("ids is:");
134 $GLOBALS['log']->debug($ids);
135 $id_md5 = substr(md5($current_user->id),0,9);
136
137
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");
143
144
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']).'&nbsp;'.$current_module_strings['LBL_REFRESH'].'</a>&nbsp;&nbsp;<a href="javascript: toggleDisplay(\'lsbo_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','border="0"  align="absmiddle"',null,null,'.gif',$mod_strings['LBL_EDIT']).'&nbsp;'. $current_module_strings['LBL_EDIT'].'</a>&nbsp;&nbsp;'.$extra_tools.'</div>';
146 ?>
147
148 <?php
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>';
150
151 if (empty($_SESSION['lsbo_ids'])) $_SESSION['lsbo_ids'] = "";
152 ?>
153
154 <p>
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">
161 <tr>
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>
164 </tr>
165
166 <tr>
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>
169 </tr>
170
171 <tr>
172 <?php
173 global $app_strings;
174 ?>
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>
176         </tr>
177 </table>
178 </form>
179 </div>
180 </p>
181 <?php
182
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>";
185
186
187         if (file_exists($cache_file_name)) {
188 global  $timedate;
189                 $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($cache_file_name)));
190         }
191         else {
192                 $file_date = '';
193         }
194 ?>
195 <span class='chartFootnote'>
196 <p align="right"><i><?php  echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
197 </span>
198 <?php
199
200 }
201
202
203
204
205         /**
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): ______________________________________..
214         */
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;
217
218                 $kDelim = $current_user->getPreference('num_grp_sep');
219
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();
227                         $where="";
228                         //build the where clause for the query that matches $user
229                         $count = count($user_id);
230                         $id = array();
231                         if ($count>0) {
232                                 foreach ($user_id as $the_id) {
233                                         $id[] = "'".$the_id."'";
234                                 }
235                                 $ids = join(",",$id);
236                                 $where .= "opportunities.assigned_user_id IN ($ids) ";
237
238                         }
239
240                         //build the where clause for the query that matches $datay
241                         $count = count($datay);
242                         $datayArr = array();
243                         if ($count>0) {
244
245                                 foreach ($datay as $key=>$value) {
246                                         $datayArr[] = "'".$key."'";
247                                 }
248                                 $datayArr = join(",",$datayArr);
249                                 $where .= "AND opportunities.lead_source IN     ($datayArr) ";
250                         }
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
256
257                         $result = $opp->db->query($query, true);
258                         //build pipeline by sales stage data
259                         $total = 0;
260                         $div = 1;
261                         global $sugar_config;
262                         $symbol = $sugar_config['default_currency_symbol'];
263                         $other = $current_module_strings['LBL_LEAD_SOURCE_OTHER'];
264                         $rowTotalArr = array();
265                         $rowTotalArr[] = 0;
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') ){
269
270                                 $currency = new Currency();
271                                 $currency->retrieve($current_user->getPreference('currency'));
272                                 $div = $currency->conversion_rate;
273                                 $symbol = $currency->symbol;
274                         }
275                         $fileContents = '     <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
276                         $leadSourceArr = array();
277                         while($row = $opp->db->fetchByAssoc($result, false))
278                         {
279                                 if($row['total']*$div<=100){
280                                         $sum = round($row['total']*$div, 2);
281                                 } else {
282                                         $sum = round($row['total']*$div);
283                                 }
284                                 if($row['lead_source'] == ''){
285                                         $row['lead_source'] = $current_module_strings['NTC_NO_LEGENDS'];
286                                 }
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']];
290                                 } else {
291                                         $salesStage = "Other";
292                                         $salesStageT = $other;
293                                 }
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;
299
300                                 $total += $sum;
301                         }
302                         foreach ($datay as $key=>$translation) {
303                                 if ($key == '') {
304                                         $key = $current_module_strings['NTC_NO_LEGENDS'];
305                                         $translation = $current_module_strings['NTC_NO_LEGENDS'];
306                                 }
307                                 if(!isset($leadSourceArr[$key])){
308                                         $leadSourceArr[$key] = $key;
309                                 }
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']);
313                                 }
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";
318                                         }
319                                 }
320                                 $fileContents .= '          </dataRow>'."\n";
321                         }
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";
326                         $i=0;
327
328                                 foreach ($salesStages as $outcome=>$outcome_translation) {
329                                         $color = generate_graphcolor($outcome,$i);
330                                         $fileContents .= '          <mapping id="'.$outcome.'" name="'.$outcome_translation.'" color="'.$color.'"/>'."\n";
331                                         $i++;
332                                 }
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.'" ';
340                         }
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;
346
347                         save_xml_file($cache_file_name, $fileContents);
348                 }
349                 $return = create_chart('hBarF',$cache_file_name);
350                 return $return;
351         }
352
353
354         function constructQuery(){
355                 global $current_user;
356                 global $app_list_strings;
357
358                 $tempx = array();
359                 $datax = array();
360                 $selected_datax = array();
361                 //get list of sales stage keys to display
362
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);
367                 }
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'));
375                 }
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);
381                         }
382                 }
383                 else {
384                         $datax = $app_list_strings['lead_source_dom'];
385                         $selected_datax = array_keys($app_list_strings['lead_source_dom']);
386                 }
387
388                 $datay = $datax;
389
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);
395                 }
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'));
403                 }
404                 else {
405                         $ids = get_user_array(false);
406                         $ids = array_keys($ids);
407                 }
408
409                 $user_id = $ids;
410
411                 $opp = new Opportunity();
412                 $where="";
413                 //build the where clause for the query that matches $user
414                 $count = count($user_id);
415                 $id = array();
416                 if ($count>0) {
417                         foreach ($user_id as $the_id) {
418                                 $id[] = "'".$the_id."'";
419                         }
420                         $ids = join(",",$id);
421                         $where .= "opportunities.assigned_user_id IN ($ids) ";
422
423                 }
424
425                 //build the where clause for the query that matches $datay
426                 $count = count($datay);
427                 $datayArr = array();
428                 if ($count>0) {
429
430                         foreach ($datay as $key=>$value) {
431                                 $datayArr[] = "'".$key."'";
432                         }
433                         $datayArr = join(",",$datayArr);
434                         $where .= "AND opportunities.lead_source IN     ($datayArr) ";
435                 }
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";
439
440                 return $query;
441         }
442
443         function constructGroupBy(){
444                 return array( 'lead_source', 'sales_stage' );
445         }
446 }
447
448 ?>