<?php include("../../include/dbconnect.php"); 
$delfile = "/tmp/print.txt";
$f_date = substr($from_date,6,4).'-'.substr($from_date,3,2).'-'.substr($from_date,0,2);
$t_date = substr($to_date,6,4).'-'.substr($to_date,3,2).'-'.substr($to_date,0,2);
if(file_exists($delfile))
 {
  unlink($delfile);
 }

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

$pgno=1;
$kount=1;

$result=mysql_query(" select name from company where  compcode='$compcode' order by name") or die(mysql_error());
$row=mysql_fetch_row($result);
$company=$row[0];

$string1='Itemwise Statement from '. $stdt. ' to  ' .$edt;


title($myfile,$company,$string1);
$kount=$kount+6;	

?>

<table width=999  border=0>
<tr><td width="993" align ="center"><h5>Labour Invoices Itemwise Statement from &nbsp;<? echo($from_date);?> to  &nbsp;<? echo($to_date);?></td></tr></table>

<table width="772" border="0">
</tr>
			 <tr><td colspan="20"><hr></td></tr>
<tr>
<td width="81" align="right"><font size="-1">Sl No</font></td>				
<td width="81" align="center"><font size="-1">Inv. No</font></td>
<td width="78" align="center"><font size="-1">Date</font></td>				
<td width="90" align="center"><font size="-1">Drawing No </font></td>				
<td width="85" align="center"><font size="-1">Order No</font></td>				
<td width="110" align="center"><font size="-1">Order Date</font></td>
<td width="111" align="right"><font size="-1">Quantity</font></td>
<td width="102" align="right"><font size="-1">Total Qty</font></td>

</tr>
			 <tr><td colspan="20"><hr></td></tr>


<?
if($kount>=55)
{
	$pgno++;
	fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
	fputs($myfile,chr(12).chr(13).chr(18)."\n");
	fputs($myfile,"                                                                                                 Page No : $pgno\n");
	$kount=0;
	title($myfile,$company,$string1);
	$kount=$kount+6;		
}

	


