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

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

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


$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);
$company = $row[0];
//acctcode='$cashcode'
?>

<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>
  <?
  $opdbamt =0;
  $opcramt =0;
$tdbamt = 0;
$tcramt = 0;
$linecnt=1;
$pgbrk=5;
$mcnt=1;

$mcnt++;
$linecnt++;
$tmp="";

$stdt = us($startdt);
$edt = us($enddt);
$tmpdt = $stdt;
$gcrtotal=0;
$gdbtotal=0;
$gpmttotal=0;
$grecpttotal=0;
 

$linecnt = 8;
$page_no++;
$result = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'CASH' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$cashcode = $row[0];

$op = mysqli_query($conn,"select opening_balance, db_cr from openbalance where acctcode = '$cashcode' and account_year = '$account_year' and compcode = '$compcode' ") or die(mysqli_error());
$op_row=mysqli_fetch_row($op);
$opening_balance = $op_row[0];
$db_cr = $op_row[1];
if($db_cr == 'DR')
{
	$opdbamt = $op_row[0];
}
elseif($db_cr == 'CR')
{
	$opcramt = $op_row[1];
}


$opresult = mysqli_query($conn,"select sum(cramt),sum(dbamt) from ledger where compcode = '$compcode' and date <'$tmpdt'and  acctcode = '$cashcode' and account_year = '$account_year' order by date ") or die(mysqli_error());
$oprow=mysqli_fetch_row($opresult);
$opdbamt = $opdbamt + $oprow[1];
$opcramt = $opcramt + $oprow[0];

$op_bal =  $opdbamt - $opcramt;		
   
	
?>


		<tr bgcolor="#D9F0AC"> 
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp; </font></Td>
		<td><font face="Arial, Helvetica, sans-serif">Opening Balance</font></td>
		<Td><div align="right"><font face="Arial, Helvetica, sans-serif">
		  <? if($op_bal>=0)echo(number_format($op_bal,2,'.',''));?>  
		  </font></div></Td>
		<td><div align="right"><font face="Arial, Helvetica, sans-serif">
		  <? if($op_bal<0)echo(number_format($op_bal*-1,2,'.',''));?>
		  </font> 
		  </div>
		<td align="right"> <font face="Arial, Helvetica, sans-serif">&nbsp; </font> </Td>
		<td align="right"> <font face="Arial, Helvetica, sans-serif">&nbsp;  </font> </Td>
         </tr>

<?


  
if($op_bal >0)
{
  $Ctotdb = $op_bal;
  $Ctotcr=0;
}
elseif($op_bal<0)
{
  $Ctotcr = $op_bal*-1;
  $Ctotdb=0;
}
while($tmpdt<=$edt)
{
	$dtchk = 'F';
	$totdb=0;
	$totcr=0;
	$result = mysqli_query($conn,"select cramt,dbamt,narration,acctcode from ledger where compcode = '$compcode' and date ='$tmpdt' order by date ");
	while($row=mysqli_fetch_row($result))
	{
	    $pos = 0;
		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];
		$cresult=mysqli_query($conn,"select description from acctmast where acctcode = '$acctcode'");
		$crow=mysqli_fetch_row($cresult);
		$cname = $crow[0];

		$amt = $amt + $cramt;
		$amt1 = $amt1 + $dbamt;
		

		?>
		<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>

     	<? 
		
		if($acctcode == $cashcode)
		{

			if($dbamt>0) 
			{
				?>
				<Td><div align="right"><font face="Arial, Helvetica, sans-serif"><? echo($dbamt); ?></font></div></Td><td colspan="3">&nbsp;</td>
				<?
				$Ctotdb= $Ctotdb + $dbamt;
			}
			elseif($cramt>0)
			{
				?>
				<td>&nbsp;</td><Td><div align="right"><font face="Arial, Helvetica, sans-serif"><? echo($cramt); ?></font></div></Td><td colspan="2">&nbsp;</td>
				<?
				$Ctotcr = $Ctotcr+$cramt;
			}
		 }
		 else
		 {
		 	?>
					 <td colspan="2">&nbsp;</td>
			<?
			
		 	if($dbamt>0) 
			{
				?>
				<Td><div align="right"><font face="Arial, Helvetica, sans-serif"><? echo($dbamt); ?></font></div></Td><td>&nbsp;</td>
				<?
			}
			elseif($cramt>0)
			{
				?>
				<td>&nbsp;</td><Td><div align="right"><font face="Arial, Helvetica, sans-serif"><? echo($cramt); ?></font></div></Td>
				<?
			}
		 }

		?> 
		</tr>
		<?
		if (($acctcode=='$cashcode') )
		{
		$Ctotdb = $Ctotdb+$dbamt;
		$Ctotcr = $Ctotcr+$cramt;
		$Cgcrtotal= $Cgcrtotal + $cramt;
		$Cgdbtotal= $Cgdbtotal +$dbamt;
		}
		if ( $acctcode!='$cashcode')
		{
		$totdb = $totdb+$dbamt;
		$totcr = $totcr+$cramt;
		$gcrtotal= $gcrtotal + $cramt;
		$gdbtotal= $gdbtotal +$dbamt;
		}
	}

		if($dtchk=='T')
	    {
		?>
		<tr bgcolor="#D9F0AC"> 
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp; </font></Td>
		<td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
		<Td><div align="right"><font face="Arial, Helvetica, sans-serif">
		  <? if($Ctotdb>0)echo(number_format($Ctotdb,2,'.',''));?>
		  </font></div></Td>
		<td><div align="right"><font face="Arial, Helvetica, sans-serif">
		  <? if($Ctotcr>0)echo(number_format($Ctotcr,2,'.',''));?>
		  </font> 
		  </div>
		<td align="right"> <div align="right"><font face="Arial, Helvetica, sans-serif"> 
		  <? if($totdb>0)echo(number_format($totdb,2,'.',''));?>
		  </font> 
		  </div>
		<td align="right"> <div align="right"><font face="Arial, Helvetica, sans-serif"> 
		  <? if($totcr>0)echo(number_format($totcr,2,'.',''));?>
	    </font></div>		</tr>
		<?

    	$clbal = $Ctotdb-$Ctotcr;

        ?>
		<tr bgcolor="#D9F0AC"> 
		<Td><font face="Arial, Helvetica, sans-serif">&nbsp; </font></Td>
		<td><font face="Arial, Helvetica, sans-serif"> Balance</font></td>
		<Td><div align="right"><font face="Arial, Helvetica, sans-serif">
		  <? if($clbal>=0)echo(number_format($clbal,2,'.',''));?>
		  </font></div></Td>
		<td><div align="right"><font face="Arial, Helvetica, sans-serif">
		  <? if($clbal<0)echo(number_format($clbal*-1,2,'.',''));?>
		  </font> 
		  </div>
		<td align="right"> <div align="right"><font face="Arial, Helvetica, sans-serif"> </font> </div></Td>
		<td align="right"> <div align="right"><font face="Arial, Helvetica, sans-serif">  </font> </div></Td>
    </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%"><center><font color="#000080">
<font size="+1"><a href= "../../index.html">Return to Index </a></font></center></td>
</tr>
</table>

