<?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');
 function CategoryShow($pcode, $PcateID, $dashVal )
	{
	    global $db;
		  static $i=0;;
		 
 	       $sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
  				  $arr1[]=$rowCate['GroupId'];
 				   CategoryShow($pcode,$rowCate['GroupId'], $dashVal  );
 			}
 		}
	// print_r($arr1);
			return($arr1);
		 
		
	}
	 
?>
 <style type="text/css">
* {
	margin: 0;
	padding: 0;
	font-size:14px;
}
#tabs {
	font-size: 90%;
	margin: 10px 0;
}
#tabs ul {
	float: left;
	background: #FFFFFF;
	width: auto;
	padding-top: 2px;
}
#tabs li {
	margin-left: 4px;
	list-style: none;
}
* html #tabs li {
	display: inline;
}
#tabs li, #tabs li a {
	float: left;
}
#tabs ul li.active {
	border-top:2px #FFFF66 solid;
	background: #CCCCCC;
}
#tabs ul li.active a {
	color: #333333;
}
#tabs div {
	background: #CCCCCC;
	clear: both;
	padding: 15px;
	min-height: 200px;
}
#tabs div h3 {
	margin-bottom: 12px;
}
#tabs div p {
	line-height: 150%;
}
#tabs ul li a {
	text-decoration: none;
	padding: 8px;
	color: #000;
	font-weight: bold;
}
.thumbs {
	float:left;
	border:#000 solid 1px;
	margin-bottom:20px;
	margin-right:20px;
}
-->
</style>
<form action="" method="post">
 
