<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

$fromdt=$_REQUEST['dfrom'];
$todt=$_REQUEST['dto'];
$type=$_REQUEST['type'];

$filename ="billwiseexcel.xls";

$contents.="\t\t\t\t\tBILLWISE REPORT \n";
$contents.="\t\t\t\tShow Report From  ".$fromdt." to ".$todt."\n";
$contents.="\t\t\t\t\tReport by: ".$type."\n";
$fromdt=implode('-', array_reverse(explode('-', $fromdt)));
$todt=implode('-', array_reverse(explode('-', $todt)));
  if($type == 'All')
  {
    $qry=mysqli_query($db,"select invno,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),customer,invdate from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invno ");
    //print "<tr><th COLSPAN=11>Billwise Report for the period from <$fromdt> to <$todt></th>";
  }
  else
  {
    $qry=mysqli_query($db,"select invno,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),customer,invdate from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname = '$type' group by invno ");
    //print "<tr><th COLSPAN=11>Billwise (Groupname : $type) Report for the period from <$fromdt> to <$todt></th>";
  }
      // print "<table class='report' border='1'>";
  $mfrom_date = $fromdt;
  $mto_date = $todt;
  $contents.="Bill No.\t Customer \t Date \t Amount \t MDA \t State Rebate Normal \t State Rebate Spl \t Reduction/Staff Disc \t Net Cash \t Tax \t Total Amount \n\n";


  $mstr[]='';
  $i=1;
  $mgname = '.';
  $mtot_total = 0;
  $mtot_mba= 0;
  $mtot_strbtnml = 0;
  $mtot_strbtspl = 0;
  $mtot_redct = 0;
  $mtot_befrtx = 0;
  $mtot_taxamt = 0;
  $mtot_nettot = 0;
  $mgr_total = 0;
  $mgr_mba= 0;
  $mgr_strbtnml = 0;
  $mgr_strbtspl = 0;
  $mgr_redct = 0;
  $mgr_befrtx = 0;
  $mgr_taxamt = 0;
  $mgr_nettot = 0;
  $ctr=0;

  while($row=mysqli_fetch_array($qry))
  {
        $rbt_tbl=mysqli_query($db,"select item_code,qty,staffdisc,total from prod_invoice where brcode = '$brcode' and invno = '$row[0]' and staffdisc > 0");
        $mstaffdisc_amt = 0;
        while($rrow=mysqli_fetch_array($rbt_tbl))
        {
           $rbt_val=mysqli_query($db,"select rbtbl,rbtvl from partmaster where partcode = '$rrow[0]'");
           $row22=mysqli_fetch_row($rbt_val);
           if($row22[0]=='Yes')
               $mstaffdisc_amt += ($row22[1]*$rrow[1])*$rrow[2]/100;
           else
           {
               $mstaffdisc_amt += $rrow[3]*$rrow[2]/100;
               $mdummy +=  $rrow[3]*$rrow[2]/100;
           }
        }

    $contents.="'".$row[0]."\t".$row[10]."\t".$row[11]."\t".number_format($row[2], 2)."\t".number_format($row[3], 2)."\t".number_format($row[4], 2)."\t".number_format($row[5], 2)."\t".number_format($mstaffdisc_amt, 2)."\t".number_format($row[7], 2)."\t".number_format($row[8], 2)."\t".number_format($row[9], 2)." \n";


    //print "<tr><td>".$row[0]."</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td><td align='right'>$row[2]</td><td align='right'>$row[3]</td><td align='right'>$row[4]</td><td align='right'>$row[5]</td><td align='right'>$row[6]</td><td align='right'>$row[7]</td><td align='right'>$row[8]</td><td align='right'>$row[9]</td></tr>";

    $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_strbtnml += $row[4];
    $mgr_strbtspl += $row[5];
    $mgr_redct += $mstaffdisc_amt;
    $mgr_befrtx += $row[7];
    $mgr_taxamt += $row[8];
    $mgr_nettot += $row[9];

    $mtot_total += $row[2];
    $mtot_mba += $row[3];
    $mtot_strbtnml += $row[4];
    $mtot_strbtspl += $row[5];
    $mtot_redct += $mstaffdisc_amt;
    $mtot_befrtx += $row[7];
    $mtot_taxamt += $row[8];
    $mtot_nettot += $row[9];
    $i++;
  }
  $contents.="\nGrand Total \t \t\t".number_format($mtot_total,2)."\t".number_format($mtot_mba, 2)."\t".number_format($mtot_strbtnml, 2)."\t".number_format($mtot_strbtspl, 2)."\t".number_format($mtot_redct, 2)."\t".number_format($mtot_befrtx, 2)."\t".number_format($mtot_taxamt, 2)."\t".number_format($mtot_nettot, 2)." \n";
$contents = strip_tags($contents);
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
print $contents;

?>