<?php
session_name('CAKEPHP'); session_start();
ob_start();
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
include('Account/fun-Trial.php');

	

$AccountsID=$_SESSION['AccountsID'];
$data='';
$data.="<table width='100%' border='1'>";
$data.="<tr>";
$data.="<th>Particulars</th>";
$data.="<th>Opening DR</th>";
$data.="<th>Opening CR</th>";
$data.="<th>Debit Amount</th>";
$data.="<th>Credit Amount</th>";
$data.="<th>Closing Amount</th>";
$data.="</tr>";
//echo "select * from accountledger where AccountsID ='". $AccountsID."'";
$TDR=array();$TCR=array();
  	
	if($AccountsID!='000')
  	 $GetLedger=DB_query("select * from accountledger where AccountsID ='".$AccountsID."' ",$db);
	 else
	 $GetLedger=DB_query("select * from accountledger where 1=1",$db);
	 
	 
	 
	if(DB_num_rows($GetLedger)>0)
	{
	 while($RowGetLedger=DB_fetch_array($GetLedger))
	 {
	 	$getOpening=DB_fetch_array(DB_query("Select openingtype,openingBalance from accountledger where AccountsID='".$AccountsID."'",$db));
		$opening_bal=$getOpening['openingBalance'];
		$opening_type=$getOpening['openingtype'];
		
		$master_party_id=$RowGetLedger['vender_client'];	
		$master_tax_id=$RowGetLedger['taxid'];	
		$ledgertype=$RowGetLedger['ledgertype'];	
	 
	 
	 if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	 {
	 	  $from_date=ConvertSQLDate($_REQUEST['from_date']);
	$to_date=ConvertSQLDate($_REQUEST['to_date']);
		  
		   $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR',$from_date,$to_date);
	  	   $TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR',$from_date,$to_date);
			//echo "++++".$TDR."<br />";
		if($ledgertype!='')
	   {
		if($ledgertype=='excisetax'){
			$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,$from_date,$to_date); $etaxDR=$etaxDetails[0]; $etaxCR=$etaxDetails[1];
			}else{$etaxDetails='';}
		if($ledgertype=='salestax')
		{
			$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,$from_date,$to_date); $staxDR=$staxDetails[0]; $staxCR=$staxDetails[1];
		}else{$staxDetails='';}
		if($ledgertype=='vendor')
		{
			$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,$from_date,$to_date); $purchCR=$vendorDetails;
		}else{$vendorDetails='';}
		if($ledgertype=='client')
		{
			$clientDetails=yearlyClientAccountByTransactions($master_party_id,$from_date,$to_date); $invDR=$clientDetails;
		}else{$clientDetails='';}
		if($ledgertype=='freight')
		{
			$freightDetails=yearlyFreightAccountByTransactions($RowGetLedger['AccountsID'],$from_date,$to_date);
			$freightDR=$freightDetails;
		}else{$freightDetails='';$freightDR='';}
		if($ledgertype=='surcharge')
		{
			$surDetails=yearlySurchargeAccountByTransactions($RowGetLedger['AccountsID'],$from_date,$to_date);
			$surDR=$surDetails;
		}else{$surDetails='';$surDR='';}
		if($ledgertype=='pckcharge')
		{
			$packDetails=yearlyPkgChargesAccountByTransactions($RowGetLedger['AccountsID'],$from_date,$to_date);
			$packDR=$packDetails;
		}else{$packDetails='';$packDR='';}
		if($ledgertype=='addcharge')
		{
			$addDetails=yearlyAddChargesAccountByTransactions($RowGetLedger['AccountsID'],$from_date,$to_date);
			$addDR=$addDetails;
		}else{$addDetails='';$addDR='';}
		if($ledgertype=='cess')		
		{
			$cessDetails=yearlyCessAccountByTransactions($master_tax_id,$from_date,$to_date);
			$cessDR=$cessDetails;
		}else{$cessDetails='';$cessDR='';}
		
		if($ledgertype=='round')
		{
			$roundDetails=yearlyRoundOffByTransactions($RowGetLedger['AccountsID'],$from_date,$to_date);
			$roundDR=$roundDetails[0];
			$roundCR=$roundDetails[1];
		}
		else{$roundDetails=''; $roundDR=''; $roundCR='';}
		
		 $ledgerPosting='';$legderDR=''; $legderCR='';
	   }
	   else
	   {
		$ledgerPosting=yearlyLedgerPostingByTransactions($RowGetLedger['AccountsID'],$from_date,$to_date); $legderDR=$ledgerPosting[0]; $legderCR=$ledgerPosting[1];
		$etaxDetails='';$etaxDR=''; $etaxCR=''; 
		$staxDetails='';$staxDR='';$staxCR=''; 
		$vendorDetails='';$purchCR=''; 
		$clientDetails='';$invDR='';
		$freightDR='';$surDR='';$packDR=''; $addDR='';
		$cessDetails='';$cessDR='';
		$roundDetails=''; $roundDR=''; $roundCR='';
	}
	
	
	 }
	 else
	 {
	   $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR','','');
	   $TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR','','');
	   
	   if($ledgertype!='')
	   {
		if($ledgertype=='excisetax'){
			$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,'',''); $etaxDR=$etaxDetails[0]; $etaxCR=$etaxDetails[1];
			}else{$etaxDetails='';}
		if($ledgertype=='salestax')
		{
			$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,'',''); $staxDR=$staxDetails[0]; $staxCR=$staxDetails[1];
		}else{$staxDetails='';}
		if($ledgertype=='vendor')
		{
			$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,'',''); $purchCR=$vendorDetails;
		}else{$vendorDetails='';}
		if($ledgertype=='client')
		{
			$clientDetails=yearlyClientAccountByTransactions($master_party_id,'',''); $invDR=$clientDetails;
		}else{$clientDetails='';}
		if($ledgertype=='freight')
		{
			$freightDetails=yearlyFreightAccountByTransactions($RowGetLedger['AccountsID'],'','');
			$freightDR=$freightDetails;
		}else{$freightDetails='';$freightDR='';}
		if($ledgertype=='surcharge')
		{
			$surDetails=yearlySurchargeAccountByTransactions($RowGetLedger['AccountsID'],'','');
			$surDR=$surDetails;
		}else{$surDetails='';$surDR='';}
	   	if($ledgertype=='pckcharge')
		{
			$packDetails=yearlyPkgChargesAccountByTransactions($RowGetLedger['AccountsID'],'','');
			$packDR=$packDetails;
		}else{$packDetails='';$packDR='';}
	   	if($ledgertype=='addcharge')
		{
			$addDetails=yearlyAddChargesAccountByTransactions($RowGetLedger['AccountsID'],'','');
			$addDR=$addDetails;
		}else{$addDetails='';$addDR='';}
	   if($ledgertype=='cess')		
		{
			$cessDetails=yearlyCessAccountByTransactions($master_tax_id,'','');
			$cessDR=$cessDetails;
		}else{$cessDetails='';$cessDR='';}
		
		if($ledgertype=='round')
		{
			$roundDetails=yearlyRoundOffByTransactions($RowGetLedger['AccountsID'],'','');
			$roundDR=$roundDetails[0];
			$roundCR=$roundDetails[1];
		}
		else{$roundDetails=''; $roundDR=''; $roundCR='';}
	   
	   $ledgerPosting='';$legderDR=''; $legderCR='';
	   
	   }
	   else
	   {
		$ledgerPosting=yearlyLedgerPostingByTransactions($RowGetLedger['AccountsID'],'',''); $legderDR=$ledgerPosting[0]; $legderCR=$ledgerPosting[1];
		$etaxDetails='';$etaxDR=''; $etaxCR=''; 
		$staxDetails='';$staxDR='';$staxCR=''; 
		$vendorDetails='';$purchCR='';
		$clientDetails='';$invDR='';
		 $freightDR='';$surDR='';$packDR=''; $addDR='';
		$cessDetails='';$cessDR='';
		$roundDetails=''; $roundDR=''; $roundCR='';
	}
	 }
	 
	 $total_DR=  $TDR+$etaxDR+$staxDR+$invDR+$legderDR+$freightDR+$surDR+$packDR+$addDR+$cessDR+$roundDR;
	 $total_CR=  $TCR+$etaxCR+$staxCR+$purchCR+$legderCR+$roundCR;
	 
	 if($RowGetLedger['openingtype']=='DR')
	{$openingDR=$RowGetLedger['openingBalance']; $openingCR='';}
	elseif($RowGetLedger['openingtype']=='CR')
	{$openingCR=$RowGetLedger['openingBalance']; $openingDR='';}
	else{$openingCR='';$openingDR='';}
		//echo "<br /><br />DR1---".$openingDR;
		//echo "<br /><br />CR1---".$openingCR;
	if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
		$prevdate=date('Y-m-d', strtotime('-1 day', strtotime(FormatDateForSQL($_REQUEST['from_date']))));
		$currentMonthOpeningDR=monthlyLedgerAccount($RowGetLedger['AccountsID'],'','','DR','',$prevdate);
		$currentMonthOpeningCR=monthlyLedgerAccount($RowGetLedger['AccountsID'],'','','CR','',$prevdate);
		
		$res=monthlyLedgerOpenings($RowGetLedger['AccountsID'],'','','',$prevdate);
		//print_r($res);echo "<br />-----<br />";
		$TotalDROpening=$currentMonthOpeningDR+$openingDR;
		$TotalCROpening=$currentMonthOpeningCR+$openingCR;
		//echo "<br /><br />DR---".$currentMonthOpeningDR;
		//echo "<br /><br />CR---".$currentMonthOpeningCR;
		if($TotalDROpening>$TotalCROpening){$OPDR=$TotalDROpening-$TotalCROpening; $OPCR='';}
		elseif($TotalCROpening>$TotalDROpening){$OPCR=$TotalCROpening-$TotalDROpening; $OPDR='';}
		else{$OPCR='';$OPDR='';}
	}
	else
	{$OPDR=$openingDR; $OPCR=$openingCR;}



$data.="<tr>";
$data.="<td><b><font size='3px'>".$RowGetLedger['AccountName']."</font></b></td>";
$data.="<td><b><div align='right'>".$OPDR."</div></b></td>";
$data.="<td><b><div align='right'>".$OPCR."</div></b></td>";
$data.="<td><b><div align='right'>";
$data.=$total_DR;
$data.="</div></b></td>";
$data.="<td><b><div align='right'>";
$data.=$total_CR;
$data.="</div></b></td>";
$data.="<td><b><div align='right'>";
	$GtotalDR=$OPDR+$total_DR;
	$GtotalCR=$OPCR+$total_CR;
	if($GtotalDR>$GtotalCR){$closing=$GtotalDR-$GtotalCR;$closing_type="DR";}elseif($GtotalCR>$GtotalDR){$closing=$GtotalCR-$GtotalDR;$closing_type="CR";}
	else{$GtotalDR='';$GtotalCR='';}
$data.=$closing.' '.$closing_type;
$data.="</div></b></td>";
$data.="</tr>";




 } 
  }
$data.="</table>";
//echo $data;
$header='';  	
header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=APRLedgerWiseSummary_exl.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data"; 