<?php 
include('./includes/Class_Branch.php');
include('./includes/AccountGroupMaster_Class.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$VPrefix=$_REQUEST['VPrefix'];?>
<input type="hidden" name="Vprefixtype" id="Vprefixtype" value="<?php echo $VPrefix; ?>" />
<?php 
$ChkVoucher_Numtype=($s->getField('voucher_master','VoucherType',$VPrefix,'voucher_num')==1?'Auto':'Manual');
$BillBookType=$s->getField('voucher_master','VoucherType',$_REQUEST['VPrefix'],'VPrefix');
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
if($_REQUEST['Debit_note'] =='Update')
$rurl="&modid=".$modid."&frmid=".$frmid."&action=".$action."&id=".$_REQUEST['id'].'&VPrefix='.$VPrefix;
else
$rurl="&modid=".$modid."&frmid=".$frmid."&VPrefix=".$VPrefix;
$targetpage="index.php?route=Account/ACBillbookPayment".$rurl;
$msgErr=array();;
if($_REQUEST['action'] )
 $rurl.="&action=".$_REQUEST['action'];
 //	 echo $BillBookType;
	 switch($BillBookType)
	 {
	   	case 'CP':
	             $LDR='';
				 $LCR=array('cash' );
				 $CRbillstatus=1;
				 $DRbillstatus=0;
				 break;
		case 'CR':
			     $CRbillstatus=0;
				 $DRbillstatus=1;
	             $LDR=array('cash');
				 $LCR='';
				 break;
   		case 'BP':
   			     $CRbillstatus=1;
				 $DRbillstatus=0;
	             $LDR='';
				 $LCR=array('bank','duty and taxes','TDS payable');
				 break;
   		case 'BR':
	             $LDR=array('bank');
				 $LCR='';
				 $CRbillstatus=0;
				 $DRbillstatus=1;
				 break;		
 		case 'JV':
	             $LDR='';
				 $LCR='';  
				 $CRbillstatus=0;
				 $DRbillstatus=0;
				 break;
 		case 'CON':
	            $LDR=array('cash','bank'); 
				$LCR=array('cash','bank'); 
				$CRbillstatus=0;
				$DRbillstatus=0;
				break;

 		case 'CDN':
	            $LDR=array('client','vender');;
				$LCR='';
				$CRbillstatus=0;
				$DRbillstatus=0;
				break;
 		case 'DBN':
	            $LDR=array('cash','bank');;
			 	$DRledgertype=array('client','vender');
     			//$DRledgertype="'".implode("','",$ledgertype1)."'";
			    $CRledgertype='';
				$LCR='';;
				$CRbillstatus=0;
				$DRbillstatus=0;
				break;
		case 'VatTax':
	       		$LDR=array('TAX'); 
				$LCR=array('cash','bank');
				$CRbillstatus=0;
				$DRbillstatus=0;
				break;
  		default: 				
       			$LDR='';
				$LCR='';
				$CRbillstatus=0;
				$DRbillstatus=0;
		break; 				 				 		 
	 }
	function CategoryShow($pcode, $PcateID, $dashVal )
	{
	    global $db;
		  static $i=0;;
		 
 	       $sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."'  ";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
  				  $arr1[]=$rowCate['GroupId'];
 				   CategoryShow($pcode,$rowCate['GroupId'], $dashVal  );
 			}
 		}
	// print_r($arr1);
			return($arr1);
	}
	 ?>
<script src="js/function-ajax.js" language="javascript"></script>
<script language="javascript">
function retCNP(i,str)
{
x=document.getElementById(i).value;
if(x==000)
{
 alert('Please Select Ledger');
   return false;
}else
	
	window.open("ACBillBookDetails.php?cnp="+x+'&VPrefix='+str,'', 'height=500,width=550');
 }
 // FOR CR (To get Data From Invoice)
 function retCNP1(i,str)
{
x=document.getElementById(i).value;
if(x==000)
{
 alert('Please Select Ledger');
   return false;
}else
	
	window.open("ACInvoiceDetails.php?cnp="+x+'&VPrefix='+str,'', 'height=500,width=550');
 }
</script>
<?php 
if($_REQUEST['AddDRLedger'])
{
		for($i=0;$i<count($_REQUEST['ledger_dr']);$i++)
		{
			if($_REQUEST['ledger_dr'][$i]!='' && $_REQUEST['debit_amount'][$i]!='' && $_REQUEST['dr_currency'][$i]!='')
			{
				$_SESSION['ACGLedgerId'][]=$_REQUEST['ledger_dr'][$i];
				$_SESSION['Amount'][]=$_REQUEST['debit_amount'][$i];
				$_SESSION['Currency'][]=$_REQUEST['dr_currency'][$i];
				$_SESSION['Amount_type'][]='DR';
			}
		}
		
if($_REQUEST['action']=='Edit')
$s->pageLocation("index.php?route=Account/ACBillbookPayment".$rurl.'&VPrefix='.$_REQUEST['VPrefix'].'&action='.$_REQUEST['action'].'&id='.$_REQUEST['id']);  
else	 
$s->pageLocation("index.php?route=Account/ACBillbookPayment".$rurl.'&VPrefix='.$_REQUEST['VPrefix'].'&action='.$_REQUEST['action']); 
}

if($_REQUEST['AddCRLedger'])
{
 	$j=0;
    if(count($_SESSION['ACGLedgerId'])>0)
  	$j=count($_SESSION['ACGLedgerId']);
 	for($i=$j+1,$k=0;$k< count($_REQUEST['ledger_cr']);$i++,$k++)
	{
 		$_SESSION['ACGLedgerId'][]=$_REQUEST['ledger_cr'][$k];
		$_SESSION['Amount'][]=$_REQUEST['credit_amount'][$k];
		$_SESSION['Amount_type'][]='CR';
		$_SESSION['Currency'][]=$_REQUEST['cr_currency'][$k];
	}	
		 
if($_REQUEST['action']=='Edit')
$s->pageLocation("index.php?route=Account/ACBillbookPayment".$rurl.'&VPrefix='.$_REQUEST['VPrefix'].'&action='.$_REQUEST['action'].'&id='.$_REQUEST['id']);  
else
$s->pageLocation("index.php?route=Account/ACBillbookPayment".$rurl.'&VPrefix='.$_REQUEST['VPrefix'].'&action='.$_REQUEST['action']);  
	 
}

if($_REQUEST['daction']=='del')
{
	$i=$_REQUEST['sid'];
	unset($_SESSION['ACGLedgerId'][$i]);
	unset($_SESSION['Amount'][$i]);
	unset($_SESSION['Currency'][$i]);
	unset($_SESSION['Amount_type'][$i]);
	
  	 $s->pageLocation("index.php?route=Account/ACBillbookPayment".$rurl.'&VPrefix='.$_REQUEST['VPrefix'].'&action='.$_REQUEST['action'].'&amount='.$_REQUEST['amount'].'&cnp='.$_REQUEST['cnp'].'&id='.$_REQUEST['id']); 
}

if($_REQUEST['daction']=='datadel')
{
   
   if($BillBookType=='CP' ||$BillBookType=='BP' )	 
	{
	  $invoice_billbookid=$s->GetFieldValue('account_invoice_billbook',"account_book_details='".$_REQUEST['sid']."'" ,'invoice_billbookid');
  	  $getinvoide_pendingamt=$s->GetFieldValue('purchase_billbook',"id='".$invoice_billbookid."'" ,'pending_amount');
	$datainv['pending_amount']=$getinvoide_pendingamt + $s->GetFieldValue('account_invoice_billbook',"account_book_details='".$_REQUEST['sid']."'" ,'adjustment');
	$datainv['created_date']=date('Y-m-d');
	$cond="id='".$invoice_billbookid."'";
	$s->RecordEdit('purchase_billbook', $datainv, $cond);
 	} 
	else if($BillBookType!='CP' ||$BillBookType!='BP' )	 
	{
	$invoice_billbookid=$s->GetFieldValue('account_invoice_billbook',"account_book_details='".$_REQUEST['sid']."'" ,'invoice_billbookid');
	$getinvoide_pendingamt=$s->GetFieldValue('invoice',"invoice_id='".$invoice_billbookid."'" ,'pending_amount');
	$datainv['pending_amount']=$getinvoide_pendingamt + $s->GetFieldValue('account_invoice_billbook',"account_book_details='".$_REQUEST['sid']."'" ,'adjustment');
	$datainv['created_date']=date('Y-m-d');
	$cond="invoice_id='".$invoice_billbookid."'";
	$s->RecordEdit('invoice', $datainv, $cond);
	}

	$condition="id='".$_REQUEST['sid']."'";
 	$s->DeleteRecord('account_invoice_billbook',  "account_book_details='".$_REQUEST['sid']."'");
 	$s->DeleteRecord('account_book_details',  $condition);
    $s->pageLocation('index.php?route=Account/ACBillbookPayment'.$rurl.'&action=Edit&VPrefix='.$_REQUEST['VPrefix'].'&id='.$_REQUEST['id']); 
   
}
 
