<?php //error_reporting(0);
$PageSecurity = 3;
include ('includes/session.inc');
include ('includes/function_lib.php');
include('includes/fun-php.php');
include('includes/fun-HRClass.php');
include('Account/fun-Trial.php');
global $db;
echo '<link href="'.$rootpath. '/css/'. $_SESSION['Theme'] .'/default.css" rel="stylesheet" type="text/css" />';

$get_financial_yr=DB_query("Select * from companies where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$result=DB_fetch_array($get_financial_yr);

$fin_year_start=$result['AccountingYearStart'];
$fin_year_end=$result['AccountingYearEnd'];

$fin_year_start_brk=explode('-',$fin_year_start);
$year1=$fin_year_start_brk[0];
$fin_year_end_brk=explode('-',$fin_year_end);
$year2=$fin_year_end_brk[0];

$month_array=array('04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December','01'=>'January','02'=>'February','03'=>'March',);
//print_r($month_array);
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
	<div class="tab_div">
<?php if(empty($_REQUEST['m']) && empty($_REQUEST['y']) && $_REQUEST['vouchers']=='')
{
	$a=explode('-',$_SESSION['financialYrStart']);
	$b=explode('-',$_SESSION['financialYrEnd']);
	$openingSql=DB_query("Select openingtype,openingBalance from accountledger where AccountsID=".$_REQUEST['q']."",$db);
	$openingRow=DB_fetch_array($openingSql);
	$openingType=$openingRow['openingtype'];
	if($openingType=='DR'){$open_DR=$openingRow['openingBalance']; $OPD=$open_DR;}
	elseif($openingType=='CR'){$open_CR=$openingRow['openingBalance']; $OPD=$open_CR;}
	else{$open_CR='';$open_DR='';}
	if($_REQUEST['fdate']=='' && $_REQUEST['tdate']=='')
	{
?>
<h1><strong>Monthly Transactions</strong></h1>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:1px solid #808080; border-collapse:collapse;">
<tr>
<th width="30%">&nbsp;</th>
<th><div align="right"><strong>Opening</strong></div></th>
<th width="15%"><div align="right"><strong>Debit</strong></div></th>
<th width="15%"><div align="right"><strong>Credit</strong></div></th>
<th width="15%"><div align="right"><strong>Closing</strong></div></th>
</tr>
<tr bgcolor="#FFFFFF">
<td><strong>Opening as on <?php echo ConvertSQLDate($_SESSION['financialYrStart'])."&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;".$OPD."&nbsp;".$openingType;;?></strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?php 
$checkEntries=DB_fetch_array(DB_query("SELECT MONTH(MAX(Dates)) as lastmonth FROM account_book JOIN account_book_details 
ON (account_book.id=account_book_details.account_bookid)
WHERE account_book_details.ledgerid=".$_REQUEST['q']." and cancel_status=0",$db));
$lastEntryMonth=$checkEntries['lastmonth'];
if($lastEntryMonth<4){$lastEntryMonthNum=$lastEntryMonth+12;}else{$lastEntryMonthNum=$lastEntryMonth;}
$array_CR=array();
$array_DR=array();
$array_CR_closing=array();
$array_DR_closing=array();
foreach($month_array as $key=>$val)
{
	if($key>='04' && $key<='12'){$yr=$a[0];}elseif($key>='01' && $key<'03'){$yr=$b[0];}	

		if($key<4){
		$month_num=$key+12;}else
		{
			if(substr($key,0,1)=='0')
			{$month_num=substr($key,-1);}
			else{$month_num=$key;}
		}
		
		
		if($key==1)
    	{
    		$lastmonth=12;
			$lastyr=$yr-1;
    	}
   		else
    	{	
    		$lastmonth=$key-1;
			$lastyr=$yr;
    	}	
	$total_CR=0;
	$total_DR=0;
	
	$get_month_total="Select A.*,B.* from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and MONTH(Dates)='".$key."' and YEAR(Dates)='".$yr."' and cancel_status=0 ";
	$get_total_result=DB_query($get_month_total,$db);
	while($get_total=DB_fetch_array($get_total_result))
	{
		$acc_type=$get_total['amount_type'];
		if($acc_type=='CR')
		{$total_CR=$total_CR+$get_total['amount'];}
		elseif($acc_type=='DR'){$total_DR=$total_DR+$get_total['amount'];}	
	}	
?> 
  <tr bgcolor="#fff">
    <td><a href="cash_bank_popup.php?q=<?php echo $_REQUEST['q'];?>&m=<?php echo $key;?>&y=<?php echo $yr;?>&mwords=<?php echo $val;?>"><strong><?php echo $val; echo ", ".$yr;
	//echo "<br />";
	
	if($month_num<=$lastEntryMonthNum){
	$currentMonthOpeningDR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'DR',$key);
	$currentMonthOpeningCR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'CR',$key);
	//echo "<br /><br />--------<br /><br />";
	//echo $currentMonthOpeningDR;
	//echo "<br />".$currentMonthOpeningCR;
	$TotalDROpening=$currentMonthOpeningDR+$open_DR;
	$TotalCROpening=$currentMonthOpeningCR+$open_CR;
	//echo "<br /><br />--------<br /><br />";
	//echo $TotalDROpening;
	//echo "<br />".$TotalCROpening;
	if($TotalDROpening>$TotalCROpening){$OP=$TotalDROpening-$TotalCROpening; $OPTYPE="DR"; $OPDR=$OP; $OPCR='';}
	elseif($TotalCROpening>$TotalDROpening){$OP=$TotalCROpening-$TotalDROpening; $OPTYPE="CR"; $OPCR=$OP; $OPDR='';}
	//echo "<br /><br />final----------"; echo $OP."&nbsp;". $OPTYPE;
	}
	else{$OP='';$OPTYPE='';}
	?></strong></a></td>
	<td><div align="right"><strong><?php echo $OP."&nbsp;".$OPTYPE;?></strong></div></td>
    <td><div align="right"><strong><?php echo $total_DR; $array_DR[]=$total_DR;?></strong></div></td>
    <td><div align="right"><strong><?php echo $total_CR; $array_CR[]=$total_CR;?></strong></div></td>
    <td><div align="right"><strong>
      <?php 
	  if($month_num<=$lastEntryMonthNum){
		$GtotalDR=$OPDR+$total_DR;
		$GtotalCR=$OPCR+$total_CR;
	  if($GtotalDR>$GtotalCR){echo $closing_DR=$GtotalDR-$GtotalCR." DR"; $array_DR_closing[]=$closing_DR;}elseif($GtotalCR>$GtotalDR){echo $closing_CR=$GtotalCR-$GtotalDR." CR"; $array_CR_closing[]=$closing_CR;}}else{echo "";;}?>
    </strong></div></td>
  </tr>
  <?php } ?>
  <tr bgcolor="#fff">
    <td><div align="right"><strong>Grand Total</strong></div></td>
	<td>&nbsp;</td>
    <td><div align="right"><strong><?php echo array_sum($array_DR);//print_r($array_DR); ?></strong></div></td>
    <td><div align="right"><strong><?php echo array_sum($array_CR);//print_r($array_CR);?></strong></div></td>
    <td><div align="right"><strong><?php 
	//print_r($array_DR_closing); print_r($array_CR_closing);
	/*$sum_DR=array_sum($array_DR_closing); 
	$sum_CR=array_sum($array_CR_closing);
	if($sum_DR>$sum_CR){echo $sum_DR-$sum_CR." DR";}elseif($sum_CR>$sum_DR){echo $sum_CR-$sum_DR." CR";}*/
	
	?></strong></div></td>
  </tr>
</table>
<p align="center"><a href="javascript:window.close();"><input type="button" value="Close" class="search" /></a></p>
<?php 
}
	else
	{
?>
<p align="right"><a href="javascript:history.go(-1);"><input type="button" value="Go Back" /></a></p>
<h1><strong>Monthly Transactions between <?php echo ConvertSQLDate($_REQUEST['fdate']);?> and <?php echo ConvertSQLDate($_REQUEST['tdate']);?></strong></h1>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:1px solid #808080; border-collapse:collapse;">
<tr>
<th width="30%">&nbsp;</th>
<th><div align="right"><strong>Opening</strong></div></th>
<th width="15%"><div align="right"><strong>Debit</strong></div></th>
<th width="15%"><div align="right"><strong>Credit</strong></div></th>
<th width="15%"><div align="right"><strong>Closing</strong></div></th>
</tr>
<tr bgcolor="#FFFFFF">
<td><strong>Opening as on <?php echo ConvertSQLDate($_SESSION['financialYrStart'])."&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;".$OPD."&nbsp;".$openingType;?></strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?php 
	$prevdate=date('Y-m-d', strtotime('-1 day', strtotime($_REQUEST['fdate'])));
	$fdate=$_REQUEST['fdate'];
	$tdate=$_REQUEST['tdate'];
	$fdate_brk=explode('-',$fdate);
	$tdate_brk=explode('-',$tdate);
	$fmnth=$fdate_brk[1];
	$tmnth=$tdate_brk[1];
	$fyr=$fdate_brk[0];
	$tyr=$tdate_brk[0];
	
	$from_month_brk=substr($fmnth,0,1);
	$to_month_brk=substr($tmnth,0,1);
	
	if($from_month_brk=='0'){$from_month=substr($fmnth,-1);}else{$from_month=$fmnth;}
	if($to_month_brk=='0'){$to_month=substr($tmnth,-1);}else{$to_month=$tmnth;}
	if($to_month<4){$to_month_num=$to_month+12;}else{$to_month_num=$to_month;}
	if($from_month<4){$from_month_num=$from_month+12;}else{$from_month_num=$from_month;}
	
	$array_CR=array();
	$array_DR=array();
	$array_CR_closing=array();
	$array_DR_closing=array();		
	foreach($month_array as $k=>$v)
	{
		$month=$k;
		if($k<4){
		$month_num=$k+12;}else
		{
			if(substr($k,0,1)=='0')
			{$month_num=substr($k,-1);}
			else{$month_num=$k;}
		}
		if($k>='04' && $k<='12'){$yr=$a[0];}elseif($k>='01' && $k<'03'){$yr=$b[0];}
		if($k==1)
    	{
    		$lastmonth=12;
			$lastyr=$yr-1;
    	}
   		else
    	{	
    		$lastmonth=$k-1;
			$lastyr=$yr;
    	}		
		
		$color='#CCCCCC';
		$flag1='';
		if($fmnth==$tmnth)
		{
			if($fmnth==$month)
			{
				$flag1=1;
			}
		}
		elseif($fmnth!=$tmnth)
		{
			if($fmnth==$month){$flag1=2;}
			elseif($tmnth==$month){$flag1=3;}
			elseif($month_num<$to_month_num && $month_num>$from_month_num)
			{$flag1=4;}
		}
		else{$flag1='';}
		
		if(isset($flag1)==1)
		{
			if($flag1==1)
			{$color='#fff';
				$get_month_total="Select A.*,B.* from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and A.Dates between '".$fdate."' and '".$tdate."' and cancel_status=0 ";
				$get_total_result=DB_query($get_month_total,$db);
			}
			elseif($flag1==2)
			{$color='#fff';
				$get_month_total="Select A.*,B.* from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and A.Dates >= '".$fdate."' and MONTH(A.Dates)='$month' and YEAR(A.DAtes)='".$fyr."' and cancel_status=0 ";
				$get_total_result=DB_query($get_month_total,$db);
			}
			elseif($flag1==3)
			{$color='#fff';
				$get_month_total="Select A.*,B.* from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and A.Dates <= '".$tdate."' and MONTH(A.Dates)='$month' and YEAR(A.DAtes)=$tyr and cancel_status=0";
				$get_total_result=DB_query($get_month_total,$db);
			}
			elseif($flag1==4)
			{$color='#fff';
				$get_month_total="Select A.*,B.* from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and MONTH(A.Dates)='$month' and YEAR(A.DAtes)=$yr and cancel_status=0";
				$get_total_result=DB_query($get_month_total,$db);
			}
			else{$get_month_total='';}
		}
		$total_CR=0;
		$total_DR=0;
		
		if($get_month_total!=''){
		while($get_total=DB_fetch_array($get_total_result)) 
		{
			$acc_type=$get_total['amount_type'];
			if($acc_type=='CR')
			{$total_CR=$total_CR+$get_total['amount'];}
			elseif($acc_type=='DR'){$total_DR=$total_DR+$get_total['amount'];}	
		}}
		
		if($flag1==1){$link="cash_bank_popup.php?q=".$_REQUEST['q']."&from=$fdate&to=$tdate&vouchers=1";}
		elseif($flag1==2){$link="cash_bank_popup.php?q=".$_REQUEST['q']."&from=$fdate&vouchers=1";}
		elseif($flag1==3){$link="cash_bank_popup.php?q=".$_REQUEST['q']."&to=$tdate&vouchers=1";}
		elseif($flag1==4){$link="cash_bank_popup.php?q=".$_REQUEST['q']."&month=".$month."&y=".$yr."&vouchers=1";}
		if($flag1!=''){
		?><!--<a href="<?php //echo $link;?>"></a>-->
		<tr bgcolor="<?php echo $color;?>">
		<td><a href="<?php echo $link;?>"><strong><?php echo $month_name = date( 'F', mktime(0, 0, 0, $month) ); 
		//echo "---".$flag1;
		if($flag1==1 || $flag1==2)
		{
			$currentMonthOpeningDR=monthlyLedgerAccount($_REQUEST['q'],'','','DR',$key,$prevdate);
			$currentMonthOpeningCR=monthlyLedgerAccount($_REQUEST['q'],'','','CR',$key,$prevdate);
		}
		elseif($flag1==3 || $flag1==4)
		{
			$currentMonthOpeningDR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'DR',$key);
			$currentMonthOpeningCR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'CR',$key);
		}
		//echo "<br /><br />--------<br /><br />";
		//echo $currentMonthOpeningDR;
		//echo "<br />".$currentMonthOpeningCR;
		$TotalDROpening=$currentMonthOpeningDR+$open_DR;
		$TotalCROpening=$currentMonthOpeningCR+$open_CR;
		//echo "<br /><br />--------<br /><br />";
		//echo $TotalDROpening;
		//echo "<br />".$TotalCROpening;
		if($TotalDROpening>$TotalCROpening){$OP=$TotalDROpening-$TotalCROpening; $OPTYPE="DR"; $OPDR=$OP; $OPCR='';}
		elseif($TotalCROpening>$TotalDROpening){$OP=$TotalCROpening-$TotalDROpening; $OPTYPE="CR"; $OPCR=$OP; $OPDR='';}
		
		?></strong></a>
		</td>
		<td><div align="right"><strong><?php echo $OP."&nbsp;".$OPTYPE;?></strong></div></td>
		<td><div align="right"><strong><?php echo $total_DR; $array_DR[]=$total_DR;?></strong></div></td>
		<td><div align="right"><strong><?php echo $total_CR; $array_CR[]=$total_CR;?></strong></div></td>
		<td><div align="right"><strong><?php 
		$GtotalDR=$OPDR+$total_DR;
		$GtotalCR=$OPCR+$total_CR;
		if($GtotalDR>$GtotalCR){echo $closing_DR=$GtotalDR-$GtotalCR." DR"; $array_DR_closing[]=$closing_DR;}
		elseif($GtotalCR>$GtotalDR){echo $closing_CR=$GtotalCR-$GtotalDR." CR"; $array_CR_closing[]=$closing_CR;}?></strong></div></td>
		</tr>
		<?php }
	}
	?>
	<tr bgcolor="#fff">
    <td><div align="right"><strong>Grand Total</strong></div></td>
	<td></td>
    <td><div align="right"><strong><?php echo array_sum($array_DR);//print_r($array_DR); ?></strong></div></td>
    <td><div align="right"><strong><?php echo array_sum($array_CR);//print_r($array_CR);?></strong></div></td>
    <td><div align="right"><strong><?php 
	//print_r($array_DR_closing); print_r($array_CR_closing);
	/*$sum_DR=array_sum($array_DR_closing); 
	$sum_CR=array_sum($array_CR_closing);
	if($sum_DR>$sum_CR){echo $sum_DR-$sum_CR." DR";}elseif($sum_CR>$sum_DR){echo $sum_CR-$sum_DR." CR";}*/
	
	?></strong></div></td>
  </tr></table>
  <p align="center"><a href="javascript:window.close();"><input type="button" value="Close" class="search" /></a></p>
	<?php }
} 

