<?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='Sales Register from '. $stdt. ' to  ' .$edt;


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

?>

<table width=999  border=0>
<tr><td width="993" align ="center"><h5><? echo($company);?></td></tr>
<tr><td width="993" align ="center"><h5>Sales Register from &nbsp;<? echo($from_date);?> to  &nbsp;<? echo($to_date);?>&nbsp;&nbsp;&nbsp;(<?=$taxtype.'&nbsp;'.$vat_percent.'&nbsp;%&nbsp;)'?></td></tr></table>

				<table width="1004" border="0">
				</tr>
			 <tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td></tr>
				
				<tr>
				<td width="46" align="left"><font size="-1">Date</font></td>

				<td width="35" align="right"><font size="-1">Inv. No</font></td>
				<td width="86" align="left"><font size="-1">Customer </font></td>				
				<td width="86" align="left"><font size="-1">Product </font></td>
				<td width="85" align="left"><font size="-1">Drawing No </font></td>				
				<td width="34" align="right"><font size="-1">Rate</font></td>				
				<td width="47" align="right"><font size="-1">Quantity</font></td>
				<td width="53" align="right"><font size="-1">Value</font></td>
				<td width="45" align="right"><font size="-1">Packing Amt</font></td>
				<td width="40" align="right"><font size="-1">BED Amt</font></td>
				<td width="48" align="right"><font size="-1">ED Cess</font></td>				
				<td width="48" align="right"><font size="-1">H. Cess</font></td>	                											
				<td width="46" align="right"><font size="-1">Freight</font></td>
				<td width="42" align="right"><font size="-1"></font><?=$taxtype.'&nbsp;'.$vat_percent.'&nbsp;%'?> </font></td>
				
				<td width="56" align="right"><font size="-1">Net </font></td>				
    <td width="39" align="right"><font size="-1">Round Off</font></td>								
				<td width="59" align="right"><font size="-1">Total Amount</font></td>				
				
				</tr>
			 <tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><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;		
}
	$gvalue=0;
	$gvatamt=0;
	$gcst3=0;
	$gvat125=0;
	$ground_off=0;
	$gross=0;
	$net=0;
	$gpacking_amt=0;
	$gbed_amt=0;
	$gcess_amt=0;
	$ghcess_amt=0;
	$gfreight=0;
	