if($_REQUEST['Debit_note'])
{
   	if($BillBookType=='CR'||$BillBookType=='BR'||$BillBookType=='JV')
	$chktype='CR';
	if($BillBookType=='CP'||$BillBookType='BP'||$BillBookType=='JV')
  	$chktype='DR';
} 
/***********************************Insert The Record Start Here Modified By Amit 7sep-2012******************/
if($_REQUEST['Debit_note'] =='Save') //
{	


   $sqlcheck=DB_query("select * from account_book where Voucher_NO='".$_REQUEST['voucher_no']."' and VPrefix='".$_REQUEST['VPrefix']."'",$db);
  $numdata=DB_num_rows($sqlcheck);	
// if($numdata>0)
 // {
  	?>
 <!-- <script> alert("Duplicate Voucher No");</script>-->
  	
  <?php //}
//else {
//if($_REQUEST['VPrefix']=='8'){

	$voucher_id=DB_query("select Voucher_NO as ID from account_book where Vprefix='".$_REQUEST['VPrefix']."' and Branch='".$_REQUEST['Branch']."'  order by id desc limit 0,1",$db);
	 if(DB_num_rows($voucher_id)>0)
	 {
	 	$row=DB_fetch_array($voucher_id);
		$voucher_no=$row['ID'];
		
		$XEP=explode("-",$voucher_no);
		
		$new=$XEP[2]+1;
	 }
	 else
	 {
	 	$new='1';
	 }

$location=$s->getField('locations','loccode', $_REQUEST['Branch'],'prefix');
$location1=$s->getField('voucher_master','VoucherType',$_GET['VPrefix'],'VPrefix');
$string=$location.'-'.$location1.'-';
$final_string=$string.str_pad($new,5,"0",STR_PAD_LEFT);
//}
////else
//{
//echo "sdfskl";
$ConAS="id='".$_REQUEST['receiptID']."' AND ledger_id='".$_REQUEST['ledgertID']."'";
$datachk['curr_check_no']=$_REQUEST['banks_checkno'];
$s->RecordEdit('receiptbankcheckissue', $datachk, $ConAS);

if($_REQUEST['checkLimit']==$_REQUEST['banks_checkno'])
{
	$ConAS23="id='".$_REQUEST['receiptID']."' AND ledger_id='".$_REQUEST['ledgertID']."'";
	$Data23['status']='1';
	$s->RecordEdit('receiptbankcheckissue', $Data23, $ConAS23);
}


	//$final_string=$_REQUEST['voucher_no'];
	
	//}
 	$dataArray['Branch']= $_REQUEST['Branch'] ;
	$dataArray['VPrefix']= $_REQUEST['VPrefix'] ;
	$dataArray['Voucher_NO']= $final_string ;
	$dataArray['document_type']= $_REQUEST['document_type'] ;
	 
	$dataArray['banks_checkno']= $_REQUEST['banks_checkno'] ;
	$dataArray['bankckeck_issueId']=$_REQUEST['receiptID'];
	$dataArray['banks_checkdate']=FormatDateForSQL($_REQUEST['banks_checkdate']);
	$dataArray['banks_receivedfrom']= $_REQUEST['banks_receivedfrom'] ;
 	$dataArray['favouring_name']=$_REQUEST['favouring_name'];
	$dataArray['cross_inst']= $_REQUEST['cross_inst'] ;
	 
	$dataArray['Dates']= FormatDateForSQL($_REQUEST['Dates']) ;	
	$dataArray['Ref_no']=  $_REQUEST['Ref_no'] ;	
	$dataArray['total_cramount']= $_REQUEST['total_cramount'] ;	
	$dataArray['total_dramount']= $_REQUEST['total_dramount'] ;
	$dataArray['narration']=trim( $_REQUEST['narration'] );
	$dataArray['posted_date']= date('Y-m-d') ;
	$dataArray['AccountBookType']= 'voucher' ;
	
    $dataArray['deposit_vouchertype']= $_REQUEST['deposit_vouchertype'] ;	
	$dataArray['dcrno']= $_REQUEST['dcrno'] ;
	$dataArray['deposit_voucherdate']=trim(FormatDateForSQL($_REQUEST['deposit_voucherdate']) );
	$dataArray['depositedby']= trim($_REQUEST['depositedby']);

	
	//$dataArray['CompanyId']= trim($_SESSION['ACGCompanyId']);
	
 	 $s->RecordInsert('account_book', $dataArray);
	 $getlastid=DB_Last_Insert_ID($db,'account_book', 'id');
	
	 for($i=0;$i<count($_SESSION['ACGLedgerId']);$i++)
		{
			if($_SESSION['ACGLedgerId'][$i]!='' && $_SESSION['Amount'][$i]!='' && $_SESSION['Currency'][$i]!='')
			{
				$data['account_bookid']=$getlastid;
				$data['ledgerid']=$_SESSION['ACGLedgerId'][$i];
				$data['amount']=$_SESSION['Amount'][$i];
				$data['currency']=$_SESSION['Currency'][$i];
				$data['amount_type']=$_SESSION['Amount_type'][$i];
				$s->RecordInsert('account_book_details', $data);
				$lst=DB_Last_Insert_ID($db,'account_book_details', 'id');
					if($_SESSION['Amount_type'][$i]==$chktype)
					{
					  	     $update_account_book_details=$lst;
					}
			  }
		}
 
if($_SESSION['Sess_AInvBillBook']!='')
{
 // echo "update account_Invoice_billbook set status=1,account_book_details='".$update_account_book_details."'	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	 ,pending_amount=grand_total-adjustment  where id='".$_SESSION['Sess_AInvBillBook']."' ";
 	$rx=DB_query("update account_invoice_billbook set status=1,account_book_details='".$update_account_book_details."'	,
	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."' ,pending_amount=grand_total-adjustment 
	where id='".$_SESSION['Sess_AInvBillBook']."' ",$db);
	$sqlquery=DB_query("select * from account_invoice_billbook where id='".$_SESSION['Sess_AInvBillBook']."' and  status=1",$db);
	$rdata=DB_fetch_array($sqlquery);
	$invoice_billbookid=$rdata['invoice_billbookid'];
	$adjustment=$rdata['adjustment'];
  	$pending_amount=$rdata['pending_amount'];
	
	if($_SESSION['Sess_Billtype']=='INV')
	{
		$adjustment=$_REQUEST['adjust'];
		$datainv['pending_amount']=$pending_amount;
		$datainv['created_date']=date('y-m-d');
		$cond="invoice_id='".$invoice_billbookid."'";
		$s->RecordEdit('invoice', $datainv, $cond);
	}		
	if($_SESSION['Sess_Billtype']=='PO')
	{
	  	$adjustment=$_REQUEST['adjust'];
		$datainv['pending_amount']=$pending_amount;
		$datainv['created_date']=date('y-m-d');
		$cond="id='".$invoice_billbookid."'";
		$s->RecordEdit('purchase_billbook', $datainv, $cond);
	}
} 
//}
	    unset($_SESSION['Sess_AInvBillBook']);
		unset($_SESSION['ACGLedgerId']);
	    unset($_SESSION['Amount']);
	    unset($_SESSION['Currency']);
	    unset($_SESSION['Amount_type']);
	 	$s->pageLocation($targetpage.'&daction=view'); 
}
//***********************************Insert Ends Here*******************************************/ 
//***********************************Update Starts Here*****************************************/
//$v2=array();
  if($_REQUEST['Debit_note'] =='Update')
  {
  if($_REQUEST['hd_voucher_no']==$_REQUEST['voucher_no'])
  {
 //	$dataArray['CompanyId']= trim($_SESSION['ACGCompanyId']);
 	$dataArray['Branch']= $_REQUEST['Branch'] ;
	$dataArray['document_type']= $_REQUEST['document_type'] ;
	$dataArray['VPrefix']= $_REQUEST['VPrefix'] ;
	$dataArray['Voucher_NO']= $_REQUEST['voucher_no'] ;
	$dataArray['Dates']= ConvertSQLDate($_REQUEST['Dates']) ;	
	$dataArray['Ref_no']=$_REQUEST['Ref_no'];	
	
	$dataArray['transation_type']= $_REQUEST['transation_type'] ;
	$dataArray['banks_checkno']= $_REQUEST['banks_checkno2'] ;
	$dataArray['banks_checkdate']=FormatDateForSQL($_REQUEST['banks_checkdate2']);
	$dataArray['banks_receivedfrom']= $_REQUEST['banks_receivedfrom'] ;
	$dataArray['AccountBookType']= 'voucher' ;
	$dataArray['favouring_name']=$_REQUEST['favouring_name'];
	$dataArray['cross_inst']= $_REQUEST['cross_inst'] ;

	$dataArray['total_cramount']= $_REQUEST['total_cramount'] ;	
	$dataArray['total_dramount']= $_REQUEST['total_dramount'] ;
	$dataArray['narration']= trim($_REQUEST['narration']) ;
	$dataArray['modified_date']= date('Y-m-d') ;
	$dataArray['deposit_vouchertype']= $_REQUEST['deposit_vouchertype'] ;	
	$dataArray['dcrno']= $_REQUEST['dcrno'] ;
	$dataArray['deposit_voucherdate']=trim(FormatDateForSQL($_REQUEST['deposit_voucherdate']) );
	$dataArray['depositedby']= trim( $_REQUEST['depositedby'] );

 	$cond="id='".$_REQUEST['id']."'";
	$s->RecordEdit('account_book', $dataArray,$cond);
	
	for($i=0;$i<count($_SESSION['ACGLedgerId']);$i++)
		{
			if($_SESSION['ACGLedgerId'][$i]!='' && $_SESSION['Amount'][$i]!='' && $_SESSION['Currency'][$i]!='')
			{
				$data['account_bookid']=$_REQUEST['id'];
				$data['ledgerid']=trim( $_SESSION['ACGLedgerId'][$i]);
				$data['amount']=trim($_SESSION['Amount'][$i]);
				$data['currency']=trim($_SESSION['Currency'][$i]);
				$data['amount_type']=trim($_SESSION['Amount_type'][$i]);				 
				$s->RecordInsert('account_book_details', $data);
				$lst=DB_Last_Insert_ID($db,'account_book_details', 'id');
					if($_SESSION['Amount_type'][$i]==$chktype)
					{
					    $update_account_book_details=$lst;
					}
			  }
		}
		 
 
	for($i=0;$i<count($_REQUEST['address_book']);$i++)
		{
			 if($_REQUEST['ledgerid'][$i]!='' && $_REQUEST['amount'][$i]!='' && $_REQUEST['currency'][$i]!='')
			 {
			//	$data['account_bookid']=$_REQUEST['address_book'][$i];
				$data['ledgerid']=$_REQUEST['ledgerid'][$i];
				$data['amount']=$_REQUEST['amount'][$i];
				$data['currency']=$_REQUEST['currency'][$i];
				$data['amount_type']=$_REQUEST['amount_type'][$i];
				/*
				if($_REQUEST['amount_type'][$i]==$chktype)
				{
				     $update_account_book_details=$_REQUEST['address_book'][$i];
				}*/
				$cond="id='".$_REQUEST['address_book'][$i]."'"; 
				$s->RecordEdit('account_book_details', $data, $cond);
				   //echo "update    account_Invoice_billbook set status=1 ,	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where account_book_details='".$_REQUEST['address_book'][$i]."' ";
  	 $rx=DB_query("update    account_invoice_billbook set status=1 ,	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where account_book_details='".$_REQUEST['address_book'][$i]."' ",$db);

			  }
		}		
		
		
		
		echo "<h3>---</h3>";
		echo $update_account_book_details;
		echo "<br>---------------</br>";
if($update_account_book_details!='')
{
  echo "update    account_invoice_billbook set status=1,account_book_details='".$update_account_book_details."',	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where id='".$_SESSION['Sess_AInvBillBook']."' ";
  	 $rx=DB_query("update    account_invoice_billbook set status=1,account_book_details='".$update_account_book_details."',	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where id='".$_SESSION['Sess_AInvBillBook']."' ",$db);
 	
 
	  if($_SESSION['Sess_Billtype']=='INV')
	  {
	  	$sqlquery=DB_query("select * from account_invoice_billbook where account_book_details='".$update_account_book_details."' and  status=1",$db);
	    $rdata=DB_fetch_array($sqlquery);
	    $invoice_billbookid=$rdata['invoice_billbookid'];
	    $adjustment=$rdata['adjustment'];
  	    $pending_amount=$rdata['pending_amount'];
 	  
		$adjustment=$_REQUEST['adjust'];
		$datainv['pending_amount']=$pending_amount;
		$datainv['created_date']=date('y-m-d');
		$cond="invoice_id='".$invoice_billbookid."'";
		$s->RecordEdit('invoice', $datainv, $cond);
	 }	
	 
	   if($_SESSION['Sess_Billtype']=='PO')
	   {
	  echo   "select * from account_invoice_billbook where account_book_details='".$update_account_book_details."' and  status=1";
	  	$sqlquery=DB_query("select * from account_invoice_billbook where account_book_details='".$update_account_book_details."' and  status=1",$db);
	  $rdata=DB_fetch_array($sqlquery);
  	   $invoice_billbookid=$rdata['invoice_billbookid'];
	  $adjustment=$rdata['adjustment'];
  	  $pending_amount=$rdata['pending_amount'];	  
	  $adjustment=$_REQUEST['adjust'];
	  $datainv['pending_amount']=$pending_amount;
		$datainv['created_date']=date('y-m-d');
		$cond="id='".$invoice_billbookid."'";
		$s->RecordEdit('purchase_billbook', $datainv, $cond);
	  
	   }
	  	
} 
		unset($_SESSION['ACGLedgerId']);
	    unset($_SESSION['Amount']);
	    unset($_SESSION['Currency']);
	    unset($_SESSION['Amount_type']);
			
  $s->pageLocation($targetpage.'&daction=view'); 
  
  	
  }
  
  else{
  //echo "select * from account_book where Voucher_NO='".$_REQUEST['voucher_no']."'";
   $sqlcheck=DB_query("select * from account_book where Voucher_NO='".$_REQUEST['voucher_no']."'",$db);
 if(DB_num_rows($sqlcheck)>0)
  {
  //echo "heloo";
  	?>
  <script> alert("Duplicate Voucher No");</script>
  	
  <?php }
  else {
 //	$dataArray['CompanyId']= trim($_SESSION['ACGCompanyId']);
 	$dataArray['Branch']= $_REQUEST['Branch'] ;
	$dataArray['document_type']= $_REQUEST['document_type'] ;
	$dataArray['VPrefix']= $_REQUEST['VPrefix'] ;
	$dataArray['Voucher_NO']= $_REQUEST['voucher_no'] ;
	$dataArray['Dates']= ConvertSQLDate($_REQUEST['Dates']) ;	
	$dataArray['Ref_no']=$_REQUEST['Ref_no'];	
	
 
	$dataArray['transation_type']= $_REQUEST['transation_type'] ;
	$dataArray['banks_checkno']= $_REQUEST['banks_checkno2'] ;
	$dataArray['banks_checkdate']=FormatDateForSQL($_REQUEST['banks_checkdate2']);
	$dataArray['banks_receivedfrom']= $_REQUEST['banks_receivedfrom'] ;
	$dataArray['AccountBookType']= 'voucher' ;
	$dataArray['favouring_name']=$_REQUEST['favouring_name'];
	$dataArray['cross_inst']= $_REQUEST['cross_inst'] ;


	$dataArray['total_cramount']= $_REQUEST['total_cramount'] ;	
	$dataArray['total_dramount']= $_REQUEST['total_dramount'] ;
	$dataArray['narration']= trim($_REQUEST['narration']) ;
	$dataArray['modified_date']= date('Y-m-d') ;
		$dataArray['deposit_vouchertype']= $_REQUEST['deposit_vouchertype'] ;	
	$dataArray['dcrno']= $_REQUEST['dcrno'] ;
	$dataArray['deposit_voucherdate']=trim(FormatDateForSQL($_REQUEST['deposit_voucherdate']) );
	$dataArray['depositedby']= trim( $_REQUEST['depositedby'] );
   

 	$cond="id='".$_REQUEST['id']."'";
	$s->RecordEdit('account_book', $dataArray,$cond);
	//$getlastid=DB_Last_Insert_ID($db,'account_book', 'id');
	
	for($i=0;$i<count($_SESSION['ACGLedgerId']);$i++)
		{
			if($_SESSION['ACGLedgerId'][$i]!='' && $_SESSION['Amount'][$i]!='' && $_SESSION['Currency'][$i]!='')
			{
				$data['account_bookid']=$_REQUEST['id'];
				$data['ledgerid']=trim( $_SESSION['ACGLedgerId'][$i]);
				$data['amount']=trim($_SESSION['Amount'][$i]);
				$data['currency']=trim($_SESSION['Currency'][$i]);
				$data['amount_type']=trim($_SESSION['Amount_type'][$i]);				 
				$s->RecordInsert('account_book_details', $data);
				$lst=DB_Last_Insert_ID($db,'account_book_details', 'id');
					if($_SESSION['Amount_type'][$i]==$chktype)
					{

					    	    $update_account_book_details=$lst;
						 
					}
			  }
		}
			 
		 
 
	for($i=0;$i<count($_REQUEST['address_book']);$i++)
		{
		 
			 if($_REQUEST['ledgerid'][$i]!='' && $_REQUEST['amount'][$i]!='' && $_REQUEST['currency'][$i]!='')
			{
			//	$data['account_bookid']=$_REQUEST['address_book'][$i];
				$data['ledgerid']=$_REQUEST['ledgerid'][$i];
				$data['amount']=$_REQUEST['amount'][$i];
				$data['currency']=$_REQUEST['currency'][$i];
				  $data['amount_type']=$_REQUEST['amount_type'][$i];
				
				/*
				if($_REQUEST['amount_type'][$i]==$chktype)
				{
				     $update_account_book_details=$_REQUEST['address_book'][$i];
				}*/
					
				$cond="id='".$_REQUEST['address_book'][$i]."'"; 
				$s->RecordEdit('account_book_details', $data, $cond);
				 
				   //echo "update    account_Invoice_billbook set status=1 ,	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where account_book_details='".$_REQUEST['address_book'][$i]."' ";
				   
  	 $rx=DB_query("update    account_invoice_billbook set status=1 ,	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where account_book_details='".$_REQUEST['address_book'][$i]."' ",$db);

			  }
		}		
		
		
		
		echo "<h3>---</h3>";
		echo $update_account_book_details;
		echo "<br>---------------</br>";
if(	$update_account_book_details!='')
{
  echo "update    account_invoice_billbook set status=1,account_book_details='".$update_account_book_details."',	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where id='".$_SESSION['Sess_AInvBillBook']."' ";
  	 $rx=DB_query("update    account_invoice_billbook set status=1,account_book_details='".$update_account_book_details."',	voucherType='". $BillBookType."',Vprefix='".$_REQUEST['VPrefix']."'	,pending_amount=grand_total-adjustment where id='".$_SESSION['Sess_AInvBillBook']."' ",$db);
 	
 
	  if($_SESSION['Sess_Billtype']=='INV')
	  {
	  	$sqlquery=DB_query("select * from account_invoice_billbook where account_book_details='".$update_account_book_details."' and  status=1",$db);
	  $rdata=DB_fetch_array($sqlquery);
	   $invoice_billbookid=$rdata['invoice_billbookid'];
	  $adjustment=$rdata['adjustment'];
  	  $pending_amount=$rdata['pending_amount'];
 	  
		$adjustment=$_REQUEST['adjust'];
		$datainv['pending_amount']=$pending_amount;
		$datainv['created_date']=date('y-m-d');
		$cond="invoice_id='".$invoice_billbookid."'";
		$s->RecordEdit('invoice', $datainv, $cond);
	 }	
	 
	   if($_SESSION['Sess_Billtype']=='PO')
	  {
	  echo   "select * from account_invoice_billbook where account_book_details='".$update_account_book_details."' and  status=1";
	  	$sqlquery=DB_query("select * from account_invoice_billbook where account_book_details='".$update_account_book_details."' and  status=1",$db);
	  $rdata=DB_fetch_array($sqlquery);
  	   $invoice_billbookid=$rdata['invoice_billbookid'];
	  $adjustment=$rdata['adjustment'];
  	  $pending_amount=$rdata['pending_amount'];	  
	  $adjustment=$_REQUEST['adjust'];
	  $datainv['pending_amount']=$pending_amount;
		$datainv['created_date']=date('y-m-d');
		$cond="id='".$invoice_billbookid."'";
		$s->RecordEdit('purchase_billbook', $datainv, $cond);
	  
	  }
	  	
} 
		unset($_SESSION['ACGLedgerId']);
	    unset($_SESSION['Amount']);
	    unset($_SESSION['Currency']);
	    unset($_SESSION['Amount_type']);
			
  $s->pageLocation($targetpage.'&daction=view');
  } 
  }
   }
   
   
   
//***********************************Update The Record End Here*****************************************/   





//*****************************************************************************************  
 
 	$Branch=trim($_SESSION['AcgBranch']) ;
 	$voucher_no= trim($_SESSION['Acgvoucher_no']) ;
 	$Dates=(isset($_SESSION['AcgDates'])?ConvertSQLDate($_SESSION['AcgDates']):'') ;	
 	$Ref_no=trim($_SESSION['AcgRef_no'])  ;	
  	$narration= trim($_SESSION['Acgnarration']) ;
	$document_type=trim($_REQUEST['AcgDocument']) ;
	
	/*
  	$Branch= $_REQUEST['Branch'] ;
	$voucher_no= $_REQUEST['voucher_no'] ;
	$Dates= ConvertSQLDate($_REQUEST['Dates']) ;	
	$Ref_no= ConvertSQLDate($_REQUEST['Ref_no']) ;	
	$total_cramount= $_REQUEST['total_cramount'] ;	
	$total_dramount= $_REQUEST['total_dramount'] ;
	$narration= $_REQUEST['narration'] ;
	*/
  
  ?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $fromtitle=  $s->getField('voucher_master','VoucherType',$_REQUEST['VPrefix'],'GroupName');?></h1>
<div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A>
<? if($daction=='view')
{

 	
	unset($_SESSION['Acgvoucher_no']);
	unset($_SESSION['AcgBranch']);
	unset($_SESSION['ACGLedgerId']);
	unset($_SESSION['Amount']);
	unset($_SESSION['Amount_type']);
	unset($_SESSION['Currency']);
	unset($_SESSION['AcgDates']);
	unset($_SESSION['AcgRef_no']);
	unset($_SESSION['Acgnarration']);
	unset($_SESSION['AcgDocument']);
			

?>
<p align="left">
<a href="<?php echo $targetpage;?>&action=addnew&VPrefix=<?php echo $VPrefix;?>"><input name="Add New" type="button" value="Add New" class="search" /></a>
</p>
<form method="post" action="">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
    <td class="tabledata1"><strong>Type of info</strong></td>
    <td><select name="Type_of_info" style="width:150px;">
		<option value="0">Select</option>
		<option value="voucher"<?php echo($_REQUEST['Type_of_info']=='voucher'?'selected':'')?>>voucher</option>
	</select>
	</td>
    <td class="tabledata1"><strong>Having</strong></td>
    <td><select name="having" style="width:150px;">
		<option value="0">Select</option>
		<option value="voucherno" <?php echo($_REQUEST['having']=='voucherno'?'selected':'')?>>Voucher no</option>
		<option value="vouchertype"<?php echo($_REQUEST['having']=='vouchertype'?'selected':'')?>>Voucher type</option>
		<option value="narration"<?php echo($_REQUEST['having']=='narration'?'selected':'')?>>Narration</option>
		<option value="instrumentno"<?php echo($_REQUEST['having']=='instrumentno'?'selected':'')?>>Instrument no</option>
		<!--<option value="searchledger"<?php echo($_REQUEST['having']=='ledger'?'selected':'')?>>Ledger</option>-->
		<!--<option value="amount"<?php echo($_REQUEST['having']=='amount'?'selected':'')?>>Amount</option>-->
	</select>
	</td>
</tr>
<tr class="row1">
    <td class="tabledata1"><strong>Range of info</strong></td>
    <td><select name="range_of_info" style="width:150px;">
		<option  value="0">Select</option>
		<option value="containing"<?php echo($_REQUEST['range_of_info']=='containing'?'selected':'')?>>Containing</option>
		<option value="equalto"<?php echo($_REQUEST['range_of_info']=='equalto'?'selected':'')?>>Equal to</option>
		<option value="startingwith"<?php echo($_REQUEST['range_of_info']=='startingwith'?'selected':'')?>>starting with</option>
	</select>
	</td>
    <td class="tabledata1"><strong>Text</strong></td>
    <td><input type="text" value="<?php echo $_REQUEST['searchtext']; ?>" name="searchtext" style="width:150px;"/></td>
</tr>
<tr class="row1">
	 <td class="tabledata1"><strong>From Date</strong></td>
    <td><input type="text" name="from" id="from" value="<?php echo $_REQUEST['from']; ?>" style="width:150px;" class="datepicker"  /></td>
	 <td class="tabledata1"><strong>To Date</strong></td>
    <td><input type="text" name="to" id="to" value="<?php echo $_REQUEST['to']; ?>" style="width:150px;" class="datepicker"  /></td>
</tr>
<tr class="row1">
	<td class="tabledata1" colspan="4"><center>
	<center><input type="submit" class="search" name="filter" value="Go" /></center></td>
</tr>
</table>
</form>
<br />
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata">VoucherId</td>
	<td class="tabledata">Branch</td>
	<td class="tabledata">Date</td>
	<td class="tabledata">Ledger</td>
	<td class="tabledata">Debit Amount</td>
	<td class="tabledata">Narration</td>
	<td class="tabledata">View </td>
	<td class="tabledata">Edit</td>
</tr>
  <?php  
if($_REQUEST['filter']){
$Typeofinfo=$_REQUEST['Type_of_info'];
	  	$having=$_REQUEST['having'];
		$range=$_REQUEST['range_of_info'];
		$searchtext=$_REQUEST['searchtext'];
		$filter=array();$filter2='';
 		switch($having)
		{
		  case 'voucherno':
		  					if($range=='equalto')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.Voucher_NO ='".$searchtext."' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";
							}
							else
							{
							$filter=" A.Voucher_NO ='".$searchtext."'";
							}
							if($range=='containing')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.Voucher_NO  like'%".$searchtext."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";		
							}
							else
							{
							$filter=" A.Voucher_NO  like'%".$searchtext."%'";		
							}			
							if($range=='startingwith')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter="  A.Voucher_NO  like'".$searchtext."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";	
							}
							else
							{
							$filter=" A.Voucher_NO  like'".$searchtext."%'";	
							}						
							
		  					break;
		  case 'vouchertype':
		  
					   $Vprefixval1= $s->getField('voucher_master','VPrefix',$searchtext,'VoucherType');
		 					 if($range=='equalto')
							 if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.VPrefix ='".$Vprefixval1."' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";
							}
							else
							{
							$filter=" A.VPrefix ='".$Vprefixval1."'";
							}
							if($range=='containing')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.VPrefix  like'%".$Vprefixval1."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";		
							}
							else
							{
							$filter=" A.VPrefix  like'%".$Vprefixval1."%'";		
							}			
							if($range=='startingwith')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.VPrefix  like'".$Vprefixval1."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";	
							}
							else
							{
							$filter=" A.VPrefix  like'".$Vprefixval1."%'";	
							}
		  
		  					break;
		  case 'narration':
		  					 if($range=='equalto')
							 if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.narration ='".$searchtext."' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";
							}
							else
							{
							$filter=" A.narration ='".$searchtext."'";
							}
							if($range=='containing')
							 if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.narration  like'%".$searchtext."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";
							}
							else
							{
							$filter=" A.narration  like'%".$searchtext."%'";
							}					
							if($range=='startingwith')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter="A.narration  like'".$searchtext."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";	
							}
							else
							{
							$filter="A.narration  like'".$searchtext."%'";	
							}
							break;	
																				
		  case 'instrumentno':
		  
		  					if($range=='equalto')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.banks_checkno ='".$searchtext."' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";
							}
							else
							{
							$filter=" A.banks_checkno ='".$searchtext."'";
							}
							if($range=='containing')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.banks_checkno  like'%".$searchtext."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";
							}
							else
							{
							$filter=" A.banks_checkno  like'%".$searchtext."%'";
							}					
							if($range=='startingwith')
							if($_REQUEST['from']!='' && $_REQUEST['to']!=''){
		  					$filter=" A.banks_checkno  like'".$searchtext."%' and A.Dates between '".FormatDateForSQL($_REQUEST['from'])."' and '".FormatDateForSQL($_REQUEST['to'])."'";	
							}
							else
							{
							$filter=" A.banks_checkno  like'".$searchtext."%'";	
							}
							
							break;
		 /* case 'searchledger':
							 if($range=='equalto')
							 {
		  					 	$daa=$s->GetFieldValue('accountledger',"AccountName = '".$searchtext."'" ,'AccountsID');
							$filter=" 1=1";	
							
							}
							if($range=='containing'){
						 	$daa=$s->GetFieldValue('accountledger',"AccountName like  '%".$searchtext."%'" ,'AccountsID');
 		  					//$ledgerid=$daa;	
							$filter=" 1=1";		
							}
							if($range=='startingwith'){
		  					$filter=" 1=1";	
		  					}
		  
		  				break;*/
	/*	  case 'amount':
		  
		  				 if($range=='equalto')
		  					$filter=" A.Voucher_NO ='".$searchtext."'";
							if($range=='containing')
		  					$filter=" A.Voucher_NO  like'%".$searchtext."%'";					
							if($range=='startingwith')
		  					$filter=" 1=1";	
						break;*/
		   																										
		}
		 	$WithoutFSQL="select A.id as Pid,B.* ,A.* from account_book A,account_book_details B where A.VPrefix='".$VPrefix."'  and A.AccountBookType='voucher'  and  A.id=B.account_bookid and  $filter  group by A.id";

}
else{
	$WithoutFSQL="select A.id as Pid,B.* ,A.* from account_book A,account_book_details B where A.VPrefix='".$VPrefix."'  and A.AccountBookType='voucher'  and  A.id=B.account_bookid   group by A.id";
	}
    $ResultData=DB_query($WithoutFSQL,$db);
    while($RowData = DB_fetch_array($ResultData))
   {  if($j++%2==0)
     $bgcolor="#CCCCCC";
	 else{
	 $bgcolor="#FFFFFF";
	 }
	 ?>
   <!--<tr bgcolor="<?php echo $bgcolor;?>">-->
<tr class="row1">
	<td><?php echo $RowData['Voucher_NO'] ?></td>
	<td><?php echo $RowData['Branch'] ?></td>
	
	<td><?php echo ConvertSQLDate($RowData['Dates']); ?></td>
		<td><?php echo    $s->getField('accountledger','AccountsID',$RowData['ledgerid'],'AccountName'); ?></td>
	<td><?php echo number_format($RowData['total_cramount']); ?></td>
		<td><?php echo $RowData['narration'] ?></td>
    <!--<td><a href="#" onclick='popup("address_book.php?id=<?php  echo $RowData['Pid'].$rurl?>&VPrefix=<?php echo $VPrefix;?>",600,600)'>View</a></td>-->
	<td><a href="address_book.php?id=<?php  echo $RowData['Pid'].$rurl?>" target="_blank">View</a></td>
	<td><a href="<?php echo $targetpage;?>&action=Edit&id=<?php  echo $RowData['Pid'];?>" >Edit</a></td>
  </tr>
  <?php } ?>
