<?php 
 $cc=0;
 $tpayb=0;     
 $tptd=0;
 $tbal=0;
 $tbal=0; 
 $tPrice=0;  
require_once "labClass.php";
 $con = new labClass();
  ?> 
 
 
<table class="table">
    <tr><td colspan="6" align="center"><small>REPORTS OF DEBITS AND PAYMENT RECORDS:</small></td></tr>
<tr><td colspan="2" align="right">
     <label for="cname" class="form-label">Customer name:</label> 
     
</td>
<td colspan="3"> 
      
<input class="form-control" list="cnames" name="cname" id="cname" onchange="processData();">
<datalist id="cnames">
    <?php
     $Q= $con->getOrderPRecListBcN();
     if ($Q->rowCount()>0) {
     $lb=$Q->fetchAll();
     foreach ($lb as $key => $l) {
      
      ?>
      <option value="<?php echo  $l["cname"];?>">  
             <?php
            
           }
           }
 
      ?>
      </datalist>

    </td>
  </tr>
  <tr>
  <td style="width:15%;">Date range:</td><td style="width:10%;" ><input type="date" style="width:200px;" class="form-control" id="dat1" name="dat1"  value="<?php echo Date("Y-m-d");?>" >
  </td>
  <td>and:</td><td style="width:10%;">
          <input type="date" style="width:200px;" class="form-control" id="dat2" name="dat2" value="<?php echo Date("Y-m-d");?>">
  </td><td style="width:10%;">  
  <button type="submit" class="btn btn-primary" style="width:100%; height:40px;" onclick="var cname=$('#cname').val(); var dat1=$('#dat1').val(); var dat2=$('#dat2').val();  $.post('pharmCusPrecBCdt.php',{dat1:dat1,dat2:dat2,cname:cname},function(data){$('#dpl').html(data).show();});">Generate</button>
 </td>      
 </tr>
 </table>

 <div class="container-fluid " id="dpl">
  <?php
 $chkp= $con->getDebitsPrec(); 
     
if ($chkp->rowCount()>0) {

  $chp=$chkp->fetchAll();
  ?>
<table class="table" border="1"> 
   <thead class="bg-secondary" >
       
      <th>  
        <small><h6>DATE</h6></small>
      </th>
      <th>  
        <small><h6>INV. NO/C'S NAME</h6></small>
      </th>
       
      <th>  
        <small><h6>TOTAL PAYABLE</h6></small>
      </th>
      <th>  
        <small><h6>PAID TO DATE</h6></small>
      </th>
      <th>  
        <small><h6>BALANCE TO DATE</h6></small>
      </th>
       <th>  
        <small><h6>PAY STATUS</h6></small>
      </th>
      
    </thead>
   <?php
    foreach ($chp as $key => $p) {
    $orderID=$p['orderID'];

    $c= $con->getThisOrder($orderID)->fetch();
    $cc++; 
        ?>
 <tbody>
 <tr>
   
 <td>
    <?php echo $c['orderdate'];?> 
    </td>
     <td>  
     <?php echo $c['orderID']."."; $parts = explode('(', $c['cname']); echo $parts[0];?> 
     </td>
     <td align="right">
    <?php echo number_format($c['payableA'],2);?> 
    </td>
    <td align="right">
    <?php echo number_format($c['paidtodate'],2);?> 
    </td>
    <td align="right">
    <?php echo number_format($c['balance'],2);?> 
    </td>
      <td align="right">
    <?php echo $c['pstatus'];?> 
    </td>
    </tr> 
     <tr>
    <td colspan="3">  
    </td>
     <td colspan="3">
       <table class="table">    
        <th>  
           <small><h6>Date paid</h6></small>
         </th>
         <th>  
           <small><h6>paid amount</h6></small>
          </th>
          <th>  
            <small><h6>Aproved by</h6></small>
           </th>
           <th>  
             <small><h6>Means of pay</h6></small>
          </th>


   <?php 
   $ch=$con->getDebitsPayLists($orderID);
    if ($ch->rowCount()>0) {

    $h=$ch->fetchAll();
     
    foreach ($h as $key => $r) {
      ?>
         <tr>
            <td>  
            <?php echo $r['datePaid'];?>
            </td> 
            <td align="right">
            <?php echo number_format($r['paidAmount'],2);?>
            </td> 
            <td align="right"> 
               <?php echo $r['timeP'];?>    
            </td>
            <td align="right">
              <?php echo $r['pOpt'];?>  
            </td>
              
         </tr>
         
 
       <?php
       $tPrice=$tPrice+$r['paidAmount'];
        } 
        ?>

         <tr>
            <td  align="right">
              Sub-total:  
            </td>
            <td align="right">
               <?php echo number_format($tPrice,2);?>  
            </td>
            <td align="right">  
            </td>
            <td align="right">   
            </td>
            <td>   
            </td>
        </tr>
 
     <?php
     $tPrice=0; 
      } 
     ?>




</table>

</td>
 
</tr>
 
          
     
    

<?php

$tbal=$tbal+$c['balance'];
$tpayb=$tpayb+$c['payableA'];
$tptd=$tptd+$c['paidtodate'];       
}
$cc=0; 
?>
<tr><td colspan="2" align="right">Total:</td><td align="right"><?php echo number_format($tpayb,2);?></td><td align="right"><?php echo number_format($tptd,2);?></td><td align="right"><?php echo number_format($tbal,2);?></td></tr>
</tbody>
</table> 

<?php
}
?>
</div>
<script type="text/javascript">
 

function processData(){ 
var ecc=0; 
 
var cname= document.getElementById('cname').value;
$.trim(cname);  
var dat1 =document.getElementById('dat1').value; 
var dat2 =document.getElementById('dat2').value;  
  //FIRST NAME VALIDATION
    alert(cname);
 if(cname==""){ 

         ecc++;
         alert('Customer name required!..');

             }
 
  if(dat1==""){ 

         ecc++;
         alert('Date range is required!..');

             }
 
if(dat2==""){ 

         ecc++;
         alert('Date range is required!..');

             }
 
    if(ecc==0)
    { 
        $.post("pharmCusPrecBCdt.php",
          {cname:cname,dat1:dat1,dat2:dat2},
          function(data){
      $('#dpl').html(data).show();

    });
    }
 
} 

 </script>