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