<?php 
session_name('CAKEPHP'); session_start();
error_reporting(0);
$PageSecurity = 11; 
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
include('./includes/AccountGroupMaster_Class.php');
include('includes/ledger_opening.php');


//die;
setlocale(LC_MONETARY, 'en_IN');
$_SESSION['data_print_data']='';
?>
<script language="javascript" type="text/javascript">
function getLedgerName(str)
    {
	  //alert(str);
	  $('.modal22').show();
      
	   if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{

		  $('.modal22').hide();     
		  
		  //alert(xmlhttp.responseText);
		    document.getElementById("ledgerid").innerHTML=xmlhttp.responseText;
		    }
	        // var data1=document.getElementById("convcurrency").value;
		
		//var data=showcurrency(data1);
		}
	
	
	  var undergroup='';	
	//var undergroup1="/"+undergroup+"/";

	
	//alert("check_data_fin.php?section=Drledgername&site="+str+"&group="+undergroup);
	xmlhttp.open("POST","check_data_fin.php?section=Drledgernameledger&site="+str+"&group="+undergroup,true);
	xmlhttp.send();
  
     
     
      }
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $s->getField('modules_form','id',$_REQUEST['frmid'],'frm_name'); ?></h1>
<form action="" name="main_account" method="post" onsubmit="return validation()">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="13%" align="right">Branch</td>
	<td width="35%"><select name="site" id="site"  onchange="getLedgerName(this.value);" >
	  <option value="">All</option>
	  <?php 
	 	   $ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')"; 
	$site_sql=DB_query($ss,$db);
	while($site_row=DB_fetch_array($site_sql))
	{?>
	  <option value="<?php echo $site_row['loccode'];?>"<?php echo($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
	  <?php 	
	}
	?>
	</select></td>
	<td width="13%" align="right">Ledgers</td>
	<td><select name="ledgerid" id="ledgerid" >
	<?php $undergroup='';
         $AccountMaster-> AccountLeger_edit($ledger_dr,'ledger_dr[]','ledgerdrID',$undergroup,$_REQUEST['ledgerid'],$_REQUEST['site']); ?>	   
  	</select></td>
</tr>
<tr class="row1">	
  	<td align="right">From Date</td>
	<td><input name="todate"  id="todate" type="text"  class="datepicker"  value="<?php  echo $_REQUEST['todate']?>"/></td>
	<td align="right">To Date</td>
	<td><input name="fromdate" id="fromdate" type="text"  class="datepicker" value="<?php  echo $_REQUEST['fromdate']?>" onchange="return date_check();" /></td>  
</tr>
<tr class="row1"> 
  <td colspan="4" style="text-align:center;"><input name="Search" type="submit" class="search" onclick="return validate();"  value="Search"/></td>
</tr>
</table>
</form>


<?php 
if($_REQUEST['Search']!='')
{ 

  $fromdate=$_REQUEST['fromdate'];
  $todate=$_REQUEST['todate'];
  //if($todate<=$fromdate)
 // {
  	$flag=1;
  $fromdate_brk=explode('/',$fromdate);

  $fromdate_1=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0];
  $todate_brk=explode('/',$todate);
  $todate_1=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0];
  //}
  $filter=array();
  if($_REQUEST['site']!='0')
  {
	  $filter[]="Branch = '".$_REQUEST['site']."'";
	  $site_branch="Branch in('".$_REQUEST['site']."')";
	  $site_branch1="site in('".$_REQUEST['site']."')";
  }
  if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
  $filter[]="Dates BETWEEN '".$from."' and '".$to."'";

    $_SESSION['fromdate']=$_REQUEST['todate'];
    $_SESSION['todate']=$_REQUEST['fromdate'];
  
  $ledgerid=$_REQUEST['ledgerid'];
  if($ledgerid=='')
   {
  $ledgerid=0;	
   }

  $todate_privious=date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $todate_1) ) ));

   $op=oppp($ledgerid,$_SESSION['financialYrStart'],$todate_privious,$_REQUEST['site']);
      if(!empty($op) && $op[0]!='0')
            { 
            	if($op[1]=='DR'){ $dramount1=$op[0];$cramount1=''; $type=$op[1]; }
            	else if($op[1]=='CR') { $cramount1=$op[0];$dramount1='';$type=$op[1]; }  
            	else if($op[1]=='') { $dramount1='';$cramount1='';$type=''; }
            $data_opening[]=array("Date"=>$todate_1,"Particulars"=>"Opening","VNO"=>"OPN","Vtype"=>"OPN","Debit_amt"=>$dramount1,"Credit_amt"=>$cramount1,"Site"=>$site,"type"=>$type);         
            }
