<?
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;
}
$brqry="select brname from branch where brcode='$brcode'";
$brrs=mysqli_query($db,$brqry);
$brrw=mysqli_fetch_array($brrs);
$brnchnm=$brrw[0];
?>

<?

$fromdt=$_REQUEST['dfrom'];
$todt=$_REQUEST['dto'];
$type=$_REQUEST['type'];
$redtype=$_REQUEST['redtype'];
$fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));

$qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),min(invno),max(invno) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname='$type' group by gname,invdate");
    $file =  'rebt_prn.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 .= "                 KERALA KHADI AND VILLAGE INDUSTRIES BOARD \n";
  $Data .= "                              FORM NO. 1 \n";
  $Data .= "                         MONTHLY REBATE STATEMENT\n";
  $Data .= "                     CENTER NAME:  $brnchnm\n";
  $Data .= chr(15);
  $Data ." \n\n";
  $Data .= "|--------------------|-----------|-----------|--------------|--------------|--------------|--------------|--------------|-------------|\n";
  $Data .= "|         Date       | From Bill |  To Bill  |    Amount    |     MDA      | State Rebate | State Rebate |    Total     |   Remarks   |\n";
  $Data .= "|                    |           |           |              |              |    Normal    |    Special   |              |             |\n";
  $Data .= "|--------------------|-----------|-----------|--------------|--------------|--------------|--------------|--------------|-------------|\n";

        echo $redtype;

        $mfrom_date = $fromdt;
  $mto_date = $todt;
        $colspan=8;
  $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))
    {
    if($mgname <> $row[0])
    {
      if($mgname <> '.')
      {


                      $mgr_total = 0;
        $mgr_mba= 0;
        $mgr_strbtnml = 0;
        $mgr_strbtspl = 0;
        $mgr_redct = 0;
        $mgr_befrtx = 0;
        $mgr_taxamt = 0;
        $mgr_nettot = 0;
      }

        $Data .= "|".substr($row[0].str_repeat(" ",20),0,20);
        $Data .= "|           |           |              |              |              |              |              |             |\n";


$mgname = $row[0];
    }

$date = $row[1];

if(($redtype=='mda')or ($redtype=='MDA'))


{
  $date1 = date_create($date);
  $Data .= "|".substr(date_format($date1, 'd-m-Y').str_repeat(" ",20),0,20);
  $Data .= "|".substr(str_repeat(" ",12).$row[10],-11);
  $Data .= "|".substr(str_repeat(" ",12).$row[11],-11);
  $mnum = number_format($row[2], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
  $mnum = number_format($row[3], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
  $Data .= "|              |              ";
  $mnum = number_format($row[3], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|             |\n";


}

if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
$date1 = date_create($date);
  $Data .= "|".substr(date_format($date1, 'd-m-Y').str_repeat(" ",20),0,20);
  $Data .= "|".substr(str_repeat(" ",12).$row[10],-11);
  $Data .= "|".substr(str_repeat(" ",12).$row[11],-11);
  $mnum = number_format($row[2], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|              ";
  $mnum = number_format($row[4], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
  $Data .= "|              ";
  $mnum = number_format($row[4], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|             |\n";


}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
$date1 = date_create($date);
  $Data .= "|".substr(date_format($date1, 'd-m-Y').str_repeat(" ",20),0,20);
  $Data .= "|".substr(str_repeat(" ",12).$row[10],-11);
  $Data .= "|".substr(str_repeat(" ",12).$row[11],-11);
  $mnum = number_format($row[2], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|              |              ";
  $mnum = number_format($row[5], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
  $mnum = number_format($row[5], 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|             |\n";


}
    $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_strbtnml += $row[4];
    $mgr_strbtspl += $row[5];
    $mgr_redct += $row[6];
    $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 += $row[6];
    $mtot_befrtx += $row[7];
    $mtot_taxamt += $row[8];
    $mtot_nettot += $row[9];
    $i++;
  }


if(($redtype=='mda')or ($redtype=='MDA'))

{
  $Data .= "|--------------------|-----------|-----------|--------------|--------------|--------------|--------------|--------------|-------------|\n";
  $Data .= "|    GRAND TOTAL     |           |           ";
  $mnum = number_format($mtot_total, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
  $mnum = number_format($mtot_mba, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|              |              ";
  $mnum = number_format($mtot_mba, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|             |\n";


}

if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
  $Data .= "|--------------------|-----------|-----------|--------------|--------------|--------------|--------------|--------------|-------------|\n";
  $Data .= "|    GRAND TOTAL     |           |           ";
  $mnum = number_format($mtot_total, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|              ";
  $mnum = number_format($mtot_strbtnml, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|              ";
  $mnum = number_format($mtot_strbtnml, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|             |\n";


}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
  $Data .= "|--------------------|-----------|-----------|--------------|--------------|--------------|--------------|--------------|-------------|\n";
  $Data .= "|    GRAND TOTAL     |           |           ";
  $mnum = number_format($mtot_total, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|              |              ";
  $mnum = number_format($mtot_strbtspl, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
  $mnum = number_format($mtot_strbtspl, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|             |\n";

}
  $Data .= "|--------------------|-----------|-----------|--------------|--------------|--------------|--------------|--------------|-------------|\n";
  $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 rebt_prn.bat");
  //header('location:rebt_stmt.php');

  ?>

 <script>
<?$fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));?>
            document.location="rebt_stmt.php?fromdt=<? echo $fromdt ?>&&todt=<? echo $todt ?>&&type=<? echo $type ?>&&redtype=<? echo $redtype ?>";
            </script>