<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
$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,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";

mysqli_query($db,$sq);
$verfcdt=$_REQUEST['ref_date'];

$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];


        }



 	$i=0;	
	foreach($_REQUEST['partcode'] as $a)
	{
 		$partcode[$i]=$a;
 		$i++;
	}     
        $i=0;       
	foreach($_REQUEST['verfyqty'] as $a)
	{
 		$verfyqty[$i]=$a;
		$i++;
	}
        $i=0;
	foreach($_REQUEST['itmrt'] as $a)
	{
 		$itmrt[$i]=$a;
		$i++;
	}
         $i=0;
	foreach($_REQUEST['ledqty'] as $a)
	{
 		$ledqty[$i]=$a;
		$i++;
	}
        $flg1=0;
        for($m=0;$m<$i;$m++)
            {
                $excsqty[$m]=$verfyqty[$m]-$ledqty[$m];
                if($excsqty[$m]>0)
                {
                    $balvl[$m]=$excsqty[$m];
                    $shrtqty[$m]='';
                }
                else
                {
                   $shrtqty[$m]= abs($excsqty[$m]);
                   $balvl[$m]='';
                }
                $grp=mysqli_query($db,"select gname from partmaster where partcode='$partcode[$m]'");
                            $grp_row=mysqli_fetch_array($grp);
                            $grp_name=$grp_row[0];
                            $partqry=mysqli_query($db,"select partname from partmaster where partcode='$partcode[$m]'");
                            $prrs=mysqli_fetch_array($partqry);
                            $partname[$m]=$prrs[0];
                            $sql="select itemcode from stockverification where itemcode='$partcode[$m]' 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='$partcode[$m]' and stockverifydt='$verfcdt' and brcode='$brcode' ");
                            }

                $query="insert into stockverification(itemcode,itemname,sellprice,ledqty,curstk,brcode,gname,stock_short,stock_excess,stockverifydt,ref_no) values('$partcode[$m]','$partname[$m]','$itmrt[$m]','$ledqty[$m]','$verfyqty[$m]','$brcode','$grp_name','$shrtqty[$m]','$balvl[$m]','$verfcdt','$inv')";
                
                //$sql="update sparestock set curstk_nor='$verfyqty[$m]' where itemcode='$partcode[$m]' and brcode='$brcode'";
               
                mysqli_query($db,$query);
                $flg1++;

            }

        
		if($flg1==$i) 
 		{
			
			//echo "Data  saved with invoice number:".$invno;
			echo "<script>document.location='stockentry.php?type=$grp_name';</script>";
		}  
 	
	
   
?>
