<?php
error_reporting(0);
include_once('include.php');
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
?>
<?php
$flt=racss(1);
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

$sqlst="select report_block from acs where user_names='".$_SESSION['usernames']."' ";
$qry=mysqli_query($conn,$sqlst);

$rst=mysqli_fetch_row($qry);
if($rst[0]==1){
printf("<div style='position:absolute;top:100;left:200;font-size:16;'>Access Denied</div>");}
else{


if(isset($_REQUEST['starts'])){
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

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

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

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

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

if(strtolower($chanell)!='all'){
$flt=$flt." and chanell='".$chanell."' ";}
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
?>  

<?php
$sqlst="select sum(cashs) as cashs from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$cashs=mysqli_fetch_row($qry);

$sqlst="select sum(credits) as creditslm from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$creditsl=mysqli_fetch_row($qry);

$sqlst="select sum(cheques) as chqsle from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$chqsll=mysqli_fetch_row($qry);

$sqlst="select sum(cashs) as cashsaled from dbrdsales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$dcashs=mysqli_fetch_row($qry);

$sqlst="select sum(credits) as creditsd from dbrdsales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$dcreditsl=mysqli_fetch_row($qry);

$sqlst="select sum(cheques) as chqsld from dbrdsales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$dchqsll=mysqli_fetch_row($qry);

$sqlst="select sum(cashcollect) as payments from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$pydt=mysqli_fetch_row($qry);

$sqlst="select sum(cheqcollect) as creditslv from dbrdsales ".$flt." and dates<='".$starts."' and dates<='".$ends."' ";
$qry=mysqli_query($conn,$sqlst);
$tctot=mysqli_fetch_row($qry);

?>

<!doctype html>
<html lang="en">
<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">Dashboard</li>
</ol>
</section>
<br>
<?php

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

$sqlm="select mranges,mainopvl,prvl,lorystvl,prvll,lorystvll,mainopvll from (select distinct item_ranges as mranges from purchases ".$flt." union select distinct item_ranges from main_inventry ".$flt." union select distinct item_ranges from vehicle_inventry ".$flt." group by item_ranges) as agsm

left join (select item_ranges,sum(box_qty*box_price) as prvl from purchases ".$flt." and  dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges) as acsm on acsm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price) as prvll from purchases ".$flt." and  dates<'".$starts."' group by item_ranges) as acsmm on acsmm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as lorystvl from vehicle_inventry where dates>='".$starts."' and dates<='".$ends.
"' and invoice_types='Stores to Vehicle' group by item_ranges) as acsvlm on acsvlm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as lorystvll from vehicle_inventry where dates<'".$starts."' and invoice_types='Stores to Vehicle' group by item_ranges) as acsvlmm on acsvlmm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as mainopvl from main_inventry where dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges) as tgsm on tgsm.item_ranges=mranges

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as mainopvll from main_inventry where dates<'".$starts."' group by item_ranges) as tgsmn on tgsmn.item_ranges=mranges order by mranges";

$maininv=0;
$puchs=0;
$lryst=0;
$oppr=0;
$opmn=0;
$opvh=0;
$qry=mysqli_query($conn,$sqlm);
while($rstt=mysqli_fetch_row($qry)){
$oppr=$oppr+$rstt[4];
$opvh=$opvh+$rstt[5];
$opmn=$opmn+$rstt[6];
$puchs=$puchs+$rstt[2];  
$maininv=$maininv+$rstt[1];
$lryst=$lryst+$rstt[3];
}
?>


<!-- Main content -->
<section class="content">
<!-- Info boxes -->
<div class="row">    
<div class="col-12 col-sm-6 col-md-3">
<div class="info-box">
<span class="info-box-icon bg-orange"><i class="fa fa-window-restore"></i></span>
<div class="info-box-content">
<span class="info-box-text">Main Inventry</span>
<span class="info-box-number">
<?php echo number_format($puchs+$maininv+$oppr+$opmn-$opvh-$lryst,0);?>
<!--<small>%</small>-->
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>

<?php
$con=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

$sqln="select sum(box_qty*box_price) as privlu from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."'";

$qry=mysqli_query($con,$sqln);
while($rstmx=mysqli_fetch_row($qry)){
$prvlu=$rstmx[0];    

}
?>

