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

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

?>



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

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





$stdt = $startdt;
$startdt = us($startdt);
$edt = $enddt;
$enddt = us($enddt);
$months = get_months($startdt, $enddt);

$linecnt=0;
$pgbrk=5;
$mcnt=1;
?>
</font>
<TABLE  width="780" BORDER=1 CELLPADDING=4 RULES=COLS  >

<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>

				
<thead>
<tr>
<td height="34" colspan="100%" ALIGN ="left"><h4><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?echo($compname);?></font></h4></td></tr>
<tr>
      <td colspan="100%"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Monthwise
        Summary From <? echo($stdt);?> To <?echo($edt);?></font></td>
    </tr>
<tr bgcolor="#DAF1A9">

<td width="312" align=center><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Particulars</font></td>


<td width="79" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Debit </font></div></td>
<td width="91" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Credit</font></div></td>
<td width="87" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Closing</font></div></td>
</tr>
<tr>



<td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>
</tr>

</thead>
<tbody>
<?

for ($grpind=0;$grpind < count($acctcode);$grpind++)
{
	$acresult = mysqli_query($conn,"select acctcode,description from acctmast where acctcode = '$acctcode[$grpind]'") or die(mysqli_error());
	$acrow = mysqli_fetch_row($acresult);
	
	$acode = $acrow[0];
	$description=$acrow[1];
	
	$tdbamt = 0;
	$tcramt = 0;
	$opbal=0;
	$clb=0;
	$closing=0;
	$opresult = mysqli_query($conn,"select opening_balance,db_cr from acctmast where acctcode = '$acode'") or die(mysqli_error());
	$oprow = mysqli_fetch_row($opresult);

	$amt = $oprow[0];
	

	if($oprow[1] =='DR')
	{
		$opbal = $amt;
	}
	elseif($oprow[1] == 'CR')
	{

		$opbal = $amt*-1;
	}
	

	
	$msresult = mysqli_query($conn,"select sum(cramt+dbamt) from ledger where compcode = '$compcode' && acctcode = '$acode' order by date ");
	$msrow = mysqli_fetch_row($msresult);
	$chck_amt = $msrow[0]+$amt;
	if($chck_amt>0)
	{	
	$opcheck = 'F';
		?>		
		
	<tr>
		<td colspan="100%"><font face="Arial, Helvetica, sans-serif"><?=$description?></font></td>
			
<?	
	
		for($ind=0;$ind<count($months);$ind++)
		{
			if($months[$ind] != $months[$ind+1])
			{
				$mon = substr($months[$ind],0,2);
				$year = substr($months[$ind],3,4);
				$first_day = $year.'-'.$mon.'-'.'01';
				$last_day = current_month($first_day);
				$time3  = strtotime($first_day);
				$mn     = date('M', $time3);
				
				if($opcheck  == 'F')
				{
				
				$mpresult = mysqli_query($conn,"select sum(dbamt),sum(cramt) from ledger where date <'$first_day'  and compcode='$compcode' and acctcode = '$acode' ")or die(mysqli_error());
				$mprow = mysqli_fetch_row($mpresult);
				$ocramt = $mprow[0];
				$odbamt = $mprow[1];
				$opbal = $opbal + $odbamt - $ocramt;					
					?>		
					<tr>
					
					<td align=left><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Opening Balance </font></td>
					
					<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
					<?if($opbal >=0) {echo(number_format($opbal,2,'.',''));}else {echo("   ");}?>
					</font></td>
					<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
					<?if($opbal< 0) {echo(number_format($opbal*-1,2,'.',''));}else {echo("   ");}?>
					</font></td>
					<td align="right"><font face="Arial, Helvetica, sans-serif"><?if($amt!= 0) {echo(number_format($amt,2).'&nbsp;'.$oprow[1]);}else {echo("   ");}?></font></td>
					</tr>
					<?
				
				
					$opcheck  = 'T';	
				}
				
				
				
				
				
				
				
				
				
				
				
				$presult = mysqli_query($conn,"select sum(dbamt),sum(cramt) from ledger where date >='$first_day' and date < '$last_day'  and compcode='$compcode' and acctcode = '$acode' ")or die(mysqli_error());
				$prow = mysqli_fetch_row($presult);
				$cramt = $prow[0];
				$dbamt = $prow[1];
				$clb = $opbal + $dbamt - $cramt;		


		
				if($clb >0)
				{
					$closing = $clb;
					$ctype = 'DR';
				}
				elseif($clb < 0)
				{
					$closing = $clb*-1;
					$ctype = 'CR';
				}			
				?>
				<tr>
				<td align=left><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$mn.'&nbsp;'.$year?></font></td>
				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? if($dbamt!=0) echo $dbamt;?> 
				</font></td>
				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? if($cramt!=0) echo $cramt;?>
				</font></td>
				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? echo(number_format($closing,2).'&nbsp;'.$ctype);?>
				</font></td>
				</tr>
				</tbody>
				<?
				$mon_year = $mn.' '.$year;
			
			}
		}		
				
	}  // end of check amount if loop
}

?>		

</table>


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

/*  FUNCTION TO PRINT THE HEADING */




function get_months($date1, $date2) {
   $time1  = strtotime($date1);
   $time2  = strtotime($date2);
   $my     = date('mY', $time2);

   $months = array(date('m,Y', $time1));

   while($time1 < $time2) {
      $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
      if(date('mY', $time1) != $my && ($time1 < $time2))
         $months[] = date('m,Y', $time1);
		 
   }

   $months[] = date('m,Y', $time2);
   return $months;
} 

?>
</font>