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

$flt=racs(1);
$conn=mysqli_connect("localhost","salehljr_rpm","Xa--G-dnWttJ","salehljr_sitegeniqe");

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($_REQUEST['subregions'])){
$subregions=$_REQUEST['subregions'];}
else{
$subregions='all';}

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

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

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

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

#$flt=racs(1);

//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($subregions)!='all'){
//$flt=$flt." and sub_regions='".$subregions."' ";}
//if(strtolower($agents)!='all'){
//$flt=$flt." and agent_names='".$agents."' ";}
//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>
<!--<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="maindash.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="reportpage.php"><i class="fa fa-list"></i> Reports</a></li>
<li class="active">Rg[<?php echo $regions;?>]</li>
<li class="active">Db[<?php echo $agents;?>]</li>
<li class="active">Cat[<?php echo $ranges;?>]</li>
</ol>
</section>
<br>

<!-- Main content -->
<section class="content">
<div class="row">
<!--<div class="col-md-6">  
<div class="box box-success">
<div class="box">    
<div class="box-body no-padding">
<div id="donutchart" style="width: 500px; height: 300px;"></div>
</div>
</div>
</div>
</div>
<div class="col-md-6">  
<div class="box box-success">
<div class="box">    
<div class="box-body no-padding">
<div id="donutchart2" style="width: 500px; height: 300px;"></div>
</div>
</div>
</div>
</div>-->
<div class="col-md-12">
<div class="box box-success">
<div class="box-header">
<h3 class="box-title">Retailer Detailes [GT - <?php echo$starts;?> - <?php echo $ends;?>]</h3>
</div>

<!-- /.box-header -->    
<form method='post' class="form-inline" action='sfaretailerdetails.php?'>
<div class="form-group">
<td>From:<i class="fa fa-calendar-plus-o"></i><input type='text' class="form-control" id='inputField' name='starts' value='<?php echo $starts ?>' autocomplete='off'></td>
</div>
<div class="form-group">
To:<i class="fa fa-calendar-minus-o"></i><td><input type='text' class="form-control" id='inputField2' name='ends' value='<?php echo $ends ?>' autocomplete='off'></td>
</div>
<div class="btn-group">
<button type="submit" style="margin:5px;" class="btn btn-info">Generate-Report</button>
</form>
<button type="button"  style="margin:5px;" class="btn btn-primary" onclick="scopys(1)">Copy-Excel</button>

<!--<button type="button"  style="margin:5px;" class="btn btn-secondary"><span class="badge bg-yellow">0000000</span>Cash</button>


<button type="button"  style="margin:5px;" class="btn btn-secondary"><span class="badge bg-red">0000000</span>Credit</button>-->
              
</div>
<br>
<div class="box box-primary">    
<div class="box-body table-responsive" style="overflow-x:auto;">
<table id="example" class="table table-bordered table-hover">
<thead>
<th class="not_mapped_style" style="text-align:center">Distributor</th>
<th class="not_mapped_style" style="text-align:center">Territory</th>
<th class="not_mapped_style" style="text-align:center">Route</th>
<th class="not_mapped_style" style="text-align:center">Customer</th>
<th class="not_mapped_style" style="text-align:center">Shop</th>
<th class="not_mapped_style" style="text-align:center">Contact</th>
<th class="not_mapped_style" style="text-align:center">Address</th>
<th class="not_mapped_style" style="text-align:center">City</th>
<th class="not_mapped_style" style="text-align:center">Type</th>
<th class="not_mapped_style" style="text-align:center">Avg:Turnover</th>
<th class="not_mapped_style" style="text-align:center">Created</th>
</thead>
<tbody>

<?php

$sqlst="select mranges,msubranges,mitems,tgvl,acvl,tgwt,acwt,odqt,odvl,rdqty,rdvl,pkbk from (select distinct item_ranges as mranges,sub_ranges as msubranges,item_names as mitems from rc_invoices ".$flt." and chanell='GENERAL TRADE' and dates>='".$starts."' and dates<='".$ends."' union select distinct item_ranges,sub_ranges,item_names from rc_targets ".$flt." and dates>='".$starts."' and dates<='".
$ends."' union select distinct item_ranges,sub_ranges,item_names from rc_orders ".$flt." and chanell='GENERAL TRADE' and dates>='".$starts."' and dates<='".
$ends."' union select distinct item_ranges,sub_ranges,item_names from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".
$ends."') as ags

left join (select item_ranges,sub_ranges,item_names,
sum(box_qty) as tgvl,sum(box_qty*box_price) as tgwt from rc_targets ".$flt." and 
dates>='".$starts."' and dates<='".$ends."' group by item_ranges,sub_ranges,item_names) as tgs on tgs.item_ranges=mranges and tgs.sub_ranges=msubranges and tgs.item_names=mitems 

left join (select item_ranges,sub_ranges,item_names,sum(box_qty) as acvl,
sum(box_qty*box_price) as acwt from rc_invoices ".$flt." and chanell='GENERAL TRADE' and dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges,sub_ranges,item_names) as acs on acs.item_ranges=mranges and acs.sub_ranges=msubranges and acs.item_names=mitems

