<?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');
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(4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December',1=>'January',2=>'February',3=>'March',);
//print_r($month_array);

if($_REQUEST['mnth']!='' && $_REQUEST['yr']!='' && $_REQUEST['vouchers']=='' && $_REQUEST['site']!='0')
{
	$ledger_name=$s->GetFieldValue('accountledger','AccountsID="'.$_REQUEST['q'].'"','AccountName');
	$ledger_opening=DB_query("Select accountledger_dtls.openingtype,accountledger_dtls.openingBalance from accountledger inner join accountledger_dtls on accountledger.AccountsID=accountledger_dtls.accountledger_id where AccountsID='".$_REQUEST['q']."'
	 and MONTH(accountledger_dtls.openingdate)<='".$_REQUEST['mnth']."' and accountledger_dtls.site='".$_REQUEST['site']."'",$db);
	//echo "Select openingtype,openingBalance from accountledger where AccountsID='".$_REQUEST['q']."' and MONTH(openingdate)<='".$_REQUEST['mnth']."'";
	$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='';}
	 
	//echo "Select openingtype,openingBalance from accountledger where AccountsID='".$_REQUEST['q']."'"; 
	$month=$_REQUEST['mnth'];
	$yr=$_REQUEST['yr'];
	
	
	/* if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		$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 '".str_replace("/","-",$_SESSION['FDATE'])."' and '".str_replace("/","-",$_SESSION['TDATE'])."' and A.cancel_status=0 order by A.Dates asc";
	}
	else{ */
	$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 A.cancel_status=0 order by A.Dates asc";
 //}
	$get_transactions_res=DB_query($get_transactions,$db);
	//echo $get_transactions;
	$month_name = date( 'F', mktime(0, 0, 0, $month) );
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
	<div class="tab_div">
<h1>Transactions for the month of <?php echo $month_name;?></h1>
<table width="100%" border="1" cellspacing="0" cellpadding="3" align="center" style="border:solid 1px #808080; border-collapse:collapse;">
<tr><th colspan="7">Ledger : <?php echo $ledger_name;?></th></tr>
<tr>
<th width="90" style="text-align:center;">Date</th>
<th>Particulars</th>
<th width="120">Voucher No.</th>
<th width="150">Voucher Type</th>
<th width="150" style="text-align:center;">Debit Amount(INR)</th>
<th width="150" style="text-align:center;">Credit Amount(INR)</th>
<th width="100" style="text-align:center;">View</th>
</tr>
</table>
<div style="width:100%; overflow-y:scroll; height:300px; float:left;">
<table width="100%" border="1" cellspacing="0" cellpadding="3" align="center" style="border:solid 1px #808080; border-collapse:collapse;">
<?php 
$DR_arr='';
$CR_arr='';
$voucher_type='';
$DR_amount='';
$CR_amount='';
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'];$DR_amount='';}else{$CR_amount='';}
if($amt_type=='DR')
{$DR_amount=$row['amount'];$CR_amount='';}else{$DR_amount='';}
?>
<tr bgcolor="#fff">
<td width="90" style="text-align:center;"><?php echo ConvertSQLDate($row['Dates']);?></td>
<td><?php echo $Ledger_name;?></td>
<td width="120"><?php echo $row['Voucher_NO'];?></td>
<td width="150"><?php 
$voucher_type=$s->GetFieldValue('voucher_master','VoucherType="'.$row['VPrefix'].'"','GroupName');
echo $voucher_type;?></td>
<td width="150" style="text-align:right;"><?php echo $DR_amount; $DR_arr[]=$DR_amount;?></td>
<td width="150" style="text-align:right;"><?php echo $CR_amount; $CR_arr[]=$CR_amount;?></td>
<td width="83" style="text-align:center;"><a href="address_book.php?id=<?php echo $row['parent_id'];?>"><img src="images/search.gif" width="15" height="15"/></a></td>
</tr>
<?php }

//echo $QUERY; echo "<br />".$flag;
?>
 
<tr bgcolor="#fff">
 		  <td width="90"></td> 
 		  <td></td>
		  <td width="120"></td> 
 		  <td width="150" style="text-align:right;"><strong>Opening Balance:</strong> </td>
 		  <td width="150" style="text-align:right;"><strong><?php echo $DR_opening;?></strong></td>
 		  <td width="150" style="text-align:right;"><strong><?php echo $CR_opening;?></strong></td>
		  <td width="83"></td>
  </tr>
	    <tr bgcolor="#fff">
 		  <td></td>
 		  <td></td>
 		  <td></td>
 		  <td style="text-align:right;"><strong>Current Total:</strong></td>
 		<td style="text-align:right;"><strong><?php if($DR_arr!='') {$aa=array_sum($DR_arr);} else {$aa='';} echo $aa;?></strong></td> 
		<td style="text-align:right;"><strong><?php if($CR_arr!='') {$bb=array_sum($CR_arr);}else{$bb='';} echo $bb?></strong></td>
		<td></td>
	    </tr>
		<?php 
		//echo $DR_opening; echo "manvi";
		if($DR_opening!='' || $CR_opening!='')
		{
			if($DR_opening!='')
			{
				$DRR=$aa+$DR_opening;
				$CRR=$bb;
			}
			elseif($CR_opening!='')
			{
				$CRR=$bb+$CR_opening;
				$DRR=$aa;
			}
		}
		else{$DRR=$aa; $CRR=$bb;}
		
		if($DRR>$CRR){$closing_DR=$DRR-$CRR;}else{$closing_DR='';}
		if($CRR>$DRR){$closing_CR=$CRR-$DRR;}else{$closing_CR='';}
		?>
	    <tr bgcolor="#fff">
 		  <td></td> 
 		  <td></td>
		  <td></td>
 		  <td style="text-align:right;"><strong>Closing Balance:</strong> </td>
 		  <td style="text-align:right;"><strong><?php echo $closing_DR; ?></strong></td>
 		<td style="text-align:right;"><strong><?php echo $closing_CR;?></strong></td> 
 		<td></td>
	    </tr>
</table> 
</div>
<p align="center"><a href="javascript:window.close();"><input type="button" value="Close" class="search" /></a></p>
<?php 	}

