<?
session_start();
$brcode=$_SESSION['brcode'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include_once('opendb.php');

$dt=date("Y-m-d");
$upd =$_REQUEST['upd'];
$pos =$_REQUEST['pos'];
$pos1 =$_REQUEST['pos1'];

if($upd){
 $j=$pos-1;
 $pos2=$j+1;
//echo $pos2;
 }
$upd1 =$_REQUEST['upd1'];
if($upd1){
  $j=1;
 $pos2=$pos1;
 }
$k=0;

$partname=$_REQUEST['partname'];
foreach($partname as $a)
{
 $partname[$k]=$a;
		 
 $k++;
}
$k=0;
foreach($_REQUEST['grp'] as $a)
{
 $grp[$k]=$a;
 $k++;
}
$k=0;
foreach($_REQUEST['subgp'] as $a)
{
 $subgp[$k]=$a; 
 $k++;
}
$k=0;
foreach($_REQUEST['redct'] as $a)
{
 $redct[$k]=$a; 
 $k++;
}
$k=0;
foreach($_REQUEST['part'] as $a)
{
 $part[$k]=$a; 
 $k++;
}
$k=0;
foreach($_REQUEST['op'] as $a)
{
 $op[$k]=$a; 
 $k++;
}
$k=0;
foreach($_REQUEST['locat'] as $a)
{
 $locat[$k]=$a; 
 $k++;
}
$k=0;
$ndp=$_REQUEST['ndp'];
foreach($ndp as $a)
{
 $ndp[$k]=$a; 
 $k++;
}
$k=0;
$list2=$_REQUEST['list2'];
foreach($list2 as $a)
{
 $list2[$k]=$a; 
 $k++;
}
$k=0;
$rebtvl=$_REQUEST['rebtvl'];
foreach($rebtvl as $a)
{
 $rebtvl[$k]=$a; 
 $k++;
}
$k=0;
$rbtvl=$_REQUEST['rbtvl'];
foreach($rbtvl as $a)
{
 $rbtvl[$k]=$a; 
 $k++;
}
	
	for($i=$j;$i<$pos2;$i++)
		{
			
			mysqli_query($db,"update partmaster set partname='$partname[$i]',subgroup='$subgp[$i]',reduction='$redct[$i]',rbtbl='$rebtvl[$i]',rbtvl='$rbtvl[$i]' where partcode='$part[$i]'");
			//$sql="update spareprmast set ndpcost='$ndp[$i]',lstprice='$list2[$i]' where partcode='$part[$i]'";
                       
                        mysqli_query($db,"update spareprmast set ndpcost='$ndp[$i]',lstprice='$list2[$i]' where partcode='$part[$i]'");
			if($op[$i]>0)
				{
					$reqry="select * from sparestock where itemcode='$part[$i]' and brcode='$brcode'";
					$rers=mysqli_query($db,$reqry);
					$rrow=mysqli_fetch_array($rers);
					$difvl=$op[$i]-$rrow[6];
					
					$num=mysqli_num_rows($rers);
					if($num>0)
						{
							if(abs($difvl)>0)
								{
									$curstk[$i]=$curstk[$i]+$difvl;
									mysqli_query($db,"update sparestock set itemname='$partname[$i]',item_subgrp='$subgp[$i]',opstk='$op[$i]',curstk='$curstk[$i]',location='$locat[$i]' where itemcode='$part[$i]' and brcode='$brcode'");	
								}
							else
								{
									mysqli_query($db,"update sparestock set itemname='$partname[$i]',item_subgrp='$subgp[$i]',opstk='$op[$i]',location='$locat[$i]' where itemcode='$part[$i]' and brcode='$brcode'");
								}
							
						}
					else
						{
							mysqli_query($db,"insert into sparestock(itemcode,itemname,opstk,curstk,brcode,location,item_grp,item_subgrp) values('$part[$i]','$partname[$i]','$op[$i]','$op[$i]','$brcode','$locat[$i]','$grp[$i]','$subgp[$i]') ");
						}
					
				}
			else
				{
					mysqli_query($db,"update sparestock set itemname='$partname[$i]',item_subgrp='$subgp[$i]',buyprice='$ndp[$i]',sellprice='$list2[$i]',opstk='$op[$i]',curstk='$curstk[$i]',location='$locat[$i]' where itemcode='$part[$i]' and brcode='$brcode'");
				}
		}
	print "<script> alert ('Updated');document.location='partmastrepo.php';</script>";

?>

 
