<?php 
if(isset($_POST["cname"]) &&isset($_POST["dat1"]) && isset($_POST["dat2"]))
{  
 $cc=0;
 $tpayb=0;     
 $tptd=0;
 $tbal=0;
 
  $text =  $_POST['cname'];

if (preg_match('/\b0\d{9}\b/', $text, $matches)) {
    $cname= $matches[0];
}
 

  $dat1= $_POST['dat1'];
  $dat2= $_POST['dat2']; 
 
 $tPrice=0;  
require_once "labClass.php";
 $con = new labClass();
  ?>
 

 <div class="container-fluid " id="dpl">
  <?php
  if ($cname=="")
  {    
    $chkp= $con->getComPDebitsRecBdt($dat1,$dat2);
    
  }
 else if($cname!="" && (($dat1!=$dat2) || ($dat1==$dat2)) )  
  {
    
  $chkp= $con->getComPDebitsRecBcCdt($cname,$dat1,$dat2);
   
  } 
  else{
  $chkp= $con->getComPDebitsRecBcCNn($cname);
  }
  //echo $cname;   
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
   $disc=0;
    foreach ($chp as $key => $p) {
    $orderID=$p['orderID'];

    $c= $con->getThisPOrder($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->getCDebitsPayListsBID($orderID,$dat1,$dat2);
   //print_r($ch);
    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 if($r['pOpt']=="DISCOUNT"){
                  $disc=$disc+$r['paidAmount'];
              }
              
              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">Cash paid : <?php echo number_format($tptd-$disc,2);?><p/>Discount :<?php echo number_format($disc,2);?></td><td align="right"><?php echo number_format($tbal,2);?></td></tr>
</tbody>
</table> 

<?php
}
else{
    echo "No matching payment!..";
}
}
else{
    echo "All field required!..";
}
?>
</div>
 