<div class="col-12 col-sm-6 col-md-3">
<div class="info-box">
<span class="info-box-icon bg-green"><i class="fa fa-window-restore"></i></span>
<div class="info-box-content">
<span class="info-box-text">Primary</span>
<span class="info-box-number"><?php echo number_format($prvlu,0);?></span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>
<!-- /.col -->

<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$sqlst="select sum(pcs_qty*pcs_price)-sum(pcs_qty*pcs_price)/100*dis_rates as srtns from sales_rtns ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$srtn=mysqli_fetch_row($qry);

$sqlst="select sum(pcs_qty*pcs_price)-sum(pcs_qty*pcs_price)/100*dis_rates as mrtns from market_rtns ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$mrtn=mysqli_fetch_row($qry);

$sqlst="select sum(free_qtys*pcs_price) as frees from rd_sales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$fre=mysqli_fetch_row($qry);

$sqlst="select sum(pcs_qty*pcs_price) as rdsale from rd_sales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$rds=mysqli_fetch_row($qry);

$sqlst="select sum(pcalls) as pcal from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$pcall=mysqli_fetch_row($qry);

?>

<!-- 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">
<span class="info-box-icon bg-aqua"><i class="fa fa-opencart"></i></span>
<div class="info-box-content">
<span class="info-box-text">Pcalls</span>    
<span class="info-box-number"><?php echo number_format($pcall[0],0) ?></span>
</div>
<!-- /.info-box-content -->
</div>
 <!-- /.info-box -->
</div>
<!-- /.col -->

<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

$sqlst="select sum(pcs_qty*pcs_price) as msales from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$msales=mysqli_fetch_row($qry);

?>

<div class="col-12 col-sm-6 col-md-3">
<div class="info-box">
<span class="info-box-icon bg-purple"><i class="fa fa-cart-plus"></i></span></i></span>
<div class="info-box-content">
<span class="info-box-text">RD Sales</span>    
<span class="info-box-number"><?php echo number_format($msales[0],0) ?></span>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->

<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Sales Recap</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
     </button>
<div class="btn-group">
<button type="button" class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-wrench"></i></button>
</div>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div>
<!-- /.box-header -->
<div class="row">
        <!-- Left col -->
        <div class="col-md-8">
          <!-- MAP & BOX PANE -->
          <!-- /.box -->
<!-- TABLE: RANGE SALES-->
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Range Sales Status as at <?php echo $dates;?></h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="table-responsive">
<table class="table no-margin">
<thead>
<thead>
<th>Range</th>
<th>Target</th>
<th>Order</th>
<th>Primary</th>
<th>%</th>
<th>Pending</th>
<th>RD</th>
<th>%</th>
</thead>
<tbody>

<?php
$flt=racss(1);
$con=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

