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