<?php

if($_POST["sdate"] && $_POST["inam"] && $_POST["recvq"] && $_POST["user"] && $_POST["orderID"])
{ 
  $recvq= $_POST['recvq'];
  $pQty= $_POST['pQty'];
  $sOpt= $_POST['sOpt'];
  $confrv= $_POST['confrv'];
  $sdate= $_POST['sdate'];
 // $user= $_POST['user'];
  $user= $_POST['user']."(".$sOpt.")";
  $orderID= $_POST['orderID'];
  $iname= $_POST['inam'];
  if($pQty>0){
  //echo $pQty=$recvq+$pQty;
  $time= $confrv.":".$_POST['user'].": At: ".$sdate.":".Date("H:i:sa")."-Qty:(".$recvq.").".$sOpt.":".$recvq;
  }else{
      
  $time= $confrv.":".$_POST['user'].": At: ".$sdate.":".Date("H:i:sa")."-Qty:(".$recvq.").".$sOpt.":".$recvq;
  }
  require_once"labClass.php";
  $con=new labClass();
  $Q= $con->getThisItemPorder($orderID,$iname);
  if($Q->rowCount()>0)
    {
        
   $rQ= $con->updatepOrderItemRecv($orderID,$time,$iname);
    //print_r($rQ->ErrorInfo());
   if($rQ->rowCount()>0)
      { 
        //////////////
        
    if($sOpt=="SHOP"){
    $g= $con->getThisItem($iname)->fetch();
     $isprice= $g['isprice'];
     $ipprice= $g['ipprice'];  
     $itemID= $g['itemID'];
     $tQty= $recvq+ $g['iqty'];
     $user=Date("H:i:sa")."By:".$user;
     $nQty= $recvq;
        //////////// 
     $rc= $con->updateItemQ($iname,$tQty,$itemID);
     //print_r($rQ->ErrorInfo());
     if($rc->rowCount()>0)
      {
     $qQ= $con->itemPurchaseEntry($itemID,$iname,$ipprice,$isprice,$nQty,$sdate,$user);
     if($rc->rowCount()>0)
      {
      ?>
      <h6 class="text-success">Saved</h6>
      <?php
        }
      else{
       ?>
        <h6 class="text-danger">Purchase register failed!..</h6>
       <?php
       }
      }
     else{
       ?>
        <h6 class="text-danger">Updating stock failed!..</h6>
       <?php
       }
       
    }
    else{
       ////////////////////////
       
    $Qs= $con->getThisStoreItem($iname);
    if($Qs->rowCount()>0)
     {
     $g= $con->getThisStoreItem($iname)->fetch();
     $isprice= $g['isprice'];
     $ipprice= $g['ipprice'];  
     $itemID= $g['itemID'];
     $tQty= $recvq+ $g['iqty'];
     $user=Date("H:i:sa")."By:".$user;
     $nQty= $recvq;
     $rQs= $con->itemPurchaseEntryStore($itemID,$iname,$ipprice,$isprice,$nQty,$sdate,$user);
   //print_r($rQ->ErrorInfo());
     if($rQs->rowCount()>0)
      { 
       //itemID:itemID,iqty:iqty,nQty:nQty,isprice:isprice,ipprice:ipprice,iname:iname,tQty:tQty
        $Qrs= $con->updateItemQStore($iname,$tQty,$itemID);
        if($Qrs->rowCount()>0)
      {
      ?>
      <h6 class="text-success">Save!..</h6>
      <?php
        }
  else{
       ?>
        <h6 class="text-danger">Item error occurred!..</h6>
       <?php
       }
       }
    }
  else{
       ?>
        <h6 class="text-danger">Item does not exist!..</h6>
       <?php
       }
       
       
       
       ///////////////////////////
       }
       }
       }
  else{
       ?>
        <h6 class="text-danger">Order do not exist!..</h6>
       <?php
       }
       }
  else{
       ?>
       <h6 class="text-danger">All field required!..</h6>
       <?php
       }
?>