nowDbDate(); } if (empty($options['obm_date_end'])) { $options['obm_date_end'] = $timedate->asDbDate($timedate->getNow()->modify("+6 months")); } parent::__construct($id, $options); } /** * @see DashletGenericChart::displayOptions() */ public function displayOptions() { if (!isset($this->obm_ids) || count($this->obm_ids) == 0) { $this->_searchFields['obm_ids']['input_name0'] = array_keys(get_user_array(false)); } return parent::displayOptions(); } /** * @see DashletGenericChart::display() */ public function display() { $currency_symbol = $GLOBALS['sugar_config']['default_currency_symbol']; if ($GLOBALS['current_user']->getPreference('currency')) { $currency = BeanFactory::newBean('Currencies'); $currency->retrieve($GLOBALS['current_user']->getPreference('currency')); $currency_symbol = $currency->symbol; } $thousands_symbol = translate('LBL_OPP_THOUSANDS', 'Charts'); $module = 'Opportunities'; $action = 'index'; $query = 'true'; $searchFormTab = 'advanced_search'; $groupBy = array( 'm', 'sales_stage', ); $data = $this->getChartData($this->constructQuery()); //I have taken out the sort as this will throw off the labels we have calculated $data = $this->sortData($data, 'm', false, 'sales_stage', true, true); $chartReadyData = $this->prepareChartData($data, $currency_symbol, $thousands_symbol); $canvasId = 'rGraphOutcomeByMonth'.uniqid(); $chartWidth = 900; $chartHeight = 500; $autoRefresh = $this->processAutoRefresh(); //$chartReadyData['data'] = [[1.1,2.2],[3.3,4.4]]; $jsonData = json_encode($chartReadyData['data']); $jsonLabels = json_encode($chartReadyData['labels']); $jsonLabelsAndValues = json_encode($chartReadyData['labelsAndValues']); $jsonKey = json_encode($chartReadyData['key']); $jsonTooltips = json_encode($chartReadyData['tooltips']); $colours = "['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928']"; if (!is_array($chartReadyData['data'])||count($chartReadyData['data']) < 1) { return "

$this->noDataMessage

"; } $chart = <<[No canvas support] $autoRefresh EOD; return $chart; } /** * @see DashletGenericChart::constructQuery() */ protected function constructQuery() { $query = "SELECT sales_stage,". DBManagerFactory::getInstance()->convert('opportunities.date_closed', 'date_format', array("'%Y-%m'"), array("'YYYY-MM'"))." as m, ". "sum(amount_usdollar/1000) as total, count(*) as opp_count FROM opportunities "; $query .= " WHERE opportunities.date_closed >= ".DBManagerFactory::getInstance()->convert("'".$this->obm_date_start."'", 'date') . " AND opportunities.date_closed <= ".DBManagerFactory::getInstance()->convert("'".$this->obm_date_end."'", 'date') . " AND opportunities.deleted=0"; if (isset($this->obm_ids) && count($this->obm_ids) > 0) { $query .= " AND opportunities.assigned_user_id IN ('" . implode("','", $this->obm_ids) . "')"; } $query .= " GROUP BY sales_stage,". DBManagerFactory::getInstance()->convert('opportunities.date_closed', 'date_format', array("'%Y-%m'"), array("'YYYY-MM'")) . " ORDER BY m"; return $query; } protected function prepareChartData($data, $currency_symbol, $thousands_symbol) { //Use the lead_source to categorise the data for the charts $chart['labels'] = array(); $chart['data'] = array(); //Need to add all elements into the key, as they are stacked (even though the category is not present, the value could be) $chart['key'] = array(); $chart['tooltips']= array(); foreach ($data as $i) { $key = $i["m"]; $stage = $i["sales_stage"]; $stage_dom_option = $i["sales_stage_dom_option"]; if (!in_array($key, $chart['labels'])) { $chart['labels'][] = $key; $chart['data'][] = array(); } if (!in_array($stage, $chart['key'])) { $chart['key'][] = $stage; } $formattedFloat = (float)number_format((float)$i["total"], 2, '.', ''); $chart['data'][count($chart['data'])-1][] = $formattedFloat; $chart['tooltips'][]="
".$stage.'('.$currency_symbol.$formattedFloat.$thousands_symbol.') '.$key; } return $chart; } }