else  { 
         $cramt='';
	     $dramt='';
 if($_REQUEST['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='".$_REQUEST['ledgerid']."'
             	    and B.openingdate between '".$todate_1."'    and '".$fromdate_1."' and B.site='".$_REQUEST['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='".$_REQUEST['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[]=$row_dtls['openingBalance'];
	          $opdate=$row_dtls['openingdate'];	                  
	        }
	        if(!empty($dramt)) { $draount=array_sum($dramt); } else $draount=0;
	        if(!empty($cramt)) { $craount=array_sum($cramt);} else $craount=0;
	        if($draount>$craount) { $dramount1=$draount-$craount; $cramount1=0; $type='DR';}
	        if($draount<$craount) { $cramount1=$craount-$draount; $dramount1=0;$type='CR';}	  
	        if($cramount1!='' || $dramount1!='' )    
            $data_opening[]=array("Date"=>$opdate,"Particulars"=>"Opening","VNO"=>"OPN","Vtype"=>"OPN","Debit_amt"=>$dramount1,"Credit_amt"=>$cramount1,"Site"=>$site,"type"=>$type); 
      } 

	   $ledgertype=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'ledgertype');
        	
        if($ledgertype=='scess')
        {
          $master_tax_id=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
          if($_REQUEST['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='".$_REQUEST['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($_REQUEST['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='".$_REQUEST['site']."' "; 
				$con1= " and  b.branch_name='".$_REQUEST['site']."' "; 
				$site_branch="Branch='".$_REQUEST['site']."'";
		        $site_brach1="site='".$_REQUEST['site']."'";
			}
			
			$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($_REQUEST['site']!='')
				 {
				 	$direct_sale_sql=DB_query("SELECT  (B.basicAmount) 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='".$_REQUEST['site']."'
												AND B.invoiceDate between  '".$todate_1."'  and '".$fromdate_1."'",$db);
					
				 }
				else 
				{

					$direct_sale_sql=DB_query("SELECT  (B.basicAmount) 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($_REQUEST['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'	";

                              //echo $GetRec2;
			}

         else if($ledgertype=='surcharge')
			{
				if($_REQUEST['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_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_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[]='CR';
				}
			}
			 else if($ledgertype=='insurance')
			{
				if($_REQUEST['site']!='')	
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and site in('".$_REQUEST['site']."') and insurnace_ledger='".$ledgerid."' and insurance!='0.00'";
				  $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
				{
				$GetRec2="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
				 and insurance_ledger='".$ledgerid."' and insurance!='0.00'";	
				 $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[]='CR';
					}
			}
			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));	
			 $ledgerid122=$ledgerid1['taxid'];
			    if($site!='')
			    {	
				echo $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='".$ledgerid122."' 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='".$ledgerid122."' || vat_cst_hrd='".$ledgerid122."') and branch_name in('".$site."')  ".$con,$db);

				}
				else
				{
				  echo  $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='".$ledgerid122."' 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='".$ledgerid122."' || vat_cst_hrd='".$ledgerid122."')   ".$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));	
			 $ledgerid123=$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.excise='".$ledgerid123."' AND A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				     and site in('".$_REQUEST['site']."') 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.excise='".$ledgerid123."' AND A.bill_book_date between  '".$todate_1."' and '".$fromdate_1."'
				      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='".$ledgerid123."' ",$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));	
			 $ledgerid125=$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_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='".$ledgerid125."' || B.cess_ID2='".$ledgerid125."') 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=".$ledgerid125." OR B.SCess_ledger=".$ledgerid125.")
				  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='".$ledgerid125."' || B.cess_ID2='".$ledgerid125."') 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=".$ledgerid125." OR B.SCess_ledger=".$ledgerid125.")
		  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')
			{
				//echo "dfdf";
                $client_check=DB_fetch_assoc(DB_query("select vender_client from accountledger  where AccountsID='".$ledgerid."'",$db));
				 if($client_check['vender_client']!='')
				 {  
				 	//echo "ravi".$_REQUEST['site'];

						 if($_REQUEST['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='".$_REQUEST['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=='cash')
			{
				
				 	$direct_sale_sql=DB_query("select * from invoice where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' and client_id='".$ledgerid."' and client_type='l'  ".$con,$db);
					while($direct_sale_res1=DB_fetch_assoc($direct_sale_sql))
					{
						$direct_sale_res[]=$direct_sale_res1;
						$amt[]=$direct_sale_res1['grandTotal'];
						$amt_type[]='DR';
					}
				
			}*/
			/* else if($ledgertype=='addcharge')
			{
				if($_REQUEST['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.additionalCharges) 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.additionalCharges) 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[]='CR';
							}

			}


           else if($ledgertype=='sales')
			{
				if($_REQUEST['site']!='')
				{
			 $GetRec25="Select * from purchase_billbook where bill_book_date between '".$todate_1."' and '".$fromdate_1."'
			 and site in('".$_REQUEST['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,
                                   basicAmount 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('".$_REQUEST['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,
                                   basicAmount 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);                

                 }                    

			  	//select * from invoice where invoiceDate  between '".$todate_1."' and '".$fromdate_1."' and round_ledger='".$ledgerid."'  
			 	
				///echo "<pre>"; print_r($direct_sale_res);
                       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']!='')
				{
			 $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);
                 }
			  
			 	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);
			} */
			
			  //  echo "<pre>"; 
			//print_r($direct_sale_res);
			//echo $GetRec2;
			  if($_REQUEST['site']!='' )
            { 
            $GetRec ="SELECT A.VPrefix as Prefixtype,A.Voucher_NO ,A.id AS Pid, A.Dates,A.transation_date_and_time as transation_date_and_time FROM account_book A  inner join account_book_details B on A.id=B.account_bookid  WHERE Branch in('".$_REQUEST['site']."') and  A.Dates between '".$todate_1."' and '".$fromdate_1."'  and A.cancel_status=0 and B.ledgerid='".$_REQUEST['ledgerid']."' group by Voucher_NO  ORDER BY A.Dates ASC ";
            }
            else
            {
          $GetRec ="SELECT A.VPrefix as Prefixtype,A.Voucher_NO ,A.id AS Pid, A.Dates,A.transation_date_and_time as transation_date_and_time FROM account_book A  inner join account_book_details B on A.id=B.account_bookid  WHERE    A.Dates between '".$todate_1."' and '".$fromdate_1."'   and A.cancel_status=0 and B.ledgerid='".$_REQUEST['ledgerid']."' group by Voucher_NO  ORDER BY A.Dates ASC ";
            }
			$ResultData5=DB_query($GetRec,$db);
			$numrows5 = DB_num_rows($ResultData5);
			while($RowData5=DB_fetch_array($ResultData5))
			{
				$data_array[]=array('Pid'=>$RowData5['Pid'],'date'=>$RowData5['Dates'],'flag'=>'voucher','Voucher_NO'=>$RowData5['Voucher_NO'],'Vprefix'=>$RowData5['Prefixtype'],'type'=>'','vou_date'=>$RowData5['transation_date_and_time']);
			}
			
			/* $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(!empty($data_array)){
			foreach ($data_array as $key =>$value)
    		{
    			$data_arrange[$key]= $value['vou_date'];
			}
					
			array_multisort($data_arrange,SORT_ASC,$data_array);
			}
?> 

<?php
 //echo "<pre>";print_r($data_opening);
      foreach($data_opening as $key=>$value_op)
      {
      	?>
    <!-- <tr>
   
     <td align="left" valign="top">&nbsp;<?php echo $date= ($value_op['Date']); ?></td>
	 <td>&nbsp;<?php echo $particular=$value_op['Particulars']; ?></td>
	 <td>&nbsp;<?php echo $value_op['VNO']; ?></td>
	 <td>&nbsp;<?php echo $value_op['Vtype']; ?></td>
	 <td><?php echo $AMTDR= $value_op['Debit_amt']; ?></td>
	 <td><?php echo $AMTCR=$value_op['Credit_amt']; ?></td>
	 <?php if($AMTDR!=0 )  $amt_closing[]=$AMTDR;   else if($AMTCR!=0) $amt_closing1[]=$AMTCR; 
           if(!empty($amt_closing)) $dr_closing=array_sum($amt_closing); 
           if(!empty($amt_closing1)) $cr_closing=array_sum($amt_closing1); 
		   if($dr_closing>$cr_closing) {   $closing_amt=$dr_closing-$cr_closing; $type='DR';}
	      	else if($dr_closing<$cr_closing) {   $closing_amt=$cr_closing-$dr_closing; $type='CR'; }
              

	   ?>

	 <td align="left" valign="top" ><?php if(!empty($closing_amt)) echo $closing=$closing_amt; ?>&nbsp; <?php echo $type; ?></td>    	
	 
	 <td></td>
	  
     </tr> 	-->

      <?php 
$data_print_data[]=array("date"=>$date,"particular"=>$particular,"vno"=>$value_op['VNO'],"Vtype"=>$value_op['Vtype'],"Debit"=>$AMTDR,"Credit"=>$AMTCR,"closing"=>$closing,"type"=>$type,"remarks"=>'',"vouc_date"=>$date,"branch"=>$_REQUEST['site']);
  }  
 ?>
	 <?php 
	 
			if(count($data_array)>0)
			  { 
		  	   $flag='';	 			  
			   $AMTDR1='';
			   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')
				{						
							//echo "select * from purchase_billbook where id ='". $val['Pid']."'";
							$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)
			  {	
			   ?>
			  
 				<?php $AMTDR=0; $AMTCR=0; 				
				 while($RowData2 = DB_fetch_array($ResultData2))
			  	 { 				 
				
 		 $date= ($val['date']);		 
				
				if($val['flag']=='voucher')
				{ //echo 'welcome';
				$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');
					}
				}
				
				 $VNO= $val['Voucher_NO'];
			
	    	   
			    if($val['flag']=='voucher'){   $Vprefixval= $s->getField('voucher_master','VoucherType',$val['Vprefix'],'VPrefix');}
				else{ $Vprefixval= $val['Vprefix'];}   $AMTDR=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']=='insurance')
				{
				if($RowData2['insurance']!='0.00')
				 $AMTDR=$RowData2['insurance'];
				}

                else if($val['type']=='salestax')
                {
                	
                  $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['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_billbook","excise='".$vatid."' and id='".$RowData2['id']."'","Sum(excise_amt)");
                


                }
                 else if($val['type']=='cess')
                {
                	
               $vatid=$s->getField('accountledger','AccountsID',$_REQUEST['ledgerid'],'taxid');
               $amt1=0;
              

                $sql_cess=DB_query("select ecess as cess_ID1,shecess as cess_ID2, 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['cess_ID1'])
                  $amt1=$amt1+$r_cess['cessamt'];
                  else  
                  $amt1=$amt1+$r_cess['sheamt'];               	
                }

                  $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;}
				
				//echo $AMTCR=$RowData2['GrandTotal'];
				} 
				else if($val['flag']=='voucher'){
				if($RowData2['amount_type']=='CR')
				 $AMTCR=$RowData2['amount'];}
				else
				{ $AMTCR=0;}
				$TCR[]= $AMTCR;
				


                     if($AMTDR!=0 && $AMTCR=='0')  $amt_closing[]=$AMTDR; 
				      else if($AMTCR!=0 && $AMTDR=='0') $amt_closing1[]=$AMTCR; 
				      if(!empty($amt_closing)) $dr_closing=array_sum($amt_closing); else $dr_closing=0;
				      if(!empty($amt_closing1)) $cr_closing=array_sum($amt_closing1); else $cr_closing=0;
				      if($dr_closing>$cr_closing) {   $closing_amt=$dr_closing-$cr_closing; $type='DR';}
				      	else if($dr_closing<$cr_closing) {   $closing_amt=$cr_closing-$dr_closing; $type='CR';}
                                       else if($dr_closing==$cr_closing) { $closing_amt=0; $type='';}
				      if(!empty($closing_amt))  $closing=$closing_amt; else  $closing= $closing_amt;  ?> &nbsp; <?php  $type;    	    
			
				 if($val['flag']!='voucher') { 
			   $remarks=$s->getField('purchase_billbook','id',$val['Pid'],'remarks');  } else {
			   	 $remarks= $s->getField('account_book','id',$val['Pid'],'narration');   } 
			
			      $trnasdata12=explode(" ",$val['vou_date']);

				$trans_date12=$date." ".$trnasdata12[1];
			  $data_print_data[]=array("date"=>$date,"particular"=>$particular,"vno"=>$VNO,"Vtype"=>$Vprefixval,"Debit"=>$AMTDR,"Credit"=>$AMTCR,"closing"=>$closing,"type"=>$type,"remarks"=>$remarks,'vou_date'=>$trans_date12,"branch"=>$_REQUEST['site']);
			  //print_r($data_print_data);
			   } 
		}
			   
			   }
			
	  }

   
	
	 if(!empty($direct_sale_res))
	 {  
     
    
	 	foreach($direct_sale_res as $key =>$value)
	 	{ 
          $date= ($value['bill_book_date']);

			
			if($ledgertype=='client' || $ledgertype=='cash' )
					{
						 $particular= $s->GetFieldValue('accountledger',"AccountsID='".$value['ledger']."'",'AccountName'); 
					}
					 else 
					 	{
					 if($value['client_type']=='l')
					 	$particular= $s->GetFieldValue('accountledger',"AccountsID='".$value['client_id']."'",'AccountName').'welcome'; 
						else
							
					 	$particular= $s->GetFieldValue('clientmaster',"id='".$value['ledger']."'",'c_name').'hiii';  }   			 
				
	    	  $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;} 
				 $AMTDR=$Deb_amt;
				  $AMTCR=$Cre_amt;				
				if($AMTDR!=0)  $amt_closing[]=$AMTDR; 
				      else if($AMTCR!=0) $amt_closing1[]=$AMTCR; 
  
                          if(!empty($amt_closing)) $dr_closing=array_sum($amt_closing); else $dr_closing=0;
				      if(!empty($amt_closing1)) $cr_closing=array_sum($amt_closing1); else $cr_closing=0;
				      if($dr_closing>$cr_closing) {   $closing_amt=$dr_closing-$cr_closing; $type='DR';}
				      	else if($dr_closing<$cr_closing) {   $closing_amt=$cr_closing-$dr_closing; $type='CR';}
                                       else if($dr_closing==$cr_closing) { $closing_amt=0; $type=''; }


	   if(!empty($closing_amt))  $closing=$closing_amt; else  $closing= $closing_amt; ?> &nbsp; <?php  $type;  
			   $remarks= $value['remarks']; 
			
				
			   
	
              $trnasdata121=explode(" ",$val['transdate']);

				$trans_date122=$date." ".$trnasdata121[1];
 $data_print_data[]=array("date"=>$date,"particular"=>$particular,"vno"=>$VNO,"Vtype"=>'Sale',"Debit"=>$AMTDR,"Credit"=>$AMTCR,"closing"=>$closing,"type"=>$type,"remarks"=>$remarks,'vou_date'=>$trans_date122,"branch"=>$_REQUEST['site']);
	 } }

//print_r($data_print_data);



if(!empty($data_print_data)){

 foreach ($data_print_data as $key => $part) {
       $sort[$key] = strtotime($part['vou_date']);
  }
}
  array_multisort($sort, SORT_ASC, $data_print_data);


		
	$_SESSION['data_print_data'] = $data_print_data;

} ?>
<p align="right">
		<?php 
	if($_SESSION['data_print_data']!='')
	{ ?>
	<!-- <a href="ex.php?lid=<?php //echo $_REQUEST['ledgerid']; ?>" target="_blank" class="search">PDF</a> -->
	<?php } 
	else { ?>

		<a href="#" class="search" onclick="callalert()">Export</a>
	<?php }
?>
	<a href="ledger_print_data.php?lid=<?php echo $_REQUEST['ledgerid']; ?>" target="_blank" class="search">Print</a></p>
<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
<tr>    
	<th width="7%" style="text-align:center;">Date</th>
	<th>Particulars</th>
	<th width="11%">V. No.</th>
	<th width="11%">V. Type</th>
	<th width="10%" style="text-align:center;">Debit Amount</th>
	<th width="10%" style="text-align:center;">Credit Amount</th>
	<th width="10%" style="text-align:center;">Closing Amount</th>
	<th width="14%">Remarks</th>	
</tr>


<?php 

 if(isset($data_print_data))
  {


//print_r($_SESSION['data_print_data']);

  foreach($data_print_data as $key=>$value)
  {		
?>			
   <tr>
    <td><?php echo $date1= ConvertSQLDate($value['date']);?></td>   
    <td><?php echo $particular1= $value['particular']; ?></td>
	<td><?php echo $VNO1= $value['vno']; ?></td>
	<td><?php echo $Vtype1= $value['Vtype'];?></td>
    <td style="text-align:right;"><?php echo $debit12= money_format('%!i',$value['Debit']);$dr_amt[]=$value['Debit'];$mon_deb=$value['Debit']; ?></td>  
   	<td style="text-align:right;"><?php echo $credit12=money_format('%!i',$value['Credit']);$cr_amt[]=$value['Credit']; $money_ceb=$value['Credit']; ?></td>
    <td style="text-align:right;"><?php if(array_sum($dr_amt)>array_sum($cr_amt)) { $closing2=array_sum($dr_amt)-array_sum($cr_amt); $type1="DR";} else if(array_sum($dr_amt)<array_sum($cr_amt)) { $closing2=array_sum($cr_amt)-array_sum($dr_amt); $type1="CR";  } 
else { $closing2=0; $type1=''; }
echo $closing1= money_format('%!i',$closing2); ?> &nbsp;<?php echo $type1; ?></td>  
    <td><?php echo  $remarks1=$value['remarks']; ?></td>
  </tr>  
  <?php
$data_print_data2[]=array("date"=>$date1,"particular"=>$particular1,"vno"=>$VNO1,"Vtype"=>$Vtype1,"Debit"=>$debit12,"Credit"=>$credit12,"closing"=>$closing1,"type"=>$type1,"remarks"=>$remarks1,"money_db"=>$mon_deb,"money_cr"=>$money_ceb,"money_clos"=>$closing2);
   }

$_SESSION['print_ledger']=$data_print_data2;


   } ?>
  <tr>
    <td style="text-align:right">Closing</strong></td>
     <?php //if(!isset($_REQUEST['b'])){ ?>
    <td><strong></strong></td>
	 <td><strong></strong></td>
	 <td><strong> </strong></td>
    <td style="text-align:right"><strong><?php if($dr_amt!='') echo money_format('%!i',array_sum($dr_amt));?></strong></td>
    <td style="text-align:right"><strong><?php if($cr_amt!='')echo money_format('%!i',array_sum($cr_amt));?></strong></td>
    <td style="text-align:right"><strong><?php if(!empty($closing)) echo money_format('%!i',$closing);?>&nbsp;<?php echo $type; ?><strong></td>
    <td>&nbsp;</td>
  </tr>
</table>

</div>
</div>

<script type="text/javascript">

function callalert()
{
	alert("Please Search the details you want to export");
}


function validate()
{
	//alert(document.getElementById("ledgerid").value);
	if(document.getElementById("ledgerid").value=='0')
	{
		alert("Please Select Ledger");
		return false;
	}
	if(document.getElementById("todate").value=='')
	{
		alert("Please Select From Date");
		return false;
	}
	if(document.getElementById("fromdate").value=='')
	{
		alert("Please Select Last Date");
		return false;
	}
	

}
function date_check()
{
	/* var fromdate = document.getElementById("fromdate").value;
	var todate = document.getElementById("todate").value;
		if(fromdate < todate)
	   {
		alert("Please Select Valid Date");
         return false;
		
	   } */
}

//$("#fromdate").datepicker({ maxDate: new Date, minDate: new Date(2007, 12, 6) });
$("#fromdate").datepicker({dateFormat: 'dd/mm/yy', minDate: new Date(2007, 6, 12) });
$("#todate").datepicker({dateFormat: 'dd/mm/yy',minDate: new Date(2007, 6, 12) });


</script>