if($_REQUEST['m']!='' && $_REQUEST['y']!='' && $_REQUEST['vouchers']=='')
{
	echo "111";
	
	$ledger_name=$s->GetFieldValue('accountledger','AccountsID="'.$_REQUEST['q'].'"','AccountName');
	 $ledger_opening=DB_query("Select openingtype,openingBalance from accountledger where AccountsID='".$_REQUEST['q']."'",$db);
	 $opening_row=DB_fetch_array($ledger_opening);
	 if($opening_row['openingtype']=='CR'){$CR_opening=$opening_row['openingBalance'];}else{$CR_opening='';}
	 if($opening_row['openingtype']=='DR'){$DR_opening=$opening_row['openingBalance'];}else{$DR_opening='';}	 
	 
	if($_REQUEST['m']>=1 && $_REQUEST['m']<=9){$month="0".$_REQUEST['m'];}else{$month=$_REQUEST['m'];}
	$yr=$_REQUEST['y'];
	
	if($_REQUEST['m']==1)
    {
    	$lastmonth=12;
		$lastyr=$yr-1;
    }
   	else
    {	
    	$lastmonth=$_REQUEST['m']-1;
		$lastyr=$yr;
    }	
	
	$get_transactions="Select A.*,B.*,A.id as parent_id from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and MONTH(Dates)='".$month."' and YEAR(Dates)='".$yr."' and cancel_status=0 order by A.Dates asc";
	$get_transactions_res=DB_query($get_transactions,$db);
	//echo $get_transactions;
?>
<p align="right"><a href="javascript:history.go(-1);"><input type="button" value="Go Back" /></a></p>
<h1><strong>Transactions for the month of <?php echo $_REQUEST['mwords'];?></strong></h1>
<table width="100%" border="1" cellspacing="0" cellpadding="3" align="center" style="border:solid 1px #808080; border-collapse:collapse;">
<tr><th colspan="7"><strong><font size="+1">Ledger : <?php echo $ledger_name;?></font></strong></th></tr>
<tr>
<th width="15%"><strong>Date</strong></th>
<th><strong>Particulars</strong></th>
<th width="20%"><strong>Voucher No.</strong></th>
<th width="10%"><strong>Voucher Type</strong></th>
<th width="20%"><strong>Debit Amount(INR)</strong></th>
<th width="20%"><strong>Credit Amount(INR)</strong></th>
<th width="5%"><strong>View</strong></th>
</tr>
<?php 
$DR_arr='';
$CR_arr='';
while($row=DB_fetch_array($get_transactions_res)){
$amt_type=$row['amount_type'];
$account_book_id=$row['account_bookid'];

if($amt_type=='CR')
{$sql="Select ledgerid from account_book_details where account_bookid='".$account_book_id."' and amount_type='DR'";}
elseif($amt_type=='DR')
{$sql="Select ledgerid from account_book_details where account_bookid='".$account_book_id."' and amount_type='CR'";}
$ledger_res=DB_query($sql,$db);
$ledger_row=DB_fetch_array($ledger_res);

$Ledger_name=$s->GetFieldValue('accountledger','AccountsID="'.$ledger_row['ledgerid'].'"','AccountName');

if($amt_type=='CR')
{ $CR_amount=$row['amount'];}else{$CR_amount='';}
if($amt_type=='DR')
{ $DR_amount=$row['amount'];}else{$DR_amount='';}
?>
<tr bgcolor="#fff">
<td><strong><?php echo ConvertSQLDate($row['Dates']);?></strong></td>
<td><strong><?php echo $Ledger_name;?></strong></td>
<td><strong><?php echo $row['Voucher_NO'];?></strong></td>
<td><strong><?php 
$voucher_type=$s->GetFieldValue('voucher_master','VoucherType="'.$row['VPrefix'].'"','GroupName');
echo $voucher_type;?></strong></td>
<td><strong><?php echo $DR_amount; $DR_arr[]=$DR_amount;?></strong></td>
<td><strong><?php echo $CR_amount; $CR_arr[]=$CR_amount;?></strong></td>
<td><a href="address_book.php?id=<?php echo $row['parent_id'];?>"><img src="images/search.gif" width="15" height="15" /></a></td>
</tr>
<?php } 
$currentMonthOpeningDR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'DR',$key);
$currentMonthOpeningCR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'CR',$key);

