<?
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>
PAN: <?print $brrow['tin']?>, GST: <?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="sparetmpdwjan24".$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,"CREATE INDEX partcode on $tbl(partcode)");

mysqli_query($db,"delete from $tbl");

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

//*********** adj stock in
 $sq9="select partcode,partname,sum(qty) from adjstock where brcode='$brcode' and entrydate>='2023-04-01'  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>='2023-04-01' 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]')"; 
print $sql;
mysqli_query($db,$sql)or die('err12'.mysqli_error($db));
}

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

//****************spare purchase 

$sq="select partcode,partname,sum(supqty) from sparepurchase where brcode='$brcode' and entrydate>='2023-04-01' 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]'")or die('spur4'.mysqli_error($db));
 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]'";
 print "purop".$sql;
  mysqli_query($db,$sql)or die('pur '.mysqli_error($db));
}

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

//***************  opening stock 
$sqq="select itemcode,itemname,sum(qty) from spareopstock where brcode='$brcode' group by  itemcode order by itemcode desc";
$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]'")or die('spstk9'.mysqli_error($db));
  if(mysqli_num_rows($rr)>0)
  {
	$inrow=mysqli_fetch_array($rr);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow[partcode]'";
    	print "opstupd".$sql;
	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]')";
//print "opstins".$sql;
	mysqli_query($db,$sql)or die('opstk'.mysqli_error($db));
   	}
}

//*************************************************************************
//***************   sales return
$sqq="select partcode,partname,sum(qty) from salesretnew where brcode='$brcode' and entrydate>='2023-04-01' and entrydate<'$dfrom'   group by  partcode";
$ree=mysqli_query($db,$sqq)or die('ERr retrn'.mysqli_error($db));

while($ress=mysqli_fetch_array($ree))
{
    $retqty=$ress[2]*-1;
  $rr2=mysqli_query($db,"select * from $tbl where partcode='$ress[partcode]'")or die('spret8'.mysqli_error($db));
  if(mysqli_num_rows($rr2)>0)
  {
	$inrow=mysqli_fetch_array($rr2);
    	$sql="update $tbl set supqty=(supqty+".$retqty.") where partcode='$inrow[partcode]'";
    	print "saleretu".$sql;
	mysqli_query($db,$sql)or die('sret'.mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl  (partcode,partname,supqty) values('$ress[0]','".addslashes($ress[1])."','$retqty')";
	print "saleretins".$sql;
	mysqli_query($db,$sql)or die('sret'.mysqli_error($db));
   	}
}

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

//********************  trans in
$sq1="select partcode,partname,sum(qty) from sparetransin where brcode='$brcode' and entrydate>='2023-04-01' and entrydate<'$dfrom'   group by  partcode";
$re1=mysqli_query($db,$sq1)or die('spin5'.mysqli_error($db));

while($res1=mysqli_fetch_array($re1))
{
  $rr3=mysqli_query($db,"select * from $tbl where partcode='$res1[partcode]'")or die('spin88'.mysqli_error($db));
  if ($res1['partcode']=='IM300858L')
  {
   $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]'";
	print "trfrinupd".$sql;
	mysqli_query($db,$sql)or die('spin22'.mysqli_error($db));
  }
  else
   	{
	$sql="insert into $tbl  (partcode,partname,supqty) values('$res1[0]','$res1[1]','$intrqty')";
	print "TRFRINins".$sql;
	mysqli_query($db,$sql)or die('trin'.mysqli_error($db));
   	}
}

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

//*********************  trans out

$sq2="select partcode,partname,sum(qty) from sparetransout where brcode='$brcode' and entrydate>='2023-04-01' and entrydate<'$dfrom' and jvno>0 group by  partcode";

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


while($res2=mysqli_fetch_array($re2))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
print "troutupd".$sql;
mysqli_query($db,$sql)or die('err2spo'.mysqli_error($db));
}

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

//********************* cs
$sq2="select partcode,partname,sum(qty) from spareinvoice where brcode='$brcode' and entrydate>='2023-04-01' 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]'";
print "csupd".$sql;
mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}
$sq2="select partcode,partname,sum(qty) from spareinvoice_oth where brcode='$brcode' and entrydate>='2023-04-01' 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]'";
print "csoupd".$sql;
mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}

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

