<?php
if( isset($_POST["tbal"]) && isset($_POST["cname"]) && isset($_POST["user"]) && isset($_POST["tbal1"]) && isset($_POST["tbal1"]) &&    isset($_POST["sdate"]))
{ 
////
    $count=0;
    $status="NOT PAID";
    $tbal= $_POST['tbal'];
    $cname= $_POST['cname'];  
    $tbal1= $_POST['tbal1']; 
    $user= $_POST['user'];
    $sdate= $_POST['sdate'];
    $pOpt="Reconciliation"; 
    $pst="PAID";
    $cdbt=$tbal;
    $spdbt=$tbal1;
  //CHECKING ENDS HER
    require_once"labClass.php";
    $con=new labClass();
if ($tbal1 > $tbal) {
   
     $excess=$tbal1 - $tbal;

     ////START PAYING CUSTOMER DEBITS
     $chkp= $con->getThisCusDebit($cname,$status);  
     if ($chkp->rowCount()>0) {
     $chp=$chkp->fetchAll();
     foreach ($chp as $key => $p) {
      $orderID=$p['orderID'];
      $p= $con->getThisOrder($orderID)->fetch();
      

      if($spdbt>=$p["balance"]){
      $spdbt=$spdbt-$p["balance"];
      $ptd=$p["paidtodate"]+$p["balance"]; 
      $cus=$p["cname"];
      $cash=$p["balance"];
      $cdbt=$cdbt-$p["balance"];
      $nbal=0;
      }else{
      $ptd=$p["paidtodate"]+$spdbt; 
      $cus=$p["cname"];
      $cash=$spdbt;
      $nbal=$p["balance"]-$spdbt;
      }
            
      if($cdbt >=0) 
       { 
       $rQc= $con->updateOrder($orderID,$nbal,$ptd,$pst);

       if($rQc->rowCount()>0)
        {
        $rc= $con->debitPRec($orderID,$cus,$cash,$sdate,$user,$pOpt);
        if($rc->rowCount()>0)
        {
         $count++;
         }
       
        }
   
       }
   
    }
   
  }
 
  ///////////////SUPPLIER DEBIT PAYMENTS////

    $obal=0;
    $cdbt=0;
    $chkp= $con->getThisCmpnyDebit($cname,$status);  
     if ($chkp->rowCount()>0) {
     $chp=$chkp->fetchAll();
     foreach ($chp as $key => $p) {
      $orderID=$p['orderID'];
      $p= $con->getThisPOrder($orderID)->fetch();
      $ptd=$p["paidtodate"]+$p["balance"];
      $obal=$obal+$p["balance"];
      $cus=$p["cname"];
      $cash=$p["balance"];
      $cdbt=$cdbt-$p["balance"];
      $nbal=0;       
      if($cdbt >=0) 
       { 
       $rQc= $con->updatePOrder($orderID,$nbal,$ptd,$pst);
       if($rQc->rowCount()>0)
        {
        $rc= $con->cPdebitPRec($orderID,$cus,$cash,$sdate,$user,$pOpt);
        if($rc->rowCount()>0)
        {
         $count++;
         }
       
        }
   
       }
   
    }
     }
   }

///////////////SUPPLIER DEBIT PAYMENTS END////

   
    

    /////////SUPPLY DEBIT IS GREATER THAN////////////////
    
if ($tbal > $tbal1) {
   
     $excess=$tbal1-$tbal;

     ////START PAYING CUSTOMER DEBITS
     $chkp= $con->getThisCmpnyDebit($cname,$status);  
     if ($chkp->rowCount()>0) {
     $chp=$chkp->fetchAll();
     foreach ($chp as $key => $p) {
      $orderID=$p['orderID'];
      $p= $con->getThisPOrder($orderID)->fetch();
      

      if($spdbt>=$p["balance"]){
      $spdbt=$spdbt-$p["balance"];
      $ptd=$p["paidtodate"]+$p["balance"]; 
      $cus=$p["cname"];
      $cash=$p["balance"];
      $cdbt=$cdbt-$p["balance"];
      $nbal=0;
      }else{
      $ptd=$p["paidtodate"]+$spdbt; 
      $cus=$p["cname"];
      $cash=$spdbt;
      $nbal=$p["balance"]-$spdbt;
      }
            
      if($cdbt >=0) 
       { 
       $rQc= $con->updatePOrder($orderID,$nbal,$ptd,$pst);
       if($rQc->rowCount()>0)
        {
        $rc= $con->cPdebitPRec($orderID,$cus,$cash,$sdate,$user,$pOpt);
        if($rc->rowCount()>0)
        {
         $count++;
         }
       
        }
   
       }
   
    }
  
  ///////////////SUPPLIER DEBIT PAYMENTS////

    $obal=0;
    $cdbt=$tbal1;
    $chkp= $con->getThisCusDebit($cname,$status);  
     if ($chkp->rowCount()>0) {
     $chp=$chkp->fetchAll();
     foreach ($chp as $key => $p) {
      $orderID=$p['orderID'];
      $p= $con->getThisOrder($orderID)->fetch();
      $ptd=$p["paidtodate"]+$p["balance"];
      $obal=$obal+$p["balance"];
      $cus=$p["cname"];
      $cash=$p["balance"];
      $cdbt=$cdbt-$p["balance"];
      $nbal=0;       
      if($cdbt >=0) 
       { 
       $rQc= $con->updateOrder($orderID,$nbal,$ptd,$pst);
       if($rQc->rowCount()>0)
        {
        $rc= $con->debitPRec($orderID,$cus,$cash,$sdate,$user,$pOpt);
        if($rc->rowCount()>0)
        {
         $count++;
         }
       
        }
   
       }
   
    }
     }
}
///////////////SUPPLIER DEBIT PAYMENTS END////

   
     }
    ///////SUPPLY IS GREATER///// 

   }

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