Files
crm.e5.pl/modules/Charts/a/PipelineByInvoiceOutsStatusDashlet/PipelineByInvoiceOutsStatusDashlet.php
2024-04-27 09:23:34 +02:00

554 lines
23 KiB
PHP
Executable File

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/**
* SugarCRM is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo. If the display of the logo is not reasonably feasible for
* technical reasons, the Appropriate Legal Notices must display the words
* "Powered by SugarCRM".
*/
require_once('include/Dashlets/Dashlet.php');
require_once('include/Sugar_Smarty.php');
require_once('include/charts/Charts.php');
require_once('modules/Dashboard/Forms.php');
class PipelineByInvoiceOutsStatusDashlet extends Dashlet {
var $pbuts_date_start;
var $pbuts_date_end;
var $pbuts_ecminvoiceout_status = null;
var $pbuts_ecminvoiceout_users = null;
var $refresh = false;
function PipelineByInvoiceOutsStatusDashlet($id, $options) {
global $timedate;
parent::Dashlet($id);
$this->isConfigurable = true;
$this->isRefreshable = true;
if(empty($options['pbuts_date_start']))
$this->pbuts_date_start = date($timedate->get_db_date_time_format(), time());
else
$this->pbuts_date_start = $options['pbuts_date_start'];
if(empty($options['pbuts_date_end']))
$this->pbuts_date_end = date($timedate->get_db_date_time_format(), strtotime('2010-01-01'));
else
$this->pbuts_date_end = $options['pbuts_date_end'];
if(empty($options['pbuts_ecminvoiceout_status']))
$this->pbuts_ecminvoiceout_status = array();
else
$this->pbuts_ecminvoiceout_status = $options['pbuts_ecminvoiceout_status'];
if(empty($options['pbuts_ecminvoiceout_users']))
$this->pbuts_ecminvoiceout_users = array();
else
$this->pbuts_ecminvoiceout_users = $options['pbuts_ecminvoiceout_users'];
if(empty($options['title']))
$this->title = translate('LBL_PIPELINE_BY_ECMINVOICEOUTS_TITLE', 'Home');
else
$this->title = $options['title'];
}
function saveOptions($req) {
global $sugar_config, $timedate, $current_user, $theme;
$options = array();
$date_start = $this->pbuts_date_start;
$date_end = $this->pbuts_date_end;
$dateStartDisplay = strftime($timedate->get_user_date_format(), strtotime($date_start));
$dateEndDisplay = strftime($timedate->get_user_date_format(), strtotime($date_end));
$seps = array("-", "/");
$dates = array($dateStartDisplay, $dateEndDisplay);
$dateFileNameSafe = str_replace($seps, "_", $dates);
if(is_file($sugar_config['tmp_dir'] . $current_user->getUserPrivGuid()."_".$theme."_my_pipeline_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml"))
unlink($sugar_config['tmp_dir'] . $current_user->getUserPrivGuid()."_".$theme."_my_pipeline_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml");
$options['title'] = $_REQUEST['pbuts_dashlet_title'];
$options['pbuts_ecminvoiceout_status'] = $_REQUEST['pbuts_ecminvoiceout_status'];
$timeFormat = $current_user->getUserDateTimePreferences();
$options['pbuts_ecminvoiceout_users'] = $_REQUEST['pbuts_ecminvoiceout_users'];
$options['pbuts_date_start'] = $timedate->swap_formats($_REQUEST['pbuts_date_start'], $timeFormat['date'], $timedate->dbDayFormat);
$options['pbuts_date_end'] = $timedate->swap_formats($_REQUEST['pbuts_date_end'], $timeFormat['date'], $timedate->dbDayFormat);
return $options;
}
function displayOptions() {
global $timedate, $image_path, $app_strings, $current_user, $app_list_strings;
$ss = new Sugar_Smarty();
$ss->assign('id', $this->id);
$ss->assign('module', $_REQUEST['module']);
$ss->assign('dashletType', 'predefined_chart');
$ss->assign('LBL_TITLE', translate('LBL_TITLE', 'Charts'));
$ss->assign('LBL_CHART_TYPE', translate('LBL_CHART_TYPE', 'Charts'));
$ss->assign('LBL_DATE_START', translate('LBL_DATE_START', 'Charts'));
$ss->assign('LBL_DATE_END', translate('LBL_DATE_END', 'Charts'));
$ss->assign('LBL_ECMINVOICEOUT_STATUS', translate('LBL_ECMINVOICEOUT_STATUS', 'Charts'));
$ss->assign('LBL_ENTER_DATE', translate('LBL_ENTER_DATE', 'Charts'));
$ss->assign('LBL_ECMINVOICEOUT_USERS', translate('LBL_ECMINVOICEOUT_USERS', 'Charts'));
$ss->assign('LBL_SELECT_BUTTON_TITLE', $app_strings['LBL_SELECT_BUTTON_TITLE']);
$ss->assign('image_path', $image_path);
//get the dates to display
$date_start = $this->pbuts_date_start;
$date_end = $this->pbuts_date_end;
$timeFormat = $current_user->getUserDateTimePreferences();
$ss->assign('date_start', $timedate->swap_formats($date_start, $timedate->dbDayFormat, $timeFormat['date']));
$ss->assign('date_end', $timedate->swap_formats($date_end, $timedate->dbDayFormat, $timeFormat['date']));
$tempx = array();
$datax = array();
$selected_datax = array();
//get list of sales stage keys to display
$user_ecminvoiceout_status = $this->pbuts_ecminvoiceout_status;
$tempx = $user_ecminvoiceout_status;
//set $datax using selected sales stage keys
if (count($tempx) > 0) {
foreach ($tempx as $key) {
$datax[$key] = $app_list_strings['ecminvoiceouts_status_dom'][$key];
array_push($selected_datax, $key);
}
}
else {
$datax = $app_list_strings['ecminvoiceouts_status_dom'];
$selected_datax = array_keys($app_list_strings['ecminvoiceouts_status_dom']);
}
$query = "select id, user_name from users where deleted='0'";
$dbm = new MysqlManager();
$dbm->connect();
$results = $dbm->query($query);
$us = array();
if(is_resource($results))
while($row = $dbm->fetchByAssoc($results)) $us[$row['id']] = $row['user_name'];
$ss->assign('dashlet_title', $this->title);
$ss->assign('selected_datax', get_select_options_with_id($app_list_strings['ecminvoiceouts_status_dom'], $selected_datax));
if(is_array($this->pbuts_ecminvoiceout_users) && count($this->pbuts_ecminvoiceout_users) > 0)
$selected_datax_users = $this->pbuts_ecminvoiceout_users;
else
$selected_datax_users = $us;
$ss->assign('selected_datax_users', get_select_options_with_id($us, $selected_datax_users));
$ss->assign('user_date_format', $timedate->get_user_date_format());
$ss->assign('cal_dateformat', $timedate->get_cal_date_format());
$ss->assign('module', $_REQUEST['module']);
return parent::displayOptions() . $ss->fetch('modules/Charts/Dashlets/PipelineByInvoiceOutsStatusDashlet/PipelineByInvoiceOutsStatusConfigure.tpl');
}
/**
* Displays the javascript for the dashlet
*
* @return string javascript to use with this dashlet
*/
function displayScript() {
global $sugar_config, $current_user, $current_language;
$xmlFile = $sugar_config['tmp_dir']. $current_user->id . '_' . $this->id . '.xml';
$chartStringsXML = 'cache/xml/chart_strings.' . $current_language .'.lang.xml';
$ss = new Sugar_Smarty();
$ss->assign('chartName', $this->id);
$ss->assign('chartXMLFile', $xmlFile);
$ss->assign('chartStyleCSS', chartStyle());
$ss->assign('chartColorsXML', chartColors());
$ss->assign('chartStringsXML', $chartStringsXML);
$str = $ss->fetch('modules/Charts/Dashlets/PredefinedChartDashletScript.tpl');
return $str;
}
function getTitle($text) {
global $image_path, $app_strings, $sugar_config;
if($this->isConfigurable)
$additionalTitle = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text
. '</td><td nowrap width="1%"><div style="width: 100%;text-align:right"><a href="#" onclick="SUGAR.mySugar.configureDashlet(\''
. $this->id . '\'); return false;" class="chartToolsLink">'
. get_image($image_path.'edit','title="' . translate('LBL_DASHLET_EDIT', 'Home') . '" alt="' . translate('LBL_DASHLET_EDIT', 'Home') . '" border="0" align="absmiddle"').'</a> '
. '';
else
$additionalTitle = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text
. '</td><td nowrap width="1%"><div style="width: 100%;text-align:right">';
if($this->isRefreshable)
$additionalTitle .= '<a href="#" onclick="SUGAR.mySugar.retrieveDashlet(\''
. $this->id . '\',\'predefined_chart\'); return false;"><img width="13" height="13" border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" alt="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" src="'
. $image_path . 'refresh.gif"/></a> ';
$additionalTitle .= '<a href="#" onclick="SUGAR.mySugar.deleteDashlet(\''
. $this->id . '\'); return false;"><img width="13" height="13" border="0" align="absmiddle" title="' . translate('LBL_DASHLET_DELETE', 'Home') . '" alt="' . translate('LBL_DASHLET_DELETE', 'Home') . '" src="'
. $image_path . 'close_dashboard.gif"/></a></div></td></tr></table>';
if(!function_exists('get_form_header')) {
global $theme;
require_once('themes/'.$theme.'/layout_utils.php');
}
$str = '<div ';
if(empty($sugar_config['lock_homepage']) || $sugar_config['lock_homepage'] == false) $str .= ' onmouseover="this.style.cursor = \'move\';"';
$str .= 'id="dashlet_header_' . $this->id . '">' . get_form_header($this->title, $additionalTitle, false) . '</div>';
return $str;
}
function display() {
global $app_list_strings, $current_language, $sugar_config, $currentModule, $action, $current_user, $theme, $timedate, $image_path;
$this->loadLanguage('PipelineByInvoiceOutsStatusDashlet', 'modules/Charts/Dashlets/');
$returnStr = '';
$user_dateFormat = $timedate->get_date_format();
$current_module_strings = return_module_language($current_language, 'Charts');
if(isset($_REQUEST['pbuts_refresh'])) {
$refresh = $_REQUEST['pbuts_refresh'];
}
else {
$refresh = false;
}
$date_start = $this->pbuts_date_start;
$date_end = $this->pbuts_date_end;
// cn: format date_start|end to user's preferred
$dateStartDisplay = strftime($timedate->get_user_date_format(), strtotime($date_start));
$dateEndDisplay = strftime($timedate->get_user_date_format(), strtotime($date_end));
$seps = array("-", "/");
$dates = array($date_start, $date_end);
$dateFileNameSafe = str_replace($seps, "_", $dates);
$dateXml[0] = $date_start;
$dateXml[1] = $date_end;
$datax = array();
$selected_datax = array();
//get list of sales stage keys to display
$user_ecminvoiceout_status = $this->pbuts_ecminvoiceout_status;
$tempx = $user_ecminvoiceout_status;
//set $datax using selected sales stage keys
if (count($tempx) > 0) {
foreach ($tempx as $key) {
$datax[$key] = $app_list_strings['ecminvoiceouts_status_dom'][$key];
array_push($selected_datax, $key);
}
}
else {
$datax = $app_list_strings['ecminvoiceouts_status_dom'];
$selected_datax = array_keys($app_list_strings['ecminvoiceouts_status_dom']);
}
$GLOBALS['log']->debug("datax is:");
$GLOBALS['log']->debug($datax);
$ids = array();
$new_ids = array();
$user_ids = $current_user->getPreference('pbuts_ids');
//get list of user ids for which to display data
if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['pbuts_ids'])) {
$ids = $user_ids;
$GLOBALS['log']->debug("USER PREFERENCES['pbuts_ids'] is:");
$GLOBALS['log']->debug($user_ids);
}
elseif (isset($_REQUEST['pbuts_ids']) && count($_REQUEST['pbuts_ids']) > 0) {
$ids = $_REQUEST['pbuts_ids'];
$current_user->setPreference('pbuts_ids', $_REQUEST['pbuts_ids']);
$GLOBALS['log']->debug("_REQUEST['pbuts_ids'] is:");
$GLOBALS['log']->debug($_REQUEST['pbuts_ids']);
$GLOBALS['log']->debug("USER PREFERENCES['pbuts_ids'] is:");
$GLOBALS['log']->debug($current_user->getPreference('pbuts_ids'));
}
else {
$ids = get_user_array(false);
$ids = array_keys($ids);
}
//create unique prefix based on selected users for image files
$id_hash = '1';
if (isset($ids)) {
sort($ids);
$id_hash = crc32(implode('',$ids));
if($id_hash < 0)
{
$id_hash = $id_hash * -1;
}
}
$GLOBALS['log']->debug("ids is:");
$GLOBALS['log']->debug($ids);
$id_md5 = substr(md5($current_user->id),0,9);
$seps = array("-", "/");
$dates = array($dateStartDisplay, $dateEndDisplay);
$dateFileNameSafe = str_replace($seps, "_", $dates);
$cache_file_name = $current_user->getUserPrivGuid()."_".$theme."_my_pipeline_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
$GLOBALS['log']->debug("cache file name is: $cache_file_name");
if (file_exists($sugar_config['tmp_dir'].$cache_file_name)) {
$file_date = date($timedate->get_date_format()." ".$timedate->get_time_format(), filemtime($sugar_config['tmp_dir'].$cache_file_name));
}
else {
$file_date = '';
}
require_once('include/Sugar_Smarty.php');
require_once('include/SugarCharts/SugarChart.php');
$sugar_smarty = new Sugar_Smarty();
$charts = array();
$sugarChart = new SugarChart();
$sugarChart->base_url = array( 'module' => 'EcmInvoiceOuts',
'action' => 'index',
'query' => 'true',
'searchFormTab' => 'advanced_search',
);
$sugarChart->url_params = array();
$ids = $this->pbuts_ecminvoiceout_users;
global $app_list_strings;
$sugarChart->enum_for_labels = array( 'status' => $app_list_strings['ecminvoiceouts_status_dom'] );
$sugarChart->group_by = $this->constructGroupBy();
$query = $this->constructQuery($datax, $dateXml[0], $dateXml[1], $ids, $sugar_config['tmp_dir'].$cache_file_name, $refresh,'hBarS',$current_module_strings);
$sugarChart->setData($this->getChartData($query));
$total = $sugarChart->getTotal();
$currency_symbol = $sugar_config['default_currency_symbol'];
if ($current_user->getPreference('currency')){
require_once('modules/Currencies/Currency.php');
$currency = new Currency();
$currency->retrieve($current_user->getPreference('currency'));
$currency_symbol = $currency->symbol;
}
$sugarChart->is_currency = true;
//$sugarChart->currency_symbol = '';
$sugarChart->thousands_symbol = translate('LBL_OPP_THOUSANDS', 'Charts');
$subtitle = translate('LBL_ECMINVOICEOUTS_SIZE', 'Charts') . " " . $currency_symbol . "1" . translate('LBL_ECMINVOICEOUTS_THOUSANDS', 'Charts');
$pipeline_total_string = translate('LBL_ECMINVOICEOUTS_TOTAL', 'Charts') . ' '. $sugarChart->currency_symbol . format_number($total) . $sugarChart->thousands_symbol;
$sugarChart->setProperties($pipeline_total_string, $subtitle, 'horizontal group by chart');
$xmlFile = $sugar_config['tmp_dir']. $current_user->id . '_' . $this->id . '.xml';
$sugarChart->saveXMLFile($xmlFile, $sugarChart->generateXML());
$returnStr .= $sugarChart->display($this->id, $xmlFile, '100%', '480', false);
return $this->getTitle('') . '<div align="center">' .$returnStr . '</div><br />';
}
// awu: Bug 16794 - this function is a hack to get the correct sales stage order until i can clean it up later
function getChartData($query){
global $app_list_strings, $current_user, $sugar_config;
$data = array();
$temp_data = array();
$selected_datax = array();
$user_ecminvoiceout_status = $this->pbuts_ecminvoiceout_status;
$tempx = $user_ecminvoiceout_status;
//set $datax using selected sales stage keys
if (count($tempx) > 0) {
foreach ($tempx as $key) {
$datax[$key] = $app_list_strings['ecminvoiceouts_status_dom'][$key];
array_push($selected_datax, $key);
}
}
else {
$datax = $app_list_strings['ecminvoiceouts_status_dom'];
$selected_datax = array_keys($app_list_strings['ecminvoiceouts_status_dom']);
}
$db = &PearDatabase::getInstance();
$result = $db->query($query);
$row = $db->fetchByAssoc($result, -1, false);
while($row != null){
array_push($temp_data, $row);
$row = $db->fetchByAssoc($result, -1, false);
}
// reorder and set the array based on the order of selected_datax
foreach($selected_datax as $ecminvoiceout_status){
foreach($temp_data as $key => $value){
if ($value['status'] == $ecminvoiceout_status){
//$value['total'] = $symbol . $value['total'];
//$value['status'] = $app_list_strings['ecminvoiceouts_status_dom'][$value['status']];
array_push($data, $value);
unset($temp_data[$key]);
}
}
}
//echo '<br><br>'; var_dump($data);
return $data;
}
function constructQuery($datax=array('foo','bar'), $date_start='2071-10-15', $date_end='2071-10-15', $user_id=array('1'), $cache_file_name='a_file', $refresh=false,$chart_size='hBarF',$current_module_strings) {
global $app_strings, $charset, $lang, $barChartColors, $current_user;
require_once('modules/Opportunities/Opportunity.php');
require_once('modules/Currencies/Currency.php');
$kDelim = $current_user->getPreference('num_grp_sep');
global $timedate;
$opp = new Opportunity;
$where="";
//build the where clause for the query that matches $user
$count = count($user_id);
$id = array();
$user_list = get_user_array(false);
foreach ($user_id as $key) {
$new_ids[$key] = $user_list[$key];
}
if ($count>0) {
foreach ($new_ids as $the_id=>$the_name) {
$id[] = "'".$the_id."'";
}
$ids = join(",",$id);
$where .= " ecminvoiceouts.assigned_user_id IN ($ids) ";
}
//build the where clause for the query that matches $datax
$count = count($datax);
$dataxArr = array();
if ($count>0) {
foreach ($datax as $key=>$value) {
$dataxArr[] = "'".$key."'";
}
$dataxArr = join(",",$dataxArr);
$where .= "AND ecminvoiceouts.status IN ($dataxArr) ";
}
//build the where clause for the query that matches $date_start and $date_end
$where .= " AND ecminvoiceouts.date_entered >= ". db_convert("'".$date_start."'",'datetime'). "
AND ecminvoiceouts.date_entered <= ".db_convert("'".$date_end."'",'datetime') ;
$where .= " AND ecminvoiceouts.assigned_user_id = users.id AND ecminvoiceouts.deleted=0 ";
//Now do the db queries
//query for opportunity data that matches $datax and $user
/*
$query = " SELECT opportunities.sales_stage,
users.user_name,
opportunities.assigned_user_id,
count( * ) AS opp_count,
sum(amount_usdollar/1000) AS total
FROM users,opportunities ";
$query .= "WHERE " .$where;
$query .= " GROUP BY opportunities.sales_stage";
$query .= ",users.user_name,opportunities.assigned_user_id";
*/
$query = " SELECT ecminvoiceouts.status as status,
users.user_name,
ecminvoiceouts.assigned_user_id,
count( * ) AS opp_count,
sum(total/1000) AS total
FROM users, ecminvoiceouts ";
$query .= "WHERE " .$where;
$query .= " GROUP BY ecminvoiceouts.status";
$query .= ", users.user_name, ecminvoiceouts.assigned_user_id";
//var_dump($query);
//$res = $GLOBALS['db']->query($query);
//var_dump($res);
return $query;
}
function constructGroupBy(){
return array(
'status',
'user_name',
);
}
}
?>