<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}
//include("../opendb.php");
//include("../test.php");

$pcode=$_REQUEST['pcode'];

function exqty1($pcode,$brcode,$user,$db)
{
    $tbl="sparetmp_br1".$user;
    mysqli_query($db,"CREATE TABLE if not exists $tbl (partcode varchar(200),partname varchar(200),supqty double(10,2),brcode varchar(20))");
    mysqli_query($db,"delete from $tbl")or die('delerr'.mysqli_error($db));

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

    $re=mysqli_query($db,$sq)or die(mysqli_error($db));

    while($res=mysqli_fetch_array($re))
    {
        $sql="insert into $tbl values('$res[0]','$res[1]','$res[2]','$res[3]')"; 
        // mysqli_query($db,$sql)or die('Purchase ERR:'.mysqli_error($db));
    }
    $sqq="select itemcode,itemname,qty,brcode from spareopstockitem  where itemcode='$pcode' order by brcode";
    $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]' and brcode='$ress[brcode]'");
  if(mysqli_num_rows($rr)>0)
  {
	$inrow=mysqli_fetch_array($rr);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow[partcode]' and brcode='$inrow[brcode]'";
	mysqli_query($db,$sql)or die('+ err'.mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl values('$ress[0]','".addslashes(trim($ress[1]))."','$ress[2]','$ress[brcode]')";
	mysqli_query($db,$sql)or die(mysqli_error($db));
   	}
}

$sqq="select partcode,partname,sum(qty),brcode from salesreturnitem  where partcode='$pcode' group by brcode,partcode";

$ree=mysqli_query($db,$sqq)or die('err416'.mysqli_error($db));

while($ress=mysqli_fetch_array($ree))
{
  $rr34=mysqli_query($db,"select * from $tbl where partcode='$ress[partcode]' and brcode='$ress[brcode]'");
  if(mysqli_num_rows($rr34)>0)
  {
	$inrow1=mysqli_fetch_array($rr34);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow1[partcode]' and brcode='$inrow1[brcode]'";
	mysqli_query($db,$sql)or die('++ Err:'.mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl values('$ress[0]','$ress[1]','$ress[2]','$ress[3]')";
	mysqli_query($db,$sql)or die(mysqli_error($db));
   	}
}

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

*/
$sq1="select partcode,partname,sum(qty),brcode from sparetransinexcess  where partcode='$pcode' group by  brcode,partcode";
$re1=mysqli_query($db,$sq1)or die('err46'.mysqli_error($db));

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

//*********** adj stock

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

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

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


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

$re2=mysqli_query($db,$sq2)or die('err54'.mysqli_error($db));


while($res2=mysqli_fetch_array($re2))
{
 $chktr=mysqli_query($db,"select * from $tbl where partcode='$res2[partcode]' and brcode='$res2[brcode]'");
 if(mysqli_num_rows($chktr)>0)
 {
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]' and brcode='$res2[brcode]'";
}
 else
{
 $sql="insert into $tbl values('$res2[0]','$res2[1]','-$res2[2]','$res2[brcode]')";
}
//print $sql.'<br>';
mysqli_query($db,$sql)or die('trans out'.mysqli_error($db));
}


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


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

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


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


//mysqli_query($db,$sql)or die('inv Err'.mysqli_error($db));
}


$sq222="select partcode,partname,sum(qty),brcode from spareinvoice_oth where partcode='$pcode' group by  brcode,partcode";

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


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


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

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



$sq222v="select item_code,item_code,sum(qty),brcode from item_purchase_veh where item_code='$pcode' group by  brcode,item_code";

$re212v=mysqli_query($db,$sq222v)or die('Err3v:'.mysqli_error($db));


