<?php    include('includes/AccountGroupMaster_Class.php');
  $table='JournalVoucher';
$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>
	<?php
  	 $branch=$_REQUEST['branch'];
	$journal_voucherdate=$_REQUEST['journal_voucherdate'];
	$voucher_code=$_REQUEST['voucher_code'];
  	$dr_cr=$_REQUEST['dr_cr'];
	if($dr_cr=='Dr')
	{$dr_cr='Cr';																				
	}
	else{
	$dr_cr='Dr';
	}

 	$condition="1=1";
  	$lastid=$s->GetLastRecord('journal_voucher_master',  $condition,'voucherId');
 

if($_REQUEST['Save'])
{

	if(empty($branch))
	{
	   $msgErr[]='Please Enter branch';
	   $status=false;
	}
	 
	 if(empty($voucher_code))
	{
	   $msgErr[]='Please Enter voucher_no';
	   $status=false;
	}
	
	
 
	 
	 
	 	$condition="branch='".$_REQUEST['branch']."' and journal_voucherdate='".$_REQUEST['journal_voucherdate']."'";
		$allready=$s->GetRecord($table,  $condition);
		$row=DB_query($allready,$db);
		if(DB_num_rows($row)>0 && $action!='Edit')
		{
			 $msgErr[]='journal_voucherdate and branch Already Exist.Please Try Again...';
			 $status=false;
		}
		
} 

//************************Delete*********************************************** 
if( $action=='del')
{
	//$condition="JournalVoucherId='".$_REQUEST['JournalVoucherId']."'";
	//$s->DeleteRecord($table,$condition);
	// $s->pageLocation('main.php?route=JournalVoucher&daction=Deleted');

}
 
  
//************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	
$id=$_REQUEST['id'];
 for($i=0;$i<count($_SESSION['voucher_no']);$i++)
 {
   if($id==$_SESSION['voucher_no'][$i])
   {
	  $branch=$_SESSION['branch'][$i];
	  	$t=explode('_',$_SESSION['voucher_no'][$i]);
		 $voucher_no=$t[1];
		$voucher_code=$t[0];
		$journal_voucherdate=ConvertSQLDate($_SESSION['journal_voucherdate'][$i]);
		$ledger_id=$_SESSION['ledger_id'][$i];
		$dr_cr=$_SESSION['dr_cr'][$i];
		$debit=$_SESSION['debit'][$i];
		$credit=$_SESSION['credit'][$i];
		$narration=$_SESSION['narration'][$i];
 	}	
 }

}
 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$id=$_REQUEST['id'];
 for($i=0;$i<count($_SESSION['voucher_no']);$i++)
 {
   if($id==$_SESSION['voucher_no'][$i])
   {
	  	$_SESSION['dr_cr'][$i]=($_REQUEST['dr_cr']);
		$_SESSION['branch'][$i]=$_REQUEST['branch'];
		$_SESSION['voucher_no'][$i]=$_REQUEST['voucher_code'].'_'.$_REQUEST['voucher_no'];
		$_SESSION['journal_voucherdate'][$i]=FormatDateForSQL($_REQUEST['journal_voucherdate']);
		$_SESSION['ledger_id'][$i]=($_REQUEST['ledger_id']);
		$_SESSION['debit'][$i]=$_REQUEST['debit'];
		if($_REQUEST['debit']!='')
		$_SESSION['current_amount'][$i]=$_REQUEST['debit'];
		else
		$_SESSION['current_amount'][$i]=$_REQUEST['credit'];
		$_SESSION['credit'][$i]=$_REQUEST['credit'];
		$_SESSION['narration'][$i]=$_REQUEST['narration'];
 	}	
 }

}
 
//************************INSERT*********************************************** 

if($_REQUEST['Save']  && $_REQUEST['action']!='edit')
{
    $_SESSION['dr_cr'][]=($_REQUEST['dr_cr']);
 	$_SESSION['branch'][]=$_REQUEST['branch'];
 	$_SESSION['voucher_no'][]=$_REQUEST['voucher_code'].'_'.$_REQUEST['voucher_no'];
  	$_SESSION['journal_voucherdate'][]=FormatDateForSQL($_REQUEST['journal_voucherdate']);
  	$_SESSION['ledger_id'][]=($_REQUEST['ledger_id']);
	$_SESSION['debit'][]=$_REQUEST['debit'];
	if($_REQUEST['debit']!='')
	$_SESSION['current_amount'][]=$_REQUEST['debit'];
	else
	$_SESSION['current_amount'][]=$_REQUEST['credit'];
 	$_SESSION['credit'][]=$_REQUEST['credit'];
	$_SESSION['narration'][]=$_REQUEST['narration'];
	$s->pageLocation('main.php?route=JournalVoucher');
	
}
 ?>
<form action="" method="get">
<table width="50%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td colspan="2"><h1>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 }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>
  
    <td colspan="4" ><div class="error"><?php 