if($_REQUEST['vouchers']=='1')
{
	if($_REQUEST['from']!=''){$from_brk=explode('-',$_REQUEST['from']);$month=$from_brk[1];}
	if($_REQUEST['to']!=''){$to_brk=explode('-',$_REQUEST['to']); $month=$to_brk[1];}
	if($_REQUEST['month']!=''){$month=$_REQUEST['month'];}
	$yr=$_REQUEST['y'];
	
	
	$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']!='')
	 {
	 	$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."' order by A.Dates asc";}
	 else if($_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."' order by A.Dates asc";
	 }
	 else if($_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['to']."' and YEAR(Dates)='".$yr."' 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>
<h2 align="center"><strong>Transactions for the month of <?php echo date( 'F', mktime(0, 0, 0, $month) );?></strong></h2>
<table width="100%" border="1" cellspacing="0" cellpadding="3" align="center" style="border:solid 1px #808080; border-collapse:collapse;">
<tr><td colspan="7"><strong><font size="+1">Ledger : <?php echo $ledger_name;?></font></strong></td></tr>
<tr>
<td width="15%"><strong>Date</strong></td>
<td><strong>Particulars</strong></td>
<td width="20%"><strong>Voucher No </strong></td>
<td width="10%"><strong>Voucher Type </strong></td>
<td width="20%"><strong>Debit Amount(INR)</strong></td>
<td width="20%"><strong>Credit Amount(INR).</strong></td>
<td  width="5%"><strong>View</strong></td>
</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 width="15%"><strong><?php echo ConvertSQLDate($row['Dates']);?></strong></td>
<td><strong><?php echo $Ledger_name;?></strong></td>
<td width="20%"><strong><?php echo $row['Voucher_NO'];?></strong></td>
<td width="10%"><strong><?php 
$voucher_type=$s->GetFieldValue('voucher_master','VoucherType="'.$row['VPrefix'].'"','GroupName');
echo $voucher_type;?></strong></td>
<td width="20%"><strong><?php echo $DR_amount; $DR_arr[]=$DR_amount;?></strong></td>
<td width="20%"><strong><?php echo $CR_amount; $CR_arr[]=$CR_amount;?></strong></td>
<td  width="5%"><a href="address_book.php?id=<?php echo $row['parent_id'];?>"><img src="images/search.gif"  width="15" height="15"/></a></td>
</tr>
<?php } ?>
<tr bgcolor="#fff">
 		  <td width="15%">&nbsp;</td> 
 		  <td>&nbsp;</td>
		  <td width="20%">&nbsp;</td> 
 		  <td width="10%"><strong>Opening Balance:</strong> </td>
 		  <td width="20%"><strong><?php echo $DR_opening;?></strong></td>
 		<td width="20%"><strong><?php echo $CR_opening;?></strong></td>
		<td width="5%">&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 echo array_sum($DR_arr);?></strong></td> 
		<td><strong><?php echo array_sum($CR_arr);?></strong></td>
		<td>&nbsp;</td>
	    </tr>
	    <tr bgcolor="#fff">
		<?php 
		if($DR_opening!='' || $CR_opening!='')
		{
			if($DR_opening!='')
			{
				$DRR=$DR_arr+$DR_opening;
				$CRR=$CR_arr;
			}
			elseif($CR_opening!='')
			{
				$CRR=$CR_arr+$CR_opening;
				$DRR=$DR_arr;
			}
		}
		//echo $DR_opening; echo "manvi";
		if($DRR>$CRR){$closing_DR=$DRR-$CRR;}else{$closing_DR='';}
		if($CRR>$DRR){$closing_CR=$CRR-$DRR;}else{$closing_CR='';}
		?>
 		  <td>&nbsp;</td> 
 		  <td>&nbsp;</td>
		  <td>&nbsp;</td>
 		  <td><strong>Closing Balance:</strong> </td>
 		  <td><strong><?php echo $closing_DR; echo $DR_opening; echo "manvi"; ?></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 
}
?>