<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['hrm_id'])):
header('Location:../index.php');
endif;

$edate=date("Y-m-d");
//////////////// Get Sequence Number ----------------------- 

$fdate=$edate;
$tdate=$edate;
$cus_id=0;
$report=0;

if(isset($_POST['SubmitButton'])){ //check if form was submitted
  $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  $cus_id = $_POST['cus_id'];
  $report=1;
}

if(isset($_POST['SubmitButton1'])){ //check if form was submitted
   $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  $cus_id = $_POST['cus_id'];
  $report=2;
}

//////////////// Get Sequence Number ----------------------- 
 $querydoc=mysqli_query($con,"select * from customer_master where cus_id=$cus_id ")or die(mysqli_error());

    while($row=mysqli_fetch_array($querydoc)){ 
    $cus_name=$row['cus_name'];  
    }

//////////////////End of gettinh sequence Number ------------------



//////////////////End of gettinh sequence Number ------------------
?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>SalesHub |Customer Outstanding </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>



</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
        <div class="content-wrapper">

          <div class="page-header">
            <h3 class="page-title">
                Settlements 
            </h3>
          
          </div>
          <div class="col-22 grid-margin">
              <div class="card">
                <div class="card-body">
              
                          
    <form  method="post" >                   
                    <div class="row">                     
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Customer: </label>
                          <div class="col-sm-9">
                             <select class="form-control select2" style="width: 100%;" name="cus_id" id="cus_id" required >
           <option value="0">All</option>  
          <?php            
         $querydoc=mysqli_query($con,"select *,CONCAT(`customer_master`.`cus_name`,', ',`customer_master`.`cus_code`) AS `cus`
          
          from customer_master  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['cus_id'];?>"><?php echo $rowd['cus'];?></option>        
          <?php }?>
          </select> 
                          </div>
                        </div>


                      </div>
                                              
                  <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">From Date: </label>
                          <div class="col-sm-8">
                            <input type="date" class="form-control" id="fdate" name="fdate" >

                          </div>
                        </div>



                    </div> <!-------End of first form raw------->

</div>
<div class="row">                     
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">To: </label>
                          <div class="col-sm-9">
                               <input type="date" class="form-control" id="tdate" name="tdate" >
                          </div>
                        </div>


                      </div>
                    </div>
<table>
<tr>
                 <td>  <button type="submit"  name="SubmitButton"class="btn btn-primary">View</button></td>
                  <td>  <button type="submit"  name="SubmitButton1"class="btn btn-primary">Age Analysis</button></td>
                 <td>  <button class="btn btn-light">Cancel</button></td>
</tr>
</table>
                  </form>
                </div>
              </div>
            </div>

  <div class="content-wrapper">
          <div class="col-lg-22 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <h6 > </h6>
                  <p class="card-description">
                    <button type="button" onclick="document.location='home.php'" class="btn btn-warning "><i class="fas fa-arrow-left"></i> Go Back                    
                    </button>
                  </p>
                  <p>Settlement Detail  : <?php echo $cus_name;  ?></p>
                  <div class="table-responsive">
                    <table class="table table-hover">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Document</th>
                          <th>Date</th>
                          <th>Type</th>
                        
                          <th class="text-right">Amount</th>
                          <th class="text-right">Apply Document</th>
                          <th class="text-right">Type</th>
                          <th class="text-right">Apply Amount</th>
                          <th class="text-right">Doc Date</th>                         
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;

$stm="SELECT seheader.docid,seheader.docdate,seheader.doctype,seheader.amount,
 sedetail.docid AS appdoc,sedetail.applyamount,sh.docdate AS refdocdate,
 sh.amount AS appdocamt,sh.doctype AS appdoctype
FROM seheader
LEFT JOIN sedetail ON sedetail.refdocno=seheader.docid AND 
sedetail.party_id=seheader.party_id
LEFT JOIN seheader AS sh  ON sh.docid=sedetail.docid
WHERE seheader.docdate BETWEEN '$fdate' AND '$tdate' AND seheader.amount>0 AND sh.cancel<>1 AND
seheader.cancel<>1 AND seheader.party_id= $cus_id order by seheader.docid";


$previousDocid = null;

$query=mysqli_query($con,$stm)or die(mysqli_error()); 

 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
                $bal=0;
                $excess=0;
                $docid=$row['docid'];
                $docdate=$row['docdate']; 
                $doctype=$row['doctype'];
                $amount=$row['amount'];
                $appdoc=$row['appdoc'];
                $applyamount=$row['applyamount'];
                $refdocdate=$row['refdocdate'];
                $appdocamt=$row['appdocamt'];
                $appdoctype=$row['appdoctype'];
               // if ($rcount==1) {
                 // $previousDocid=$docid;

                //}
          

              //$Final_outstandingTotal=$Final_outstandingTotal + $bal;
               
         ?>
            

             
   <tr>
         
         <?php If ( $previousDocid==$docid) { ?>


            <td></td>
                         <td></td>
                         <td></td>
                           <td></td>          
                        
                         <td align="right"></td>                         
                          <td align="right"><?php echo $appdoc;?></td>
                           <td align="right"><?php echo $appdoctype;?></td>
                          
                           <td align="right"><?php echo $applyamount;?></td>
                           <td align="right"><?php echo date("Y-m-d",strtotime( $row['refdocdate']));?></td>

       <?php   } else {  ?>               



                         <td><?php echo $rcount;  ?></td>
                         <td><a href="https://invox.lk/salessys/pages/invoice_duplicate.php?inv_no=<?php echo $docid; ?>" onclick="window.open(this.href, '_blank', 'width=800, height=600'); return false;"><?php echo $row['docid']; ?></a></td>
                         <td><?php echo  date("Y-m-d",strtotime( $row['docdate'])) ;?></td>
                           <td><?php echo $row['doctype'];?></td>          
                        
                         <td align="right"><?php echo number_format($row['amount'],2);?></td>                         
                          <td align="right"><?php echo $appdoc;?></td>
                           <td align="right"><?php echo $appdoctype;?></td>
                           <td align="right"><?php echo number_format($applyamount,);?></td>
                           <td align="right"><?php echo date("Y-m-d",strtotime( $row['refdocdate']));?></td>
              
                        <?php  

                          if ($rcount==1) {
                  $previousDocid=$docid;

                }
                 if ($row['docid'] != $previousDocid) {   
                    $previousDocid=$docid;
                 }


                         } ?> 

</tr>


 <?php }   

                 ?>

                      </tbody>
                    </table>



        </div>  <!----- <div class="content-wrapper">------>
        </div>
          
          
  </div>

 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
</div>
  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });



</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })

</script>



