<?php
//include_once('include.php');
include_once('racss.php');
include_once('head.php');
include_once('starter.php'); 
//include_once('sidebar.php');
include_once('alert.php');
include_once('dateformat.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);

$conn=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."' ";}

?>
<!doctype html>

<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<ol class="breadcrumb">
<li><a href="#"><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">Distributor Outstanding Report</h3>
</div>

<!-- /.box-header -->    
<form method='post' class="form-inline" action='ledgerdetails.php?'>
<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>
 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>

<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 class="thead-dark">
<th class="text-center">Distributor</th>
<th class="text-center">Region</th>
<th class="text-center">Territory</th>
<th class="text-center">Due</th>
<th class="text-center">Details</th>
</thead>
<tbody>

<?php
$result="select distinct agent_names,regions,territory_names,sum(tot_amounts) from st_paymode ".$flt." and dates>='2019-03-31' and dates<='".$ends."' and pay_types<>'Security' and pay_types<>'Open Balance' and pay_types<>'Claim Hold' group by agent_names ";

$totdue=0;
$qrym=mysqli_query($conn,$result);
while($rst=mysqli_fetch_row($qrym)){
$totdue=$totdue+$rst[3];
?> 
<tr>
<td><?php echo $rst[0]; ?></td>
<td><?php echo $rst[1]; ?></td>
<td><?php echo $rst[2]; ?></td>
<td align='right'><?php echo number_format($rst[3],2,'.',','); ?></td>
<td>
<a href='#?supname=agents=<?php echo $rst[0];?>&start=$rst[0]&end=$rst[0]'class='btn btn-info btn-xs' >Ledger</a>
<a href='#?supname=$rst[0]&start=$rst[0]&end=$rst[0]'class='btn btn-warning btn-xs' >Chq Realiz</a>
<a href='#modalchqbnk' onClick='showModal(this)' class='btn btn-danger btn-xs' data-toggle='modal'>Rtn Chq</a>
</td>
</tr>
<?php } ?>
<td class="success">Total</td><td class="success"></td><td class="success"></td><td align='right' class='success'><?php echo number_format($totdue,2,'.',','); ?></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 myFunction() {
var $rows = $('#example tr');
$('#myInput').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
    
    $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).hide();
});
}
  
 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 c_color(){
    if (document.getElementById('ClColor').value >'5000') {
        document.getElementById('ClColor').style.color = "white";
        document.getElementById('ClColor').style.background="red";
    }
}    
    
    </script>
</body>
</html>

