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

<title>Ledger Printing</title>
<form>
<body bgcolor="#ffffff"  topmargin=0>

<font face="Arial, Helvetica, sans-serif">
<?

if(!($myfile=fopen("D:/temp/invoice.txt","w")))
{
  print("file couldnot be opened");
}
$fname = 'invoice.txt';
$totcr=0;
$totdr=0;
$query = "delete from pledger";
mysqli_query($conn,$query) or die (mysqli_error());

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

$linecnt=0;
$pgbrk=5;
$mcnt=1;
?>

<table width=918 border=0>
<thead>
<tr><td colspan="100%" align="center"><font face="Arial, Helvetica, sans-serif"><?=$compname?></font></td></tr>
<tr><td colspan="100%" align="center"><font face="Arial, Helvetica, sans-serif">Day book from&nbsp;<?=$startdt?>&nbsp;to&nbsp;<?=$enddt?></font></td></tr>
<tr> 

<td width="84"><font face="Arial, Helvetica, sans-serif">Date</font></td>
<td width="374"><font face="Arial, Helvetica, sans-serif">Description</font></td>
<td width="100"><div align="right"><font face="Arial, Helvetica, sans-serif">Receipt</font></div></td>
<td width="107"><div align="right"><font face="Arial, Helvetica, sans-serif">Payments</font></div></td>
<td width="121"><div align="right"><font face="Arial, Helvetica, sans-serif">Debit</font></div></td>
<td width="106"><div align="right"><font face="Arial, Helvetica, sans-serif">Credit</font></div></td>
</tr>
<tr><td  colspan="100%"><hr></td></tr>
</thead>
<tbody>
  <?
$tdbamt = 0;
$tcramt = 0;
?>
  <?
$mcnt++;
$linecnt++;
$tmp="";

$stdt = us($startdt);
$edt = us($enddt);
$tmpdt = $stdt;
$gcrtotal=0;
$gdbtotal=0;
$gpmttotal=0;
$grecpttotal=0;
while($tmpdt<=$edt)
{
	$dtchk = 'F';
	$totdb=0;
	$totcr=0;
	$result = mysqli_query($conn,"select cramt,dbamt,narration,acctcode,record from ledger where compcode = '$compcode' and date ='$tmpdt' order by date ");
	while($row=mysqli_fetch_row($result))
	{
	
		if($dtchk=='F')
		{
			?>
			<tr> 
			<Td colspan="100%" align="left"> <font face="Arial, Helvetica, sans-serif"> 
			<?=british($tmpdt)?>
			</font></Td>
			</tr>
			<?
			$dtchk = 'T';
		}
		$cramt = $row[0];
		$dbamt = $row[1];			
		$narration = addslashes($row[2]);
		$acctcode = $row[3];
		$record= $row[4];
		$cresult=mysqli_query($conn,"select description from acctmast where acctcode = '$acctcode'");
		$crow=mysqli_fetch_row($cresult);
		$cname = $crow[0];
		?>
		<tr> 
		<td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
		<td colspan="5"> <font face="Arial, Helvetica, sans-serif"> 
		<?=$cname?>
		</font></td>
		</tr>
		

		<tr> 
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></Td>
		<td> <font face="Arial, Helvetica, sans-serif"><a href = "editview.php?record=<?echo($row[4]);?>" target="_blank"> 
		<?=$narration?></a>
		</font></td>
		
		
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></Td>
		<td><font face="Arial, Helvetica, sans-serif">&nbsp;</font> </td>
		<td align="right"> <font face="Arial, Helvetica, sans-serif"> <? if($dbamt>0)echo($dbamt);?></font> </td>
		<td align="right"> <font face="Arial, Helvetica, sans-serif"> <? if($cramt>0)echo($cramt);?></font></td>
		</tr>
		<?
		$totdb = $totdb+$dbamt;
		$totcr = $totcr+$cramt;
		$gcrtotal= $gcrtotal + $cramt;
		$gdbtotal= $gdbtotal +$dbamt;
	}
	if($totdb>0)
	{
		?>
		<tr bgcolor="#D9F0AC"> 
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></Td>
		<td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></Td>
		<td><font face="Arial, Helvetica, sans-serif">&nbsp;</font> 
		<td align="right"> <font face="Arial, Helvetica, sans-serif"> 
		<? if($totdb>0)echo($totdb);?>
		</font> 
		<td align="right"> <font face="Arial, Helvetica, sans-serif"> 
		<? if($totcr>0)echo($totcr);?>
		</font></tr>
		<?
    }	
	$sec = mysqli_query($conn,"select '$tmpdt' +  interval 1 day ");
	$secondday = mysqli_fetch_row($sec);
	$tmpdt = substr($secondday[0],0,4).'-'.substr($secondday[0],5,2).'-'.substr($secondday[0],8,2);
}
?>
</tbody>
<tr> 
<td colspan="100%"><font face="Arial, Helvetica, sans-serif"><a href = "dload.php?dfile=<?echo($fname);?>">Download 
Text file</a></font></td>
</tr>
<tr> 
<td colspan="100%"><center><font color="#000080">
<font size="+1"><a href= "../../index.html">Return to Index </a></font></center></td>
</tr>
</table>
<?






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

?>
</font>