for( $kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
   
   
  <tr>
    <td width="35%">Branch</td>
    <td width="65%">
	
		
			<?php 
	$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;?>" />
<?php   $_SESSION['vcode']=($lastid+1);?>
	<input name="voucher_no" type="text" size="27"  value="<?php echo $_SESSION['vcode'];?>"> 	</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 accountcode,accountname  FROM chartmaster  WHERE 1=1",$db);
  			?><select name="ledger_id">
			<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>
	
	 </td>
    <td><?php
	
 	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> 
       <input type="submit" name="Save" value="Add">    </td><td>&nbsp;</td><td>&nbsp;</td>
  </tr>
</table>
</form>
 






<?php 
  if($_REQUEST['SaveJournalVocuher'])
  {
  
  for($i=0;$i<count($_SESSION['voucher_no']);$i++)
  {
	$data['dr_cr']=($_SESSION['dr_cr'][$i]);
	$data['branch']=$_SESSION['branch'][$i];
	$data['voucher_no']=$_SESSION['voucher_no'][$i];
	$data['journal_voucherdate']=$_SESSION['journal_voucherdate'][$i];
	$data['ledger_id']=($_SESSION['ledger_id'][$i]);
	$data['debit']=$_SESSION['debit'][$i];
	if($_SESSION['debit'][$i]!='')
	$data['current_amount']=$_SESSION['debit'][$i];
	else
	$data['current_amount']=$_SESSION['credit'][$i];
	$data['credit']=$_SESSION['credit'][$i];
	$data['narration']=$_SESSION['narration'][$i];
	$insert1= $s->insertRecord($table, $data);
	 $sql=DB_query($insert1,$db);
  }		
  // $s->pageLocation('main.php?route=JournalVoucher&daction=Inserted');
  
  }
 
 
   if($_REQUEST['SaveJournalVocuher']&& $_REQUEST['action']=='edit')
  {
  $data['branch']=$_REQUEST['branch'];
 $data['voucher_no']=$_REQUEST['voucher_code'].'_'.$_REQUEST['voucher_no'];
 $data['journal_voucherdate']=FormatDateForSQL($_REQUEST['journal_voucherdate']);
 $data['journal_voucherdate']=FormatDateForSQL($_REQUEST['journal_voucherdate']);
 $data['ledger_id']=($_REQUEST['ledger_id']);
 $data['dr_cr']=($_REQUEST['dr_cr']);
 $data['debit']=($_REQUEST['debit']);
 $data['narration']=($_REQUEST['narration']);
 $data['credit']=($_REQUEST['credit']);
 $cond="JournalVoucherId='".$_REQUEST['JournalVoucherId']."'";
 	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
 $s->pageLocation('main.php?route=JournalVoucher&daction=Updated');
 
 }?>
 <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>Currency</strong></th>
		     <th scope="col"><strong>Current Amount</strong></th>
      <th scope="col"><strong>Credit</strong></th>
	   <th scope="col"><strong>Debit</strong></th>
	  <th scope="col"><strong>Edit</strong></th>
    <th scope="col"><strong>Delete</strong></th>
  </tr>
 
  
   <?php 
   $GetRecord = "Select * from $table";
   $ResultData = DB_query($GetRecord,$db);
   $j=0;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='3'>--No Record--</td>";
   
   // for($i=0;$i<count($_SESSION['voucher_no']);$i++)
  while($RowData = DB_fetch_array($ResultData))
   {  if($j++%2==0)
     $bgcolor="#CCCCCC";
	 else{
	 $bgcolor="#FFFFFF";
	 }
	 
	 $_SESSION['vcode']=$vcode;
   ?><tr bgcolor="<?php echo $bgcolor;?>" >
   <td><div align="center"><?php 
  
   echo $RowData['JournalVoucherId']; ?></div></td>
    <td><?php  
	 
	 echo $s->getField('locations','loccode',$RowData['branch']  ,'locationname');
	 
	 ?>	  </td>
	   <td><?php echo $RowData['dr_cr']; ?>	  </td>
	    <td>INR	  </td>
		<td><?php 
		 
		 echo $RowData['current_amount'];
	 
		 ?>	  </td>
		 <td><?php echo    $RowData['credit']; ?>	  </td>
		  <td><?php echo $RowData['debit']; ?>	  </td>
	    
   
   <td><div align="center"><a href="main.php?route=JournalVoucher&amp;action=Edit&amp;id=<?php echo $RowData['JournalVoucherId'][$i]?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><a href="main.php?route=JournalVoucher&amp;action=del&amp;Delid=<?php echo $RowData['JournalVoucherId'][$i] ?>" onClick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></div></td>	
	 </tr>
	<?php }   
   ?> 
   <tr>
     <td colspan="3"><input name="SaveJournalVocuher"  value="Save" type="submit" /></td>
   </tr>
</table>
</form>