<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='index.html' </script>";
  return;
  
}
include("../opendb.php");
include("../test.php");
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
$y2=$_REQUEST['y2'];
$m2=$_REQUEST['m2'];
$d2=$_REQUEST['d2'];
 $dfrom=$y1.'-'.$m1.'-'.$d1;
 $dto=$y2.'-'.$m2.'-'.$d2;
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
?>

<center>
<table>
<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE MARKETING P LTD </b><BR>
	 <?print $brrow['braddr'].",".$brrow['brcity'].",<br><u> PH - ".$brrow['brphone']?></u>
TIN: <?print $brrow['tin']?>, CST: <?print $brrow['cstno']?>&nbsp;
</td></tr>
</TABLE>

<?
print "<B>STOCK REPORT</B> <BR> From ".datetohtml($dfrom)." To ".datetohtml($dto);

?>
<link rel="stylesheet" href="../style.css" type="text/css" />

<style>
	//tr1{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<center>
<?
$tbl="sparetmpdw".$brcode.$user;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl (partcode varchar(200),partname varchar(200),supqty double(10,2) default 0, inqty double(10,2) default 0, outqty double(12,2) default 0)");
mysqli_query($db,"delete from $tbl");
//*********** adj stock in
 $sq9="select partcode,partname,sum(qty) from adjstock where brcode='$brcode' and entrydate<'$dfrom' group by partcode";

 $re513=mysqli_query($db,$sq9)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re513))
{
  $sql="insert into $tbl (partcode,partname,supqty) values('$res5[0]','".addslashes($res5[1])."','$res5[2]')"; 

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

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

 $re513o=mysqli_query($db,$sq9)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re513o))
{
  $sql="insert into $tbl (partcode,partname,supqty) values('$res[0]','".addslashes($res[1])."','$res[2]')"; 

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

//****************
$sq="select partcode,partname,sum(supqty) from sparepurchase where brcode='$brcode' and entrydate<'$dfrom' group by  partcode";
//and entrydate>='$dfrom' 
$re=mysqli_query($db,$sq)or die(mysqli_error($db));

while($res=mysqli_fetch_array($re))
{
 $chinstk=mysqli_query($db,"select * from $tbl where partcode='$res[0]'");
 if(mysqli_num_rows($chinstk)<=0)
  $sql="insert into $tbl (partcode,partname,supqty) values('$res[0]','".addslashes($res[1])."','$res[2]')"; 
else
 $sql="update $tbl set supqty=(supqty+".$res[2].") where partcode='$res[partcode]'";
  mysqli_query($db,$sql)or die('pur '.mysqli_error($db));
}

$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))
{
// print $ress[itemcode].'-'.$ress[2].'<br>';
  $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)or die(mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl  (partcode,partname,supqty) values('".addslashes($ress[0])."','".addslashes(trim($ress[1]))."','$ress[2]')";

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


$sqq="select partcode,partname,sum(qty) from salesreturn where brcode='$brcode' and entrydate<'$dfrom'   group by  partcode";
$ree=mysqli_query($db,$sqq)or die('ERr retrn'.mysqli_error($db));

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)or die('sret'.mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl  (partcode,partname,supqty) values('$ress[0]','".addslashes($ress[1])."','$ress[2]')";
	mysqli_query($db,$sql)or die('sret'.mysqli_error($db));
   	}
}


$sq1="select partcode,partname,sum(qty) from sparetransin where brcode='$brcode' and entrydate<'$dfrom'   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 ($res1['partcode']=='IM300858L11')
  {
   $intrqty=$res1[2]/20;   
  }
  else
  {
    $intrqty=$res1[2];    
  }
  if(mysqli_num_rows($rr3)>0)
  {
    $inrow=mysqli_fetch_array($rr3);
	$sql="update $tbl set supqty=(supqty+".$intrqty.") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl  (partcode,partname,supqty) values('$res1[0]','$res1[1]','$intrqty')";
	mysqli_query($db,$sql)or die('trin'.mysqli_error($db));
   	}
}



