<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include_once('testtot.php');
$dfrom=$_REQUEST['dfrom'];
$dto=$_REQUEST['dto'];
$dfrom=implode('-', array_reverse(explode('-', $dfrom)));
$dto = implode('-', array_reverse(explode('-', $dto)));;
$it_type=$_REQUEST['it_type'];

$type=$_REQUEST['gname'];
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);

$filename ="stkrpt_grws.xls";
$contents="SlNo. \t Item_cd \t Item_name \t OP Stock \t Purchase \t Trans In \t Sales Rtn \t Stock Adj In \t Sales \t Trans Out \t Purch Rtn \t Stock Adj out \t Closing \t NDP \t Amount \t Location \n";

$tbl="sparetmpdw".$brcode.$user;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl (partcode varchar(200),partname varchar(200),op_inqty double(10,2) default 0,op_outqty double(10,2) default 0, stkadj_in double(10,2) default 0, stkadj_out double(12,2) default 0,purch_in double(12,2) default 0,salertn_in double(12,2) default 0,trans_in double(12,2) default 0,trans_out double(12,2) default 0,sale_out double(12,2) default 0,purchrtn_out double(12,2) default 0)");
mysqli_query($db,"delete from $tbl");
if($it_type=='Damaged')
{
    if($type=='All')
    {
        mysqli_query($db,"insert into $tbl (partcode,op_inqty) select itemcode,sum(dam_qty) from qtytransf where brcode='$brcode' and dataexchdt<'$dfrom' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate<'$dfrom' and itemtyp='D' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select itemcode,itemname,sum(opstk_dam) from sparestock where brcode='$brcode' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate<'$dfrom' and itemtyp='D' group by item_code") or die('err12'.mysqli_error($db));
        //****************************************************************************** in out*******
        mysqli_query($db,"insert into $tbl (partcode,trans_in) select itemcode,sum(dam_qty) from qtytransf where brcode='$brcode' and dataexchdt>='$dfrom' and dataexchdt<='$dto' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,sale_out) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate>='$dfrom' and invdate<='$dto' and itemtyp='D' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,trans_out) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and itemtyp = 'D' group by item_code") or die('err12'.mysqli_error($db));
        }
    else
    {
        mysqli_query($db,"insert into $tbl (partcode,op_inqty) select itemcode,sum(dam_qty) from qtytransf,partmaster where qtytransf.brcode='$brcode' and qtytransf.dataexchdt<'$dfrom' and partmaster.partcode= qtytransf.itemcode and partmaster.gname = '$type' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate<'$dfrom' and itemtyp='D' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select itemcode,itemname,sum(opstk_dam) from sparestock where brcode='$brcode' and item_grp = '$type' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate<'$dfrom' and gname = '$type' and itemtyp = 'D'  group by item_code") or die('err12'.mysqli_error($db));
        //****************************************************************************** in out*******
        mysqli_query($db,"insert into $tbl (partcode,trans_in) select itemcode,sum(dam_qty) from qtytransf,partmaster where qtytransf.brcode='$brcode' and qtytransf.dataexchdt<='$dto' and qtytransf.dataexchdt>='$dfrom' and partmaster.partcode= qtytransf.itemcode and partmaster.gname = '$type' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,sale_out) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate>='$dfrom' and invdate<='$dto' and itemtyp='D' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,trans_out) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and itemtyp='D' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
    }
}
else if($it_type=='Normal')
{
    if($type=="All")
    {
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty,op_outqty) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate<'$dfrom' group by partcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select itemcode,itemname,sum(opstk) from sparestock where brcode='$brcode' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate<'$dfrom' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate<'$dfrom' and itemtyp ='N' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate<'$dfrom' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,op_outqty) select a.itemcode,sum(a.nor_qty) from qtytransf a,partmaster b where a.brcode='$brcode' and a.itemcode=b.partcode and a.dataexchdt<'$dfrom' group by itemcode") or die('err12'.mysqli_error($db));
        //****************************************************************************** in out*******
        mysqli_query($db,"insert into $tbl (partcode,partname,stkadj_in,stkadj_out) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by partcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,purch_in) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,salertn_in) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,trans_out) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and itemtyp ='N' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,trans_in) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,sale_out) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,purchrtn_out) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto'  group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,trans_out) select itemcode,sum(nor_qty) from qtytransf where brcode='$brcode' and dataexchdt>='$dfrom' and dataexchdt<='$dto' group by itemcode") or die('err12'.mysqli_error($db));
     }
     else
     {
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty,op_outqty) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate<'$dfrom' and gname = '$type' group by partcode") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select itemcode,itemname,sum(opstk) from sparestock where brcode='$brcode' and item_grp = '$type' group by itemcode") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate<'$dfrom' and gname = '$type' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate<'$dfrom' and gname = '$type' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate<'$dfrom' and gname = '$type' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,op_outqty) select a.itemcode,sum(a.nor_qty) from qtytransf a,partmaster b where a.brcode='$brcode' and a.itemcode=b.partcode and a.dataexchdt<'$dfrom' and b.gname='$type' group by itemcode") or die('err12'.mysqli_error($db));
         //****************************************************************************** in out*******
         mysqli_query($db,"insert into $tbl (partcode,partname,stkadj_in,stkadj_out) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' and gname = '$type'  group by partcode") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,purch_in) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,salertn_in) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,trans_out) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,trans_in) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,sale_out) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' and itemtyp='N' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,purchrtn_out) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,trans_out) select a.itemcode,sum(a.nor_qty) from qtytransf a,partmaster b where a.brcode='$brcode' and a.itemcode=b.partcode and a.dataexchdt <='$dto' and a.dataexchdt >='$dfrom' and b.gname='$type' group by itemcode") or die('err12'.mysqli_error($db));
         }
}
else
{
    if($type=="All")
    {
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty,op_outqty) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate<'$dfrom' group by partcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select itemcode,itemname,sum(opstk) from sparestock where brcode='$brcode' group by itemcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate<'$dfrom' group by item_code") or die('err12'.mysqli_error($db));
        //****************************************************************************** in out*******
        mysqli_query($db,"insert into $tbl (partcode,partname,stkadj_in,stkadj_out) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' group by partcode") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,purch_in) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,salertn_in) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,trans_out) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,trans_in) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,sale_out) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
        mysqli_query($db,"insert into $tbl (partcode,partname,purchrtn_out) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' group by item_code") or die('err12'.mysqli_error($db));
     }
     else
     {
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty,op_outqty) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate<'$dfrom' and gname = '$type' group by partcode") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select itemcode,itemname,sum(opstk) from sparestock where brcode='$brcode' and item_grp = '$type' group by itemcode") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_inqty) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate<'$dfrom' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,op_outqty) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate<'$dfrom' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         //****************************************************************************** in out*******
         mysqli_query($db,"insert into $tbl (partcode,partname,stkadj_in,stkadj_out) select partcode,partname,sum(qty),sum(outqty) from adjstock where brcode='$brcode' and entrydate>='$dfrom' and entrydate <='$dto' and gname = '$type'  group by partcode") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,purch_in) select item_code,item,sum(qty) from itempurchase where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,salertn_in) select item_code,item,sum(rtnqty) from salesretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,trans_out) select item_code,item,sum(qty) from brtransferout where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,trans_in) select item_code,item,sum(qty) from brtransferin where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,sale_out) select item_code,item,sum(qty) from prod_invoice where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type'  group by item_code") or die('err12'.mysqli_error($db));
         mysqli_query($db,"insert into $tbl (partcode,partname,purchrtn_out) select item_code,item,sum(qty) from purchretn where brcode='$brcode' and invdate>='$dfrom' and invdate <='$dto' and gname = '$type' group by item_code") or die('err12'.mysqli_error($db));
     }
}
//**************************************
$re=mysqli_query($db,"select partcode,partname,sum(op_inqty),sum(op_outqty),sum(purch_in),sum(trans_in),sum(salertn_in),sum(stkadj_in),sum(sale_out),sum(trans_out),sum(purchrtn_out),sum(stkadj_out) from $tbl group by partcode");
$sql="select partcode,partname,sum(op_inqty),sum(op_outqty),sum(purch_in),sum(trans_in),sum(salertn_in),sum(stkadj_in),sum(sale_out),sum(trans_out),sum(purchrtn_out),sum(stkadj_out) from $tbl group by partcode";
//echo $sql;
//print "<tr><td>Item Group : </td><td>$type</td></tr>";