$TotalDROpening=$currentMonthOpeningDR+$DR_opening;
$TotalCROpening=$currentMonthOpeningCR+$CR_opening;

if($TotalDROpening>$TotalCROpening){$OP=$TotalDROpening-$TotalCROpening; $OPTYPE="DR"; $OPDR=$OP; $OPCR='';}
elseif($TotalCROpening>$TotalDROpening){$OP=$TotalCROpening-$TotalDROpening; $OPTYPE="CR"; $OPCR=$OP; $OPDR='';}
?>
<tr bgcolor="#fff">
 		  <td>&nbsp;</td> 
 		  <td>&nbsp;</td>
		  <td>&nbsp;</td> 
 		  <td nowrap="nowrap"><strong>Opening Balance:</strong> </td>
 		  <td><strong><?php echo $OPDR;?></strong></td>
 		<td><strong><?php echo $OPCR;?></strong></td>
		<td>&nbsp;</td>
  </tr>
	    <tr bgcolor="#fff">
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
 		  <td nowrap="nowrap">&nbsp;</td>
 		  <td>&nbsp;<strong>Current Total:</strong></td>
 		<td><strong><?php if($DR_arr!=''){echo $aa=array_sum($DR_arr);}else{$aa='';}?></strong></td> 
		<td><strong><?php if($CR_arr!=''){echo $bb=array_sum($CR_arr);}else{$bb='';}?></strong></td>
		<td>&nbsp;</td>
	    </tr>
		<?php 
		$GtotalDR=$OPDR+$aa;
		$GtotalCR=$OPCR+$bb;
		if(($GtotalDR)>($GtotalCR)){$closing_DR=($GtotalDR)-($GtotalCR);}else{$closing_DR='';}
		if(($GtotalCR)>($GtotalDR)){$closing_CR=($GtotalCR)-($GtotalDR);}else{$closing_CR='';}
		?>
	    <tr bgcolor="#fff">
 		  <td>&nbsp;</td> 
 		  <td>&nbsp;</td>
		  <td>&nbsp;</td>
 		  <td><strong>Closing Balance:</strong> </td>
 		  <td><strong><?php echo $closing_DR;?></strong></td>
 		<td><strong><?php echo $closing_CR;?></strong></td> 
 		<td>&nbsp;</td>
	    </tr>
