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.
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.
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
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
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.
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.
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 ********************************************************************************/
42 require_once('include/Dashlets/DashletGenericChart.php');
44 class MyPipelineBySalesStageDashlet extends DashletGenericChart
46 public $mypbss_date_start;
47 public $mypbss_date_end;
48 public $mypbss_sales_stages = array();
50 protected $_seedName = 'Opportunities';
53 * @see DashletGenericChart::__construct()
55 public function __construct(
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');
69 parent::__construct($id,$options);
73 * @see DashletGenericChart::displayOptions()
75 public function displayOptions()
77 global $app_list_strings;
79 $selected_datax = array();
80 if (count($this->mypbss_sales_stages) > 0)
81 foreach ($this->mypbss_sales_stages as $key)
82 $selected_datax[] = $key;
84 $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
86 $this->_searchFields['mypbss_sales_stages']['options'] = $app_list_strings['sales_stage_dom'];
87 $this->_searchFields['mypbss_sales_stages']['input_name0'] = $selected_datax;
89 return parent::displayOptions();
93 * @see DashletGenericChart::display()
95 public function display()
97 global $sugar_config, $current_user, $timedate;
99 require_once('include/SugarCharts/SugarChartFactory.php');
100 $sugarChart = SugarChartFactory::getInstance();
101 $sugarChart->base_url = array( 'module' => 'Opportunities',
104 'searchFormTab' => 'advanced_search',
106 $sugarChart->url_params = array( 'assigned_user_id' => $current_user->id );
107 $sugarChart->group_by = $this->constructGroupBy();
109 $currency_symbol = $sugar_config['default_currency_symbol'];
110 if ($current_user->getPreference('currency')){
112 $currency = new Currency();
113 $currency->retrieve($current_user->getPreference('currency'));
114 $currency_symbol = $currency->symbol;
117 $sugarChart->is_currency = true;
118 $sugarChart->thousands_symbol = translate('LBL_OPP_THOUSANDS', 'Charts');
120 $subtitle = translate('LBL_OPP_SIZE', 'Charts') . " " . $currency_symbol . "1" . translate('LBL_OPP_THOUSANDS', 'Charts');
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');
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))
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);
136 elseif (!empty($this->mypbss_date_start))
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);
141 elseif (!empty($this->mypbss_date_end))
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);
147 $xmlFile = $sugarChart->getXMLFileName($this->id);
148 $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
150 return $this->getTitle('') .
151 '<div align="center">' .$sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div><br />'. $this->processAutoRefresh();
155 * awu: Bug 16794 - this function is a hack to get the correct sales stage order
156 * until i can clean it up later
158 * @param $query string
161 private function getChartData(
165 global $app_list_strings, $db;
168 $temp_data = array();
169 $selected_datax = array();
171 $user_sales_stage = $this->mypbss_sales_stages;
172 $tempx = $user_sales_stage;
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);
182 $datax = $app_list_strings['sales_stage_dom'];
183 $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
186 $result = $db->query($query);
187 $row = $db->fetchByAssoc($result, false);
191 $row = $db->fetchByAssoc($result, false);
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'];
202 unset($temp_data[$key]);
210 * @param $dataset array
213 private function getHorizBarTotal(
218 foreach($dataset as $value){
226 * @param $dataset array
229 private function constructCEChartData(
234 foreach($dataset as $key=>$value){
235 $newData[$value['sales_stage']] = $value['total'];
241 * @see DashletGenericChart::constructQuery()
243 protected function constructQuery()
245 $query = "SELECT opportunities.sales_stage,
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";
263 * @see DashletGenericChart::constructGroupBy()
265 protected function constructGroupBy()
267 $groupBy = array('sales_stage');
269 array_push($groupBy, 'user_name');