]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/Dashlets/MyPipelineBySalesStageDashlet/MyPipelineBySalesStageDashlet.php
Release 6.5.1
[Github/sugarcrm.git] / modules / Charts / Dashlets / MyPipelineBySalesStageDashlet / MyPipelineBySalesStageDashlet.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-2012 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
41
42 require_once('include/Dashlets/DashletGenericChart.php');
43
44 class MyPipelineBySalesStageDashlet extends DashletGenericChart
45 {
46     public $mypbss_date_start;
47     public $mypbss_date_end;
48     public $mypbss_sales_stages = array();
49
50     protected $_seedName = 'Opportunities';
51
52     /**
53      * @see DashletGenericChart::__construct()
54      */
55     public function __construct(
56         $id,
57         array $options = null
58         )
59     {
60         global $timedate;
61
62         if(empty($options['mypbss_date_start']))
63             $options['mypbss_date_start'] = $timedate->nowDbDate();
64         if(empty($options['mypbss_date_end']))
65             $options['mypbss_date_end'] = $timedate->asDbDate($timedate->getNow()->modify("+6 months"));
66         if(empty($options['title']))
67             $options['title'] = translate('LBL_MY_PIPELINE_FORM_TITLE', 'Home');
68
69         parent::__construct($id,$options);
70     }
71
72     /**
73      * @see DashletGenericChart::displayOptions()
74      */
75     public function displayOptions()
76     {
77         global $app_list_strings;
78
79         $selected_datax = array();
80         if (count($this->mypbss_sales_stages) > 0)
81             foreach ($this->mypbss_sales_stages as $key)
82                 $selected_datax[] = $key;
83         else
84             $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
85
86         $this->_searchFields['mypbss_sales_stages']['options'] = $app_list_strings['sales_stage_dom'];
87         $this->_searchFields['mypbss_sales_stages']['input_name0'] = $selected_datax;
88
89         return parent::displayOptions();
90     }
91
92     /**
93      * @see DashletGenericChart::display()
94      */
95     public function display()
96     {
97         global $sugar_config, $current_user, $timedate;
98
99         require_once('include/SugarCharts/SugarChartFactory.php');
100                 $sugarChart = SugarChartFactory::getInstance();
101                 $sugarChart->base_url = array(  'module' => 'Opportunities',
102                                                                 'action' => 'index',
103                                                                 'query' => 'true',
104                                                                 'searchFormTab' => 'advanced_search',
105                                                          );
106         $sugarChart->url_params = array( 'assigned_user_id' => $current_user->id );
107         $sugarChart->group_by = $this->constructGroupBy();
108
109         $currency_symbol = $sugar_config['default_currency_symbol'];
110                 if ($current_user->getPreference('currency')){
111
112             $currency = new Currency();
113             $currency->retrieve($current_user->getPreference('currency'));
114             $currency_symbol = $currency->symbol;
115         }
116
117         $sugarChart->is_currency = true;
118         $sugarChart->thousands_symbol = translate('LBL_OPP_THOUSANDS', 'Charts');
119
120         $subtitle = translate('LBL_OPP_SIZE', 'Charts') . " " . $currency_symbol . "1" . translate('LBL_OPP_THOUSANDS', 'Charts');
121
122         $query = $this->constructQuery();
123                         $dataset = $this->constructCEChartData($this->getChartData($query));
124                         $sugarChart->setData($dataset);
125                         $total = format_number($this->getHorizBarTotal($dataset), 0, 0, array('convert'=>true));
126                         $pipeline_total_string = translate('LBL_TOTAL_PIPELINE', 'Charts') . $sugarChart->currency_symbol . $total . $sugarChart->thousands_symbol;
127                         $sugarChart->setProperties($pipeline_total_string, $subtitle, 'horizontal bar chart');
128
129         // Bug #53753 We have to add values for filter based on "Expected Close Date" field
130         if (!empty($this->mypbss_date_start) && !empty($this->mypbss_date_end))
131         {
132             $sugarChart->url_params['date_closed_advanced_range_choice'] = 'between';
133             $sugarChart->url_params['start_range_date_closed_advanced'] = $timedate->to_display_date($this->mypbss_date_start, false);
134             $sugarChart->url_params['end_range_date_closed_advanced'] = $timedate->to_display_date($this->mypbss_date_end, false);
135         }
136         elseif (!empty($this->mypbss_date_start))
137         {
138             $sugarChart->url_params['date_closed_advanced_range_choice'] = 'greater_than';
139             $sugarChart->url_params['range_date_closed_advanced'] = $timedate->to_display_date($this->mypbss_date_start, false);
140         }
141         elseif (!empty($this->mypbss_date_end))
142         {
143             $sugarChart->url_params['date_closed_advanced_range_choice'] = 'less_than';
144             $sugarChart->url_params['range_date_closed_advanced'] = $timedate->to_display_date($this->mypbss_date_end, false);
145         }
146
147         $xmlFile = $sugarChart->getXMLFileName($this->id);
148         $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
149
150         return $this->getTitle('') .
151             '<div align="center">' .$sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div><br />'. $this->processAutoRefresh();
152     }
153
154         /**
155      * awu: Bug 16794 - this function is a hack to get the correct sales stage order
156      * until i can clean it up later
157      *
158      * @param  $query string
159      * @return array
160      */
161     private function getChartData(
162         $query
163         )
164     {
165         global $app_list_strings, $db;
166
167         $data = array();
168         $temp_data = array();
169         $selected_datax = array();
170
171         $user_sales_stage = $this->mypbss_sales_stages;
172         $tempx = $user_sales_stage;
173
174         //set $datax using selected sales stage keys
175         if (count($tempx) > 0) {
176             foreach ($tempx as $key) {
177                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
178                 array_push($selected_datax, $key);
179             }
180         }
181         else {
182             $datax = $app_list_strings['sales_stage_dom'];
183             $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
184         }
185
186         $result = $db->query($query);
187         $row = $db->fetchByAssoc($result, false);
188
189         while($row != null){
190                 $temp_data[] = $row;
191                 $row = $db->fetchByAssoc($result, false);
192         }
193
194                 // reorder and set the array based on the order of selected_datax
195         foreach($selected_datax as $sales_stage){
196                 foreach($temp_data as $key => $value){
197                         if ($value['sales_stage'] == $sales_stage){
198                                 $value['sales_stage'] = $app_list_strings['sales_stage_dom'][$value['sales_stage']];
199                                 $value['key'] = $sales_stage;
200                                 $value['value'] = $value['sales_stage'];
201                                 $data[] = $value;
202                                 unset($temp_data[$key]);
203                         }
204                 }
205         }
206         return $data;
207     }
208
209     /**
210      * @param  $dataset array
211      * @return int
212      */
213     private function getHorizBarTotal(
214         $dataset
215         )
216     {
217         $total = 0;
218         foreach($dataset as $value){
219                 $total += $value;
220         }
221
222         return $total;
223     }
224
225     /**
226      * @param  $dataset array
227      * @return array
228      */
229     private function constructCEChartData(
230         $dataset
231         )
232     {
233         $newData = array();
234         foreach($dataset as $key=>$value){
235                 $newData[$value['sales_stage']] = $value['total'];
236         }
237         return $newData;
238     }
239
240         /**
241      * @see DashletGenericChart::constructQuery()
242      */
243     protected function constructQuery()
244     {
245         $query = "SELECT opportunities.sales_stage,
246                         users.user_name,
247                         opportunities.assigned_user_id,
248                         count(*) AS opp_count,
249                         sum(amount_usdollar/1000) AS total
250                     FROM users,opportunities  ";
251         $query .= " WHERE opportunities.assigned_user_id IN ('{$GLOBALS['current_user']->id}') " .
252                         " AND opportunities.date_closed >= ". db_convert("'".$this->mypbss_date_start."'",'date').
253                         " AND opportunities.date_closed <= ".db_convert("'".$this->mypbss_date_end."'",'date') .
254                         " AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
255         if ( count($this->mypbss_sales_stages) > 0 )
256             $query .= " AND opportunities.sales_stage IN ('" . implode("','",$this->mypbss_sales_stages) . "') ";
257         $query .= " GROUP BY opportunities.sales_stage ,users.user_name,opportunities.assigned_user_id";
258
259         return $query;
260     }
261
262     /**
263      * @see DashletGenericChart::constructGroupBy()
264      */
265     protected function constructGroupBy()
266     {
267         $groupBy = array('sales_stage');
268
269                 array_push($groupBy, 'user_name');
270         return $groupBy;
271     }
272 }
273
274 ?>