<?
if($show)
{
 $dfrom=$y1.'-'.$m1.'-'.$d1;
 $dto=$y2.'-'.$m2.'-'.$d2;
session_start();
include("opendb.php");
include("test.php");
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);



?>

<link rel="stylesheet" href="style1.css" type="text/css" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<style>
	//tr1{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<body>
<?php include('includes/top.php'); ?>

<center>


<table>
<TR><TD COLSPAN=2 ALIGN=CENTER>
<?
$qry1=mysqli_query($db,"select * from company_master where brcode='$brcode'");
 
 $row1=mysqli_fetch_array($qry1);
print "<font size='4'><b>".$row1[2]."</b></font><br>".$row1[4]."<br>".$row1[5]."<br>".$row1[6];
if(($row1[7]>0)&&($row1[7]!=''))
    print "<br>Tel: ".$row1[7];
 if(($row1[8]>0)&&($row1[8]!=''))
   print "<br> Fax: ".$row1[8];
?>
</td></tr>
</TABLE>
<?
print "<B>SPARE TRANSACTION REPORT </B> <BR> From ".date('d-m-Y',strtotime($dfrom))." To ".date('d-m-Y',strtotime($dto)) ;
?>

<center>
<table border=1 class='report' style='border-collapse:collapse;'>
<th>Partcode<th>Partname<th>Op. Stock<th>Purchase<th>Sales<th>Service<th>Trans IN<th>Trans OUT<th>Sales Ret<th>Purch Ret<th>Warr Sale<th>Warr Sale Ret<th>Stk Adj(IN)<th>Stk Adj(OUT)<th>Closing Stock
<?
$tbl="stockrepdet".$brcode.$user;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl (partcode varchar(200),partname varchar(200),op double(10,2)default 0,purch double(10,2)default 0,serv double(10,2)default 0,sale double(10,2)default 0,transin double(10,2)default 0,transout double(10,2)default 0,sal_ret double(10,2)default 0,pur_ret double(10,2)default 0,war_sal double(10,2)default 0,war_sal_ret double(10,2)default 0, adj_in double(10,2)default 0,adj_out double(10,2)default 0,opin double(10,2)default 0,opout double(10,2)default 0,brcode varchar(20),grp varchar(100),state varchar(50),dto date)");
mysqli_query($db,"delete from $tbl");





mysqli_query($db,"insert into $tbl (partcode,partname,op) select itemcode as partcode,itemname as partname,sum(qty)op from spareopstock where brcode='$brcode' group by  itemcode");

//Opening Calculation
mysqli_query($db,"insert into $tbl (partcode,partname,opin) select pcode,pname sum(qty) from lubricant_purmast	 where brcode='$brcode' and prdate<'$dfrom' group by  pcode");
mysqli_query($db,"insert into $tbl (partcode,partname,opout) select partcode,partname,sum(qty)  from jcardparts where brcode='$brcode' and edate<'$dfrom' group by  partcode");

//Transactions


mysqli_query($db,"insert into $tbl (partcode,partname,purch) select pcode,pname,sum(qty)purch from lubricant_purmast where brcode='$brcode' and prdate>='$dfrom' and prdate<='$dto' group by  pcode");
mysqli_query($db,"insert into $tbl (partcode,partname,serv) select partcode,partname,sum(qty)serv from jcardparts where brcode='$brcode' and edate>='$dfrom' and edate<='$dto' group by  partcode");
mysqli_query($db,"update $tbl set dto='$dto',grp='Spares'");

$trantbl=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),sum(serv) from $tbl group by partcode") or die("ERR:".mysqli_error($db));
while($tran=mysqli_fetch_array($trantbl))
{
	$mop = $tran[2]+$tran[13]-$tran[14];
	$mcl = $mop+$tran[3]-$tran[4]+$tran[5]-$tran[6]+$tran[7]-$tran[8]-$tran[9]+$tran[10]+$tran[11]-$tran[12]-$tran[15];
	print "<tr><td>$tran[0]</td><td>$tran[1]</td><td align=right>$mop</td><td align=right>$tran[3]</td><td align=right>$tran[4]</td><td align=right>$tran[15]</td><td align=right>$tran[5]</td><td align=right>$tran[6]</td><td align=right>$tran[7]</td><td align=right>$tran[8]</td><td align=right>$tran[9]</td><td align=right>$tran[10]</td><td align=right>$tran[11]</td><td align=right>$tran[12]</td><td align=right>$mcl</td></tr>";
}
?>
</table>
//print "<script> window.open('spares/stockrepoval.php'); </script>";

<?
}
else
echo 'oops!...out of page';
?>
