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

$tax_qry=mysqli_query($db,"select gname from groupmaster"); 
?>

<?

    $file =  'stax_salesday.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'];
	$todt=$_REQUEST['dt'];
        $type=$_REQUEST['type'];
        if ($type == 'All')
		$qry=mysqli_query($db,"select gname,sum(befrtx),sum(taxamt),sum(nettot),invno,group_concat(item),invdate,sum(total),tax from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by gname,invdate,invno");
	else
		$qry=mysqli_query($db,"select gname,sum(befrtx),sum(taxamt),sum(nettot),invno,group_concat(item),invdate,sum(total),tax from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname = '$type' group by gname,invdate,invno");
            
	//$qry=mysqli_query($db,"select gname,sum(befrtx),sum(taxamt),sum(nettot),invno,group_concat(item),invdate,sum(total),tax from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by gname,invdate,invno");
	
	$mfrom_date = implode('-', array_reverse(explode('-', $fromdt)));
	$mto_date = implode('-', array_reverse(explode('-', $todt)));
	$Data .= "              VAT Report for the period from <$mfrom_date> to <$mto_date>\n".chr(15);
  	$Data .= "|-----------------|----------|-------------------------|--------------|--------------|--------------|------------------------------|\n";
    $Data .= "|      Bill No    |   Date   |       DESCRIPTION       |     SALES    | VAT COLLECTED| TOTAL AMOUNT |          ITEM DETAILS        |\n";                                    
    $Data .= "|-----------------|----------|-------------------------|--------------|--------------|--------------|------------------------------|\n";
    //margin <- 3 less 3 
	//print "<tr><th COLSPAN=11>MONTHLY VAT REGISTER FOR THE PERIOD FROM <$mfrom_date> TO <$mto_date></th>";
	//print "<tr><th>BillNo</th><th>Date</th><th>Descripton</th><th>Sales</th><th>VAT Collected</th><th>Total Amount</th><th>Item Details</th>";
    $colspan=6;
	$mstr[]='';
	$i=1;
	$mgname1 = '.';
	$mtot_befrtx = 0;
	$mtot_taxamt = 0;
	$mtot_nettot = 0;
	$mgr_befrtx = 0;
	$mgr_taxamt = 0;
	$mgr_nettot = 0;
	$ctr=0;
    $ctr_flg = 0;
	$mbill_to = "";
    $minv_date = ".";
    $mgrp_befrtx = 0;
    $mgrp_taxamt = 0;
    $mgrp_nettot = 0;
	$mline_ctr = 1;
	while($row=mysqli_fetch_array($qry))
  	{  
        if($ctr_flg++==0)
            $mbill_frm = $row[4];
		if($mgname1 <> $row[0])
		{
			if($mgname1 <> '.')
			{	
                $mdate = implode('-', array_reverse(explode('-', $minv_date)));
                $mgrp_befrtx += $mtot_befrtx;
                $mgrp_taxamt += $mtot_taxamt;
                $mgrp_nettot += $mtot_nettot;
                $mbill = "$mbill_frm - $mbill_to";
                $Data .= "|".substr($mbill.str_repeat(" ",17),0,17);
                $Data .= "|".substr($mdate.str_repeat(" ",10),0,10);
                $Data .= "|".substr($mgname1.str_repeat(" ",25),0,25);
                $mnum = number_format($mtot_befrtx, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mtot_taxamt, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mtot_nettot, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $Data .= "|".substr($mdesc.str_repeat(" ",30),0,30)."|\n";					
 
                //print "<tr><td><b>$mbill_frm - $mbill_to</td><td>$mdate</td><td><b>$mgname1</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><b>$mdesc</td></tr>";                
				$mtot_befrtx = 0;
				$mtot_taxamt = 0;
				$mtot_nettot = 0;                
                $mbill_frm = $row[4];
                $mgr_befrtx += $mgrp_befrtx;
                $mgr_taxamt += $mgrp_taxamt;
                $mgr_nettot += $mgrp_nettot;
                $Data .= "|------------------------------------------------------|--------------|--------------|--------------|------------------------------|\n";
                $Data .= "|                  ".substr($mgname1." TOTAL".str_repeat(" ",25),0,25);
                $mnum = number_format($mgrp_befrtx, 2, '.', '');
                $Data .= "           |".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mgrp_taxamt, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mgrp_nettot, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $Data .= "|".str_repeat(" ",30)."|\n";
                $Data .= "|------------------------------------------------------|--------------|--------------|--------------|------------------------------|\n";                	                
                //print "<tr><td><b>$mgname1 Total</td><td></td><td><b></td><td align='right'><b>".number_format($mgrp_befrtx,2,'.',',')."</td><td align='right'><b>".number_format($mgrp_taxamt,2,'.',',')."</td><td align='right'><b>".number_format($mgrp_nettot,2,'.',',')."</td><td><b>$mdesc</td></tr>";
                $mgrp_befrtx = 0;
                $mgrp_taxamt = 0;
                $mgrp_nettot = 0;	
                //print "<tr><td><b>$mgname1 Total</td><td></td><td><b></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><b>$mdesc</td></tr>";
                $minv_date = $row[6];        				
            }
			$mgname1 = $row[0];
		}
        if($minv_date <> $row[6])
		{
			if($minv_date <> '.')
			{				     
                $mdate = implode('-', array_reverse(explode('-', $minv_date)));
                $mgrp_befrtx += $mtot_befrtx;
                $mgrp_taxamt += $mtot_taxamt;
                $mgrp_nettot += $mtot_nettot;
                $mbill = "$mbill_frm - $mbill_to";
                $Data .= "|".substr($mbill.str_repeat(" ",17),0,17);
                $Data .= "|".substr($mdate.str_repeat(" ",10),0,10);
                $Data .= "|".substr($mgname1.str_repeat(" ",25),0,25);
                $mnum = number_format($mtot_befrtx, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mtot_taxamt, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mtot_nettot, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $Data .= "|".substr($mdesc.str_repeat(" ",30),0,30)."|\n";
				//print "<tr><td><b>$mbill_frm - $mbill_to</td><td><b>$mdate</td><td><b>$mgname1</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><b>$mdesc</td></tr>";	
				$mtot_befrtx = 0;
				$mtot_taxamt = 0;
				$mtot_nettot = 0;                
                $mbill_frm = $row[4];			
            }
			$minv_date = $row[6];
		}
        if($row[8]>0)
            $mtot_befrtx += $row[1];
        else
            $mtot_befrtx += $row[7];
        $mtot_taxamt += $row[2];
        $mtot_nettot += $row[3];
        $mdesc = $row[5];           		
        $mbill_to = $row[4];
        $minv_date = $row[6];
		$i++;
		if ($mline_ctr>38)
			{	
        
		$mline_ctr = 1;
		$Data .= "|-----------------|----------|-------------------------|--------------|--------------|--------------|------------------------------|\n";
		$Data .= "\n\n\n\n";
		$Data .= chr(18);
		$Data .= "				KERALA KHADI & VILLAGE INDUSTRIES BOARD\n";
		$Data .= "              VAT Report for the period from <$mfrom_date> to <$mto_date>\n".chr(15);
  		$Data .= "|-----------------|----------|-------------------------|--------------|--------------|--------------|------------------------------|\n";
		$Data .= "|      Bill No    |   Date   |       DESCRIPTION       |     SALES    | VAT COLLECTED| TOTAL AMOUNT |          ITEM DETAILS        |\n";  
		$Data .= "|-----------------|----------|-------------------------|--------------|--------------|--------------|------------------------------|\n";
		
			}
		$mline_ctr++;
	
	}
    $mgrp_befrtx += $mtot_befrtx;
    $mgrp_taxamt += $mtot_taxamt;
    $mgrp_nettot += $mtot_nettot;
    $mgr_befrtx += $mgrp_befrtx;
    $mgr_taxamt += $mgrp_taxamt;
    $mgr_nettot += $mgrp_nettot;
    $mdate = implode('-', array_reverse(explode('-', $minv_date)));
    $mbill = "$mbill_frm - $mbill_to";
    $Data .= "|".substr($mbill.str_repeat(" ",17),0,17);
    $Data .= "|".substr($mdate.str_repeat(" ",10),0,10);
    $Data .= "|".substr($mgname1.str_repeat(" ",25),0,25);
    $mnum = number_format($mtot_befrtx, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
    $mnum = number_format($mtot_taxamt, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
    $mnum = number_format($mtot_nettot, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
    $Data .= "|".substr($mdesc.str_repeat(" ",30),0,30)."|\n";
    //print "<tr><td><b>$mbill_frm - $mbill_to</td><td><b>$mdate</td><td><b>$mgname1</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><b>$mdesc</td></tr>";
    $Data .= "|------------------------------------------------------|--------------|--------------|--------------|------------------------------|\n";
                $Data .= "|                  ".substr($mgname1." TOTAL".str_repeat(" ",25),0,25);
                $mnum = number_format($mgrp_befrtx, 2, '.', '');
                $Data .= "           |".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mgrp_taxamt, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mgrp_nettot, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $Data .= "|".str_repeat(" ",30)."|\n";	          
    $Data .= "|------------------------------------------------------|--------------|--------------|--------------|------------------------------|\n";
    //print "<tr><td><b>$mgname1 Total</td><td></td><td><b></td><td align='right'><b>".number_format($mgrp_befrtx,2,'.',',')."</td><td align='right'><b>".number_format($mgrp_taxamt,2,'.',',')."</td><td align='right'><b>".number_format($mgrp_nettot,2,'.',',')."</td><td><b>$mdesc</td></tr>";
                $Data .= "|                 ".substr(" T O T A L".str_repeat(" ",25),0,25);
                $mnum = number_format($mgr_befrtx, 2, '.', '');
                $Data .= "            |".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mgr_taxamt, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $mnum = number_format($mgr_nettot, 2, '.', '');
                $Data .= "|".substr(str_repeat(" ",14).$mnum,-14);
                $Data .= "|".str_repeat(" ",30)."|\n";
    $Data .= "|------------------------------------------------------|--------------|--------------|--------------|------------------------------|\n";    
    //print "<tr><td colspan=3 align='center'><b>T O T A L</td><td align='right'><b>".number_format($mgr_befrtx,2,'.',',')."</td><td align='right'><b>".number_format($mgr_taxamt,2,'.',',')."</td><td align='right'><b>".number_format($mgr_nettot,2,'.',',')."</td><td></td></tr>";			
	//print "</table>";
    $Data .= chr(18);
	$Data .= chr(12);
	fwrite($handle, $Data);
	fclose($handle);
	$output = shell_exec("lp -o media=Custom.10x6in '$file'");
        //$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c staxday.bat");
    header('location:stax_salesday.php');
?>