</table>
<p align="center"><a href="javascript:window.close();"><input type="button" value="Close" class="search" /></a></p>
<?php 	}

if($_REQUEST['vouchers']=='1')
{//echo "222";
	if($_REQUEST['from']!=''){$from_brk=explode('-',$_REQUEST['from']);$month=$from_brk[1]; $yr=$from_brk[0];}
	if($_REQUEST['to']!=''){$to_brk=explode('-',$_REQUEST['to']); $month=$to_brk[1]; $yr=$to_brk[0];}
	if($_REQUEST['month']!=''){$month=$_REQUEST['month']; $yr=$_REQUEST['y'];}
	//echo $_REQUEST['from']."--".$month."--".$yr;
	
	if($month==1)
    {
    	$lastmonth=12;
		$lastyr=$yr-1;
    }
   	else
    {	
    	$lastmonth=$month-1;
		$lastyr=$yr;
    }
	
	$ledger_name=$s->GetFieldValue('accountledger','AccountsID="'.$_REQUEST['q'].'"','AccountName');
	$ledger_opening=DB_query("Select openingtype,openingBalance from accountledger where AccountsID='".$_REQUEST['q']."'",$db);
	$opening_row=DB_fetch_array($ledger_opening);
	if($opening_row['openingtype']=='CR'){$CR_opening=$opening_row['openingBalance'];}else{$CR_opening='';}
	if($opening_row['openingtype']=='DR'){$DR_opening=$opening_row['openingBalance'];}else{$DR_opening='';}
	 
	 if($_REQUEST['from']!='' && $_REQUEST['to']=='')
	 {
	 	$get_transactions="Select A.*,B.*,A.id as parent_id from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and MONTH(Dates)='".$month."' and A.Dates>='".$_REQUEST['from']."' and YEAR(Dates)='".$yr."'  and cancel_status=0 order by A.Dates asc";}
	 elseif($_REQUEST['month']!='' && $_REQUEST['from']=='')
	 {
	 	$get_transactions="Select A.*,B.*,A.id as parent_id from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and MONTH(Dates)='".$month."' and YEAR(Dates)='".$yr."' and cancel_status=0 order by A.Dates asc";
	 }
	 elseif($_REQUEST['to']!='' && $_REQUEST['from']=='')
	 {
	 	$get_transactions="Select A.*,B.*,A.id as parent_id from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and MONTH(Dates)='".$month."' and A.Dates<='".$_REQUEST['to']."' and YEAR(Dates)='".$yr."' and cancel_status=0 order by A.Dates asc";
	 }
	 elseif($_REQUEST['from']!='' && $_REQUEST['to']!='')
		{
			$get_transactions="Select A.*,B.*,A.id as parent_id from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid='".$_REQUEST['q']."' and A.Dates between '".$_REQUEST['from']."' and '".$_REQUEST['to']."' and cancel_status=0 order by A.Dates asc";
		}
	$get_transactions_res=DB_query($get_transactions,$db);
	//echo $get_transactions;
?>
<p align="right"><a href="javascript:history.go(-1);"><input type="button" value="Go Back" /></a></p>
<h1><strong>Transactions for the month of <?php echo date( 'F', mktime(0, 0, 0, $month) );?></strong></h1>
<table width="98%" border="1" cellspacing="0" cellpadding="3" align="center" style="border:solid 1px #808080; border-collapse:collapse;">
<tr><th colspan="7"><strong><font size="+1">Ledger : <?php echo $ledger_name;?></font></strong></th></tr>
<tr>
<th width="15%"><strong>Date</strong></th>
<th><strong>Particulars</strong></th>
<th width="20%"><strong>Voucher No.</strong></th>
<th width="10%"><strong>Voucher Type</strong></th>
<th width="20%"><strong>Debit Amount(INR)</strong></th>
<th width="20%"><strong>Credit Amount(INR)</strong></th>
<th width="5%"><strong>View</strong></th>
</tr>
<?php 
$DR_arr='';
$CR_arr='';
while($row=DB_fetch_array($get_transactions_res)){
$amt_type=$row['amount_type'];
$account_book_id=$row['account_bookid'];

if($amt_type=='CR')
{$sql="Select ledgerid from account_book_details where account_bookid='".$account_book_id."' and amount_type='DR'";}
elseif($amt_type=='DR')
{$sql="Select ledgerid from account_book_details where account_bookid='".$account_book_id."' and amount_type='CR'";}
$ledger_res=DB_query($sql,$db);
$ledger_row=DB_fetch_array($ledger_res);

$Ledger_name=$s->GetFieldValue('accountledger','AccountsID="'.$ledger_row['ledgerid'].'"','AccountName');

if($amt_type=='CR')
{ $CR_amount=$row['amount'];}else{$CR_amount='';}
if($amt_type=='DR')
{ $DR_amount=$row['amount'];}else{$DR_amount='';}
?>
<tr bgcolor="#fff">
<td><strong><?php echo ConvertSQLDate($row['Dates']);?></strong></td>
<td><strong><?php echo $Ledger_name;?></strong></td>
<td><strong><?php echo $row['Voucher_NO'];?></strong></td>
<td><strong><?php 
$voucher_type=$s->GetFieldValue('voucher_master','VoucherType="'.$row['VPrefix'].'"','GroupName');
echo $voucher_type;?></strong></td>
<td><strong><?php echo $DR_amount; $DR_arr[]=$DR_amount;?></strong></td>
<td><strong><?php echo $CR_amount; $CR_arr[]=$CR_amount;?></strong></td>
<td><a href="address_book.php?id=<?php echo $row['parent_id'];?>"><img src="images/search.gif" width="15" height="15" /></a></td>
</tr>
<?php } 
if($_REQUEST['from']=='')
{
	$currentMonthOpeningDR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'DR',$key);
	$currentMonthOpeningCR=monthlyLedgerAccount($_REQUEST['q'],$lastmonth,$lastyr,'CR',$key);
}
elseif($_REQUEST['from']!='' && $_REQUEST['month']=='')
{
	$prevdate=date('Y-m-d', strtotime('-1 day', strtotime($_REQUEST['from'])));
	$currentMonthOpeningDR=monthlyLedgerAccount($_REQUEST['q'],'','','DR',$key,$prevdate);
	$currentMonthOpeningCR=monthlyLedgerAccount($_REQUEST['q'],'','','CR',$key,$prevdate);
}

