<?php

function oppp($ledgerid,$todate_1,$fromdate_1,$site)
{

	global $db;
	global $s;	
	
	$cramt_privious='';
	  $dramt='';
	  $DR_amount='';
	  $CR_amount='';
	  $AMTDR='';
	  $AMTCR='';
	
        if($site!='') {
                     
          		  $sql_check_dtls=DB_query("select B.openingtype,B.openingBalance,B.openingdate  from accountledger_dtls B  
          		 	inner join accountledger A on A.AccountsID=B.accountledger_id where A.AccountsID='".$ledgerid."'
             	    and B.openingdate between '".$todate_1."'    and '".$fromdate_1."' and B.site='".$site."' 
             	    order by B.openingdate ASC",$db);  }
               else {        
           		$sql_check_dtls=DB_query("select B.openingtype,B.openingBalance,B.openingdate ,B.site from accountledger_dtls B
           		     inner join accountledger A on A.AccountsID=B.accountledger_id where A.AccountsID='".$ledgerid."' 
           		     and  B.openingdate between '".$todate_1."'    and '".$fromdate_1."' order by B.openingdate ASC ",$db); }
           while($row_dtls=DB_fetch_array($sql_check_dtls))
	        { 
	        	
	        	if($row_dtls['openingtype']=='DR')
	            $dramt[]=$row_dtls['openingBalance'];
	           else if($row_dtls['openingtype']=='CR')
	           $cramt_privious[]=$row_dtls['openingBalance'];
	           $opdate=$row_dtls['openingdate'];
	                
	        }
	        if(!empty($dramt)) { $draount=array_sum($dramt); } else $draount=0;
	        if(!empty($cramt_privious)) { $craount=array_sum($cramt_privious);} else $craount=0;
	        if($draount>$craount) { $dramount1=$draount-$craount; $cramount1=0; $type='DR';}
	        if($draount<$craount) { $cramount1=$craount-$draount; $dramount1=0;$type='CR';}
	        if($dramount1!=0 || $cramount1!=0)
	        	$DR_amount[]=$dramount1;
	            $CR_amount[]=$cramount1;
         
	        
    

	 $ledgertype=$s->getField('accountledger','AccountsID',$ledgerid,'ledgertype');
        
    /*   if($ledgertype=='scess')
        {
          $master_tax_id=$s->getField('accountledger','AccountsID',$ledgerid,'taxid');
          if($site!='')
          {
          $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B 	where  (B.Ecess_ledger=".$master_tax_id." OR B.SCess_ledger=".$master_tax_id.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and branch_name='".$site."' ",$db);
          }
          else
          {
          $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B where  (B.Ecess_ledger=".$master_tax_id." OR B.SCess_ledger=".$master_tax_id.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' 	",$db);                  
          }	

									while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
									{
										if($direct_sale_res1['cess_ID1']!='' || $direct_sale_res1['scess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res1;
										if($master_tax_id==$direct_sale_res1['cess_ID1'])
										$amt[]=$direct_sale_res1['total_pur_cess1'];
									    else
									    $amt[]=$direct_sale_res1['total_pur_cess2'];	
										$amt_type[]='CR';
									}
									}

        } */
       

		
		
			if($site=="")
			{
				$con= " "; 
				$con1= " "; 
				$site_data=array();
				$sql_site=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
				while($row_site=DB_fetch_array($sql_site))
				{
				$site_data[]=$row_site['loccode'];
				}
				$site_data1=implode("','",$site_data);
				$site_branch="Branch in('".$site_data1."')";
				$site_branch1="site in('".$site_data1."')";
			}
			else
			{
				$con= " and  branch_name='".$site."' "; 
				$con1= " and  b.branch_name='".$site."' "; 
				$site_branch="Branch='".$site."'";
		        $site_brach1="site='".$site."'";
			}
			$GetRec ="SELECT A.VPrefix as Prefixtype,A.Voucher_NO ,A.id AS Pid,A.Dates as Dates, A.transation_date_and_time as trans FROM account_book A WHERE   A.Dates between  '".$todate_1."' and '".$fromdate_1."' 
			and $site_branch and A.cancel_status=0   ORDER BY A.Dates ASC ";
			$ResultData5=DB_query($GetRec,$db);
			$numrows5 = DB_num_rows($ResultData5);
			while($RowData5=DB_fetch_array($ResultData5))
			{
				$trnasdata1=explode(" ",$RowData5['trans']);

				$trans_date=$RowData5['Dates']." ".$trnasdata1[1];
				$data_array[]=array('Pid'=>$RowData5['Pid'],'date'=>$RowData5['Dates'],'flag'=>'voucher','Voucher_NO'=>$RowData5['Voucher_NO'],'Vprefix'=>$RowData5['Prefixtype'],'type'=>'','vou_date'=>$trans_date);
			}
			


			if($ledgertype=='' )
	        {
			$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$sitedata."') and  ledger='".$ledgerid."' and total_basic_amount!='0.00'";
           


           /* if($site!='')
				 {
				 	$direct_sale_sql=DB_query("SELECT  (B.discounted_amt) AS total_pur_cess1,client_id as ledger,  
												B.transaction_date_time  AS transdate ,B.invoiceDate 
												 AS bill_book_date ,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
												from invoice B 												
												where  (B.ledger_id=".$ledgerid.") and branch_name='".$site."'
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);
					
				 }
				else 
				{



					$direct_sale_sql=DB_query("SELECT  (B.discounted_amt) AS total_pur_cess1,client_id as ledger,  
												B.transaction_date_time  AS transdate ,B.invoiceDate 
												 AS bill_book_date ,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
												from invoice B 												
												where  (B.ledger_id=".$ledgerid.") 
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);

				}	*/
				 	

		               
             
               

				 	
					/* while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
					{
						$direct_sale_res[]=$direct_sale_res1;
						$amt[]=$direct_sale_res1['total_pur_cess1'];
						$amt_type[]='CR';
					} */


			
			}
		/* 	else if($ledgertype=='vendor')
			{
				if($site!='')
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$sitedata."') and vendor='".$vendorid."' and GrandTotal!='0.00'";
				 else
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and  vendor='".$vendorid."' and GrandTotal!='0.00'	";
			} */
          /* else if($ledgertype=='insurance')
			{
				if($site!='')	
				{
				
				  $direct_sale_sql=DB_query("select (B.insurance) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (insurance_ledger='".$ledgerid."'  ) and branch_name  in('".$_REQUEST['site']."')  ".$con,$db);

				}
				else
				{
				
				 $direct_sale_sql=DB_query("select (B.insurance) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (insurance_ledger='".$ledgerid."' )  ".$con,$db);	
				}
					while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
					{
						$direct_sale_res[]=$direct_sale_res1;
						$amt[]=$direct_sale_res1['total_pur_cess1'];
						$amt_type[]='DR';
					}
			} */

        /*  else if($ledgertype=='surcharge')
			{
				if($site!='')	
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and surcharge_ledger='".$ledgerid."' and sur_oct_amt!='0.00'";
				  $direct_sale_sql=DB_query("select (B.surcharge_hrd_amt+B.surcharge_amt) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (surcharge_ledger='".$ledgerid."' || surcharge_hrd_ledger='".$ledgerid."' ) and branch_name  in('".$_REQUEST['site']."') ".$con,$db);

				}
				else
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and surcharge_ledger='".$ledgerid."' and sur_oct_amt!='0.00'"; 
				 $direct_sale_sql=DB_query("select (B.surcharge_hrd_amt+B.surcharge_amt) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (surcharge_ledger='".$ledgerid."' || surcharge_hrd_ledger='".$ledgerid."' )  ".$con,$db);	
				}
					while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
			} */
			/* else if($ledgertype=='freight')
			{
				if($site!='')
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and freight_ledger='".$ledgerid."' and freight!='0.00'";
				  $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."') and freight_desc=0 ".$con,$db);
				    while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}
				  $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."') and freight_desc=1 ".$con,$db); 
				
				  while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
				}
				else
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and freight_ledger='".$ledgerid."' and freight!='0.00'";	

			   $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."'  and freight_desc=1 ".$con,$db);
			      while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
			    $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."'  and freight_desc=0 ".$con,$db);
				 while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}
		    	}				

				
				
			} */
			/* else if($ledgertype=='salestax')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			    if($site!='')
			    {	
				 $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate,A.id as id,A.vendor as vendor,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$site."') and A.vat_cst='".$ledgerid."' and A.taxvalue!='0.00' Group by A.id  ";

                 $direct_sale_sql=DB_query("select (B.VatCstValue+B.vat_cst_amt_hrd) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (vatCst_ledger='".$ledgerid."' || vat_cst_hrd='".$ledgerid."') and branch_name in('".$site."')  ".$con,$db);

				}
				else
				{
				   $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
				    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
				  and A.vat_cst='".$ledgerid."' and A.taxvalue!='0.00' Group by A.id "; 
				   $direct_sale_sql=DB_query("select (B.VatCstValue+B.vat_cst_amt_hrd) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (vatCst_ledger='".$ledgerid."' || vat_cst_hrd='".$ledgerid."')   ".$con,$db);
				}
				while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}

			} */

			/* else if($ledgertype=='excisetax')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			 if($site!='')	
			 {
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate ,A.id as id,A.vendor as vendor,A.remarks as remarks  FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and B.Excise_Duty='".$ledgerid."' and B.Excise_Duty_value!='0.00' 
				 Group by A.id  ";
				 $direct_sale_sql=DB_query("SELECT  (B.exciseValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.exciseDuty=".$ledgerid.") 
														and branch_name='".$_REQUEST['site']."' AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'	",$db);
			 }	 
			 else
			 {
				   $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
				    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
				  and B.Excise_Duty='".$ledgerid."' and B.Excise_Duty_value!='0.00' Group by A.id "; 
         
		 $direct_sale_sql1=DB_query("SELECT  (B.exciseValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 	  where	invoiceDate between '".$todate_1."' and '".$fromdate_1."' and B.exciseDuty='".$ledgerid."' ",$db);


		     }
		     while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql1))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='CR';
							}

			} */

			/* else if($ledgertype=='cess')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			 if($site!='')	
			 {
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks  FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and (B.cess_ID1='".$ledgerid."' || B.cess_ID2='".$ledgerid."') and (B.Edu_Cess!='0.00' || B.SHE_Cess!='0.00') Group by A.id  ";

                $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
				  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
				  from invoice B 	where  (B.Ecess_ledger=".$ledgerid." OR B.SCess_ledger=".$ledgerid.")
				  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and branch_name='".$_REQUEST['site']."' ",$db);

				
			}
			else
			{
		    $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
		    AS billdate,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
            purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
		   and (B.cess_ID1='".$ledgerid."' || B.cess_ID2='".$ledgerid."') and (B.Edu_Cess!='0.00' || B.SHE_Cess!='0.00') Group by A.id "; 
		   
           $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B where  (B.Ecess_ledger=".$ledgerid." OR B.SCess_ledger=".$ledgerid.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' 	",$db);     

			}
		

							while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
									{
										if($direct_sale_res1['cess_ID1']!='' || $direct_sale_res1['scess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res1;
										if($master_tax_id==$direct_sale_res1['cess_ID1'])
										$amt[]=$direct_sale_res1['total_pur_cess1'];
									    else
									    $amt[]=$direct_sale_res1['total_pur_cess2'];	
										$amt_type[]='CR';
									}
								}

		}



			 */
			/* else if($ledgertype=='pckcharge')
			{
				 if($site!='')
				 {
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and pckg_ledger='".$ledgerid."' and pkg_charges!='0.00'";
				  $direct_sale_sql25=DB_query("SELECT B.pkg_ledger as cess_ID1, (B.pkgchargesamt) AS total_pur_cess1,client_id 
				  as ledger,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo 
				  as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks from invoice B
				  where  (B.pkg_ledger=".$ledgerid.") AND B.invoiceDate between  '".$todate_1."' and B.branch_name in('".$_REQUEST['site']."')  and '".$fromdate_1."' and B.pkgchargesamt!='0'	",$db);
				 }
				 else
				 {	
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				  and pckg_ledger='".$ledgerid."' and pkg_charges!='0.00'";	
				  $direct_sale_sql25=DB_query("SELECT B.pkg_ledger as cess_ID1, (B.pkgchargesamt) AS total_pur_cess1,client_id 
				  as ledger,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo 
				  as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks from invoice B
				  where  (B.pkg_ledger=".$ledgerid.") AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and B.pkgchargesamt!='0' 	",$db);   
				 } 
				 while($direct_sale_res25=DB_fetch_assoc($direct_sale_sql25))
									{
										if($direct_sale_res25['cess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res25;
										$amt[]=$direct_sale_res25['total_pur_cess1'];	
										$amt_type[]='CR';
									}
								}
			}
                */
			/* else if($ledgertype=='client')
			{
				//echo "dfdf";
                $client_check=DB_fetch_assoc(DB_query("select vender_client from accountledger  where AccountsID='".$ledgerid."'",$db));
				 if($client_check['vender_client']!='')
				 {  
				 	// "ravi".$site;

						 if($site!='')
						 {

						 	$direct_sale_sql=DB_query("SELECT  (B.grandTotal) AS total_pur_cess1,ledger_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.client_id=".$client_check['vender_client'].") 
														and branch_name='".$site."' AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'	",$db);
							
						 }
						else
						{
                          

							$direct_sale_sql=DB_query("SELECT  (B.grandTotal) AS total_pur_cess1,ledger_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.client_id=".$client_check['vender_client'].") 
											AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);

						}	 	
				 	
							
						
				}



			}  */
			
			/* else if($ledgertype=='addcharge')
			{
				if($site!='')
				{	
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."')  and addcharge_ledger='".$ledgerid."' and handling!='0.00'";
				   $direct_sale_sql=DB_query("select (B.otheramount) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and other_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."')  ".$con,$db);


				}
				else
				{
                $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."' and 
				  addcharge_ledger='".$ledgerid."' and handling!='0.00'";
				    $direct_sale_sql=DB_query("select (B.otheramount) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and other_ledger='".$ledgerid."'  ".$con,$db);


				}
				while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='DR';
							}

			}

        */

           /* else if($ledgertype=='sales'){
			 if($site!=''){
			 $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$site."') and ledger='".$ledgerid."' ";			 
			  $direct_sale_sql1=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   discounted_amt As basic_amount,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."' and branch_name in('".$site."') ",$db); }                 else {
                $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			      and ledger='".$ledgerid."' and round_off!='0.00'";			 
			    $direct_sale_sql1=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   discounted_amt As basic_amount,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."' ",$db);  }          
                       while($direct_sale_res12=DB_fetch_assoc($direct_sale_sql1)){
									if($direct_sale_res12['basic_amount']!='0.00'){
									$direct_sale_res[]=$direct_sale_res12;
									$amt[]=$direct_sale_res12['basic_amount'];$amt_type[]='CR'; }
							      } 
			} */
		/* 	else if($ledgertype=='round')
			{ 
				if($site!='')
				{ 
			 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$_REQUEST['site']."') and round_ledger='".$ledgerid."' and round_off!='0.00'";
			 
			  $direct_sale_sql=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   roundOff As round_off,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' 
										and round_ledger='".$ledgerid."'  and branch_name in('".$_REQUEST['site']."')	",$db);

                 }
                 else
                 {

                $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			      and round_ledger='".$ledgerid."' and round_off!='0.00'";
			     
			  
			    $direct_sale_sql=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   roundOff As round_off,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' 
										and round_ledger='".$ledgerid."'",$db);


                 }


			  	//select * from invoice where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' and round_ledger='".$ledgerid."'  
			 	while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					 $direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['round_off'];
					if($direct_sale_res1['sign']=='+')
						$amt_type[]='CR';
					else
						$amt_type[]='DR';
				}
				//echo "<pre>"; print_r($amt_type);
			} */
			
			
			$ResultData65=DB_query($GetRec25,$db);
			$numrows65 = DB_num_rows($ResultData65);
			while($RowData65=DB_fetch_array($ResultData65)){
			$data_array[]=array('Pid'=>$RowData65['id'],'date'=>$RowData65['transation_date_and_time'],'flag'=>'bill','Voucher_NO'=>$RowData65['bill_book_no'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['bill_book_date']);}
			$ResultData6=DB_query($GetRec2,$db);
			$numrows6 = DB_num_rows($ResultData6);
			while($RowData6=DB_fetch_array($ResultData6)){
				$data_array[]=array('Pid'=>$RowData6['id'],'date'=>$RowData6['bill_book_date'],'flag'=>'bill','Voucher_NO'=>$RowData6['bill_book_no'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['transation_date_and_time']);}
			$ResultData61=DB_query($GetRec21,$db);
			$numrows61 = DB_num_rows($ResultData61);
			while($RowData61=DB_fetch_array($ResultData61))	{
				$data_array[]=array('Pid'=>$RowData61['id'],'date'=>$RowData61['billdate'],'flag'=>'bill','Voucher_NO'=>$RowData61['billno'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['bill_book_date']);}
			
	                
        
          
			if(count($data_array)>0)
			  { 
		  	   $flag=''; $AMTDR1='';$AMTDR=0; $AMTCR=0; 
			   foreach($data_array as $k=>$val)
			   { 
					    if($val['flag']=='voucher')	{				
				        $type_ledger=$s->GetFieldValue("account_book_details","account_bookid ='".$val['Pid']."'  and ledgerid='".$ledgerid."'","amount_type");										 
						 $ResultData2=DB_query("select A.id,A.account_bookid,A.amount as AMT,  A.ledgerid as ledgerid,B.amount,B.currency,B.amount_type FROM account_book_details A, account_book_details  B   where A.account_bookid ='".$val['Pid']."'  and B.ledgerid='".$ledgerid."' AND A.ledgerid!='".$ledgerid."' and  A.account_bookid=B.account_bookid 	and A.amount!='0.00' and A.amount!='' and A.amount_type!='".$type_ledger."' group  by B.id ",$db);									
			 			  $site=$s->getField('account_book','id',$val['Pid'],'Branch');	}	
				else if($val['flag']=='bill'){					
							$ResultData2=DB_query("select * from purchase_billbook where id ='". $val['Pid']."'",$db);
							$site=$s->getField('purchase_billbook','id',$val['Pid'],'site');}							
						
			    if(DB_num_rows($ResultData2)>0) { 				
				 while($RowData2 = DB_fetch_array($ResultData2))
			  	 { 		   
				if($val['flag']=='voucher')
				{
				 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
				}
				else
				{
					if($vendorid=='')
					{
					 $particular= $s->GetFieldValue('suppliers',"id='".$RowData2['vendor']."'",'vendor_name');
					}
					else
					{
					 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledger']."'",'AccountName');
					}
				}
			    $AMTDR=0; $AMTCR=0; 
				if($val['flag']=='voucher' ){
				if($RowData2['amount_type']=='DR')
				$AMTDR=$RowData2['amount'];}
				elseif($val['flag']=='bill')
				{
				 if($val['type']==''){ if($RowData2['total_basic_amount']!='0.00') $AMTDR=$RowData2['total_basic_amount'];}
				 else if($val['type']=='surcharge'){ if($RowData2['sur_oct_amt']!='0.00') $AMTDR=$RowData2['sur_oct_amt'];}
				 else if($val['type']=='freight') { if($RowData2['freight']!='0.00') $AMTDR=$RowData2['freight'];} 
				 else if($val['type']=='pckcharge') {	if($RowData2['pkg_charges']!='0.00') $AMTDR=$RowData2['pkg_charges'];} 
				 else if($val['type']=='addcharge') {	if($RowData2['add_charges']!='0.00') $AMTDR=$RowData2['add_charges'];}
                 else if($val['type']=='salestax') {                	
                 $vatid=$s->getField('accountledger','AccountsID',$ledgerid,'taxid');
                 $AMTDR=$s->GetFieldValue("purchase_billbook","vat_cst='".$vatid."' and id='".$RowData2['id']."'","Sum(taxvalue)");  
                 }
                 else if($val['type']=='excisetax') {                	
                 $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
			                 $AMTDR=$s->GetFieldValue("purchase_bill_details","Excise_Duty='".$vatid."' and purch_billbookid='".$RowData2['id']."'","Sum(Excise_Duty_value)"); 
                 }
                 else if($val['type']=='cess') {              	
                  $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
			               $amt1=0;
			              

			                $sql_cess=DB_query("select ecess,shecess,ecess_amt as cessamt,shecess_amt as sheamt from 
			                            purchase_billbook where  (ecess='".$vatid."' || shecess='".$vatid."') 
			                            and id='".$RowData2['id']."' ",$db);
			                while($r_cess=DB_fetch_array($sql_cess))
			                {
			                 
			                  if($vatid==$r_cess['ecess'])
			                  $amt1=$amt1+$r_cess['ecess_amt'];
			                  else  
			                  $amt1=$amt1+$r_cess['shecess_amt'];               	
			                }

			                  $AMTDR=$amt1;
	            }
				else if($val['type']=='round') {
				                  if($RowData2['round_off']!='0.00') {
				                                                      if($RowData2['round_off']>0)
				                                                       {
				                                                         $round=explode("-",$RowData2['round_off']);
				                                                         if($round[1]!='') $AMTDR=$round[1];
				                                                          else $AMTDR=$RowData2['round_off'];
				                                                         } 
				                                                          else $AMTCR=0;
				                                                      }
				
				   }
				}
				    else { $AMTDR=0;}	
				   			
				     $TDR[]= $AMTDR;				
			         $AMTCR=0;
					 if($val['flag']=='bill')
					 	{ 
					 		if($val['type']=='vendor')
					 						   {
					                             if($RowData2['GrandTotal']!='0.00')
					                             $AMTCR=$RowData2['GrandTotal'];					                         
					                           } else if($val['type']=='round')	
					                           {
						                            if($RowData2['round_off']!='0.00')
						                            {
							                            if($RowData2['round_off']>0) $AMTDR=0;
							                            else 
							                            {
							                            $round=explode("-",$RowData2['round_off']);
							                            if($round[1]!='') $AMTCR=$round[1];	else $AMTCR=$RowData2['round_off'];				
							                            }
					                                }
					                            } 
												else				
												{ $AMTCR=0;}
					
					
					  } 
					  else if($val['flag']=='voucher'){
					  if($RowData2['amount_type']=='CR')
					  $AMTCR=$RowData2['amount'];}
					  else{ $AMTCR=0;}
					  $TCR[]= $AMTCR;                  			     
			         		 
			          $trnasdata12=explode(" ",$val['vou_date']);
				      $trans_date12=$date." ".$trnasdata12[1];			         
			           $DR_amount[]=$AMTDR;
			           $CR_amount[]=$AMTCR;
			          
			   } 
			 }
			          
		  }
			
	    }

    
	  
	
				 if(!empty($direct_sale_res))
				 {      
				 	foreach($direct_sale_res as $key =>$value)
				 	{
			          
					   			 
						$VNO= $value['bill_book_no'];	    	    
				    	if($amt_type[$key]=='DR'){ $Deb_amt=$amt[$key]; $Cre_amt=0;} else{ $Cre_amt=$amt[$key]; $Deb_amt=0;} 			
			            $trnasdata121=explode(" ",$val['transdate']);
						$trans_date122=$date." ".$trnasdata121[1];			            
			            $DR_amount[]=$Deb_amt;
			            $CR_amount[]=$Cre_amt;
				   }
				}

//print_r($DR_amount);
if(empty($DR_amount)) $DRamt1=0; else $DRamt1=array_sum($DR_amount);
if(empty($CR_amount)) $CRamt1=0; else $CRamt1=array_sum($CR_amount);
if($DRamt1>$CRamt1) { $opening_bal=$DRamt1-$CRamt1; $type1='DR'; }
else if($DRamt1<$CRamt1) { $opening_bal=$CRamt1-$DRamt1; $type1='CR'; }
else{$opening_bal=0; $type1=''; }
$opening_bal_return[]=$opening_bal;
$opening_bal_return[]=$type1;
return $opening_bal_return;	
} 


function oppp_reconsilation($ledgerid,$todate_1,$fromdate_1,$site)
{
	global $db;
	global $s;	
	
	$cramt_privious='';
	  $dramt='';
	  $DR_amount='';
	  $CR_amount='';
	  $AMTDR='';
	  $AMTCR='';
        if($site!='') {

		   		
          		 $sql_check_dtls=DB_query("select B.openingtype,B.openingBalance,B.openingdate  from accountledger_dtls B  
          		 	inner join accountledger A on A.AccountsID=B.accountledger_id where A.AccountsID='".$ledgerid."'
             	    and B.openingdate between '".$todate_1."'    and '".$fromdate_1."' and B.site='".$site."' 
             	    order by B.openingdate ASC",$db);  }
               else {  
                      
           		$sql_check_dtls=DB_query("select B.openingtype,B.openingBalance,B.openingdate ,B.site from accountledger_dtls B
           		     inner join accountledger A on A.AccountsID=B.accountledger_id where A.AccountsID='".$ledgerid."' 
           		     and  B.openingdate between '".$todate_1."'    and '".$fromdate_1."' order by B.openingdate ASC ",$db); }
           while($row_dtls=DB_fetch_array($sql_check_dtls))
	        {
	        	
	        	if($row_dtls['openingtype']=='DR')
	            $dramt[]=$row_dtls['openingBalance'];
	           else if($row_dtls['openingtype']=='CR')
	           $cramt_privious[]=$row_dtls['openingBalance'];
	           $opdate=$row_dtls['openingdate'];
	                
	        }
	        if(!empty($dramt)) { $draount=array_sum($dramt); } else $draount=0;
	        if(!empty($cramt_privious)) { $craount=array_sum($cramt_privious);} else $craount=0;
	        if($draount>$craount) { $dramount1=$draount-$craount; $cramount1=0; $type='DR';}
	        if($draount<$craount) { $cramount1=$craount-$draount; $dramount1=0;$type='CR';}
	        if($dramount1!=0 || $cramount1!=0)
	        	$DR_amount[]=$dramount1;
	            $CR_amount[]=$cramount1;
         
	        
    

	 $ledgertype=$s->getField('accountledger','AccountsID',$ledgerid,'ledgertype');
        
        if($ledgertype=='scess')
        {
          $master_tax_id=$s->getField('accountledger','AccountsID',$ledgerid,'taxid');
          if($site!='')
          {
          $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,member as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transation_date_and_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B 	where  (B.Ecess_ledger=".$master_tax_id." OR B.SCess_ledger=".$master_tax_id.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and branch_name='".$site."' ",$db);
          }
          else
          {
          $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,member as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transation_date_and_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B where  (B.Ecess_ledger=".$master_tax_id." OR B.SCess_ledger=".$master_tax_id.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' 	",$db);                  
          }	

									while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
									{
										if($direct_sale_res1['cess_ID1']!='' || $direct_sale_res1['scess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res1;
										if($master_tax_id==$direct_sale_res1['cess_ID1'])
										$amt[]=$direct_sale_res1['total_pur_cess1'];
									    else
									    $amt[]=$direct_sale_res1['total_pur_cess2'];	
										$amt_type[]='CR';
									}
									}

        }
       

		
		
			if($site=="")
			{
				$con= " "; 
				$con1= " "; 
				$site_data=array();
				$sql_site=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
				while($row_site=DB_fetch_array($sql_site))
				{
				$site_data[]=$row_site['loccode'];
				}
				$site_data1=implode("','",$site_data);
				$site_branch="Branch in('".$site_data1."')";
				$site_branch1="site in('".$site_data1."')";
			}
			else
			{
				$con= " and  branch_name='".$site."' "; 
				$con1= " and  b.branch_name='".$site."' "; 
				$site_branch="Branch='".$site."'";
		        $site_brach1="site='".$site."'";
			}
			$GetRec ="SELECT A.VPrefix as Prefixtype,A.Voucher_NO ,A.id AS Pid,A.Dates as Dates, A.transation_date_and_time as trans FROM account_book A WHERE   A.Dates between  '".$todate_1."' and '".$fromdate_1."' 
			and $site_branch and A.cancel_status=0   ORDER BY A.Dates ASC ";
			$ResultData5=DB_query($GetRec,$db);
			$numrows5 = DB_num_rows($ResultData5);
			while($RowData5=DB_fetch_array($ResultData5))
			{
				$trnasdata1=explode(" ",$RowData5['trans']);

				$trans_date=$RowData5['Dates']." ".$trnasdata1[1];
				$data_array[]=array('Pid'=>$RowData5['Pid'],'date'=>$RowData5['Dates'],'flag'=>'voucher','Voucher_NO'=>$RowData5['Voucher_NO'],'Vprefix'=>$RowData5['Prefixtype'],'type'=>'','vou_date'=>$trans_date);
			}
			

			// $ledgertype;
			if($ledgertype=='' )
	        {
			$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$sitedata."') and  ledger='".$ledgerid."' and total_basic_amount!='0.00'";
           


            if($site!='')
				 {
				 	$direct_sale_sql=DB_query("SELECT  (B.discounted_amt) AS total_pur_cess1,client_id as ledger,  
												B.transaction_date_time  AS transdate ,B.invoiceDate 
												 AS bill_book_date ,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
												from invoice B 												
												where  (B.ledger_id=".$ledgerid.") and branch_name='".$site."'
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);
					
				 }
				else 
				{



					$direct_sale_sql=DB_query("SELECT  (B.discounted_amt) AS total_pur_cess1,client_id as ledger,  
												B.transaction_date_time  AS transdate ,B.invoiceDate 
												 AS bill_book_date ,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
												from invoice B 												
												where  (B.ledger_id=".$ledgerid.") 
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);

				}	
				 	

		               
             
               

				 	
					while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
					{
						$direct_sale_res[]=$direct_sale_res1;
						$amt[]=$direct_sale_res1['total_pur_cess1'];
						$amt_type[]='CR';
					}


			
			}
			else if($ledgertype=='vendor')
			{
				if($site!='')
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$sitedata."') and vendor='".$vendorid."' and GrandTotal!='0.00'";
				 else
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and  vendor='".$vendorid."' and GrandTotal!='0.00'	";
			}

         else if($ledgertype=='surcharge')
			{
				if($site!='')	
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$site."') and surcharge_ledger='".$ledgerid."' and sur_oct_amt!='0.00'";
				 else
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and surcharge_ledger='".$ledgerid."' and sur_oct_amt!='0.00'"; 	
			}
			else if($ledgertype=='freight')
			{
				if($_REQUEST['site']!='')
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and freight_ledger='".$ledgerid."' and freight!='0.00'";
				  $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."') and freight_desc=0 ".$con,$db);
				    while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}
				  $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."') and freight_desc=1 ".$con,$db); 
				
				  while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
				}
				else
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and freight_ledger='".$ledgerid."' and freight!='0.00'";	

			   $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."'  and freight_desc=1 ".$con,$db);
			      while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
			    $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."'  and freight_desc=0 ".$con,$db);
			
				 while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}
		    	}				

				
				
			}


			
			else if($ledgertype=='salestax')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			 if($site!='')	
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate,A.id as id,A.vendor,A.remarks as remarks as vendor FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$site."') and B.vat_id='".$ledgerid."' and B.vat_amt!='0.00' Group by A.id  ";
				 else
				   $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
				    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
				  and B.vat_id='".$ledgerid."' and B.vat_amt!='0.00' Group by A.id "; 

			}

			else if($ledgertype=='excisetax')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			 if($_REQUEST['site']!='')	
			 {
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate ,A.id as id,A.vendor as vendor,A.remarks as remarks  FROM     purchase_billbook A 
			      where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and A.excise='".$ledgerid."' and A.excise_amt!='0.00' 
				 Group by A.id  ";
				 $direct_sale_sql1=DB_query("SELECT  (B.exciseValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.exciseDuty=".$ledgerid.") 
														and branch_name='".$_REQUEST['site']."' AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'	",$db);
			 }	 
			 else
			 {
				   $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate ,A.id as id,A.vendor as vendor,A.remarks as remarks  FROM     purchase_billbook A 
			      where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and A.excise='".$ledgerid."' and A.excise_amt!='0.00' 
				 Group by A.id"; 
         
		 $direct_sale_sql1=DB_query("SELECT  (B.exciseValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 	  where	invoiceDate between '".$todate_1."' and '".$fromdate_1."' and B.exciseDuty='".$ledgerid."' ",$db);


		     }
		     while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql1))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='CR';
							}

			}

			else if($ledgertype=='cess')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid13=$ledgerid1['taxid'];
			 if($_REQUEST['site']!='0')	
			 {
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
		    AS billdate,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_billbook A  where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'  and site in('".$_REQUEST['site']."')
		     and (A.ecess='".$ledgerid13."' || A.shecess='".$ledgerid13."') and (A.ecess_amt!='0.00' || A.shecess_amt!='0.00')
				Group by A.id  ";

                $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
				  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
				  from invoice B 	where  (B.Ecess_ledger=".$ledgerid13." OR B.SCess_ledger=".$ledgerid13.")
				  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and branch_name='".$_REQUEST['site']."' ",$db);

				
			}
			else
			{
		    $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
		    AS billdate,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_billbook A  where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
		   and (A.ecess='".$ledgerid13."' || A.shecess='".$ledgerid13."') and (A.ecess_amt!='0.00' || A.shecess_amt!='0.00') Group by A.id "; 
		   
           $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B where  (B.Ecess_ledger=".$ledgerid13." OR B.SCess_ledger=".$ledgerid13.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' 	",$db);     

			}
		

							while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
									{
										if($direct_sale_res1['cess_ID1']!='' || $direct_sale_res1['scess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res1;
										if($master_tax_id==$direct_sale_res1['cess_ID1'])
										$amt[]=$direct_sale_res1['total_pur_cess1'];
									    else
									    $amt[]=$direct_sale_res1['total_pur_cess2'];	
										$amt_type[]='CR';
									}
								}

		}

			else if($ledgertype=='pckcharge')
			{
				 if($_REQUEST['site']!='')
				 {
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and pckg_ledger='".$ledgerid."' and pkg_charges!='0.00'";
				  $direct_sale_sql25=DB_query("SELECT B.pkg_ledger as cess_ID1, (B.pkgchargesamt) AS total_pur_cess1,client_id 
				  as ledger,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo 
				  as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks from invoice B
				  where  (B.pkg_ledger=".$ledgerid.") AND B.invoiceDate between  '".$todate_1."' and B.branch_name in('".$_REQUEST['site']."')  and '".$fromdate_1."' and B.pkgchargesamt!='0'	",$db);
				 }
				 else
				 {	
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				  and pckg_ledger='".$ledgerid."' and pkg_charges!='0.00'";	
				  $direct_sale_sql25=DB_query("SELECT B.pkg_ledger as cess_ID1, (B.pkgchargesamt) AS total_pur_cess1,client_id 
				  as ledger,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo 
				  as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks from invoice B
				  where  (B.pkg_ledger=".$ledgerid.") AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and B.pkgchargesamt!='0' 	",$db);   
				 } 
				 while($direct_sale_res25=DB_fetch_assoc($direct_sale_sql25))
									{
										if($direct_sale_res25['cess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res25;
										$amt[]=$direct_sale_res25['total_pur_cess1'];	
										$amt_type[]='CR';
									}
								}
			}

			else if($ledgertype=='client')
			{
				// "dfdf";
                $client_check=DB_fetch_assoc(DB_query("select vender_client from accountledger  where AccountsID='".$ledgerid."'",$db));
				 if($client_check['vender_client']!='')
				 {  
				 	// "ravi".$site;

						 if($site!='')
						 {

						 	$direct_sale_sql=DB_query("SELECT  (B.grandTotal) AS total_pur_cess1,ledger_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.client_id=".$client_check['vender_client'].") 
														and branch_name='".$site."' AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'	",$db);
							
						 }
						else
						{
                          

							$direct_sale_sql=DB_query("SELECT  (B.grandTotal) AS total_pur_cess1,ledger_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.client_id=".$client_check['vender_client'].") 
											AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);

						}	 	
				 	
							while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='DR';
							}
						
				}



			}
			
			else if($ledgertype=='addcharge'){
				if($site!=''){	
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$site."') and addcharge_ledger='".$ledgerid."' and handling!='0.00'";}
				else {
                $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				  addcharge_ledger='".$ledgerid."' and handling!='0.00'";}		
			}


           else if($ledgertype=='sales'){
			 if($site!=''){
			 $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$site."') and ledger='".$ledgerid."' ";			 
			  $direct_sale_sql1=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   discounted_amt As basic_amount,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."' and branch_name in('".$site."') ",$db); }                 else {
                $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			      and ledger='".$ledgerid."' and round_off!='0.00'";			 
			    $direct_sale_sql1=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   discounted_amt As basic_amount,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."' ",$db);  }          
                       while($direct_sale_res12=DB_fetch_assoc($direct_sale_sql1)){
									if($direct_sale_res12['basic_amount']!='0.00'){
									$direct_sale_res[]=$direct_sale_res12;
									$amt[]=$direct_sale_res12['basic_amount'];$amt_type[]='CR'; }
							      }
			}
			else if($ledgertype=='round'){
				if($site!=''){
			 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$site."') and round_ledger='".$ledgerid."' and round_off!='0.00'";
			 
			  $direct_sale_sql=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   round_off_value As round_off,'invoice' as table1,remarks as remarks
										from invoice B 	where invoiceDate between '".$todate_1."' and '".$fromdate_1."' 
										and ledger_id='".$ledgerid."'  and branch_name in('".$site."')	",$db); }
                 else{
                $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			      and round_ledger='".$ledgerid."' and round_off!='0.00'";			 
			    $direct_sale_sql=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   round_off_value As round_off,'invoice' as table1,remarks as remarks
										from invoice B 	where invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."'",$db);
                 } 
			 	while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql)){
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['round_total'];
								if($direct_sale_res1['sign']=='+') $amt_type[]='CR';else$amt_type[]='DR';
				       }
				
			}
			
			
			
			$ResultData65=DB_query($GetRec25,$db);
			$numrows65 = DB_num_rows($ResultData65);
			while($RowData65=DB_fetch_array($ResultData65)){
			$data_array[]=array('Pid'=>$RowData65['id'],'date'=>$RowData65['transation_date_and_time'],'flag'=>'bill','Voucher_NO'=>$RowData65['bill_book_no'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['bill_book_date']);}
			$ResultData6=DB_query($GetRec2,$db);
			$numrows6 = DB_num_rows($ResultData6);
			while($RowData6=DB_fetch_array($ResultData6)){
				$data_array[]=array('Pid'=>$RowData6['id'],'date'=>$RowData6['bill_book_date'],'flag'=>'bill','Voucher_NO'=>$RowData6['bill_book_no'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['transation_date_and_time']);}
			$ResultData61=DB_query($GetRec21,$db);
			$numrows61 = DB_num_rows($ResultData61);
			while($RowData61=DB_fetch_array($ResultData61))	{
				$data_array[]=array('Pid'=>$RowData61['id'],'date'=>$RowData61['billdate'],'flag'=>'bill','Voucher_NO'=>$RowData61['billno'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['bill_book_date']);}
			
	                
 
			if(count($data_array)>0)
			  { 
		  	   $flag=''; $AMTDR1='';$AMTDR=0; $AMTCR=0; 
			   foreach($data_array as $k=>$val)
			   { 
					    if($val['flag']=='voucher')	{				
				        $type_ledger=$s->GetFieldValue("account_book_details","account_bookid ='".$val['Pid']."'  and ledgerid='".$ledgerid."'","amount_type");										 
						 $ResultData2=DB_query("select A.id,A.account_bookid,A.amount as AMT,  A.ledgerid as ledgerid,B.amount,B.currency,B.amount_type FROM account_book_details A, account_book_details  B   where A.account_bookid ='".$val['Pid']."'  and B.ledgerid='".$ledgerid."' AND A.ledgerid!='".$ledgerid."' and  A.account_bookid=B.account_bookid 	and A.amount!='0.00' and A.amount!='' and A.amount_type!='".$type_ledger."' group  by B.id ",$db);									
			 			  $site=$s->getField('account_book','id',$val['Pid'],'Branch');	}	
				else if($val['flag']=='bill'){					
							$ResultData2=DB_query("select * from purchase_billbook where id ='". $val['Pid']."'",$db);
							$site=$s->getField('purchase_billbook','id',$val['Pid'],'site');}							
						
			    if(DB_num_rows($ResultData2)>0) { 				
				 while($RowData2 = DB_fetch_array($ResultData2))
			  	 { 		   
				if($val['flag']=='voucher')
				{
				 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
				}
				else
				{
					if($vendorid=='')
					{
					 $particular= $s->GetFieldValue('suppliers',"id='".$RowData2['vendor']."'",'vendor_name');
					}
					else
					{
					 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledger']."'",'AccountName');
					}
				}
			    $AMTDR=0; $AMTCR=0; 
				if($val['flag']=='voucher' ){
				if($RowData2['amount_type']=='DR')
				$AMTDR=$RowData2['amount'];}
				elseif($val['flag']=='bill')
				{
				 if($val['type']==''){ if($RowData2['total_basic_amount']!='0.00') $AMTDR=$RowData2['total_basic_amount'];}
				 else if($val['type']=='surcharge'){ if($RowData2['sur_oct_amt']!='0.00') $AMTDR=$RowData2['sur_oct_amt'];}
				 else if($val['type']=='freight') { if($RowData2['freight']!='0.00') $AMTDR=$RowData2['freight'];} 
				 else if($val['type']=='pckcharge') {	if($RowData2['pkg_charges']!='0.00') $AMTDR=$RowData2['pkg_charges'];} 
				 else if($val['type']=='addcharge') {	if($RowData2['add_charges']!='0.00') $AMTDR=$RowData2['add_charges'];}
                 else if($val['type']=='salestax') {                	
                 $vatid=$s->getField('accountledger','AccountsID',$ledgerid,'taxid');
                 $AMTDR=$s->GetFieldValue("purchase_bill_details","vat_id='".$vatid."' and purch_billbookid='".$RowData2['id']."'","Sum(vat_amt)");  
                 }
                 else if($val['type']=='excisetax') {                	
                  $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
			                 $AMTDR=$s->GetFieldValue("purchase_bill_details","Excise_Duty='".$vatid."' and purch_billbookid='".$RowData2['id']."'","Sum(Excise_Duty_value)");
                 }
                 else if($val['type']=='cess') {              	
               $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
			               $amt1=0;
			              

			                $sql_cess=DB_query("select ecess,shecess,ecess_amt as cessamt,shecess_amt as sheamt from 
			                            purchase_billbook where  (ecess='".$vatid."' || shecess='".$vatid."') 
			                            and id='".$RowData2['id']."' ",$db);
			                while($r_cess=DB_fetch_array($sql_cess))
			                {
			                 
			                  if($vatid==$r_cess['ecess'])
			                  $amt1=$amt1+$r_cess['ecess_amt'];
			                  else  
			                  $amt1=$amt1+$r_cess['shecess_amt'];               	
			                }

			                  $AMTDR=$amt1;
	            }
				else if($val['type']=='round') {
				                  if($RowData2['round_off']!='0.00') {
				                                                      if($RowData2['round_off']>0)
				                                                       {
				                                                         $round=explode("-",$RowData2['round_off']);
				                                                         if($round[1]!='') $AMTDR=$round[1];
				                                                          else $AMTDR=$RowData2['round_off'];
				                                                         } 
				                                                          else $AMTCR=0;
				                                                      }
				
				   }
				}
				    else { $AMTDR=0;}	
				   			
				     $TDR[]= $AMTDR;				
			         $AMTCR=0;
					 if($val['flag']=='bill')
					 	{ 
					 		if($val['type']=='vendor')
					 						   {
					                             if($RowData2['GrandTotal']!='0.00')
					                             $AMTCR=$RowData2['GrandTotal'];					                         
					                           } else if($val['type']=='round')	
					                           {
						                            if($RowData2['round_off']!='0.00')
						                            {
							                            if($RowData2['round_off']>0) $AMTDR=0;
							                            else 
							                            {
							                            $round=explode("-",$RowData2['round_off']);
							                            if($round[1]!='') $AMTCR=$round[1];	else $AMTCR=$RowData2['round_off'];				
							                            }
					                                }
					                            } 
												else				
												{ $AMTCR=0;}
					
					
					  } 
					  else if($val['flag']=='voucher'){
					  if($RowData2['amount_type']=='CR')
					  $AMTCR=$RowData2['amount'];}
					  else{ $AMTCR=0;}
					  $TCR[]= $AMTCR;                  			     
			         		 
			          $trnasdata12=explode(" ",$val['vou_date']);
				      $trans_date12=$date." ".$trnasdata12[1];			         
			           $DR_amount[]=$AMTDR;
			           $CR_amount[]=$AMTCR;
			          
			   } 
			 }
			          
		  }
			
	    }

    
	  
	
				 if(!empty($direct_sale_res))
				 {      
				 	foreach($direct_sale_res as $key =>$value)
				 	{
			          
					   			 
						$VNO= $value['bill_book_no'];	    	    
				    	if($amt_type[$key]=='DR'){ $Deb_amt=$amt[$key]; $Cre_amt=0;} else{ $Cre_amt=$amt[$key]; $Deb_amt=0;} 			
			            $trnasdata121=explode(" ",$val['transdate']);
						$trans_date122=$date." ".$trnasdata121[1];			            
			            $DR_amount[]=$Deb_amt;
			            $CR_amount[]=$Cre_amt;
				   }
				}

//	print_r($data_print_data);

if(empty($DR_amount)) $DRamt1=0; else $DRamt1=array_sum($DR_amount);
if(empty($CR_amount)) $CRamt1=0; else $CRamt1=array_sum($CR_amount);
if($DRamt1>$CRamt1) { $opening_bal=$DRamt1-$CRamt1; $type1='DR'; }
else if($DRamt1<$CRamt1) { $opening_bal=$CRamt1-$DRamt1; $type1='CR'; }
else{$opening_bal=0; $type1=''; }
$opening_bal_return[]=$opening_bal;
$opening_bal_return[]=$type1;
return $opening_bal_return;	
} 



function oppp_ledger($ledgerid,$todate_1,$fromdate_1,$site)
{
	//echo $ledgerid."--".$todate_1."--".$fromdate_1."--".$site;
	global $db;
	global $s;	
	
	$cramt_privious='';
	  $dramt='';
	  $DR_amount='';
	  $CR_amount='';
	  $AMTDR='';
	  $AMTCR='';
        if($site!='0') {

		   		
          		 $sql_check_dtls=DB_query("select B.openingtype,B.openingBalance,B.openingdate  from accountledger_dtls B  
          		 	inner join accountledger A on A.AccountsID=B.accountledger_id where A.AccountsID='".$ledgerid."'
             	    and B.openingdate between '".$todate_1."'    and '".$fromdate_1."' and B.site='".$site."' 
             	    order by B.openingdate ASC",$db);  }
               else {                         
           		$sql_check_dtls=DB_query("select B.openingtype,B.openingBalance,B.openingdate ,B.site from accountledger_dtls B
           		     inner join accountledger A on A.AccountsID=B.accountledger_id where A.AccountsID='".$ledgerid."' 
           		     and  B.openingdate between '".$todate_1."'    and '".$fromdate_1."' order by B.openingdate ASC ",$db); }

           while($row_dtls=DB_fetch_array($sql_check_dtls))
	        {
	        	
	        	if($row_dtls['openingtype']=='DR')
	            $dramt[]=$row_dtls['openingBalance'];
	           else if($row_dtls['openingtype']=='CR')
	           $cramt_privious[]=$row_dtls['openingBalance'];
	           $opdate=$row_dtls['openingdate'];
	                
	        }
	        if(!empty($dramt)) { $draount=array_sum($dramt); } else $draount=0;
	        if(!empty($cramt_privious)) { $craount=array_sum($cramt_privious);} else $craount=0;
	        if($draount>$craount) { $dramount1=$draount-$craount; $cramount1=0; $type='DR';}
	        if($draount<$craount) { $cramount1=$craount-$draount; $dramount1=0;$type='CR';}
	        if($dramount1!=0 || $cramount1!=0)
	        	$DR_amount[]=$dramount1;
	            $CR_amount[]=$cramount1;
         
	        
    

	  $ledgertype=$s->getField('accountledger','AccountsID',$ledgerid,'ledgertype');
        
       /* if($ledgertype=='scess')
        {
          $master_tax_id=$s->getField('accountledger','AccountsID',$ledgerid,'taxid');
          if($site!='0')
          {
          $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B 	where  (B.Ecess_ledger=".$master_tax_id." OR B.SCess_ledger=".$master_tax_id.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and branch_name='".$site."' ",$db);
          }
          else
          {
          $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B where  (B.Ecess_ledger=".$master_tax_id." OR B.SCess_ledger=".$master_tax_id.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' 	",$db);                  
          }	

									while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
									{
										if($direct_sale_res1['cess_ID1']!='' || $direct_sale_res1['scess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res1;
										if($master_tax_id==$direct_sale_res1['cess_ID1'])
										$amt[]=$direct_sale_res1['total_pur_cess1'];
									    else
									    $amt[]=$direct_sale_res1['total_pur_cess2'];	
										$amt_type[]='CR';
									}
									}

        }
       
  */
		
		
			if($site=="0")
			{
				$con= " "; 
				$con1= " "; 
				$site_data=array();
				$sql_site=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
				while($row_site=DB_fetch_array($sql_site))
				{
				$site_data[]=$row_site['loccode'];
				}
				$site_data1=implode("','",$site_data);
				$site_branch="Branch in('".$site_data1."')";
				$site_branch1="site in('".$site_data1."')";
			}
			else
			{
				$con= " and  branch_name='".$site."' "; 
				$con1= " and  b.branch_name='".$site."' "; 
				$site_branch="Branch='".$site."'";
		        $site_brach1="site='".$site."'";
			}
			$GetRec ="SELECT A.VPrefix as Prefixtype,A.Voucher_NO ,A.id AS Pid,A.Dates as Dates, A.transation_date_and_time as trans FROM account_book A WHERE   A.Dates between  '".$todate_1."' and '".$fromdate_1."' 
			and $site_branch and A.cancel_status=0   ORDER BY A.Dates ASC ";
			$ResultData5=DB_query($GetRec,$db);
			$numrows5 = DB_num_rows($ResultData5);
			while($RowData5=DB_fetch_array($ResultData5))
			{
				$trnasdata1=explode(" ",$RowData5['trans']);

				$trans_date=$RowData5['Dates']." ".$trnasdata1[1];
				$data_array[]=array('Pid'=>$RowData5['Pid'],'date'=>$RowData5['Dates'],'flag'=>'voucher','Voucher_NO'=>$RowData5['Voucher_NO'],'Vprefix'=>$RowData5['Prefixtype'],'type'=>'','vou_date'=>$trans_date);
			}
			


			if($ledgertype=='' )
	        {
			$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$sitedata."') and  ledger='".$ledgerid."' and total_basic_amount!='0.00'";
           


           /* if($site!='0')
				 {
				 	$direct_sale_sql=DB_query("SELECT  (B.discounted_amt) AS total_pur_cess1,client_id as ledger,  
												B.transaction_date_time  AS transdate ,B.invoiceDate 
												 AS bill_book_date ,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
												from invoice B 												
												where  (B.ledger_id=".$ledgerid.") and branch_name='".$site."'
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);
					
				 }
				else 
				{



					$direct_sale_sql=DB_query("SELECT  (B.discounted_amt) AS total_pur_cess1,client_id as ledger,  
												B.transaction_date_time  AS transdate ,B.invoiceDate 
												 AS bill_book_date ,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
												from invoice B 												
												where  (B.ledger_id=".$ledgerid.") 
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);

				}	
				 	 */

		               
             
               

				 	
				/*	while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
					{
						$direct_sale_res[]=$direct_sale_res1;
						$amt[]=$direct_sale_res1['total_pur_cess1'];
						$amt_type[]='CR';
					} */


			
			}
			else if($ledgertype=='vendor')
			{
                                $vendorid=$s->GetFieldValue("accountledger","AccountsID='".$ledgerid."' and ledgertype='vendor'",'vender_client');
				if($site!='0')
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$sitedata."') and vendor='".$vendorid."' and GrandTotal!='0.00'";
				 else
			 	 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and  vendor='".$vendorid."' and GrandTotal!='0.00'	";
			}
          else if($ledgertype=='insurance')
			{
				if($_REQUEST['site']!='0')	
				{
				
				  $direct_sale_sql=DB_query("select (B.insurance) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (insurance_ledger='".$ledgerid."'  ) and branch_name  in('".$_REQUEST['site']."')  ".$con,$db);

				}
				else
				{
				
				 $direct_sale_sql=DB_query("select (B.insurance) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (insurance_ledger='".$ledgerid."' )  ".$con,$db);	
				}
					while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
					{
						$direct_sale_res[]=$direct_sale_res1;
						$amt[]=$direct_sale_res1['total_pur_cess1'];
						$amt_type[]='DR';
					}
			}

         else if($ledgertype=='surcharge')
			{
				if($_REQUEST['site']!='0')	
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and surcharge_ledger='".$ledgerid."' and sur_oct_amt!='0.00'";
				  $direct_sale_sql=DB_query("select (B.surcharge_hrd_amt+B.surcharge_amt) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (surcharge_ledger='".$ledgerid."' || surcharge_hrd_ledger='".$ledgerid."' ) and branch_name  in('".$_REQUEST['site']."') ".$con,$db);

				}
				else
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and surcharge_ledger='".$ledgerid."' and sur_oct_amt!='0.00'"; 
				 $direct_sale_sql=DB_query("select (B.surcharge_hrd_amt+B.surcharge_amt) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (surcharge_ledger='".$ledgerid."' || surcharge_hrd_ledger='".$ledgerid."' )  ".$con,$db);	
				}
					while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
			}
			else if($ledgertype=='freight')
			{
				if($_REQUEST['site']!='0')
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and freight_ledger='".$ledgerid."' and freight!='0.00'";
				  $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."') and freight_desc=0 ".$con,$db);
				    while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}
				  $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."') and freight_desc=1 ".$con,$db); 
				
				  while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
				}
				else
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and freight_ledger='".$ledgerid."' and freight!='0.00'";	

			   $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."'  and freight_desc=1 ".$con,$db);
			      while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='DR';
				}
			    $direct_sale_sql=DB_query("select (B.freightValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and freight_ledger='".$ledgerid."'  and freight_desc=0 ".$con,$db);
			
				 while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}
		    	}				

				
				
			}


			
			else if($ledgertype=='salestax')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			    if($site!='0')
			    {	
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate,A.id as id,A.vendor,A.remarks as remarks as vendor FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$site."') and A.vat_cst='".$ledgerid."' and A.taxvalue!='0.00' Group by A.id  ";

                 $direct_sale_sql=DB_query("select (B.VatCstValue+B.vat_cst_amt_hrd) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (vatCst_ledger='".$ledgerid."' || vat_cst_hrd='".$ledgerid."') and branch_name in('".$site."')  ".$con,$db);


				}
				else
				{
				   $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
				    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
				  and A.vat_cst='".$ledgerid."' and A.taxvalue!='0.00' Group by A.id "; 
				   $direct_sale_sql=DB_query("select (B.VatCstValue+B.vat_cst_amt_hrd) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and (vatCst_ledger='".$ledgerid."' || vat_cst_hrd='".$ledgerid."')   ".$con,$db);

				 
				}
				while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['total_pur_cess1'];
					$amt_type[]='CR';
				}

			}

			else if($ledgertype=='excisetax')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			 if($_REQUEST['site']!='0')	
			 {
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate ,A.id as id,A.vendor as vendor,A.remarks as remarks  FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and B.Excise_Duty='".$ledgerid."' and B.Excise_Duty_value!='0.00' 
				 Group by A.id  ";
				 $direct_sale_sql=DB_query("SELECT  (B.exciseValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.exciseDuty=".$ledgerid.") 
														and branch_name='".$_REQUEST['site']."' AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'	",$db);
			 }	 
			 else
			 {
				   $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
				    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
				  and B.Excise_Duty='".$ledgerid."' and B.Excise_Duty_value!='0.00' Group by A.id "; 
         
		 $direct_sale_sql1=DB_query("SELECT  (B.exciseValue) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 	  where	invoiceDate between '".$todate_1."' and '".$fromdate_1."' and B.exciseDuty='".$ledgerid."' ",$db);


		     }
		     while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql1))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='CR';
							}

			}

			else if($ledgertype=='cess')
			{	
			 $ledgerid1=DB_fetch_assoc(DB_query("select taxid from accountledger  where AccountsID='".$ledgerid."'",$db));	
			 $ledgerid=$ledgerid1['taxid'];
			 if($_REQUEST['site']!='0')	
			 {
				$GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
			    AS billdate ,A.id as id,A.vendor as vendor ,A.remarks as remarks  FROM purchase_bill_details B INNER JOIN
                 purchase_billbook A ON A.id=B.purch_billbookid   where  A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and (B.cess_ID1='".$ledgerid."' || B.cess_ID2='".$ledgerid."') and (B.Edu_Cess!='0.00' || B.SHE_Cess!='0.00') Group by A.id  ";

                $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
				  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
				  from invoice B 	where  (B.Ecess_ledger=".$ledgerid." OR B.SCess_ledger=".$ledgerid.")
				  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and branch_name='".$_REQUEST['site']."' ",$db);

				
			}
			else
			{
		    $GetRec21="SELECT A.bill_book_no as billno,A.transation_date_and_time AS transdate, A.bill_book_date 
		    AS billdate,A.id as id,A.vendor as vendor ,A.remarks as remarks FROM purchase_bill_details B INNER JOIN
            purchase_billbook A ON A.id=B.purch_billbookid where A.bill_book_date  between  '".$todate_1."' and '".$fromdate_1."'
		   and (B.cess_ID1='".$ledgerid."' || B.cess_ID2='".$ledgerid."') and (B.Edu_Cess!='0.00' || B.SHE_Cess!='0.00') Group by A.id "; 
		   
           $direct_sale_sql=DB_query("SELECT B.Ecess_ledger as cess_ID1, B.SCess_ledger as scess_ID1, (B.ECessValue) AS total_pur_cess1,client_id as ledger,  
		  (B.SAHCessValue) AS total_pur_cess2 ,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
		  from invoice B where  (B.Ecess_ledger=".$ledgerid." OR B.SCess_ledger=".$ledgerid.")
		  AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' 	",$db); 		    

			}
		

						/*	while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
									{
										if($direct_sale_res1['cess_ID1']!='' || $direct_sale_res1['scess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res1;
										if($master_tax_id==$direct_sale_res1['cess_ID1'])
										$amt[]=$direct_sale_res1['total_pur_cess1'];
									    else
									    $amt[]=$direct_sale_res1['total_pur_cess2'];	
										$amt_type[]='CR';
									}
								}  */

		}



			else if($ledgertype=='pckcharge')
			{
				 if($_REQUEST['site']!='0')
				 {
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and pckg_ledger='".$ledgerid."' and pkg_charges!='0.00'";
				  $direct_sale_sql25=DB_query("SELECT B.pkg_ledger as cess_ID1, (B.pkgchargesamt) AS total_pur_cess1,client_id 
				  as ledger,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo 
				  as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks from invoice B
				  where  (B.pkg_ledger=".$ledgerid.") AND B.invoiceDate between  '".$todate_1."' and B.branch_name in('".$_REQUEST['site']."')  and '".$fromdate_1."' and B.pkgchargesamt!='0'	",$db);
				 }
				 else
				 {	
				 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				  and pckg_ledger='".$ledgerid."' and pkg_charges!='0.00'";	
				  $direct_sale_sql25=DB_query("SELECT B.pkg_ledger as cess_ID1, (B.pkgchargesamt) AS total_pur_cess1,client_id 
				  as ledger,B.transaction_date_time  as transdate , B.invoiceDate as bill_book_date,invoiceNo 
				  as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks from invoice B
				  where  (B.pkg_ledger=".$ledgerid.") AND B.invoiceDate between  '".$todate_1."'    and '".$fromdate_1."' and B.pkgchargesamt!='0' 	",$db);   
				 } 
				/* while($direct_sale_res25=DB_fetch_assoc($direct_sale_sql25))
									{
										if($direct_sale_res25['cess_ID1']!='')
										{
										$direct_sale_res[]=$direct_sale_res25;
										$amt[]=$direct_sale_res25['total_pur_cess1'];	
										$amt_type[]='CR';
									}
								} */
			}

			else if($ledgertype=='client')
			{
				// "dfdf";
                $client_check=DB_fetch_assoc(DB_query("select vender_client from accountledger  where AccountsID='".$ledgerid."'",$db));
				 if($client_check['vender_client']!='')
				 {  
				 	// "ravi".$site;

						 if($site!='0')
						 {

						 	$direct_sale_sql=DB_query("SELECT  (B.grandTotal) AS total_pur_cess1,ledger_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.client_id=".$client_check['vender_client'].") 
														and branch_name='".$site."' AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'	",$db);
							
						 }
						else
						{
                          

							$direct_sale_sql=DB_query("SELECT  (B.grandTotal) AS total_pur_cess1,ledger_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														from invoice B 												
														where  (B.client_id=".$client_check['vender_client'].") 
											AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);

						}	 	
				 	
							/* while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='DR';
							} */
						
				}



			}
			
			else if($ledgertype=='addcharge')
			{
				if($_REQUEST['site']!='0')
				{	
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."')  and addcharge_ledger='".$ledgerid."' and handling!='0.00'";
				   $direct_sale_sql=DB_query("select (B.otheramount) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and other_ledger='".$ledgerid."' and branch_name  in('".$_REQUEST['site']."')  ".$con,$db);


				}
				else
				{
                $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."' and 
				  addcharge_ledger='".$ledgerid."' and handling!='0.00'";
				    $direct_sale_sql=DB_query("select (B.otheramount) AS total_pur_cess1,client_id as ledger,  
														B.transaction_date_time  AS transdate ,B.invoiceDate AS bill_book_date,invoiceNo as bill_book_no,'invoice' as table1,invoice_id as id,remarks as remarks
														 from invoice B where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' 
			   	and other_ledger='".$ledgerid."'  ".$con,$db);


				}
				/* while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
							{
								$direct_sale_res[]=$direct_sale_res1;
								$amt[]=$direct_sale_res1['total_pur_cess1'];
								$amt_type[]='DR';
							}
           */
			}



           else if($ledgertype=='sales'){
			 if($site!='0'){
			 $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$site."') and ledger='".$ledgerid."' ";			 
			  $direct_sale_sql1=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   discounted_amt As basic_amount,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."' and branch_name in('".$site."') ",$db); }                 else {
                $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			      and ledger='".$ledgerid."' and round_off!='0.00'";			 
			    $direct_sale_sql1=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   discounted_amt As basic_amount,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' and ledger_id='".$ledgerid."' ",$db);  }          
                       /* while($direct_sale_res12=DB_fetch_assoc($direct_sale_sql1)){
									if($direct_sale_res12['basic_amount']!='0.00'){
									$direct_sale_res[]=$direct_sale_res12;
									$amt[]=$direct_sale_res12['basic_amount'];$amt_type[]='CR'; }
							      } */
			}
			else if($ledgertype=='round')
			{
				if($_REQUEST['site']!='0')
				{
			 $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$_REQUEST['site']."') and round_ledger='".$ledgerid."' and round_off!='0.00'";
			 
			  $direct_sale_sql=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   roundOff As round_off,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' 
										and round_ledger='".$ledgerid."'  and branch_name in('".$_REQUEST['site']."')	",$db);

                 }
                 else
                 {

                $GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			      and round_ledger='".$ledgerid."' and round_off!='0.00'";
			 
			    $direct_sale_sql=DB_query("SELECT  invoice_id as id,transaction_date_time  AS transdate ,invoiceDate AS bill_book_date,client_id as ledger,invoiceNo as bill_book_no,
                                   roundOff As round_off,'invoice' as table1,remarks as remarks
										from invoice B 										
										where  		invoiceDate between '".$todate_1."' and '".$fromdate_1."' 
										and round_ledger='".$ledgerid."'",$db);


                 }


			  	//select * from invoice where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' and round_ledger='".$ledgerid."'  
			 /*	while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
				{
					$direct_sale_res[]=$direct_sale_res1;
					$amt[]=$direct_sale_res1['round_off'];
					if($direct_sale_res1['sign']=='+')
						$amt_type[]='CR';
					else
						$amt_type[]='DR';
				} */
				//echo "<pre>"; print_r($amt_type);
			}
			
			
			
			$ResultData65=DB_query($GetRec25,$db);
			$numrows65 = DB_num_rows($ResultData65);
			while($RowData65=DB_fetch_array($ResultData65)){
			$data_array[]=array('Pid'=>$RowData65['id'],'date'=>$RowData65['transation_date_and_time'],'flag'=>'bill','Voucher_NO'=>$RowData65['bill_book_no'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['bill_book_date']);}
			$ResultData6=DB_query($GetRec2,$db);
			$numrows6 = DB_num_rows($ResultData6);
			while($RowData6=DB_fetch_array($ResultData6)){
				$data_array[]=array('Pid'=>$RowData6['id'],'date'=>$RowData6['bill_book_date'],'flag'=>'bill','Voucher_NO'=>$RowData6['bill_book_no'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['transation_date_and_time']);}
			$ResultData61=DB_query($GetRec21,$db);
			$numrows61 = DB_num_rows($ResultData61);
			while($RowData61=DB_fetch_array($ResultData61))	{
				$data_array[]=array('Pid'=>$RowData61['id'],'date'=>$RowData61['billdate'],'flag'=>'bill','Voucher_NO'=>$RowData61['billno'],'Vprefix'=>'Purchase','type'=>$ledgertype,'vou_date'=>$RowData65['bill_book_date']);}
			
	                
 
			if(count($data_array)>0)
			  { 
		  	   $flag=''; $AMTDR1='';$AMTDR=0; $AMTCR=0; 
			   foreach($data_array as $k=>$val)
			   { 
					    if($val['flag']=='voucher')	{				
				        $type_ledger=$s->GetFieldValue("account_book_details","account_bookid ='".$val['Pid']."'  and ledgerid='".$ledgerid."'","amount_type");										 
						 $ResultData2=DB_query("select A.id,A.account_bookid,A.amount as AMT,  A.ledgerid as ledgerid,B.amount,B.currency,B.amount_type FROM account_book_details A, account_book_details  B   where A.account_bookid ='".$val['Pid']."'  and B.ledgerid='".$ledgerid."' AND A.ledgerid!='".$ledgerid."' and  A.account_bookid=B.account_bookid 	and A.amount!='0.00' and A.amount!='' and A.amount_type!='".$type_ledger."' group  by B.id ",$db);									
			 			  $site=$s->getField('account_book','id',$val['Pid'],'Branch');	}	
				else if($val['flag']=='bill'){					
							$ResultData2=DB_query("select * from purchase_billbook where id ='". $val['Pid']."'",$db);
							$site=$s->getField('purchase_billbook','id',$val['Pid'],'site');}							
						
			    if(DB_num_rows($ResultData2)>0) { 				
				 while($RowData2 = DB_fetch_array($ResultData2))
			  	 { 		   
				if($val['flag']=='voucher')
				{
				 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledgerid']."'",'AccountName');
				}
				else
				{
					if($vendorid=='')
					{
					 $particular= $s->GetFieldValue('suppliers',"id='".$RowData2['vendor']."'",'vendor_name');
					}
					else
					{
					 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$RowData2['ledger']."'",'AccountName');
					}
				}
			    $AMTDR=0; $AMTCR=0; 
				if($val['flag']=='voucher' ){
				if($RowData2['amount_type']=='DR')
				$AMTDR=$RowData2['amount'];}
				elseif($val['flag']=='bill')
				{
				 if($val['type']==''){ if($RowData2['total_basic_amount']!='0.00') $AMTDR=$RowData2['total_basic_amount'];}
				 else if($val['type']=='surcharge'){ if($RowData2['sur_oct_amt']!='0.00') $AMTDR=$RowData2['sur_oct_amt'];}
				 else if($val['type']=='freight') { if($RowData2['freight']!='0.00') $AMTDR=$RowData2['freight'];} 
				 else if($val['type']=='pckcharge') {	if($RowData2['pkg_charges']!='0.00') $AMTDR=$RowData2['pkg_charges'];} 
				 else if($val['type']=='addcharge') {	if($RowData2['add_charges']!='0.00') $AMTDR=$RowData2['add_charges'];}
                 else if($val['type']=='salestax') {                	
                 $vatid=$s->getField('accountledger','AccountsID',$ledgerid,'taxid');
                 $AMTDR=$s->GetFieldValue("purchase_billbook","vat_cst='".$vatid."' and id='".$RowData2['id']."'","Sum(taxvalue)");  
                 }
                 else if($val['type']=='excisetax') {                	
                 $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
			                 $AMTDR=$s->GetFieldValue("purchase_bill_details","Excise_Duty='".$vatid."' and purch_billbookid='".$RowData2['id']."'","Sum(Excise_Duty_value)"); 
                 }
                 else if($val['type']=='cess') {              	
                  $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
			               $amt1=0;
			              

			                $sql_cess=DB_query("select ecess,shecess,ecess_amt as cessamt,shecess_amt as sheamt from 
			                            purchase_billbook where  (ecess='".$vatid."' || shecess='".$vatid."') 
			                            and id='".$RowData2['id']."' ",$db);
			                while($r_cess=DB_fetch_array($sql_cess))
			                {
			                 
			                  if($vatid==$r_cess['ecess'])
			                  $amt1=$amt1+$r_cess['ecess_amt'];
			                  else  
			                  $amt1=$amt1+$r_cess['shecess_amt'];               	
			                }

			                  $AMTDR=$amt1;
	            }
				else if($val['type']=='round') {
				                  if($RowData2['round_off']!='0.00') {
				                                                      if($RowData2['round_off']>0)
				                                                       {
				                                                         $round=explode("-",$RowData2['round_off']);
				                                                         if($round[1]!='') $AMTDR=$round[1];
				                                                          else $AMTDR=$RowData2['round_off'];
				                                                         } 
				                                                          else $AMTCR=0;
				                                                      }
				
				   }
				}
				    else { $AMTDR=0;}	
				   			
				     $TDR[]= $AMTDR;				
			         $AMTCR=0;
					 if($val['flag']=='bill')
					 	{ 
					 		if($val['type']=='vendor')
					 						   {
					                             if($RowData2['GrandTotal']!='0.00')
					                             $AMTCR=$RowData2['GrandTotal'];					                         
					                           } else if($val['type']=='round')	
					                           {
						                            if($RowData2['round_off']!='0.00')
						                            {
							                            if($RowData2['round_off']>0) $AMTDR=0;
							                            else 
							                            {
							                            $round=explode("-",$RowData2['round_off']);
							                            if($round[1]!='') $AMTCR=$round[1];	else $AMTCR=$RowData2['round_off'];				
							                            }
					                                }
					                            } 
												else				
												{ $AMTCR=0;}
					
					
					  } 
					  else if($val['flag']=='voucher'){
					  if($RowData2['amount_type']=='CR')
					  $AMTCR=$RowData2['amount'];}
					  else{ $AMTCR=0;}
					  $TCR[]= $AMTCR;                  			     
			         		 
			          $trnasdata12=explode(" ",$val['vou_date']);
				      $trans_date12=$date." ".$trnasdata12[1];			         
			           $DR_amount[]=$AMTDR;
			           $CR_amount[]=$AMTCR;
			          
			   } 
			 }
			          
		  }
			
	    }

    
	  
	
				 if(!empty($direct_sale_res))
				 {      
				 	foreach($direct_sale_res as $key =>$value)
				 	{
			          
					   			 
						$VNO= $value['bill_book_no'];	    	    
				    	if($amt_type[$key]=='DR'){ $Deb_amt=$amt[$key]; $Cre_amt=0;} else{ $Cre_amt=$amt[$key]; $Deb_amt=0;} 			
			            $trnasdata121=explode(" ",$val['transdate']);
						$trans_date122=$date." ".$trnasdata121[1];			            
			            $DR_amount[]=$Deb_amt;
			            $CR_amount[]=$Cre_amt;
				   }
				}

	//print_r($DR_amount);
if(empty($DR_amount)) $DRamt1=0; else $DRamt1=array_sum($DR_amount);
if(empty($CR_amount)) $CRamt1=0; else $CRamt1=array_sum($CR_amount);
if($DRamt1>$CRamt1) { $opening_bal=$DRamt1-$CRamt1; $type1='DR'; }
else if($DRamt1<$CRamt1) { $opening_bal=$CRamt1-$DRamt1; $type1='CR'; }
else{$opening_bal=0; $type1=''; }
$opening_bal_return[]=$opening_bal;
$opening_bal_return[]=$type1;
return $opening_bal_return;	
} 



?>
