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

?>

 
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>wilson Invoice</title>

</head>
<body bgcolor="#FFFFFF" class="Normall" lang=EN-US>
<?

$fname = 'print.txt';
$delfile = "/tmp/print.txt";
if(file_exists($delfile))
 {
  unlink($delfile);
 }
//if(!($myfile=fopen("d:/temp/print.txt","w")))
if(!($myfile=fopen("/tmp/print.txt","w")))
{
  print("file couldnot be opened");
}
for($incount=0;$incount <count($invnos);$incount++)
{
		echo("Invoice No : $invnos[$incount]<br>");
		$sno=1;
		$invresult = mysql_query("select  distinct bcode,prodcode,prepare_time from labourinv where account_year='$account_year1' and invno = '$invnos[$incount]'  ") or die(mysql_error());			
		$row=mysql_fetch_row($invresult);
		
		$invno = $invnos[$incount];
		$bcode = $row[0];
		$prodcode = $row[1]; 
		$date = substr($tdate,8,2).'-'.substr($tdate,5,2).'-'.substr($tdate,0,4);
		$prepare_time= $date.'/'.$row[2];
		
		$temp_order='';
		$orderno='';
		$schedule_no='';
		$poresult = mysql_query("select  po_no,po_date,schedule_no,schedule_date from labourinv where account_year='$account_year1' and invno = '$invnos[$incount]'  and drawing_no != 'TOTAL' order by po_no ") or die(mysql_error());
		while($porow =mysql_fetch_row($poresult))
		{
					$po_date = substr($porow[1],8,2).'-'.substr($porow[1],5,2).'-'.substr($porow[1],0,4);
					$schedule_date = substr($porow[3],8,2).'-'.substr($porow[3],5,2).'-'.substr($porow[3],0,4);
					if($temp_order != $porow[0])
					{
				
						$orderno = $porow[0].'/'.$po_date.',';
						$temp_order = $porow[0];					}
   					     $schedule_no = $schedule_no.$porow[2].'/'.$schedule_date.',';
		}
		
		$result= mysql_query("select name,address1,address2,city,state,pin,cst,tin,taxtype,eccno,unit ,vendercode from buy where bcode ='$bcode' ") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$bname = $row[0].'  '.$row[10];
		$baddress1=$row[1];
		$baddress2=$row[2];
		if(!empty($row[4]))
		{
			$bcity = $row[3].','.$row[4];
		}
		else
		{
			$bcity= $row[3];
		}
		$bcst=$row[6];
		$btin=$row[7];
		$btaxtype2=$row[8];
		$eccno= $row[9];
		$vendercode=$row[11];
				

		$totline=0;
		
		fputs($myfile,chr(27).'C$'."\n");
		while($linecnt < 5)
		{
			fputs($myfile,"\n");
			$linecnt++;
		}
		
		fputs($myfile,chr(27).'M');		
		strcontrol($myfile,'',8);
		strcontrol($myfile,$bname,58);
		fputs($myfile,"\n");
		
		strcontrol($myfile,'',8);		
		strcontrol($myfile,$baddress1,57);		
		strcontrol($myfile,'',6);
		fputs($myfile,chr(14));
		strcontrol($myfile,$invnos[$incount],10);
		fputs($myfile,chr(18));
	  	fputs($myfile,"\n");
		
		strcontrol($myfile,'',8);		
		strcontrol($myfile,$baddress2,57);		
		fputs($myfile,"\n");
		
		strcontrol($myfile,'',8);		
		strcontrol($myfile,$bcity,59);		
	//	strcontrol($myfile,'',8);		
		strcontrol($myfile,$date,12);	
		fputs($myfile,"\n\n\n");			
		strcontrol($myfile,'',66);
		strcontrol($myfile,$orderno,30);		
	
		fputs($myfile,"\n\n\n");						
		
if(!empty($btin))
		{
		strcontrol($myfile,'',12);
		strcontrol($myfile,'TIN No: ',10);				
		strcontrol($myfile,$btin,50);
		}		
		fputs($myfile,"\n");

		if(!empty($vendercode))
		{
		strcontrol($myfile,'',8);
		strcontrol($myfile,'VENDOR CODE: ',12);				
		strcontrol($myfile,$vendercode,50);
		}	
		fputs($myfile,"\n");	
		if(!empty($bcst))
		{
		strcontrol($myfile,'',12);
		strcontrol($myfile,'CST',12);				
		strcontrol($myfile,$bcst,50);
		}		

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


		
		if(!empty($eccno))
		{
		strcontrol($myfile,'',12);
		strcontrol($myfile,'ECC No ',12);				
		strcontrol($myfile,$eccno,50);
		}		
		fputs($myfile,"\n");

		


			fputs($myfile,"\n");



				
		

		

		
		
		$presult = mysql_query("select  bcode,prodcode from labourinv where account_year='$account_year1' and invno = '$invnos[$incount]'  and drawing_no!='TOTAL' ") or die(mysql_error());
		$prow= mysql_fetch_row($presult);
		$bcode = $prow[0];
		$prodcode =$prow[1];
		
	
					

		
		strcontrol($myfile,'',25);
		//strcontrol($myfile,$tariff,70);
		fputs($myfile,"\n\n\n");

		$linecnt= $linecnt+18;				
					
		$result = mysql_query("select distinct drawing_no,bcode,prodcode from labourinv where account_year='$account_year1' and invno = '$invnos[$incount]'  and drawing_no != 'TOTAL' ") or die(mysql_error());
		while($row =mysql_fetch_row($result))
		{

			$invresult = mysql_query("select  sum(quantity),avg(rate),sum(value) from labourinv where account_year='$account_year1' and invno = '$invnos[$incount]'  and drawing_no='$row[0]' ") or die(mysql_error());
			$invrow= mysql_fetch_row($invresult);
			$qty = $invrow[0];
			$rate = round($invrow[1],2);
			$value = $invrow[2];


			$tresult = mysql_query("select prodname from product where product.bcode like  '$row[1]%' and product.prodcode like '$row[2]%'   ") or die(mysql_error());		
			$trow= mysql_fetch_row($tresult);
			$prodname = $trow[0];			
			strcontrol($myfile,'',5);		
			strcontrol($myfile,$sno,6);
			strcontrol($myfile,$row[0],17);
			strcontrol($myfile,substr($prodname,0,30),30);
			numcontrol($myfile, number_format($qty,0),8);
			numcontrol($myfile,number_format($rate,2),11);
			numcontrol($myfile,$value,19);
			fputs($myfile,"\n");
			$linecnt++;
			$sno++;
		}
		$result = mysql_query("select distinct packing_percent,packing_amt,bed,bed_amt,cess,cess_amt,vat_percent,vat_amount,freight,total,round_off,gtotal ,value from labourinv where account_year='$account_year1' and invno = '$invnos[$incount]'  and drawing_no = 'TOTAL' ") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$packing_percent = $row[0];
		$packing_amt=$row[1];
		$bed = $row[2];
		$bed_amt=$row[3];
		$cess=$row[4];
		$cess_amt=$row[5];
		$vat_percent=$row[6];
		$vat_amt=$row[7];
		$freight=$row[8];
		$total =$row[9];
		$round_off=$row[10];
		$gtotal = $row[11];
		$value=$row[12];
		$duty_posted=$row[13];
		
		$firsttotal = $value+$packing_amt;
		$secondtotal = $firsttotal + $bed_amt+$cess_amt;
		$thirdtotal = $secondtotal + $vat_amt;

		
		
	
	    	fputs($myfile,"\n\n\n\n\n\n\n");
		    strcontrol($myfile," ",25);
		    strcontrol($myfile,"(MATERIAL RECEIVED VIDE PARTY'S ",35);
		    fputs($myfile,"\n");


			
	       	strcontrol($myfile," ",27);
		    $sch = 'Challan No :'.$schedule_no.')';
	    	strcontrol($myfile,$sch,12);

	       	fputs($myfile,"\n");		
			$linecnt= $linecnt+9;			
		
		
			fputs($myfile,"\n\n");
			
		if($round_off !=0)
		{
			strcontrol($myfile,'',10);			
			strcontrol($myfile,"ADD /LESS : ROUNDED OFF A/C",58);
			numcontrol($myfile,$round_off,24);
			fputs($myfile,"\n");		
		}
		else
		{
			fputs($myfile,"\n");
		}	
		strcontrol($myfile,"",80);
		strcontrol($myfile,'----------',9);
		fputs($myfile,"\n");	

		$linecnt=$linecnt+13;
			

		while($linecnt < 57)
		
		{
			fputs($myfile,"\n");
			$linecnt++;
		}
			
				$chrnum = connum($gtotal).' only';
		       strcontrol($myfile,'',7);
			    strcontrol($myfile,$chrnum,70);

				numcontrol($myfile,number_format($gtotal,2),15);
				fputs($myfile,"\n");

				fputs($myfile,"\n");

				fputs($myfile,"\n");
				

				fputs($myfile," \n");

				fputs($myfile,chr(12));
}
				


				

					for($ln=1;$ln<=7;$ln++)
					{
						fputs($myfile,"\n");
					}
					$linecnt=1;


?>
<a href = "/wilson/dload.php?dfile=<?echo($fname);?>">Download Text file</a>
<br><br><font size=+1><center><a href= "linput.php">Make another Bill</a></center></font>
<br><br><font size=+1><center><a href= "../../index.html">Return to Index</a></center></font>

                                                                             
</body>
</html>
<?

function line($myfile,$maxlen)
{
for($sk=1;$sk <$maxlen;$sk++)
{ fputs($myfile,"_"); };
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," "); };
}


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 heading($myfile,$invno,$date)
{





	fputs($myfile,"-------------------------------------------------------------------------\n");
	fputs($myfile," Sl.No           Particulars                Qty       Rate   Amount  \n");
	fputs($myfile,"---------------------------------------------------------------------\n");


}

?>
