<?php
if($_POST["patientID"] && $_POST["recID"] && $_POST["iname"]  && $_POST["iqty"] && $_POST["sdate"] && $_POST["user"])
{  
    $patientID= $_POST['patientID'];
	$iname= $_POST['iname'];
    $sdate= $_POST['sdate'];
    $recID= $_POST['recID'];
    $iQty= $_POST['iqty'];
    $sdate= $_POST['sdate'];
    $user= $_POST['user'];
     
    $billOp= "Cash";
    $regDate= $sdate;
  //CHECKING ENDS HER
  require_once"labClass.php";
  $con=new labClass();
  $Q= $con->getThisDosEX($recID,$iname);
  if($Q->rowCount()>0)
    {
   $rQ= $con->getThisItem($iname);
  
   if($rQ->rowCount()>0)
      {
        $g=$rQ->fetch();
        if ($g['iqty']>=$iQty) {
        $itemID=$g['itemID'];
        $amount=$g['isprice']*$iQty;
        $tQty=$g['iqty']-$iQty;

        $Qr= $con->DosageDispensed($user,$iQty,$iname,$recID,$patientID);
         //print_r($Qr->ErrorInfo());
        if($Qr->rowCount()>0)
        {

         $upS= $con->updateItemQ($iname,$tQty,$itemID);
       if($upS->rowCount()>0)
        {
      $sd= $con->getServID($patientID,$regDate);
      if($sd->rowCount()>0)
      {
        $g=$sd->fetch();
        $sID=$g['sID'];
       }
      else{
        $sB= $con->getBillID();
        $sID=$sB->rowCount()+1;
      }
         $in= $con->billMedical($sID,$sdate,$iname,$user, $billOp,$amount,$patientID);
         if($in->rowCount()>0)
        {
        ?>
        <h6 class="text-success">Save!..</h6>
        <?php
      }
    else{
       ?>
        <h6 class="text-danger">Saving failed!..</h6>
       <?php
        }
       }else{
       ?>
        <h6 class="text-danger">Deduction failed!..</h6>
       <?php
        }
        }
  else{
       ?>
        <h6 class="text-danger">Error occurred!..</h6>
       <?php
        }
        }
        else{
       ?>
        <h6 class="text-danger">Check your stock!..</h6>
       <?php
       }
       }
    }
  else{
       ?>
        <h6 class="text-danger">Item does not exist!..</h6>
       <?php
       }
       }
  else{
       ?>
       <h6 class="text-danger">All field required!..</h6>
       <?php
       }
?>