/* lock for stock

$sq2="select partcode,partname,sum(qty) from warcouponinvoicepassey  where brcode='$brcode' and entrydate>='2023-04-01' 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));
}
//************
*/
//*******************  jobcard
$jre=mysqli_query($db,"select distinct(invno) from jobcard where date<'$dfrom'  and date>='2023-04-01' and brcode='$brcode' and cname not like '%ASSURANT%'")or die('Err j:'.mysqli_error($db));
//print "select jcno from jobcard where date<'$dfrom'  and date>='2023-04-01' and brcode='$brcode'";
//date>='$dfrom'
while($jrow=mysqli_fetch_array($jre))
{
$sq2="select partcode,partname,sum(qty) from jcardparts where brcode='$brcode' and jcno='$jrow[0]' and invno='$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]'";
print "jcupd".$sql;
mysqli_query($db,$sql)or die('err4'.mysqli_error($db));
}

}

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

//***************** purchase return

$sq2="select partcode,partname,sum(retqty) from sparepurreturn where brcode='$brcode' and entrydate>='2023-04-01' and entrydate<'$dfrom' group by  partcode";
$re23=mysqli_query($db,$sq2)or die('err4s'.mysqli_error($db));
while($res2=mysqli_fetch_array($re23))
{
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";
print "pretupd".$sql;
mysqli_query($db,$sql)or die('err6'.mysqli_error($db));
}

$sq2r="select partcode,partname,sum(qty) from sparepurreteich where brcode='$brcode' and entrydate>='2023-04-01' and entrydate<'$dfrom' group by  partcode";
$re23r=mysqli_query($db,$sq2r)or die('spretei77'.mysqli_error($db));
while($res2r=mysqli_fetch_array($re23r))
{
$sqlr="update $tbl set supqty=(supqty-".$res2r[2].") where partcode='$res2r[partcode]'";
print "pretei".$sqlr;
mysqli_query($db,$sqlr)or die('err6'.mysqli_error($db));
}


print "opening stock calculation";

//opening stock calculation end *****************************************


//*********** 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]'")or die('adj78'.mysqli_error($db));
 $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));
  }
}

		//**************trans in
//************************************************************************

$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']=='IM300858L1')
  {
   $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));
  }
}
//*************************************************************************


//*************sales return 
$sqq="select partcode,partname,sum(qty) from salesretnew 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))
{
    $retqty=$ress[2]*-1;
  $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+".$retqty.") 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])."','$retqty')";
	mysqli_query($db,$sql)or die('sret34'.mysqli_error($db));
   	}
}

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

//*(************ cs

$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('err4cdc'.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('Err31:'.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('err41'.mysqli_error($db));
}

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

$jre=mysqli_query($db,"select distinct(invno) from jobcard where  date>='$dfrom'  and date <='$dto' and brcode='$brcode' and cname not like 'ASSURANT%'")or die('Err j:'.mysqli_error($db));
//print "select jcno from jobcard where date<'$dfrom'  and date>='2023-04-01' and brcode='$brcode'";
//date>='$dfrom'
while($jrow=mysqli_fetch_array($jre))
{
$sq2="select partcode,partname,sum(qty) from jcardparts where brcode='$brcode' and date>='$dfrom' and date <='$dto'  and invno='$jrow[0]'  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('err4ee'.mysqli_error($db));
}
}
//*******************************************************************

//****************** trans out
$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('err2vff'.mysqli_error($db));
}

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

/* lock for stock
$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));
}
*/


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

