<?php    include('includes/AccountGroupMaster_Class.php');
  $table='journalvoucher';
  $modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
 $rurl="&modid=".$modid."&frmid=".$frmid;

$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;
?>
<script language="javascript">
function getvocuher(str)
{
 document.getElementById("voucher_code").value=str;
}
function selectDRCR(str)
{
if(str=='Dr'){
  	 document.getElementById("credit").disabled=true;
      document.getElementById("debit").disabled=false;
  }
 else{
  document.getElementById("debit").disabled=true;
   
    document.getElementById("credit").disabled=false;
   }
}
</script>
	<script type="text/javascript">
function showLegder(str)
{
  if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","GetSubLedger.php?q="+str,true);
xmlhttp.send();
}
</script> 
	 
<?php 

if($_REQUEST['action']=='Edit' && isset($_REQUEST['Jid']))
{
    $sql=DB_query("select * from journalvoucher where JournalVoucherId='".$_REQUEST['Jid']."'",$db);
	$Rdata=DB_fetch_array($sql);
	$branch=$Rdata['branch'];
	$voucher_no=$Rdata['voucher_no'] ;
	$voucher_code=$Rdata['voucher_code'] ;
	$journal_voucherdate=$Rdata['journal_voucherdate'];
	$ledger_id=$Rdata['ledger_id'];
	$sub_ledger_id=$Rdata['sub_ledger_id'];
	$dr_cr=$Rdata['dr_cr'];
	$debit=$Rdata['debit'];
	$narration=$Rdata['narration'];
	$credit=$Rdata['credit'];
	$btn_display=1;
 }
 else{
      $sql=DB_query("select * from journal_voucher_master A,journalvoucher B  where A.voucherId ='".$_REQUEST['id']."' and A.voucherId=B.TransId",$db);
	 $Rdata=DB_fetch_array($sql);
	 $branch=$Rdata['branch'];
	 $voucher_no=$Rdata['voucher_no'] ;
	 $voucher_code=$Rdata['voucher_code'] ;
	 $journal_voucherdate=$Rdata['journal_voucherdate'];
	 $btn_display=0;
	/* $ledger_id=$Rdata['ledger_id'];
	 $sub_ledger_id=$Rdata['sub_ledger_id'];
	 $dr_cr=$Rdata['dr_cr'];
	 $debit=$Rdata['debit'];
	 $narration=$Rdata['narration'];
	 $credit=$Rdata['credit'];
	 */
 }

?>	 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td ><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
  </tr>
</table>

