<?php
if(isset($_POST["pOpt"]) &&isset($_POST["bal"]) && isset($_POST["orno"]) && isset($_POST["user"]) && isset($_POST["cbal"]) &&    isset($_POST["sdate"]))
{ 
////
    $_POST['cbal'];
    $_POST['bal'];
    $cbal= $_POST['cbal'];
    $cash= $_POST['bal'];
    $orderID= $_POST['orno'];
    $pOpt= $_POST['pOpt'];
    $user= $_POST['user'];
    $sdate= $_POST['sdate'];
    $tPrice=$cash;
    if($cbal==$cash)
    {
      $pst="PAID";
    }
    else{
       $pst="NOT PAID"; 
    } 
  //CHECKING ENDS HER
    require_once"labClass.php";
    $con=new labClass(); 
 $pOpt="CASH";
 $k= $con->getMuamalaCash($pOpt,$sdate);
 if($k->rowCount()>0)
      {
   $cs=$k->fetch() ;
   if($cs['statusB']=="Saved"){
       
    $Qc= $con->getThisNonOrderDbBID($orderID);
      //print_r($Qc->ErrorInfo());
    if($Qc->rowCount()>0)
     {
      $p= $con->getThisNonOrderDbBID($orderID)->fetch();
      $ptd=$p["paidtodate"]+$cash;
      $obal=$p["balance"];
      $cus=$p["cname"];
      $nbal=$obal-$cash;       
      if(($ptd>0) && ($nbal>=0)) 
       { 
       $rQc= $con->updateNonOrder($orderID,$nbal,$ptd,$pst);
       if($rQc->rowCount()>0)
        {
        $rc= $con->debitNonPurchRec($orderID,$cus,$cash,$sdate,$user,$pOpt);
        if($rc->rowCount()>0)
        {
        
          //////////////////////////////
        $pOpt="CASH";
        $e= $con->getMuamalaCash($pOpt,$sdate);
          if($e->rowCount()>0)
           {
          $nn=$e->fetch();
          $tPrice= $nn['amount']+$tPrice;
          $r= $con->updateToDayCash($tPrice,$pOpt,$sdate);
              if($r->rowCount()>0)
               {
                ///$rQc= $con->miamala($pOpt,$user,$tPrice,$sdate);
                  ?>
                   <h6 class="text-success">Save!..</h6>
                  <?php
               }
               else{
                  ?>
                  <h6 class="text-danger">Transaction update failed!..</h6>
                 <?php
                   }
              }
           else{
              $rc= $con->miamala($pOpt,$user,$tPrice,$sdate);
                  if($rc->rowCount()>0)
                   {
                    ?>
                      <h6 class="text-success">Save!..</h6>
                    <?php
                    }
                 else{
                    ?>
                    <h6 class="text-danger">Failed to register transaction!..</h6>
                   <?php
                    }
               }
    //////////////
         
         
         
         
         }
        else{
          ?>
          <h6 class="text-danger">Technical error!..</h6>
          <?php
           }
        }
  else{
       ?>
       <h6 class="text-danger">Upadate failed!..</h6>
       <?php
       }
       }
  else{
       ?>
       <h6 class="text-danger">Invalid balance submit!..</h6>
       <?php
       }
      }
  else{
       ?>
       <h6 class="text-danger">Invalid order number!..</h6>
       <?php
       }
       
   }
else{
   echo "Transaction closed up can't delete!";

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