]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/Dashlets/MyPipelineBySalesStageDashlet/MyPipelineBySalesStageDashlet.php
Release 6.4.0
[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-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
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;
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         $xmlFile = $sugarChart->getXMLFileName($this->id);
130         $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
131
132         return $this->getTitle('') .
133             '<div align="center">' .$sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div><br />'. $this->processAutoRefresh();
134     }
135
136         /**
137      * awu: Bug 16794 - this function is a hack to get the correct sales stage order
138      * until i can clean it up later
139      *
140      * @param  $query string
141      * @return array
142      */
143     private function getChartData(
144         $query
145         )
146     {
147         global $app_list_strings, $db;
148
149         $data = array();
150         $temp_data = array();
151         $selected_datax = array();
152
153         $user_sales_stage = $this->mypbss_sales_stages;
154         $tempx = $user_sales_stage;
155
156         //set $datax using selected sales stage keys
157         if (count($tempx) > 0) {
158             foreach ($tempx as $key) {
159                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
160                 array_push($selected_datax, $key);
161             }
162         }
163         else {
164             $datax = $app_list_strings['sales_stage_dom'];
165             $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
166         }
167
168         $result = $db->query($query);
169         $row = $db->fetchByAssoc($result, false);
170
171         while($row != null){
172                 $temp_data[] = $row;
173                 $row = $db->fetchByAssoc($result, false);
174         }
175
176                 // reorder and set the array based on the order of selected_datax
177         foreach($selected_datax as $sales_stage){
178                 foreach($temp_data as $key => $value){
179                         if ($value['sales_stage'] == $sales_stage){
180                                 $value['sales_stage'] = $app_list_strings['sales_stage_dom'][$value['sales_stage']];
181                                 $value['key'] = $sales_stage;
182                                 $value['value'] = $value['sales_stage'];
183                                 $data[] = $value;
184                                 unset($temp_data[$key]);
185                         }
186                 }
187         }
188         return $data;
189     }
190
191     /**
192      * @param  $dataset array
193      * @return int
194      */
195     private function getHorizBarTotal(
196         $dataset
197         )
198     {
199         $total = 0;
200         foreach($dataset as $value){
201                 $total += $value;
202         }
203
204         return $total;
205     }
206
207     /**
208      * @param  $dataset array
209      * @return array
210      */
211     private function constructCEChartData(
212         $dataset
213         )
214     {
215         $newData = array();
216         foreach($dataset as $key=>$value){
217                 $newData[$value['sales_stage']] = $value['total'];
218         }
219         return $newData;
220     }
221
222         /**
223      * @see DashletGenericChart::constructQuery()
224      */
225     protected function constructQuery()
226     {
227         $query = "SELECT opportunities.sales_stage,
228                         users.user_name,
229                         opportunities.assigned_user_id,
230                         count(*) AS opp_count,
231                         sum(amount_usdollar/1000) AS total
232                     FROM users,opportunities  ";
233         $query .= " WHERE opportunities.assigned_user_id IN ('{$GLOBALS['current_user']->id}') " .
234                         " AND opportunities.date_closed >= ". db_convert("'".$this->mypbss_date_start."'",'date').
235                         " AND opportunities.date_closed <= ".db_convert("'".$this->mypbss_date_end."'",'date') .
236                         " AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
237         if ( count($this->mypbss_sales_stages) > 0 )
238             $query .= " AND opportunities.sales_stage IN ('" . implode("','",$this->mypbss_sales_stages) . "') ";
239         $query .= " GROUP BY opportunities.sales_stage ,users.user_name,opportunities.assigned_user_id";
240
241         return $query;
242     }
243
244     /**
245      * @see DashletGenericChart::constructGroupBy()
246      */
247     protected function constructGroupBy()
248     {
249         $groupBy = array('sales_stage');
250
251                 array_push($groupBy, 'user_name');
252         return $groupBy;
253     }
254 }
255
256 ?>