<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include('testword.php');
$query="select brname,braddr,brcity,tin,cstno from branch where brcode ='$brcode'";
$rs=mysqli_query($db,$query);
$brw=mysqli_fetch_array($rs);
$inno=$_REQUEST['inno'];
$rs=mysqli_query($db,"select * from brtransferout where invno='$inno' and brcode='$brcode'");
$rw=mysqli_fetch_array($rs);
$num=mysqli_num_rows($rs);

if($num>0)
{


	$file =  'brtrsfer.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 .= "\n\n\n";
	$Data .= "                     KERALA KHADI & VILLAGE INDUSTRIES BOARD\n";
	$Data .= "                           CENTER NAME : ".$brw[0]." \n";
	$Data .= "           ".$brw[2]."\n";
	$Data .= "KGST No.: \n";
	$Data .= "CST No.:".$brw[4]."\n";
	$Data .= "TIN : ".$brw[3]."\n";

	$Data .=chr(15);


$invdate=date('d-m-Y', strtotime($rw[1]));

$Data .= "Date : ".$invdate."\n";
$Data .= "Invoice No. : ".$rw[0]."\n";


$qr=mysqli_query($db,"select * from branch where brname='$rw[2]'");
$qw=mysqli_fetch_array($qr);
$Data .= "From :                                                                                                  To :\n";
$Data .= $brw[0]."                                                                             ".$rw[2]."\n\n";

$Data.= "|------------------|------------------|-------------------------|---------------|----------------|------------------|\n";
$Data.= "|      SL No.      |      Stock No    |       Particulars       |      Qty      |      Rate      |      Amount      |\n";
$Data.= "|------------------|------------------|-------------------------|---------------|----------------|------------------|\n";

$slno=1;
$query=mysqli_query($db,"select * from brtransferout where invno='$inno' and brcode='$brcode'");
while($row=mysqli_fetch_array($query))
{
$mda=$row[16];
$commission=$row[17];
$discount=$row[19];
$totdisc=$mda+$commission+$discount;
$ntvl=$row[10]-$totdisc;
//$taxamount=$ntvl*($row[20]/100);
$margin=$row[10]*($row[23]/100);
$grandamt=$ntvl+$taxamount+$margin+$row[24]+$row[25];

$Data .= "|".substr(str_repeat(" ",18).$slno++,-18);
$Data .= "|".substr(str_repeat(" ",18).$row[5],-18);
$Data .= "|".substr(str_repeat(" ",25).$row[4],-25);
$Data .= "|".substr(str_repeat(" ",15).$row[7],-15);
$Data .= "|".substr(str_repeat(" ",16).$row[8],-16);
$Data .= "|".substr(str_repeat(" ",18).$row[10],-18)."|\n";
$Data.= "|------------------|------------------|-------------------------|---------------|----------------|------------------|\n";

$netdisc+=$row[19];
$netval+=$ntvl;
$tottax+=$taxamount;
$totcomision+=$commission;
$totamt+=$grandamt;
$total+=$row[10];
}
$totamt=round($totamt,2);

$Data.= "|                                 Gross Amount                                                  ".substr(str_repeat(" ",20).$total,-20)."|\n";
$Data.= "|                                 Commission                                                    ".substr(str_repeat(" ",20).$totcomision,-20)."|\n";
$Data.= "|                                 Net Amount                                                    ".substr(str_repeat(" ",20).$totamt,-20)."|\n";
$Data.= "|------------------|------------------|-------------------------|---------------|----------------|------------------|\n\n\n\n";


$Data .= "The above invoice will be prepared in triplicate by the officer of the centre. Two copies will be sent by\n";
$Data .= "him to the recipient. The recipient will acknowledge receipt and return one copy to the sender\n";
$Data .= "retaining the other for his record. The sender will keep up the acknowledgement.\n\n";

$Data .= "                                                                                              Dispatch Bhavan Manager\n\n";

$Data .= "Amount in words:".word($totamt)."\n\n\n";
$Data .= "Prepared By\n\n";
$Data .= "Checked By\n\n\n";

$Data .= "Received goods in good condition as per this bill ...................................................... Despatched by\n";
$Data .= "Train/Lorry Service To ...............................................Under R.R./Lorry Way Bill No ...................\n";
$Data .= "Dated ......................... Against Order No  ......................................Dated ........................\n\n";
$Data .= "                                                                                                             Received\n";
$Data .= "                                                                                                  Signature With Date\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 brtransfer.bat");
     header('location:brtransfer_printinv1.php');
	 }
?>


