<?
//session_start();
//include("../opendb.php");
//include("../test.php");
?>

<center>
<?
$tbl="sparetmp".$brcode;
mysqli_query($db,"CREATE TABLE $tbl (partcode varchar(200),partname varchar(200),supqty double(10,2))");
mysqli_query($db,"delete from $tbl");

$sq="select partcode,partname,sum(supqty) from sparepurchase where brcode='$brcode' group by  partcode";
$re=mysqli_query($db,$sq);


while($res=mysqli_fetch_array($re))
{
  $sql="insert into $tbl values('$res[0]','$res[1]','$res[2]')"; 
  mysqli_query($db,$sql);
}

$sqq="select itemcode,itemname,sum(qty) from spareopstock where brcode='$brcode' group by  itemcode";
$ree=mysqli_query($db,$sqq)or die('ERr spareopstock'.mysqli_error($db));

while($ress=mysqli_fetch_array($ree))
{
  $rr=mysqli_query($db,"select * from $tbl where partcode='$ress[itemcode]'");
  if(mysqli_num_rows($rr)>0)
  {
	$inrow=mysqli_fetch_array($rr);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$ress[0]','".addslashes(trim($ress[1]))."','$ress[2]')";
	mysqli_query($db,$sql);
   	}
}


$sqq="select partcode,partname,sum(qty) from salesreturn where brcode='$brcode' group by  partcode";
$ree=mysqli_query($db,$sqq);

while($ress=mysqli_fetch_array($ree))
{
  $rr2=mysqli_query($db,"select * from $tbl where partcode='$ress[partcode]'");
  if(mysqli_num_rows($rr2)>0)
  {
	$inrow=mysqli_fetch_array($rr2);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$ress[0]','$ress[1]','$ress[2]')";
	mysqli_query($db,$sql);
   	}
}



$sq1="select partcode,partname,sum(qty) from sparetransin where brcode='$brcode' group by  partcode";
$re1=mysqli_query($db,$sq1);

while($res1=mysqli_fetch_array($re1))
{
  $rr3=mysqli_query($db,"select * from $tbl where partcode='$res1[partcode]'");
  if(mysqli_num_rows($rr3)>0)
  {
    $inrow=mysqli_fetch_array($rr3);
	$sql="update $tbl set supqty=(supqty+".$res1[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$res1[0]','$res1[1]','$res1[2]')";
	mysqli_query($db,$sql);
   	}
}


$sq2="select partcode,partname,sum(qty) from sparetransout where brcode='$brcode' group by  partcode";

$re2=mysqli_query($db,$sq2);


while($res2=mysqli_fetch_array($re2))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err2'.mysqli_error($db));
}


$sq2="select partcode,partname,sum(qty) from spareinvoice where brcode='$brcode' group by  partcode";

$re21=mysqli_query($db,$sq2)or die('Err3:'.mysqli_error($db));


while($res2=mysqli_fetch_array($re21))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}

//************

$sq2="select partcode,partname,sum(qty) from warspareinvoice where brcode='$brcode' group by  partcode";

$re21=mysqli_query($db,$sq2)or die('Err3:'.mysqli_error($db));


while($res2=mysqli_fetch_array($re21))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}

//************



$sq2="select partcode,partname,sum(qty) from jcardparts where brcode='$brcode' group by  partcode";

$re212=mysqli_query($db,$sq2)or die('Err3:'.mysqli_error($db));


while($res2=mysqli_fetch_array($re212))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}


//*****************

$sq2="select partcode,partname,sum(retqty) from sparepurreturn where brcode='$brcode' group by  partcode";
$re23=mysqli_query($db,$sq2);
while($res2=mysqli_fetch_array($re23))
{
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
mysqli_query($db,$sql)or die('err6'.mysqli_error($db));
}

//************ transout -war
$tbl1="spareinvwar".$brcode;
$sq5="select partcode,partname,sum(psnqty) from $tbl1  group by  partcode";
$re51=mysqli_query($db,$sq5);
while($res5=mysqli_fetch_array($re51))
{
$sql="update $tbl set supqty=(supqty-".$res5[2].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err8'.mysqli_error($db));
}
return;
//************ trans in war

$sq6="select partcode,partname,sum(psnqty) from sparetrinwar where brcode='$brcode' group by  partcode";
$re52=mysqli_query($db,$sq6);
while($res5=mysqli_fetch_array($re52))
{
$sql="update $tbl set supqty=(supqty+".$res6[2].") where partcode='$res6[partcode]'";
mysqli_query($db,$sql)or die('err10'.mysqli_error($db));
}
/*
//************** war trans out
$sq16="select partcode,partname,sum(psnqty) from warsparetransout where brcode='$brcode' group by  partcode";
$re521=mysqli_query($db,$sq16);
while($res501=mysqli_fetch_array($re521))
{
$sql="update $tbl set supqty=(supqty-".$res501[2].") where partcode='$res501[partcode]'";
//print $sql.'<br>';
mysqli_query($db,$sql)or die('err101'.mysqli_error($db));
}

*/
//************ foc invoice

$sq5="select partcode,partname,sum(qty) from spareinvoicefoc where brcode='$brcode' group by  partcode";
$re53=mysqli_query($db,$sq5);
while($res5=mysqli_fetch_array($re53))
{
$sql="update $tbl set supqty=(supqty+".$res5[2].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err12'.mysqli_error($db));
}

//*********** adj stock
 $sql="select sum(qty) from adjstock where partcode='$pcode' and brcode='$brcode'";
 $re512=mysqli_query($db,$sql)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re512))
{
$sql="update $tbl set supqty=(supqty+".$res5[0].") where partcode='$res5[partcode]'";

mysqli_query($db,$sql)or die('err12'.mysqli_error($db));
}

 $sql="select sum(outqty) from adjstock where partcode='$pcode' and brcode='$brcode'";
 $re513=mysqli_query($db,$sql)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re513))
{
$sql="update $tbl set supqty=(supqty-".$res5[0].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err12'.mysqli_error($db));
}

?>