<h2>Account Book</h2>
<div id="container">
  <div id="tabs">
    <ul style="background-color:#999966" >
	<?php 
	 $GetRecord = "Select * from voucher_master where Vprefix  not in('CBJ','MEMO','ChRet') order by GroupName  ";
  	 $ResultData = DB_query($GetRecord,$db);
	 $j=0;
   	 while($RowData = DB_fetch_array($ResultData))
	 {
	 
	  $arrBook[]=$RowData['VoucherType'];
	  // $arrBook=array('Ledger','Bank','Cash','Contra');
	  if($_REQUEST['tabid']==$j)
 	     $bg= "style='background-color:#999999'";
		 else $bg="";
  	?>
    <li <?php echo $bg?>>
	<a href="index.php?route=AccountBook<?php echo $rurl;?>&tabid=<?php echo $j?>"><?php echo $RowData['GroupName'];?></a></li>
	<?php  $j++;} 
	 $arrBook[]='Day';	 $arrBook[]='Ledger';
	
	 $rx=array('Day','Ledger');
	 for($kl=$j,$k=0;$k<2;$k++){
	 if($_REQUEST['tabid']==$kl)
 	     $bg= "style='background-color:#999999'";
		 else $bg="";
	?>
	<li <?php echo $bg?>>
	<a href="index.php?route=AccountBook<?php echo $rurl;?>&tabid=<?php echo $kl++?>&AcType=<?php  echo $rx[$k];?>"><?php  echo $rx[$k];?> Book</a></li>
	<?php }?>
	
    </ul>
	<?php 
 for($j=0,$i=0;$i<count($arrBook);$i++,$j++)
{	
 if($_REQUEST['tabid']==$i){
 	$BillBookType= $s->getField('voucher_master','VoucherType',$arrBook[$i],'Vprefix');
	switch($BillBookType)
	 {
		case 'CP':
		case 'CR':
		$LDR=array('cash');
		break;
		case 'BP':
		case 'BR':
		$LDR=array('bank');
		break;		
		case 'JV':
		$LDR='';
		break;
		default: 				
		$LDR='';
		break; 				 				 		 
	 }
 
 ?>
<div id="<?php echo $j?>"> <h3>
<?php 
if(isset($_REQUEST['AcType']))
{
echo $_REQUEST['AcType'];
}else{?>
<?php  echo $s->getField('voucher_master','VoucherType',$arrBook[$i],'GroupName')?>
 <?php }?> Book</h3>
 <p>
<form action="" method="post">
<table width="60%" border="0">
<tr>
<td><?php
 	if($LDR!=''){
		$LRX=implode("','",$LDR);
		
	    $sqlCate = "select * from  accountgroupmaster  where    MainGroupId   = 0  and GroupName in('$LRX') ";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr1[]=$rowCate['GroupId'];
  				$arr1[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		$rx=array();
		for($l=0;$l<count($arr1);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr1[$l]))
		 $rx[]=implode(',',$arr1[$l]);
		 else
		  $rx[]=$arr1[$l];
			 
		}
		$undergroup= implode(',',$rx );
		}else{
		$undergroup='';	
		}
		
	 if($_REQUEST['AcType']!='Day')
 	   $AccountMaster-> AccountLeger($_REQUEST['ledgerid'],'ledgerid','ledgerid',$undergroup);
    ?></td>
	<td><input name="todate" type="text" class="datepicker"  value="<?php  echo $_REQUEST['todate']?>"/></td>
<td><input name="fromdate" type="text" class="datepicker"value="<?php  echo $_REQUEST['fromdate']?>" /></td>
<td><input name="Search" type="submit"  value="Search"/></td>
</tr>

</table>
</form>
	  
	  <?php
	  if($_REQUEST['Search'])
	  {
		$ledgerid=$_REQUEST['ledgerid'] ;
		$todate=FormatDateForSQL($_REQUEST['todate']) ;
		$fromdate=FormatDateForSQL($_REQUEST['fromdate']) ;
		if($_REQUEST['AcType']!='Day' && $_REQUEST['AcType']!='Ledger'){
		
	    $GetRecord1 = "SELECT  A.Voucher_NO ,A.id AS Pid, A.Dates ,B.*,sum(B.amount) AS AMT  FROM `account_book` A, account_book_details B where  A.VPrefix  ='".$arrBook[$i]."' and Dates between  '".$todate."' and '".$fromdate."' and A.id=B.account_bookid  and B.ledgerid!='".$ledgerid."'  group by B.ledgerid  ";
		}
		else if($_REQUEST['AcType']=='Day')
		{
 		  $GetRecord1 = "SELECT  A.Vprefix,A.Voucher_NO ,A.id AS Pid, A.Dates ,B.* ,(B.amount) AS AMT  FROM `account_book` A, account_book_details B where   Dates between  '".$todate."' and '".$fromdate."' and A.id=B.account_bookid  ";
		}else if($_REQUEST['AcType']=='Ledger')
		{
		  $GetRecord1 = "SELECT  A.Vprefix,A.Voucher_NO ,A.id AS Pid, A.Dates ,B.* ,(B.amount) AS AMT  FROM `account_book` A, account_book_details B where   Dates between  '".$todate."' and '".$fromdate."' and A.id=B.account_bookid  and B.ledgerid='".$ledgerid."'    ";

		
		}
		//echo   $GetRecord1;
		 
   	   $ResultData1 = DB_query($GetRecord1,$db);
 	   ?>
 	  
	  <table width="98%" border="0" cellspacing="3" cellpadding="3" align="center">
    	<tr bgcolor="#666666" style="color:#FFFFFF">
    	    <td width="10%"><strong> Date</strong></td>
    	    <td width="20%"><strong>Ledger Name </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
		   $TCR=array();$TDR=array();
 		  if(DB_num_rows($ResultData1)>0)
		  {
			   while($RowData1 = DB_fetch_array($ResultData1))
			   {?>
				<tr bgcolor="#FFFFFF">
				<td><?php echo ConvertSQLDate($RowData1['Dates'])  ?></td>
				<td><?php  //echo $RowData1['ledgerid']; ?>
				<?php  echo $s->getField('accountledger','AccountsID',$RowData1['ledgerid'],'AccountName');;?>			</td>    
				<td><?php echo $RowData1['Voucher_NO']; ?></td>  
				<?php ?>
	    	     <td width="10%"> <?php 
			if($_REQUEST['AcType']=='Day' || $_REQUEST['AcType']=='Ledger'){	 
 			echo	 $s->getField('voucher_master','VoucherType',$RowData1['Vprefix'],'Vprefix');
			$Vprefixval= $s->getField('voucher_master','VoucherType',$RowData1['Vprefix'],'VoucherType');
			}else{ echo $RowData1['amount_type'];}
				  ?>  </td>
				<td><?php  
				if($RowData1['amount_type']=='DR')
				echo $AMTDR=$RowData1['AMT'];
				else
				 echo $AMTDR=0;
				 
				$TDR[]= $AMTDR;;
				 ?></td>
				<td><?php  
				if($RowData1['amount_type']=='CR')
				echo $AMTCR=$RowData1['AMT'];
				else
				 echo $AMTCR=0;
				$TCR[]= $AMTCR;;
				?></td>    	    
				 <td>
				 <?php if($_REQUEST['AcType']!='Day' && $_REQUEST['AcType']!='Ledger'){
				 $Vprefixval=$arrBook[$i];
				 }
				 ?>
				 <a href="#" onclick='popup("address_book.php?id=<?php  echo $RowData1['Pid'].$rurl?>&VPrefix=<?php echo $Vprefixval;?>",600,600)'><img src="images/search.gif"  width="20" height="20"/></a></td>	    
			</tr>
			
			<?php  
			}
		  }
		  else{echo "<tr><td colspan=4>No Record</td></tr>";}
		
		 
		
		?>
		<tr bgcolor="#666666" style="color:#FFFFFF">
 		  <td>&nbsp;</td>
 		   
 		  <td>&nbsp;</td>
		  <?php if($_REQUEST['AcType']=='Day' || $_REQUEST['AcType']=='Ledger'){?>
		  <td>&nbsp;</td>
		  <?php }?>
		  <td>&nbsp;</td>
 		  <td > <strong>Total:</strong> </td>
 		  <td><b><?php echo array_sum($TDR); ?></b>&nbsp;</td>
 		<td><b><?php echo array_sum($TCR); ?></b></td> <td>&nbsp;</td>
	    </tr>
  	  </table> 
	  <?php } ?>
      </p><?php }
 } ?>
 <?php 
 
 ?>