if(isset($_REQUEST['starts'])){
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

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

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

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

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

if(isset($_REQUEST['terrys'])){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='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(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}


$sqlstt="select mzones,tgvl,ovl,acvl,rdvlv,penvl from (select distinct item_ranges as mzones from itemsmrd WHERE item_ranges<>'MELLON' and item_ranges<>'FLOUR' and item_ranges<>'ICING' and item_ranges<>'NOODLES'and item_ranges<>'PAPADAM' and item_ranges<>'PASTA' and item_ranges<>'SALT' and item_ranges<>'TOMATO' and item_ranges<>'SOYA' and item_ranges<>'SPICES' order by item_ranges) as ags
 
left join (select item_ranges,sum(box_qty*box_price) as tgvl from rc_targets ".$flt." and 
dates>='".$starts."' and dates<='".$ends."' group by item_ranges) as tgs on tgs.item_ranges=mzones 

left join (select item_ranges,sum(box_qty*box_price) as acvl from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by item_ranges) 
as acs on acs.item_ranges=mzones

left join (select item_ranges,sum(box_qty*box_price) as ovl from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status<>'CANCELLED' group by item_ranges) 
as acss on acss.item_ranges=mzones 

left join (select item_ranges,sum(box_qty*box_price) as penvl from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status='PENDING' group by item_ranges) 
as acssm on acssm.item_ranges=mzones 

left join (select item_ranges,sum(pcs_qty*pcs_price) as rdvlv from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by item_ranges) 
as rdps on rdps.item_ranges=mzones ";


$tottarget=0;
$totpri=0;
$totrd=0;
$totod=0;
$totpen=0;
$qry=mysqli_query($con,$sqlstt);
while($rst=mysqli_fetch_row($qry)){
$tottarget=$tottarget+$rst[1];
$totpri=$totpri+$rst[3];
$totod=$totod+$rst[2];
$totrd=$totrd+$rst[4];
$totpen=$totpen+$rst[5];
    
?> 
<tr>
<td><?php echo ucwords(strtolower($rst[0]));?></td>
<td><?php echo number_format($rst[1],0);?></td>
<td><?php echo number_format($rst[2],0);?></td>
<td><?php echo number_format($rst[3],0);?></td>
<td><?php echo number_format($rst[3]/$rst[1]*100,0);?></td>
<td><?php echo number_format($rst[5],0);?></td>
<td><?php echo number_format($rst[4],0);?></td>
<td><?php echo number_format($rst[4]/$rst[1]*100,0);?></td>
</tr>
<?php } ?>
</tbody>
<td>Total</td><td><?php echo number_format($tottarget,0);?></td><td><?php echo number_format($totod,0);?></td><td><?php echo number_format($totpri,0);?></td><td><?php echo number_format($totpri/$tottarget*100,0);?></td><td><?php echo number_format($totpen,0);?></td><td><?php echo number_format($totrd,0);?></td><td><?php echo number_format($totrd/$tottarget*100,0);?></td>
</table>	
</div>
<!-- /.table-responsive -->
</div>
<!-- /.box-body -->
<div class="box-footer clearfix">
<a href="javascript:void(0)" class="btn btn-sm btn-info btn-flat pull-left">Stock Transfer</a>
<a href="javascript:void(0)" class="btn btn-sm btn-default btn-flat pull-right">View All Items</a>
</div>
<!-- /.box-footer -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
                
<div class="col-md-4">
<p class="text-center">
<strong></u>Sales Contribution at <?php echo $dates ?></strong>
</p>
<div class="progress-group">
<span class="progress-text">Cash Sales</span>
<span class="progress-number"><b><?php echo number_format ($dcashs[0],0) ?></b>/<?php echo number_format($cashs[0],0) ?></span>
<div class="progress sm">
<div class="progress-bar progress-bar-aqua" style="width: <?php echo number_format(($dcashs[0]/$cashs[0])*100,0)?>%"></div>
</div>
</div>
<!-- /.progress-group -->
<div class="progress-group">
<span class="progress-text">Credit Sales</span>
<span class="progress-number"><b><?php echo number_format ($dcreditsl[0],0) ?></b>/<?php echo number_format($creditsl[0],0) ?></span>
<div class="progress sm">
<div class="progress-bar progress-bar-yellow" style="width: <?php echo number_format(($dcreditsl[0]/$creditsl[0])*100,0)?>%"></div>
</div>
</div>
<!-- /.progress-group -->
<div class="progress-group">
<span class="progress-text">Cheque Sales</span>
<span class="progress-number"><b><?php echo number_format ($dchqsll[0],0) ?></b>/<?php echo number_format($chqsll[0],0) ?></span>
<div class="progress sm">
<div class="progress-bar progress-bar-green" style="width: <?php echo number_format(($dchqsll[0]/$chqsll[0])*100,0)?>%"></div>
</div>
</div>
<!-- /.progress-group -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</div>
<!-- ./box-body -->

<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$sqlst="select sum(expen_amount) as examtvl from expenses_detail ".$flt." and expen_date>='".$starts."' and expen_date<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$expct=mysqli_fetch_row($qry);

$revenus=($mrds[0]+$msrtn[0]+$mmrtn[0]+$mdsrt[0])/100*17.5;
$ntrd=($mrds[0]+$msrtn[0]+$mmrtn[0]+$mdsrt[0]);
$rvcst=($revenus-$expct[0])/$ntrd*100;

?>

      <!-- Main row -->
      <div class="row">
        <!-- Left col -->
        <div class="col-md-8">
          <!-- MAP & BOX PANE -->
          <!-- /.box -->
<!-- TABLE: RANGE INVENTRY-->
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Range Wise Inventry as at <?php echo $dates;?></h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="table-responsive">
<table class="table no-margin">
<thead>
<thead>
<th>Range</th>
<th>Open Stock</th>
<th>Closing Stock</th>
<th>Primary</th>
<th>RD</th>
<th>S-Rtns</th>
</thead>
<tbody>

<?php
$flt=racss(1);
$con=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

if(isset($_REQUEST['starts'])){
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

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

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

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

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

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

if(strtolower($chanell)!='all'){
$flt=$flt." and chanell='".$chanell."' ";}
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}

$sqlst="select mranges,prvl,srtnvl,rdvl,mainstk,vhstk,mainstktr,vhstktr,mainstkopp,mainstktropp,vhstkopp,vhstktropp,prvlopp,srtnvlopp,rdvlopp from (select distinct item_ranges as mranges from purchases ".$flt." union select distinct item_ranges from rd_sales ".$flt." union select distinct item_ranges from sales_rtns ".$flt." union select distinct item_ranges from main_inventry ".$flt." union select distinct item_ranges from vehicle_inventry ".$flt.") as ags

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as mainstk from main_inventry ".$flt." and  dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges) as acst on acst.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as mainstkopp from main_inventry ".$flt." and  dates<'".$starts."' group by item_ranges) as acstt on acstt.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as mainstktr from main_inventry ".$flt." and  dates>='".$starts."' and dates<='".$ends.
"' and invoice_types='Vehicle to Stores' group by item_ranges) as acstq on acstq.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as mainstktropp from main_inventry ".$flt." and  dates<'".$starts."' and invoice_types='Vehicle to Stores' group by item_ranges) as acstqq on acstqq.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as vhstktr from vehicle_inventry ".$flt." and  dates>='".$starts."' and dates<='".$ends.
"' and invoice_types='Stores to Vehicle' group by item_ranges) as acstm on acstm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as vhstktropp from vehicle_inventry ".$flt." and  dates<'".$starts."' and invoice_types='Stores to Vehicle' group by item_ranges) as acstmm on acstmm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as vhstk from vehicle_inventry ".$flt." and  dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges) as acstmn on acstmn.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price+pcs_qty*pcs_price) as vhstkopp from vehicle_inventry ".$flt." and  dates<'".$starts."' group by item_ranges) as acstmnm on acstmnm.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price) as prvl from purchases ".$flt." and  dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges) as acs on acs.item_ranges=mranges 