$sq2="select partcode,partname,sum(qty) from sparetransout where brcode='$brcode' and entrydate<'$dfrom' and jvno>0 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' and entrydate<'$dfrom' and qty>0 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 spareinvoice_oth where brcode='$brcode' and entrydate<'$dfrom' and qty>0 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 warcouponinvoicepassey  where brcode='$brcode' and entrydate<'$dfrom'  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));
}
//************


$jre=mysqli_query($db,"select jcno from jobcard where date<'$dfrom' and brcode='$brcode'")or die('Err j:'.mysqli_error($db));
//date>='$dfrom'
while($jrow=mysqli_fetch_array($jre))
{
$sq2="select partcode,partname,sum(qty) from jcardparts where brcode='$brcode' and jcno='$jrow[0]' 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' and entrydate<'$dfrom' 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));
}

$sq2r="select partcode,partname,sum(qty) from sparepurreteich where brcode='$brcode' and entrydate<'$dfrom' group by  partcode";
$re23r=mysqli_query($db,$sq2r);
while($res2r=mysqli_fetch_array($re23r))
{
$sqlr="update $tbl set supqty=(supqty-".$res2r[2].") where partcode='$res2r[partcode]'";
mysqli_query($db,$sqlr)or die('err6'.mysqli_error($db));
}
/*
//************ transout -war
$tbl1="spareinvwar".$brcode;
$sq5="select partcode,partname,sum(psnqty) from $tbl1 and entrydate<'$dfrom' 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));
}*/
//************ trans in war
/*
$sq6="select partcode,partname,sum(psnqty) from sparetrinwar where brcode='$brcode' and entrydate<'$dfrom' group by  partcode";
$re52=mysqli_query($db,$sq6);
while($res51=mysqli_fetch_array($re52))
{
$sql="update $tbl set supqty=(supqty+".$res51[2].") where partcode='$res51[partcode]'";
mysqli_query($db,$sql)or die('err10'.mysqli_error($db));
}
*/
//************ foc invoice

$sq5="select partcode,partname,sum(qty) from spareinvoicefoc where brcode='$brcode' and entrydate<'$dfrom' 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));
}

//****************************************************************************** in out*******

