<?php include("../../include/dbconnect.php"); 
$delfile = "../../../../tmp/invoice.txt";

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

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

$pgno=1;
$kount=1;

$result=mysqli_query($conn," select name from company where  compcode='$compcode' order by name") or die(mysqli_error());
$row=mysqli_fetch_row($result);
$company=$row[0];

$string1='Purchase Register from '. $stdt. ' to  ' .$edt;


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

?>
<TABLE  width="972" BORDER=1 CELLPADDING=4 RULES=COLS  >






<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>	
<COLGROUP></COLGROUP>			
<thead>
<tr><td colspan="100%" align ="center"><h4><?=$company?></h4></td></tr>
<tr>
  <td colspan="100%" align ="center"><h5>
    <p>Purchase Register from &nbsp;<? echo($stdt);?> to  &nbsp;<? echo($edt);?>&amp;n</p>
    <p>&nbsp;(<?=$reporttype?>)</p></td></tr>

  <tr> 

    <td width="46" align="right"><font size="-1">Inv. No</font></td>
    <td width="73" align="right"><font size="-1">Inv. Date</font></td>
    <td width="73" align="right"><font size="-1">Entry. Date</font></td>    
    <td width="194" align="left"><div align="left"><font size="-1">Supplier Name 
        </font></div></td>
    <td width="231" align="left"><div align="left"><font size="-1">Description</font></div></td>
    <td width="77" align="right"><div align="right"><font size="-1">Qty</font></div></td>
        <td width="77" align="right"><div align="right"><font size="-1">Rate</font></div></td>
    <td width="58" align="right"><div align="right"><font size="-1">GST  %</font></div></td>
    <td width="56" align="right"><div align="right"><font size="-1">GST Amt</font></div></td>

    <td width="70" align="right"><div align="right"><font size="-1">Accessable Value 
        </font></div></td>
  </tr>
  <tr> 
    <td><hr></td>
    <td><hr></td>
    <td><hr></td>
    <td><hr align="left"></td>
    <td><hr align="left"></td>
    <td><hr align="right"></td>

    <td><hr align="right"></td>
    <td><hr align="right"></td>
    <td><hr align="right"></td>
        <td><hr align="right"></td>
  </tr></thead>
  <?
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;
	$gtaxamt=0;
	$ge1taxamt=0;
	$ground_off=0;
	$gtot=0;
	$net =0;
	
$todate = $stdt;
$temp = $stdt;
$startdt = us($stdt);

$temp=$edt;
$enddt = us($edt);

