<?php
function getUnderGroupTree($gid,$Tarray)
{
global $db;
//echo "gid---";print_r($gid);
$ID=implode(',',$gid);
//echo "ID---";print_r($ID);
//echo "<br />Tarray--";print_r($Tarray);
	$sql="Select * from accountgroupmaster where MainGroupId in ($ID)"; //echo "<br />".$sql;
	$result=DB_query($sql,$db);
	while($row=DB_fetch_array($result))
	{$array[]=$row['GroupId'];}
	if($array!=''){
	$total_array=array_merge($gid,$array);
	}
	if($Tarray!='' && $array!=''){$final=array_merge($Tarray,$total_array);}else{$final=$total_array;}
	//echo "<br />final---";print_r($final);
	//print_r($array);echo "<br />-----------in function -----------<br />";
	if($array!=''){return getUnderGroupTree($array,$final);}
	else{if($Tarray!='')return array_unique($Tarray);}
}

function GetAccountGroupmaster($mainid)
{
   global $db;
   static $rx=0;
   
   $q1=DB_query("select * from accountgroupmaster where GroupId='".$mainid."'  ",$db);
 while( $RVPrefix=DB_fetch_array($q1))
 {
 	if($RVPrefix['MainGroupId']==0)
	{
	    $rx=$RVPrefix['GroupName'];
 	   //break;
	}
	  GetAccountGroupmaster($RVPrefix['MainGroupId']);
  }
    return ($rx);
}

//====================================================================
function GetAll_GroupmasterID($mainid)
{
   global $db;
   static $rx=array();
 $q1=DB_query("select * from accountgroupmaster where MainGroupId='".$mainid."' ",$db);
 while( $RVPrefix=DB_fetch_array($q1))
 {
  	     $rx[]=$RVPrefix['GroupId'];
  	    GetAll_GroupmasterID($RVPrefix['GroupId']);
 }
 
    return ($rx);
}

//***********************************************************************
 	function CategoryShow($pcode, $PcateID, $dashVal)
	{
	    global $db;
		 
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
				$sqlCateSel = "select * from accountgroupmaster where GroupId  =".$rowCate['GroupId']."";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['GroupId']==$pcode)
				 $px="selected";
 		?>
 				<option value="<?php echo $rowCate['GroupId'];?>" <?php echo $px ?>><?php echo $dashVal;?>
				 <?php echo   $rowCate['GroupName'];?> </option>
	   <?php			
				   CategoryShow($pcode,$rowCate['GroupId'], $dashVal);
			}
		}
	}   
//***********************************	
function CategoryShow1($pcode, $PcateID, $dashVal)
	{
	    global $db;
		 
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
 			while($rowCate = DB_fetch_array($rsCate))
			{
			  $t1=APRGroupWise_TrialBalance($rowCate['GroupId'],'DR');
			  $t2=APRGroupWise_TrialBalance($rowCate['GroupId'],'CR'); 
			  	$link="index.php?route=Account/AccountingReport&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=APRGroupWise-TrialBalance-1&acc_group=".$rowCate['GroupId']."";
			//$ledger_id=$get_ledger_row['AccountsID'];
			if($t1>0 || $t2>0){
 		?>
				<tr>
					<td width="20%"><a href="<?php echo $link;?>" target="_blank"><?php echo $dashVal.$rowCate['GroupName'] ;?></a>
					<input   size="4"  type="text" value="<?php echo $rowCate['GroupId'];?>">
					</td>
					<td  width="15%"> </td>
					<td  width="15%"><?php echo APRGroupWise_TrialBalance($rowCate['GroupId'],'DR');
					$t11[]=APRGroupWise_TrialBalance($rowCate['GroupId'],'DR');
					 ?></td>
					<td  width="15%"><?php echo APRGroupWise_TrialBalance($rowCate['GroupId'],'CR'); 
					$t22[]=APRGroupWise_TrialBalance($rowCate['GroupId'],'CR');
					?> </td>
					<td  width="15%"> </td>
					<td  width="10%"> </td>
				  </tr>
 				 
	   <?php	}	
				//   CategoryShow1($pcode,$rowCate['GroupId'], $dashVal);
			}
		}
	}   
	
	
//===========================================================================
function SeperateByCommes($x)
{
   if(count($x)>1)
   {
	   $val="";
	   for($i=0;$i<count($x);$i++)
	   {
		  $val.="'".$x[$i]."',";
	   }
	   
	   $val1=substr($val,0,-1);
   }
   else{
    $val1="'".$str."'";
   }
  
   return($val1);
}

//*****************************ACCOUNTING**************************************

