<?
include("../../include/dbconnect.php");

$delfile = "../../../tmp/invoice.txt";

if(file_exists($delfile))
 {
  unlink($delfile);
 }



if(!($myfile=fopen("../../../../tmp/invoice.txt","w")))
{
  print("file couldnot be opened");
}

$fname = 'invoice.txt';
if($taxtype =='3')
{
	$invtype = 'Sales ReturnBILL';
}
elseif($taxtype == '2')
{
	$invtype = 'Sales ReturnBILL';
	
}

fputs($myfile,"                                                          TIN  : 32091447914\n\n");
fputs($myfile,chr(14));
fputs($myfile,"   NARAYANA   AYURVEDA   CHIKITSALAYAM");
fputs($myfile,chr(18));	
fputs($myfile,"\n                       CHITTILAMCHERY POST, PALAKKAD DIST., PIN-678704\n");
fputs($myfile,"                          (The Kerala Value Added Tax Rules 2005)\n");
fputs($myfile,"                                       FORM No.8 E\n");
if($taxtype == '2')
{
	fputs($myfile,"         (For Sales Returnof taxable goods made from unregistered dealers)\n");
}
fputs($myfile,"                                      $invtype                                \n");
fputs($myfile,"_____________________________________________________________________________\n");
fputs($myfile,"Name and address of the Unregistered Dealer:                             \n");
$linecount = 9;

echo("Invoice Printed $poinv_no <br>");
$result = mysqli_query($conn,"select bcode from salesreturn where poinv_no = '$poinv_no' and poinv_date = '$poinv_date' and compcode = '$compcode' and prodcode = 'TOTAL' and taxtype = '$taxtype' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$bcode = $row[0];


$result = mysqli_query($conn,"select name,address1,address2,city,pin from buy where bcode = '$bcode' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$name = $row[0];
$address1 = $row[1];
$address2 = $row[2];
$address3 = $row[3]- $row[4];;

$result = mysqli_query($conn,"select ipno from inpatient where mr_no = '$bcode' and active = 'Yes' ") or die(mysqli_error());
$row = mysqli_fetch_array($result);
$ipno = $row[0];
$mrno = $bcode;


strcontrol($myfile,$name,40);
fputs($myfile,"                                  ");
fputs($myfile,"\n");

strcontrol($myfile,$address1,40);
fputs($myfile,"                      ");
strcontrol($myfile,"Invoice No:".$poinv_no,10);
fputs($myfile,"\n");
strcontrol($myfile,$address2,40);
fputs($myfile,"                      ");
strcontrol($myfile,"Date:".british($poinv_date),10);
fputs($myfile,"\n");
strcontrol($myfile,$address3,40);
fputs($myfile,"\n");
$linecount = $linecount+6;
fputs($myfile,"|___________________________________________________________________________|\n");
fputs($myfile,"|Sl|           Product Name          |       Qty   |       Rate|       Value|\n");
fputs($myfile,"|__|_________________________________|_____________|___________|____________|\n");
$linecount = $linecount+3;
$total =0;
$sl = 1;
$result = mysqli_query($conn,"select prodcode,compcode,batch_no,quantity,rate,value from salesreturn where poinv_no = '$poinv_no' and poinv_date = '$poinv_date' and compcode = '$compcode'  and prodcode != 'TOTAL' and taxtype = '$taxtype' ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$prodcode = $row[0];
	$presult = mysqli_query($conn,"select prodname,packing from product where prodcode = '$prodcode'") or die(mysqli_error());
	$prow = mysqli_fetch_row($presult);
	$prodname = $prow[0];
	$batch_no = $row[2];
	$qty = $row[3];
	$rate = $row[4];
	$value = $row[5];
	$total = $total + $value;
	fputs($myfile,"|");
	strcontrol($myfile,substr($sl,0,3),3);
	fputs($myfile,"| ");
	strcontrol($myfile,substr($prodname,0,33),33);

	fputs($myfile,"| ");
	numcontrol($myfile,number_format($qty,2),10);
	fputs($myfile,' ');
	fputs($myfile,substr($prow[1],0,3),3);
	fputs($myfile,"| ");
	numcontrol($myfile,number_format($rate,3),11);	
	fputs($myfile,"| ");
	numcontrol($myfile,number_format($value,2),12);	
	fputs($myfile,"|\n");
	$sl++;	
	$linecount++;	
}
while($linecount< 27)
{
	fputs($myfile,"|  |                                 |             |           |            |\n");
	$linecount++;
}
fputs($myfile,"|__|_________________________________|_____________|___________|____________|\n");

fputs($myfile,"|                                                        Total    |");
numcontrol($myfile,number_format($total,2),10);	
fputs($myfile,"| \n");

$taxres = mysqli_query($conn,"select tax,cess from salestax where taxtype = '$taxtype' ") or die(mysqli_error());
$taxrow = mysqli_fetch_row($taxres);
$vat_percent = $taxrow[0];
$cess_percent = $taxrow[1];
$vatamt = $total * $vat_percent/100;
$cessamt = $vatamt * $cess_percent/100;
$gtotal = $total + $vatamt+$cessamt;
if($vatamt>0)
{
fputs($myfile,"|                                                  Vat @ $vat_percent% |");
numcontrol($myfile,number_format($vatamt,2),13);	
fputs($myfile,"| \n");
fputs($myfile,"|                                                  Cess @ $cess_percent%|");
numcontrol($myfile,number_format($cessamt,2),13);	
fputs($myfile,"| \n");

fputs($myfile,"|                                                  Grand Total |");
numcontrol($myfile,number_format($gtotal,2),13);	
fputs($myfile,"| \n");
$linecount=$linecount+2;
}

fputs($myfile,"|___________________________________________________________________________|\n");




fputs($myfile,"	                                    for NARAYANA AYURVEDA CHIKITSALAYAM\n\n\n");
fputs($myfile,"                                           Authorised Signatory\n");

fputs($myfile,"Original to be issued to Seller, Duplicate to Company. Monthly return and \n"); 
fputs($myfile,"triplicate to be kept by the purchasing dealer\n\n");

?>
<br><br><font size=+1><center><a href= "index.html">Return to Index</a></center>
</font> 
<p align="center">&nbsp; </p>
<center>
  <font size="+1"><a href = "dload.php?dfile=<?echo($fname);?>">Download Text file</a></font> 
</center>
<?

function numcontrol($myfile,$stname,$maxlen)
{
$plen = strlen($stname);
$tlen = $maxlen-$plen;
for($sk=1;$sk <$tlen;$sk++)
{ fputs($myfile," "); };
fputs($myfile,$stname);
//fputs($myfile,"\n");
}

function strcontrol($myfile,$stname,$maxlen)
{
	$plen = strlen($stname);
	$tlen = $maxlen-$plen;
	fputs($myfile,$stname);
	for($sk=1;$sk<$tlen;$sk++)
	{ fputs($myfile," "); }
}
?>
