<?
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 $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' and brcode='$brcode'";
    $re=mysqli_query($db,$sq)or die(mysqli_error($db));
    while($res=mysqli_fetch_array($re))
    {
        if($res[2]>0)
        {
        $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 spareopstock  where itemcode='$pcode' and brcode='$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('Opstock ERR:'.mysqli_error($db));
   	    }
    }
    
    $sqq="select partcode,partname,qty,brcode from sparetransin  where partcode='$pcode'  and brcode='$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[partcode]' 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('Trans IN:'.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('Trans IN ERR:'.mysqli_error($db));
   	    }
    }
    
    $sqq="select partcode,partname,qty,brcode from sparetransout  where partcode='$pcode'  and brcode='$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[partcode]' 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('TRans Out 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 salesreturn  where partcode='$pcode'  and brcode='$brcode'";
    $ree=mysqli_query($db,$sqq)or die('err416'.mysqli_error($db));
    while($ress=mysqli_fetch_array($ree))
    {
        if($ress[2]>0)
        {
        $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('Sale Ret ERR:'.mysqli_error($db));
        }
        else
   	    {
	        $sql="insert into $tbl values('$ress[0]','$ress[1]','$ress[2]','$ress[3]')";
            mysqli_query($db,$sql)or die('Sale Ret ERR:'.mysqli_error($db));
   	    }
        }
    }
    $sq8="select sum(qty),partcode,partname,brcode from adjstock where partcode='$pcode'  and brcode='$brcode'";
    $re512=mysqli_query($db,$sq8)or die('ERr adjtock'.mysqli_error($db));
    while($res5=mysqli_fetch_array($re512))
    {
        if($res5[0]>0)
        {
        $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]'";
    	    print $sql;
	        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]')";
	        print $sql;
	        mysqli_query($db,$sql)or die('upd adj err'.mysqli_error($db));
   	    }
        }
    }
    $sq2="select partcode,partname,sum(qty),brcode from spareinvoice where partcode='$pcode'  and brcode='$brcode'";

    $re21=mysqli_query($db,$sq2)or die('Err3:'.mysqli_error($db));
    while($res2=mysqli_fetch_array($re21))
    {
        if(abs($res2[2])>0)
        {
        $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'  and brcode='$brcode'";
    $re212=mysqli_query($db,$sq222)or die('Err3:'.mysqli_error($db));
    while($res22=mysqli_fetch_array($re212))
    {
        if(abs($red22[2])>0)
        {
        $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));
        }
    }

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

    $re513=mysqli_query($db,$sq9)or die(mysqli_error($db));
    while($res5=mysqli_fetch_array($re513))
    {
        
        if(abs($res5[2]>0)){
        $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
?>
