<?
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');
$brcode=$_SESSION['brcode'];
$i=0;
$partcode=$_REQUEST['partcode'];
foreach($partcode as $a)
{
 	$partcode[$i]=$a; 
 	$i++;
}
$i=0;
$partname=$_REQUEST['partname'];
foreach($partname as $a)
{
 	$partname[$i]=$a; 
 	$i++;
}
$i=0;
$rate=$_REQUEST['rate'];
foreach($rate as $a)
{
 	$rate[$i]=$a; 
 	$i++;
}
$i=0;
$qty=$_REQUEST['qty'];
foreach($qty as $a)
{
 	$qty[$i]=$a; 
 	$i++;
}
$i=0;
$outqty=$_REQUEST['outqty'];
foreach($outqty as $a)
{
 	$outqty[$i]=$a; 
 	$i++;
}
$i=0;
$gtot=$_REQUEST['gtot'];
foreach($gtot as $a)
{
 	$gtot[$i]=$a; 
 	$i++;
}

$i=0;
$note=$_REQUEST['note'];
foreach($note as $a)
{
 	$note[$i]=$a; 
 	$i++;
}

$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y=$_REQUEST['y'];
$entrydate=$y.'-'.$m.'-'.$d;
//$entrydate=date("Y-m-d");
 $jcres=mysqli_query($db,"select max(refno) from adjstock where brcode='$brcode'");
 $jcrow=mysqli_fetch_row($jcres);
 if($jcrow[0]<=0)
   $refno=1;
 else
  $refno=($jcrow[0]+1);
for($j=0;$j<$i;$j++)
{
	if($qty[$j]>0 || $outqty[$j]>0)
		{
			/*$grp=mysqli_query($db,"select gname from partmaster where partcode='$partcode[$j]'");
       		$grp_row=mysqli_fetch_array($grp);
			$grp_name=$grp_row[0];*/
 			$sql="insert into adjstock(entrydate,brcode,refno,partcode,partname,rate,qty,outqty,gtot,note,user) values('$entrydate','$brcode','$refno','$partcode[$j]','$partname[$j]','$rate[$j]','$qty[$j]','$outqty[$j]','$gtot[$j]','$note[$j]','$user')";
 			mysqli_query($db,$sql) or die(mysqli_error($db));
			/*$reqry="select * from sparestock where itemcode='$partcode[$j]' and brcode='$brcode'";
			$rers=mysqli_query($db,$reqry);
			$num=mysqli_num_rows($rers);
			if($num>0)
				{
					if($qty[$j]>0)
						{
							mysqli_query($db,"update sparestock set curstk=(curstk+'$qty[$j]') where itemcode='$partcode[$j]' and brcode='$brcode'");
						}
					if($outqty[$j]>0)
						{
							mysqli_query($db,"update sparestock set curstk=(curstk-'$outqty[$j]') where itemcode='$partcode[$j]' and brcode='$brcode'");
						}
				}
			else
				{
					if($qty[$j]>0)
						{
							mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk,brcode) values('$partcode[$j]','$partname[$j]','$qty[$j]','$brcode') ");
						}	
					if($outqty[$j]>0)
						{
							mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk,brcode) values('$partcode[$j]','$partname[$j]','-$outqty[$j]','$brcode') ");
						}
				}*/
		}
		print "Data saved";
		echo "<script> document.location='adjstock.php';</script>";
}
?>



