<?
include_once('includes/top.php');
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;
}
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body>
<title>Sales Register</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-latest.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form.css" rel="stylesheet" />
<script type="text/javascript">
$(function() {
if ($.browser.msie && $.browser.version.substr(0,1)<7)
{
  $('li').has('ul').mouseover(function(){
  $(this).children('ul').show();
  }).mouseout(function(){
  $(this).children('ul').hide();
  })
        }
});
</script>
<script>
function valid()
{
}
</script>
<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;
}
</style>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
new JsDatePick({
  useMode:2,
  target:"inputField",
  dateFormat:"%Y-%m-%d"
});
new JsDatePick({
  useMode:2,
  target:"inputField1",
  dateFormat:"%Y-%m-%d"
});

};
</script>

<SCRIPT LANGUAGE="JavaScript" SRC="util/PopupWindow.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/AnchorPosition.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/date.js"></SCRIPT>
<script>

var cal1 = new CalendarPopup();
cal1.showYearNavigation();

var cal2 = new CalendarPopup();
cal2.showYearNavigation();

</script>
<style>
th{
    // background-color: grey;
    font-weight: bold;
    color: white;
    border:1px solid #D7D7D1;
    border-collapse:collapse;
background: rgb(181,189,200); /* Old browsers */
background: -moz-linear-gradient(top, rgba(181,189,200,1) 0%, rgba(130,140,149,1) 36%, rgba(40,52,59,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(181,189,200,1)), color-stop(36%,rgba(130,140,149,1)), color-stop(100%,rgba(40,52,59,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b5bdc8', endColorstr='#28343b',GradientType=0 ); /* IE6-9 */
}
.ro_settled td
{
background: #6bba70; /* Old browsers */
background: -moz-linear-gradient(top, #6bba70 0%, #6bba70 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6bba70), color-stop(100%,#6bba70)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* IE10+ */
background: linear-gradient(to bottom, #6bba70 0%,#6bba70 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='#6bba70',GradientType=0 ); /* IE6-9 */
color:white;
}
.ro_settl td
{
background: #6bba70; /* Old browsers */
background: -moz-linear-gradient(top, #6bba70 0%, #6bba70 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6bba70), color-stop(100%,#6bba70)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* IE10+ */
background: linear-gradient(to bottom, #6bba70 0%,#6bba70 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='#6bba70',GradientType=0 ); /* IE6-9 */
color:red;
}
</style>
<body background="back_ground.jpg">
<?php //include('logo2.php'); ?>
<?php //include('includes/top.php'); ?>
<form action='<?print $PHP_SELF?>' method='post' >
<br>
<table class="container"><tr><td>
<br>
<center>
<?
$tax_qry=mysqli_query($db,"select gname from groupmaster");
$file =  'stax_sales.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 & VILLAGE INDUSTRIES BOARD\n";

  $fromdt=$_REQUEST['df'];
  $fromdt=implode('-', array_reverse(explode('-', $fromdt)));

  $todt=$_REQUEST['dt'];
  $todt=implode('-', array_reverse(explode('-', $todt)));
  $type=$_REQUEST['type'];
  $tbl='sales_summ'.$brcode.$user;
  mysqli_query($db,"delete from $tbl");
  mysqli_query($db,"create table $tbl(gname varchar(50),total double(14,2),mba double(14,2),strbtnml double(14,2),strbtspl double(14,2),redct double(14,2),befrtx double(14,2),taxamt double(14,2),nettot double(14,2),dbttot double(14,2))");
  if ($type == 'All')
    $qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),invno,pmode,tax,customer,group_concat(item) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by gname,invdate,invno");
  else
    $qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),invno,pmode,tax,customer,group_concat(item) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname = '$type' group by tax,invdate,invno");
  //print "<table class='report' border='1'>";
  $mfrom_date = implode('-', array_reverse(explode('-', $fromdt)));
  $mto_date = implode('-', array_reverse(explode('-', $todt)));
  //print "<tr><th COLSPAN=11>Sales Register for the period from <$mfrom_date> to <$mto_date></th>";

  $Data .= "      Sales Register for the period from <$mfrom_date> to <$mto_date>\n".chr(15);
    $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
  $Data .= "  |          BillNo           |       Date    |   Net Cash    |    Tax     | Total Amount |          Item Description            |\n";
  //print "<tr><th>BillNo</th><th>Date</th><th>Net Cash</th><th>Tax</th><th>Total Amount</th><th>Item Description</th>";
    $colspan=6;
  $mstr[]='';
  $i=1;
  $mgname1 = '.';
  $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;
  $mgr_dbtrs=0; $mgr_dbtrs1=0;
  $mline_ctr = 0;
  while($row=mysqli_fetch_array($qry))
    {
    if($mgname1 <> $row[0])
    {
      if($mgname1 <> '.')
      {
        $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
        $mstr = "$mtax% Tax Total ($mgname1)";
        $Data .= "  |".substr($mstr.str_repeat(" ",27),0,27)."|               ";
        $mnum = number_format($mgr_befrtx, 2, '.', '');
        $Data .= "|".substr(str_repeat(" ",15).$mnum,-15);
        $mnum = number_format($mgr_taxamt, 2, '.', '');
        $Data .= "|".substr(str_repeat(" ",14).$mnum,-12);
        $mnum = number_format($mgr_nettot, 2, '.', '');
        $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|                                      |\n";
//print "<tr><td><b>$mtax% Tax Total ($mgname1)</td><td></td><td align='right'>".$mgr_befrtx."</td><td align='right'>".$mgr_taxamt."</td><td align='right'>".$mgr_nettot."</td></tr>";

        mysqli_query($db,"insert into $tbl (gname,total,mba,strbtnml,strbtspl,redct,befrtx,taxamt,nettot,dbttot) values ('$mgname',$mgr_total,$mgr_mba,$mgr_strbtnml,$mgr_strbtspl,$mgr_redct,$mgr_befrtx,$mgr_taxamt,$mgr_nettot,$mgr_dbtrs)") or die(mysqli_error($db));              $mgr_total = 0;
        $mgr_mba= 0;
        $mgr_strbtnml = 0;
        $mgr_strbtspl = 0;
        $mgr_redct = 0;
        $mgr_befrtx = 0;
        $mgr_taxamt = 0;
        $mgr_nettot = 0;
        $mgr_dbtrs=0;
      }
        $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
        $mstr = "$row[12].% Tax ($row[0])";
        $Data .= "  |".substr($mstr.str_repeat(" ",27),0,27)."|               |               |            |              |                                      |\n";
        $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
        //print "<tr><td><b>".$row[12]."% Tax ($row[0]) </td><td></td><td></td><td></td><td></td><td></td></tr>";
      $mtax = $row[12];
      $mgname1 = $row[0];
    }

    $date = $row[1];

  $dbtrs=0;
  $cshvl=$row[9];
  $mtot_nettot += $row[9];
  $mgr_dbtrs+=$dbtrs;
  $mgr_dbtrs1+=$dbtrs;
    $dt=implode('-', array_reverse(explode('-', $row[1])));
    if($row[8]==0)
    {
      $mstr = "$row[10]";
      $Data .= "  |".substr($mstr.str_repeat(" ",27),0,27);
      $date1 = date_create($dt);
      $Data .= "|  ".substr(date_format($date1, 'd-m-Y').str_repeat(" ",15),0,13);
      $mnum = number_format($row[2], 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",15).$mnum,-15);
      $mnum = number_format($row[8], 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",14).$mnum,-12);
      $mnum = number_format($row[2], 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
      $Data .= "|".substr($row[14].str_repeat(" ",38),0,38)."|\n";
      //print "<tr><td>".$row[10]."</td><td>$dt</td><td align='right'>$row[2]</td><td align='right'>$row[8]</td><td align='right'>$row[2]</td><td>$row[14]</td></tr>";
      $mgr_befrtx += $row[2];
      $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_strbtnml += $row[4];
    $mgr_strbtspl += $row[5];
    $mgr_redct += $row[6];

    $mgr_taxamt += $row[8];
    $mgr_nettot += $row[2];
    $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];
    }
    else
    {
      $mstr = "$row[10]";
      $Data .= "  |".substr($mstr.str_repeat(" ",27),0,27);
      $date1 = date_create($dt);
      $Data .= "|  ".substr(date_format($date1, 'd-m-Y').str_repeat(" ",15),0,13);
      $mnum = number_format($row[7], 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",15).$mnum,-15);
      $mnum = number_format($row[8], 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",14).$mnum,-12);
      $mnum = number_format($cshvl, 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
      $Data .= "|".substr($row[14].str_repeat(" ",38),0,38)."|\n";

      //print "<tr><td>".$row[10]."</td><td>$dt</td><td align='right'>$row[7]</td><td align='right'>$row[8]</td><td align='right'>$cshvl</td><td>$row[14]</td></tr>";
      $mgr_befrtx += $row[7];
      $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_strbtnml += $row[4];
    $mgr_strbtspl += $row[5];
    $mgr_redct += $row[6];

    $mgr_taxamt += $row[8];
    $mgr_nettot += $cshvl;
    $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];
    }


    $i++;
    if ($mline_ctr>46)
      {

    $mline_ctr = 0;
    $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
    $Data .= chr(18);
    $Data .= chr(12);
    $Data .= "\n";
    $Data .= "              KERALA KHADI & VILLAGE INDUSTRIES BOARD\n";
    $Data .= "      Sales Register for the period from <$mfrom_date> to <$mto_date>\n".chr(15);
      $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
    $Data .= "  |          BillNo           |       Date    |   Net Cash    |    Tax     | Total Amount |          Item Description            |\n";
    $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";

      }
    $mline_ctr++;
  }
  $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
  $mstr = "$mtax% Tax Total ($mgname1)";
  $Data .= "  |".substr($mstr.str_repeat(" ",27),0,27)."|               ";
  $mnum = number_format($mgr_befrtx, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",15).$mnum,-15);
  $mnum = number_format($mgr_taxamt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-12);
  $mnum = number_format($mgr_nettot, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",14).$mnum,-14)."|                                      |\n";
  $Data .= "  |---------------------------|---------------|---------------|------------|--------------|--------------------------------------|\n";
  //print "<tr><td><b>$mtax Tax Total ($mgname1)</td><td></td><td align='right'>".$mgr_befrtx."</td><td align='right'>".$mgr_taxamt."</td><td align='right'>".$mgr_nettot."</td></tr>";
  mysqli_query($db,"insert into $tbl (gname,total,mba,strbtnml,strbtspl,redct,befrtx,taxamt,nettot,dbttot) values ('$mgname',$mgr_total,$mgr_mba,$mgr_strbtnml,$mgr_strbtspl,$mgr_redct,$mgr_befrtx,$mgr_taxamt,$mgr_nettot,$mgr_dbtrs)") or die(mysqli_error($db));
//  print "<tr><td><b>Grand Total</td><td></td><td></td><td align='right'><b>".$mtot_befrtx."</td><td align='right'><b>".$mtot_taxamt."</td><td align='right'><b>".$mtot_nettot."</td></tr>";
  //print "</table>";
  $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 stax_sls.bat");
  $fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));
  header('location:stax_sales.php?fromdt='.$fromdt.'&&todt='.$todt);
?>