<?php include("../../include/dbconnect.php");
$startdt = substr($from_date,6,4).'-'.substr($from_date,3,2).'-'.substr($from_date,0,2);
$enddt = substr($to_date,6,4).'-'.substr($to_date,3,2).'-'.substr($to_date,0,2);

set_time_limit(0);

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

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

$fname = 'print.txt';


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



$linecnt=0;

headprint($from_date,$to_date);

if ($linecnt== 0)
{
 headtext($myfile,$mill[$index],$from_date,$to_date);
 $linecnt =3;
}
 
for($pind=0;$pind< count($prodcode);$pind++)
{        
       
  	$presult = mysql_query("select prodname from rawmaterial where  prodcode = '$prodcode[$pind]' ");
	$prow = mysql_fetch_row($presult);
	$prodname= $prow[0];

	fputs($myfile,"\n\n");
	fputs($myfile,chr(18));
	fputs($myfile,chr(14));
	fputs($myfile,"$prodname\n\n");
	$linecnt= $linecnt+4;
	fputs($myfile,chr(15));
       
    ?>
	<table width = 747>
	<td align = "left" colspan="100%"><b><font  color="#3300FF"><?echo($prodname);?></b></td></table><?
	
     $tprqty=0;
	 $tselqty=0;

	 $result3 = mysql_query("select sum(prqty)-sum(selqty) from stktrans where  prodcode = '$prodcode[$pind]' && date < '$startdt'  and compcode='$compcode' order by prodcode,date,selqty,prqty,record");
	 $row3=mysql_fetch_row($result3);


	$opening=$row3[0];
	 $status='F';

   
	$result = mysql_query("select date,opbal,prqty,selqty,clbal,invno,bcode from stktrans where  prodcode = '$prodcode[$pind]' && date >= '$startdt' && date <= '$enddt'   and compcode='$compcode' order by prodcode,date,selqty,prqty,record");
	while($row=mysql_fetch_row($result))
	{
		$status='T';
		$date = $row[0];
		$opening = $row[1];
		$prqty=$row[2];
		$selqty = $row[3];
		$closing = $row[4];
		$tprqty=$tprqty+$prqty;
		$tselqty = $tselqty+$selqty;
		$invno=$row[5];
		$remark=$row[6];
		$bcode=$row[7];
        $date = substr($date,8,2).'-'.substr($date,5,2).'-'.substr($date,0,4);	

			$cresult = mysql_query("select name from supplier where bcode = '$bcode'  ");


		
		
		$crow=mysql_fetch_row($cresult);
		$bname = $crow[0];
	
		if($selqty==0)
			{$selqty=' ';}
		if($prqty==0)
			{$prqty=' ';}
		if($invno==0)
			$invno=' ';
	    ?>   
	
		<table width = 760>
		<tr>
		<td width =81  align = "left"><font size=2 face="Arial, Helvetica, sans-serif"><? echo($date);?></font></td>
	
		<td width=58 align = "right"><font size=2 face="Arial, Helvetica, sans-serif" ><? echo(number_format($opening,2));?></font></td>

		<?
		if($prqty>0)
		{
			?>
		<td width=54 align = "right"><font size=2 face="Arial, Helvetica, sans-serif" color="#FF0066"><? echo($prqty);?></font></td>			
			<td width=86 align = "left"><font size=2 face="Arial, Helvetica, sans-serif" color="#FF0066"><? echo($bname);?></font></td>
		 <td width =82 align = "right"><font size=2 face="Arial, Helvetica, sans-serif" color="#FF0066"><? echo($invno);?></font></td>
		<td width=32  align = "right"><font size=2 face="Arial, Helvetica, sans-serif"  color="#0066CC">&nbsp;</font></td>		 			
		 
			<?
		}
		else
		{
			?>
		<td width=34 align = "right"><font size=2 face="Arial, Helvetica, sans-serif" color="#FF0066">&nbsp;</font></td>			
			<td width=107 align = "left"><font size=2 face="Arial, Helvetica, sans-serif"  color="#0066CC"><? echo($bname);?></font></td>
			<td width =54 align = "right"><font size=2 face="Arial, Helvetica, sans-serif" color="#0066CC"><? echo($invno);?></font></td>			
		<td width=54  align = "right"><font size=2 face="Arial, Helvetica, sans-serif"  color="#0066CC"><? echo($selqty);?></font></td>			
			<?
		
		}
		?>


		<td width=60 align = "right"><font size=2 face="Arial, Helvetica, sans-serif"><? echo($closing);?></font></td>

		</tr>
		</table>
         <?  
         strcontrol($myfile,$date,14);
		 numcontrol($myfile,$opening,10);
		 fputs($myfile,"     ");
		 numcontrol($myfile,$prqty,10);
		 fputs($myfile,"  ");
		 strcontrol($myfile,substr(ltrim($bname),0,30),35);	 
		 strcontrol($myfile,substr(ltrim($invno),1,10),10);	 
		 numcontrol($myfile,$selqty,10);
		 numcontrol($myfile,$closing,10);
   	      fputs($myfile,"  ");
		 strcontrol($myfile,$remark,40);
		 fputs($myfile,"\n");	 
		 $linecnt++;
		 if ($linecnt >52)
		 {
			line($myfile,102);
			fputs($myfile,chr(12));
			fputs($myfile,"\n");
		    headtext($myfile,$from_date,$to_date);
	        $linecnt =3;
  	  	 }
     } // end of while loop
 	 if($status=='F')
	 {
	 ?>
		<table width = 170>
		<tr>
		<td width =60  align = "left"><font size=2 face="Arial, Helvetica, sans-serif"><?echo($from_date);?></td>
		<td width=60 align = "right"><font size=2 face="Arial, Helvetica, sans-serif"><?echo(number_format($opening,2));?></td>
		</tr></table>
	 
	 <?
	 }

} // end of product 	 

