<?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'];
    $data='';
  if($_REQUEST['AcType']!='Day' && $_REQUEST['AcType']!='Ledger'){
	 	 // echo "<br>----------------333----------------".$_REQUEST['AcType']."-----------------------------------<br>";
 		 
		if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) 
		{
			$data.="<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>"."Bank Receipt Book"."</td>
					</tr>
					  <tr>";
						
						$data.="<td > <strong>Ledger Name: ".$getLedgerName."</strong></td>";
						$data.="<td><strong>From Date:</strong></td>
								  <td>".$_REQUEST['todate']."</td>";
						$data.="<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='200px' 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
		{
			$data = "<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>"."Bank Receipt Book"."</td>
					</tr>
					  <tr>";
						
						$data.="<td > <strong>Ledger Name: ".$getLedgerName."</strong></td>";
						$data.="<td><strong>From Date:</strong></td>
								  <td>".$_REQUEST['todate']."</td>";
						$data.="<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='200px' 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>";
			}
			$data.= "</tr></table>";
 		  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($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) 
					{
			   			
						$data.="<table width='950px' border='0'  align='center' style='font-family:Arial, Helvetica, sans-serif; font-size:14px;'>";
						  $data.="<tr><td width='35px' align='center'>".$i++."</td>";
							$data.="<td width='250px'>".ConvertSQLDate($RowData1['Dates'])."</td>";
							$data.="<td>&nbsp;</td>";
							$data.="<td width='128px' align='center'>".$RowData1['Voucher_NO']."</td>
							<td align='center' width='120px'>".$vpr= $s->GetFieldValue('voucher_master',"VoucherType='".$RowData1['VPrefix']."'",'VPrefix')."</td>";
							$data.="<td  colspan='2'></td>";
							if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) {      
							$data.="<td>".$s->getField('account_book','id',$RowData1['Pid'],'narration')."</td>";
							 }
						  $data.="</tr>";
						  
							 while($RowData2 = DB_fetch_array($ResultData2))
							 { 
							
						  $data.="<tr>";
							$data.="<td>&nbsp;</td>";
								$data.="<td>&nbsp;</td>";
							$data.="<td style='padding-left:100px; width:20px;' align='left'>"; 
								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);
									$data.=$s->GetFieldValue('accountledger',"AccountsID='".$acid1['ledgerid']."'",'AccountName');
								}
								else 
								{	
								$data.=$s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
								}
								
							$data.="</td>";
							$data.="<td>&nbsp;</td>";
							$data.="<td width='140px'>&nbsp;</td>";
							$data.="<td width='100px' align='center'>";
							if($RowData2['amount_type']=='DR')
							$data.=$AMTDR=$RowData2['amount'];
								else
								$data.=$AMTDR=0;
								 
								$TDR[]= $AMTDR;;
							
							$data.="</td>";
							$data.="<td width='150px' align='center'>";
							
								if($RowData2['amount_type']=='CR')
								$data.=$AMTCR=$RowData2['amount'];
								else
								 $data.=$AMTCR=0;
								$TCR[]= $AMTCR;;
							
							$data.="</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
							}
						  $data.="<tr>";
							$data.="<td colspan='8'><hr></td>
						  </tr>
						</table>";
					
					} 
					else
					{ 
						$data.="<table width='950px' border='0'  align='center' style='font-family:Arial, Helvetica, sans-serif; font-size:14px;'>
						  <tr>";
							$data.="<td width='35px' align='center'>".$i++."</td>";
							$data.="<td width='290px'>".ConvertSQLDate($RowData1['Dates'])."</td>";
							$data.="<td>&nbsp;</td>";
							$data.="<td width='150px' align='center'>".$RowData1['Voucher_NO']."</td>";
							$data.="<td align='center'>".$vpr= $s->GetFieldValue('voucher_master',"VoucherType='".$RowData1['VPrefix']."'",'VPrefix')."</td>";
							$data.="<td  colspan='2'></td>";
							if($_REQUEST['ledgerentry']==1 || $_REQUEST['ledgerentry']==2) {     
							$data.="<td>".$s->getField('account_book','id',$RowData1['Pid'],'narration')."</td>";
							 } 
						  $data.="</tr>";
						  
							 while($RowData2 = DB_fetch_array($ResultData2))
							 { 
							$data.="<tr><td>&nbsp;</td><td>&nbsp;</td>
							<td style='padding-left:100px; width:20px;' align='left'>"; 
								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);
								$data.=$s->GetFieldValue('accountledger',"AccountsID='".$acid1['ledgerid']."'",'AccountName');
								}
								else 
								{	
											
								$data.=$s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
								}
								
							$data.="</td>
							<td width='140px'>&nbsp;</td>
							<td width='140px'>&nbsp;</td>
							<td width='150px' align='center'>";
							if($RowData2['amount_type']=='DR')
							$data.=$AMTDR=$RowData2['amount'];
								else
						    $data.=$AMTDR=0;
								 
								$TDR[]= $AMTDR;;
							
							$data.="</td>";
							$data.="<td align='center'>";
							if($RowData2['amount_type']=='CR')
							$data.= $AMTCR=$RowData2['amount'];
								else
								$data.= $AMTCR=0;
								$TCR[]= $AMTCR;;
							$data.="</td>
							</tr>";
						
						   }
						  $data.="<tr><td colspan='8'><hr></td></tr>";
						  $data.="</table>";
						  
						
					
					}
				}
 }
    $data.="<table><tr>
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
 		   <td>&nbsp;</td>";
		  $DR_total=array_sum($TDR);
		  $CR_total=array_sum($TCR);		 
		   
		   $data.="<td align='center'> <strong>Closing Balance:</strong> </td>";
 		  $data.="<td align='center'><b>".moneyFormatIndia(round($DR_total))."</b></td>";
 			$data.="<td align='center'><b>".moneyFormatIndia(round($CR_total))."</b></td>  <td>&nbsp;</td></tr><table>";
			
	  	 }
$header='';  		
header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=Ledger_Export.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data"; 

	}
?>
