<? session_start();
if(strlen($brcode)<=0)
{
 	print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 	return;
}

include("../opendb.php");
include("../test.php");
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:white} 
th{background-color:c1d799;font-size:13}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<form action='<?print $PHP_SELF?>'>
<table bgcolor='silver'>
<th colspan=2>DATE WISE STOCK REPORT
<tr><td> Date  </td><td>dd<input type=text name='d' size=2 value='<?print date("d",time())?>'>
				mm<input type=text name='m' size=2 value='<?print date("m",time())?>'>
				yy<input type=text name='y' size=4 value='<?print date("Y",time())?>'>
			</td></tr>
<tr><td colspan=2 align=center><input type=submit name='show' value='Show'>
<input type=submit name='prn' value='Print'>
</td></tr>
</table>
</form>

<center>
<?
if($prn)
{
   $df=$y.'-'.$m.'-'.$d;
   $dt=$y1.'-'.$m1.'-'.$d1;
       print "<script> window.open('prndwstkrepov.php?df=$df');</script>";
}

if($show)
{
	$adr='tmpkk';
	$adr.=str_replace('.','',$REMOTE_ADDR);
	$df=$y.'-'.$m.'-'.$d;
	print "<font size=3><b>Stock report for the day ".datetohtml($df)."</b>";
	$sq="delete from ".$adr;

	mysqli_query($db,$sq);
	$sql="create table ".$adr." (da varchar(100),config varchar(250),mcode varchar(100), engno varchar(200),";
	$sql.="chno varchar(100),pdate date, pvalue double(14,2), pinv varchar(100),rem varchar(20), loc varchar(200))";
	mysqli_query($db,$sql);
	$re=mysqli_query($db,"select dano,config,mcode,engno,chno,pdate,pval,pinv,loc from vehopstock where brcode='$brcode' order by pdate");
	while($row=mysqli_fetch_row($re))
	{
		$sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','OP','$row[8]')";
		// print $sq;
		mysqli_query($db,$sq) or die(mysqli_error());
	}
	$re=mysqli_query($db,"select da,config,mcode,engno,chasis,entrydate,amt2,invno,loc,type,taxtype,netamt from veh_purchase
	where  brcode='$brcode' and oldornew='-' and entrydate <='$df'  order by entrydate")or die(mysqli_error());
	$i=1;
	while($row=mysqli_fetch_row($re))
	{
		if ($row[10]=='CST')
		{
			$sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[11]','$row[7]','$row[9] ','$row[8]')";
			print $i++.$sq."<br>";
			mysqli_query($db,$sq) or die(mysqli_error());
		}
		else
		{
			$sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[9] ','$row[8]')";
			//print $i++.$sq."<br>";
			 mysqli_query($db,$sq) or die(mysqli_error());
		}
	}
	//*****************  sales return 
	$sql="select * from veh_salereturn where date<='$df' and brcode='$brcode' ";
	$result=mysqli_query($db,$sql)or die(mysqli_error());
	while($row=mysqli_fetch_array($result))
	{
	   	$chres=mysqli_query($db,"select engno,chno,loc,pinv,pval,pdate from vehopstock where dano='$row[da]' and brcode='$brcode' ");
	  	if(mysqli_num_rows($chres)>0)
	  	{ 
	  		$chrow=mysqli_fetch_array($chres);
	 		mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','$chrow[0]','$chrow[1]','$chrow[pdate]','$chrow[pval]','$chrow[pinv]','','$chrow[2]')")or die('err:'.mysqli_error());
	 	}
	  	else
	 	{
			$chres1=mysqli_query($db,"select engno,chasis,loc,invno,entrydate,amt2,taxtype,netamt from veh_purchase where da='$row[da]' and brcode='$brcode'");
			if(mysqli_num_rows($chres1)>0)
	  		{ 
	  			$chrow1=mysqli_fetch_array($chres1);
	  			if($chrow1[taxtype]=='CST')
	      			{
	       			mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','$chrow1[0]','$chrow1[1]','$chrow1[entrydate]','$chrow1[netamt]','$chrow1[invno]','','$chrow1[2]')")or die('err:'.mysqli_error());					
	      			}
	   			else
	      			{
	       			mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','$chrow1[0]','$chrow1[1]','$chrow1[entrydate]','$chrow1[amt2]','$chrow1[invno]','','$chrow1[2]')")or die('err:'.mysqli_error());
	      			}
	  		}
		}
	}

	//*****************  purchase return 
	$sql="select * from veh_purchreturn where date<='$df' and brcode='$brcode' ";
	$result=mysqli_query($db,$sql)or die(mysqli_error());
	while($row=mysqli_fetch_array($result))
	{
		mysqli_query($db,"delete from ".$adr." where da='$row[da]' limit 1 ")or die(mysqli_error());
	}

	//**********
	$re=mysqli_query($db,"select da from veh_invoice where brcode='$brcode' and invdate<='$df'  order by invdate");
	$i=1;
	print "<table cellspacing=1 cellpadding=2 bgcolor=gray>";
	//print "<th>No<th>Error<th>DA  <th>Purchase Date<th>Saleno <th>Saledate ";
	while($row=mysqli_fetch_row($re))
	{
		$r=mysqli_query($db,"select da from ".$adr." where da='$row[0]' ");
		if(mysqli_num_rows($r)>0)
		{
			$sql="delete from ".$adr." where da='$row[0]' limit 1";
			mysqli_query($db,$sql)or die(mysqli_error());
		}
		else
		{
			$rek=mysqli_query($db,"select * from veh_purchase where da='$row[0]' and brcode='$brcode' ");
			$rekk=mysqli_fetch_array($rek);
			$rek1=mysqli_query($db,"select * from veh_invoice where da='$row[0]' and brcode='$brcode' ");
			$rekk1=mysqli_fetch_array($rek1);
			//print "<tr><td align=center>".$i++."</td><td>Purchase Error</td><td>$row[0]</td><td>".datetohtml($rekk[entrydate])."</td><td>$rekk1[invno]</td><td>".datetohtml($rekk1[invdate])."</td></tr>";
		}
	}
	print "</table>";
	$re=mysqli_query($db,"select da from veh_transout where brcode='$brcode' and invdate<='$df'  order by invdate");
	$i=1;
	while($row=mysqli_fetch_row($re))
	{
	// $sql="delete from ".$adr." where da='$row[0]' limit 1";
	// mysqli_query($db,$sql);
	}

//***********88888888888888888*****************************
//print $df;

	$sq="select * from ".$adr;
	$res=mysqli_query($db,$sq);
	if(mysqli_num_rows($res)>0)
	{
 		print "<table cellspacing=1 cellpadding=2 bgcolor=gray>";
	 	print "<th>No<th>DA  <th>Config<th>M.Code <th>Eng NO <th>Ch No <th>Pur Date<th> P.Value <th>Pur Inv. <th>Rem <th>Loc<th>Age";
	 	$i=1;
		$t=0;
		while($row=mysqli_fetch_array($res))
		{
			$ag=(strtotime($df)-strtotime($row[pdate]))/86400;
			$lres=mysqli_query($db,"select lname from loc_master where code='$row[9]'");
			$lorow=mysqli_fetch_row($lres);
			print "<tr><td align=center>".$i++."</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>";
	  		print "<td>$row[4]</td><td>".datetohtml($row[5])."</td><td align=right>$row[6]</td><td>$row[7]</td><td align=center>$row[8]</td><td>$lorow[0]</td><td>$ag</td>";
	 		$t+=$row[6];
	 	}
		print "<tr><td colspan=7><center><b>Total</td><td align=right><b>$t</td><td colspan=3></td></tr>";
		print "</table>";
	}
} // end if show
?>