<?php 
if(isset($_POST["cname"]))
{
 $cc=0;
  $tPrice=0;     
  $ttprice=0;
  $tptd=0;
  $tbal=0;
  $cname=$_POST["cname"];
  require_once "labClass.php";
 $con = new labClass();
  ?>
 <div class="container-fluid " id="dpl">
  <?php
 $chn= $con->getThisCusDBOrder($cname); 
if ($chn->rowCount()>0) {
  $cnm=$chn->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 ($cnm as $key => $cn) {
        $orderID=$cn['orderID'];
    $chkp= $con->getThisOrder($orderID); 
if ($chkp->rowCount()>0) {
  $chp=$chkp->fetchAll();
    foreach ($chp as $key => $p) {
     
    $cc++;  
        ?>
 <tbody>
     
 <tr>
    <!-- Column 1 -->
    <td>
        <?php echo $p['orderdate']; ?>
    </td>

    <!-- Column 2 -->
    <td>
        <?php 
        echo $p['orderID']."."; 
        $parts = explode('(', $p['cname']); 
        echo $parts[0];
        ?>
    </td>

    <!-- Column 3 -->
    <td align="right">
        <b>Payable:</b><br/>
        <?php 
        echo number_format($p['payableA'],2);
        $ttprice += $p['payableA'];
        $tptd += $p['paidtodate'];
        $tbal += $p['balance'];
        ?> 
    </td>

    <!-- Column 4 -->
    <td align="right">
        <b>Paid:</b><br/>
        <?php echo number_format($p['paidtodate'],2); ?> 
    </td>

    <!-- Column 5 -->
    <td align="right">
        <b>Balance:</b><br/>
        <?php echo number_format($p['balance'],2); ?> 
    </td>

    <!-- Last Column -->
    <td>
         <b>Status:</b><br/>
        <?php echo $p['pstatus']; ?> 
    </td>
</tr>

 
     <tr>
    <td colspan="2">  
    </td>
     <td colspan="3">
       <table class="table">    
        <th>  
           <small><h6>Item name</h6></small>
         </th>
         <th>  
           <small><h6>Price</h6></small>
          </th>
          <th>  
            <small><h6>Qty</h6></small>
           </th>
           <th>  
             <small><h6>Total</h6></small>
          </th>


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

    $ch=$cp->fetchAll();
     
    foreach ($ch as $key => $c) {
      ?>
         <tr>
            <td>  
            <?php echo $c['iname'];?>
            </td> 
            <td align="right">
            <?php echo number_format($c['isprice'],2);?>
            </td> 
            <td align="right"> 
               <?php echo $c['eQty'];?>    
            </td>
            <td align="right">
              <?php echo number_format($c['tprice'],2);?>  
            </td>
              
         </tr>
         
 
       <?php
       $tPrice=$tPrice+$c['isprice']*$c['eQty'];
        } 
        ?>

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

<small>PAYMENT DETAIL:</small>
<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; 
      }
      else{


     ?>
     <tr>
            <td colspan="5">
              No payment recorded!..  
            </td>
             
        </tr>
<?php
} 
} 
} 
?>

</table>
</td>
<td>  
</td>
</tr>
<?php 
$tPrice=0;      
}
$cc=0; 
?>
<tr><td  align="right">Total:</td><td><?php echo number_format($ttprice,2);?></td> <td align="right">
    <?php echo number_format( $tptd,2);?> 
    </td>
    <td align="right">
    <?php echo number_format($tbal,2);?> 
    </td>
     
    </tr> </tr>
</tbody>
</table> 

<?php
}
}
else{
    echo "No matching payment!..";
}
?>
</div>
 <button type="submit" class="btn btn-primary" value="Print" style="width:13%; height:40px;" onclick="printPageArea();">Print </button>
 <script>
  function printPageArea(){
    var printContent = document.getElementById('co').innerHTML;
    var originalContent = document.body.innerHTML;
    document.body.innerHTML = printContent;
    window.print();
    document.body.innerHTML = originalContent;
}
</script>
 