<?php  extract($_POST);
$PageSecurity = 1;
$tabindex=0;
include('includes/Class_AccountBook.php');
include('includes/AccountGroupMaster_Class.php');
$title = _('Account Book');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
include('includes/SQL_CommonFunctions.inc');
$LCR=array('cash','bank');
include('fun-Trial.php');
$financial_start=date('Y');
$financial_end=date('Y')+1;
if($_REQUEST['All']!='')
{unset($_REQUEST);}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
	<div class="tab_div">
<h1>Cash/Bank Book</h1>
<form action="" method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td width="14%"></td>
	<td width="13%" align="right">From Date</td>
	<td width="20%"><input type="text" name="from_date" id="" value="<?php if($_REQUEST['from_date']!=''){ echo $_REQUEST['from_date']; } else { echo ConvertSQLDate($_SESSION['financialYrStart']);  }?>" class="datepicker" /></td>
	<td width="13%" align="right">To Date</td>
	<td width="20%"><input type="text" name="to_date" id="" value="<?php if($_REQUEST['to_date']!='') { echo $_REQUEST['to_date']; } else { echo ConvertSQLDate($_SESSION['financialYrEnd']); }?>" class="datepicker" /></td>
	<td></td>
</tr>
<tr class="row1">
	<td colspan="6" align="center"><input type="submit" name="Show" id="" class="search" value="Show" /> 
	<!-- <input type="submit" name="All" class="search" value="All" /> --></td>
</tr>
</table>

</form>
<?php
if($_REQUEST['Show']!='')
{
	$from_date=$_REQUEST['from_date'];
	$to_date=$_REQUEST['to_date'];
	
	$from_date_brk=explode('/',$from_date);
	$from_day=$from_date_brk[0];
	$from_month=$from_date_brk[1];
	$from_year=$from_date_brk[2];
	$from_Sqlformat=$from_year."-".$from_month."-".$from_day;
	
	$to_date_brk=explode('/',$to_date);
	$to_day=$to_date_brk[0];
	$to_month=$to_date_brk[1];
	$to_year=$to_date_brk[2];
	$to_Sqlformat=$to_year."-".$to_month."-".$to_day;
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
   <tr>
   <?php $account_type_sql="Select GroupId,GroupName from accountgroupmaster WHERE GroupName LIKE '%bank%' OR GroupName like '%cash%'";
   		 $account_type_res=DB_query($account_type_sql,$db);
		 while($acc_type_row=DB_fetch_array($account_type_res))
		 {
			$account_type_id=$acc_type_row['GroupId'];
			
			$acc_group_sql=DB_query("Select * from accountgroupmaster where GroupId='".$account_type_id."'",$db);
			$acc_group_row=DB_fetch_array($acc_group_sql);
			$account_group_name=$acc_group_row['GroupName'];
			$account_group_id=$acc_group_row['GroupId'];
   ?>
     <td valign="top" width="33%">
     <h3><?php echo $account_group_name;?></h3>
	 <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
		<tr>
			<th></th>
			<th width="25%" style="text-align:center;">Dr</th>
			<th width="27%" style="text-align:center;">Cr</th>
			<th width="10%"></th>
		</tr>
		<?php 
		$ledger_sql=DB_query("Select * from accountledger where UnderGroup='".$account_group_id."'",$db);
		$array_CR=array();
		$array_DR=array();
		
		while($ledger_row=DB_fetch_array($ledger_sql))
		{			
			$ledger_id=$ledger_row['AccountsID'];
			$openingSql=DB_query("Select openingtype,openingBalance from accountledger where AccountsID=$ledger_id",$db);
			$openingRow=DB_fetch_array($openingSql);
			$openingType=$openingRow['openingtype'];
			if($openingType=='DR'){$open_DR=$openingRow['openingBalance'];}
			elseif($openingType=='CR'){$open_CR=$openingRow['openingBalance'];}
			else{$open_CR='';$open_DR='';}
			if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
			{
				$total_DR=Get_AccountByLedger($ledger_id,'DR',FormatDateForSQL($_REQUEST['from_date']),FormatDateForSQL($_REQUEST['to_date']));
				$total_CR=Get_AccountByLedger($ledger_id,'CR',FormatDateForSQL($_REQUEST['from_date']),FormatDateForSQL($_REQUEST['to_date']));
				$prevdate=date('Y-m-d', strtotime('-1 day', strtotime(FormatDateForSQL($_REQUEST['from_date']))));
				$currentMonthOpeningDR=monthlyLedgerAccount($ledger_id,'','','DR','',$prevdate);
				$currentMonthOpeningCR=monthlyLedgerAccount($ledger_id,'','','CR','',$prevdate);
			
				$TotalDROpening=$currentMonthOpeningDR+$open_DR;
				$TotalCROpening=$currentMonthOpeningCR+$open_CR;
				if($TotalDROpening>$TotalCROpening){$OP=$TotalDROpening-$TotalCROpening; $OPTYPE="DR"; $OPDR=$OP; $OPCR='';}
				elseif($TotalCROpening>$TotalDROpening){$OP=$TotalCROpening-$TotalDROpening; $OPTYPE="CR"; $OPCR=$OP; $OPDR='';}
			}
			else
			{
				$total_DR=Get_AccountByLedger($ledger_id,'DR','','');
				$total_CR=Get_AccountByLedger($ledger_id,'CR','','');
				$OPDR=$open_DR;
				$OPCR=$open_CR;
			}			
		?>
		<tr>
		<td><strong><?php echo $ledger_row['AccountName'];  //cho "---".$openingType; //."===".$ledger_row['AccountsID'];?></strong></td>
		<td><div align="right"><strong><?php echo $total_DR;?></strong></div></td>
		<td><div align="right"><strong><?php echo $total_CR;?></strong></div></td>
		<td>
		<?php if($_REQUEST['Show']!=''){?>
		<input type="button" name="pop1" value="+" onclick="popup('cash_bank_popup.php?q=<?php echo $ledger_row['AccountsID'];?>&fdate=<?php echo $from_Sqlformat;?>&tdate=<?php echo $to_Sqlformat;?>',600,450)">
		<?php }else {?>
		<input type="button" name="pop1" value="+" onclick="popup('cash_bank_popup.php?q=<?php echo $ledger_row['AccountsID'];?>',600,450)"><?php } ?>
		</td>
		</tr>
		<?php 
		$GTotalDR=$OPDR+$total_DR;
		$GTotalCR=$OPCR+$total_CR;
		
		if($GTotalDR>$GTotalCR)
		{ $closing_DR=$GTotalDR-$GTotalCR;}else{$closing_DR='';}
		if($GTotalCR>$GTotalDR)
		{ $closing_CR=$GTotalCR-$GTotalDR;}else{$closing_CR='';}
		?>
		<tr>
		<td><div align="right"><strong>Opening :</strong></div></td>
		<td><div align="right"><strong><?php echo $OPDR;?></strong></div></td>
		<td><div align="right"><strong><?php echo $OPCR;?></strong></div></td>
		<td></td>
		</tr>
		<tr>
		<td><div align="right"><strong>Closing :</strong></div></td>
		<td><div align="right"><strong><?php echo $closing_DR;?></strong></div></td>
		<td><div align="right"><strong><?php echo $closing_CR;?></strong></div></td>
		<td></td>
		</tr>
		<?php } ?>
		
	</table>
	 </td>
	 <?php }?>
   </tr>
   
</table>
