<?php
if(isset($_POST["cname"]))
{  
 $cc=0;
 $tpayb=0;     
 $tptd=0;
 $tbal=0;
 $tpayb1=0;     
 $tptd1=0;
 $tbal1=0;
 
$c1=0;
$c2=0;
 $parts =explode(':', $_POST['cname']);  
 $cname=$parts[1];  
  $status="NOT PAID";
  require_once "labClass.php";
 $con = new labClass();
  ?> 
 <div class="container-fluid" id="dpl">
  <?php
  $chkp= $con->getThisCusDebit($cname,$status);  
  if ($chkp->rowCount()>0) {
  $chp=$chkp->fetchAll();
  ?>
  <?php echo $_POST['cname'];?> DEBIT TO ROMBO STAR:
  <table class="table table-bordered" border="1"> 
   <thead class="bg-secondary" >
      <th align="center">  
        <small><h6>DATE</h6></small>
      </th>
      <th align="center">  
        <small><h6>INV. NO</h6></small>
      </th>
      <th align="center">  
        <small><h6>TOTAL PAYABLE</h6></small>
      </th>
      <th align="center">  
        <small><h6>PAID TO DATE</h6></small>
      </th>
      <th align="center">  
        <small><h6>BALANCE</h6></small>
      </th>
        
    </thead>
   <?php
    foreach ($chp as $key => $p) {
        
   $c1++; 
    $orderID=$p['orderID'];
    $cp= $con->getThisInvItemList($orderID);
    $cc++; 
    $nr=$cp->rowCount();
    ?>
 <tbody>
 <tr>
 <td align="center">
    <?php echo $p['orderdate'];?> 
    </td>
     <td align="center">  
     <a href="#" onclick="var orno=document.getElementById('ono<?php echo $c1;?>').innerHTML; $.post('pharmInvDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="ono<?php echo $c1;?>"><?php echo $p['orderID'];?></sup></a>
     </td>
     <td align="center">
    <?php echo number_format($p['payableA'],2);?> 
    </td>
    <td align="center">
    <?php echo number_format($p['paidtodate'],2);?> 
    </td>
    <td align="center">
         
        <?php echo number_format($p['balance'],2);?> 
    </td>
       
    </tr>   
<?php
$tbal=$tbal+$p['balance'];
$tpayb=$tpayb+$p['payableA'];
$tptd=$tptd+$p['paidtodate'];      
}
$cc=0; 
?>
<tr><td></td><td align="right">Total:</td><td align="center"><?php echo number_format($tpayb,2);?></td><td align="center"><?php echo number_format($tptd,2);?></td><td align="center"><?php echo number_format($tbal,2);?></td></tr>
</tbody>
</table> 

<?php
}
else{
    ?>
<h6 class="text-danger"><?php echo $_POST['cname'];?> has no debit to company!..</h6>
<?php
}
?>

<!---CUSTOMER DEBIT ENDS AND COMPANY DEBIT BEGIN-->

<?php
  $chkp= $con->getThisCmpnyDebit($cname,$status);;  
  if ($chkp->rowCount()>0) {
  $chp=$chkp->fetchAll();
  ?>
  ROMBO STAR DEBIT TO 
  <?php echo $_POST['cname'];?> :
  <table class="table table-bordered" border="1"> 
   <thead class="bg-secondary" >
      <th align="center">  
        <small><h6>DATE</h6></small>
      </th>
      <th align="center">  
        <small><h6>INV. NO</h6></small>
      </th>
      <th align="center">  
        <small><h6>TOTAL PAYABLE</h6></small>
      </th>
      <th align="center">  
        <small><h6>PAID TO DATE</h6></small>
      </th>
      <th align="center">  
        <small><h6>BALANCE</h6></small>
      </th>
        
    </thead>
   <?php
    foreach ($chp as $key => $p) {
    $orderID=$p['orderID'];
    $cp= $con->getThisInvItemList($orderID);
    $c2++; 
    $nr=$cp->rowCount();
    ?>
 <tbody>
 <tr>
 <td align="center">
    <?php echo $p['orderdate'];?> 
    </td>
     <td align="center">  
     <a href="#" onclick="var orno=document.getElementById('orn<?php echo $c2;?>').innerHTML; $.post('pharmCmpnyDbtPayDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orn<?php echo $c2;?>"><?php echo $p['orderID'];?></sup></a> 
     </td>
     <td align="center">
    <?php echo number_format($p['payableA'],2);?> 
    </td>
    <td align="center">
    <?php echo number_format($p['paidtodate'],2);?> 
    </td>
    <td align="center">
         
        <?php echo number_format($p['balance'],2);?>  
    </td>
       
    </tr>   
<?php
$tbal1=$tbal1+$p['balance'];
$tpayb1=$tpayb1+$p['payableA'];
$tptd1=$tptd1+$p['paidtodate'];      
}
$cc=0; 
?>
<tr><td></td><td align="right">Total:</td><td align="center"><?php echo number_format($tpayb1,2);?></td><td align="center"><?php echo number_format($tptd1,2);?></td><td align="center"><?php echo number_format($tbal1,2);?></td></tr>
</tbody>
</table> 

<?php
}
else{
    ?>
<h6 class="text-danger">Company has no debit to <?php echo $_POST['cname'];?>!..</h6>
<?php
}

?>

<!---COMPANY DEBIT END-->
SUMMARY OF BALANCE:
 
  <table class="table table-bordered" border="1"> 
   <thead class="bg-secondary" >
      <th>  
        <small><h6>ROMBO STAR BALANCE</h6></small>
      </th>
      <th>  
        <small><h6><?php echo $_POST['cname'];?> BALANCE </h6></small>
      </th>
       
    </thead>
   
 <tbody>
 <tr>
 <td>
    <?php echo number_format($tbal1,2);?> 
    </td>
     <td>  
      <?php echo number_format($tbal,2);?>
     </td>
</tr>   
 
<tr><td colspan="2"> 
 <?php
if($tbal>$tbal1)
{

  echo "The company has to receive: ".number_format($tbal1-$tbal,2)." from ". $_POST['cname'];
}

 if ($tbal1>$tbal) {
  echo "The company has to pay: ". number_format($tbal-$tbal1,2)." to ". $_POST['cname'];
}?>
  
</td></tr>
</tbody>
</table>

<table class="table" border="1"> 
    
    <sup id="rdn"></sup>
 
<tr><td colspan="2" align="right"> 
  <input type="hidden" name="cn" id="cn" value="<?php echo  $_POST['cname'];?>">
  <input type="hidden" name="tbal" id="tbal" value="<?php echo $tbal;?>">

   <input type="hidden" name="tbal1" id="tbal1" value="<?php echo $tbal1;?>">

<!--alert(sdate+':'+sdate+',tbal:'+tbal+',tbal1:'+tbal1+',cname:'+cname+',user:'+user);-->
 
  <!--<button type="submit" class="btn btn-success" 
  style="width:100%; height:40px;" 

  name="postUpdate" id="postUpdate"

  value="Update" id="submit" 

  onclick="var sdate=$('#sdate').val();var cname=$('#cn').val(); var user=$('#user').val();  var tbal =$('#tbal').val();var tbal1 =$('#tbal1').val(); if(tbal!=''  && tbal1!=''&& cname!=''  && user!='' ) { $.post('pharmPayReconcilPro.php',{sdate:sdate,tbal:tbal,tbal1:tbal1,cname:cname,user:user},function(data){$('#disp').html(data).show();}); } else{
    document.getElementById('rdn').innerHTML='Valide item detail required!..';}" > 
  <small>Reconciliate</small>
</button>-->
 
      
     
  
</td></tr>
 
</table>

</div>
<?php
}
else{
    ?>
<h6 class="text-danger">All field required!..</h6>
<?php
}
?>