<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$sq="CREATE TABLE IF NOT EXISTS stockverification (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `itemcode` varchar(100) DEFAULT NULL,
  `itemname` varchar(100) DEFAULT NULL,
  `sellprice` double(10,2) DEFAULT NULL,
  `ledqty` double(10,2) DEFAULT NULL,
  `curstk` double(10,2) DEFAULT NULL,
  `brcode` varchar(50) DEFAULT NULL,
  `gname` varchar(100) DEFAULT NULL,
  `item_subgrp` varchar(100) DEFAULT NULL,
  `stock_short` double(10,2) DEFAULT NULL,
  `stock_excess` double(10,2) DEFAULT NULL,
  `stockverifydt` date DEFAULT NULL,
  `ref_no` varchar(20) DEFAULT NULL,
  `remrks` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";

mysqli_query($db,$sq);
$verfcdt=$_REQUEST['entrdt'];
$ldqty=$_REQUEST['ldqty'];
$verqt=$_REQUEST['verqt'];
$itemcod=$_REQUEST['itemcod'];
$remrks=$_REQUEST['notes'];
$re=mysqli_query($db,"select ref_no from stockverification where brcode='$brcode' and stockverifydt='$verfcdt'");
if(mysqli_num_rows($re)<1)
{
    $re1=mysqli_query($db,"select max(ref_no) from stockverification where brcode='$brcode'");
    if(mysqli_num_rows($re1)<1)
        {
            $inv='000001';
        }
    else
        {
            $row=mysqli_fetch_array($re1);
            $inv=substr($row[0],-6)+1;
            $inv=substr('000000'.$inv,-6);
            if($inv==0)
                {
                    $inv='000001';
                }

        }
  }
else
        {
            $row1=mysqli_fetch_array($re);
            $inv=$row1[0];


        }
    $excsqty=$verqt-$ldqty;
    if($excsqty>0)
        {
            $balvl=$excsqty;
            $shrtqty='';
        }
    else
        {
            $shrtqty= abs($excsqty);
            $balvl='';
         }
           $grp=mysqli_query($db,"select a.gname,a.partname,b.lstprice from partmaster a,spareprmast b where a.partcode=b.partcode and a.partcode='$itemcod'");
           $grp_row=mysqli_fetch_array($grp);
           $grp_name=$grp_row[0];                           
           $partname=$grp_row[1];
           $itmrt=$grp_row[2];
           $sql="select itemcode from stockverification where itemcode='$itemcod' and stockverifydt='$verfcdt' and brcode='$brcode'";
           $sqlrs=mysqli_query($db,$sql);
           $num=mysqli_num_rows($sqlrs);
           if($num>0)
              {
                 mysqli_query($db,"delete from stockverification where itemcode='$itemcod' and stockverifydt='$verfcdt' and brcode='$brcode' ");
              }
            $query="insert into stockverification(itemcode,itemname,sellprice,ledqty,curstk,brcode,gname,stock_short,stock_excess,stockverifydt,ref_no,remrks) values('$itemcod','$partname','$itmrt','$ldqty','$verqt','$brcode','$grp_name','$shrtqty','$balvl','$verfcdt','$inv','$remrks')";
            
            mysqli_query($db,$query);
            $verifyvl=$verqt*$itmrt;
            $balrate=$balvl*$itmrt;
            $shrtvl=$shrtqty*$itmrt;
?>
<input type='hidden'  id="vrflvl" value="<? echo $verifyvl ?>">
<input type='hidden' id="mblqty" value="<? echo $balvl ?>">
<input type='hidden'  id="mblrate" value="<? echo $balrate; ?>">
<input type='hidden' id="mshrtqty" value="<? echo $shrtqty; ?>">
<input type='hidden'  id="shrtvl" value="<? echo $shrtvl ?>">
<input type='hidden' name="entrydt" id="entrydt" value="<? echo $verfcdt ?>">