$re=mysqli_query($db,"select * from $tbl order by partcode")or die('print33'.mysqli_error($db));
print "<table border=1 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>Weightd <br>Avg cost<th>Total";
$i=1;
$tprice=0;
$tprice1=0;
while($row=mysqli_fetch_array($re))
{
    if($row['partcode']=='IM300858L1')
    {
        $supqty1=$row['supqty']*20;
        $inqty1=$row['inqty']*20;
        $outqty1=$row['outqty']*20;
        print $outqty1;
        $close=($supqty1+$inqty1-$outqty1);
        $close2=($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']);
        $close2=($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>='2023-04-01' 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 entrydate>='2023-04-01' 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");

	}     */          
	$mndp = 0;
        $mndp1 = 0;
        //$bal_qty=0;
	$purrow=mysqli_fetch_row($purres);
  	if($purrow[0]>0)
  	{
		if($purrow[3]=='CST')
		{
			$mndp=$purrow[1]/$purrow[2];
		}
		else
		{
			$mndp=$purrow[0];
		}
	  }
	  else
	  {
       $purres11=mysqli_query($db,"select amount from sparetransin where partcode='$row[0]' and entrydate>='2023-04-01' 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";
       $purrow11=mysqli_fetch_row($purres11);
  	    if($purrow11[0]>0)
  	    {
	        $mndp=$purrow11[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>";
 	
 	$mavg_cost = 0;
	     
	    $bal_qty = $close;
	    //print "select price,tot,supqty,taxtype from sparepurchase where partcode='$row[0]' and brcode='$brcode' and entrydate<='$dto' and entrydate>='2023-04-01' order by entrydate desc";
	    
	    $purres=mysqli_query($db,"select price,tot,supqty,taxtype from sparepurchase where partcode='$row[0]' and brcode='$brcode' and entrydate<='$dto' and entrydate>='2023-04-01' order by entrydate desc")or die('sppurr88'.mysqli_error($db));
        while($purrow=mysqli_fetch_array($purres))
        {
  	        if($purrow[0]>0)
  	        {
		        if($purrow[3]=='CST')
		        {
    			    $mndp1=$purrow[1]/$purrow[2];
		        }
		        else
		        {
		            if ($row[0]=='IM300836L'||  $row[0]=='IM300851L' ||$row[0]=='IM300904L')
		            {
		             $mndp1=$purrow[0]/11;   
		            }
		            else if ($row[0]=='IM300858L')
		            {
		             $mndp1=$purrow[0]/20;   
		            }
		            else
		            {
		              $mndp1=$purrow[0];  
		            }
    			    
		        }
	        }
	      /*  else
	  {
       $purres112=mysqli_query($db,"select amount from sparetransin where partcode='$row[0]' and entrydate>='2023-04-01' 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";
       $purrow112=mysqli_fetch_row($purres112);
  	    if($purrow112[0]>0)
  	    {
	        $mndp1=$purrow112[0];     
  	    }
	  }*/
	        //print $row[0];
	       // print $mndp1;
	        if($purrow[2]<=$bal_qty)
	            $mavg_cost += ($mndp1 * $purrow[2]);
	        else
	            $mavg_cost += ($mndp1 * $bal_qty);
	        $bal_qty = $bal_qty - $purrow[2];
	        //print $row[0];
	       //print $bal_qty;
	        
	        if($bal_qty<=0)
	            break;
        }
           
        if($bal_qty>0)
        {
            $prres=mysqli_query($db,"select ndpvalue from spareopstock where itemcode='$row[0]' and brcode = '$brcode'")or die('spop987'.mysqli_error($db));
		    $prrow=mysqli_fetch_row($prres);
		    $mndp1=$prrow[0];
		    if($mndp1<=0)
		    {
		        //print $row[0];
		        /*$prres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$row[0]' order by effectfrom desc");
		        $prrow=mysqli_fetch_row($prres);
		        $mndp1=$prrow[0];*/
		        
		        $purres112=mysqli_query($db,"select amount from sparetransin where partcode='$row[0]' and entrydate>='2023-04-01' 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";
       $purrow112=mysqli_fetch_row($purres112);
  	    if($purrow112[0]>0)
  	    {
	        $mndp1=$purrow112[0];     
  	    }
  	    
		    }
		        $mavg_cost += ($mndp1 * $bal_qty);
		    
        }
         //if($close>0)
         //print $row[0];
        // print $bal_qty;
         //print $close;
         
         /*if($bal_qty>0)
        $wght_pr = round($mavg_cost/$bal_qty,2);
        else
        $wght_pr=0;
        $mflgno='';
        */
        
        if($close2>0)
        $wght_pr = round($mavg_cost/$close2,2);
        else
        $wght_pr=0;
        $mflgno='';
        
        /*if(substr($row[0],0,2)=='LP' or substr($row[0],0,5)== '33333')
        {
            
            $prres=mysqli_query($db,"select price from localpartsprice where partcode='$row[0]' limit 1");
		    $prrow=mysqli_fetch_row($prres);
		    if($prrow[0]>0)
		        $mndp=$prrow[0];
		    else
		        $mflgno=' *';
        }*/
  
        $pr=($mndp*($close2));
        $pr1=($wght_pr*$close2);
        $tprice+=$pr;
        $tprice1+=$pr1;
        print "<td align=right>".number_format($wght_pr,2)."</td><td align=right>".number_format($pr1,2)." $mflgno</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>";*/
	print "</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>";
 print "<td></td><td  bgcolor='silver' align=right>".number_format($tprice1,2)."</td>";

 print "<td bgcolor='silver'></td></tr>";
?>
</table>