<form action="" method="post">
<table width="50%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td colspan="2" ><h1><?php if($_REQUEST['action']!='')
	echo $_REQUEST['action'] ;
	else
	echo "New";
	 ?>&nbsp;Journal Voucher </h1></td>
	 
  </tr>
  
  <?php if(isset($daction))
  {?>
    <tr>
  
    <td colspan="4" ><div class="success"><?php echo "Record ".$daction." Successfully" ?></div></td>
  </tr>
  <?php }
 
   ?>
   
   
   
  <tr>
    <td width="35%">Branch</td>
    <td width="65%">
	
		
			<?php $_SESSION['ledgid']=1;
	$result_location = DB_query("SELECT loccode,locationname FROM  locations  WHERE 1=1",$db);
  			?><select name="branch" style="width:200px;"  onchange="getvocuher(this.value)">
			<option value="00">Please Select Branch Name</option>
			<?php 			
 			while($myrow_location = DB_fetch_row($result_location))
			{
			
			  $p='';
			  if($myrow_location[0]==$branch)
			  $p="selected";
			?>
			<option value="<?php echo $myrow_location[0]; ?>" <?php echo $p;?>><?php echo $myrow_location[1]; ?></option>
			<?php }?>
			</select>
	 </td>
	 <td>Date</td>
	  <td><input name="journal_voucherdate" type="text" size="27" class="datepicker"  value="<?php echo $journal_voucherdate;?>"> 	</td>
  </tr>
  <tr>
    <td>Voucher No. </td>
    <td><input name="voucher_code" type="text" id="voucher_code" size="5"  value="<?php  echo $voucher_code;?>" />
 
	<input name="voucher_no" type="text" size="27"  value="<?php echo $voucher_no;?>"> 	</td>
	<td colspan="2"></td>
  </tr>
  
   <tr>
    <td colspan="4"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Ledger Name</td>
    <td>D/C</td>
    <td>Debit(INR)</td>
    <td>Credit(INR)</td>
    <td>Narration</td>
  </tr>
  <tr>
    <td>
	<?php $result_ledger = DB_query("SELECT AccountsID,AccountName  FROM accountledger  WHERE 1=1",$db);   			?>
			<select name="ledger_id" onchange="showLegder(this.value)">
			<option value="000">Please select Ledger</option>
			<?php 			
 			while($myrow_ledger = DB_fetch_row($result_ledger))
			{
			  $p='';
			  if($myrow_ledger[0]==$ledger_id)
			  $p="selected";
			?>
			<option value="<?php echo $myrow_ledger[0]; ?>"<?php echo $p; ?>><?php echo $myrow_ledger[1]; ?></option>
			<?php }?>
			</select>	
			
			<div id="txtHint">
			<?php  
			
			$result_ledger = DB_query("SELECT LedgerId,SubLedgerName  FROM mastersubledger  WHERE LedgerId='".$sub_ledger_id."'",$db);
 		if( DB_num_rows($result_ledger)>0)
		{
  			?>
 			SubLedger: 
			<select name="sub_ledger_id" tabindex="5"  >
			<option value="0">Please select Ledger</option>
			<?php 			
 			while($myrow_ledger = DB_fetch_row($result_ledger))
			{
			  $p='';
			  if($myrow_ledger[0]==$sub_ledger_id)
			  $p="selected";
			?>
			<option value="<?php echo $myrow_ledger[0]; ?>"<?php echo $p; ?>><?php echo $myrow_ledger[1]  ?></option>
			<?php }?>
			</select>
			
			<?php }?></div>
	 </td>
    <td><?php
	if(isset($_REQUEST['dr_cr']))
	{
		if($_REQUEST['dr_cr']=='Dr')
		$dr_cr='Cr';
			else
			$dr_cr='Dr';
	}

 	if( $dr_cr=='Dr')
	   $p='selected';
	if($dr_cr=='Cr')
	  $q='selected';
	 
	 
	 ?>
 
	<select name="dr_cr" style="width:50px" onchange="selectDRCR(this.value)">
	<option value="000" selected="selected">&nbsp;</option>
	<option value="Dr" <?php echo $p ?>>Dr.</option>
		<option value="Cr" <?php echo $q;?>>Cr.</option>
	</select>
	
	 </td>
    <td><div id='hiddebit'>
	<?php if($dr_cr=='Dr'){?>
	<input type="text" name="debit" id="debit" value="<?php echo $debit; ?>" />
	<?php }else{ ?>
	<input type="text" name="debit" id="debit" value="<?php echo $debit; ?>" disabled="disabled" />
	<?php } ?>
	</div></td>
    <td><div id='hidcredit'>
	
	<?php if($dr_cr=='Cr'){?>
 	<input type="text" name="credit" id="credit" value="<?php echo $credit; ?>" />
	<?php }else{ ?>
		<input type="text" name="credit" id="credit" value="<?php echo $credit; ?>" disabled="disabled" />
		<?php } ?>
	</div></td>
    <td><input type="text" name="narration" value="<?php echo $narration; ?>" /></td>
  </tr>
</table>
     </td>
    </tr>
 
  <tr>
    <td>&nbsp;</td>
    <td> <?php if(isset($_REQUEST['Jid'])){?>
       <input type="submit" name="SaveJournalVocuher" value="Save">   
	   <?php } ?>
	    </td><td>&nbsp;</td><td>&nbsp;</td>
  </tr>