</table>

<?php }

//  EDIT SECTION
else if($_REQUEST['action']=='Edit')
{

$query="select *  from account_book  where id='".$_REQUEST['id']."'  and AccountBookType='voucher'";
   	$result=DB_query($query,$db);
			$myrow=DB_fetch_array($result);
 			$voucher_no=$myrow['Voucher_NO'];
			$Dates=ConvertSQLDate($myrow['Dates']);
			$Ref_no= $myrow['Ref_no'] ;
			$voucher_no=$myrow['Voucher_NO'];
			$narration=trim($myrow['narration']);
			$Branch=$myrow['Branch'];
			$document_type=$myrow['document_type'];
			$transation_type=$myrow['transation_type'];
		 	$banks_checkno=$myrow['banks_checkno'];
			$bankckeck_issueId=$myrow['bankckeck_issueId'];
			$banks_checkdate=ConvertSQLDate($myrow['banks_checkdate']);
			$banks_receivedfrom=$myrow['banks_receivedfrom'];
			

			if($BillBookType=='CON'){
			$favouring_name=$_SESSION['CompanyRecord']['CompanyName'];
			$cross_inst='A/C PAYEE';
			}
			else			{
			$favouring_name=$myrow['favouring_name'];
			$cross_inst=$myrow['cross_inst'];
			}
			
 			$deposit_vouchertype=$myrow['deposit_vouchertype'];
			$dcrno=$myrow['dcrno'];
			$deposit_voucherdate=ConvertSQLDate($myrow['deposit_voucherdate']);
			$depositedby=$myrow['depositedby'];
			
	
if($BillBookType=='CR')  
    $credit_amount= $_REQUEST['amount']; 
 else {
 $dr_amount= $_REQUEST['amount']; ;
$debit_amount =$_REQUEST['amount']; ;
}
   $ledger_dr=$_REQUEST['cnp'];	
 ?>
<form action="" method="post">


<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <?php if(count($msgErr)>0){?>
   <tr>
   <td colspan="4"><?php
   for($i=0;$i<count($msgErr);$i++)
   {
     echo "<div class='error'>".$msgErr[$i].'</div>';
   }
    ?></td>
   </tr>
   <?php } ?>
<tr class="row1">
    <td class="tabledata1">Branch</td>
    <td><?php echo $ClassBranch->Branch($Branch,$VPrefix);?> </td>
    <td class="tabledata1">Voucher No.</td>
    <td width="27%"><input name="voucher_no" type="text"  value="<?php echo $voucher_no;?>" style="width:150px;" />
	<input name="hd_voucher_no" type="hidden"  value="<?php echo $voucher_no;?>" style="width:150px;" />
	</td>
</tr>
<tr class="row1">
   <td class="tabledata1">Date</td>
    <td><input name="Dates"  id="Dates" type="text" class="datepicker"  value="<?php echo $Dates;?>"   onblur="chkdate(this.value)" style="width:150px;" /></td>
   <td class="tabledata1">Ref. No.</td>
    <td><input name="Ref_no" type="text" value="<?php echo $Ref_no;?>"   style="width:150px;"/></td>
</tr>
<?php  if($BillBookType=='JV'){?>
<tr class="row1">
    <td class="tabledata1">&nbsp;</td>
    <td class="tabledata1">&nbsp;</td>
    <td class="tabledata1">Document</td>
    <td><select name="document_type" style="width:150px;">
	<option value="None" selected="selected">None</option>
	<?php $arr_doc=array('Vat Voucher','TDS Voucher','Service Tax Voucher');
	for($kl=0;$kl<count( $arr_doc);$kl++){
	$p='';
	if($arr_doc[$kl]==$document_type)
	$p='selected';
	echo "<option value='".$arr_doc[$kl]."' $p>".$arr_doc[$kl]."</option>";
	}
	 ?></select>
	 </td>
</tr>
	<?php }?>
<tr class="row1">
    <td class="tabledata1" colspan="4">
	<table width="100%" border="0" cellspacing="0" cellpadding="3">
  	<tr class="row1">
    <td class="tabledata1" nowrap="nowrap">Dr. Ledger Name</td>
    <td class="tabledata1">Debit(INR)</td>
    <td class="tabledata1">Curr.</td>
	<td class="tabledata1">&nbsp;</td>
  	</tr>
	<tr class="row1">
    <td>
	<?php if($LDR==''){?>
		<div style="float:left;">
		<input type="text" name="ledgername" id="ledgername" style="width:72px;" onkeyup="getledgernameDR(this.value)" />
		</div>
		<?php }?>
		<div id="searchledgerId" style="float:right">
	<?php 
		$arr1=array();$arr2=array();
		 //print_r($LDR);
		 
		//  print_r($LCR);
		if($LDR!=''){
		$LRX=implode("','",$LDR);
		
	    $sqlCate = "SELECT * FROM accountgroupmaster WHERE GroupName REGEXP '^$LRX'";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr1[]=$rowCate['GroupId'];
  				//$arr1[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		$rx=array();
		for($l=0;$l<count($arr1);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr1[$l]))
		 $rx[]=implode(',',$arr1[$l]);
		 else
		  $rx[]=$arr1[$l];
			 
		}
		$undergroup= implode(',',$rx );
		}else{
		$undergroup='';	
//**************************************************************

		$arr3=array();$arr4=array();
		if($LCR!=''){
		$LCX=implode("','",$LCR);
 	    $sqlCate = "select * from  accountgroupmaster  where    MainGroupId   = 0  and GroupName in('$LCX')  ";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr3[]=$rowCate['GroupId'];
  				$arr3[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		$rx=array();
		for($l=0;$l<count($arr3);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr3[$l]))
		 $rx[]=implode(',',$arr3[$l]);
		 else
		  $rx[]=$arr3[$l];
			 
		}
		$undergroup1= implode(',',$rx );
		}
