rendered paste body /**
* Creates pie chart image of opportunities by lead_source.
* param $datax- the sales stage data to display in the x-axis
* param $datay- the sum of opportunity amounts for each opportunity in each sales stage
* to display in the y-axis
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
* All Rights Reserved..
* Contributor(s): ______________________________________..
*/
function pipeline_by_lead_source($legends=array('foo','bar'), $user_id=array('1'), $cache_file_name='a_file', $refresh=true, $show_bu,$size,$business_unit_select) {
global $app_strings,$lang_crm, $current_module_strings, $log, $charset, $tmp_dir;
global $theme;
include_once ("jpgraph/src/jpgraph_pie.php");
include_once ("jpgraph/src/jpgraph_pie3d.php");
$font = calculate_font_family($lang_crm);
if (!file_exists($cache_file_name) || !file_exists($cache_file_name.'.map') || $refresh == true) {
$log =& LoggerManager::getLogger('opportunity charts');
$log->debug("starting pipeline chart");
$log->debug("legends is:");
$log->debug($legends);
$log->debug("user_id is: ");
$log->debug($user_id);
$log->debug("cache_file_name is: $cache_file_name");
//Now do the db queries
//query for opportunity data that matches $legends and $user
$where="";
//build the where clause for the query that matches $user
$count = count($user_id);
if ($count>0) {
$where = "(";
$first = true;
if($business_unit_select == '--not specified--') {
foreach ($user_id as $the_id) {
if (!$first) $where .= "OR ";
$first = false;
$where .= "crmentity.smownerid='$the_id' ";
}
} else {
$where .= "potential.business_unit='$business_unit_select' ";
}
if($show_bu)
{
$where .= "or potential.business_unit='".$GLOBALS['current_user']->business_unit."'";
}
$where .= ") ";
}
//build the where clause for the query that matches $datax
$count = count($legends);
if ($count>0) {
$where .= "AND ( ";
$first = true;
foreach ($legends as $key=>$value) {
if (!$first) $where .= "OR ";
$first = false;
$where .= "leadsource ='$key' ";
}
$where .= ")";
}
$sql = " SELECT potentialid, leadsource, smcreatorid, smownerid, closingdate , potential.amount as adjusted_amount, sales_stage ";
$sql .= " , concat(' ',u2.first_name,' ',u2.last_name) as user_name ";
$sql .= " FROM potential ";
$sql .= " inner join crmentity on crmentity.crmid=potential.potentialid ";
$sql .= " inner join users on users.id = ".$GLOBALS['current_user']->id." ";
$sql .= " inner join users u2 on u2.id = smownerid ";
$sql .= " where crmentity.deleted=0 ";
if ($where != "") $sql .= " and ".$where;
$sql .= " ORDER BY potential.amount DESC, closingdate DESC; ";
global $adb;
$result = $adb->query($sql);
$numRows = $adb->num_rows($result);
$log->debug("XXXXX -> Number of rows = ".$numRows);
$total = 0;
$count = array();
$sum = array();
$user_names = array();
for($i=0; $i < $numRows;$i++) {
$sales_stage = $adb->query_result($result,$i,"sales_stage");
$leadsource = $adb->query_result($result,$i,"leadsource");
$amount = $adb->query_result($result,$i,"adjusted_amount");
$user_names[$assigned_user_id] = $adb->query_result($result,$i,"user_name");
if (!isset($sum[$leadsource])) $sum[$leadsource] = 0;
if (isset($amount) && isset($leadsource)) {
$sum[$leadsource] = $sum[$leadsource] + ($amount/1000);
if (isset($count[$leadsource])) $count[$leadsource]++;
else $count[$leadsource] = 1;
$total = $total + ($amount/1000);
}
}
$total = round($total,2);
$visible_legends = array();
$data= array();
$aTargets = array();
$aAlts = array();
foreach ($legends as $lead_source_key=>$lead_source_translation) {
if (isset($sum[$lead_source_key]))
{
array_push($data, $sum[$lead_source_key]);
if($lead_source_key != '')
{
array_push($visible_legends, $lead_source_translation);
}
else
{
// put none in if the field is blank.
array_push($visible_legends, $current_module_strings['NTC_NO_LEGENDS']);
}
array_push($aTargets, "index.php?module=Potentials&action=ListView&leadsource=".urlencode($lead_source_key)."&query=true&viewname=0&start=1");
array_push($aAlts, $count[$lead_source_key]." ".$current_module_strings['LBL_OPPS_IN_LEAD_SOURCE']." $lead_source_translation ");
}
}
$log->debug("sum is:");
$log->debug($sum);
$log->debug("count is:");
$log->debug($count);
$log->debug("total is: $total");
if ($total == 0) {
return ($current_module_strings['ERR_NO_OPPS']);
}
if($theme == "blue")
{
$font_color = "#212473";
}
else
{
$font_color = "#000000";
}
// Create the Pie Graph.
if($size == 'large')
$graph = new PieGraph(980,520,$cache_file_name);
else
$graph = new PieGraph(490,360,$cache_file_name);
$graph->SetShadow();
// Setup title
$title = $current_module_strings['LBL_TOTAL_PIPELINE'].getCurrencySymbol().$total.$app_strings['LBL_THOUSANDS_SYMBOL'];
$graph->title->Set($title);
$graph->title->SetColor($font_color);
$graph->title->SetFont($font,FS_BOLD,11);
// No frame around the image
$graph->SetFrame(false);
//$graph->SetMarginColor('#F5F5F5');
$graph->legend->Pos(0.01,0.10);
$graph->legend->SetColor($font_color);
$graph->legend->SetFont($font,FS_NORMAL,12);
$subtitle = $current_module_strings['LBL_OPP_SIZE'].getCurrencySymbol().$current_module_strings['LBL_OPP_SIZE_VALUE'];
$graph->footer->left->Set($subtitle);
$graph->footer->left->SetColor($font_color);
$graph->footer->left->SetFont($font,FS_NORMAL,8);
// Create pie plot
$p1 = new PiePlot3d($data);
$p1->SetSize(0.30);
$p1->SetTheme("water");
$p1->SetCenter(0.33,0.35);
$p1->SetAngle(30);
$p1->value->SetFont($font,FS_NORMAL,12);
$p1->SetLegends($visible_legends);
$p1->SetLabelType(PIE_VALUE_ABS);
$p1->value->SetFormat(getCurrencySymbol().'%d');
//set client side image map URL's
$p1->SetCSIMTargets($aTargets,$aAlts);
$graph->Add($p1);
$graph->Stroke($cache_file_name);
$imgMap = $graph->GetHTMLImageMap('pipeline_by_lead_source');
save_image_map($cache_file_name.'.map', $imgMap);
}
else {
$imgMap_fp = fopen($cache_file_name.'.map', "rb");
$imgMap = fread($imgMap_fp, filesize($cache_file_name.'.map'));
fclose($imgMap_fp);
}
$fileModTime = filemtime($cache_file_name.'.map');
$return = "\n$imgMap\n";
$return .= "<img src='$cache_file_name?modTime=$fileModTime'\n";
$return .= "ismap usemap='#pipeline_by_lead_source' border='0'>\n";
return $return;
}
}