<?
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'];
$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");
$row=mysqli_fetch_row($qry);
$qry1=mysqli_query($db,"select brname from branch where brcode = '$brcode'");
$row1=mysqli_fetch_row($qry1);
$brname = $row1[0];
$mtot_total = $row[2];

$qry3=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 salesretn where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname='$type' group by gname");
$row3=mysqli_fetch_row($qry3);
$mtot_total3 = $row3[2];

if(($redtype=='mda')or ($redtype=='MDA'))
{
  $mtot_rebt3 = $row3[3];
}
if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
  $mtot_rebt3 = $row3[4];
}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
  $mtot_rebt3 = $row3[5];
}

$file =  'reb_printcertprn.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.chr(15);

if(($redtype=='mda')or ($redtype=='MDA'))
{
  $mtot_rebt = $row[3];
}
if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
  $mtot_rebt = $row[4];
}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
  $mtot_rebt = $row[5];
}
$Data .="\n\n\n\n\n\n";
$mnum = number_format($mtot_total, 2, '.', '');
$mstr  = substr(str_repeat(" ",17).$mnum,-16);
$mnum1 = number_format($mtot_total3, 2, '.', '');
$mstr1  = substr(str_repeat(" ",17).$mnum1,-16);
$mnum2 = number_format(($mtot_total-$mtot_total3), 2, '.', '');
$mstr2  = substr(str_repeat(" ",17).$mnum2,-16);

$mnum= number_format($mtot_rebt, 2, '.', '');
$mstr3= substr(str_repeat(" ",17).$mnum,-16);
$mnum1= number_format($mtot_rebt3, 2, '.', '');
$mstr4 = substr(str_repeat(" ",17).$mnum1,-16);
$mnum2= number_format(($mtot_rebt-$mtot_rebt3), 2, '.', '');
$mstr5 = substr(str_repeat(" ",17).$mnum2,-16);


$Data .="            Total Sales    : ".$mstr."                        Rebate      : ".$mstr3."\n";
$Data .="            Sales Returned : ".$mstr1."                        Rebates     : ".$mstr4."\n";
$Data .="            Net Sales      : ".$mstr2."                        Net Rebates : ".$mstr5."\n\n\n";

$Data .="                                                         CERTIFICATE \n\n\n";

$Data .="                  Certified that the duplicate copies of the cash memos mentioned in the centers statements\n";
$Data .="            enclosed with the claim are kept in record with use and will be made available to the staff of the\n";
$Data .="            commission or the officers of the Goverment audit department for checking whenever demanded. \n\n\n";

$Data .="                  Certified that the sales for which the rebate has been claimed do not included the wholesales\n";
$Data .="            of sales or goods returned or sales or goods on which rebate is not admissible or sales of government\n";
$Data .="            departments. \n\n\n";

$Data .="                  Certified that all the centres for which claims have been made in this statement are approved \n";
$Data .="            branches of this institution. \n\n\n";

$Data .="                  Certified that claims are made strictly according to the books of account of this institution \n";
$Data .="            and that the amount of the claim has not been claimed and received previously and that in the event \n";
$Data .="            of any discrepency found during inspection or audit, this institution will be liable to repay such \n";
$Data .="            amount to the commission promptly and rebates subsidy claimed above have been paid at the rates and \n";
$Data .="            according to the scales and conditions prescribed in the sanctions of the each scheme. \n\n\n";

$Data .="            Place : ".$brname."\n\n";
$Data .="            Date  : ".date('d-m-Y')."\n\n";
$Data .="                (Rubber stamp of the institution) \n\n";
$Data .="                                                                    Signature of Manager \n\n";
$Data .="            *(Strike which is not applicable) Net Rebate amount for payment Rs. ..................... Entered in \n";
$Data .="            the Ledger Folio No.\n\n\n";
$Data .="                CHECKING CLERK \n\n\n";
$Data .="              Superintendent \n\n\n";
$Data .="              Khadi & VI Commission \n\n\n";
$Data .="              Trivandum \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 reb_printcertprn.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>