<?php    include('includes/AccountGroupMaster_Class.php');

 $table='mastersubledger';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;
if($_REQUEST['Save'])
{
	if(empty($_REQUEST['SubLedgerName']))
	{
	   $msgErr[]='Please Enter SubLedgerName';
	   $status=false;
	}
	 
	 if(empty($_REQUEST['UnderLedger']))
	{
	   $msgErr[]='Please Enter UnderLedger';
	   $status=false;
	}
	 
	 
	 	$condition="SubLedgerName='".$_REQUEST['SubLedgerName']."' and Identification='".$_REQUEST['Identification']."'";
		$allready=$s->GetRecord($table,  $condition);
		$row=DB_query($allready,$db);
		if(DB_num_rows($row)>0 && $action!='Edit')
		{
			 $msgErr[]='Identification and SubLedgerName Already Exist.Please Try Again...';
			 $status=false;
		}
		
} 

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

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

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="LedgerId='".$_REQUEST['LedgerId']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	$SubLedgerName=$RData['SubLedgerName'];
	$UnderLedger=$RData['UnderLedger'];
	$Identification=$RData['Identification'];

}
 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{
 $data['SubLedgerName']=$_REQUEST['SubLedgerName'];
 $data['UnderLedger']=$_REQUEST['UnderLedger'];
  $data['Identification']=$_REQUEST['Identification'];

 $cond="LedgerId='".$_REQUEST['LedgerId']."'";
 	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
 $s->pageLocation('main.php?route=MasterSubLedger&daction=Updated');
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{
 $data['SubLedgerName']=$_REQUEST['SubLedgerName'];
 $data['UnderLedger']=$_REQUEST['UnderLedger'];
  $data['Identification']=$_REQUEST['Identification'];
 	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
 $s->pageLocation('main.php?route=MasterSubLedger&daction=Inserted');
}
?>
<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>Master Sub Ledgers </h1></td>
  </tr>
  
  <?php if(isset($daction))
  {?>
    <tr>
  
    <td colspan="2" ><div class="success"><?php echo "Record ".$daction." Successfully" ?></div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>
  
    <td colspan="2" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
   
   
  <tr>
    <td width="35%">SubLedger Name</td>
    <td width="65%"><input name="SubLedgerName" type="text" size="27"  value="<?php echo $SubLedgerName;?>"></td>
  </tr>
  <tr>
    <td>Under Ledger </td>
    <td>
		<?php
  	$result_ledger = DB_query("SELECT AccountsID,AccountName  FROM accountledger  WHERE SubLedger='1'",$db);
  			?><select name="UnderLedger">
			<option value="000">Please select Ledger</option>
			<?php 			
 			while($myrow_ledger = DB_fetch_row($result_ledger))
			{
			  $p='';
			  if($myrow_ledger[0]==$UnderLedger)
			  $p="selected";
			?>
			<option value="<?php echo $myrow_ledger[0]; ?>"<?php echo $p; ?>><?php echo $myrow_ledger[1]; ?></option>
			<?php }?>
			</select>
 
 	</td>
  </tr>
  
  <tr>
    <td>Identification </td>
    <td><input name="Identification" type="text" size="27"  value="<?php echo $Identification;?>">
 	</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td> 
	
      <input type="submit" name="Save" value="<?php echo ($action=='Edit'?"Edit":"Save") ?>">
    
    </td>
  </tr>
</table>
 </form>
 


 
 
 <table width="80%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th scope="col">LedgerId</th>
	  <th scope="col">SubLedgerName</th>
	    <th scope="col">UnderLedger</th>
      <th scope="col">Identification</th>
	<th scope="col">Edit</th>
    <th scope="col">Delete</th>
  </tr>
 
  <tr>
   <?php 
   $GetRecord = "Select * from $table";
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='3'>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {?>
   <td><div align="center"><?php echo $RowData['LedgerId']; ?></div></td>
    <td><?php echo $RowData['SubLedgerName']; ?>	  </td>
	   <td><?php 
	   echo ucfirst($s->getField('accountledger','AccountsID',$RowData['UnderLedger'],'AccountName'));
	   
	    ?>	  </td>
	    <td><?php echo $RowData['Identification']; ?>	  </td>
   
   <td><div align="center"><a href="main.php?route=MasterSubLedger&action=Edit&LedgerId=<?php echo $RowData['LedgerId']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><a href="main.php?route=MasterSubLedger&action=del&LedgerId=<?php echo $RowData['LedgerId']; ?>" 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 $i++;}   
   ?> 
 
</table>