//******************************************************		*
		
		
		}
		//print_r($DRledgertype);print_r($LDR);
 	   $AccountMaster-> AccountLeger($ledger_dr,'ledger_dr[]','ledger_drID',$undergroup,$undergroup1);?>
	
			 </div>
			 
	<input type="hidden" name="undergroup1" id="undergroup1" value="<?php echo $undergroup1; ?>"	 />
	<input type="hidden" name="undergroup" id="undergroup" value="<?php echo $undergroup; ?>"	 />
	 	    </td>
    <td><input name="debit_amount[]" id="debit_amount" type="text" style="width:150px;" value="<?php echo $debit_amount;?>" onkeyup="calculateTotalCrDr()" /></td> <!--onkeyup="adddebitamountedit(this.value)" -->
    <td>
	   <select name="dr_currency[]" id="dr_currency" style="width:150px;" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
       </select>
	  </td>
    <td nowrap="nowrap"><input type="button" class="search" name="AddDRLedger" onclick="adddebitlistedit()" value="Add" /> 
	<?php 
	//echo "----".$BillBookType;
	
	if($BillBookType!='JV' &&  $BillBookType!='CR' && $BillBookType!='BR' && $BillBookType!='CON' && $BillBookType!='CDN'){?>
 	 <a href="#"  onclick="retCNP('ledger_drID',<?php echo $VPrefix; ?>)"><input type="button" class="search" value="Bill wise Adj"/></a>
	 <?php } ?>
	</td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Cr. Ledger Name</td>
    <td class="tabledata1">Credit(INR)</td>
    <td class="tabledata1">Curr.</td>
  </tr>
  <tr class="row1">
  <td>
  <?php if($LCR==''){?>
		<div style="float:left;">
		<input type="text" name="ledgername" id="ledgername" style="width:72px;" onkeyup="getledgernameCR(this.value)" />
		</div>
		<?php }?>
		<div id="searchledgerCr" style="float:right;">
	<?php 	
	 	$arr3=array();$arr4=array();
		if($LCR!=''){
		$LCX=implode("|",$LCR);
	   $sqlCate = "select * from  accountgroupmaster  where     GroupName REGEXP '^$LCX'    ";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr1[]=$rowCate['GroupId'];
  				//$arr1[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		$rx=array();
		for($l=0;$l<count($arr1);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr1[$l]))
		 $rx[]=implode(',',$arr1[$l]);
		 else
		  $rx[]=$arr1[$l];
			 
		}
		$rx=array_unique($rx);
		$undergroup1= implode(',',$rx );
	 	}else
		{
		$undergroup1='';
 	//	$CRledgertype=	"'".implode("','",$LDR)."'";
		}
		 
	if(isset($_REQUEST['cnp']))	
	{ $AccountMaster-> AccountLeger($_REQUEST['cnp'],'ledger_cr[]','ledger_crID',$undergroup1);}
 	else $AccountMaster-> AccountLeger($ledger_cr,'ledger_cr[]','ledger_crID',$undergroup1);
	 	   ?> 
			</div>
			<input type="hidden" name="undergroup1CR" id="undergroup1CR" value="<?php echo $undergroup1; ?>"	 />
	 	    </td>
  <td><input name="credit_amount[]" id="credit_amount" style="width:150px;" type="text" value="<?php echo $credit_amount;?>" onkeyup="calculateTotalCrDr()" /></td>
  <td><select name="cr_currency[]" id="cr_currency" style="width:150px;" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
      </select></td>
  <td><input type="button" name="AddCRLedger" onclick="addcreditlistedit()" class="search" value="Add" />
	<?php if(($BillBookType=='CR' || $BillBookType=='BR' )&& $BillBookType!='CON' ){?>
 	 <a href="#"  onclick="retCNP1('ledger_crID',<?php echo $VPrefix; ?>)"><input type="button" class="search" value="Bill wise Adj"/></a>
	 <?php } ?>
	
	 </td>
  </tr>
  <tr class="row1">
  	<td colspan="8">
	<table width="100%" border="0" cellspacing="0" cellpadding="3">
	<tr class="row1">
	<td class="tabledata"> Ledger Name</td>
	<td class="tabledata">Amount</td>
	<td class="tabledata">Credit</td>
	<td class="tabledata">Debit</td>
	<td class="tabledata">Currency</td>
	<td class="tabledata">&nbsp;</td>
	</tr>
	<?php 
 	$total_cr=array();$total_dr=array();
	$query1="select *  from account_book_details where account_bookid ='".$myrow['id']."'  ";
	$result1=DB_query($query1,$db);
	$kk=0;
	while($myrow1=DB_fetch_array($result1)){?>
  	<tr class="row1">
  	<td><?php  //echo $i;?> 
		<input name="address_book[]" type="hidden" value="<?php  echo $myrow1['id'];?>" />
		<?php $AccountMaster-> AccountLeger1($myrow1['ledgerid'],'ledgerid[]','ledgerid','');?>
		 </td>
  	<td> 
		<input name="amount[]" type="text" style="width:150px;"  value="<?php  echo  $myrow1['amount'];?>" id="amount_<?php echo $kk;?>" onkeyup="calculateTotalCrDr()"/>
		</td>
  	<td> <?php
		echo ($myrow1['amount_type']=='CR'?$myrow1['amount']:"0");
		$total_cr[]=($myrow1['amount_type']=='CR'?$myrow1['amount']:"0");
		
		?>
		<input name="amount_type[]" type="hidden"  value="<?php  echo  $myrow1['amount_type'];?>" id="amount_type_<?php echo $kk;?>" />
		</td>
  	<td> <?php  echo  ($myrow1['amount_type']=='DR'?  $myrow1['amount']:"0");
		 $total_dr[]=($myrow1['amount_type']=='DR'?  $myrow1['amount']:"0");
		 ?></td>
  	<td><input name="currency[]" style="width:150px;" type="text"  value="<?php  echo $myrow1['currency'];?>"/></td>
	<td><a href="<?php echo $targetpage;?>&action=Edit&daction=datadel&sid=<?php  echo $myrow1['id'];?>&id=<?php echo $_REQUEST['id'];?>&VPrefix=<?php echo $VPrefix;?>" onclick="return confirm('Are you sure? It cannot be undone')"><img src="images/Icon_Delete3.gif" /></a></td>
  	</tr>
		 
<?php     $kk++;}
	//	echo "<pre>";
		//print_r($_SESSION['Amount_type']);