//*********** adj stock in
 $sq9="select partcode,partname,sum(qty) from adjstock where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by partcode";

 $re513=mysqli_query($db,$sq9)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re513))
{
 $adchk=mysqli_query($db,"select * from $tbl where partcode='$res5[0]'");
 if(mysqli_num_rows($adchk)<=0)
  $sql="insert into $tbl (partcode,partname,supqty) values('$res5[0]','".addslashes($res5[1])."','$res5[2]')"; 
 else
 $sql="update $tbl set inqty=(inqty+".$res5[2].") where partcode='$res5[partcode]'";

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

//*********** adj stock out
 $sq9="select partcode,partname,sum(outqty) from adjstock where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by partcode";

 $re513a=mysqli_query($db,$sq9)or die(mysqli_error($db));
while($res5=mysqli_fetch_array($re513a))
{
 $adchk6=mysqli_query($db,"select * from $tbl where partcode='$res5[0]'");
 $adro=mysqli_fetch_array($adchk6);
 if(mysqli_num_rows($adchk6)<=0)
  $sqla="insert into $tbl (partcode,partname,outqty) values('$res5[0]','".addslashes($res5[1])."','$res5[2]')"; 
 else if($adro['outqty']>0)
 $sqla="update $tbl set outqty=(outqty-".$res5[2].") where partcode='$res5[partcode]'";
 else
 $sqla="update $tbl set outqty=".$res5[2]." where partcode='$res5[partcode]'";

mysqli_query($db,$sqla)or die('err12adjout'.mysqli_error($db));
}

// ************* purchase *************//

$sq="select partcode,partname,sum(supqty) from sparepurchase where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by  partcode";
//and entrydate>='$dfrom' 
$rein=mysqli_query($db,$sq)or die(mysqli_error($db));


while($resin=mysqli_fetch_array($rein))
{

  $rr3in=mysqli_query($db,"select * from $tbl where partcode='$resin[partcode]'");
  if(mysqli_num_rows($rr3in)>0)
  {
    $inrow=mysqli_fetch_array($rr3in);
	$sql="update $tbl set inqty=(inqty+".$resin[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql)or die(mysqli_error($db));
  }
  else
  {
   $sql="insert into $tbl (partcode,partname,inqty) values('$resin[0]','".addslashes($resin[1])."','$resin[2]')";
   mysqli_query($db,$sql)or die(mysqli_error($db));
  }
}

		//**************8888888888888888
$sq="select partcode,partname,sum(qty) from sparetransin where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by  partcode";
//and entrydate>='$dfrom' 
$retin=mysqli_query($db,$sq)or die(mysqli_error($db));

while($resin=mysqli_fetch_array($retin))
{
  $rrt3in=mysqli_query($db,"select * from $tbl where partcode='$resin[partcode]'");
  if ($resin['partcode']=='IM300858L11')
  {
   $intrqty1=$resin[2]/20;   
  }
  else
  {
    $intrqty1=$resin[2];    
  }
  
  if(mysqli_num_rows($rrt3in)>0)
  {
    $inrow=mysqli_fetch_array($rrt3in);
	$sql="update $tbl set inqty=(inqty+".$intrqty1.") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql)or die(mysqli_error($db));
  }
  else
  {
   $sql="insert into $tbl (partcode,partname,inqty) values('$resin[0]','".addslashes($resin[1])."','$intrqty1')";
   mysqli_query($db,$sql)or die(mysqli_error($db));
  }
}


//************ trans in war
/*
$sq6="select partcode,partname,sum(psnqty) from sparetrinwar where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by  partcode";
$re521=mysqli_query($db,$sq6);
while($res5=mysqli_fetch_array($re521))
{
$sql="update $tbl set inqty=(inqty+".$res6[2].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err10'.mysqli_error($db));
}
*/
//*************888888888888 
$sqq="select partcode,partname,sum(qty) from salesreturn where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto'   group by  partcode";
$reein=mysqli_query($db,$sqq)or die('ERr retrn'.mysqli_error($db));

while($ress=mysqli_fetch_array($reein))
{
  $rr2in=mysqli_query($db,"select * from $tbl where partcode='$ress[partcode]'");
  if(mysqli_num_rows($rr2in)>0)
  {
	$inrow=mysqli_fetch_array($rr2in);
    	$sql="update $tbl set inqty=(inqty+".$ress[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql)or die(mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl  (partcode,partname,inqty) values('$ress[0]','".addslashes($ress[1])."','$ress[2]')";
	mysqli_query($db,$sql);
   	}
}


//*(************ sales

$sq2="select partcode,partname,sum(qty) from spareinvoice where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' and qty>0 group by  partcode";

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

while($res2=mysqli_fetch_array($re21sale))
{
$sql="update $tbl set outqty=(outqty+".$res2[2].") where partcode='$res2[partcode]'";
//print $sql.'<br>';
mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}
$sq2="select partcode,partname,sum(qty) from spareinvoice_oth where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' and qty>0 group by  partcode";

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

while($res2=mysqli_fetch_array($re21sale))
{
$sql="update $tbl set outqty=(outqty+".$res2[2].") where partcode='$res2[partcode]'";
//print $sql.'<br>';
mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}
//*(************ pur ret

$sq2rr="select partcode,partname,sum(qty) from sparepurreteich where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' and qty>0 group by  partcode";

$re21salerr=mysqli_query($db,$sq2rr)or die('Err3:'.mysqli_error($db));

while($res2rr=mysqli_fetch_array($re21salerr))
{
$sqlrr="update $tbl set outqty=(outqty+".$res2rr[2].") where partcode='$res2rr[partcode]'";
//print $sql.'<br>';
mysqli_query($db,$sqlrr)or die('err4'.mysqli_error($db));
}

//************ job card


$sq2="select partcode,partname,sum(qty) from jcardparts where brcode='$brcode' and date>='$dfrom' and date <='$dto'   group by  partcode";

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

while($res2=mysqli_fetch_array($re212j))
{
$sql="update $tbl set outqty=(outqty+".$res2[2].") where partcode='$res2[partcode]'";

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



//******************
$sq2="select partcode,partname,sum(qty) from sparetransout where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' and jvno>0 group by  partcode";

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


while($res2=mysqli_fetch_array($re2trout))
{
 
$sql="update $tbl set outqty=(outqty+".$res2[2].") where partcode='$res2[partcode]'";

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

$sq2="select partcode,partname,sum(qty) from warcouponinvoicepassey  where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto'  group by  partcode";

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


while($res2=mysqli_fetch_array($re2trout))
{
 
$sql="update $tbl set outqty=(outqty+".$res2[2].") where partcode='$res2[partcode]'";

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

/*
//************ transout -war
$tbl12="spareinvwar".$brcode;
$sq5="select partcode,partname,sum(psnqty) from $tbl12 where entrydate>='$dfrom' and entrydate <='$dto' and trans='n' group by  partcode";
$re51t=mysqli_query($db,$sq5);
while($res5=mysqli_fetch_array($re51t))
{
$sql="update $tbl set outqty=(outqty+".$res5[2].") where partcode='$res5[partcode]'";
mysqli_query($db,$sql)or die('err8'.mysqli_error($db));
}
*/
//****************
//************** war trans out
/*
$sq16="select partcode,partname,sum(psnqty),brcode from warsparetransout where brcode='$brcode' and date>='$dfrom' and date <='$dto' group by  partcode";
$re521=mysqli_query($db,$sq16);
while($res501=mysqli_fetch_array($re521))
{
 $cch=mysqli_query($db,"select outqty from $tbl where partcode='$res501[partcode]'");
 $cchrow=mysqli_fetch_array($cch);
 if($cchrow[0]>0)
$sql="update $tbl set outqty=(outqty-".$res501[2].") where partcode='$res501[partcode]'";
 else
$sql="update $tbl set outqty=".$res501[2]." where partcode='$res501[partcode]'";


mysqli_query($db,$sql)or die('warsp tr out -err101'.mysqli_error($db));
}*/

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

$re=mysqli_query($db,"select * from $tbl order by partcode");
print "<table border=0 cellspacing=1 cellpadding=3 bgcolor='lightsteelblue'>"; 
print "<th>No.<th>Part Code <th>Part Name<th>OP Qty <th>In Qty <th>Out Qty
<th>Closing<th>NDP <th>Amount <th>Location<th>Group<th>Subgroup";
$i=1;
$tprice=0;
while($row=mysqli_fetch_array($re))
{
    if($row['partcode']=='IM300858L11')
    {
        $supqty1=$row['supqty']*20;
        $inqty1=$row['inqty']*20;
        $outqty1=$row['outqty']*20;
        
        $close=($supqty1+$inqty1-$outqty1);
  	$tclose+=$close;
  	if($close!=0)
  	{
 	print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'><td>".$i++."</td>";
 	print "<td>$row[partcode]</td><td>$row[partname]</td><td align=right>$supqty1</td>";
	print "<td align=right bgcolor='ivory'>$inqty1</td>";
	print "<td align=right bgcolor='ivory'>$outqty1</td>";
	
 	print "<td align=right bgcolor='ivory'>$close</td>";
  	}
    }
    else
    {
        $close=($row['supqty']+$row['inqty']-$row['outqty']);
  	$tclose+=$close;
        	if($close!=0)
  	{
        print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'><td>".$i++."</td>";
 	print "<td>$row[partcode]</td><td>$row[partname]</td><td align=right>$row[supqty]</td>";
	print "<td align=right bgcolor='ivory'>$row[inqty]</td>";
	print "<td align=right bgcolor='ivory'>$row[outqty]</td>";
	
 	print "<td align=right bgcolor='ivory'>$close</td>";
  	}
    }
    
    if($close!=0)
  	{
  	    //print "select price,tot,supqty,taxtype from sparepurchase where partcode='$row[0]' and entrydate<='$dto'  and brcode='$brcode' order by entrydate desc limit 1";
	$purres=mysqli_query($db,"select price,tot,supqty,taxtype from sparepurchase where partcode='$row[0]' and entrydate<='$dto'  and brcode='$brcode' order by entrydate desc limit 1")or die('err8'.mysqli_error($db));
	/*if(mysqli_num_rows($purres)<=0)
	{
		$purres=mysqli_query($db,"select price,tot,supqty,taxtype from gen4psnauto_psn_ampleicher.sparepurchase where partcode='$row[0]' order by entrydate desc limit 1");
		print "select price,tot,supqty,taxtype from gen4psnauto_psn_ampleicher.sparepurchase where partcode='$row[0]' order by entrydate desc limit 1";
	}*/		
	if(mysqli_num_rows($purres)<=0)
	{
		$purres=mysqli_query($db,"select price,tot,supqty,taxtype from gen4psnauto_psn_ampleicher2223.sparepurchase where partcode='$row[0]' and brcode='$brcode' order by entrydate desc limit 1")or die('err9'.mysqli_error($db));
//print "select price,tot,supqty,taxtype from gen4psnauto_psn_ampleicher2223.sparepurchase where partcode='$row[0]' and brcode='$brcode' order by entrydate desc limit 1";
	}
	  /* if(mysqli_num_rows($purres)<=0)
	{
		$purres=mysqli_query($db,"select price,tot,supqty,taxtype from gen4psnauto_psn_ampleicher2223.sparepurchase where partcode='$row[0]' order by entrydate desc limit 1");

	}     */                                                      
	$purrow=mysqli_fetch_row($purres);
  	if($purrow[0]>0)
  	{
		if($purrow[3]=='CST')
		{
			$mndp=$purrow[1]/$purrow[2];
		}
		else
		{
			$mndp=$purrow[0];
		}
	  }
	  else
	  {
       $purres=mysqli_query($db,"select amount from sparetransin where partcode='$row[0]' and entrydate<='$dto' and brcode='$brcode' order by entrydate desc limit 1")or die('err11'.mysqli_error($db)); 
//print "select amount from sparetransin where partcode='$row[0]' and entrydate<='$dto' and brcode='$brcode' order by entrydate desc limit 1";
       $purrow=mysqli_fetch_row($purres);
  	    if($purrow[0]>0)
  	    {
	        $mndp=$purrow[0];     
  	    }
  	    else
  	    {
		//$prres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$row[0]' order by effectfrom desc");
		$prres=mysqli_query($db,"select ndpvalue from spareopstock where itemcode='$row[0]' ")or die('err12'.mysqli_error($db));
		//print "select ndpvalue from spareopstock where itemcode='$row[0]' ";
		$prrow=mysqli_fetch_row($prres);
		$mndp=$prrow[0];
  	    }
	  }
 	$pr=($mndp*($row['supqty']+$row['inqty']-$row['outqty']));
 	$tprice+=$pr;
 	print "<td align=right>".putComa($mndp)."</td><td align=right>".putcoma($pr)."</td>";
  	$locres=mysqli_query($db,"select location from sparelocmast where itemcode='$row[partcode]' and brcode='$brcode'");
  	$locrow=mysqli_fetch_row($locres);
	$mgrp=mysqli_query($db,"select group1,subgroup from partmaster where partcode='$row[partcode]'");
  	$mgrp1=mysqli_fetch_row($mgrp);
	print "<td>$locrow[0]</td><td>$mgrp1[0]</td><td>$mgrp1[1]</td></tr>";
//***
//	mysqli_query($db,"insert into spareopstock1 values('$row[partcode]','$row[partname]','','$brcode','$close')");
//***	
}//end close!=0
}
 print "<tr ><td colspan=6 align=center bgcolor='silver'>Total</td>";
 print "<td>$tclose</td><td  bgcolor='silver' align=right>".putComa(round($tprice,2))."</td>";
 print "<td bgcolor='silver'></td></tr>";
?>
</table>



