]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/Dashlets/PipelineBySalesStageDashlet/PipelineBySalesStageDashlet.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Charts / Dashlets / PipelineBySalesStageDashlet / PipelineBySalesStageDashlet.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 PipelineBySalesStageDashlet extends DashletGenericChart
45 {
46     public $pbss_date_start;
47     public $pbss_date_end;
48     public $pbss_sales_stages = array();
49
50     /**
51      * @see DashletGenericChart::$_seedName
52      */
53     protected $_seedName = 'Opportunities';
54
55     /**
56      * @see DashletGenericChart::__construct()
57      */
58     public function __construct(
59         $id,
60         array $options = null
61         )
62     {
63         global $timedate;
64
65         if(empty($options['pbss_date_start']))
66             $options['pbss_date_start'] = $timedate->nowDbDate();
67
68         if(empty($options['pbss_date_end']))
69             $options['pbss_date_end'] = $timedate->asDbDate($timedate->getNow()->modify("+6 months"));
70
71         if(empty($options['title']))
72                 $options['title'] = translate('LBL_PIPELINE_FORM_TITLE', 'Home');
73
74         parent::__construct($id,$options);
75     }
76
77     /**
78      * @see DashletGenericChart::displayOptions()
79      */
80     public function displayOptions()
81     {
82         global $app_list_strings;
83
84         if (!empty($this->pbss_sales_stages) && count($this->pbss_sales_stages) > 0)
85             foreach ($this->pbss_sales_stages as $key)
86                 $selected_datax[] = $key;
87         else
88             $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
89
90         $this->_searchFields['pbss_sales_stages']['options'] = $app_list_strings['sales_stage_dom'];
91         $this->_searchFields['pbss_sales_stages']['input_name0'] = $selected_datax;
92
93         return parent::displayOptions();
94     }
95
96     /**
97      * @see DashletGenericChart::display()
98      */
99     public function display()
100     {
101         global $current_user, $sugar_config;
102
103         require_once('include/SugarCharts/SugarChartFactory.php');
104         $sugarChart = SugarChartFactory::getInstance();
105         $sugarChart->base_url = array(
106             'module' => 'Opportunities',
107             'action' => 'index',
108             'query' => 'true',
109             'searchFormTab' => 'advanced_search',
110             );
111         //fixing bug #27097: The opportunity list is not correct after drill-down
112         //should send to url additional params: start range value and end range value
113         $sugarChart->url_params = array('start_range_date_closed' => $this->pbss_date_start,
114                                         'end_range_date_closed' => $this->pbss_date_end);
115         $sugarChart->group_by = $this->constructGroupBy();
116         $sugarChart->setData($this->getChartData($this->constructQuery()));
117         $sugarChart->is_currency = true;
118         $sugarChart->thousands_symbol = translate('LBL_OPP_THOUSANDS', 'Charts');
119
120         $currency_symbol = $sugar_config['default_currency_symbol'];
121         if ($current_user->getPreference('currency')){
122
123             $currency = new Currency();
124             $currency->retrieve($current_user->getPreference('currency'));
125             $currency_symbol = $currency->symbol;
126         }
127         $subtitle = translate('LBL_OPP_SIZE', 'Charts') . " " . $currency_symbol . "1" . translate('LBL_OPP_THOUSANDS', 'Charts');
128
129         $pipeline_total_string = translate('LBL_TOTAL_PIPELINE', 'Charts') . $sugarChart->currency_symbol . format_number($sugarChart->getTotal(), 0, 0, array('convert'=>true)) . $sugarChart->thousands_symbol;
130             $sugarChart->setProperties($pipeline_total_string, $subtitle, 'horizontal group by chart');
131
132         $xmlFile = $sugarChart->getXMLFileName($this->id);
133         $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
134
135         return $this->getTitle('') . '<div align="center">' . $sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div>'. $this->processAutoRefresh();
136     }
137
138         /**
139      * awu: Bug 16794 - this function is a hack to get the correct sales stage order until
140      * i can clean it up later
141      *
142      * @param  $query string
143      * @return array
144      */
145     private function getChartData(
146         $query
147         )
148     {
149         global $app_list_strings, $db;
150
151         $data = array();
152         $temp_data = array();
153         $selected_datax = array();
154
155         $user_sales_stage = $this->pbss_sales_stages;
156         $tempx = $user_sales_stage;
157
158         //set $datax using selected sales stage keys
159         if (count($tempx) > 0) {
160             foreach ($tempx as $key) {
161                 $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
162                 $selected_datax[] = $key;
163             }
164         }
165         else {
166             $datax = $app_list_strings['sales_stage_dom'];
167             $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
168         }
169
170         $result = $db->query($query);
171         while($row = $db->fetchByAssoc($result, false))
172                 $temp_data[] = $row;
173
174                 // reorder and set the array based on the order of selected_datax
175         foreach($selected_datax as $sales_stage){
176                 foreach($temp_data as $key => $value){
177                         if ($value['sales_stage'] == $sales_stage){
178                                 $value['sales_stage'] = $app_list_strings['sales_stage_dom'][$value['sales_stage']];
179                                 $value['key'] = $sales_stage;
180                                 $value['value'] = $value['sales_stage'];
181                                 $data[] = $value;
182                                 unset($temp_data[$key]);
183                         }
184                 }
185         }
186         return $data;
187     }
188
189     /**
190      * @see DashletGenericChart::constructQuery()
191      */
192     protected function constructQuery()
193     {
194         $query = "  SELECT opportunities.sales_stage,
195                         users.user_name,
196                         opportunities.assigned_user_id,
197                         count(*) AS opp_count,
198                         sum(amount_usdollar/1000) AS total
199                     FROM users,opportunities  ";
200         $query .= " WHERE opportunities.date_closed >= ". db_convert("'".$this->pbss_date_start."'",'date').
201                         " AND opportunities.date_closed <= ".db_convert("'".$this->pbss_date_end."'",'date') .
202                         " AND opportunities.assigned_user_id = users.id  AND opportunities.deleted=0 ";
203         if ( count($this->pbss_sales_stages) > 0 )
204             $query .= " AND opportunities.sales_stage IN ('" . implode("','",$this->pbss_sales_stages) . "') ";
205         $query .= " GROUP BY opportunities.sales_stage ,users.user_name,opportunities.assigned_user_id";
206
207         return $query;
208     }
209
210     /**
211      * @see DashletGenericChart::constructGroupBy()
212      */
213     protected function constructGroupBy()
214     {
215        return array(
216            'sales_stage',
217            'user_name',
218            );
219     }
220 }