if(count($_SESSION['ACGLedgerId'])>0)		 
{
 $jj=$kk;
foreach ($_SESSION['ACGLedgerId'] as $i => $LED)
{ ?>
  	 <tr class="row1">
  	    <td><?php  echo $s->getField('accountledger','AccountsID',$LED,'AccountName');;?></td>
  	    <td> 
		<input name="" type="hidden"  value="<?php  echo $_SESSION['Amount'][$i];?>" id="amount_<?php echo $jj;?>"/>
		<?php  echo $_SESSION['Amount'][$i];?></td>
  	    <td>
		<input name="" type="hidden"  value="<?php  echo  $_SESSION['Amount_type'][$i];?>" id="amount_type_<?php echo $jj;?>"/>
		 <?php //echo $_SESSION['Amount_type'][$i];
		echo ($_SESSION['Amount_type'][$i]=='CR'? $_SESSION['Amount'][$i]:"0");
		$total_cr[]=($_SESSION['Amount_type'][$i]=='CR'? $_SESSION['Amount'][$i]:"0");
		
		?></td>
  	     <td> <?php  echo  ($_SESSION['Amount_type'][$i]=='DR'? $_SESSION['Amount'][$i]:"0");
		 $total_dr[]=($_SESSION['Amount_type'][$i]=='DR'? $_SESSION['Amount'][$i]:"0");
		 ?></td>
  	   <td> <?php  echo $_SESSION['Currency'][$i];?></td>
	   <td> <a href="<?php echo $targetpage;?>&daction=del&sid=<?php echo $i?>&VPrefix=<?php echo $VPrefix;?>&action=<?php echo $action?>&id=<?php echo $_REQUEST['id'];?>"  onclick="return confirm('Are you sure? It cannot be undone')"><img src="images/Icon_Delete3.gif" /></a></td>
  	    </tr>
		<?php $jj++;} 
} ?>
  	   <input type="hidden" id="count_entries" value="<?php if(count($_SESSION['ACGLedgerId'])>0){echo $jj;}else{echo $kk;}?>" />
	  </table></td>
  </tr>
  <tr class="row1">
	<td class="tabledata1" colspan="6" id="dis_crdr"><div  style="z-index:-9999;"></div></td>
	</tr>
  <tr>
  	<td colspan="8">
	<?php //echo $BillBookType;
		if( $BillBookType!='CP' && $BillBookType!='CR' && $BillBookType!='JV' && $BillBookType!='CON' ){
	?>
	<div id='hid-Bankdetails'>
	<fieldset>
	<legend> Bank Check Details</legend>
 	<table width="100%" cellpadding="0" cellspacing="0" border="0">	
	
	 
 	<tr>
    <td width="13%">Transation Type </td>
    <td width="28%">
	<select  name="transation_type" style="width:auto">
	<option value="CHEQUE" <?php  echo($transation_type=='CHEQUE'?'selected':'')?>  >CHEQUE</option>
		<option value="ELECTRONIC CHANGE"<?php echo ($transation_type=='ELECTRONIC CHANGE'?'selected':'')?> >ELECTRONIC CHANGE</option>
			<option value="ELECTRONIC DD/PO"<?php echo ($transation_type=='ELECTRONIC DD/PO'?'selected':'')?> >ELECTRONIC DD/PO</option>
				<option value="INTER BANK TRANSFER"<?php echo  ($transation_type=='INTER BANK TRANSFER'?'selected':'')?> >INTER BANK TRANSFER</option>
					<option value="OTHER"<?php echo ($transation_type=='OTHER'?'selected':'')?> >OTHER</option>
						<option value="SAME BANK TRANSFER" <?php echo  ($transation_type=='SAME BANK TRANSFER'?'selected':'')?> >SAME BANK TRANSFER</option>
	</select></td>
    <td width="8%">Instrument No.</td>
    <td width="22%">
	<?php 
	
	
	if($bankckeck_issueId=='' || $bankckeck_issueId=='0')
			{
				$readonly='';
			}
			else
			{
				$readonly='readonly';
			}
	
	?>
	
	<input type="text" name="banks_checkno2"  id='banks_checkno2' <?php echo $readonly; ?> value="<?php echo $banks_checkno; ?>"></td>   

    <td width="10%">Instrument Date</td>
    <td width="19%"><input type="text" name="banks_checkdate2" id="banks_checkdate2" class="datepicker" value="<?php echo $banks_checkdate; ?>" /></td>   
  </tr>
 	<tr>
 	  <td>Bank Name </td>
 	  <td><input type="text" name="banks_receivedfrom"    value="<?php echo $banks_receivedfrom; ?>" /></td>
 	  <td>Favouring name</td>
 	  <td><input type="text" name="favouring_name"   value="<?php echo $favouring_name; ?>" /></td>
 	  <td>Cross inst</td>
 	  <td><input type="text" name="cross_inst"   value="<?php echo $cross_inst; ?>" /></td>
 	  </tr>
  </table>
  </fieldset>
  </div>
  <?php } ?>
	  <?php  
	if( $BillBookType=='VatTax' && $BillBookType!='ExDV'  && $BillBookType!='JV'  && $BillBookType!='CON'){
	?>
	<fieldset>
	
	<legend> Deposit Voucher Type</legend>
 	<table width="100%" cellpadding="0" cellspacing="0" border="0">	 
 	<tr>
    <td>Voucher Type</td>
    <td><input type="text" name="deposit_vouchertype"  id='deposit_vouchertype' value="<?php echo $deposit_vouchertype; ?>"   /></td>
    <td>DCR No.</td>
    <td><input type="text" name="dcrno" id="dcrno"   value="<?php echo $dcrno; ?>" /></td>   
    </tr>
 	<tr>
 	  <td>Date</td>
 	  <td><input type="text" name="deposit_voucherdate" id="deposit_voucherdate" class="datepicker" value="<?php echo $deposit_voucherdate; ?>" /></td>
 	  <td>Deposited By</td>
 	  <td><input type="text" name="depositedby" id="depositedby"  value="<?php echo $depositedby; ?>" /></td>
 	  </tr>
  </table>
  </fieldset>
	
	 <?php } ?>
	
	
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr class="row1">
	<td class="tabledata1">Total Credit Amt.</td>
    <td><input type="text" name="total_cramount"  id='total_cramount' value="<?php echo array_sum($total_cr); ?>"  readonly="true" />
	<input type="hidden" name="total_cramount1"  id='total_cramount1' value="<?php echo array_sum($total_cr); ?>"  readonly="true" />
	
	</td>
    <td class="tabledata1">Total Debit Amt.</td>
    <td><input type="text" name="total_dramount" id="total_dramount" value="<?php echo array_sum($total_dr); ?>"readonly="true" />
	<input type="hidden" name="total_dramount1" id="total_dramount1" value="<?php echo array_sum($total_dr); ?>"readonly="true" />
	</td>
    <td class="tabledata1">Difference Amt.</td>
    <td><?php  $diff=trim(array_sum($total_cr))-trim(array_sum($total_dr));?>
	<input type="text" name="diff_amount" id="credit2" value="<?php echo $diff ?>" readonly="true"/></td>
  </tr>