function Get_AccountByLedger($LId,$amount_type,$from_date='',$to_date='',$site)
{
  global $db;
  if($site!='0')
	{	
  $sitedata="Branch in('".$site."')";	
	}	
	else
	{
	$sitedata="1=1";
    }
if($from_date!='' && $to_date!='')  
{
	
	
    $q1=DB_query("select  SUM(B.amount) AS TAmount ,B.ledgerid from  account_book A,account_book_details B 
    	where A.id=B.account_bookid and A.Dates between '".str_replace("/","-",$from_date)."' and '".str_replace("/","-",$to_date)."' and  B.ledgerid in (".$LId.") and B.amount_type='".$amount_type."' and A.cancel_status=0 and ".$sitedata."  ",$db);
	//echo "select  SUM(B.amount) AS TAmount ,B.ledgerid from  account_book A,account_book_details B where A.id=B.account_bookid and A.Dates between '".str_replace("/","-",$from_date)."' and '".str_replace("/","-",$to_date)."' and  B.ledgerid in (".$LId.") and B.amount_type='".$amount_type."' <br /><br />";
}else{
     $q1=DB_query("select  SUM(B.amount) AS TAmount ,B.ledgerid from  account_book A,account_book_details B where A.id=B.account_bookid and  B.ledgerid in (".$LId.")   and B.amount_type='".$amount_type."'  AND A.Dates>='".$_SESSION['financialYrStart']."' and A.Dates<='".$_SESSION['financialYrEnd']."' and A.cancel_status=0 and ".$sitedata." ",$db);
	//echo "1-----------select  SUM(B.amount) AS TAmount ,B.ledgerid from  account_book A,account_book_details B where A.id=B.account_bookid and  B.ledgerid in (".$LId.")   and B.amount_type='".$amount_type."'  AND A.Dates>='".$_SESSION['financialYrStart']."' and A.Dates<='".$_SESSION['financialYrEnd']."' <br /><br />";
   }
    while( $RVPrefix=DB_fetch_array($q1))
    {
 	 
      return ($RVPrefix['TAmount']);
 }
}
//========To get the total value of excise duty posted in the ledgers through the transactions(Sale & Purcahse)=======
function yearlyETaxAccountByTransactions($taxId,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{	
   $sitedata="branch_name in('".$site."')";	
   $sitedata1="site in('".$site."')";	
	}	
	else
	{
	$sitedata="1=1";
	$sitedata1="1=1";
    }
	$return_array='';
	if($from_date!='' && $to_date!='')
	{

		$query1=DB_fetch_array(DB_query("Select SUM(exciseValue) as total_inv_excise from invoice where exciseDuty='".$taxId."' AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and ".$sitedata."",$db));
		$query2=DB_fetch_array(DB_query("Select SUM(A.excise_amt) as total_pur_excise ,A.bill_book_date from  purchase_billbook A where  A.excise='".$taxId."' AND A.bill_book_date>='".str_replace("/","-",$from_date)."' and A.bill_book_date<='".str_replace("/","-",$to_date)."' and ".$sitedata1."",$db));

		
		//echo "<br /><br />2---------Select SUM(B.Excise_Duty_value) as total_pur_excise ,A.bill_book_date from purchase_bill_details B, purchase_billbook A where A.id=B.purch_billbookid and B.Excise_Duty='".$taxId."' AND A.bill_book_date>='".str_replace("/","-",$from_date)."' and A.bill_book_date<='".str_replace("/","-",$to_date)."'";
	}
	else
	{
		

		$query1=DB_fetch_array(DB_query("Select SUM(exciseValue) as total_inv_excise from invoice where exciseDuty='".$taxId."' AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and ".$sitedata."",$db));
		$query2=DB_fetch_array(DB_query("Select SUM(A.excise_amt) as total_pur_excise ,A.bill_book_date from  purchase_billbook A where A.excise='".$taxId."' AND A.bill_book_date>='".$_SESSION['financialYrStart']."' and A.bill_book_date<='".$_SESSION['financialYrEnd']."' and ".$sitedata1." ",$db));
	//echo "Select SUM(B.Excise_Duty_value) as total_pur_excise ,A.bill_book_date from purchase_bill_details B, purchase_billbook A where A.id=B.purch_billbookid and B.Excise_Duty='".$taxId."' AND A.bill_book_date>='".$_SESSION['financialYrStart']."' and A.bill_book_date<='".$_SESSION['financialYrEnd']."' <br />";
	}
	//echo $query2['total_pur_excise'];
	$return_array[]=$query2['total_pur_excise'];
	$return_array[]=$query1['total_inv_excise'];
	return $return_array;
}
//====To get the total value of Sales Tax posted in the ledgers through the transactions(Sale & Purcahse)========================
function yearlySTaxAccountByTransactions($taxId,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{
     $sitedata="branch_name in ('".$site."')";
     $sitedata1="site in ('".$site."')";
	}	
	else
	{
    $sitedata="1=1";
     $sitedata1="1=1";
	}
	$return_array='';
	if($from_date!='' && $to_date!='')

	{
	$query1=DB_fetch_array(DB_query("Select sum(VatCstValue+vat_cst_amt_hrd) as total_inv_vatsct from invoice where
	(vatCst_ledger='".$taxId."' || vat_cst_hrd='".$taxId."')  AND invoiceDate>='".str_replace("/","-",$from_date)."'
	 and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata ",$db));
		
	$query2=DB_fetch_array(DB_query("Select SUM(A.taxvalue) as total_pur_vatsct from purchase_billbook A where  A.vat_cst='".$taxId."' AND A.bill_book_date>='".str_replace("/","-",$from_date)."' and A.bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata1 ",$db));
	}
	else
	{

	$query1=DB_fetch_array(DB_query("Select SUM(VatCstValue+vat_cst_amt_hrd) as total_inv_vatsct from invoice where (vatCst_ledger='".$taxId."' || vat_cst_hrd='".$taxId."')  AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."'
	 and $sitedata ",$db));
	$query2=DB_fetch_array(DB_query("Select SUM(A.taxvalue) as total_pur_vatsct from purchase_billbook A where  A.vat_cst='".$taxId."' AND A.bill_book_date>='".$_SESSION['financialYrStart']."' and A.bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));
	}
	$return_array[]=$query2['total_pur_vatsct'];
	$return_array[]=$query1['total_inv_vatsct'];

	return $return_array;
}






/************************************************************************************************************************/













function yearlyVendorAccountByTransactions($vendorId,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	$sitedata="site in ('".$site."')";
	else
	$sitedata="1=1";	
	if($vendorId!=''){
	if($from_date!='' && $to_date!='')
	{
		$query=DB_fetch_array(DB_query("Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata ",$db));
	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."'";
	}
	else
	{
	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
		$query=DB_fetch_array(DB_query("Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata ",$db));
	}
	return $query['purch_total']; }
}
//************************************
function yearlyClientAccountByTransactions($clientId,$from_date='',$to_date='',$site)
{
	global $db;
	//echo $site;
	if($site!='0')
	$sitedata1="branch_name in('".$site."')";    
	else
	$sitedata1="1=1";
	if($clientId!=''){
	if($from_date!='' && $to_date!='')
	{
		
		$query=DB_fetch_array(DB_query("Select SUM(grandTotal) as inv_total from invoice where client_id=$clientId AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
	}
	else
	{	

		$query=DB_fetch_array(DB_query("Select SUM(grandTotal) as inv_total from invoice where client_id=$clientId AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));
		
	}
	return $query['inv_total']; }
}










//************************************
function yearlyFreightAccountByTransactions($freightLid,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{
     $sitedata1="branch_name in('".$site."')";
     $sitedata="site in('".$site."')";
	}	
	else
	{
    $sitedata="1=1";
     $sitedata1="1=1";
	}
	if($freightLid!=''){
	if($from_date!='' && $to_date!='')
	{

		$query=DB_fetch_array(DB_query("Select SUM(freight) as freight_total from purchase_billbook where freight_ledger=$freightLid AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata ",$db));
		$query1=DB_fetch_array(DB_query("select sum(B.freightValue) AS total_pur_cess1 from invoice B where invoiceDate   between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$freightLid."'  and freight_desc=0  and $sitedata1",$db));		
		$query2=DB_fetch_array(DB_query("select sum(B.freightValue) AS total_pur_cess1 from invoice B where invoiceDate   between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$freightLid."'  and freight_desc=1 and $sitedata1 ",$db));
	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."'";
	}
	else
	{
		$query=DB_fetch_array(DB_query("Select SUM(freight) as freight_total from purchase_billbook where freight_ledger=$freightLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata ",$db));
		$query1=DB_fetch_array(DB_query("select sum(B.freightValue) AS total_pur_cess1 from invoice B where invoiceDate   between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' 
			   	and freight_ledger='".$freightLid."'  and freight_desc=0  and $sitedata1",$db));		
		$query2=DB_fetch_array(DB_query("select sum(B.freightValue) AS total_pur_cess1 from invoice B where invoiceDate   between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' 
			   	and freight_ledger='".$freightLid."'  and freight_desc=1 and $sitedata1 ",$db));

	
		//echo "Select SUM(freight) as freight_total from purchase_billbook where freight_ledger=$freightLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	}
	$dramt=$query2['total_pur_cess1']+$query['freight_total'];
	$cramt=$query1['total_pur_cess1'];
    $amount[]=$dramt;
    $amount[]=$cramt;


	return $amount; }
}
//************************************
function yearlySurchargeAccountByTransactions($surLid,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{
     $sitedata="branch_name in('".$site."')";
     $sitedata1="site in('".$site."')";
	}	
	else
	{
    $sitedata="1=1";
     $sitedata1="1=1";
	}	
	if($surLid!=''){
	if($from_date!='' && $to_date!='')
	{
        

        $query=DB_fetch_array(DB_query("Select SUM(sur_oct_amt) as sur_total from purchase_billbook where surcharge_ledger=$surLid AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."'";
		$query1=DB_fetch_array(DB_query("select sum(B.surcharge_hrd_amt+B.surcharge_amt) AS total_pur_cess1
														 from invoice B where invoiceDate  between '".str_replace("/","-",$from_date)."' and '".str_replace("/","-",$to_date)."' 
			   	and (surcharge_ledger='".$surLid."' || surcharge_hrd_ledger='".$surLid."' ) and $sitedata",$db));

	
	}
	else
	{

		$query=DB_fetch_array(DB_query("Select SUM(sur_oct_amt) as sur_total from purchase_billbook where surcharge_ledger=$surLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata1 ",$db));
	   $query1=DB_fetch_array(DB_query("select sum(B.surcharge_hrd_amt+B.surcharge_amt) AS total_pur_cess1
														 from invoice B where invoiceDate  between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' 
			   	and (surcharge_ledger='".$surLid."' || surcharge_hrd_ledger='".$surLid."' ) and $sitedata ",$db));

		
		//echo "Select SUM(sur_oct_amt) as sur_total from purchase_billbook where surcharge_ledger=$surLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	}
	$amount[]=$query['sur_total'];
	$amount[]=$query1['total_pur_cess1'];
	return $amount; }
}
//************************************
function yearlyPkgChargesAccountByTransactions($pkgLid,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{
     $sitedata="branch_name in('".$site."')";
     $sitedata1="site in('".$site."')";
	}	
	else
	{
    $sitedata="1=1";
     $sitedata1="1=1";
	}		
	if($pkgLid!=''){
	if($from_date!='' && $to_date!='')
	{
		

		$query=DB_fetch_array(DB_query("Select SUM(pkg_charges) as pkg_total from purchase_billbook where pckg_ledger=$pkgLid AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
       $query1=DB_fetch_array(DB_query("SELECT sum(B.pkgchargesamt) AS total_pur_cess1 from invoice B
				  where  (B.pkg_ledger=".$pkgLid.") AND B.invoiceDate between  '".str_replace("/","-",$from_date)."'    and '".str_replace("/","-",$to_date)."' and B.pkgchargesamt!='0' and $sitedata",$db));

	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."'";
	}
	else
	{
		$query=DB_fetch_array(DB_query("Select SUM(pkg_charges) as pkg_total from purchase_billbook where pckg_ledger=$pkgLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));
		 $query1=DB_fetch_array(DB_query("SELECT sum(B.pkgchargesamt) AS total_pur_cess1 from invoice B
				  where  (B.pkg_ledger=".$pkgLid.") AND B.invoiceDate between  '".$_SESSION['financialYrStart']."'   
				   and '".$_SESSION['financialYrEnd']."' and B.pkgchargesamt!='0' and $sitedata",$db));

		
		//echo "Select SUM(pkg_charges) as pkg_total from purchase_billbook where pckg_ledger=$pkgLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	}
	$amount[]=$query['pkg_total'];
	$amount[]=$query1['total_pur_cess1'];
	return $amount; }
}
//***********************************
function yearlyAddChargesAccountByTransactions($addLid,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{
     $sitedata="branch_name in ('".$site."')";
     $sitedata1="site in ('".$site."')";
	}	
	else
	{
    $sitedata="1=1";
    $sitedata1="1=1";
	}			
	if($addLid!=''){
	if($from_date!='' && $to_date!='')
	{
	
        $query=DB_fetch_array(DB_query("Select SUM(add_charges) as add_total from purchase_billbook where addcharge_ledger=$addLid AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
		$query1=DB_fetch_array(DB_query("Select SUM(additionalCharges) as add_total from invoice where other_ledger=$addLid 
			AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata",$db));

	
	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."'";
	}
	else
	{
		$query=DB_fetch_array(DB_query("Select SUM(add_charges) as add_total from purchase_billbook where addcharge_ledger=$addLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));
		$query1=DB_fetch_array(DB_query("Select SUM(additionalCharges) as add_total from invoice where other_ledger=$addLid 
			AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata",$db));
		
		//echo "Select SUM(handling) as add_total from purchase_billbook where addcharge_ledger=$addLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	}
	$amount[]=$query['add_total'];
	$amount[]=$query1['add_total'];
	return $amount;}
}
//************************************

function yearlyCessAccountByTransactions($cessid,$from_date='',$to_date='',$site)
{
	global $db;
		if($site!='0')
	{
     $sitedata1="branch_name in('".$site."')";
     $sitedata="site in('".$site."')";
	}	
	else
	{
    $sitedata="1=1";
    $sitedata1="1=1";
	}	

	if($cessid!=''){
		if($from_date!='' && $to_date!='')
	{
		$query=DB_fetch_array(DB_query("SELECT A.ecess, A.shecess, SUM(A.ecess_amt) AS total_pur_cess1, 
										SUM(A.shecess_amt) AS total_pur_cess2  from 
										purchase_billbook A where (A.ecess=".$cessid." OR A.shecess=".$cessid.")
										AND A.bill_book_date>='".str_replace("/","-",$from_date)."' 
										AND A.bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata",$db));
		$query1=DB_fetch_array(DB_query("SELECT A.Ecess_ledger, A.SCess_ledger, SUM(A.ECessValue) AS total_pur_cess1, 
										SUM(A.SAHCessValue) AS total_pur_cess2  from 
										invoice A where (A.Ecess_ledger=".$cessid." OR A.SCess_ledger=".$cessid.")
										AND A.invoiceDate>='".str_replace("/","-",$from_date)."' 
										AND A.invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
		

	}
	else
	{
		$query=DB_fetch_array(DB_query("SELECT A.ecess, A.shecess, SUM(A.ecess_amt) AS total_pur_cess1, 
										SUM(A.shecess_amt) AS total_pur_cess2  from  
										purchase_billbook A where  (A.ecess=".$cessid." OR A.shecess=".$cessid.")
										AND A.bill_book_date>='".$_SESSION['financialYrStart']."' 
										AND A.bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata",$db));
		$query1=DB_fetch_array(DB_query("SELECT A.Ecess_ledger, A.SCess_ledger, SUM(A.ECessValue) AS total_pur_cess1, 
										SUM(A.SAHCessValue) AS total_pur_cess2  from  
										invoice A where  (A.Ecess_ledger=".$cessid." OR A.SCess_ledger=".$cessid.")
										AND A.invoiceDate>='".$_SESSION['financialYrStart']."' 
										AND A.invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));


		  

		
	}
	
	if($cessid==$query['ecess'])
	{$cessVal=$query['total_pur_cess1'];}
	elseif($cessid==$query['shecess'])
	{$cessVal=$query['total_pur_cess2'];}

    if($cessid==$query1['Ecess_ledger'])
	{$cessVal1=$query1['total_pur_cess1'];}
	elseif($cessid==$query1['SCess_ledger'])
	{$cessVal1=$query1['total_pur_cess2'];}
    $amount[]=$cessval;
    $amount[]=$cessVal1;

	return $amount; }

	
}


























//************************************
function yearlyLedgerPostingByTransactions($ledgerId,$from_date='',$to_date='',$site)
{
	global $db;
	if($site!='0')
	{
	$sitedata1="branch_name in('".$site."')";	
	$sitedata="site in('".$site."')";
    }
	else
	{	
	$sitedata="1=1";
	$sitedata1="1=1";	
    }
	$return_array='';
	if($from_date!='' && $to_date!='')
	{

		$query1=DB_fetch_array(DB_query("Select SUM(basicAmount) as total_inv_basic from invoice where ledger_id in(".$ledgerId.") AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata1 ",$db));
		
		$query2=DB_fetch_array(DB_query("Select SUM(total_item_amount) as total_pur_basic from purchase_billbook where ledger in (".$ledgerId.") AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata ",$db));

		
	}
	else
	{
		$query1=DB_fetch_array(DB_query("Select SUM(basicAmount) as total_inv_basic from invoice where ledger_id in (".$ledgerId.") AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata1 ",$db));
		$query2=DB_fetch_array(DB_query("Select SUM(total_item_amount) as total_pur_basic from purchase_billbook where ledger in (".$ledgerId.") AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata ",$db));
		//echo "<br />Select SUM(total_item_amount) as total_pur_basic from purchase_billbook where ledger='".$ledgerId."' AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	}
	$return_array[]=$query2['total_pur_basic'];
	$return_array[]=$query1['total_inv_basic'];
	return $return_array;
}




function yearlyinsuranceAccountByTransactions($addLid,$from_date='',$to_date='',$site)
{
     global $db;
     if($site!='0')
	{
	$sitedata1="branch_name in('".$site."')";	
	$sitedata="site in('".$site."')";
    }
	else
	{	
	$sitedata="1=1";
	$sitedata1="1=1";	
    } 

	if($addLid!=''){
	if($from_date!='' && $to_date!='')
	{
		$query=DB_fetch_array(DB_query("Select SUM(insurance) as add_total from purchase_billbook where insurance_ledger=$addLid AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata",$db));
		$query1=DB_fetch_array(DB_query("Select SUM(insurance) as add_total from invoice where insurance_ledger=$addLid 
			AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));

	//echo "Select SUM(GrandTotal) as purch_total from purchase_billbook where vendor=$vendorId AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."'";
	}
	else
	{
		$query=DB_fetch_array(DB_query("Select SUM(insurance) as add_total from purchase_billbook where insurance_ledger=$addLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata",$db));
		$query1=DB_fetch_array(DB_query("Select SUM(insurance) as add_total from invoice where insurance_ledger=$addLid 
			AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));
		//echo "Select SUM(add_charges) as add_total from purchase_billbook where addcharge_ledger=$addLid AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	}
	$amount[]=$query['add_total'];
	$amount[]=$query1['add_total'];
	return $amount;
	//return $query['add_total']; 
    }	
}



//************************************
function Get_TotalAccountByLedger($amount_type,$from_date='',$to_date='')
{
   global $db;
 if($from_date!='' && $to_date!='')  
{
     $q1=DB_query("select  SUM(B.amount) AS TAmount ,B.ledgerid  from   account_book A, account_book_details B where A.id=B.account_bookid and  B.amount_type='".$amount_type."' and  A.Dates between '".$from_date."' and '".$to_date."' and  A.cancel_status=0  group by B.amount_type ",$db);
}else{
     $q1=DB_query("select  SUM(B.amount) AS TAmount ,B.ledgerid  from  account_book A, account_book_details B where A.id=B.account_bookid and  B.amount_type='".$amount_type."' and A.cancel_status=0 group by B.amount_type ",$db);
}
    while( $RVPrefix=DB_fetch_array($q1))
 {
  
      return ($RVPrefix['TAmount']);
 }
}

function yearlyRoundOffByTransactions($roundLid,$from_date='',$to_date='',$site)
{
	 global $db;

	  if($site!='0')
	{
	$sitedata1="branch_name in('".$site."')";	
	$sitedata="site in('".$site."')";
    }
	else
	{	
	$sitedata="1=1";
	$sitedata1="1=1";	
    } 
	 
	  if($from_date!='' && $to_date!='')
	 {
	 	
	 	$query3=DB_fetch_array(DB_query("SELECT SUM(round_off) as roundDR FROM purchase_billbook WHERE round_ledger=".$roundLid." AND pluMin='+' AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata",$db));
	 	$query4=DB_fetch_array(DB_query("SELECT ABS(SUM(round_off)) as roundCR FROM purchase_billbook WHERE round_ledger=".$roundLid." AND pluMin='-' AND bill_book_date>='".str_replace("/","-",$from_date)."' and bill_book_date<='".str_replace("/","-",$to_date)."' and $sitedata ",$db));
	 	$query5=DB_fetch_array(DB_query("SELECT SUM(roundOff) as roundCR FROM invoice WHERE round_ledger=".$roundLid." AND sign='+' AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
	 	$query6=DB_fetch_array(DB_query("SELECT ABS(SUM(roundOff)) as roundDR FROM invoice WHERE round_ledger=".$roundLid." AND sign='-' AND invoiceDate>='".str_replace("/","-",$from_date)."' and invoiceDate<='".str_replace("/","-",$to_date)."' and $sitedata1",$db));
	 
	 }
	 else
	 {
	// echo "<br />SELECT SUM(round_off) as roundDR FROM purchase_billbook WHERE round_ledger=".$roundLid." AND round_off>0 AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
	//echo "<br />SELECT SUM(round_off) as roundCR FROM purchase_billbook WHERE round_ledger=".$roundLid." AND round_off<0 AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'";
		$query3=DB_fetch_array(DB_query("SELECT SUM(round_off) as roundDR FROM purchase_billbook WHERE round_ledger=".$roundLid." AND pluMin='+' AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."' and $sitedata ",$db));
	 	$query4=DB_fetch_array(DB_query("SELECT ABS(SUM(round_off)) as roundCR FROM purchase_billbook WHERE round_ledger=".$roundLid." AND pluMin='-' AND bill_book_date>='".$_SESSION['financialYrStart']."' and bill_book_date<='".$_SESSION['financialYrEnd']."'and $sitedata ",$db));
	 $query5=DB_fetch_array(DB_query("SELECT SUM(roundOff) as roundCR FROM invoice WHERE round_ledger=".$roundLid." AND sign='+' AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata1 ",$db));
	 	$query6=DB_fetch_array(DB_query("SELECT ABS(SUM(roundOff)) as roundDR FROM invoice WHERE round_ledger=".$roundLid." AND sign='-' AND invoiceDate>='".$_SESSION['financialYrStart']."' and invoiceDate<='".$_SESSION['financialYrEnd']."' and $sitedata1",$db));
		//echo "<br />".$query3['roundDR'];
		//echo "<br />".$query4['roundCR'];		
	 }
	 	$return_array[]=$query3['roundDR']+$query6['roundDR'];
		$return_array[]=$query4['roundCR']+$query5['roundCR'];
		return $return_array;
}
//******************************************************////






//Function for Balance Sheet for calculating consolidated amount under each group//
function getGroupBalanceSheet($group_id,$from_date='',$to_date='',$site)
{
	global $db;
	//echo "from---".$from_date."to---".$to_date."---".$site;
	//echo "SELECT GroupId FROM accountgroupmaster WHERE MainGroupId=".$group_id." ";
	$sql1=DB_query("SELECT GroupId FROM accountgroupmaster WHERE MainGroupId=".$group_id." ",$db);
	$ar='';
	$ledger_arr='';
	while($res1=DB_fetch_array($sql1))
	{$ar[]=$res1['GroupId'];}
	//echo $group_id;
	//print_r($ar); echo "<br />";
	if($ar!='')
	{
		$underLevels=getUnderGroupTree($ar,'');
		//echo "underlevels--";print_r($underLevels);echo "<br />";
		if($underLevels!='')
		{$groups=$underLevels;}
		else
		{$groups=$ar;}
		//print_r($groups); echo "<br /><br />";
		$allGroups=$group_id.','.implode(',',$groups);
		 $getDirectLedgers="Select AccountsID,UnderGroup from accountledger where UnderGroup in (".$allGroups.")";
		$directLedgersResult=DB_query($getDirectLedgers,$db);
	}
	else
	{
		 $getDirectLedgers="Select AccountsID,UnderGroup from accountledger where UnderGroup=".$group_id."";
		$directLedgersResult=DB_query($getDirectLedgers,$db);
	}
		
   
		//echo "<b />****************************************<br />";
	    $ledger_arr='';$tot_VoucherDR='';$tot_VoucherCR='';$tot_CR_open='';$tot_DR_open='';$etaxDetails1='';$staxDetails1='';
		$vendorDetails1='';$clientDetails1='';$etaxDRArr='';$etaxCRArr='';$staxDRArr='';$staxCRArr='';$purchCRArr='';$invDRArr='';
		$legderDRArr='';$legderCRArr='';$freightDetails='';$freightDR='';$freightDRArr='';$surDetails='';$surDR='';$surDRArr='';
		$packDetails='';$packDR='';$packDRArr='';$addDetails='';$addDR='';$addDRArr='';$cessDetails='';$cessDRArr='';
		$roundDetails='';$roundDRArray[]='';$roundCRArray[]='';
		
		$a1='';$a2='';$b1='';$b2='';$c1='';$c2='';$d1='';$d2='';
		
		while($directLedgers=DB_fetch_array($directLedgersResult))
		{
			$ledger_arr[]=$directLedgers['AccountsID'];
		}
		//echo "-----".$group_id; echo "<br />";
		//echo $group_id."ledger_array--";print_r($ledger_arr); echo "<br /><br />";
		
if($ledger_arr!='')
{
			
	 $ledger_string=implode(',',$ledger_arr);
	  if($from_date!='' && $to_date!='')
	  {
		$tot_VoucherDR=Get_AccountByLedger($ledger_string,'DR',$from_date,$to_date,$site);
		$tot_VoucherCR=Get_AccountByLedger($ledger_string,'CR',$from_date,$to_date,$site);
	  }
	else
    {
	$tot_VoucherDR=Get_AccountByLedger($ledger_string,'DR','','',$site);
	$tot_VoucherCR=Get_AccountByLedger($ledger_string,'CR','','',$site);}
	
	if($from_date!='' && $to_date!=''){
	$tot_CR_open=getLedgerOpenings($ledger_string,'CR',$from_date,$to_date,$site);
	$tot_DR_open=getLedgerOpenings($ledger_string,'DR',$from_date,$to_date,$site);
	}else{
	$tot_CR_open=getLedgerOpenings($ledger_string,'CR','','',$site);
	$tot_DR_open=getLedgerOpenings($ledger_string,'DR','','',$site); }

	//echo $tot_CR_open."--".$tot_DR_open."<br>";
	
	$getLedgerType=DB_query("Select AccountsID,vender_client,taxid,ledgertype from accountledger 
		where AccountsID in ($ledger_string)",$db);
	//echo "<br /><br />Select AccountsID,vender_client,taxid,ledgertype from accountledger where AccountsID in ($ledger_string)";

	$invserviceDR=0;
	$final_sum=0;
	while($LedgerTypes=DB_fetch_array($getLedgerType))
	{	
	    $ledgertype=$LedgerTypes['ledgertype'];
		$master_tax_id=$LedgerTypes['taxid'];
		$master_party_id=$LedgerTypes['vender_client'];
		if($ledgertype!='' && ($ledgertype!='sales' || $ledgertype!='bank' || $ledgertype!='cash'))
		{
			if($ledgertype=='excisetax')
			{
			if($from_date!='' && $to_date!='')
			{$etaxDetails1=yearlyETaxAccountByTransactions($master_tax_id,$from_date,$to_date,$site); }
			else{$etaxDetails1=yearlyETaxAccountByTransactions($master_tax_id,'','',$site);} 
			$etaxDR1=$etaxDetails1[0]; $etaxCR1=$etaxDetails1[1];
			$etaxDRArr[]=$etaxDR1; 
			$etaxCRArr[]=$etaxCR1; 
			}else{$etaxDetails1='';$$etaxDR1='';$etaxCR1='';}		
			
			
			if($ledgertype=='salestax')
			{
			if($from_date!='' && $to_date!=''){
			$staxDetails1=yearlySTaxAccountByTransactions($master_tax_id,$from_date,$to_date,$site); }
			else{$staxDetails1=yearlySTaxAccountByTransactions($master_tax_id,'','',$site); }
          
			$staxDR1=$staxDetails1[0]; $staxCR1=$staxDetails1[1];
			$staxDRArr[]=$staxDR1;    
			$staxCRArr[]=$staxCR1;    
			}else{$staxDetails1='';}
			
			if($ledgertype=='vendor')
			{
			if($from_date!='' && $to_date!='')
			{$vendorDetails1=yearlyVendorAccountByTransactions($master_party_id,$from_date,$to_date,$site);}else{
			$vendorDetails1=yearlyVendorAccountByTransactions($master_party_id,'','',$site); }
			$purchCR1=$vendorDetails1;
			$purchCRArr[]=$purchCR1; 
			}else{$vendorDetails1='';}
			
			if($ledgertype=='client')
			{
			if($from_date!='' && $to_date!='')
			$clientDetails1=yearlyClientAccountByTransactions($master_party_id,$from_date,$to_date,$site);
			else
			$clientDetails1=yearlyClientAccountByTransactions($master_party_id,'','',$site); 
			$invDR1=$clientDetails1;
			$invDRArr[]=$invDR1; 
			}else{$clientDetails1='';}
			if($ledgertype=='freight')
			{
			if($from_date!='' && $to_date!='')
			{$freightDetails=yearlyFreightAccountByTransactions($LedgerTypes['AccountsID'],$from_date,$to_date,$site);}else{
			$freightDetails=yearlyFreightAccountByTransactions($LedgerTypes['AccountsID'],'','',$site);} 
			//$freightDR=$freightDetails; $freightDRArr[]=$freightDR; 
			$freightDR=$freightDetails[0];$freightCR=$freightDetails[1]; $freightDRArr[]=$freightDR; $freightCRArr[]=$freightCR; 

			}else{$freightDetails=''; }
			
			if($ledgertype=='surcharge')
			{
			if($from_date!='' && $to_date!='')
			{$surDetails=yearlySurchargeAccountByTransactions($LedgerTypes['AccountsID'],$from_date,$to_date,$site);}else{
			$surDetails=yearlySurchargeAccountByTransactions($LedgerTypes['AccountsID'],'','',$site);} 
			$surDR=$surDetails[0]; $surDRArr[]=$surDR; $surCR=$surDetails[1]; $surCRArr[]=$surCR; 
			}else{$surDetails='';}
			
			if($ledgertype=='pckcharge')
			{
			if($from_date!='' && $to_date!='')
			{$packDetails=yearlyPkgChargesAccountByTransactions($LedgerTypes['AccountsID'],$from_date,$to_date,$site);}else{
			$packDetails=yearlyPkgChargesAccountByTransactions($LedgerTypes['AccountsID'],'','',$site);} 
			$packDR=$packDetails[0]; $packDRArr[]=$packDR; $packCR=$packDetails[1]; $packCRArr[]=$packCR; 
			}else{$packDetails='';}
			
			if($ledgertype=='addcharge')
			{
			if($from_date!='' && $to_date!='')
			{$addDetails=yearlyAddChargesAccountByTransactions($LedgerTypes['AccountsID'],$from_date,$to_date,$site);}else{
			$addDetails=yearlyAddChargesAccountByTransactions($LedgerTypes['AccountsID'],'','',$site);} 
			$addDR=$addDetails[0];$addCR=$addDetails[1]; $addDRArr[]=$addDR;$addCRArr[]=$addCR; 
			}else{$addDetails='';}
			if($ledgertype=='cess')		
			{
			if($from_date!='' && $to_date!='')
			{$cessDetails=yearlyCessAccountByTransactions($master_tax_id,$from_date,$to_date,$site);}else{
			$cessDetails=yearlyCessAccountByTransactions($master_tax_id,'','',$site);}
			$cessDR=$cessDetails[0]; $cessDRArr[]=$cessDR;$cessCR=$cessDetails[1]; $cessCRArr[]=$cessCR;
			}else{$cessDetails='';}
			
	       if($ledgertype=='insurance')		
			{
			if($from_date!='' && $to_date!='')
			{$insurance=yearlyinsuranceAccountByTransactions($master_tax_id,$from_date,$to_date,$site);}else{
			$insurance=yearlyinsuranceAccountByTransactions($master_tax_id,'','',$site);}
			$insuranceDR=$insurance[0]; $insuranceDRArr[]=$insuranceDR;$insuranceCR=$insurance[1]; $insuranceCRArr[]=$insuranceCR;
			}else{$insurance='';}
			
			if($ledgertype=='round')		
			{
			if($from_date!='' && $to_date!='')			
			$roundDetails=yearlyRoundOffByTransactions($LedgerTypes['AccountsID'],$from_date,$to_date,$site);
			else			
			$roundDetails=yearlyRoundOffByTransactions($LedgerTypes['AccountsID'],'','',$site);			
			$roundDR=$roundDetails[0];$roundCR=$roundDetails[1];
			$roundDRArray[]=$roundDR;
			$roundCRArray[]=$roundCR;		
			
			}else
			{$roundDetails='';}
			
		}
		else
		{
			if($from_date!='' && $to_date!='')			
				$ledgerPosting1=yearlyLedgerPostingByTransactions($LedgerTypes['AccountsID'],$from_date,$to_date,$site);			
			else			 
				$ledgerPosting1=yearlyLedgerPostingByTransactions($LedgerTypes['AccountsID'],'','',$site); 	
			

			$legderDR1=$ledgerPosting1[0]; $legderCR1=$ledgerPosting1[1];
			$ledgerserviceCR=$ledgerPostingservice;
			$ledgerserviceCRArr[]=$ledgerserviceCR; 
			$legderDRArr[]=$legderDR1;  
			$legderCRArr[]=$legderCR1;
			//********************************//
			$etaxDetails1='';
			$staxDetails1='';
			$vendorDetails1='';
			$clientDetails1='';
			$freightDetails='';
			$surDetails='';
			$packDetails='';
			$addDetails='';
			$cessDetails='';
			$roundDetails='';
			$insurance='';
			$ledgerPosting2='';
		}
	}

	//print_r($cessDRArr1);

	if($etaxDRArr!='')$A1=array_sum($etaxDRArr); //echo "1--";print_r($etaxDRArr); echo "<br />";
	if($etaxCRArr!='')$A2=array_sum($etaxCRArr); //echo "2--";print_r($etaxCRArr);echo "<br />";
	if($staxDRArr!='')$B1=array_sum($staxDRArr); //echo "3--";print_r($staxDRArr);echo "<br />";
	if($staxCRArr!='')$B2=array_sum($staxCRArr); //echo "4--";print_r($staxCRArr);echo "<br />";
	if($purchCRArr!='')$C2=array_sum($purchCRArr); //echo "5--";print_r($purchCRArr);echo "<br />";
	if($invDRArr!='')$C1=array_sum($invDRArr); //echo "6--";print_r($invDRArr);echo "<br />";
	if($legderDRArr!='')$D1=array_sum($legderDRArr);  //echo "7--";print_r($legderDRArr);echo "<br />";
	if($legderCRArr!='')$D2=array_sum($legderCRArr); //echo "8--";print_r($legderCRArr);echo "<br />";
	if($freightDRArr!='')$E1=array_sum($freightDRArr); //echo "9--";print_r($freightDRArr);echo "<br />";
	if($surDRArr!='')$F1=array_sum($surDRArr); //echo "10--";print_r($surDRArr);echo "<br />";
	if($packDRArr!='')$G1=array_sum($packDRArr); //echo "11--";print_r($packDRArr);echo "<br />";
	if($addDRArr!='')$H1=array_sum($addDRArr); //echo "12--";print_r($addDRArr);echo "<br />";
	if($cessDRArr!='')$I1=array_sum($cessDRArr); //echo "13--";print_r($cessDRArr);echo "<br />";
	if($roundDRArray!='')$J1=array_sum($roundDRArray); //echo "14--";print_r($roundDRArray);echo "<br />";
	if($roundCRArray!='')$J2=array_sum($roundCRArray); //echo "15--";print_r($roundCRArray);echo "<br />";
	if($freightCRArr!='')$J3=array_sum($freightCRArr); //echo "16--";print_r($freightCRArr);echo "<br />";
    if($addCRArr!='')$J4=array_sum($addCRArr); //echo "17--";print_r($addCRArr);echo "<br />";
    if($surCRArr!='')$J5=array_sum($surCRArr); //echo "18--";print_r($surCRArr);echo "<br />";
    if($insuranceDR!='')$J6=array_sum($insuranceDR); //echo "19--";print_r($insuranceDR);echo "<br />";
    if($insuranceCR!='')$J7=array_sum($insuranceCR); //echo "20--";print_r($insuranceCR);echo "<br />";
    if($packCRArr!='')$J8=array_sum($packCRArr); //echo "21--";print_r($packCRArr);echo "<br />";
    if($cessCRArr!='')$J9=array_sum($cessCRArr); //echo "22--";print_r($cessCRArr);echo "<br />";
     if($legderCRArr1!='')$J10=array_sum($legderCRArr1); //echo "23--";print_r($legderCRArr1);echo "<br />";
      if($legderDRArr1!='')$J11=array_sum($legderDRArr1); //echo "24--";print_r($legderDRArr1);echo "<br />";

  
	//echo "<br />----------------<br /><br />";



		}
//$TD=$tot_VoucherDR+$A1+$B1+$C1+$D1+$E1+$F1+$G1+$H1+$I1+$J1+$j6+$j11;
		$TD=$tot_VoucherDR;
       //$TC=$tot_VoucherCR+$A2+$B2+$C2+$D2+$J2+$j3+$j4+$j5+$j7+$j8+$j9+$j10;
		$TC=$tot_VoucherCR;
	
$return_array[]=$TD;
$return_array[]=$TC;	
$return_array[]=$tot_DR_open;
$return_array[]=$tot_CR_open;
return $return_array;
}
function getitemOpenings($ledgers,$type,$from_date,$to_date)
{
	global $db;
	if($from_date!='' && $to_date!='')
	{
		$query="Select SUM(amount) as total_opening from opening_stockitemwise 
       where  reveive_date<='".str_replace('/','-',$to_date)."'";
	}
	else{
	$query="Select SUM(amount) as total_opening from opening_stockitemwise
     ";}
	//echo $query;
	$res=DB_query($query,$db);
	$row=DB_fetch_array($res);
   //echo $row['total_opening'];
	return $row['total_opening'];
}

	
function getLedgerOpenings($ledgers,$type,$from_date,$to_date,$site)
{
	global $db;
	global $s;
	$openingvalue='';
	$ledger1=explode(",",$ledgers);
	if(empty($ledger1))
    $ledger1=$ledgers;
for($i=0;$i<count($ledger1);$i++)
{
	 $ledgertype=$s->GetFieldValue("accountledger","AccountsID in('".$ledger1[$i]."')","ledgertype");

if($from_date!='' && $to_date!='')
	{
		
			if($site!='0')
			{
			 $query="Select SUM(B.openingBalance) as total_opening from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id where A.AccountsID in (".$ledger1[$i].") and B.openingtype='$type' and B.openingdate<='".str_replace('/','-',$to_date)."' and B.site in('".$site."')";
		    }
		    else
		    {
		     $query="Select SUM(B.openingBalance) as total_opening from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id where A.AccountsID in (".$ledger1[$i].") and B.openingtype='$type' and B.openingdate<='".str_replace('/','-',$to_date)."' ";		
		    }
		  
	}
	else
	{
	   
         	if($site!='0')
			{
		    $query="Select SUM(B.openingBalance) as total_opening from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id where A.AccountsID in (".$ledger1[$i].") and B.openingtype='$type' and B.site in('".$site."')";
	        }
	        else
	        {
             $query="Select SUM(B.openingBalance) as total_opening from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id where A.AccountsID in (".$ledger1[$i].") and B.openingtype='$type' ";
	        }
	    
    }
   // echo $ledger1[$i]."--".$query."<br>";


   $res=DB_query($query,$db);
	$row=DB_fetch_array($res);
   $openingvalue=$openingvalue+$row['total_opening'];
  }

	//$res=DB_query($query,$db);
	//$row=DB_fetch_array($res);

	return $openingvalue;
}

function getMonthsBalanceSheet($lid,$month,$yr,$flag='',$site)
{
	global $db;
		
		
		$QUERY11=DB_fetch_array(DB_query("Select vender_client from accountledger where AccountsID=$lid",$db));
		$QUERY22=DB_fetch_array(DB_query("Select taxid from accountledger where AccountsID=$lid",$db));
		$QUERY33=DB_fetch_array(DB_query("Select ledgertype from accountledger where AccountsID=$lid",$db));
		
		$master_party_id=$QUERY11['vender_client'];
		$master_tax_id=$QUERY22['taxid'];
		$ledgertype=$QUERY33['ledgertype'];
	//echo "<br />flag---".$flag."---".$site;
		//echo "isset---".isset($flag);
//////******* Conditions of dates (User selected period or financial year dates) NOT TO BE INCLUDED IN THE QUERIES**********//
if($flag!='0')
{ 
		//echo "<br />flag---".$flag."---".$site;
		if(isset($flag)==1 && $flag!='')
		{
		if($flag==1)
		{
						if($site!='0')		
						{	
							
	  $cond1="A.Dates between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."' 
	    	 and A.Branch in('".$site."')";
									$cond2="A.bill_book_date between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'and A.site in('".$site."')";
										$cond3="invoiceDate between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'	and branch_name in('".$site."')";
										$cond4="bill_date between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."' and site in('".$site."')";
										$cond5="invoice_date between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'and site in('".$site."')";	
						}	
						else
						{
						$cond1="A.Dates between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'";
						$cond2="A.bill_book_date between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'";
							$cond3="invoiceDate between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'";
							$cond4="bill_date between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'";
							$cond5="invoice_date between '".str_replace('/','-',$_SESSION['FDATE'])."' and '".str_replace('/','-',$_SESSION['TDATE'])."'";	
						}	
			}

		elseif($flag==2)
		{
		
        	if($site!='0')		
			{	
	 	$cond1="A.Dates >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(A.Dates)=$month and A.Branch in('".$site."')";
		$cond2="A.bill_book_date >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(A.bill_book_date)=$month 
		and A.site in('".$site."')";
		$cond3="invoiceDate >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(invoiceDate)=$month and branch_name in('".$site."')";
		$cond4="bill_date >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(bill_date)=$month and site in('".$site."')";
		$cond5="invoice_date >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(invoice_date)=$month and site in('".$site."')";
         	}
         	else
         	{
        $cond1="A.Dates >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(A.Dates)=$month";
		$cond2="A.bill_book_date >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(A.bill_book_date)=$month";
		$cond3="invoiceDate >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(invoiceDate)=$month";
		$cond4="bill_date >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(bill_date)=$month";
		$cond5="invoice_date >= '".str_replace('/','-',$_SESSION['FDATE'])."' and MONTH(invoice_date)=$month";
         	}
		}
		elseif($flag==3)
		{
			if($site!='0')
			{
		$cond1="A.Dates <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(A.Dates)=$month and A.Branch in('".$site."')";
		$cond2="A.bill_book_date <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(A.bill_book_date)=$month and A.site in('".$site."')";
		$cond3="invoiceDate <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(invoiceDate)=$month and branch_name='".$site."'";
		$cond4="bill_date <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(bill_date)=$month and site in('".$site."')";
		$cond5="invoice_date <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(invoice_date)=$month  and site in ('".$site."')";
		   }
		   else
		   {
       	$cond1="A.Dates <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(A.Dates)=$month ";
		$cond2="A.bill_book_date <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(A.bill_book_date)=$month";
		$cond3="invoiceDate <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(invoiceDate)=$month";
		$cond4="bill_date <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(bill_date)=$month";
		$cond5="invoice_date <= '".str_replace('/','-',$_SESSION['TDATE'])."' and MONTH(invoice_date)=$month";

		   }		
		}
		elseif($flag==4)
		{
		if($site!='0')
		{
		$cond1="MONTH(A.Dates)=$month and A.Branch in('".$site."')";
		$cond2="MONTH(A.bill_book_date)=$month and A.site in('".$site."')";
		$cond3="MONTH(invoiceDate)=$month and branch_name in('".$site."')";
		$cond4="MONTH(bill_date)=$month and site in('".$site."') ";
		$cond5="MONTH(invoice_date)=$month and site in('".$site."')";
	    }
        else
        {
        $cond1="MONTH(A.Dates)=$month";
		$cond2="MONTH(A.bill_book_date)=$month";
		$cond3="MONTH(invoiceDate)=$month";
		$cond4="MONTH(bill_date)=$month";
		$cond5="MONTH(invoice_date)=$month";

        }		
		}
	}
		elseif(isset($flag)==1 && $flag=='')
		{
			if($site!='0')
			{
			$cond1="MONTH(A.Dates)='$month' and YEAR(A.Dates)='$yr' and A.Branch in('".$site."')";
			$cond2="MONTH(A.bill_book_date)=$month and YEAR(A.bill_book_date)=$yr and A.site in('".$site."')";
			$cond3="MONTH(invoiceDate)=$month and YEAR(invoiceDate)=$yr and branch_name in('".$site."')";
			$cond4="MONTH(bill_date)=$month and YEAR(bill_date)=$yr and site in('".$site."')";
			$cond5="MONTH(invoice_date)=$month and YEAR(invoice_date)=$yr and site in('".$site."')";

			}	
			else
			{
			$cond1="MONTH(A.Dates)='$month' and YEAR(A.Dates)='$yr'";
			$cond2="MONTH(A.bill_book_date)=$month and YEAR(A.bill_book_date)=$yr";
			$cond3="MONTH(invoiceDate)=$month and YEAR(invoiceDate)=$yr";
			$cond4="MONTH(bill_date)=$month and YEAR(bill_date)=$yr";
			$cond5="MONTH(invoice_date)=$month and YEAR(invoice_date)=$yr";
		    }
		
		}
//****************************************************************************************//		
		//echo $cond1."<br />";
		//echo $cond2."<br />";echo $cond3."<br />";
		//echo $ledgertype;
		
		$getVoucherCR=DB_fetch_array(DB_query("Select SUM(B.amount) as TCRAmount from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid=$lid and B.amount_type='CR' and A.cancel_status=0 and $cond1",$db));
		$getVoucherDR=DB_fetch_array(DB_query("Select SUM(B.amount) as TDRAmount from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid=$lid and B.amount_type='DR' and A.cancel_status=0 and $cond1",$db));
		
	if($ledgertype!='' && $ledgertype!='sales')
	{
		if($ledgertype=='vendor')
		{
			$query=DB_fetch_array(DB_query("Select SUM(A.GrandTotal) as purch_total 
			from purchase_billbook A where A.vendor=$master_party_id and $cond2",$db));
			$purch_CR=$query['purch_total'];
		}
		elseif($ledgertype=='client')
		{
			if($master_party_id!='')
			{
			$query=DB_fetch_array(DB_query("Select SUM(grandTotal) as inv_total 
			from invoice where client_id=$master_party_id and $cond3",$db));
			$inv_DR=$query['inv_total'];

			
						
			
			}
		}
		else if($ledgertype=='excisetax')
		{
        
			$query1=DB_fetch_array(DB_query("Select SUM(exciseValue) as total_inv_excise from invoice 
				where exciseDuty='".$master_tax_id."' and $cond3",$db));
			$excise_CR=$query1['total_inv_excise'];
			$query2=DB_fetch_array(DB_query("Select SUM(A.excise_amt) as total_pur_excise ,A.bill_book_date from  purchase_billbook A
			 where  A.excise='".$master_tax_id."' and $cond2 ",$db));
			
		
	   		 $excise_DR=$query2['total_pur_excise'];
		}
		
		
		
		elseif($ledgertype=='salestax')
		{
			$query1=DB_fetch_array(DB_query("Select SUM(VatCstValue) as total_inv_vatsct from invoice 
				where VatCst_ledger='".$master_tax_id."' and $cond3",$db));
			$salestax_CR=$query1['total_inv_vatsct'];
			
			$query2=DB_fetch_array(DB_query("Select SUM(A.taxvalue) as total_pur_vatsct from purchase_billbook A where A.vat_cst='".$master_tax_id."' 
				and $cond2",$db));
			$salestax_DR=$query2['total_pur_vatsct'];
		}
		elseif($ledgertype=='freight')
		{
			$query1=DB_fetch_array(DB_query("Select SUM(A.freight) as freight_total from purchase_billbook A where freight_ledger=$lid and $cond2",$db));
			$query2=DB_fetch_array(DB_query("Select SUM(A.freightValue) as freight_total from invoice A where freight_ledger=$lid and freight_desc='0' and $cond3",$db));
			$query3=DB_fetch_array(DB_query("Select SUM(A.freightValue) as freight_total from invoice A where freight_ledger=$lid and freight_desc='1' and $cond3",$db));
			$freight_DR=$query1['freight_total']+$query2['freight_total'];
			$freight_CR=$query3['freight_total'];
		}
		elseif($ledgertype=='surcharge')
		{
			$query1=DB_fetch_array(DB_query("Select SUM(A.sur_oct_amt) as sur_total from purchase_billbook A where surcharge_ledger=$lid and $cond2",$db));
			$query2=DB_fetch_array(DB_query("Select SUM(A.surcharge_amt) as sur_total from invoice A where surcharge_ledger=$lid and $cond3",$db));
			$surc_DR=$query1['sur_total'];
			$surc_CR=$query2['sur_total'];
		}
		elseif($ledgertype=='pckcharge')
		{
			$query1=DB_fetch_array(DB_query("Select SUM(A.pkg_charges) as pkg_total from purchase_billbook A where pckg_ledger=$lid and $cond2",$db));
			$query2=DB_fetch_array(DB_query("Select SUM(A.pkgchargesamt) as pkg_total from invoice A where pkg_ledger=$lid and $cond3",$db));
			$pkg_DR=$query1['pkg_total'];
			$pkg_CR=$query2['pkg_total'];
		}
		elseif($ledgertype=='addcharge')
		{
			$query1=DB_fetch_array(DB_query("Select SUM(A.add_charges) as add_total from purchase_billbook A where addcharge_ledger=$lid and $cond2",$db));
			$query2=DB_fetch_array(DB_query("Select SUM(A.additionalCharges) as add_total from invoice A where other_ledger=$lid and $cond3",$db));
			$add_DR=$query1['add_total'];
			$add_CR=$query2['add_total'];
		}
		elseif($ledgertype=='insurance')
		{

			$query=DB_fetch_array(DB_query("Select SUM(A.insurance) as add_total from purchase_billbook A where A.insurance_ledger=$lid AND $cond2 ",$db));
		      $query1=DB_fetch_array(DB_query("Select SUM(insurance) as add_total from invoice where insurance_ledger=$lid 
			      AND $cond3 ",$db));
			
			$ins_DR=$query['add_total'];
			$ins_CR=$query1['add_total'];
		}
		elseif($ledgertype=='sales')
		{
			$query1=DB_fetch_array(DB_query("Select SUM(basicAmount) as total_inv_basic from invoice where ledger_id=(".$lid.") AND $cond3 ",$db));
		
		    $query2=DB_fetch_array(DB_query("Select SUM(A.total_item_amount) as total_pur_basic from purchase_billbook A where A.ledger=(".$lid.") AND $cond2",$db));

			
			
			$sales_DR=$query2['total_pur_basic'];
			$sales_CR=$query1['total_pur_basic'];
		}
		elseif($ledgertype=='cess')		
		{
			$query1=DB_fetch_array(DB_query("SELECT B.cess_ID1, B.cess_ID2, SUM(B.Edu_Cess) AS total_pur_cess1, 
										SUM(B.SHE_Cess) AS total_pur_cess2 from purchase_bill_details B, 
										purchase_billbook A where A.id=B.purch_billbookid 
										AND (B.cess_ID1=".$master_tax_id." OR B.cess_ID2=".$master_tax_id.")
										AND B.tax_type='Excisable' and $cond2",$db));
		$query2=DB_fetch_array(DB_query("SELECT A.Ecess_ledger, A.SCess_ledger, SUM(A.ECessValue) AS total_pur_cess1, 
										SUM(A.SAHCessValue) AS total_pur_cess2  from 
										invoice A where (A.Ecess_ledger=".$master_tax_id." OR A.SCess_ledger=".$master_tax_id.")
										and $cond3 ",$db));
													
			if($master_tax_id==$query1['cess_ID1'])
			{$cessDR=$query1['total_pur_cess1'];}
			elseif($master_tax_id==$query1['cess_ID2'])
			{$cessDR=$query1['total_pur_cess2'];}
		    if($master_tax_id==$query2['Ecess_ledger'])
			{$cessCR=$query2['total_pur_cess1'];}
			elseif($master_tax_id==$query2['SCess_ledger'])
			{$cessCR=$query2['total_pur_cess2'];}
			
			
		}
		elseif($ledgertype=='round')		
		{
			$query1=DB_fetch_array(DB_query("SELECT SUM(A.round_off) as roundDR FROM purchase_billbook A
											WHERE A.round_ledger=".$lid." AND A.pluMin='+' and $cond2",$db));

											
			$query2=DB_fetch_array(DB_query("SELECT ABS(SUM(A.round_off)) as roundCR FROM purchase_billbook A
											WHERE A.round_ledger=".$lid." AND A.pluMin='-' and $cond2",$db));

			$query5=DB_fetch_array(DB_query("SELECT SUM(roundOff) as roundCR FROM invoice WHERE round_ledger=".$lid." AND sign='+' AND $cond3 ",$db));
	 	    $query6=DB_fetch_array(DB_query("SELECT ABS(SUM(roundOff)) as roundDR FROM invoice WHERE round_ledger=".$lid." AND sign='-' and $cond3",$db));
										
			$round_DR=$query1['roundDR']+$query6['roundDR'];
			$round_CR=$query2['roundCR']+$query5['roundCR'];		
		}
		
	}
	else
	{
		$query1=DB_fetch_array(DB_query("Select SUM(basicAmount) as total_inv_basic from invoice where ledger_id='".$lid."' and $cond3",$db));
		$ledger_CR=$query1['total_inv_basic'];
		
		$query2=DB_fetch_array(DB_query("Select SUM(A.total_item_amount) as total_pur_basic from purchase_billbook A where A.ledger='".$lid."' and $cond2",$db));
		$ledger_DR=$query2['total_pur_basic'];
	}

	/* $totalDR=$getVoucherDR['TDRAmount']+$inv_DR+$excise_DR+$salestax_DR+$ledger_DR+$freight_DR+$surc_DR+$pkg_DR+$add_DR+$cessDR+$round_DR+$sales_DR+$ins_DR;
	$totalCR=$getVoucherCR['TCRAmount']+$purch_CR+$excise_CR+$salestax_CR+$ledger_CR+$round_CR+$freight_CR+$surc_CR+$pkg_CR+$add_CR+$cessCR+$sales_CR+$ins_CR; */

	$totalDR=$getVoucherDR['TDRAmount'];
	$totalCR=$getVoucherCR['TCRAmount'];


	
	$return_array[]=$totalDR;
	$return_array[]=$totalCR;
	
	return $return_array;
	 }
}

function monthlyLedgerAccount($ledgerid,$lastmonth,$lastyr,$type,$currmonth,$prevdate='')
{
	global $db;
	if($lastmonth<=9){$mnth="0".$lastmonth;}else{$mnth=$lastmonth;}
	if($mnth=='04')
	{
		$lastFinanceStart=$_SESSION['financialYrStart'];
		$lastFinanceEnd=$_SESSION['financialYrEnd'];
	}
	if($prevdate!='')
	{
		echo $sql="Select SUM(B.amount) as TAMOUNT from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid=$ledgerid and A.Dates between '".$_SESSION['financialYrStart']."' and '".$prevdate."' and B.amount_type='$type' and A.cancel_status=0 "; 
	//echo "<br />111-".$sql;
		$result=DB_query($sql,$db);
		$row=DB_fetch_array($result);
	}else{
	if($currmonth!='04')
	{
		$lastDateofPreviousMonth=date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date($mnth).'/01/'.date($lastyr).' 00:00:00'))));
		$sql="Select SUM(B.amount) as TAMOUNT from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid=$ledgerid and A.Dates between '".$_SESSION['financialYrStart']."' and '".$lastDateofPreviousMonth."' and B.amount_type='$type' and A.cancel_status=0"; //echo "<br />222-".$sql;
		$result=DB_query($sql,$db);
		$row=DB_fetch_array($result);
		
	}}
	return $row['TAMOUNT'];
}


//****** CONCEPT SIMILAR TO ABOVE
function monthlyLedgerOpenings($ledgerid,$lastmonth,$lastyr,$currmonth,$prevdate='',$site)
{
	global $db;
	if($lastmonth<=9){$mnth="0".$lastmonth;}else{$mnth=$lastmonth;}
	if($mnth=='04')
	{
		$lastFinanceStart=$_SESSION['financialYrStart'];
		$lastFinanceEnd=$_SESSION['financialYrEnd'];
	}
	if($prevdate!='')
	{
		$last_date=$prevdate;
	}
	else
	{
		if($currmonth!='04')
		{
			$last_date=date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date($mnth).'/01/'.date($lastyr).' 00:00:00'))));
		}
	}
	$eDRARR='';$eCRARR='';
	$sDRARR='';$sCRARR='';
	$purchCRARR='';$invDRARR='';
	$ledgerDRARR='';$ledgerCRARR='';
	$ledgerDetailsSQL=DB_query("Select * from accountledger where AccountsID in ($ledgerid)",$db);
	while($ledgerDetailRow=DB_fetch_array($ledgerDetailsSQL))
	{
		$ledgertype=$ledgerDetailRow['ledgertype'];
		$master_tax_id=$ledgerDetailRow['taxid'];
		$master_party_id=$ledgerDetailRow['vender_client'];
		if($ledgertype!='' && $ledger_type!='sales')
		{
			if($ledgertype=='excisetax')
			{
				$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,$_SESSION['financialYrStart'],$last_date,$site);
				$eTaxDR=$etaxDetails[0]; $eDRARR[]=$eTaxDR;
				$eTaxCR=$etaxDetails[1]; $eCRARR[]=$eTaxCR;
			}else{$eTaxDR='';$eTaxCR='';}
			if($ledgertype=='salestax')
			{
				$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,$_SESSION['financialYrStart'],$last_date,$site); 
				$sTaxDR=$staxDetails[0]; $sDRARR[]=$sTaxDR; 
				$sTaxCR=$staxDetails[1]; $sCRARR[]=$sTaxCR;
			}else{$sTaxDR='';$sTaxCR='';}
			if($ledgertype=='vendor')
			{
				$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,$_SESSION['financialYrStart'],$last_date,$site);
				$purchCR=$vendorDetails; $purchCRARR[]=$purchCR;
			}else{$vendorDetails='';$purchCR='';}
			if($ledgertype=='client')
			{
				$clientDetails=yearlyClientAccountByTransactions($master_party_id,$_SESSION['financialYrStart'],$last_date,$site); 
				$invDR=$clientDetails; $invDRARR[]=$invDR;
				
			}else{$invDR='';}		
		}
		else
		{
			$ledgerPosting=yearlyLedgerPostingByTransactions($ledgerDetailRow['AccountsID'],$_SESSION['financialYrStart'],$last_date,$site); 
			$ledgerDR=$ledgerPosting[0]; $ledgerDRARR[]=$ledgerDR;
			$ledgerCR=$ledgerPosting[1]; $ledgerCRARR[]=$ledgerCR;
		}
	}
	if($eDRARR!='')$aa=array_sum($eDRARR);
	if($eCRARR!='')$bb=array_sum($eCRARR);
	if($sDRARR!='')$cc=array_sum($sDRARR);
	if($sCRARR!='')$dd=array_sum($sCRARR);
	if($invDRARR!='')$ee=array_sum($invDRARR);
	if($purchCRARR!='')$ff=array_sum($purchCRARR);
	if($ledgerDRARR!='')$gg=array_sum($ledgerDRARR);
	if($ledgerCRARR!='')$hh=array_sum($ledgerCRARR);
	
	$TotalOPDR=$aa+$cc+$ee+$gg;
	$TotalOPCR=$bb+$dd+$ff+$hh;
	
	$return_arr[]=$TotalOPDR;
	$return_arr[]=$TotalOPCR;
	
	return $return_arr;
}

 function proffitAndLoss($site)
 {
 	global $db;
	$table='accountgroupmaster';
	$getExpenseId=DB_fetch_array(DB_query("Select GroupId from accountgroupmaster where GroupName='EXPENSE'",$db));
	$ExpenseId=$getExpenseId['GroupId'];
	//getExpenseId=$s->getField($table,'GroupName','EXPENSE','GroupId');
	$getUnderGroups=DB_query("select * from $table where NatureOfGroup=$ExpenseId and MainGroupId=0",$db);
		
		while($underGroups=DB_fetch_array($getUnderGroups))
		{
			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$x=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
			else{$x=getGroupBalanceSheet($underGroups['GroupId'],'','',$site);}
			
			if($x[0]>$x[1]){$LIABDR=$x[0]-$x[1];$LIABDR_ARR[]=$LIABDR;}
			elseif($x[1]>$x[0]){$LIABCR=$x[1]-$x[0];$LIABCR_ARR[]=$LIABCR;}
			
		} 
		
		
		
	if($LIABDR_ARR!=''){$sum1=array_sum($LIABDR_ARR);}
    if($LIABCR_ARR!=''){$sum2=array_sum($LIABCR_ARR);}
    if($sum1>$sum2){$expense=($sum1-$sum2); $expense_type='DR';}else{$expense=($sum2-$sum1);$expense_type='CR';}
 
 	$getIncomeId=DB_fetch_array(DB_query("Select GroupId from accountgroupmaster where GroupName='INCOME'",$db));
	$IncomeId=$getIncomeId['GroupId'];
 
 		//$getIncomeId=$s->getField($table,'GroupName','INCOME','GroupId');
	  $getUnderGroups=DB_query("select * from $table where NatureOfGroup=$IncomeId and MainGroupId=0",$db);
		while($underGroups=DB_fetch_array($getUnderGroups))
		{
			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$y=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
			else{
			$y=getGroupBalanceSheet($underGroups['GroupId'],'','',$site);}
			
			if($y[0]>$y[1]){$ASSDR=$y[0]-$y[1];  $ASSDR_ARR[]=$ASSDR;}
			elseif($y[1]>$y[0]){$ASSCR=$y[1]-$y[0]; $ASSCR_ARR[]=$ASSCR;}
			
		} 
 
 		if($ASSDR_ARR!=''){$sumA1=array_sum($ASSDR_ARR);}
   if($ASSCR_ARR!=''){$sumA2=array_sum($ASSCR_ARR);}
  if($sumA1>$sumA2){$income=($sumA1-$sumA2); $income_type='DR'; }else{$income=($sumA2-$sumA1); $income_type='CR';}
 
  if($expense_type==$income_type)
  {
     if($expense_type=='CR')
	 {
	 	$proffit=$expense+$income;
		$proffit_type=$expense_type;
	 }
	 elseif($expense_type=='DR')
	 {
	 	$loss=$expense+$income;
		$loss_type=$expense_type;
	 }
  }
  elseif($expense_type!=$income_type)
  {
  		if($expense>$income)
		{
			$value=$expense-$income;
			$value_type=$expense_type;
		}
		elseif($income>$expense)
		{
			$value=$income-$expense;
			$value_type=$income_type;
		}
		
		if($value_type=='CR')
		{$proffit=$value;$proffit_type=$value_type;}
		elseif($value_type=='DR')
		{$loss=$value;$loss_type=$value_type;}
  }
  else{$loss=''; $loss_type=''; $proffit=''; $proffit_type='';}
 
 $return_array[]=$proffit;
 $return_array[]=$proffit_type;
 $return_array[]=$loss;
 $return_array[]=$loss_type;

 
 return $return_array;
 
 } 



function postedclosingledger_array($ledgerid)
{
	global $db;
	$srt_yr=explode('-', $_SESSION['financialYrStart']);
 	$end_yr=explode('-', $_SESSION['financialYrEnd']);
 	$prev_srt_yr1=$srt_yr[0]-1;
 	$prev_end_yr1=$end_yr[0]-1;
 	//echo "select ledgerid,manual_closing_amt,manual_closing_type from acc_fin_yr_closing where start_yr='".$prev_srt_yr1."' and end_yr='".$prev_end_yr1."' and ledgerid='".$ledgerid."' "."<br>";
	$posted_led_sql=DB_query("select ledgerid,manual_closing_amt,manual_closing_type from acc_fin_yr_closing where start_yr='".$prev_srt_yr1."' and end_yr='".$prev_end_yr1."' and ledgerid='".$ledgerid."' ",$db);
	while($posted_led_res1=DB_fetch_assoc($posted_led_sql))
	{
		$posted_led_res[$posted_led_res1['ledgerid']]=$posted_led_res1;
	}
	return $posted_led_res;
}
function getledgeropeningyearwise($ledgerid,$site)
{
	 global $db;
	 	$posted_led='';
	 //	echo "select * from accountledger where AccountsID='".$ledgerid."'"."<br>";
	$led_sql=DB_fetch_assoc(DB_query("select * from accountledger where AccountsID='".$ledgerid."'",$db));
	 $led_sql['ledgertype'];
    
    	if($site!='0')
    	{
    		$sql_check_dtls=DB_query("select B.openingtype,B.openingBalance  from accountledger_dtls B  inner join accountledger A on
		           A.AccountsID=B.accountledger_id where A.AccountsID='".$led_sql['AccountsID']."' and B.site in('".$site."') and B.openingdate between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' ",$db); 

    	}
    	else
    	{
          $sql_check_dtls=DB_query("select B.openingtype,B.openingBalance  from accountledger_dtls B  inner join accountledger A on
		           A.AccountsID=B.accountledger_id where A.AccountsID='".$led_sql['AccountsID']."'  and B.openingdate between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' ",$db); 

    	}
     
      
		         
		           while($row_dtls=DB_fetch_array($sql_check_dtls))
			        {
			         $opening_amt[$row_dtls['openingtype']][]=$row_dtls['openingBalance']; 
			        }
         
			          if(!empty($opening_amt[DR]))
			          {
			          $openingDR=array_sum($opening_amt[DR]);
			          $led_opening[DR]=$openingDR;
			          //$led_opening[]='DR';
			          }
			          if(!empty($opening_amt[CR]))
			          {
			          $openingCR=array_sum($opening_amt[CR]);
			          $led_opening[CR]=$openingCR;
			          //$led_opening[]='CR';
                      }  
    
	// echo "<pre>";
     //print_r($led_opening);

	return $led_opening;
}
// -----------------------opening for particular group----------------------------------------------------------//

function getgroupopening($group_res)
{
	global $db;
	foreach($group_res as $key => $val)
	{
		//echo "select * from accountledger where UnderGroup='".$val."'";
		$led_sql=DB_query("select * from accountledger where CompanyId='".$_SESSION['ACGCompanyId']."' and  UnderGroup='".$val."'",$db);
		while($led_res=DB_fetch_assoc($led_sql))
		{
			$open_array=getledgeropeningyearwise($led_res['AccountsID']);
			if($open_array[0]!=0)
			{
				if($open_array[1]=='DR')
					$open_amt_dr[]=$open_array[0];
				else if($open_array[1]=='CR')	
					$open_amt_cr[]=$open_array[0];
			}
		}

	}
		  $tot_dr=array_sum($open_amt_dr);
		  $tot_cr=array_sum($open_amt_cr);
		if($tot_dr<$tot_cr)
			{$final_opening[]=$tot_cr-$tot_dr;
			 $final_opening[]='CR';}		
		elseif($tot_dr>$tot_cr)
			{$final_opening[]=$tot_dr-$tot_cr;
			 $final_opening[]='DR';}
			 
	return $final_opening;
}

// ------------------------------------- get all down group --------------------------------------------//
function getallundergroups($groupid)
{
	global $db;
	$group_sql=DB_query("SELECT GroupId FROM accountgroupmaster where  MainGroupId='".$groupid."'",$db) ; 
	while($group_res1=DB_fetch_assoc($group_sql))
		{
		
			$_SESSION['down_acc_group'][]=$group_res1['GroupId'];
			//$dwn_grp_arr[$k]=$groupid;
			getallundergroups($group_res1['GroupId'],$group_res);		
		}	
		return $_SESSION['down_acc_group'];
}



function monthlyLedgerAccount_new($ledgerid,$lastmonth,$lastyr,$type,$currmonth,$prevdate='')
{
	global $db;
	if($lastmonth<=9){$mnth="0".$lastmonth;}else{$mnth=$lastmonth;}
	if($mnth=='04')
	{
		$lastFinanceStart=$_SESSION['financialYrStart'];
		$lastFinanceEnd=$_SESSION['financialYrEnd'];
	}

	if($prevdate!='')
	{
		/* $sql="Select SUM(B.amount) as TAMOUNT from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid=$ledgerid and A.Dates between '".$_SESSION['financialYrStart']."' and '".$prevdate."' and B.amount_type='$type' and A.cancel_status=0 "; */
           if($_REQUEST['site']!='0')
           {
		 $sql="Select SUM(amount) as TAMOUNT from account_book inner join account_book_details on account_book.id=account_book_details.account_bookid where account_book_details.ledgerid=$ledgerid and account_book.Dates < '".$prevdate."' and account_book_details.amount_type='$type' and branch='".$_REQUEST['site']."' and account_book.cancel_status=0 ";
		  }
		  else
		  {
		  	 $sql="Select SUM(amount) as TAMOUNT from account_book inner join account_book_details on account_book.id=account_book_details.account_bookid where account_book_details.ledgerid=$ledgerid and account_book.Dates < '".$prevdate."' and account_book_details.amount_type='$type' and account_book.cancel_status=0 ";
		  }

	//echo "<br />111-".$sql;
		$result=DB_query($sql,$db);
		$row=DB_fetch_array($result);
	}else{
	if($currmonth!='04')
	{
		$lastDateofPreviousMonth=date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date($mnth).'/01/'.date($lastyr).' 00:00:00'))));
		$sql="Select SUM(amount) as TAMOUNT from account_book A, account_book_details B where A.id=B.account_bookid and B.ledgerid=$ledgerid and A.Dates between '".$_SESSION['financialYrStart']."' and '".$lastDateofPreviousMonth."' and B.amount_type='$type' and A.cancel_status=0"; //echo "<br />222-".$sql;
		$result=DB_query($sql,$db);
		$row=DB_fetch_array($result);
		
	}}
	return $row['TAMOUNT'];
}


function get_opening_filter_basis($ledgerid,$site,$fdate,$type)
{ 
  global $db;
  
  if($fdate!='')
  {
  	 if($site!='0')
  	 {
  	     $sql="Select SUM(amount) as TAMOUNT from account_book inner join account_book_details on account_book.id=account_book_details.account_bookid where account_book_details.ledgerid=$ledgerid and account_book.Dates < '".$fdate."' and account_book_details.amount_type='$type' and branch='".$site."' and account_book.cancel_status=0 ";
  	 }
  	 else
  	 {
        $sql="Select SUM(amount) as TAMOUNT from account_book inner join account_book_details on account_book.id=account_book_details.account_bookid where account_book_details.ledgerid=$ledgerid and account_book.Dates < '".$fdate."' and account_book_details.amount_type='$type' and account_book.cancel_status=0 ";
  	 }    

  }
  else
  {
  	if($site!='0')
  	 {
  	      $sql="Select SUM(amount) as TAMOUNT from account_book inner join account_book_details on account_book.id=account_book_details.account_bookid where account_book_details.ledgerid=$ledgerid  and account_book_details.amount_type='$type' and branch='".$site."' and account_book.cancel_status=0 ";
  	 }
  	 else
  	 {
        $sql="Select SUM(amount) as TAMOUNT from account_book inner join account_book_details on account_book.id=account_book_details.account_bookid where account_book_details.ledgerid=$ledgerid  and account_book_details.amount_type='$type' and account_book.cancel_status=0 ";
  	 } 
  }	 
       //echo $sql;
  	 $result=DB_query($sql,$db);
  	 $rs=DB_fetch_array($result);
  	 //echo $rs[0];

  	 return $rs[0];  
  }


   ?>  


 