left join (select item_ranges,sum(box_qty*box_price) as prvlopp from purchases ".$flt." and  dates<'".$starts."' group by item_ranges) as acsm on acsm.item_ranges=mranges 

left join (select item_ranges,sum(pcs_qty*pcs_price) as srtnvl from sales_rtns where dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges) as acss on acss.item_ranges=mranges 

left join (select item_ranges,sum(pcs_qty*pcs_price) as srtnvlopp from sales_rtns where dates<'".$starts."' group by item_ranges) as acssm on acssm.item_ranges=mranges 

left join (select item_ranges,sum(pcs_qty*pcs_price) as rdvl from rd_sales where
dates>='".$starts."' and dates<='".$ends."' group by item_ranges) as tgs on tgs.item_ranges=mranges 

left join (select item_ranges,sum(pcs_qty*pcs_price) as rdvlopp from rd_sales where
dates<'".$starts."' group by item_ranges) as tgss on tgss.item_ranges=mranges 
";

$minvst=0;
$vhinvst=0;
$pri=0;
$rd=0;
$srtn=0;
$qry=mysqli_query($con,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$minvst=$minvst+$rst[4]+$rst[1]+$rst[8]+$rst[12]-$rst[5]-$rst[10];
$vhinvst=$vhinvst+$rst[5]-$rst[2]+$rst[10]+$rst[13]-$rst[3]-$rst[6]-$rst[9]-$rst[14];
$pri=$pri+$rst[1];
$rd=$rd+$rst[3];
$srtn=$srtn+$rst[2];
    
?> 
<tr>
<td><?php echo ucwords(strtolower($rst[0]));?></td>
<td><?php echo number_format($rst[4]+$rst[1]+$rst[8]+$rst[12]-$rst[5]-$rst[10],0);?></td>
<td><?php echo number_format($rst[5]-$rst[2]+$rst[10]+$rst[13]-$rst[3]-$rst[6]-$rst[9]-$rst[14],0);?></td>
<td><?php echo number_format($rst[1],0);?></td>
<td><?php echo number_format($rst[3],0);?></td>
<td><?php echo number_format($rst[2],0);?></td>
</tr>
<?php } ?>
</tbody>
<td>Total</td><td><?php echo $minvst;?></td><td><?php echo $vhinvst;?></td><td><?php echo $pri;?></td><td><?php echo $rd;?></td><td><?php echo $srtn;?></td>
</table>	
</div>
<!-- /.table-responsive -->
</div>
<!-- /.box-body -->
<div class="box-footer clearfix">
<a href="javascript:void(0)" class="btn btn-sm btn-info btn-flat pull-left">Stock Transfer</a>
<a href="javascript:void(0)" class="btn btn-sm btn-default btn-flat pull-right">View All Items</a>
</div>
<!-- /.box-footer -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
        