$temp=' ';
$temptot=0;
$invresult= mysql_query("select distinct invno from salesreg  where invdate >='$f_date' and invdate <='$t_date'  and compcode='$compcode' and taxtype = '$taxtype' and vat_percent = '$vat_percent' and drawing_no = 'TOTAL' order by invno,invdate,record ")or die(mysql_error());
while($invrow = mysql_fetch_row($invresult))
{
	$temp_drawing_no = '';
	$temp_rate = 0;
	$temp_qty = 0;
	$presult = mysql_query("select invdate,bcode,invno,prodcode,drawing_no,rate,quantity,value,vat_percent,vat_amount,round_off,gtotal,vat_percent,packing_amt,bed_amt,cess_amt,freight,total,hcess_amt  from salesreg where invdate >='$f_date' and invdate <='$t_date'  and compcode='$compcode' and invno = '$invrow[0]' order by invno,drawing_no,record ")or die(mysql_error());
	while($prow = mysql_fetch_row($presult))
	{
		
		$total=0;
		$date = substr($prow[0],8,2).'-'.substr($prow[0],5,2).'-'.substr($prow[0],0,4);
		$bcode= $prow[1];
		$invno= $prow[2];		
		$prodcode= $prow[3];		
		$drawing_no= $prow[4];
		$rate= $prow[5];						
		if($drawing_no != $temp_drawing_no and drawing_no != 'TOTAL')
		{
			$query ="select sum(quantity),sum(value) from salesreg where invdate >='$f_date' and invdate <='$t_date'  and compcode='$compcode' and invno = '$invrow[0]'  and drawing_no ='$drawing_no'  ";				
			$qtresult = mysql_query($query)or die(mysql_error());
			$qrow = mysql_fetch_row($qtresult);
			$qty=$qrow[0];
			$value=$qrow[1];
		}	
		
		$tax_rate=$prow[8];
		$taxamt=$prow[9];
		$round_off=$prow[10];
		$gtot = $prow[11];
		$taxpercent = $prow[12];
		$packing_amt= $prow[13];
		$bed_amt= $prow[14];
		$cess_amt= $prow[15];
		$hcess_amt = $prow[18];
		$freight = $prow[16];
		$total = $prow[17];
		
		
		$gpacking_amt=$gpacking_amt+$packing_amt;
		$gbed_amt=$gbed_amt+$bed_amt;
		$gcess_amt = $gcess_amt + $cess_amt;
		$ghcess_amt = $ghcess_amt + $hcess_amt;		
		$gfreight = $gfreight + $freight;
		$gvatamt = $gvatamt + $taxamt;
		$net = $net+ $total;
		$gross = $gross+$gtot;
		
		if($drawing_no == 'TOTAL')
		{
			$drawing_no='';
			$gvalue=$gvalue+$value;			
		}
		//************************* TAX TYPE IS AVAILABLE IN BUYER MASTER IF REQUIRED.***************************
			
		$bslct= mysql_query("select name,taxtype from buy where bcode = '$bcode' ") or die(mysql_error());
		$brow=mysql_fetch_row($bslct);
		$bname = $brow[0];
		$taxcode =$brow[1];

	
		
		$ground_off=$ground_off+$round_off;
	
		$clr='#000000';
	
		$prodslct= mysql_query("select prodname from product where prodcode ='$prodcode' ") or die(mysql_error());
		$prrow=mysql_fetch_row($prodslct);
		$prodname = $prrow[0];
		if($prodcode== '')
		{	
			$prodname = 'TOTAL';
			$clr ='#990000';
		}
	
		if($drawing_no != $temp_drawing_no and $drawing_no != 'TOTAL'  )
		{
		?>

		<tr>
		<td width="46" align="left"><font size="-1" color="<? echo($clr);?>" ><? if($temp!= $invno) echo($date);?></font></td>
															
		<td width="35" align="center"><font size="-1" color="<? echo($clr);?>"><? if($temp!= $invno) echo($invno);?></font></td>
		<td width="86" align="left"><font size="-1" color="<? echo($clr);?>"><? if($temp!= $invno) echo($bname);?> </font></td>															
		<td width="86" align="left"><font size="-1" color="<? echo($clr);?>"><? echo($prodname);?> </font></td>
		<td width="85" align="left"><font size="-1" color="<? echo($clr);?>"><? echo($drawing_no);?> </font></td>		
		<td width="34" align="right"><font size="-1" color="<? echo($clr);?>"><? if($rate>0) echo($rate);?></font></td>				
		<td width="47" align="right"><font size="-1" color="<? echo($clr);?>"><? if($qty>0)  echo($qty);?></font></td>
	
		<td width="53" align="right"><font size="-1" color="<? echo($clr);?>"><? if($value>0)  echo(number_format($value,2,'.',''));?></font></td>
		<td width="45" align="right"><font size="-1" color="<? echo($clr);?>"><? if($packing_amt>0)  echo(number_format($packing_amt,2,'.',''));?></font></td>
		<td width="40" align="right"><font size="-1" color="<? echo($clr);?>"><? if($bed_amt>0)  echo(number_format($bed_amt,2,'.',''));?></font></td>
      
		<td width="48" align="right"><font size="-1" color="<? echo($clr);?>"><? if($cess_amt>0)  echo(number_format($cess_amt,2,'.',''));?></font></td>
		<td width="48" align="right"><font size="-1" color="<? echo($clr);?>"><? if($hcess_amt>0)  echo(number_format($hcess_amt,2,'.',''));?></font></td>          
		<td width="46" align="right"><font size="-1" color="<? echo($clr);?>"><? if($freight>0)  echo(number_format($freight,2,'.',''));?></font></td>								
		<td width="42" align="right"><font size="-1" color="<? echo($clr);?>"><? if($taxamt>0)    echo(number_format($taxamt,2,'.',''));?></font></td>

		<td width="56" align="right"><font size="-1" color="<? echo($clr);?>"><? if($total>0)    echo(number_format($total,2,'.',''));?></font></td>					
		<td width="39" align="right"><font size="-1" color="<? echo($clr);?>"><? if($round_off!=0)  echo(number_format($round_off,2,'.',''));?></font></td>								
		<td width="59" align="right"><font size="-1" color="<? echo($clr);?>"><? if( $gtot>0)  echo(number_format($gtot,2,'.','')); ?></font></td>				
		
		</tr>
		 

		<?  
		}
		$temp_drawing_no = $drawing_no;

		if($temptot== $gtot)
		{
			$temptot=0;
		}
					
		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;
	}
	fputs($myfile,"\n");
	$kount++;
}										
	fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
?>
			 <tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td></tr>
<tr>
<td width="46" align="left"><font size="-1">&nbsp;</font></td>
<td width="35" align="left"><font size="-1">&nbsp;</font></td>
<td width="86" align="left"><font size="-1">&nbsp;</font></td>													
<td width="86" align="left"><font size="-1">&nbsp;</font></td>												
<td width="85" align="left"><font size="-1">&nbsp; </font></td>
<td width="34" align="right"><font size="-1">&nbsp;</font></td>				
<td width="47" align="right"><font size="-1">&nbsp;</font></td>

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

		<td width="45" align="right"><font size="-1" color="<? echo($clr);?>"><? if($gpacking_amt>0)  echo(number_format($gpacking_amt,2,'.',''));?></font></td>
		<td width="40" align="right"><font size="-1" color="<? echo($clr);?>"><? if($gbed_amt>0)  echo(number_format($gbed_amt,2,'.',''));?></font></td>
		<td width="48" align="right"><font size="-1" color="<? echo($clr);?>"><? if($gcess_amt>0)  echo(number_format($gcess_amt,2,'.',''));?></font></td>
		<td width="48" align="right"><font size="-1" color="<? echo($clr);?>"><? if($ghcess_amt>0)  echo(number_format($ghcess_amt,2,'.',''));?></font></td>        
		<td width="46" align="right"><font size="-1" color="<? echo($clr);?>"><? if($gfreight>0)  echo(number_format($gfreight,2,'.',''));?></font></td>								
		<td width="42" align="right"><font size="-1" color="<? echo($clr);?>"><? if($gvatamt>0)    echo(number_format($gvatamt,2,'.',''));?></font></td>

		<td width="56" align="right"><font size="-1" color="<? echo($clr);?>"><? if($net>0)    echo(number_format($net,2,'.',''));?></font></td>
<td width="39" align="right"><font size="-1"><? if($ground_off!=0)  echo($ground_off);?></font></td>								
<td width="59" align="right"><font size="-1"><? if($gross>0)  echo(number_format($gross,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,"    ");
	}
}
?>