Skip to content

Commit

Permalink
Top customers
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Topotushko committed May 24, 2019
1 parent 4d166af commit 1b9431e
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 187 deletions.
171 changes: 168 additions & 3 deletions classes/reports.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5248,17 +5248,18 @@ function yearRatio(){
function _renderTopCustomerLine($data, $arrTotal, $strTitle, $strClass=""){
?>
<tr class="<?php echo $strClass;?>">
<td><?php echo $data['i'];?></td>
<td><?php echo $strTitle;?></td>
<td><?php echo $data['ivlTitle'];?></td>
<td class="budget-decimal"><?php Reports::render($data['GrossRevenue'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render($data['Revenue'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render($data['GP'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render_ratio($data['GP'],$data['GrossRevenue'],1);?></td>
<td class="budget-decimal"><?php Reports::render_ratio($data['GP'],$data['KPI']*100,0);?></td>
<td class="budget-decimal budget-ytd"><?php Reports::render($data['GOP'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render($data['KPI'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render_ratio($data['GOP'],$data['GrossRevenue'],1);?></td>
<td class="budget-decimal budget-ytd"><?php Reports::render($data['GOP'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render_ratio($data['GOP'],$data['GrossRevenue'],1);?></td>
<td class="budget-decimal"><?php Reports::render_ratio($data['GOP'],$data['KPI']*100,0);?></td>
<td class="budget-decimal"><?php Reports::render($data['KPI'],0,'.',',');?></td>
<td class="budget-decimal"><?php Reports::render_ratio($data['GOP'],$arrTotal['GOP'],0);?></td>
</tr>
<?php
Expand All @@ -5272,5 +5273,169 @@ function _echoExecutionTime(){

}

function topCustomers($top = 10, $bottom = 5, $period_type = "ytd", $title=""){

$sqlActual = "SUM(".$this->oBudget->getThisYTDSQL($period_type,$arrActualRates).")";
//$sqlBudget = "SUM(".$this->oBudget->getThisYTDSQL($period_type,$arrBudgetRates).")";

$sql = "SELECT customer_group_code as optValue,
customer_group_title as optText,
ivlTitle,
{$sqlActual} as GOP
FROM vw_master
{$this->sqlWhere}
AND scenario='{$this->oBudget->id}' ".self::GOP_FILTER."
AND IFNULL(customer_group_code,0)<>0
GROUP BY customer_group_code
ORDER BY GOP DESC
";
$rs = $this->oSQL->q($sql);

$i = 1;
while ($rw = $this->oSQL->f($rs)){
$arrReport[$rw['optText']]['GOP'] = $rw['GOP'];
$arrReport[$rw['optText']]['ivlTitle'] = $rw['ivlTitle'];
$arrReport[$rw['optText']]['i'] = $i;
$arrCGFilter[] = $rw['optValue'];
$i++;
}

$arrMeasure = Array(Array('id'=>'Revenue','title'=>'Net revenue','filter'=>Reports::REVENUE_FILTER),
Array('id'=>'GrossRevenue','title'=>'Gross revenue','filter'=>Reports::GROSS_REVENUE_FILTER),
Array('id'=>'GP','title'=>'Gross profit','filter'=>Reports::GP_FILTER),
Array('id'=>'GOP','title'=>'GOP','filter'=>Reports::GOP_FILTER)
);

for($i = 0;$i<count($arrMeasure);$i++){

$sql = "SELECT customer_group_code as optValue,
customer_group_title as optText,
{$sqlActual} as {$arrMeasure[$i]['id']}
FROM vw_master
{$this->sqlWhere}
AND scenario='{$this->oBudget->id}'
{$arrMeasure[$i]['filter']}
##AND customer_group_code IN (".implode(',',$arrCGFilter).")
GROUP BY customer_group_code
";

$rs = $this->oSQL->q($sql);
while ($rw = $this->oSQL->f($rs)){
$arrReport[$rw['optText']][$arrMeasure[$i]['id']] = $rw[$arrMeasure[$i]['id']];
}

$sql = "SELECT {$sqlActual} as {$arrMeasure[$i]['id']}
FROM vw_master
{$this->sqlWhere}
AND scenario='{$this->oBudget->id}'
{$arrMeasure[$i]['filter']}";
$rs = $this->oSQL->q($sql);
$rw = $this->oSQL->f($rs);
$arrReportOther[$arrMeasure[$i]['id']] = $rw[$arrMeasure[$i]['id']];
$arrReportTotal[$arrMeasure[$i]['id']] = $rw[$arrMeasure[$i]['id']];


}

$sql = "SELECT customer_group_code as optValue,
customer_group_title as optText,
SUM(".$this->oBudget->getThisYTDSQL($period_type).") as KPI,
unit
FROM vw_sales
{$this->sqlWhere}
AND scenario='{$this->oBudget->id}' AND unit IN ('Kgs','TEU','Trips')
##AND customer_group_code IN (".implode(',',$arrCGFilter).")
GROUP BY customer_group_code
";
$rs = $this->oSQL->q($sql);
while ($rw = $this->oSQL->f($rs)){
$arrReport[$rw['optText']]['KPI'] = $rw['KPI'];
}

$sql = "SELECT SUM(".$this->oBudget->getThisYTDSQL($period_type).") as KPI,
unit
FROM vw_sales
{$this->sqlWhere}
AND scenario='{$this->oBudget->id}' AND unit IN ('Kgs','TEU','Trips')
";
$rs = $this->oSQL->q($sql);
$rw = $this->oSQL->f($rs);
$arrReportOther['KPI'] = $rw['KPI'];
$arrReportTotal['KPI'] = $rw['KPI'];


$tableID = "top_".md5(time());
?>
<table class="budget" id="<?php echo $tableID;?>">
<caption><?php echo "Top {$top} and bottom {$bottom} customers, {$title}"; ?></caption>
<thead>
<tr>
<th rowspan="2">#</th>
<th rowspan="2">Customer</th>
<th rowspan="2">Vertical</th>
<th colspan="2">Revenue</th>
<th colspan="3">Gross Profit</th>
<th colspan="3">Gross Operating Profit</th>
<th rowspan="2">Volume</th>
<th rowspan="2">% of total GOP</th>
</tr>
<tr>
<th>Gross</th>
<th>Net</th>
<th>RUB</th>
<th>%</th>
<th>per unit</th>
<th class="budget-ytd">RUB</th>
<th>%</th>
<th>per unit</th>2
</tr>
</thead>
<tbody>
<?php

foreach ($arrReport as $customer=>$values){
if($values['Revenue']>0 && $customer!=''){
$arrFilteredReport[$customer] = $values;
}
}

$arrTop = array_slice($arrFilteredReport,0,$top, true);
$arrBottom = array_slice($arrFilteredReport,-$bottom, $bottom, true);
// $arrBottom = array_slice($arrFilteredReport,-1,min(5,count($arrFilteredReport)-10), true);



foreach ($arrTop as $customer=>$values){
$this->_renderTopCustomerLine($values, $arrReportTotal, $customer);
for($i = 0;$i<count($arrMeasure);$i++){
$arrReportOther[$arrMeasure[$i]['id']] -= $values[$arrMeasure[$i]['id']];
}
$arrReportOther['KPI'] -= $values['KPI'];
}

foreach ($arrBottom as $customer=>$values){
//$this->_renderTopCustomerLine($values, $arrReportTotal, $customer); // skip, do not display yet
for($i = 0;$i<count($arrMeasure);$i++){
$arrReportOther[$arrMeasure[$i]['id']] -= $values[$arrMeasure[$i]['id']];
}
$arrReportOther['KPI'] -= $values['KPI'];
}

$this->_renderTopCustomerLine($arrReportOther, $arrReportTotal, "Others");

foreach ($arrBottom as $customer=>$values){
$this->_renderTopCustomerLine($values, $arrReportTotal, $customer);
}
?>
</tbody>
<tfoot>
<?php
$this->_renderTopCustomerLine($arrReportTotal, $arrReportTotal, "Total", "budget-subtotal");
?>
</tfoot>
</table>
<button onclick="SelectContent('<?php echo $tableID;?>');">Copy table</button>
<?php
}
}
?>
74 changes: 42 additions & 32 deletions rep_summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,6 @@
$sqlActual = "SUM(".$oBudget->getThisYTDSQL($period_type,$arrActualRates).")";
$sqlBudget = "SUM(".$oBudget->getThisYTDSQL($period_type,$arrBudgetRates).")";

$settings['gpcus'] = Array('title'=>"{$strPCHeader} :: GP by customer",
'sqlBase' => "SELECT customer_group_code as optValue,
customer_group_title as optText,
{$sqlActual} as Actual,
0 as Budget,
{$sqlActual} as Diff
FROM vw_master
{$oReport->sqlWhere}
AND scenario='{$oBudget->id}'
".Reports::GP_FILTER."
GROUP BY customer_group_code
UNION ALL
SELECT customer_group_code as optValue,
customer_group_title as optText,
0 as Actual,
{$sqlBudget} as Budget,
-{$sqlBudget} as Diff
FROM vw_master
{$oReport->sqlWhere}
AND scenario='{$oReference->id}'
AND source<>'Estimate'
".Reports::GP_FILTER."
GROUP BY customer_group_code",
'denominator'=>$denominator,
'actual_title'=>$oBudget->title,
'budget_title'=>$oReference->title,
'tolerance'=>0.03,
'limit'=>10);

$oWF = new Waterfall($settings['gpcus']);
$oWF->draw();

$settings['oop'] = Array('title'=>"{$strPCHeader} :: OOP by factors",
'sqlBase' => "SELECT IF(`Group_code` IN (108,110,96),item,Group_code) as optValue,
IF(`Group_code` IN (108,110,96),`Budget item`,`Group`) as optText,
Expand Down Expand Up @@ -135,6 +103,37 @@
$oWF = new Waterfall($settings['oop']);
$oWF->draw();

$settings['gpcus'] = Array('title'=>"{$strPCHeader} :: GP by customer",
'sqlBase' => "SELECT customer_group_code as optValue,
customer_group_title as optText,
{$sqlActual} as Actual,
0 as Budget,
{$sqlActual} as Diff
FROM vw_master
{$oReport->sqlWhere}
AND scenario='{$oBudget->id}'
".Reports::GP_FILTER."
GROUP BY customer_group_code
UNION ALL
SELECT customer_group_code as optValue,
customer_group_title as optText,
0 as Actual,
{$sqlBudget} as Budget,
-{$sqlBudget} as Diff
FROM vw_master
{$oReport->sqlWhere}
AND scenario='{$oReference->id}'
AND source<>'Estimate'
".Reports::GP_FILTER."
GROUP BY customer_group_code",
'denominator'=>$denominator,
'actual_title'=>$oBudget->title,
'budget_title'=>$oReference->title,
'tolerance'=>0.03,
'limit'=>10);

$oWF = new Waterfall($settings['gpcus']);
$oWF->draw();

if (strpos($oBudget->type,'Budget')===false){
$sqlActual = "SUM(".$oBudget->getThisYTDSQL('nm',$arrActualRates).")";
Expand Down Expand Up @@ -206,6 +205,17 @@


}

//==================== Top 10 customers ==========================/

if(strpos($oBudget->type,'Budget')!==false){
$period_type = 'fye'; $period_title = "Full year";
} else {
$period_type = 'ytd'; $period_title = "YTD";
}

$oReport->topCustomers(10,5,$period_type,$strPCHeader);

?>
</div>
<?php
Expand Down
Loading

0 comments on commit 1b9431e

Please sign in to comment.