for($ind=0;$ind < count($bcode);$ind++)
{
	$temp_drawing='';
	$sl=1;
	$qtytot =0;
	
	$bresult = mysql_query("select name,unit from buy where bcode = '$bcode[$ind]' ") or die(mysql_error());
	$brow = mysql_fetch_row($bresult);
	$bname = $brow[0].'&nbsp;&nbsp;'.$brow[1];
	$bstatus ='F';
	$totstatus ='F';
	$dresult = mysql_query("select distinct drawing_no  from labourinv where invdate >='$f_date' and invdate <='$t_date'  and compcode='$compcode' and bcode ='$bcode[$ind]'  and drawing_no != 'TOTAL' and drawing_no != 'CANCELLED' order by bcode,drawing_no ")or die(mysql_error());
	while($drow = mysql_fetch_row($dresult))
	{
				$drawing_total=0;
				$presult = mysql_query("select invdate,invno,prodcode,drawing_no,quantity,po_no,po_date  from labourinv where invdate >='$f_date' and invdate <='$t_date'  and compcode='$compcode' and bcode ='$bcode[$ind]' and drawing_no = '$drow[0]' order by bcode,drawing_no,invno,record ")or die(mysql_error());
				while($prow = mysql_fetch_row($presult))
				{
					
			
					$date = substr($prow[0],8,2).'-'.substr($prow[0],5,2).'-'.substr($prow[0],0,4);
					$invno= $prow[1];		
					$prodcode= $prow[2];		
					$drawing_no= $prow[3];
					$qty = $prow[4];
					$po_no=$prow[5];
					$po_date=substr($prow[6],8,2).'-'.substr($prow[6],5,2).'-'.substr($prow[6],0,4);
					$drawing_total = $drawing_total+$qty;
					if($bstatus == 'F')
					{
						?>
						
						<tr bgcolor="#FAF8B4"><td colspan="20"><? echo($bname);?></td></tr>
						<?
						$bstatus='T';		
					}
					
				  ?>		
					
					<tr bgcolor="#D3E4D1">
			
					<td width="81" align="center"><font size="-1" color="<? echo($clr);?>"><? echo($sl);?></font></td>															
					<td width="81" align="center"><font size="-1" color="<? echo($clr);?>"><? echo($invno);?></font></td>
					<td width="78" align="left"><font size="-1" color="<? echo($clr);?>" ><?  echo($date);?></font></td>		
					<td width="90" align="left"><font size="-1" color="<? echo($clr);?>"><? echo($drawing_no);?> </font></td>				
					<td width="85" align="left"><font size="-1" color="<? echo($clr);?>"><?  echo($po_no);?> </font></td>															
					<td width="110" align="left"><font size="-1" color="<? echo($clr);?>"><? echo($po_date);?> </font></td>
					<td width="111" align="right"><font size="-1" color="<? echo($clr);?>"><?  echo(round($qty,0));?></font></td>				
			
		
					<?  

			
								
					if($kount>=55)
					{
						$pgno++;
						fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
						fputs($myfile,chr(12).chr(13).chr(18)."\n");
						fputs($myfile,"                                                                                                 Page No : $pgno\n");
						$kount=0;
						title($myfile,$company,$string1);
						$kount=$kount+6;		
					}
					if($temp!= $invno)
					{
						strcontrol($myfile,$date,12);
						strcontrol($myfile,$invno,6);
						strcontrol($myfile,substr($bname,0,35),38);										
					}
					else
					{
						strcontrol($myfile,'',54);				
					}		
					strcontrol($myfile,substr($prodname,0,35),38);
					if($rate>0){numcontrol($myfile,$rate,8);}else{ strcontrol($myfile,'',8);}
					if($qty>0){	numcontrol($myfile,$qty,10);} else{ strcontrol($myfile,'',10);}				
					if($value>0){numcontrol($myfile,$value,10);}else{ strcontrol($myfile,'',10);}
					if($vat4>0){numcontrol($myfile,$vat4,10);}else{ strcontrol($myfile,'',10);}
					if($cst3>0){numcontrol($myfile,$cst3,10);} 	else{ strcontrol($myfile,'',10);}			
					if($round_off !=0){strcontrol($myfile,$round_off,8);}	else{ strcontrol($myfile,'',8);}				
					numcontrol($myfile,$gtot,12);
					fputs($myfile,"\n");
					$kount++;					
					$temp=$invno;
					$sl++;
				}
				?>
				<td width="102" align="right"><font size="-1" color="<? echo($clr);?>"> <? echo($drawing_total);?></font></td>
				</tr>	  
				<tr  ><td  colspan="100%">&nbsp;</td></tr>
				
				<?
		}
	  	
			
	fputs($myfile,"\n");
	$kount++;
}								
	fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
?>
			 <tr><td colspan="20"><hr></td></tr>
<tr>
<td width="81" align="left"><font size="-1">&nbsp;</font></td>
<td width="81" align="left"><font size="-1">&nbsp;</font></td>
<td width="78" align="left"><font size="-1">&nbsp;</font></td>													
<td width="90" align="left"><font size="-1">&nbsp;</font></td>												
<td width="85" align="left"><font size="-1">&nbsp; </font></td>
<td width="110" align="right"><font size="-1">&nbsp;</font></td>				
<td width="111" align="right"><font size="-1">&nbsp;</font></td>

<td width="102" align="right"><font size="-1"><? if($gvalue>0)  echo(number_format($gvalue,2,'.',''));?></font></td>

</tr>
</table>			 
<?
strcontrol($myfile,"",105);

numcontrol($myfile,number_format($gvalue,2),12);
numcontrol($myfile,$gvat4,10);
numcontrol($myfile,$gcst3,7);
strcontrol($myfile,$ground_off,8);				
numcontrol($myfile,$gtot,12);
fputs($myfile,"\n");
$kount++;					
?>


</table>
<a href = "/wilson/dload.php?dfile=<?echo($fname);?>">Download Text file</a>
    <br>
    <br><font size=1><center><a href= "../../index.html">Return to index page</center></font>
                                                                             
</body>
</form>

<?

function title($myfile,$company,$string1) 
{
	fputs($myfile,chr(18).chr(14));
	fputs($myfile,"$company \n\n");
	fputs($myfile,"$string1 \n");
	fputs($myfile,chr(27).'M');
	fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
	fputs($myfile,"Date     Inv No   Buyer                               Product                                Rate    Qty     Amount     VAT       E1 Sales     Total Amt.\n"); 
	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," "); };
	if($stname >0)
	{
		fputs($myfile,$stname);
	}
	else
	{
	fputs($myfile,"    ");
	}
}
?>