]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Charts/Dashlets/OutcomeByMonthDashlet/OutcomeByMonthDashlet.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Charts / Dashlets / OutcomeByMonthDashlet / OutcomeByMonthDashlet.php
1 <?php
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.
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 OutcomeByMonthDashlet extends DashletGenericChart 
45 {
46     public $obm_ids = array();
47     public $obm_date_start;
48     public $obm_date_end;
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['obm_date_start'])) 
63             $options['obm_date_start'] = date($timedate->get_db_date_time_format(), time());
64             
65         if(empty($options['obm_date_end']))
66             $options['obm_date_end'] = date($timedate->get_db_date_time_format(), strtotime("+6 months", time()));
67             
68         parent::__construct($id,$options);
69     }
70     
71     /**
72      * @see DashletGenericChart::displayOptions()
73      */
74     public function displayOptions()
75     {
76         if (!isset($this->obm_ids) || count($this->obm_ids) == 0)
77                         $this->_searchFields['obm_ids']['input_name0'] = array_keys(get_user_array(false));
78         
79         return parent::displayOptions();
80     }
81     
82     /**
83      * @see DashletGenericChart::display()
84      */
85     public function display() 
86     {
87         $currency_symbol = $GLOBALS['sugar_config']['default_currency_symbol'];
88         if ($GLOBALS['current_user']->getPreference('currency')){
89             
90             $currency = new Currency();
91             $currency->retrieve($GLOBALS['current_user']->getPreference('currency'));
92             $currency_symbol = $currency->symbol;
93         }
94
95         require("modules/Charts/chartdefs.php");
96         $chartDef = $chartDefs['outcome_by_month'];
97                 
98         require_once('include/SugarCharts/SugarChart.php');
99         $sugarChart = new SugarChart();
100         $sugarChart->setProperties('', 
101             translate('LBL_OPP_SIZE', 'Charts') . ' ' . $currency_symbol . '1' .translate('LBL_OPP_THOUSANDS', 'Charts'), 
102             $chartDef['chartType']);
103         $sugarChart->base_url = $chartDef['base_url'];
104         $sugarChart->group_by = $chartDef['groupBy'];
105         $sugarChart->url_params = array();
106         $sugarChart->getData($this->constructQuery());
107         $sugarChart->is_currency = true;
108         $sugarChart->data_set = $sugarChart->sortData($sugarChart->data_set, 'm', false, 'sales_stage', true, true);
109         $xmlFile = $sugarChart->getXMLFileName($this->id);
110         $sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
111         
112         return $this->getTitle('<div align="center"></div>') . 
113             '<div align="center">' . $sugarChart->display($this->id, $xmlFile, '100%', '480', false) . '</div><br />';
114         }
115     
116     /**
117      * @see DashletGenericChart::constructQuery()
118      */
119     protected function constructQuery()
120     {
121         $query = "SELECT sales_stage,".
122             db_convert('opportunities.date_closed','date_format',array("'%Y-%m'"),array("'YYYY-MM'"))." as m, ".
123             "sum(amount_usdollar/1000) as total, count(*) as opp_count FROM opportunities ";
124         $query .= " WHERE opportunities.date_closed >= ".db_convert("'".$this->obm_date_start."'",'datetime') .
125                         " AND opportunities.date_closed <= ".db_convert("'".$this->obm_date_end."'",'datetime') . 
126                         " AND opportunities.deleted=0";
127         if (count($this->obm_ids) > 0)
128             $query .= " AND opportunities.assigned_user_id IN ('" . implode("','",$this->obm_ids) . "')";
129         $query .= " GROUP BY sales_stage,".
130                         db_convert('opportunities.date_closed','date_format',array("'%Y-%m'"),array("'YYYY-MM'")) .
131                     " ORDER BY m";
132         
133         return $query;
134     }
135 }
136
137 ?>