<tr class="row1">
	<td class="tabledata1">Narration</td>
    <td colspan="5" valign="top">
	<textarea name="narration" id="narration" cols="95" rows="5"  ><?php echo ($narration!=''?trim($narration):'');?></textarea></td>
    </tr>
        </table>    </td>
  </tr>
    </table></td>
    </tr>
<tr class="row1">
	<td class="tabledata1" colspan="4" style="text-align:center;">
	<input type="submit" name="Debit_note" value="Update" class="search" onclick="return checkUpdateValidate();"/> 
	</td>
</tr>
</table>




<?php //if($diff==0){ ?>
 
 <?php //} ?>
</form>



<?php 
}
else{

  ?>
<form action="" method="post">
<?php 
  // $BillBookType;
  if($BillBookType=='CR' || $BillBookType=='BR' )  
     $credit_amount= $_REQUEST['amount']; 
 else 
   $dr_amount= $_REQUEST['amount']; ;
 


  $ledger_dr=$_REQUEST['cnp'];
 ?>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<?php if(count($msgErr)>0){?>
<tr class="row1">
	<td class="tabledata1" colspan="4">
	<div class='error'><?php
	   for($i=0;$i<count($msgErr);$i++)
	   {
		 echo "".$msgErr[$i].'<br>';
	   }?>
	</div>
</td>
</tr>
   <?php } ?>
<tr class="row1">
	<td class="tabledata1">Branch</td>
    <td><?php //echo $ClassBranch->Branch($Branch,$VPrefix);
					$sql_state=DB_query("SELECT loccode,locationname FROM  locations ",$db);	?>
					<select name="Branch" id="Branch" style="width:150px;"> <!--onchange=generateCode(this.value,'<?php //echo $VPrefix; ?>')-->
				<option value='000'>--Select  Branch--</option><?php 
					while($row_branch=DB_fetch_array($sql_state))
					{
					   $p='';
					   if($row_branch['loccode']==$branchid)
					   $p='selected';
					  echo "<option value='".$row_branch['loccode']."' $p >".$row_branch['locationname']."</option>";
					}?>
	</select>
			
	
	
	
	</td>
    <td class="tabledata1">Voucher No.</td>
    <td>
	<?php 
	 
	if($ChkVoucher_Numtype=='Manual'){
		?>
	<input name="voucher_no" id="voucher_no" type="text"  value="<?php echo $voucher_no;?>" style="width:150px;" />
	<?php }else{ 
	?>
		<div id='hid-Branchdiv'><input name="voucher_no" type="text"  value="Auto generated" id="voucher_no" style="width:150px;"  readonly='true'/></div>
	<?php } ?>
	</td>
</tr>
<tr class="row1">
    <td class="tabledata1">Date</td>
    <td><input name="Dates" id="Dates" type="text" style="width:150px;" class="datepicker"  value="<?php echo $Dates;?>"   onblur="chkdate(this.value)" /></td>
    <td class="tabledata1"> Ref. No.</td>
    <td><input name="Ref_no" id="Ref_no" type="text"   value="<?php echo $Ref_no;?>"   style="width:150px;"/></td>
</tr>
 
	
<?php  if($BillBookType=='JV'){?>
<tr class="row1">
	<td class="tabledata1">&nbsp;</td>
	<td class="tabledata1">&nbsp;</td>
    <td class="tabledata1">Document</td>
    <td><select name="document_type" style="width:150px;">
		<option value="None" selected="selected">None</option>
	<?php $arr_doc=array('Vat Voucher','TDS Voucher','Service Tax Voucher');
	for($kl=0;$kl<count( $arr_doc);$kl++){
	$p='';
	if($arr_doc[$kl]==$document_type)
	$p='selected';
	echo "<option value='".$arr_doc[$kl]."' $p>".$arr_doc[$kl]."</option>";
	}
	 ?></select>
	 </td>
</tr>
	<?php }?>
<tr class="row1">
    <td colspan="4">
	<table width="100%" border="0" cellspacing="0" cellpadding="3">
		<tr class="row1">	
    	<td class="tabledata1">Dr. Ledger Name</td>
		<td class="tabledata1">Debit(INR)</td>
    	<td class="tabledata1">Curr.</td>
		<td class="tabledata1">&nbsp;</td>
		</tr>
		<tr class="row1">
    	<td style="white-space:nowrap">
		<?php if($LDR==''){?>
		<div style="float:left;">
		<input type="text" name="ledgername" id="ledgername" style="width:72px;" onkeyup="getledgernameDR(this.value)" />
		</div>
		<?php }?>
		<div id="searchledgerId" style="float:right">
		<?php  //echo $BillBookType; 
		$arr1=array();$arr2=array();
	 //print_r($LDR);
		if($LDR!=''){
		$LRX=implode("|",$LDR);
		
	    $sqlCate = "SELECT * FROM accountgroupmaster WHERE GroupName REGEXP '^$LRX'";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr1[]=$rowCate['GroupId'];
  				//$arr1[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		//print_r($arr1);
		$rx=array();
		for($l=0;$l<count($arr1);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr1[$l]))
		 $rx[]=implode(',',$arr1[$l]);
		 else
		  $rx[]=$arr1[$l];
			 
		}
		//print_r($rx);
		$undergroup= implode(',',$rx );
		}
		else
		{
		$undergroup='';	
//**************************************************************

		$arr3=array();$arr4=array();
		
		if($LCR!=''){
		$LCX=implode("|",$LCR);
 	    //$sqlCate = "select * from  accountgroupmaster  where    MainGroupId   = 0  and GroupName in('$LCX')  ";
		
		 $sqlCate = "select * from  accountgroupmaster  where     GroupName REGEXP '^$LCX'    ";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr3[]=$rowCate['GroupId'];
  				//$arr3[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		$rx=array();
		for($l=0;$l<count($arr3);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr3[$l]))
		 $rx[]=implode(',',$arr3[$l]);
		 else
		  $rx[]=$arr3[$l];
			 
		}
		$undergroup1= implode(',',$rx );
		}
//*******************************************************
		
		}
		//echo $undergroup1;
		//print_r($DRledgertype);print_r($LDR);
$AccountMaster-> AccountLeger($ledger_dr,'ledger_dr[]','ledger_drID',$undergroup,$undergroup1);?>
		</div>
	<input type="hidden" name="undergroup1" id="undergroup1" value="<?php echo $undergroup1; ?>"	 />
	<input type="hidden" name="undergroup" id="undergroup" value="<?php echo $undergroup; ?>"	 />
