]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/Charts.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Campaigns / Charts.php
1 <?php
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.
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:  Includes the functions for Customer module specific charts.
41 ********************************************************************************/
42
43
44
45
46 require_once('include/charts/Charts.php');
47
48
49
50 class campaign_charts {
51         /**
52         * Creates opportunity pipeline image as a VERTICAL accumlated bar graph for multiple users.
53         * param $datax- the month data to display in the x-axis
54         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
55         * All Rights Reserved..
56         * Contributor(s): ______________________________________..
57         */
58         
59         function campaign_response_by_activity_type($datay= array(),$targets=array(),$campaign_id, $cache_file_name='a_file', $refresh=false, $marketing_id='') {
60                 global $app_strings, $mod_strings, $charset, $lang, $barChartColors,$app_list_strings;
61                 if (!file_exists($cache_file_name) || $refresh == true) {
62                         $GLOBALS['log']->debug("datay is:");
63                         $GLOBALS['log']->debug($datay);
64                         $GLOBALS['log']->debug("user_id is: ");
65                         $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
66             
67                         $focus = new Campaign();
68                                         
69                         $query = "SELECT activity_type,target_type, count(*) hits ";
70                         $query.= " FROM campaign_log ";
71                         $query.= " WHERE campaign_id = '$campaign_id' AND archived=0 AND deleted=0";
72
73             //if $marketing id is specified, then lets filter the chart by the value
74             if (!empty($marketing_id)){
75                 $query.= " AND marketing_id ='$marketing_id'"; 
76             }            
77
78                         $query.= " GROUP BY  activity_type, target_type";
79                         $query.= " ORDER BY  activity_type, target_type";
80                         $result = $focus->db->query($query);
81
82                         $leadSourceArr = array();
83                         $total=0;
84                         $total_targeted=0;
85                         $rowTotalArr = array();
86                         $rowTotalArr[] = 0;
87                         while($row = $focus->db->fetchByAssoc($result, -1, false))
88                         {
89                                 if(!isset($leadSourceArr[$row['activity_type']]['row_total'])) {
90                                         $leadSourceArr[$row['activity_type']]['row_total']=0;
91                                 }
92                                 
93                                 $leadSourceArr[$row['activity_type']][$row['target_type']]['hits'][] = $row['hits'];
94                                 $leadSourceArr[$row['activity_type']][$row['target_type']]['total'][] = $row['hits'];
95                                 $leadSourceArr[$row['activity_type']]['outcome'][$row['target_type']]=$row['target_type'];
96                                 $leadSourceArr[$row['activity_type']]['row_total'] += $row['hits'];
97
98                                 if (!isset($leadSourceArr['all_activities'][$row['target_type']])) {
99                                         $leadSourceArr['all_activities'][$row['target_type']]=array('total'=>0);
100                                 }
101                                 
102                                 $leadSourceArr['all_activities'][$row['target_type']]['total'] += $row['hits'];
103
104                                 $total += $row['hits'];
105                                 if ($row['activity_type'] =='targeted') {
106                                         $targeted[$row['target_type']]=$row['hits'];
107                                         $total_targeted+=$row['hits'];
108                                 }
109                         }
110                         
111                         $fileContents = '     <yData defaultAltText="'.$mod_strings['LBL_ROLLOVER_VIEW'].'">'."\n";
112                         
113                         foreach ($datay as $key=>$translation) {                                
114                                 if ($key == '') {
115                                         //$key = $mod_strings['NTC_NO_LEGENDS'];
116                                         $key = 'None';
117                                         $translation = $mod_strings['NTC_NO_LEGENDS'];
118                                 }
119                                 if(!isset($leadSourceArr[$key])){
120                                         $leadSourceArr[$key] = $key;
121                                 }
122                                 
123                                 
124                                 if(is_array($leadSourceArr[$key]) && isset($leadSourceArr[$key]['row_total'])){$rowTotalArr[]=$leadSourceArr[$key]['row_total'];}
125                                 if(is_array($leadSourceArr[$key]) && isset($leadSourceArr[$key]['row_total']) && $leadSourceArr[$key]['row_total']>100){
126                                         $leadSourceArr[$key]['row_total'] = round($leadSourceArr[$key]['row_total']);
127                                 }
128                                 $fileContents .= '          <dataRow title="'.$translation.'" endLabel="'.$leadSourceArr[$key]['row_total'].'">'."\n";
129                                 
130                                 if(is_array($leadSourceArr[$key]['outcome'])){
131                                         
132                                         
133                                 
134                                         foreach ($leadSourceArr[$key]['outcome'] as $outcome=>$outcome_translation){
135                                                 //create alternate text.
136                         $alttext = ' ';
137                         if(isset($targeted) && isset($targeted[$outcome])&& !empty($targeted[$outcome])){
138                                                 $alttext=$targets[$outcome].': '.$mod_strings['LBL_TARGETED'].' '.$targeted[$outcome]. ', '.$mod_strings['LBL_TOTAL_TARGETED'].' '. $total_targeted. ".";
139                         }
140                                                 if ($key != 'targeted'){
141                                                         $alttext.=" $translation ". array_sum($leadSourceArr[$key][$outcome]['hits']);
142                                                 }
143                                                 $fileContents .= '               <bar id="'.$outcome.'" totalSize="'.array_sum($leadSourceArr[$key][$outcome]['total']).'" altText="'.$alttext.'" url="#'.$key.'"/>'."\n";
144                                         }
145                                 }
146
147                                                                 
148                                 $fileContents .= '          </dataRow>'."\n";
149                         }
150                         $fileContents .= '     </yData>'."\n";
151                         
152                         $max = get_max($rowTotalArr);
153                         
154                         $fileContents .= '     <xData min="0" max="'.$max.'" length="10" prefix="'.''.'" suffix=""/>'."\n";
155                         $fileContents .= '     <colorLegend status="on">'."\n";
156                         $i=0;
157
158                         foreach ($targets as $outcome=>$outcome_translation) {
159                                 $color = generate_graphcolor($outcome,$i);
160                                 $fileContents .= '          <mapping id="'.$outcome.'" name="'.$outcome_translation.'" color="'.$color.'"/>'."\n";
161                                 $i++;
162                         }
163                         $fileContents .= '     </colorLegend>'."\n";
164                         $fileContents .= '     <graphInfo>'."\n";
165                         $fileContents .= '          <![CDATA['.' '.']]>'."\n";
166                         $fileContents .= '     </graphInfo>'."\n";
167                         $fileContents .= '     <chartColors ';
168                         foreach ($barChartColors as $key => $value) {
169                                 $fileContents .= ' '.$key.'='.'"'.$value.'" ';
170                         }
171                         $fileContents .= ' />'."\n";
172                         $fileContents .= '</graphData>'."\n";
173                         $total = round($total, 2);
174                         $title = '<graphData title="'.$mod_strings['LBL_CAMPAIGN_RESPONSE_BY_RECIPIENT_ACTIVITY'].'">'."\n";
175                         $fileContents = $title.$fileContents;
176
177                         save_xml_file($cache_file_name, $fileContents);
178                 }
179                 
180                 
181                 
182                 
183                 $return = create_chart('hBarF',$cache_file_name);
184                 return $return;
185         }
186         
187         //campaign roi compputations
188         function campaign_response_roi($datay= array(),$targets=array(),$campaign_id, $cache_file_name='a_file', $refresh=false,$marketing_id='',$is_dashlet=false,$dashlet_id='') {
189                 global $app_strings,$mod_strings, $current_module_strings, $charset, $lang, $app_list_strings, $current_language,$sugar_config;
190                 
191                 $not_empty = false;
192                 
193                 if ($is_dashlet){
194                         $mod_strings = return_module_language($current_language, 'Campaigns');
195                 }
196                 
197                 if (!file_exists($cache_file_name) || $refresh == true) {
198                         $GLOBALS['log']->debug("datay is:");
199                         $GLOBALS['log']->debug($datay);
200                         $GLOBALS['log']->debug("user_id is: ");
201                         $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
202             
203                         $focus = new Campaign();
204             $focus->retrieve($campaign_id);
205                         $opp_count=0;
206                         $opp_query  = "select count(*) opp_count,sum(" . db_convert("amount_usdollar","IFNULL",array(0)).")  total_value";                 
207             $opp_query .= " from opportunities";
208             $opp_query .= " where campaign_id='$campaign_id'";
209             $opp_query .= " and sales_stage='Prospecting'";
210             $opp_query .= " and deleted=0";
211                                     
212             $opp_result=$focus->db->query($opp_query);
213             $opp_data=$focus->db->fetchByAssoc($opp_result);
214 //            if (empty($opp_data['opp_count'])) $opp_data['opp_count']=0;
215             if (empty($opp_data['total_value'])) $opp_data['total_value']=0;
216             
217             //report query
218                         $opp_query1  = "select SUM(opp.amount) as revenue";                
219             $opp_query1 .= " from opportunities opp";
220             $opp_query1 .= " right join campaigns camp on camp.id = opp.campaign_id";
221             $opp_query1 .= " where opp.sales_stage = 'Closed Won'and camp.id='$campaign_id' and opp.deleted=0";
222             $opp_query1 .= " group by camp.name";
223
224             $opp_result1=$focus->db->query($opp_query1);              
225             $opp_data1=$focus->db->fetchByAssoc($opp_result1);
226
227                         //if (empty($opp_data1[])) 
228             if (empty($opp_data1['revenue'])){
229                                 $opp_data1[$mod_strings['LBL_ROI_CHART_REVENUE']] = 0;
230                 unset($opp_data1['revenue']);
231             }else{
232                 $opp_data1[$mod_strings['LBL_ROI_CHART_REVENUE']] = $opp_data1['revenue'];
233                 unset($opp_data1['revenue']);
234                                 $not_empty = true;
235             }                           
236
237                         $camp_query1  = "select camp.name, SUM(camp.actual_cost) as investment,SUM(camp.budget) as budget,SUM(camp.expected_revenue) as expected_revenue";                                         
238             $camp_query1 .= " from campaigns camp";            
239             $camp_query1 .= " where camp.id='$campaign_id'";
240             $camp_query1 .= " group by camp.name";
241             
242             $camp_result1=$focus->db->query($camp_query1);              
243             $camp_data1=$focus->db->fetchByAssoc($camp_result1);
244             //query needs to be lowercase, but array keys need to be propercased, as these are used in 
245             //chart to display legend
246            
247                         if (empty($camp_data1['investment'])) 
248                                 $camp_data1['investment'] = 0;
249                         else
250                                 $not_empty = true;
251                         if (empty($camp_data1['budget'])) 
252                                 $camp_data1['budget'] = 0;
253                         else
254                                 $not_empty = true;                              
255             if (empty($camp_data1['expected_revenue'])) 
256                 $camp_data1['expected_revenue'] = 0;
257                         else
258                                 $not_empty = true;              
259              
260             $opp_data1[$mod_strings['LBL_ROI_CHART_INVESTMENT']]=$camp_data1['investment'];
261                 $opp_data1[$mod_strings['LBL_ROI_CHART_BUDGET']]=$camp_data1['budget'];
262                 $opp_data1[$mod_strings['LBL_ROI_CHART_EXPECTED_REVENUE']]=$camp_data1['expected_revenue'];     
263
264                         $query = "SELECT activity_type,target_type, count(*) hits ";
265                         $query.= " FROM campaign_log ";
266                         $query.= " WHERE campaign_id = '$campaign_id' AND archived=0 AND deleted=0";
267             //if $marketing id is specified, then lets filter the chart by the value
268             if (!empty($marketing_id)){
269                 $query.= " AND marketing_id ='$marketing_id'"; 
270             }            
271                         $query.= " GROUP BY  activity_type, target_type";
272                         $query.= " ORDER BY  activity_type, target_type";
273                         $result = $focus->db->query($query);
274
275                         $leadSourceArr = array();
276                         $total=0;
277                         $total_targeted=0;
278
279                 }
280                 
281                 global $current_user;
282                 $user_id = $current_user->id;
283
284                 require_once('include/SugarCharts/SugarChart.php');
285                 
286                 $width = ($is_dashlet) ? '100%' : '720px';
287                 
288                 $return = '<script type="text/javascript" src="' . getJSPath('include/javascript/swfobject.js' ) . '"></script>';
289                 if (!$is_dashlet){
290                         $return .= '<br />';
291                 }               
292
293        
294         $currency_id = $focus->currency_id;
295         $currency_symbol = $sugar_config['default_currency_symbol'];
296         if(!empty($currency_id)){
297             
298             $cur = new Currency();
299             $cur->retrieve($currency_id);
300             $currency_symbol = $cur->symbol;
301         }
302
303                 
304                 $sugarChart = new SugarChart();
305                 $sugarChart->is_currency = true;
306         $sugarChart->currency_symbol = $currency_symbol; 
307                 if ($not_empty)
308                         $sugarChart->setData($opp_data1);
309                 else
310                         $sugarChart->setData(array());
311                 $sugarChart->setProperties($mod_strings['LBL_CAMPAIGN_RETURN_ON_INVESTMENT'], $mod_strings['LBL_AMOUNT_IN'].$currency_symbol, 'bar chart');
312
313         if (!$is_dashlet){
314                         $xmlFile = $sugarChart->getXMLFileName('roi_details_chart');
315                         $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
316                         $return .= $sugarChart->display('roi_details_chart', $xmlFile, $width, '480');
317                 }
318                 else{
319                         $xmlFile = $sugarChart->getXMLFileName($dashlet_id);
320                         $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
321                         $return .= $sugarChart->display($dashlet_id, $xmlFile, $width, '480');
322                 }               
323             
324                 return $return;
325         }
326 }// end charts class
327 ?>