<?php
error_reporting(0);
include_once('include.php');
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}

$flt=racs(1);
if($flt=="access failed"){
printf("<div style='font-color:red;'>>Access failed\nContact Sales and Marketing division</div>");
}
else{
    
$conn=mysqli_connect("localhost","salehljr","!2n+l)a3iX~U","salehljr_diana");

if(isset($zones)){
$zones=$_REQUEST['zones'];}
else{
$zones='all';}

if(isset($regions)){
$regions=$_REQUEST['regions'];}
else{
$regions='all';}

if(isset($agents)){
$agents=$_REQUEST['agents'];}
else{
$agents='all';}

if(isset($terrys)){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='all';}

if(isset($subregions)){
$subregions=$_REQUEST['subregions'];}
else{
$subregions='all';}

if(isset($agenttypes)){
$agenttypes=$_REQUEST['agenttypes'];}
else{
$agenttypes='all';}

if(isset($items)){
$items=$_REQUEST['items'];}
else{
$items='all';}

if(isset($ranges)){
$ranges=$_REQUEST['ranges'];}
else{
$ranges='all';}

if(isset($subranges)){
$subranges=$_REQUEST['subranges'];}
else{
$subranges='all';}

if(isset($dbids)){
$dbids=$_REQUEST['dbids'];}
else{
$dbids='all';}

if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($subregions)!='all'){
$flt=$flt." and sub_regions='".$subregions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
//if(strtolower($dbids)!='all'){
//$flt=$flt." and group_dbid='".$dbids."' ";}
if(strtolower($agenttypes)!='all'){
$flt=$flt." and agent_types='".$agenttypes."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}
if(strtolower($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}
?>  

<!doctype html>
<html lang="en">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<!--<ol class="breadcrumb">
<li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">GENERAL TRADE</li>
</ol>
<div class="alert alert-info alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<h4><i class="icon fa fa-info"></i> Note!</h4>
April / May / June budget not set for (GT).
</div>-->
</section>

<?php
$years=date('Y');
$months=date('m');

$sqlstn="select tgv,acv from (select sum(box_qty*box_price) as tgv from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE') as tgs,
(select sum(box_qty*box_price) as acv from rc_invoices ".$flt." and dates>='".($years-1)."-".$months."-01' and dates<='".
($years-1)."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,($years-1))."' and chanell='GENERAL TRADE') as acs ";

$qryn=mysqli_query($conn,$sqlstn);
$rstmt=mysqli_fetch_row($qryn);
$fymgrow=$rstmt[0]-$rstmt[1];
$fymgrowd=$fymgrow/$rstmt[1]*100;
?>
<!-- Main content -->
<section class="content">
<!-- Info boxes -->
<div class="row">    
<div class="col-12 col-sm-6 col-md-3">
<div class="info-box bg-aqua">
<span class="info-box-icon"><span class="info-box-text"><?php echo date('M') ;?></span></span>
<div class="info-box-content">
<span class="info-box-text"><?php echo number_format($rstmt[1]/1000000,2,'.',',');?> [<?php echo (date('Y')-1) ;?>]</span>
<span class="info-box-text"><?php echo number_format($rstmt[0]/1000000,2,'.',',');?> [<?php echo (date('Y')) ;?>]</span>
<div class="progress">
<div class="progress-bar" style="width: <?php echo number_format($fymgrowd,2,'.',',');?>%"></div>
</div>
<span class="progress-description">
<?php echo number_format($fymgrowd,0,'.',',');?>% Growth
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>

<!--/////////////////Financial year achivements calculations-- SHOULD GIVE DATE FORMULA -->
<?php
$sqlss="select tg,pri from (select sum(box_qty*box_price) as tg from rc_targets ".$flt." and dates>='2022-04-01' and dates<='2023-03-31') as tgs,
(select sum(box_qty*box_price) as pri from rc_invoices ".$flt." and dates>='2022-04-01' and dates<='2023-03-31' and chanell='GENERAL TRADE') as acs ";

$qrss=mysqli_query($conn,$sqlss);
$rstss=mysqli_fetch_row($qrss);
?>

<!---FINANCILA DATE FORMULA NEEDED TO APPLY -->
<div class="col-12 col-sm-6 col-md-3">
<div class="info-box bg-green">
<span class="info-box-icon"><span class="info-box-text">FY-<?php echo date('y')-1;?>/<?php echo (date('y'));?><br></span></span>
<div class="info-box-content">
<span class="info-box-text">T - <?php echo number_format($rstss[0]/1000000,2,'.',',');?></span>
<span class="info-box-text">P - <?php echo number_format($rstss[1]/1000000,2,'.',',');?></span>
<div class="progress">
<div class="progress-bar" style="width: <?php echo number_format(($rstss[1]/$rstss[0]*100),2,'.',',');?>%"></div>
</div>
<span class="progress-description">
<?php echo number_format(($rstss[1]/$rstss[0]*100),2,'.',',');?>% in 0M
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>    

<?php
if($months>=4){
$fstarts=date('Y-04-01');
$pstarts=($years-1)."-04-01";
$fyears=$years;
$pyears=$years-2;
$pends=$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years);}
else{
$fstarts=($years-1)."-04-01";
$pstarts=($years-2)."-04-01";
$fyears=$years-1;
$pyears=$years-1;
$pends=($years-1)."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$fstarts);}
$ppends=($years-1)."-03-31";
$starts=date('Y-m-01');
$ends=date('Y-m-t');
$fydif=0;


///////Dates need adjustments

$sqlst="select tgv,acv from (select sum(box_qty*box_price) as tgv from rc_invoices ".$flt." and dates>='".$fstarts."' and dates<='".$ends."' and chanell='GENERAL TRADE') as tgs,
(select sum(box_qty*box_price) as acv from rc_invoices ".$flt." and dates>='".$pstarts."' and dates<='".$ppends."' and chanell='GENERAL TRADE') as acs ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$fyfif=($rst[0]-$rst[1])/$rst[1]*100;
?>

<!-- fix for small devices only -->
<div class="clearfix visible-sm-block"></div>
<div class="col-12 col-sm-6 col-md-3">
<div class="info-box bg-yellow">
<span class="info-box-icon"><span class="info-box-text">FY-<?php echo date('y')-1;?>/<?php echo date('y');?></span></span>
<div class="info-box-content">
<span class="info-box-text"><?php echo number_format($rst[1]/1000000,2,'.',',');?> [<?php echo (date('y')-2) ;?>/<?php echo date('y')-1;?>]</span>
<span class="info-box-text"><?php echo number_format($rst[0]/1000000,2,'.',',');?> [<?php echo date('y')-1 ;?>/<?php echo date('y');?>]</span>
<div class="progress">
<div class="progress-bar" style="width: <?php echo number_format($rst[0]/$rst[1]*100,0,'.',',');?>%"></div>
</div>
<span class="progress-description"><?php echo number_format($fyfif,2,'.',',');?>% 0M
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
 </div>

 
<?php 
$monthdate=date('t');
$datess=cal_days_in_month(CAL_GREGORIAN, 9, 2023);
$prevdatestart=date('Y-m-d', strtotime('-1 month', strtotime($starts)));
$prevdateend=date('Y-m-d', strtotime('-1 month', strtotime($ends)));;

//$prevdatestart = new DateTime("first day of last month");
//$prevdateend = new DateTime("last day of last month");

//$mstartdatem ='2020-09-01';
//$enddatem = '2020-09-30';

//echo $mstartdatem->format('Y-m-d'); // 2012-02-01
//echo $enddatem->format('Y-m-d'); // 2012-02-29

$conn=mysqli_connect("localhost","salehljr","!2n+l)a3iX~U","salehljr_diana");

$chartcash = '';
$chartcredit = '';
$chartmonths= '';
$chartdate='';

$charttarget='';
$chartprimary='';
$firstchartdate='';


//Chart for the Previous Month for Target / Primary / RD

//$sqlstt="select mzones,prvl,rdvl from (select distinct dates as mzones from rc_targets ".$flt." and dates>='".$prevdatestart."' and dates<='".$prevdateend."' union select distinct dates as mzones from rc_invoices ".$flt." and dates>='".$prevdatestart."' and dates<='".$prevdateend."') as ags

//left join (select dates,sum(box_qty*box_price)as prvl from rc_invoices ".$flt." and dates>='".$prevdatestart."' and dates<='".$prevdateend."' group by dates) as prpr on prpr.dates=mzones

//left join (select dates,sum(box_qty*box_price)as rdvl from rc_targets ".$flt." and dates>='".$prevdatestart."' and dates<='".$prevdateend."' group by dates) as prprr on prprr.dates=mzones order by mzones";

//$resultp = mysqli_query($conn, $sqlstt);
//loop through the returned data
//while ($rowp = mysqli_fetch_array($resultp)) {

//$charttarget = $charttarget . '"'. $rowp['2'].'",';
//$chartprimary = $chartprimary . '"'. $rowp['1'] .'",';
//$firstchartdate = $firstchartdate . '"'. date('d-M',strtotime($rowp['0'])) .'",';
//}

//$charttarget = trim($charttarget,",");
//$chartprimary = trim($chartprimary,",");
//$firstchartdate = trim($firstchartdate,",");

//$sqlsttrend="select  DATE_FORMAT(dates, '%M') AS Month,SUM(box_qty*box_price)
//FROM rc_invoices ".$flt." and dates>='2020-04-01' and dates<='".$ends."' and chanell='GENERAL TRADE'
//GROUP BY DATE_FORMAT(dates, '%M') ORDER BY dates";

//$resultptrend = mysqli_query($conn, $sqlsttrend);
//loop through the returned data
//while ($rowpt = mysqli_fetch_array($resultptrend)) {

//$primarytrend = $primarytrend . '"'. $rowpt['1'] .'",';
//$targettrend = $targettrend . '"'. $rowpt['2'] .'",';
//$rdtrend = $rdtrend . '"'. $rowpt['3'] .'",';
//$trendate = $trendate . '"'. date('M',strtotime($rowpt['0'])) .'",';
//}

//$primarytrend = trim($primarytrend,",");
//$targettrend = trim($targettrend,",");
//$rdtrend = trim($rdtrend,",");
//$trendate = trim($trendate,",");

/////Chart 0st code Start here/////
$chartrange="select mzoness,bisvl,chvl,wfvall,snkval,crkval,pranges from (select distinct date_charts as mzoness,item_ranges as pranges from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' group by DATE_FORMAT(date_charts, '%M')) as agch

LEFT JOIN (select date_charts,sum(box_qty*box_price)as bisvl,item_ranges from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' and item_ranges='BISCUITS' group by DATE_FORMAT(date_charts, '%M')) as bsch on bsch.date_charts=mzoness

LEFT JOIN (select date_charts,sum(box_qty*box_price)as chvl,item_ranges from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' and item_ranges='CHOCOLATE' group by DATE_FORMAT(date_charts, '%M')) as chch on chch.date_charts=mzoness

LEFT JOIN (select date_charts,sum(box_qty*box_price)as wfvall,item_ranges from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' and item_ranges='WAFER' group by DATE_FORMAT(date_charts, '%M')) as wfch on wfch.date_charts=mzoness

LEFT JOIN (select date_charts,sum(box_qty*box_price)as snkval,item_ranges from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' and item_ranges='SNACK' group by DATE_FORMAT(date_charts, '%M')) as snch on snch.date_charts=mzoness

LEFT JOIN (select date_charts,sum(box_qty*box_price)as crkval,item_ranges from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' and item_ranges='CRACKER' group by DATE_FORMAT(date_charts, '%M')) as crch on crch.date_charts=mzoness
order by mzoness ASC";

$rangeresult = mysqli_query($conn, $chartrange);
while ($rowrange= mysqli_fetch_array($rangeresult)) {

$chartrangebis = $chartrangebis . '"'. $rowrange['1'].'",';
$chartrangech = $chartrangech . '"'. $rowrange['2'] .'",';
$chartrangewf = $chartrangewf . '"'. $rowrange['3'] .'",';
$chartrangesnk = $chartrangesnk . '"'. $rowrange['4'] .'",';
$chartrangescrk = $chartrangescrk . '"'. $rowrange['5'] .'",';
$chartrangedate = $chartrangedate . '"'. date('M-y',strtotime($rowrange['0'])) .'",';
}

$chartrangebis = trim($chartrangebis,",");
$chartrangech = trim($chartrangech,",");
$chartrangewf = trim($chartrangewf,",");
$chartrangesnk = trim($chartrangesnk,",");
$chartrangescrk = trim($chartrangescrk,",");
$chartrangedate = trim($chartrangedate,",");
?><!--Chart 01 codes end here -->

<?php
/////Chart two code Start here/////
$sqlstk="select mzoness,prvll,tggvll,chanels,rdsvll from (select distinct date_charts as mzoness,chanell as chanels from rc_targets ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' group by DATE_FORMAT(date_charts, '%M') UNION select distinct date_charts as mzoness,chanell as chanels from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' group by DATE_FORMAT(date_charts, '%M')) as ags
LEFT JOIN (select date_charts,sum(box_qty*box_price)as tggvll from rc_targets ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' group by DATE_FORMAT(date_charts, '%M')) as prpr on prpr.date_charts=mzoness
LEFT JOIN (select date_charts,sum(box_qty*box_price)as prvll from rc_invoices ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' group by DATE_FORMAT(date_charts, '%M')) as prprr on prprr.date_charts=mzoness
LEFT JOIN (select date_charts,sum(pcs_qty*pcs_price)as rdsvll from rd_saleinv ".$flt." and date_charts>='2024-04-01' and date_charts<='".$ends."' and chanell='GENERAL TRADE' group by DATE_FORMAT(date_charts, '%M')) as rdsp on rdsp.date_charts=mzoness
order by mzoness ASC";

$resultk = mysqli_query($conn, $sqlstk);
//loop through the returned data
while ($rowk = mysqli_fetch_array($resultk)) {

$chartprimaryy = $chartprimaryy . '"'. $rowk['1'].'",';
$charttargett = $charttargett . '"'. $rowk['2'] .'",';
$chartrds = $chartrds . '"'. $rowk['4'] .'",';
$chartdate = $chartdate . '"'. date('M-y',strtotime($rowk['0'])) .'",';
}

$chartprimaryy = trim($chartprimaryy,",");
$charttargett = trim($charttargett,",");
$chartrds = trim($chartrds,",");
$chartdate = trim($chartdate,",");
?> <!--Chart Two codes end here -->

<?php

$rm1sql="select sum(box_qty*box_price) as prvalue from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and zones='REGION-1'";
$rm1result= mysqli_query($conn,$rm1sql);
while($rm1row = mysqli_fetch_array($rm1result))
$rm1=$rm1row[0];

$rm2sql="select sum(box_qty*box_price) as prvalue from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and zones='REGION-2'";
$rm2result= mysqli_query($conn,$rm2sql);
while($rm2row = mysqli_fetch_array($rm2result))
$rm2=$rm2row[0];

$rm3sql="select sum(box_qty*box_price) as prvalue from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and zones='REGION-3'";
$rm3result= mysqli_query($conn,$rm3sql);
while($rm3row = mysqli_fetch_array($rm3result))
$rm3=$rm3row[0];

?>

<div class="col-12 col-sm-6 col-md-3">
<div class="info-box bg-red">
<span class="info-box-icon"><span class="info-box-text">RM</span></span>
<div class="info-box-content">
<span class="info-box-text">RM1-<?php echo number_format($rm1,0,'.',',');?></span>
<span class="info-box-text">RM2-<?php echo number_format($rm2,0,'.',',');?></span>
<div class="progress">
<div class="progress-bar" style="width: 0%"></div>
</div>
<span class="progress-description">
RM3-<?php echo number_format($rm3,0,'.',',');?>
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>

<div class="row">
<div class="container-fluid">
<div class="col-md-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Range-Trend[Primary]</h3>
<div class="box-tools pull-right">
<h3 class="box-title"><?php echo date('M-y');?></h3>
</div>

<!-- /.box-header -->
<div class="box-body">
<div class="row">
    
<div class="col-md-6">
<div class="chart">
<!-- Sales Chart Canvas -->
<canvas id="myChart2" style="height:200px; height: 38vh; background: #222; border: 1px solid #555652; margin-top: 10px;"></canvas>

</div>
</div>

<div class="col-md-6">
<div class="chart">
<!-- Sales Chart Canvas -->
<canvas id="myChart" style="height:200px; height: 38vh; background: #222; border: 1px solid #555652; margin-top: 10px;"></canvas>
</div>
</div>

</div>
</div>


<div class="box-footer">
<div class="row">
<div class="col-sm-3 col-xs-6">
<div class="description-block border-right">
<span class="description-percentage text-green"><i class="fa fa-caret-up"></i> 0.0%</span>
<h5 class="description-header"><?php echo number_format($revenus,0);?></h5>
<span class="description-text">GROWTH</span>
</div>
 <!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-3 col-xs-6">
<div class="description-block border-right">
<span class="description-percentage text-yellow"><i class="fa fa-caret-left"></i> 0%</span>
<h5 class="description-header"><?php echo number_format($expct[0],0);?></h5>
<span class="description-text">Admin COST</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->

<div class="col-sm-3 col-xs-6">
<div class="description-block border-right">
<span class="description-percentage text-green"><i class="fa fa-caret-up"></i> <?php echo number_format($rvcst,0);?>%</span>
<h5 class="description-header"><?php echo number_format($revenus-$expct[0],0);?></h5>
<span class="description-text">MTD COST</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->


<div class="col-sm-3 col-xs-6">
<div class="description-block">
<span class="description-percentage text-red"><i class="fa fa-caret-down"></i> 0%</span>
<h5 class="description-header"><?php echo number_format($revenus-$expct[0],0);?></h5>
<span class="description-text">YTD COST</span>
</div>
<!-- /.description-block -->
</div>
</div>
<!-- /.row -->
</div>
<!-- /.box-footer -->
</div ><!-- /. this top box 12md above chart heading -->
<!-- /.box -->
</div> <!-- /.this top box 12md above chart heading -->
<!-- /.col -->
</div>
<!-- /.row -->

<!-- Main row range contribution-->
<div class="row">
<div class="container-fluid">    
<!-- Left col -->
<div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title">Primary Range - [<?php echo date('M');?>]</h3>
</div>
<!-- /.box-header -->
<div class="box">
<div class="box-body no-padding">
<div id="piechart" style="height:150px; height: 38vh; width:450px; margin-top: 10px;"></div>

</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
   
   
 <div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title">Primary Range - [F/Y] <?php echo date('y')-1;?>/<?php echo date('y');?></h3>
</div>
<!-- /.box-header -->
<div class="box">
<div class="box-body no-padding">
<div id="donutchartt" style="height:150px; height: 38vh; width:450px; margin-top: 10px;"></div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
<!-- /.box -->
</div><!-- /.fluid range contribution-->  
</div><!-- /.row range cintribution--> 


<!-- Main row range contribution-->
<div class="row">
<div class="container-fluid">    
<!-- Left col -->
<div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title">RD Range - [<?php echo date('M');?>]</h3>
</div>
<!-- /.box-header -->
<div class="box">
<div class="box-body no-padding">
<div id="donutchart" style="height:150px; height: 38vh; width:450px; margin-top: 10px;"></div>

</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
   
   
<div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title">RD Range - [F/Y]<?php echo date('y')-1;?>/<?php echo date('y');?></h3>
</div>
<!-- /.box-header -->
<div class="box">
<div class="box-body no-padding">
<div id="donutchartft" style="height:150px; height: 38vh; width:450px; margin-top: 10px;"></div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
<!-- /.box -->
</div><!-- /.fluid range contribution-->  
</div><!-- /.row range cintribution--> 



<!-- Main row top products primary-->
<div class="row">
<div class="container-fluid">    
<!-- Left col -->
<div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title"><mark><P class="text-warning">Top Products-Primary-[<?php echo date('M');?>]</P></mark></h3>

<div class="card-body table-responsive p-0" style="height: 300px;">
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Qty</th>
<th>Ton</th>
<th>Value</th>
</tr>
</thead>
<tbody>
    
<?php
$tablerowid=1; 
$sqlsttop="select distinct item_names as items,sum(box_qty) as priqty,sum(box_qty* box_price) as privalue,sum(box_qty*packets_per_box*packet_weights/1000000) from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' group by item_names ORDER BY sum(box_qty*box_price) DESC";
$qry=mysqli_query($conn,$sqlsttop);
while($rstopitem=mysqli_fetch_row($qry)){
?>    
<tr>
<td><?php echo $tablerowid++;?></td>
<td class="text-success"><?php echo ucfirst(strtolower($rstopitem[0])); ?></td>
<td><?php echo number_format($rstopitem[1],0,'.',','); ?></td>
<td class="text-info"><?php echo number_format($rstopitem[3],2,'.',','); ?></td>
<td><?php echo number_format($rstopitem[2],0,'.',','); ?></td>
</tr>

<?php } ?>    
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
   
   
 <div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title"><mark><P class="text-warning">Top Products-RD-[<?php echo date('M');?>]</P></mark></h3>

<div class="card-body table-responsive p-0" style="height: 300px;">
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Qty</th>
<th>Ton</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<?php
$rdtableidcounter=1;
$sqlsttop="select distinct item_names as items,sum(pcs_qty)/packets_per_box as priqty,sum(pcs_qty* pcs_price) as privalue,sum(pcs_qty*packet_weights/1000000) from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by item_names ORDER BY sum(pcs_qty*pcs_price) DESC";
$qry=mysqli_query($conn,$sqlsttop);
while($rstopitem=mysqli_fetch_row($qry)){
?>    
<tr>
<td><?php echo $rdtableidcounter++;?></td>
<td class="text-success"><?php echo ucfirst(strtolower($rstopitem[0])); ?></td>
<td class="bg-info"><?php echo number_format($rstopitem[1],0,'.',','); ?></td>
<td class="text-info"><?php echo number_format($rstopitem[3],2,'.',','); ?></td>
<td><?php echo number_format($rstopitem[2],0,'.',','); ?></td>
</tr>

<?php } ?>     
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>  
</div><!-- /.fluidTOP PRODUCTS-->  
</div><!-- /.row Top products--> 



<!-- Main row top Distributor primary-->
<div class="row">
<div class="container-fluid">    
<!-- Left col -->
<div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title"><mark><P class="text-warning">Top Distributor-Primary-[<?php echo date('M');?>]</P></mark></h3>

<div class="card-body table-responsive p-0" style="height: 300px;">
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Distributor Name</th>
<th>Territory</th>
<th>Ton</th>
<th>Sales</th>
<th>Ach%</th>
</tr>
</thead>
<tbody>
    
<?php
$dbpritblid=1; 
$sqlsttopdbpri="select distinct agent_names as agentspri,territory_names,sum(box_qty* box_price) as privalue,sum(box_qty*packets_per_box*packet_weights/1000000) from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='General Trade' group by agent_names ORDER BY sum(box_qty*box_price) DESC";
$qry=mysqli_query($conn,$sqlsttopdbpri);
while($rstopdbpri=mysqli_fetch_row($qry)){
$achpri=0;    
?>    
<tr>
<td><?php echo $dbpritblid++;?></td>
<td class="text-success"><?php echo ucfirst(strtolower($rstopdbpri[0])); ?></td>
<td><?php echo ucfirst(strtolower($rstopdbpri[1])); ?></td>
<td class="text-info"><?php echo number_format($rstopdbpri[3],2,'.',','); ?></td>
<td><?php echo number_format($rstopdbpri[2],0,'.',','); ?></td>
<td class="danger"><?php echo number_format($achpri,0,'.',',');?></td>
</tr>

<?php } ?>    
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
   
   
<div class="col-md-6">
<!-- MAP & BOX PANE -->
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title"><mark><P class="text-warning">Top Distributor-RD-[<?php echo date('M');?>]</P></mark></h3>

<div class="card-body table-responsive p-0" style="height: 300px;">
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Distributor Name</th>
<th>Territory</th>
<th>Ton</th>
<th>Sales</th>
<th>Ach%</th>
</tr>
</thead>
<tbody>
<?php
$rdtbliddb=1;
$sqlsttopdbrd="select distinct agent_names as items,territory_names as rddbterri,sum(pcs_qty* pcs_price) as rddbvalue,sum(pcs_qty*packet_weights/1000000) from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by agent_names ORDER BY sum(pcs_qty*pcs_price) DESC";
$qry=mysqli_query($conn,$sqlsttopdbrd);
while($rstopdbrd=mysqli_fetch_row($qry)){
$achrd=0;    
?>    
<tr>
<td><?php echo $rdtbliddb++;?></td>
<td class="text-success"><?php echo ucfirst(strtolower($rstopdbrd[0])); ?></td>
<td><?php echo ucfirst(strtolower($rstopdbrd[1])); ?></td>
<td class="text-info"><?php echo number_format($rstopdbrd[3],2,'.',','); ?></td>
<td><?php echo number_format($rstopdbrd[2],0,'.',','); ?></td>
<td class="danger"><?php echo number_format($achrd,0,'.',',');?></td>
</tr>

<?php } ?>     
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>  
</div><!-- /.fluidTOP PRODUCTS-->  
</div><!-- /.row Top products--> 


  


</section>
<!-- /.content -->
</div><!-- /.chart fluid -->
<!-- /.content-wrapper -->

<?php include_once('footer.php');

}?>

<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [<?php echo $chartdate; ?>],
datasets: [
{
label: 'Target',
data: [<?php echo $charttargett; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(244, 67, 54)',
borderWidth: 3
},

{
label: 'Primary',
data: [<?php echo $chartprimaryy; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(0,255,255)',
borderWidth: 3	
},
{
label: 'RD',
data: [<?php echo $chartrds; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(245, 127, 23)',
borderWidth: 3	
}]
},
options: {
scales: {scales:{yAxes: [{beginAtZero: false}], xAxes: [{autoskip: true, maxTicketsLimit: 20}]}},
tooltips:{mode: 'index'},
legend:{display: true, position: 'top', labels: {fontColor: 'rgb(255,255,255)', fontSize: 12}}
}
});

var ctx = document.getElementById('myChart2').getContext('2d');
var myChart2 = new Chart(ctx, {
type: 'line',
data: {
labels: [<?php echo $chartrangedate; ?>],
datasets: [{
label: 'Biscuits',
data: [<?php echo $chartrangebis; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(0, 127, 255, 1.00)',
borderWidth: 3
},
{
label: 'Chocolate',
data: [<?php echo $chartrangech; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(255, 170, 170, 1.00)',
borderWidth: 3	
},

{
label: 'Wafer',
data: [<?php echo $chartrangewf; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(95, 191, 0, 1.00)',
borderWidth: 3	
},

{
label: 'Snack',
data: [<?php echo $chartrangesnk; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(127, 0, 255, 1.00)',
borderWidth: 3	
},

{
label: 'Cracker',
data: [<?php echo $chartrangescrk; ?>],
backgroundColor: 'transparent',
borderColor:'rgba(244, 83, 83, 1.00)',
borderWidth: 3	
}

]
},
options: {
scales: {scales:{yAxes: [{beginAtZero: false}], xAxes: [{autoskip: true, maxTicketsLimit: 20}]}},
tooltips:{mode: 'index'},
legend:{display: true, position: 'top', labels: {fontColor: 'rgb(255,255,255)', fontSize: 12}}
}
});


google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['item_ranges', 'contribution'],
<?php
$sql = "SELECT distinct item_ranges,sum(box_qty*box_price) FROM rc_invoices ".$flt." and dates between '".$starts."' and '".$ends."' and chanell='GENERAL TRADE' group by item_ranges";
$fire = mysqli_query($conn,$sql);
while ($result = mysqli_fetch_assoc($fire)) {
echo"['".$result['item_ranges']."',".$result['sum(box_qty*box_price)']."],";
}
?>
]);
var options = {
title: 'Sales contribution'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChartpr);
function drawChartpr() {
var data = google.visualization.arrayToDataTable([
['item_ranges', 'contribution'],
<?php
$sql = "SELECT distinct item_ranges,sum(box_qty*box_price) FROM rc_invoices ".$flt." and dates between '2024-04-01' and '".$ends."' and chanell='GENERAL TRADE' group by item_ranges";
$fire = mysqli_query($conn,$sql);
while ($result = mysqli_fetch_assoc($fire)) {
echo"['".$result['item_ranges']."',".$result['sum(box_qty*box_price)']."],";
}
?>
]);
var options = {
title: 'Sales contribution'
};
var chart = new google.visualization.PieChart(document.getElementById('donutchartt'));
chart.draw(data, options);
}


google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChartt);
function drawChartt() {
var data = google.visualization.arrayToDataTable([
['item_ranges', 'contribution'],
<?php
$sql = "SELECT distinct item_ranges,sum(pcs_qty*pcs_price) FROM rd_saleinv ".$flt." and dates between '".$starts."' and '".$ends."' group by item_ranges";
$firee = mysqli_query($conn,$sql);
while ($resultt = mysqli_fetch_assoc($firee)) {
echo"['".$resultt['item_ranges']."',".$resultt['sum(pcs_qty*pcs_price)']."],";
}
?>
]);
var options = {
title: 'Sales Contribution',
pieHole: 0.4,
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
}

google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawCharttfy);
function drawCharttfy() {
var data = google.visualization.arrayToDataTable([
['item_ranges', 'contribution'],
<?php
$sql = "SELECT distinct item_ranges,sum(pcs_qty*pcs_price) FROM rd_saleinv ".$flt." and dates between '2024-04-01' and '".$ends."' group by item_ranges";
$firee = mysqli_query($conn,$sql);
while ($resultt = mysqli_fetch_assoc($firee)) {
echo"['".$resultt['item_ranges']."',".$resultt['sum(pcs_qty*pcs_price)']."],";
}
?>
]);
var options = {
title: 'Sales Contribution',
pieHole: 0.4,
};
var chart = new google.visualization.PieChart(document.getElementById('donutchartft'));
chart.draw(data, options);
}

</script>
</body>
</html>