<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$type=$_REQUEST['type'];
$grp=$_REQUEST['grp'];
$subgrp=$_REQUEST['subgrp'];
$dfrom=$_REQUEST['dfrom'];
$dto=$_REQUEST['dto'];
include("opendb.php");
include("test.php");
if(strlen($user)<=0)
{
    print "<script> alert('Invalid User'); document.location='inde.php' </script>";
    return;
}
  $mfrom_date = $dfrom;
  $mto_date = $dto;
  $fromdt = implode('-', array_reverse(explode('-', $dfrom)));
  $todt = implode('-', array_reverse(explode('-', $dto)));
  $mfr_dt = $fromdt;
  $mto_dt = $todt;

if($type=='All')
    $msup = "";
  else
    $msup = " and customer = '$type' ";
  if($grp=='All')
    $mgrp = "";
  else
    $mgrp = " and prod_invoice.gname = '$grp' ";
  if($subgrp=='All')
    $qry=mysqli_query($db,"select item_code,item,sum(qty),sum(total) from prod_invoice where invdate between '$mfr_dt' and '$mto_dt' and brcode='$brcode' ".$msup.$mgrp." group by item_code");
  else
  {
    $qry=mysqli_query($db,"select item_code,item,sum(qty),sum(total) from prod_invoice,partmaster where invdate between '$mfr_dt' and '$mto_dt' and prod_invoice.brcode='$brcode' ".$msup.$mgrp." and prod_invoice.item_code=partmaster.partcode and partmaster.subgroup = '$subgrp' group by item_code");
  }



//$qry=mysqli_query($db,"select item_code,item,sum(qty),sum(total) from prodinvoicewholesale where invdate between '$mfr_dt' and '$mto_dt' and brcode='$brcode' ".$msup.$mgrp." group by item_code");
      $fromdt=implode('-', array_reverse(explode('-', $dfrom)));
  $todt=implode('-', array_reverse(explode('-', $dto)));

  $filename ="who_qtysl.xls";
  $contents.="\t\t\t\t\t\t\tKERALA KHADI & VILLAGE INDUSTRIES BOARD\n";
  $contents.="\t\t\t\t\t\t\t\tQuantity wise Sales Report for the period from ".$mfrom_date." to ".$mto_date."\n";
  $contents.="Item Code\t Item \tQty \t Rate \t Value \n\n";
$i=1;
  $mtot_total = 0;
    while($row=mysqli_fetch_array($qry))
    {
    $contents.=$row[0]."\t".$row[1]."\t".$row[2]."\t".round(($row[3]/$row[2]),2)."\t".$row[3]."\n";

    $mgr_total += $row[3];
    $i++;
  }

  $contents.="Grand Total \t\t\t\t".$mgr_total."\n";
  $contents = strip_tags($contents);
  header('Content-type: application/ms-excel');
  header('Content-Disposition: attachment; filename='.$filename);
  print $contents;

?>