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

<center>
<?
$tbl="sparetmp".$brcode.$user;
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());

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);
   	}
}

//*********** adj stock
 $sq8="select sum(qty),partcode,partname from adjstock where brcode='$brcode' group by partcode";

 $re512=mysqli_query($db,$sq8)or die('ERr adjtock'.mysqli_error());

while($res5=mysqli_fetch_array($re512))
{
  $rr=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]'");
  if(mysqli_num_rows($rr)>0)
  {
	$inrow=mysqli_fetch_array($rr);
    	$sql="update $tbl set supqty=(supqty+".$res5[0].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$res5[1]','".addslashes(trim($res5[2]))."','$res5[0]')";
	mysqli_query($db,$sql)or die('adj in err:'.mysqli_error());
   	}
}

//********************sale return
$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);
   	}
}

$sq6="select partcode,partname,sum(recqty) from sparetrinwarph where brcode='$brcode' group by  partcode";
$phre52=mysqli_query($db,$sq6);
while($res5=mysqli_fetch_array($phre52))
{
 $seareph=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]'");
 if(mysqli_num_rows($seareph)>0)
{
$sql="update $tbl set supqty=(supqty+".$res5[2].") where partcode='$res5[partcode]'";
}
 else
{
$sql="insert into $tbl values('$res5[0]','".addslashes(trim($res5[1]))."','$res5[2]')";
}
mysqli_query($db,$sql)or die('err10'.mysqli_error());
}


$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))
{
 $cch=mysqli_query($db,"select * from $tbl where partcode='$res2[partcode]'");
  if(mysqli_num_rows($cch)>0)
   {
     $sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
     mysqli_query($db,$sql)or die('err2'.mysqli_error());
   }
   else
    {
        $sq="insert into $tbl values('$res2[0]','$res2[1]','-$res2[2]')";
         mysqli_query($db,$sq) or die('tranout err'.mysqli_error());

    }

}


$sq2="select partcode,partname,sum(qty) from spareinvoice where brcode='$brcode' group by  partcode";
$re21=mysqli_query($db,$sq2)or die('Err3:'.mysqli_error());

while($res2=mysqli_fetch_array($re21))
{
 $cch=mysqli_query($db,"select * from $tbl where partcode='$res2[partcode]'");
 if(mysqli_num_rows($cch)>0)
{
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
mysqli_query($db,$sql)or die('err4'.mysqli_error());
}
else
{
 $sq="insert into $tbl values('$res2[0]','$res2[1]','-$res2[2]')";
   mysqli_query($db,$sq) or die('saleinv err'.mysqli_error());
}


}

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

$sq2="select partcode,partname,sum(qty) from warspareinvoice where brcode='$brcode' group by  partcode";
$re21=mysqli_query($db,$sq2)or die('Err3:'.mysqli_error());

while($res2=mysqli_fetch_array($re21))
{
 $cch=mysqli_query($db,"select * from $tbl where partcode='$res2[partcode]'");
 if(mysqli_num_rows($cch)>0)
{
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
mysqli_query($db,$sql)or die('err4'.mysqli_error());
}
else
{
 $sq="insert into $tbl values('$res2[0]','$res2[1]','-$res2[2]')";
   mysqli_query($db,$sq) or die('warspare err'.mysqli_error());
}


}

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


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

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

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

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


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

$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))
{
  $cch=mysqli_query($db,"select * from $tbl where partcode='$res2[partcode]'");
 if(mysqli_num_rows($cch)>0)
{
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
mysqli_query($db,$sql)or die('err6'.mysqli_error());
}
else
{
$sq="insert into $tbl values('$res2[0]','$res2[1]','-$res2[2]')";
         mysqli_query($db,$sq) or die('tranout err'.mysqli_error());
}
}

//************ transout -war
$tbl1="spareinvwar".$brcode;
$sq5="select partcode,partname,sum(psnqty) from $tbl1  where trans='n' group by  partcode";
$re51=mysqli_query($db,$sq5);
while($res5=mysqli_fetch_array($re51))
{
  $cch=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]'");
 if(mysqli_num_rows($cch)>0)
{
$sql="update $tbl set supqty=(supqty-".$res5[2].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err8'.mysqli_error());
}
else
{
$sq="insert into $tbl values('$res5[0]','$res5[1]','-$res5[2]')";
         mysqli_query($db,$sq) or die('tranout err'.mysqli_error());
}

}



//*********** adj stock
 $sq9="select sum(outqty),partcode,partname from adjstock where brcode='$brcode' group by partcode";

 $re513=mysqli_query($db,$sq9)or die(mysqli_error());
while($res5=mysqli_fetch_array($re513))
{

 $adj_ch=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]'");
 if(mysqli_num_rows($adj_ch)>0)
{
  $sql="update $tbl set supqty=(supqty-".$res5[0].") where partcode='$res5[partcode]'";

}
else
{ 
 $sql="insert into $tbl values('$res5[partcode]','$res5[partname]','-$res5[0]')";

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


//************** 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))
{
 $cch=mysqli_query($db,"select * from $tbl where partcode='$res501[partcode]'");
 if(mysqli_num_rows($cch)>0)
{
$sql="update $tbl set supqty=(supqty-".$res501[2].") where partcode='$res501[partcode]'";
}
else
{
  $sql="insert into $tbl values('$res501[partcode]','$res501[partname]','-$res501[2]')";
}
//print $sql.'<br>';
mysqli_query($db,$sql)or die('err101'.mysqli_error());
}

//************ 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))
{
 $cch=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]'");
 if(mysqli_num_rows($cch)>0)
{
$sql="update $tbl set supqty=(supqty-".$res5[2].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err12'.mysqli_error());
}
else
{
 $sql="insert into $tbl values('$res5[partcode]','$res5[partname]','-$res5[0]')";
mysqli_query($db,$sql)or die('err12'.mysqli_error());
}

}
//print "<script> window.open('stockvalid.php'); </script>";
return;
?>
