<?
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;
}
include_once('testword.php');
$user=$_SESSION['user'];
$savecount=$_REQUEST['savecount'];
$invno=$_REQUEST['invno'];
$re=mysqli_query($db,"select * from prod_invoice where invno='$invno' and brcode='$brcode'")or die('err:'.mysqli_error($db));
$row=mysqli_fetch_array($re)or die("Fetch Error:".mysqli_error($db));
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$file =  'bill.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 .=$pagelenth;
$Data .= "\n\n\n\n";
$Data .=  "                                                                     ".$invno."\n";
$Data .=  "                                                                    ".$row[invdate]."\n\n";
$Data .=  "                         ".$row[customer];
$re1=mysqli_query($db,"select * from customer where cname ='$row[customer]' and brcode='$brcode'")or die('err:'.mysqli_error($db));
$row1=mysqli_fetch_array($re1);
$Data .= ", ".$row1[addr1];
if($row1[tin]>'')
	$Data .= ' TIN:'.$row[tin]."\n\n\n\n\n";
else
	$Data .= "\n\n\n\n\n";
$Data .= chr(15);
$sql="select * from prod_invoice where invno='$invno' and brcode='$brcode'";
$re1=mysqli_query($db,$sql)or die('err:'.mysqli_error($db));
$i=1;
$tq=0;
$tp=0;
$cnt=1;
$mtot_total = 0;
$mtot_redct = 0;
$mtot_mba = 0;
$mtot_strbtspl = 0;
$mtot_strbtnml = 0;
$mtot_befrtx = 0;
$mtot_taxamt = 0;
$mtot_nettot = 0;
$mline_ctr = 1;
while($row1=mysqli_fetch_array($re1))
{
	$mitem_code = $row1[item_code].str_repeat(" ",20);
        $mitem = $row1[item].str_repeat(" ",50);
	$mtax =  $row1[tax].str_repeat(" ",10);
	$munitprice = str_repeat(" ",10).$row1[unitprice];
	$mqty = str_repeat(" ",10).$row1[qty];
	$mtotal = str_repeat(" ",10).$row1[total];
	$mredct = str_repeat(" ",10).$row1[redct];
	$mmba = str_repeat(" ",10).$row1[mba];
	$mstrbtspl = str_repeat(" ",10).$row1[strbtspl];
	$mstrbtnml = str_repeat(" ",10).$row1[strbtnml];
	$mbefrtx = str_repeat(" ",10).$row1[befrtx];
	$mtaxamt = str_repeat(" ",10).$row1[taxamt];
	$mnettot = str_repeat(" ",10).$row1[nettot];

	$mtot_total += $row1[total];
	$mtot_redct += $row1[redct];
	$mtot_mba += $row1[mba];
	$mtot_strbtspl += $row1[strbtspl];
	$mtot_strbtnml += $row1[strbtnml];
	$mtot_befrtx += $row1[befrtx];
	$mtot_taxamt += $row1[taxamt];
	$mtot_nettot += $row1[nettot];
	if ($mline_ctr>15)
	{
		$mline_ctr = 1;
		$Data .= "\n\n\n\n\n\n\n\n\n\n\n\n\n";
		$Data .= chr(18);
		$Data .=  "                                                                     ".$invno."\n";
		$Data .=  "                                                                    ".$row[invdate]."\n\n";
		$Data .=  "                         ".$row[customer];
		if($row1[tin]>'')
			$Data .= ' TIN:'.$row[tin]."\n\n\n\n\n";
		else
			$Data .= "\n\n\n\n\n";
		$Data .= chr(15);
	}
	$mline_ctr++;
	$slno = "  ".$i;
	$Data .= substr($slno,-2)."   ".substr($mitem_code,0,10).substr($mitem,0,33)." ".substr($mtax,0,5).substr($munitprice,-8).substr($mqty,-6).substr($mtotal,-8);
	$Data .= substr($mredct,-7).substr($mmba,-7).substr($mstrbtspl,-10).substr($mstrbtnml,-9).substr($mbefrtx,-9).substr($mtaxamt,-9).substr($mnettot,-10)."\n";
	$i++;
}
// Total 18 Lines
for ($x = $mline_ctr-1; $x <= 15; $x++) {
    	$Data .= "\n";
} 
$mtotal = str_repeat(" ",10).$mtot_total;
	  
$mredct = str_repeat(" ",10).$mtot_redct;
$mmba = str_repeat(" ",10).$mtot_mba;
$mstrbtspl = str_repeat(" ",10).$mtot_strbtspl;
$mstrbtnml = str_repeat(" ",10).$mtot_strbtnml;
$mbefrtx = str_repeat(" ",10).$mtot_befrtx;
$mtaxamt = str_repeat(" ",10).$mtot_taxamt;
$mtot_nettot=number_format($mtot_nettot, 2, '.', '');
$mnettot = str_repeat(" ",10).$mtot_nettot;
//$mnettot=round($mnettot,2);

$mwords = word($mnettot);
$Data .= str_repeat(" ",68).substr($mtotal,-8);
$Data .= substr($mredct,-7).substr($mmba,-7).substr($mstrbtspl,-10).substr($mstrbtnml,-9).substr($mbefrtx,-9).substr($mtaxamt,-9).substr($mnettot,-10)."\n";
$Data .= "                             ".$mwords;
$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("print billALP001khadi.prn");
	//$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c prt.bat");
     //exec("dir", $output, $return);
    //echo "Dir returned $return, and output:\n";
    //var_dump($output);
//echo $file;
header('Location:countersale_printinv1_prvyr.php');

?>
<input type=hidden id='savecount' value=''>
</table>
