2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 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;
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 $xmlFile = $sugarChart->getXMLFileName($this->id);
130 $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
132 return $this->getTitle('') .
133 '<div align="center">' .$sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div><br />'. $this->processAutoRefresh();
137 * awu: Bug 16794 - this function is a hack to get the correct sales stage order
138 * until i can clean it up later
140 * @param $query string
143 private function getChartData(
147 global $app_list_strings, $db;
150 $temp_data = array();
151 $selected_datax = array();
153 $user_sales_stage = $this->mypbss_sales_stages;
154 $tempx = $user_sales_stage;
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);
164 $datax = $app_list_strings['sales_stage_dom'];
165 $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
168 $result = $db->query($query);
169 $row = $db->fetchByAssoc($result, -1, false);
173 $row = $db->fetchByAssoc($result, -1, false);
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'];
184 unset($temp_data[$key]);
192 * @param $dataset array
195 private function getHorizBarTotal(
200 foreach($dataset as $value){
208 * @param $dataset array
211 private function constructCEChartData(
216 foreach($dataset as $key=>$value){
217 $newData[$value['sales_stage']] = $value['total'];
223 * @see DashletGenericChart::constructQuery()
225 protected function constructQuery()
227 $query = "SELECT opportunities.sales_stage,
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."'",'datetime').
235 " AND opportunities.date_closed <= ".db_convert("'".$this->mypbss_date_end."'",'datetime') .
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";
245 * @see DashletGenericChart::constructGroupBy()
247 protected function constructGroupBy()
249 $groupBy = array('sales_stage');
251 array_push($groupBy, 'user_name');