</td>
    	<td> <input name="debit_amount[]" style="width:150px;" type="text" id="debit_amount" value="<?php //echo $dr_amount;?>" onkeyup="adddebitamount(this.value)" /></td>
    	<td>
	   <select name="dr_currency[]" id="dr_currency" style="width:150px;" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
       </select>
	  </td>
    	<td><input type="button" class="search" name="AddDRLedger" onclick="adddebitlist()" value="Add"/> 
		<?php if($CRbillstatus==1){ 
	// if($BillBookType!='JV' &&  $BillBookType!='CR' && $BillBookType!='BR'  && $BillBookType!='CON' && $BillBookType!='DBN'){?>
 	 	<a href="#"  onclick="retCNP('ledger_drID',<?php echo $VPrefix; ?>)"><input type="button" class="search" value="Bill wise Adj"/></a>
	 	<?php } ?>
      	</td>
  		</tr>
		<tr class="row1">
    	<td class="tabledata1">Cr. Ledger Name</td>
    	<td class="tabledata1">Credit(INR)</td>
    	<td class="tabledata1">Curr.</td>
		<td class="tabledata1">&nbsp;</td>
		</tr>
		<tr class="row1">
		<td> 
		<?php if($LCR==''){?>
		<div style="float:left;">
		<input type="text" name="ledgername" id="ledgername" style="width:72px;" onkeyup="getledgernameCR(this.value)" />
		</div>
		<?php }?>
		<div id="searchledgerCr" style="float:right;">
	 <?php $arr3=array();$arr4=array();
		//print_r($LCR); 
		if($LCR!=''){
		$LCX=implode("|",$LCR);
	    $sqlCate = "select * from  accountgroupmaster  where     GroupName REGEXP '^$LCX'    ";
		$rsCate  = DB_query($sqlCate,$db);
		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate =DB_fetch_array($rsCate))
			{
 			  	 $arr1[]=$rowCate['GroupId'];
  				//$arr1[]= CategoryShow($pcode, $rowCate['GroupId'], $dashVal );
 			}
		}
		//print_r($arr1);
		$rx=array();
		for($l=0;$l<count($arr1);$l++)
		{
		// echo $arr1[$l].'<br>';
		 if(is_array($arr1[$l]))
		 $rx[]=implode(',',$arr1[$l]);
		 else
		  $rx[]=$arr1[$l];
			 
		}
		$rx=array_unique($rx);
		$undergroup1= implode(',',$rx );
	 	}else
		{
		$undergroup1='';
 	//	$CRledgertype=	"'".implode("','",$LDR)."'";
		}
	if(isset($_REQUEST['cnp'])){
		 $AccountMaster-> AccountLeger($_REQUEST['cnp'],'ledger_cr[]','ledger_crID',$undergroup1);
	}
 	else
	{
	 $AccountMaster-> AccountLeger($ledger_cr,'ledger_cr[]','ledger_crID',$undergroup1);
	 }
	?>
	</div>
	<input type="hidden" name="undergroup1CR" id="undergroup1CR" value="<?php echo $undergroup1; ?>"	 />
	<!--<input type="hidden" name="undergroupCR" id="undergroupCR" value="<?php echo $undergroup1; ?>"	 />-->
	
	 </td>
    	<td><input name="credit_amount[]" id="credit_amount" style="width:150px;" type="text" value="<?php //echo $credit_amount;?>" onkeyup="addcreditamount()" /></td>
    	<td><select name="cr_currency[]" id="cr_currency" style="width:150px;" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
      </select></td>
    	<td> <input type="button" name="AddCRLedger" onclick="addcreditlist()" value="Add" class="search" />
		<?php if($DRbillstatus==1){
	 //if(($BillBookType=='CR' || $BillBookType=='BR' || $BillBookType=='DBN' ) && ($BillBookType!='CON'  	) ){?>
 	 	<a href="#"  onclick="retCNP1('ledger_crID',<?php echo $VPrefix; ?>)"><input type="button" class="search" value="Bill wise Adj"/></a>
	 	<?php } ?>
		
		</td>
  		</tr>
  <tr>
  	<td colspan="8"><div id="dis_crdr" style="z-index:-9999;"></div></td>
  </tr>
  <tr>
  	<td colspan="8">
	 <?php  //echo $BillBookType;
		if( $BillBookType!='CP' && $BillBookType!='CR' && $BillBookType!='JV' && $BillBookType!='CON'){
		
		
			if($BillBookType=='CON'){
			$favouring_name=$_SESSION['CompanyRecord']['CompanyName'];
			$cross_inst='A/C PAYEE';
			}
			
	?>
	<!--<div id='hid-Bankdetails'>-->
	<fieldset>
	<legend> Bank CheckDetails</legend>
<table width="100%" cellpadding="3" cellspacing="0" border="0">	 
<tr class="row1">
	<td class="tabledata1" width="14%">Transation Type </td>
    <td width="21%"><select  name="transation_type" style="width:150px;">
<option value="CHEQUE" <?php  echo ($transation_type=='CHEQUE'?'selected':'')?>  >Cheque</option>
<option value="NEFT" <?php  echo ($transation_type=='NEFT'?'selected':'')?>  >NEFT</option>
<option value="RTGS" <?php  echo ($transation_type=='RTGS'?'selected':'')?>  >RTGS</option>
<option value="Cheque in hand" <?php  echo ($transation_type=='CHEQUE'?'selected':'')?>  >Cheque in hand</option>
<option value="ELECTRONIC CHANGE"<?php  echo($transation_type=='ELECTRONIC CHANGE'?'selected':'')?> >Electronic Change</option>
<option value="ELECTRONIC DD/PO"<?php echo ($transation_type=='ELECTRONIC DD/PO'?'selected':'')?> >Electronic DD/PO</option>
<option value="INTER BANK TRANSFER"<?php echo ($transation_type=='INTER BANK TRANSFER'?'selected':'')?> >Inter Bank Transfer</option>
<option value="OTHER"<?php  echo($transation_type=='OTHER'?'selected':'')?> >OTHER</option>
<option value="SAME BANK TRANSFER" <?php echo ($transation_type=='SAME BANK TRANSFER'?'selected':'')?> >Same Bank Transfer</option>
	</select></td>
	
    <td class="tabledata1" width="14%">Instrument NO</td>
    <td width="19%" id="INSNO"><input type="text" name="banks_checkno" style="width:150px;"  id='banks_checkno' value="<?php echo $banks_checkno; ?>"   /></td>
    <td class="tabledata1" width="13%">Instrument Date</td>
    <td width="19%"><input type="text" name="banks_checkdate" style="width:150px;" id="banks_checkdate" class="datepicker" value="<?php echo $banks_checkdate; ?>" /></td>   
  </tr>
<tr class="row1">
	<td class="tabledata1" width="14%">Bank Name</td>
	<td width="21%"><input type="text" name="banks_receivedfrom" style="width:150px;" id="banks_receivedfrom"  value="<?php echo $banks_receivedfrom; ?>" /></td> 
	
	<td class="tabledata1">Favouring name</td>
	<td><input type="text" name="favouring_name" id="favouring_name" style="width:150px;"  value="<?php echo $favouring_name; ?>" /></td>
	<td class="tabledata1">Cross inst</td>
	<td><input type="text" name="cross_inst" id="cross_inst" style="width:150px;"  value="<?php echo $cross_inst; ?>" /></td>
</tr>
</table>
  </fieldset> 
	<!--</div>-->
  <?php } ?>
  
   <?php  
	if( $BillBookType=='VatTax' && $BillBookType!='ExDV' && $BillBookType!='JV'  && $BillBookType!='CON'){
	?>
  <fieldset>
	
	<legend> Deposit Voucher Type</legend>
<table width="100%" cellspacing="0" cellpadding="3" >	 
<tr class="row1">
	<td class="tabledata1">Voucher Type</td>
	<td><input type="text" name="deposit_vouchertype"  id='deposit_vouchertype' value="<?php echo $deposit_vouchertype; ?>"   /></td>
	<td class="tabledata1">DCR No.</td>
	<td><input type="text" name="dcrno" id="dcrno"   value="<?php echo $dcrno; ?>" /></td>   
	</tr>
	<tr class="row1">
	<td class="tabledata1">Date</td>
	<td><input type="text" name="deposit_voucherdate" id="deposit_voucherdate" class="datepicker" value="<?php echo $deposit_voucherdate; ?>" /></td>
	<td class="tabledata1">Deposited By</td>
	<td><input type="text" name="depositedby" id="depositedby"  value="<?php echo $depositedby; ?>" /></td>
</tr>
</table>
  </fieldset>
  <?php } ?>
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
    <td class="tabledata1" align="right" nowrap="nowrap">Total Credit Amt.</td>
    <td><input type="text" name="total_cramount"  id='total_cramount' value="0<?php //if(isset($_REQUEST['total_cramount'])){echo $_REQUEST['total_cramount'];}else{echo 0;} ?>" readonly="true" style="width:70px;"   />
	<input type="hidden" name="total_cramount1"  id='total_cramount1' value="0<?php //if(isset($_REQUEST['total_cramount'])){echo $_REQUEST['total_cramount'];}else{echo 0;} ?>" readonly="true" style="width:60px;"   /></td>
    <td class="tabledata1" align="right" nowrap="nowrap">Total Debit Amt.</td>
    <td><input type="text" name="total_dramount" id="total_dramount" value="0<?php //if(isset($_REQUEST['total_dramount'])){echo $_REQUEST['total_dramount'];}else{echo 0;} ?>" readonly="true" style="width:100px;"/>
	<input type="hidden" name="total_dramount1" id="total_dramount1" value="0<?php //if(isset($_REQUEST['total_dramount'])){echo $_REQUEST['total_dramount'];}else{echo 0;} ?>" readonly="true" style="width:100px;"/></td>
    <td class="tabledata1" align="right" nowrap="nowrap">Difference Amt.</td>
    <td><?php 
//$diff=	array_sum($total_cr)-array_sum($total_dr);
	?>
	<input type="text" name="diff_amount" id="credit2" value="<?php //if(isset($_REQUEST['diff_amount'])){echo $_REQUEST['diff_amount'];}else{echo "";} ?>" readonly="true" style="width:100px;"/></td>
  </tr>
<tr class="row1">
    <td class="tabledata1">Narration</td>
    <td colspan="5"><textarea name="narration" id="narration" cols="95" rows="5" >
	<?php echo ($narration!=''?trim($narration):'');?></textarea></td>
    </tr>
        </table>    </td>
  </tr>
    </table></td>
    </tr>
<tr class="row1">
	<td class="tabledata1" style="text-align:center;" colspan="4">
	<input type="submit" name="Debit_note" value="Save" class="search" onclick="return validateForm();"/>
	</td>
</tr>
</table>
<?php 

 //if(array_sum($total_cr)==array_sum($total_dr)  && (array_sum($total_cr)!=0 && array_sum($total_dr)!=0)) {?>
  
 <?php //}?>
