<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include("opendb.php");
include("test.php");
$thirty1=$_REQUEST['thirty1'];
$sixty1=$_REQUEST['sixty1'];
$ninty1=$_REQUEST['ninty1'];
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
$tbl="stockagingbr".$brcode.$user;
$tbl2="stockrepvalbr".$brcode.$user;
$tbl_pur="purchasedetbr".$brcode.$user;
$r=mysqli_query($db,"select dto from $tbl");
$rw=mysqli_fetch_array($r);
$dtt=date("Y-m-d");
$dtt1=date("Y-m-d");
$next_year = mktime(0,0,0,date("m"),date("d"),date("Y")+1);
?>
 <link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >
    <?
print "<center> <font color='red'> <b>".getbrname($brcode)."</b> </font><br><font size=7><b><i><font color=#23D21D> Stock</font></I> </b><font size=6>Valuation as on ".datetohtml($rw[dto])."' ";
print "<table border=1><th>Date <th>Part Code <th>Part Name <th>Grp<th>Qty<th>Price<th>Total";

mysqli_query($db,"CREATE TABLE $tbl2 (partcode varchar(200),partname varchar(200),entrydate date,supqty double(10,2),price double(10,2))");

mysqli_query($db,"delete from $tbl2");
$re=mysqli_query($db,"select partcode,partname,sum(op),sum(purch),sum(sale),sum(transin),sum(transout),sum(sal_ret),sum(pur_ret),sum(war_sal),sum(war_sal_ret),sum(adj_in),sum(adj_out),sum(opin),sum(opout),dto,sum(serv) from $tbl group by partcode") or die("ERR:".mysqli_error($db));

while($row=mysqli_fetch_array($re))
{
	$t='stk';
	$mop = $row[2]+$row[13]-$row[14];
	$no=$mop+$row[3]-$row[4]+$row[5]-$row[6]+$row[7]-$row[8]-$row[9]+$row[10]+$row[11]-$row[12]-$row[16];
	//	$no=$row[supqty]+$row[inqty]-$row[outqty];
//	$sql="select entrydate,supqty,aprice,disc,hand,tax,taxtype from $tbl_pur where partcode='$row[partcode]' and brcode='$brcode' and entrydate<='$row[dto]' order by entrydate desc";
//	$chk=mysqli_query($db,$sql)or die(mysqli_error($db));
//	if(mysqli_num_rows($chk)<=0)
//	{
//			$sql="select entrydate,supqty,aprice,disc,hand,tax,taxtype from $tbl_pur where partcode='$row[partcode]'  and entrydate<='$row[dto]' order by entrydate desc";
//			$chk=mysqli_query($db,$sql)or die(mysqli_error($db));
				$t='stk';
//	}
/*	if ($no>0)
	{
		while($row1=mysqli_fetch_array($chk))
		{
			$pcode=$row[partcode];
			$pname=htmlspecialchars(addslashes($row[partname]));
			$dt=$row1[entrydate];
			$pr=$row1[2]/$row1[1];
			$qt=$row1[1];
			if($row1[taxtype]=='CST')
			{
				$tx=($pr*$row1[tax]/100);
				$pr+=$tx;
			}
			if($no>0)
			{ 
				if($qt>$no)
				{
					$qt = $no;
				}
				$sq="insert into $tbl2 values('$pcode','$pname','$dt', '$qt','$pr')";
				mysqli_query($db,$sq) or die('err2'.mysqli_error($db));
			}
			else
			{	
				break;
			}
			$no-=$qt;
		}
*/
		if($no>0)
		{ 
		 	$sql="select entrydate,supqty,aprice,disc,hand,tax,taxtype from $tbl_pur where partcode='$row[partcode]' order by entrydate desc";
		  	$chk=mysqli_query($db,$sql)or die(mysqli_error($db));
			
		 	if(mysqli_num_rows($chk)<=0)
		 	{
				if ($brcode=='PSN026')
					$sql="select effectfrom,ndpcost from spareprmast where partcode='$row[partcode]' and brcode='PSN026' order by effectfrom desc limit 1";
				else
					$sql="select effectfrom,ndpcost from spareprmast where partcode='$row[partcode]' and brcode!='PSN026' order by effectfrom desc limit 1";
				$chk=mysqli_query($db,$sql)or die(mysqli_error($db));
				$t='pr';
		 	}
			$row1=mysqli_fetch_array($chk);
	     		if($t=='stk')
	    		{	
				$pcode=$row[partcode];
				$pname=htmlspecialchars(addslashes($row[partname]));
				$dt=$row1[entrydate];
				//$dis=$row1[[price]*$row1[disc]
				//$am1=$row1[price]-$dis
				$pr=$row1[2]/$row1[1];
				if($row1[hand]>0)
				{
					$hnd=($pr*$row1[hand]/100);
					$pr+=$hnd;
				}
				if($row1[taxtype]=='CST')
				{
					$tx=($pr*$row1[tax]/100);
					$pr+=$tx;
				}
				$qt=$row1[1];
			}
			else
			{
				$pcode=$row[partcode];
				$pname=htmlspecialchars(addslashes($row[partname]));
				$dt=$row1[effectfrom];
				$pr=$row1[1];
				$qt=$row[supqty];
			}
			if($t=='pr' && $no>$row1[1])
			{ 
	      		//	$no-=$qt;
	     		}
			else if($no>0)
	     		{ 
			//      $no-=$no;
			}
			$pcode=$row[partcode];
			$pname=htmlspecialchars(addslashes($row[partname]));
			$mpart=mysqli_query($db,$sql);
			$row1=mysqli_fetch_array($mpart);
			if($dt<='')
			    $dt = '0000-00-00';
			else
			{
			    $sq="insert into $tbl2 values('$pcode','$pname','$dt', '$no','$pr')";
			//print $sq."<br>";
			    mysqli_query($db,$sq) or die('err2'.mysqli_error($db));
			}
		}
	}

$next_year = mktime(0,0,0,date("m"),date("d"),date("Y")+1);
$tbl_sal="saledet".$brcode.$user;

$re2=mysqli_query($db,"select $tbl2.*,sum(price*supqty)/sum(supqty),sum(supqty),group1 from $tbl2,partmaster where partmaster.partcode = $tbl2.partcode group by $tbl2.partcode");

while($row=mysqli_fetch_array($re2))
{
	//if($row[group1]=='AC DELCO')
	//{
		$x=$row[5]*$row[6];
		print "<tr><td>".datetohtml($row[entrydate])."</td><td>$row[partcode]</td><td>$row[partname]</td><td>$row[group1]</td>";
		print "<td align=right>$row[6]</td><td align=right>".round($row[5],2)."</td><td align=right>$x</td><td></td></tr>";
		$tq+=$row[6];
		$totx+=$x;
//	}
}
print "<tr><td colspan=4><center><b>Total</b></td><td align=right><b>$tq</td><td></td>";
print "<td align=right><b>".putComa($totx)."</td>";
?>


</table>

<script> window.open('ageingnewbr.php?thirty2=<?print $thirty1 ?>&sixty2=<?print $sixty1 ?>&ninty2=<?print $ninty1 ?>&dd=<?print $rw[dto] ?>'); </script>