while($res22v=mysqli_fetch_array($re212v))
{
  $chkin=mysqli_query($db,"select * from $tbl where partcode='$res22v[item_code]' and brcode='$res22v[brcode]'");
  $ttt="select * from $tbl where partcode='$res22v[item_code]' and brcode='$res22v[brcode]'";
  //print $ttt;
 if(mysqli_num_rows($chkin)>0)
 {
 $sql="update $tbl set supqty=(supqty+".$res22v[2].") where partcode='$res22v[item_code]' and 
  brcode='$res22v[brcode]'";
 }
 else
{
 $sql="insert into $tbl values('$res22v[0]','$res22v[1]','$res22v[2]','$res22v[brcode]')";
}


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

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




$sq222vv="select partcode,partname,sum(qty),brcode from spareinvoice_oth_veh where partcode='$pcode' group by  brcode,partcode";

$re212vv=mysqli_query($db,$sq222vv)or die('Err3:'.mysqli_error($db));


while($res22vv=mysqli_fetch_array($re212vv))
{
  $chkin=mysqli_query($db,"select * from $tbl where partcode='$res22vv[partcode]' and brcode='$res22vv[brcode]'");
 if(mysqli_num_rows($chkin)>0)
 {
 $sqlvv="update $tbl set supqty=(supqty-".$res22vv[2].") where partcode='$res22vv[partcode]' and 
  brcode='$res22vv[brcode]'";
 }
 else
{
 $sqlvv="insert into $tbl values('$res22vv[0]','$res22vv[1]','-$res22vv[2]','$res22vv[brcode]')";
}


mysqli_query($db,$sqlvv)or die('inv Err'.mysqli_error($db));
}

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


//************ pur ret

$sq222vvr="select partcode,partname,sum(retqty),brcode from item_purchase_ret where partcode='$pcode' group by  brcode,partcode";

$re212vvr=mysqli_query($db,$sq222vvr)or die('Err3 pr:'.mysqli_error($db));


while($res22vvr=mysqli_fetch_array($re212vvr))
{
  $chkinr=mysqli_query($db,"select * from $tbl where partcode='$res22vvr[partcode]' and brcode='$res22vvr[brcode]'");
 if(mysqli_num_rows($chkinr)>0)
 {
 $sqlvvr="update $tbl set supqty=(supqty-".$res22vvr[2].") where partcode='$res22vvr[partcode]' and 
  brcode='$res22vvr[brcode]'";
 }
 else
{
 $sqlvvr="insert into $tbl values('$res22vvr[0]','$res22vvr[1]','-$res22vvr[2]','$res22vvr[brcode]')";
}


mysqli_query($db,$sqlvvr)or die('ret Err r'.mysqli_error($db));
}

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

/*$sq211="select partcode,partname,sum(qty),brcode from warspareinvoice where partcode='$pcode' group by  brcode,partcode";

$re211=mysqli_query($db,$sq211)or die('Err3:'.mysqli_error($db));


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


//mysqli_query($db,$sql)or die('inv Err'.mysqli_error($db));
}*/
//************


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

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


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

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


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

$sq2="select partcode,partname,sum(retqty),brcode from sparepurreturn where partcode='$pcode' group by  brcode,partcode";
$re22=mysqli_query($db,$sq2)or die('err6'.mysqli_error($db));
while($res2=mysqli_fetch_array($re22))
{
   $chkin=mysqli_query($db,"select * from $tbl where partcode='$res2[partcode]' and brcode='$res2[brcode]'");
 if(mysqli_num_rows($chkin)>0)
{

$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]' and brcode='$res2[brcode]'";
//mysqli_query($db,$sql)or die('err6'.mysqli_error($db));
}
else
{
$sql="insert into $tbl values('$res2[0]','$res2[1]','-$res2[2]','$res2[brcode]')";
}

}
/*
//************ transout -war
$brres1=mysqli_query($db,"select brcode from branch");
while($brrow1=mysqli_fetch_array($brres1))
{
$tbl1="spareinvwar".$brrow1['brcode'];
$sq5="select partcode,partname,sum(psnqty) from $tbl1  where trans='n' and partcode='$pcode' group by partcode";

$re5a=mysqli_query($db,$sq5); //or die('War Err'.mysqli_error($db));
while($res5=mysqli_fetch_array($re5a))

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

//mysqli_query($db,$sql)or die('err8'.mysqli_error($db));
}
} */
/*
//************** war trans out
$sq16="select partcode,partname,sum(psnqty),brcode from warsparetransout where partcode='$pcode' group by brcode, partcode";
$re521=mysqli_query($db,$sq16);
while($res501=mysqli_fetch_array($re521))
{
   $chkin=mysqli_query($db,"select * from $tbl where partcode='$res501[partcode]' and brcode='$res501[brcode]'");
 if(mysqli_num_rows($chkin)>0)
{

$sql="update $tbl set supqty=(supqty-".$res501[2].") where partcode='$res501[partcode]' and brcode='$res501[brcode]'";
}
else
{
$sql="insert into $tbl values('$res5[0]','$res5[1]','-$res5[2]','$res5[brcode]')";

}
//print $sql.'<br>';
//mysqli_query($db,$sql)or die('err101'.mysqli_error($db));
}


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

$sq5="select partcode,partname,sum(qty),brcode from spareinvoicefoc where partcode='$pcode' group by  brcode,partcode";
$re53=mysqli_query($db,$sq5)or die('err6'.mysqli_error($db));
while($res5=mysqli_fetch_array($re53))
{
   $chkin=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]' and brcode='$res5[brcode]'");
 if(mysqli_num_rows($chkin)>0)
{
$sql="update $tbl set supqty=(supqty-".$res5[2].") where partcode='$res5[partcode]' and brcode='$res5[brcode]'";
mysqli_query($db,$sql)or die('err12'.mysqli_error($db));
}
else
{
$sql="insert into $tbl values('$res5[0]','$res5[1]','-$res5[2]','$res5[brcode]')";

}

}

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

 $sq9="select sum(outqty),partcode,brcode from adjstock where partcode='$pcode' group by brcode,partcode";

 $re513=mysqli_query($db,$sq9)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re513))
{
   $chkin=mysqli_query($db,"select * from $tbl where partcode='$res5[partcode]' and brcode='$res5[brcode]'");
 if(mysqli_num_rows($chkin)>0)
{
$sql="update $tbl set supqty=(supqty-".$res5[0].") where partcode='$res5[partcode]' and brcode='$res5[brcode]'";
}
else
{
$sql="insert into $tbl values('$res5[0]','$res5[1]','-$res5[2]','$res5[brcode]')";
}
//mysqli_query($db,$sql)or die('err12'.mysqli_error($db));
}



$sq="select * from $tbl where brcode='$brcode'";

$rem_1=mysqli_query($db,$sq)or die('Err'.mysqli_error($db));
$row=mysqli_fetch_array($rem_1);

return $row['supqty'];

}// end function