</table>
</form>
 

  <?php 
 
   $dr=0;
  $cr=0;
  	$checkbalance=DB_query("select * from journal_voucher_master A,journalvoucher B where A.voucherId  = B.TransId and  voucherId ='".$_REQUEST['id']."'",$db);
	while($rtx=DB_fetch_array($checkbalance)	)
	{
		if($rtx['dr_cr']=='Dr')
		{
		  $dr+=$rtx['debit'];
		}
		else{
		$cr+=$rtx['credit'];
		}
		
	}
	 
	if($dr!=$cr)
	{
	      echo  $msgErr1="<div class='error'>Debit and Credit Amount are Not Same So Please Check The Transation</div>";
	   //$status=false;
	}

  
   if($_REQUEST['SaveJournalVocuher']&& $_REQUEST['action']=='Edit')
  {
 
  $status=true;
	 
	if($status)
	{
	$data['branch']=$_REQUEST['branch'];
	$data['voucher_no']=$_REQUEST['voucher_no'];
	$data['voucher_code']=$_REQUEST['voucher_code'];
	$data['journal_voucherdate']=FormatDateForSQL($_REQUEST['journal_voucherdate']);
	$data['ledger_id']=($_REQUEST['ledger_id']);
	$data['sub_ledger_id']=($_REQUEST['sub_ledger_id']);
	$data['dr_cr']=($_REQUEST['dr_cr']);
	$data['debit']=($_REQUEST['debit']);
	$data['narration']=($_REQUEST['narration']);
	$data['credit']=($_REQUEST['credit']);
	if(isset($_REQUEST['credit']))
	$data['current_amount']=$_REQUEST['credit'];
	else
	$data['current_amount']=$_REQUEST['debit'];
	$cond="JournalVoucherId='".$_REQUEST['Jid']."'";
 	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
      $s->pageLocation('index.php?route=JournalVoucherDetails&action=Edit&id='.$_REQUEST['id'].$rurl.'&daction=Update');
 	}
 }

 ?>
 <form action="" method="post">
 <table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th scope="col"><strong>  VoucherId</strong></th>
	  <th scope="col"><strong>Branch</strong></th>
	    <th scope="col"><strong>Dr./Cr.</strong></th>
		<th scope="col"><strong>Ledger</strong></th>
		<th scope="col"><strong>SubLedger</strong></th>
		   <th scope="col"><strong>Currency</strong></th>
		   
      <th scope="col"><strong>Credit</strong></th>
	   <th scope="col"><strong>Debit</strong></th>  
	   <th scope="col"><strong>Current Amount</strong></th>
	  <th scope="col"><strong>Edit</strong></th>
  </tr>
 
  
   <?php 
     $j=0;
	 $l=1;
  $amount_credit=0;
  $amount_debit=0;
   $ResultData= DB_query("select * from journalvoucher where TransId='".$_REQUEST['id']."'",$db);
  while($RowData = DB_fetch_array($ResultData))
  {  if($j++%2==0)
     $bgcolor="#CCCCCC";
	 else
	 $bgcolor="#FFFFFF";
   
	 
	 if($RowData['dr_cr']=='Dr')
	 {
		 $amount_debit+= $RowData['debit'];
	 }
	 else{
	 	 $amount_credit+= $RowData['credit'];
	 }
   ?><tr bgcolor="<?php echo $bgcolor;?>" >
   <td><div align="center"><?php 
    echo $l++; //$RowData['JournalVoucherId']; ?></div></td>
    <td><?php  
	
	echo $s->getField('locations','loccode',$RowData['branch'],'locationname');
	 
	 ?>	  </td>
	   <td><?php echo  $RowData['dr_cr']; ?>	  </td>
	    <td><div align="left"><?php    
     
	  echo $s->getField('accountledger','AccountsID', $RowData['ledger_id'] ,'AccountName');
	?></div></td>
	 <td><div align="left"><?php    
     
	  echo $s->getField('mastersubledger','LedgerId', $RowData['sub_ledger_id'] ,'SubLedgerName');
	?></div></td>
	
	    <td>INR	  </td>
		
		 <td><?php echo    $RowData['credit']; ?>	  </td>
		  <td><?php echo $RowData['debit']; ?>	  </td>
	    
   <td><?php 
	 
		 echo $RowData['current_amount'];
	 
		 ?>	  </td>
   <td><div align="center"><a href="index.php?route=JournalVoucherDetails&action=Edit&id=<?php echo $_REQUEST['id'] ?>&Jid=<?php echo $RowData['JournalVoucherId'].$rurl?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	 
	 </tr>
 
	<?php }  
	if($amount_credit!=0 || $amount_debit!=0)
	{
	?> 
	  <tr bgcolor="#333333" style="color:#FFFFFF" >
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>Total:</td>
     <td><?php  echo $amount_credit;?> </td>
       <td><?php  echo $amount_debit;?> </td>
     <td>&nbsp;</td>
   </tr>   
   <?php } ?>
</table>
</form>