$i=1;
$tprice=0;
while($row=mysqli_fetch_array($re))
{
  $op_qty = $row[2]-$row[3];
  if(abs($op_qty)>0 || abs($row[4])>0 ||abs($row[5])>0 ||abs($row[6])>0 ||abs($row[7])>0 ||abs($row[8])>0 ||abs($row[9])>0 ||abs($row[10])>0 ||abs($row[11])>0)
  {
     $contents.=$i++."\t";
            $contents.=$row[0]."\t";
            $contents.=$row[1]."\t";
            $contents.=$op_qty."\t";
            $contents.=$row[4]."\t";
            $contents.=$row[5]."\t";
            $contents.=$row[6]."\t";
    $contents.=$row[7]."\t";
            $contents.=$row[8]."\t";
            $contents.=$row[9]."\t";
    $contents.=$row[10]."\t";
            $contents.=$row[11]."\t";
    $close = $op_qty+($row[4]+$row[5]+$row[6]+$row[7])-($row[8]+$row[9]+$row[10]+$row[11]);
            $contents.=$close."\t";
    $prres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$row[0]' order by effectfrom desc");
    $prrow=mysqli_fetch_row($prres);
    $pr=($prrow[0]*($close));
    $tprice+=$pr;
     $contents.=$prrow[0]."\t";
    $contents.=number_format($pr,2)."\t";
    $locres=mysqli_query($db,"select location from sparestock where itemcode='$row[partcode]' and brcode='$brcode'");
    $locrow=mysqli_fetch_row($locres);
            $answer = $locrow[0];
            $contents.=$answer."\n";

            $totkm+=$kmval;
  }
}
$contents.="\n";
$contents.="Grand Total\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";

$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.=number_format($tprice,2)."\t";
$contents = strip_tags($contents);
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
print $contents;
?>