<?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'];}

?>
<!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">Agent Total RD-Sales</h3>
</div>

<!-- /.box-header -->    
<form method='post' class="form-inline" action='agtotalrd.php?actp=rpt%s'>
<div class="form-group">
<td>From:<i class="fa fa-calendar"></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"></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-success" onclick="scopys(1)">Copy-Excel</button>
</div>
<br>
<div class="box box-primary">    
<div class="box-body table-responsive" style="overflow-x:auto;">
<table id="example1" class="table table-bordered table-striped">
<thead>
<th>Wdays</th>
<th>Agent</th>
<th>Area</th>
<th>Cash</th>
<th>Credit</th>
<th>Cheq</th>
<th>Total-Sales</th>
<th>Dis</th>
<th>Free</th>
<th>PC</th>
<th>Cash-Col</th>
<th>Chq-Col</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 distinct agent_names,count(distinct(invoice_number)) as wdys,sum(cashs) as cash,sum(credits) as credit,sum(cheques) as cheq,sum(chqcollect) as chqcol,sum(cashcollect) as cashcol,sum(pcalls) as pcl,sum(pcs_qty*pcs_price)/100*dis_rates as dis,sum(free_qtys*pcs_price) as freevl,territory_names from dbrdsales ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by agent_names";

$qry=mysqli_query($con,$sqlst);
while($rst=mysqli_fetch_row($qry)){

?> 
<tr>
<td><?php echo $rst[1]; ?></td>
<td><?php echo $rst[0]; ?></td>
<td><?php echo $rst[10]; ?></td>
<td align='center'><?php echo number_format($rst[2],2,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[3],2,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[4],2,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[2]+$rst[3]+$rst[4],2,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[8],0,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[9],0,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[7],0,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[5],2,'.',','); ?></td>
<td align='center'><?php echo number_format($rst[6],2,'.',','); ?></td>
</tr>
<?php } ?>
</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("example1");

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
    })
    
    </script>
</body>
</html>