$temp=' ';
$mytrans='';
if($reporttype == 'Listed by Invoice Date')
{
$invresult= mysqli_query($conn,"select distinct poinv_no,poinv_no,poinv_date,bcode,prodcode,quantity,rate,vat_amount,vat_percent,value,trans_id,exp_date from purchasereg  where poinv_date >='$startdt' and poinv_date <='$enddt'    order by poinv_date ")or die(mysqli_error());
}
else
{
$invresult= mysqli_query($conn,"select distinct poinv_no,poinv_no,poinv_date,bcode,prodcode,quantity,rate,vat_amount,vat_percent,value,trans_id,exp_date from purchasereg  where exp_date >='$startdt' and exp_date <='$enddt'   order by poinv_date ")or die(mysqli_error());
	
}
while($invrow = mysqli_fetch_row($invresult))
{
	$date = british($invrow[0]);
	$invno = $invrow[1];
	$inv_date = british($invrow[2]);
	$bcode = $invrow[3];
	$prodcode = $invrow[4];	
	$qty = $invrow[5];	
	$rate = $invrow[6];	
	$taxamt = $invrow[7];	
	$vat_percent = $invrow[8];	
	$value = $invrow[9];	
	$trans_id  = $invrow[10];
	$exp_date = british($invrow[11]);

//	if($taxcode == 'VAT')
//	{
		$vatamt=$taxamt;
		$vat_total = $vat_total + $taxamt;				
//	}
//	elseif($taxcode == 'CST' )
//	{
//		$cstamt =$taxamt;
//		$cst_total = $cst_total + $taxamt;			
//	}
	
	$vatcess_total = $vatcess_total + $cessamt ;
//	$cst_total = $cst_total + $cst_amt ;
	$grtotal = $grtotal + $value;

	
	
		$bslct= mysqli_query($conn,"select name from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$bname = $brow[0];
		
		$bslct= mysqli_query($conn,"select prodname from product where prodcode = '$prodcode' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$prodname = $brow[0];		

		?>
  <tbody>
    <tr> 
	<?
	if($mytrans  != $trans_id)
	{
	?>

    <td width="46" align="right"><font size="-1"><?=$invno?></font></td>
    <td width="73" align="right"><font size="-1"><?=$inv_date?></font></td>
    <td width="73" align="right"><font size="-1"><?=$exp_date?></font></td>    
    <td width="194" align="left"><div align="left"><font size="-1"><?=$bname?></font></div></td>
	<?
	$mytrans = $trans_id;
	}
	else
	{
	?>
	<td>&nbsp;</td>	<td>&nbsp;</td>	<td>&nbsp;</td>
	<?
	}
	?>
    <td width="231" align="left"><div align="left"><font size="-1"><?=$prodname?></font></div></td>
    <td width="77" align="right"><div align="right"><font size="-1"><? if($qty>0) echo $qty; ?></font></div></td>
        <td width="77" align="right"><div align="right"><font size="-1"><? if($rate>0) echo $rate; ?></font></div></td>
    <td width="58" align="right"><div align="right"><font size="-1"><? if($vat_percent>0) echo $vat_percent;?></font></div></td>
    <td width="56" align="right"><div align="right"><font size="-1"><? if($vatamt>0) echo $vatamt; ?></font></div></td>

    <td width="70" align="right"><div align="right"><font size="-1"><? if($value>0) echo $value;?></font></div></td>
    </tr>
  </tbody>
  <?  


		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($qty>0){	numcontrol($myfile,$qty,10);} else{ strcontrol($myfile,'',10);}				
		if($vat_percent>0){numcontrol($myfile,$vat_percent,10);}else{ strcontrol($myfile,'',10);}
		if($vatamt>0){numcontrol($myfile,$vatamt,10);}else{ strcontrol($myfile,'',10);}
		if($cstamt>0){numcontrol($myfile,$cstamt,10);} 	else{ strcontrol($myfile,'',10);}			
		if($value!=0){numcontrol($myfile,$value,12);}	else{ strcontrol($myfile,'',8);}				
		
		fputs($myfile,"\n");
		$kount++;					
		$temp=$invno;
		$vatamt=0;
		$cstamt=0;
	
	fputs($myfile,"\n");
	$kount++;
}										
	fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
?>
  <tr> 
    <td><hr></td>
    <td><hr></td>
    <td><hr></td>
    <td><hr align="left"></td>
    <td><hr align="left"></td>
    <td><hr align="right"></td>
    <td><hr align="right"></td>
        <td><hr align="right"></td>
    <td><hr align="right"></td>

    <td><hr align="right"></td>
  </tr>
  <tr> 
    <td width="46" align="center"><font size="-1">&nbsp;  </font></td>
    <td width="46" align="center"><font size="-1">&nbsp;  </font></td>
    <td width="73" align="left"><font size="-1">&nbsp;  </font></td>
    <td width="55" align="left"><font size="-1">&nbsp;  </font></td>
    <td width="55" align="left"><font size="-1">&nbsp;  </font></td>
    <td width="46" align="center"><font size="-1">&nbsp;  </font></td>
    <td width="73" align="left"><font size="-1">&nbsp;  </font></td>


    <td width="56" align="right"><div align="right"><font size="-1">&nbsp;</font></div></td>
    <td width="58" align="right"><div align="right"><font size="-1"><?=$vat_total?></font></div></td>
	    <td width="70" align="right"><div align="right"><font size="-1"><?=$grtotal?></font></div></td>
  </tr>
</table>

  
			 
<?
strcontrol($myfile,"",98);

numcontrol($myfile,number_format($vat_total,2),12);
numcontrol($myfile,$vatcess_total,10);
numcontrol($myfile,$cst_total,7);
	
numcontrol($myfile,$grtotal,12);
fputs($myfile,"\n");
$kount++;					
?>


</table>

    <br>

                                                                             
</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                                Qty    VAT     Vat Cess   CST.        Value  \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,"    ");
	}
}
?>