<div class="col-md-4">
<!-- Info Boxes Style 2 -->
<div class="info-box bg-yellow">
<span class="info-box-icon"><i class="fa fa-plus-circle"></i></span>
<?php
$date1=date_create("$starts");
$date2=date_create("$dates");
$diff=date_diff($date1,$date2);
?>

<?php
$sqlst="select sum(cashcollect) as cashamt from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$cashcolect=mysqli_fetch_row($qry);

$sqlst="select sum(cashcollect) as csdep from dbrdsales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$cashdy=mysqli_fetch_row($qry);

?>

<div class="info-box-content">
<span class="info-box-text">Cash Collection</span>
<span class="info-box-number"><b><?php echo number_format($cashdy[0],0)?></b><span class="text-red">/<?php echo number_format($cashcolect[0],0)?></span></span>

<div class="progress">
<div class="progress-bar" style="width:<?php echo number_format(($cashdy[0]/$cashcolect[0])*100,0)?>%"></div>
</div>
<span class="progress-description">
<?php echo number_format(($cashdy[0]/$cashcolect[0])*100,0)?>% contribution in <?php echo $diff->format("%R%a days") ?></span>
</div>
<!-- /.info-box-content -->
</div>


<?php
$sqlst="select sum(chqcollect) as chqamount from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."'";
$qry=mysqli_query($conn,$sqlst);
$chqrel=mysqli_fetch_row($qry);

$sqlst="select sum(chqcollect) as chqamount from dbrdsales ".$flt." and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$chqtrl=mysqli_fetch_row($qry);

?>


<!-- /.info-box -->
<div class="info-box bg-green">
<span class="info-box-icon"><i class="fa fa-check-square-o"></i></span>

<div class="info-box-content">
<span class="info-box-text">Cheque Collection</span>
<span class="info-box-number"><?php echo number_format($chqtrl[0],0)?>/<?php echo number_format($chqrel[0],0)?></span>
<div class="progress">
<div class="progress-bar" style="width: <?php echo number_format(($chqtrl[0]/$chqrel[0])*100,0)?>%"></div>
</div>
<span class="progress-description">
<?php echo number_format(($chqtrl[0]/$chqrel[0])*100,0)?>% Contribution in <?php echo $diff->format("%R%a days") ?>
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->









</div>
<!-- /.col -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->

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

<script>

$(function () { 
Highcharts.chart('container', {
    chart: {
        type: 'line',
        animation: Highcharts.svg, // don't animate in old IE
        marginRight: 10,
        events: {
            load: function () {

                // set up the updating of the chart each second
                var series = this.series[0];
                setInterval(function () {
                    var x = (new Date()).getTime(), // current time
                        y = Math.random();
                    series.addPoint([x, y], true, true);
                }, 1000);
            }
        }
    },

    time: {
        useUTC: false
    },

    title: {
        text: 'Live Sales data'
    },
    xAxis: {
        type: 'datetime',
        tickPixelInterval: 150
    },
    yAxis: {
        title: {
            text: 'Value'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        headerFormat: '<b>{series.name}</b><br/>',
        pointFormat: '{point.x:%Y-%m-%d %H:%M:%S}<br/>{point.y:.2f}'
    },
    legend: {
        enabled: false
    },
    exporting: {
        enabled: false
    },
    series: [{
        name: 'Random data',
        data: (function () {
            // generate an array of random data
            var data = [],
                time = (new Date()).getTime(),
                i;

            for (i = -19; i <= 0; i += 1) {
                data.push({
                    x: time + i * 1000,
                    y: Math.random()
                });
            }
            return data;
        }())
    }]
});
});

</script>
<?php }?>
</body>
</html>