line($myfile,102);

?>
<hr align = "left" width = 750></hr>
<a href = "/stt/dload.php?dfile=<?echo($fname);?>">Download Text file</a>
<center><font color="#000080"><font size="+1"><a href="/wilson/stores.html">Return to Index </a></font></center>


<?
function headprint($from_date,$to_date)
{
	?>
	<table width=750  border=0>
	<tr><td align ="center"><h5>Stock Register from the period &nbsp;<? echo($from_date);?>&nbsp;to&nbsp; <? echo($to_date);?></td></tr></table>
	<hr align="left" width = 750></hr>
	<table width = 750>
	<tr>

	<td width="102"  align = "left"><font size=2 face="Arial, Helvetica, sans-serif">Date</td>

	<td width="85"  align = "right"><font size=2 face="Arial, Helvetica, sans-serif">Opening</td>

	<td width="101"  align = "right"><font size=2 face="Arial, Helvetica, sans-serif">Receipts</td>

	<td width="177" align = "center"><font size=2 face="Arial, Helvetica, sans-serif">Supplier </td>

	<td width="70" align = "right"><font size=2 face="Arial, Helvetica, sans-serif">Inv. No</td>

	<td width="102" align = "right"><font size=2 face="Arial, Helvetica, sans-serif">Consumed_Qty</td>

	<td width="81" align = "right"><font size=2 face="Arial, Helvetica, sans-serif">Closing</td>

	</table>
    <hr align="left" width = 750 ></hr>
	<?
}
function headtext($myfile,$from_date,$to_date)
{

	fputs($myfile,"Stock Register from the period $from_date  to $to_date \n");
	fputs($myfile,Chr(15));
	line($myfile,102);
	fputs($myfile,"  Date       Opening       Pur. Qty       Supplier / Buyer name        Inv_No        Sel_qty  Closing \n");
	line($myfile,102);
}

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


function line($myfile,$maxlen)
{
	for($sk=1;$sk <$maxlen;$sk++)
	{ 
		fputs($myfile,"_"); 
	}
	fputs($myfile,"\n");

}

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,"    ");
	}
}

?>
