<?php 
  $cc=0;
  $rc=0;
  $tPrice=0;     
  $ttprice=0;
  $ttInv=0;
  $user=$_POST['user'];
  require_once "labClass.php";
  
 $con = new labClass();
  ?>
  <div class="container-fluid" id="co"> 
  <small><h6>COMPANY DEBIT DETAIL REPORT:</h6></small>
  <table class="table">
    <tr>
      <td colspan="4">
        <sup id="tr"></sup>
      <label for="cname" class="form-label">Choose customer from the list:</label>
<input class="form-control" list="cnames" name="cname" id="cname" onchange="processData();">
<datalist id="cnames">
    <?php
     $Q= $con->getporderListBcN();
     if ($Q->rowCount()>0) {
     $lb=
     $Q->fetchAll();
     foreach ($lb as $key => $l) {
      
      ?>
      <option value="<?php echo  $l["cname"];?>">  
             <?php
            
           }
           }
 
      ?>
      </datalist>

    </td>
    <!-- <td style="width:15%;">Sort by un-received:
     <select  class="form-control" id="unrecv" name="unrecv" onchange="processD();"> 
     <option value=""></option>
     <option value="Un-confirmed">Un-confirmed</option> 
     </select>
          
  </td>-->
  </tr>
 <tr>
  <td style="width:15%;">Sort by 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 sdate=$('#sdate').val(); var dat1=$('#dat1').val(); var dat2=$('#dat2').val();  $.post('purchaseOrderRBdt.php',{sdate:sdate,dat1:dat1,dat2:dat2},function(data){$('#dpl').html(data).show();});">Generate</button>
 </td>      
 </tr>
 </table>

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

  $chp=$chkp->fetchAll();
  ?>
<table class="table table-bordered" border="1"> 
   <thead class="bg-secondary" >
       
      <th>  
        <small><h6>ORDER DATE</h6></small>
      </th>
      <th>  
        <small><h6>ORDER NO/C.NAME</h6></small>
      </th>
       
      <th>  
        <small><h6>PATICULAR</h6></small>
      </th>
      
      <th>  
        <small><h6>QTY</h6></small>
      </th>
      <th>  
        <small><h6>CONFIRM RECV QTY</h6></small>
      </th>
       
       <th>  
        <small><h6>ORDER VALUE</h6></small>
      </th>
      <th>  
        <small><h6>PAID TO DATE</h6></small>
      </th>
      <th>  
        <small><h6>BALANCE</h6></small>
      </th>
      
    </thead>
   <?php
    foreach ($chp as $key => $p) {
    $orderID=$p['orderID'];

    //
    $cc++; 
    $cp= $con->getThispOrderItemList($orderID);
    $nr=$cp->rowCount();


        ?>
 <tbody>
 <tr>
    <td rowspan="<?php echo $nr+2; ?>">
    <?php echo $p['orderdate'];?> 
    </td>
 
     <td rowspan="<?php echo $nr+2; ?>" >  
     <sup id="orno<?php echo $cc; ?>"><?php echo $p['orderID'];?></sup>.
     <?php echo $p['cname'];?>
     </td>

    <td colspan="3" >
    </td>
   <td rowspan="<?php echo $nr+2; ?>">
      
    
    <?php echo $p['payableA'];?>
      
    
    
    </td>
    <td rowspan="<?php echo $nr+2; ?>">
        <?php echo $p['paidtodate'];?>
    </td>
     
    <td rowspan="<?php echo $nr+2; ?>"> 
   
    <?php 
    echo $p['balance'] 
       
    ?>
    </td>
    </tr>    
     <?php 
   
    if ($cp->rowCount()>0) {

    $ch=$cp->fetchAll();
     
      foreach ($ch as $key => $c) {
         $rc++;
      ?>
          <tr>
            <td>
                 
            <sup id="in<?php echo $rc;?>"><?php echo $c['iname'];?></sup>
            </td> 
            
            <td> 
               <?php echo $c['eQty'];?><br/> 
                 <?php if ($c['confrecv']!=NULL) 
                { 
                
                $text=$c['confrecv'];
                if (preg_match('/Qty:\((\d+)\)/', $text, $matches)) 
                {
                 $quantity = (int) $matches[1];
                 if ($c['eQty']>$quantity)
                 {
                     echo "rcv:".$quantity;
                 }
                }
                }
                ?>
            </td>

            <td>
              </td>
              
                   </tr>
                   <?php 
                       } 
                       
                    }       
}
$cc=0; 
?>
 
</tbody>
</table> 

<?php

}
?>
 </div>
 <script type="text/javascript">
   function processData(){ 
var ecc=0; 
var cname =document.getElementById('cname').value; 
$.trim(cname);
 
if(cname==""){ 

         ecc++;
         document.getElementById('tr').innerHTML="Item customer is required!..";

             }
else{
document.getElementById('tr').innerHTML="";
    }
    
    if(ecc==0)
    { 
        $.post("purchOrderRBCN.php",
          {cname:cname},
          function(data){
      $('#dpl').html(data).show();

    });
    }
 
}

 

 </script>