<?php
ob_start();
session_name('CAKEPHP'); session_start();
error_reporting(FALSE);

	$PageSecurity = 3;
	include ('includes/session.inc');
	include ('includes/function_lib.php');
	include('includes/fun-php.php');
	include('includes/fun-HRClass.php');

 	global $db; 
	  $GetRecord1 = $_SESSION['ACGAccountBookQuery'];
	//$GetRecord1= $_SESSION['ledger'];
	$ResultData1 = DB_query($GetRecord1,$db);
	$numrows = DB_num_rows($ResultData1);
	
   $ledgerdata = $_REQUEST['ledgerdata'];
   $getLedgerName=$s->GetFieldValue('accountledger',"AccountsID='".$ledgerdata."'" ,'AccountName');
   $accountType=$s->GetFieldValue('accountledger',"AccountsID='".$ledgerdata."'" ,'openingtype');
	if($accountType=='DR')
	{
		$openingDRBalance=$s->GetFieldValue('accountledger',"AccountsID='".$ledgerdata."'" ,'openingBalance');
	}
	elseif($accountType=='CR')
	{
		$openingCRBalance=$s->GetFieldValue('accountledger',"AccountsID='".$ledgerdata."'" ,'openingBalance');
	}
	$Compamies=DB_query("select * from companies",$db);
	$CompamiesRow=DB_fetch_array($Compamies);
	$CompamiesRow['CompanyId'];
	$branch=$s->GetFieldValue('locations',"CompanyId='".$CompamiesRow['Branch_Permission']."'",'locationname');

	
	if(isset($_SESSION['searchledger']) && $_SESSION['searchledger']!='')
	 $ledgerid=$_SESSION['searchledger'];
  
  if($_REQUEST['AcType']!='Day' && $_REQUEST['AcType']!='Ledger'){
	 	 // echo "<br>----------------333----------------".$_REQUEST['AcType']."-----------------------------------<br>";
 		 
		if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) 
		{
			$header = "<table width='950px' border='0' >
					<tr>
						<td colspan='5' style='padding-top:5px; padding-bottom:5px; text-align:center; font-size:30px;'>".$CompamiesRow['CompanyName']."<br>"."Journal Voucher Book"."</td>
					</tr>
					  <tr>";
						
						$header.="<td > <strong>Ledger Name: ".$getLedgerName."</strong></td>";
						$header.="<td><strong>From Date:</strong></td>
								  <td>".$_REQUEST['todate']."</td>";
						$header.="<td ><strong>To Date:</strong></td>
								  <td >".$_REQUEST['fromdate']."</td>
					  </tr>
					</table>
					<br />
					<table width='950px' border='0'  align='center' style='font-family:Arial, Helvetica, sans-serif; font-size:16px; padding-top:10px; padding-bottom:10px;'>
					<tr bgcolor='#CCCCCC'>
						<td width='10px' align='center'><strong> S.No</strong></td>
						<td width='100px' align='center'><strong> Date</strong></td>
						<td width='150px' align='center'><strong>Particulars</strong></td>
						<td width='150px' align='center'><strong>Voucher No </strong></td>
						<td width='150px' align='center'><strong>Voucher Type </strong></td>
						<td width='150px' align='center'><strong>Debit Amount</strong></td>
						<td width='160px' align='center'><strong>Credit Amount</strong></td>
						<td width='150px' align='center'><strong>Narrations</strong></td>";
		}
		else
		{
			$header = "<table width='950px' border='0' >
					<tr>
						<td colspan='5' style='padding-top:5px; padding-bottom:5px; text-align:center; font-size:30px;'>".$CompamiesRow['CompanyName']."<br>"."Journal Voucher Book"."</td>
					</tr>
					  <tr>";
						
						$header.="<td > <strong>Ledger Name: ".$getLedgerName."</strong></td>";
						$header.="<td><strong>From Date:</strong></td>
								  <td>".$_REQUEST['todate']."</td>";
						$header.="<td ><strong>To Date:</strong></td>
								  <td >".$_REQUEST['fromdate']."</td>
					  </tr>
					</table>
					<br />
					<table width='950px' border='0'  align='center' style='margin-bottom:10px;'>
					<tr bgcolor='#CCCCCC'>
						<td width='10px' align='center'><strong> S.No</strong></td>
						<td width='100px' align='center'><strong> Date</strong></td>
						<td width='225px' align='center'><strong>Particulars</strong></td>
						<td width='150px' align='center'><strong>V No </strong></td>
						<td width='150px' align='center'><strong>V Type </strong></td>
						<td width='150px' align='center'><strong>Debit Amount</strong></td>
						<td width='160px' align='center'><strong>Credit Amount</strong></td>";
			}
			$header.= "</tr></table>";
						
			if($_REQUEST['ledgerentry']==1)
			{
				$cout=12;
			}
			else if($_REQUEST['ledgerentry']==2)
			{
				$cout=8;
			}
			else if($_REQUEST['ledgerentry']== 3)
			{ 
				$cout=8;
			}
			else if($_REQUEST['ledgerentry']==4)
			{
				$cout=12;
			} 
			else if($_REQUEST['ledgerentry']=='')
			{
				$cout=1;	
			}
			
			echo $header;
			$counter=0;	
			
				
 		  if(DB_num_rows($ResultData1)>0)
		  {  
		  	$i=1;
		   	while($RowData1 = DB_fetch_array($ResultData1))
			{  
			    if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==4)
				{
				   $ResultData2=DB_query("select A.* FROM account_book_details A   where A.account_bookid ='". $RowData1['Pid']."' and A.ledgerid='".$ledgerdata."'    ",$db);
				}
				else					   
				{
					$ResultData2=DB_query("select A.id,A.account_bookid,A.amount as AMT,  B.ledgerid as ledgerid,B.amount,B.currency,B.amount_type FROM account_book_details A, account_book_details  B   where A.account_bookid ='". $RowData1['Pid']."' and A.ledgerid='".$ledgerdata."' and  A.account_bookid=B.account_bookid",$db);
				}
						
				if(DB_num_rows($ResultData2)>0)
				{	
					if($counter%$cout==0)
					{	
						if($counter!='0')
						{
							if($_REQUEST['ledgerentry']==1)
							{
								echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";
								echo $header; 
							}
							else if($_REQUEST['ledgerentry']== 3)
							{
								echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";
								echo $header; 
							} 
							else if($_REQUEST['ledgerentry']==2)
							{
								echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";
								echo $header; 
							}
							else if($_REQUEST['ledgerentry']==4)
							{
								echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";
								echo $header; 
							}
						}
					  }	
						 $counter++;
						 
					if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) 
					{
			   			?>
						<table width='950px' border="0"  align='center' style="font-family:Arial, Helvetica, sans-serif; font-size:14px;">
						  <tr>
							<td width="35px" align="center"><?php echo $i.".";?></td>
							<td width="225px"><?php echo ConvertSQLDate($RowData1['Dates'])?></td>
							<td width="130px" align="center"><?php echo $RowData1['Voucher_NO'];?> </td>
							<td align="center">
							<?php   
							  echo  $vpr= $s->GetFieldValue('voucher_master',"VoucherType='".$RowData1['VPrefix']."'",'VPrefix');?>
							</td>
							<td  colspan="2"></td>
							<?php if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) {      ?>
							<td align="center"><?php	echo $s->getField('account_book','id',$RowData1['Pid'],'narration');  ?>
							</td>
							<?php } ?>
						  </tr>
						  <?php 
							 while($RowData2 = DB_fetch_array($ResultData2))
							 { 
								switch($vpr)
								{
								  case 'CP':  
											 break;
								}
							?>
						  <tr >
							<td>&nbsp;</td>
							<td style="padding-left:90px; width:20px;" align="center"><?php 
								if($_REQUEST['AcType']!='Day'  &&  $_REQUEST['ledgerentry']!=2 && $_REQUEST['ledgerentry']!=3)
								{
									$acid=DB_query("select ledgerid from account_book_details where ledgerid!='".$RowData2['ledgerid']."' and account_bookid='".$RowData2['account_bookid']."' limit 1 ",$db);
									$acid1=DB_fetch_array($acid);
									echo $s->GetFieldValue('accountledger',"AccountsID='".$acid1['ledgerid']."'",'AccountName');
								}
								else 
								{	
									echo $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
								}
								?>
							</td>
							<td>&nbsp;</td>
							<?php ?>
							<td width="140px">&nbsp;</td>
							<td width="120px" align="center">
							<?php  
								if($RowData2['amount_type']=='DR')
								echo $AMTDR=$RowData2['amount'];
								else
								 echo $AMTDR=0;
								 
								$TDR[]= $AMTDR;;
							?>
							</td>
							<td width="120px" align="center">
							<?php  
								if($RowData2['amount_type']=='CR')
								echo $AMTCR=$RowData2['amount'];
								else
								 echo $AMTCR=0;
								$TCR[]= $AMTCR;;
							?>
							</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						  </tr>
  							<?php 
							}?>
						  <tr>
							<td colspan="8"><hr></td>
						  </tr>
						</table>
					<?php 
					} 
					else
					{ ?>
						<table width='950px' border="0"  align='center' style="font-family:Arial, Helvetica, sans-serif; font-size:14px;">
						  <tr>
							<td width="35px" align="center"><?php echo $i++;?></td>
							<td width="340px"><?php echo ConvertSQLDate($RowData1['Dates'])?></td>
							<td width="150px" align="center"><?php echo $RowData1['Voucher_NO'];	  ?> 
							</td>
							<td align="center" width="150px">
							<?php   
								echo  $vpr= $s->GetFieldValue('voucher_master',"VoucherType='".$RowData1['VPrefix']."'",'VPrefix');?>
							</td>
							<td  colspan="2"></td>
							<?php if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) {      ?>
							<td>
							<?php	
								//echo $s->getField('account_book','id',$RowData1['Pid'],'narration');  
							?>
							</td>
							<?php } ?>
						  </tr>
						  <?php 
							 while($RowData2 = DB_fetch_array($ResultData2))
							 { 
							 
								switch($vpr)
								{
								  case 'CP':  
											 break;
								}
							?>
							<tr >
							<td>&nbsp;</td>
							<td style="padding-left:60px; width:100px;" align="center"><?php 
								if($_REQUEST['AcType']!='Day'  &&  $_REQUEST['ledgerentry']!=2 && $_REQUEST['ledgerentry']!=3)
								{
								$acid=DB_query("select ledgerid from account_book_details where ledgerid!='".$RowData2['ledgerid']."' and account_bookid='".$RowData2['account_bookid']."' limit 1 ",$db);
								$acid1=DB_fetch_array($acid);
								 echo $s->GetFieldValue('accountledger',"AccountsID='".$acid1['ledgerid']."'",'AccountName');
								}
								else 
								{	
									echo $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
								}
								?>
							</td>
							<td>&nbsp;</td>
							<td width="140px" >&nbsp;</td>
							<td width="10px" align="center">
							<?php  
								if($RowData2['amount_type']=='DR')
								echo $AMTDR=$RowData2['amount'];
								else
								 echo $AMTDR=0;
								 
								$TDR[]= $AMTDR;;
							?>
							</td>
							</td>
							<td align="center" >
							<?php  
								if($RowData2['amount_type']=='CR')
								echo $AMTCR=$RowData2['amount'];
								else
								 echo $AMTCR=0;
								$TCR[]= $AMTCR;
							?></td>
							</tr>
						<?php 
						   }?>
						  <tr>
							<td colspan="8"><hr></td>
						  </tr>
						</table>
					<?php 
					}
				}
			   $i++;
			 }
	  	 }
	}
?>