</form>
<?php } ?>
<script language="javascript" type="text/javascript">
function validateForm()
{
	if(document.getElementById("credit2").value!=0)
	{
		alert("Sorry! Total credit is not equal to total debit");
		return false;
	}
	if(document.getElementById("Branch").value==000)
	{
		alert("Please select Branch First.");
		return false;
	}
	if(document.getElementById("voucher_no").value=="")
	{
		alert("Please Voucher Number.");
		return false;
	}
	if(document.getElementById("Dates").value=="")
	{
		alert("Please enter date.");
		return false;
	}
	if(document.getElementById("Ref_no").value=="")
	{
		alert("Please referance Number.");
		return false;
	}
	
}

function checkUpdateValidate()
{
	/*var count=document.getElementById("count_entries").value;
	alert(count);
	var dr_amt=0;
	var cr_amt=0;
	for(var i=0; i<count;i++)
	{
		var amount=document.getElementById("amount_"+i).value;
		var amount_type=document.getElementById("amount_type_"+i).value;
		alert(amount+"--"+amount_type);
		if(amount_type=='CR'){cr_amt=parseFloat(cr_amt)+parseFloat(amount);}
		else if(amount_type=='DR'){dr_amt=parseFloat(dr_amt)+parseFloat(amount);}
	}*/
	
	if(parseFloat(document.getElementById("total_cramount").value)!=parseFloat(document.getElementById("total_dramount").value))
	{
		alert("Sorry! Total credit is not equal to total debit");
		return false;
	}
	return true;
}
function calculateTotalCrDr()
{
    if(document.getElementById("debit_amount").value!='')
    var obj1=document.getElementById("debit_amount").value;
	else var obj1=0;
	
	if(document.getElementById("credit_amount").value!='')
    var obj2=document.getElementById("credit_amount").value;
	else
	var obj2=0;
	
	
	var count=document.getElementById("count_entries").value;
	var dr_amt=0;
	var cr_amt=0;
	for(var i=0; i<count;i++)
	{
		var amount=document.getElementById("amount_"+i).value;
		var amount_type=document.getElementById("amount_type_"+i).value;
		if(amount_type=='CR'){cr_amt=parseFloat(cr_amt)+parseFloat(amount);}
		else if(amount_type=='DR'){dr_amt=parseFloat(dr_amt)+parseFloat(amount);}
	}
	var dr_amt=parseFloat(obj1)+dr_amt;
	var cr_amt=parseFloat(obj2)+cr_amt;
	
	document.getElementById("total_cramount").value=cr_amt.toFixed(2);
	document.getElementById("total_dramount").value=dr_amt.toFixed(2);
	
	if(cr_amt>dr_amt){var diff=parseFloat(cr_amt)-parseFloat(dr_amt);}
	else if(dr_amt>cr_amt){var diff=parseFloat(dr_amt)-parseFloat(cr_amt);}else{var diff=0;}
	document.getElementById("credit2").value=diff.toFixed(2);
}

function generateCode(str,prefix)
{
	if (str=="")
	{
		document.getElementById("hid-Branchdiv").value="";
		return;
	}
	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)
		{
			document.getElementById("hid-Branchdiv").innerHTML=xmlhttp.responseText;
		}
	}
 	xmlhttp.open("GET","ACBillbookPayment-Branch.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.send();
 
}
function adddebitamount()
{
		//var dramt=0;
		
		var str=document.getElementById("debit_amount").value;
		var cramt = document.getElementById("total_cramount").value;
		var dramt = document.getElementById("total_dramount1").value;
		
		var dramt=parseFloat(dramt)+parseFloat(str);

		document.getElementById("total_dramount").value=dramt.toFixed(2);
		var diff=parseFloat(dramt)-parseFloat(cramt)
		document.getElementById("credit2").value=diff.toFixed(2);

}
function addcreditamount()
{
		//var cramt=0;
		var str=document.getElementById("credit_amount").value;
		var dramt = document.getElementById("total_dramount").value;
		var cramt = document.getElementById("total_cramount1").value;
		
		cramt=parseFloat(cramt)+parseFloat(str);

		document.getElementById("total_cramount").value=cramt.toFixed(2);
		var diff=parseFloat(cramt)-parseFloat(dramt)
		document.getElementById("credit2").value=diff.toFixed(2);
	
}

function adddebitamountedit()
{
	var dramt=0;
	var str=document.getElementById("debit_amount").value;
	var dramt = document.getElementById("total_dramount1").value;
	var cramt = document.getElementById("total_cramount").value;
	dramt=parseFloat(dramt)+parseFloat(str);
	document.getElementById("total_dramount").value=dramt.toFixed(2);
	var diff=parseFloat(dramt)-parseFloat(cramt)
	document.getElementById("credit2").value=diff.toFixed(2);
	
}


function addcreditamountedit()
{
	var cramt=0;
	var str=document.getElementById("credit_amount").value;
	var cramt = document.getElementById("total_cramount1").value;
	var dramt = document.getElementById("total_dramount").value;
	
	cramt=parseFloat(cramt)+parseFloat(str);
	
	document.getElementById("total_cramount").value=cramt.toFixed(2);
	var diff=parseFloat(cramt)-parseFloat(dramt)
	document.getElementById("credit2").value=diff.toFixed(2);
	
}

function adddebitlist()
{
	var accId = document.getElementById("ledger_drID").value;
	var drAmt = document.getElementById("debit_amount").value;
	var drCur = document.getElementById("dr_currency").value;
	
	if(accId=="0")
	{
	alert("Please select Ledger Name");
	return;
	}
	if(drAmt=="")
	{
	alert("Please enter Debit Amount");
	return;
	}
	document.getElementById("total_dramount1").value=document.getElementById("total_dramount").value;
	document.getElementById("debit_amount").value='';
	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)
	{
	document.getElementById("dis_crdr").innerHTML=xmlhttp.responseText;
	}
	}
	//xmlhttp.open("GET","ACCreditDebit.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.open("GET","ACCreditDebit.php?ledger_dr="+accId+"&debit_amount="+drAmt+"&dr_currency="+drCur,false);
	xmlhttp.send();
}

function addcreditlist()
{
	var accId = document.getElementById("ledger_crID").value;
	var crAmt = document.getElementById("credit_amount").value;
	var crCur = document.getElementById("cr_currency").value;
	
	if(accId=="0")
	{
	alert("Please select Ledger Name");
	return;
	}
	if(crAmt=="")
	{
	alert("Please enter Credit Amount");
	return;
	}
	document.getElementById("total_cramount1").value=document.getElementById("total_cramount").value;
	document.getElementById("credit_amount").value='';
	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)
	{
	document.getElementById("dis_crdr").innerHTML=xmlhttp.responseText;
	}
	}
	//xmlhttp.open("GET","ACCreditDebit.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.open("GET","ACCreditDebit.php?ledger_cr="+accId+"&credit_amount="+crAmt+"&cr_currency="+crCur,false);
	xmlhttp.send();
}

function adddebitlistedit()
{
	var accId = document.getElementById("ledger_drID").value;
	var drAmt = document.getElementById("debit_amount").value;
	var drCur = document.getElementById("dr_currency").value;
	
	if(accId=="0")
	{
	alert("Please select Ledger Name");
	return;
	}
	if(drAmt=="")
	{
	alert("Please enter Debit Amount");
	return;
	}
	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)
	{
	document.getElementById("dis_crdr").innerHTML=xmlhttp.responseText;
	}
	}
	//xmlhttp.open("GET","ACCreditDebit.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.open("GET","ACCreditDebitedit.php?ledger_dr="+accId+"&debit_amount="+drAmt+"&dr_currency="+drCur,true);
	xmlhttp.send();
}


function addcreditlistedit()
{
	var accId = document.getElementById("ledger_crID").value;
	var crAmt = document.getElementById("credit_amount").value;
	var crCur = document.getElementById("cr_currency").value;
	
	if(accId=="0")
	{
	alert("Please select Ledger Name");
	return;
	}
	if(crAmt=="")
	{
	alert("Please enter Credit Amount");
	return;
	}
	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)
	{
	document.getElementById("dis_crdr").innerHTML=xmlhttp.responseText;
	}
	}
	//xmlhttp.open("GET","ACCreditDebit.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.open("GET","ACCreditDebitedit.php?ledger_cr="+accId+"&credit_amount="+crAmt+"&cr_currency="+crCur,true);
	xmlhttp.send();
}

function deletedrcr(str,type,amt)
{
alert(type);
alert(amt);
	if(type=='CR'){
	document.getElementById("total_cramount").value=parseFloat(document.getElementById("total_cramount").value)-parseFloat(amt);
	alert(parseFloat(document.getElementById("total_cramount").value)-parseFloat(amt));
	document.getElementById("total_cramount1").value=document.getElementById("total_cramount").value;
	document.getElementById("credit2").value=document.getElementById("total_cramount").value-document.getElementById("total_dramount").value;
	}else{
	document.getElementById("total_dramount").value=parseFloat(document.getElementById("total_dramount").value)-parseFloat(amt);
	document.getElementById("total_dramount1").value=document.getElementById("total_dramount").value;
	document.getElementById("credit2").value=document.getElementById("total_cramount").value-document.getElementById("total_dramount").value;
	}	
	
	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)
		{
			document.getElementById("dis_crdr").innerHTML=xmlhttp.responseText;
		}
	}
 	//xmlhttp.open("GET","ACCreditDebit.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.open("GET","ACCreditDebit.php?delcrdrid="+str,true);
	xmlhttp.send();
}

function deletedrcredit(str)
{
	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)
		{
			document.getElementById("dis_crdr").innerHTML=xmlhttp.responseText;
		}
	}
 	//xmlhttp.open("GET","ACCreditDebit.php?Prefix="+prefix+"&q="+str,true);
	xmlhttp.open("GET","ACCreditDebit.php?delcrdrid="+str+"&edit=edit",true);
	xmlhttp.send();
}

function getledgernameDR(str)
{
	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)
	{
	document.getElementById("searchledgerId").innerHTML=xmlhttp.responseText;
	}
	}
	var undergroup1=document.getElementById("undergroup1").value;
	var undergroup=document.getElementById("undergroup").value;
	xmlhttp.open("GET","searchledgerName.php?searchWord="+str+"&undergroup1="+undergroup1+"&undergroup="+undergroup,true);
	xmlhttp.send();
}



function getledgernameCR(str)
{
	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)
	{
	document.getElementById("searchledgerCr").innerHTML=xmlhttp.responseText;
	}
	}
	var undergroup1=document.getElementById("undergroup1CR").value;
	xmlhttp.open("GET","searchledgerNameCR.php?searchWord="+str+"&undergroup1="+undergroup1,true);
	xmlhttp.send();
}


function GetBankDetals(str)
{
	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)
	{
	document.getElementById("INSNO").innerHTML=xmlhttp.responseText;
	}
	}
	var Vprefixtype=document.getElementById("Vprefixtype").value;
	xmlhttp.open("GET","ACBillbookBankDetails.php?q="+str+"&VPREFIXTYPE="+Vprefixtype,true);
	xmlhttp.send();
}

</script>