<?
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;
}

//$date = new DateTime('$df');
//$mfrom_date = $date->format('d-m-Y');
$df=$_REQUEST['df'];
$dt=$_REQUEST['dt'];
$mfrom_date = $df;
$mto_date  = $dt;
$fromdt = implode('-', array_reverse(explode('-', $df)));
$todt = implode('-', array_reverse(explode('-', $dt)));
$file =  'sale_summ.prn';
$handle = fopen($file, 'w');
$dss=chr(27).chr(71).chr(N);
$dse=chr(27).chr(72);
$initialized = chr(27).chr(64);
$load = chr(27).chr(25).chr(66);
$bold = chr(27).chr(14);
$unbold = chr(20);
$pagelenth=chr(27).chr(67).chr(36);
$Data = $initialized;

$Data .= "   SALES SUMMARY REPORT FROM $mfrom_date TO  $mto_date\n".chr(15);
$Data .= "|--------------------|------------|------------|------------|------------|------------|------------|---------|------------|-----------|\n";
$Data .= "| Group Name         |   Amount   |    MDA     |State Rebate|State Rebate| Reduction  |  Net Cash  |  Tax    |   Total    |  Debtors  |\n";
$Data .= "|                    |            |            |    Normal  |   Special  |            |            |         |            |           |\n";
$Data .= "|--------------------|------------|------------|------------|------------|------------|------------|---------|------------|-----------|\n";
//<table>
//<tr><td >SALES SUMMARY REPORT from

$tbl='sales_summ'.$brcode.$user;
//$qry=mysqli_query($db,"select * from $tbl");
$qry=mysqli_query($db,"select gname,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),invno,pmode from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by gname");

//print "<table class='report' border=1>";
//print "<tr><th>Group Name</th><th>Amount</th><th>MDA</th><th>State Rebate Normal</th><th>State Rebate Spl</th><th>Reduction</th><th>Net Cash</th><th>Tax</th><th>Total Amount</th><th>Debtors</th>";
$mtot_total = 0;
$mtot_mba= 0;
$mtot_strbtnml = 0;
$mtot_strbtspl = 0;
$mtot_redct = 0;
$mtot_befrtx = 0;
$mtot_taxamt = 0;
$mtot_nettot = 0;
$mtot_dbtr = 0;
while($row=mysqli_fetch_array($qry))
{
//  $Data .= "|                    |            |            |    Normal  |   Special  |            |            |         |            |           |\n";
  $Data .= "|".substr($row[0].str_repeat(" ",20),0,20);
  $mnum = number_format($row[1], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($row[2], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($row[3], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($row[4], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($row[5], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($row[6], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($row[7], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",9).$mnum,-9);
  $qry1=mysqli_query($db,"select sum(nettot) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname = '$row[0]' and pmode = 'CREDIT' ");
  $row1=mysqli_fetch_array($qry1);
  $mdbt_amt = $row1[0];
  $mnum = number_format($row[8]-$mdbt_amt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
  $mnum = number_format($mdbt_amt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",12).$mnum,-11)."|\n";
  //print "<tr><td>$row[0]</td><td align='right'>$row[1]</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>";
  $mtot_total += $row[1];
  $mtot_mba += $row[2];
  $mtot_strbtnml += $row[3];
  $mtot_strbtspl += $row[4];
  $mtot_redct += $row[5];
  $mtot_befrtx += $row[6];
  $mtot_taxamt += $row[7];
  $mtot_nettot += ($row[8]-$mdbt_amt);
  $mtot_dbtr += $mdbt_amt;
}
$Data .= "|--------------------|------------|------------|------------|------------|------------|------------|---------|------------|-----------|\n";
$Data .= "|     GRAND TOTAL    ";
$mnum = number_format($mtot_total, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_mba, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_strbtnml, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_strbtspl, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_redct, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_befrtx, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_taxamt, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",9).$mnum,-9);
$mnum = number_format($mtot_nettot, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
$mnum = number_format($mtot_dbtr, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",12).$mnum,-11)."|\n";
$Data .= "|--------------------|------------|------------|------------|------------|------------|------------|---------|------------|-----------|\n";
//print "<tr><td><b>Grand Total</td><td align='right'><b>".number_format($mtot_total, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_mba, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_strbtnml, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_strbtspl, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_redct, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_befrtx, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_taxamt, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_nettot, 2, '.', '')."</td><td align='right'><b>".number_format($mtot_dbtr, 2, '.', '')."</td></tr>";
$Data .= chr(18);
$Data .= chr(12);
fwrite($handle, $Data);
fclose($handle);
//print "lp -o media=Custom.10x6in $file";
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c sale_summ.bat");
$fromdt = implode('-', array_reverse(explode('-', $fromdt)));
$todt = implode('-', array_reverse(explode('-', $todt)));
header('location:sale_summ.php?fromdt='.$fromdt.'&&todt='.$todt);
?>