left join (select item_ranges,sub_ranges,item_names,sum(pcs_qty/packets_per_box) as rdqty,
sum(pcs_qty*pcs_price) as rdvl,packets_per_box as pkbk from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends.
"' group by item_ranges,sub_ranges,item_names) as rdc on rdc.item_ranges=mranges and rdc.sub_ranges=msubranges and rdc.item_names=mitems

left join (select item_ranges,sub_ranges,item_names,sum(box_qty) as odqt,
sum(box_qty*box_price) as odvl from rc_orders ".$flt." and chanell='GENERAL TRADE' and dates>='".$starts."' and dates<='".$ends.
"' and order_status<>'CANCELLED' group by item_ranges,sub_ranges,item_names) as acss on acss.item_ranges=mranges and acss.sub_ranges=msubranges and acss.item_names=mitems
order by mranges,tgvl desc

";
 
$tgv=0;
$acv=0;
$tgw=0;
$acw=0;
$rdsl=0;
$rdqtt=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tgw=$tgw+$rst[5];
$acw=$acw+$rst[6];
$odw=$odw+$rst[8];
$rdsl=$rdsl+$rst[10];
$rdqtt=$rst[9]/$rst[11];

if($rst[3]==0){
$pcs=0;}
else{
$pcs=$rst[4]*100/$rst[3];}
	
if($rst[5]==0){
$wpcs=0;}
else{
$wpcs=$rst[6]*100/$rst[5];}

$chanell='General Trade';
?> 
<tr>
<td><?php echo ucwords(strtolower($rst[0])); ?></td>
<td><?php echo ucwords(strtolower($rst[1])); ?></td>
<td><?php echo ucwords(strtolower($rst[2])); ?></td>
<td align='right' class="info text-danger"><?php echo number_format($rst[3],0,'.',','); ?></td>
<td align='right'><?php echo number_format($rst[5],0,'.',','); ?></td>
<td align='right' class="info text-danger"><?php echo number_format($rst[7],0,'.',','); ?></td>
<td align='right'><?php echo number_format($rst[8],0,'.',','); ?></td>
<td align='right' class="info text-danger"><?php echo number_format($rst[4],0,'.',','); ?></td>
<td align='right'><?php echo number_format($rst[6],0,'.',','); ?></td>
<td align='right' class="text-danger"><?php echo number_format($rst[6]/$rst[5]*100,0,'.',','); ?></td>
<td align='right' class="info text-danger"><?php echo number_format($rst[9],0,'.',','); ?></td>
<td align='right'><?php echo number_format($rst[10],0,'.',','); ?></td>
<td align='right' class="text-danger"><?php echo number_format($rst[10]/$rst[5]*100,0,'.',','); ?></td>
<td align='right' class="info text-danger"><?php echo number_format(0,0,'.',','); ?></td>
</tr>
<?php } ?>
<td class="success">Total</td><td class="success"></td><td class="success"></td><td class="success"></td><td class="success" align='center'><?php echo number_format($tgw,0);?><td class="success"></td><td class="success" align='center'><?php echo number_format($odw,0);?><td class="success"></td><td class="success" align='center'><?php echo number_format($acw,0);?><td class="success"></td><td class="success"></td><td class="success" align='center'><?php echo number_format($rdsl,0);?></td><td class="success"></td><td class="success"></td>
</tbody>
</table>	
</div>
</div>
</div>
</section>
</div>
<?php include_once('footer.php');?>

<script>
  $(function () {
    $('#example1').DataTable()
    $('#example2').DataTable({
      'paging'      : true,
      'lengthChange': false,
      'searching'   : false,
      'ordering'    : true,
      'info'        : true,
      'autoWidth'   : false,
    })
  })
  
function scopys(pr)
{
var dts=document.getElementById("example");

var sels=window.getSelection();
sels.removeAllRanges();
var ranges=document.createRange();
ranges.selectNodeContents(dts);
sels.addRange(ranges);
document.execCommand("copy");
swal('Coppied!','Open Excel Sheet Press CTRL+V','success');
sels.removeAllRanges();
} 

 //Date picker
    $('#datepicker').datepicker({
        format: 'yyyy/mm/dd',
      autoclose: true
    })
function myFunction() {
  // Declare variables 
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("example");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[2];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    } 
  }
}

google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChartt);
function drawChartt() {
var data = google.visualization.arrayToDataTable([
['regions', 'contribution'],
<?php
$sql = "SELECT distinct item_ranges,sum(box_qty*box_price) FROM rc_invoices ".$flt." and chanell='General Trade' 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(box_qty*box_price)']."],";
}
?>
]);
var options = {
title: 'Monthly Contribution(Primary)',
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(drawChartp);
function drawChartp() {
var data = google.visualization.arrayToDataTable([
['Region', 'Sales Value'],
<?php
$sql = "SELECT distinct item_ranges,sum(box_qty*box_price) FROM rc_invoices ".$flt." and chanell='General Trade' and dates between '2020-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(box_qty*box_price)']."],";
}
?>
]);

var options = {
title: 'Financial Year Cont(Primary) :',
pieHole: 0.4,
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart2'));
chart.draw(data, options);
}

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

