<?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=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($_REQUEST['range'])){
$itmrang=$_REQUEST['range'];}
else{
$itmrang='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."' ";}
if(strtolower($itmrang)!='all'){
$flt=$flt." and item_ranges='".$itmrang."' ";}
?>
<!doctype html>
<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>

<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<div class="box box-success">
<div class="box-header">
<h3 class="box-title">Sales Range Dates - <?php echo $starts;?> - <?php echo $ends;?></h3>
</div>

<!-- /.box-header -->    
<form method='post' class="form-inline" action='slrangeitems.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-warning" onclick="scopys(1)">Copy-Excel</button>
<div class="form-group">
Search:<i class="fa fa-search"></i><td><input type='text' class="form-control" style="margin:5px;" id="myInput" placeholder="Type keyword to search" autocomplete='off' onkeyup="myFunction()"></td>
</div>
</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-striped">
<thead>
<th>Item</th>
<th>Qty</th>
<th class="text-center">Primary</th>
<th>Qty</th>
<th class="text-center">RD-Sales</th>
</thead>
<tbody>

<?php
$sqlst="select mzones,prvl,rdvl,prqt,rdqt from (select distinct item_names as mzones from rc_invoices ".$flt." and 
dates>='".$starts."' and dates<='".$ends."' union select distinct item_names from dbrdsales ".$flt." and 
dates>='".$starts."' and dates<='".$ends."') as ags
 
left join (select item_names,sum(box_qty*box_price) as prvl,sum(box_qty)*packets_per_box as prqt from rc_invoices ".$flt." and 
dates>='".$starts."' and dates<='".$ends."' group by item_names) as tgs on tgs.item_names=mzones 

left join (select item_names,sum(pcs_qty*pcs_price) as rdvl,sum(pcs_qty) as rdqt from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by item_names) 
as acss on acss.item_names=mzones 

 ";

$totpr=0;
$totrd=0;
$qry=mysqli_query($con,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$totpr=$totpr+$rst[1];
$totrd=$totrd+$rst[2];

?> 
<tr>
<td><?php echo ucwords(strtolower($rst[0])); ?></td>
<td align='center'><?php echo number_format($rst[3],0); ?></td>
<td align='center'><?php echo number_format($rst[1],2); ?></td>
<td align='center'><?php echo number_format($rst[4],0); ?></td>
<td align='center'><?php echo number_format($rst[2],2);?></td>
<td align='center'>  
</tr>

<?php } ?>
<td>Total</td><td></td><td align='center'><?php echo number_format($totpr,2);?></td><td></td><td align='center'><?php echo number_format($totrd,2);?></td>
</tbody>
</table>	
</div>
</div>
</div>
</section>
</div>
<?php include_once('footer.php');?>

<?php
if(isset($_GET["do"])=="alert_from_insert"){
//MSK-000143-6-PHP-JS-INSERT
$msg=$_GET['msg'];
if($msg==1){
echo"
<script>
var myModal = $('#nopermition');
myModal.modal('show');
myModal.data('hideInterval', setTimeout(function(){
myModal.modal('hide');
}, 3000));
</script>
";
}
}
?>
<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")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    } 
  }
}
    </script>
</body>
</html>