$TotalDROpening=$currentMonthOpeningDR+$DR_opening;
$TotalCROpening=$currentMonthOpeningCR+$CR_opening;

if($TotalDROpening>$TotalCROpening){$OP=$TotalDROpening-$TotalCROpening; $OPTYPE="DR"; $OPDR=$OP; $OPCR='';}
elseif($TotalCROpening>$TotalDROpening){$OP=$TotalCROpening-$TotalDROpening; $OPTYPE="CR"; $OPCR=$OP; $OPDR='';}
?>
<tr bgcolor="#fff">
 		  <td>&nbsp;</td> 
 		  <td>&nbsp;</td>
		  <td>&nbsp;</td> 
 		  <td nowrap="nowrap"><strong>Opening Balance:</strong> </td>
 		  <td><strong><?php echo $OPDR;?></strong></td>
 		<td><strong><?php echo $OPCR;?></strong></td>
		<td>&nbsp;</td>
  </tr>
	    <tr bgcolor="#fff">
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
 		  <td nowrap="nowrap">&nbsp;</td>
 		  <td>&nbsp;<strong>Current Total:</strong></td>
 		<td><strong><?php if($DR_arr!=''){echo $aa=array_sum($DR_arr);}else{$aa=0;}?></strong></td> 
		<td><strong><?php if($CR_arr!=''){echo $bb=array_sum($CR_arr);}else{$bb=0;}?></strong></td>
		<td>&nbsp;</td>
	    </tr>
		<?php 
		$GtotalDr=$aa+$OPDR;
		$GtotalCr=$bb+$OPCR;
		if(($GtotalDr)>($GtotalCr)){$closing_DR=($GtotalDr)-($GtotalCr);}else{$closing_DR='';}
		if(($GtotalCr)>($GtotalDr)){$closing_CR=($GtotalCr)-($GtotalDr);}else{$closing_CR='';}
		?>
	    <tr bgcolor="#fff">
 		  <td>&nbsp;</td> 
 		  <td>&nbsp;</td>
		  <td>&nbsp;</td>
 		  <td><strong>Closing Balance:</strong> </td>
 		  <td><strong><?php echo $closing_DR;?></strong></td>
 		<td><strong><?php echo $closing_CR;?></strong></td> 
 		<td>&nbsp;</td>
	    </tr>
</table>
<p align="center"><a href="javascript:window.close();"><input type="button" value="Close" class="search" /></a></p>
<?php 
}
?>