<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('25','edits','R');    
$_SESSION['cuPages']='acc_ledgerlist.php';   
$cuPage='acc_ledgerlist.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='account';
$menuh='Account';
$phead='ledlist';
$page='Edit Ledger';
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php 
if(isset($_POST['update_ledger'])){
	$legid = remove_junk(escape($_POST['legid']));
    $name = ucwords(remove_junk(escape($_POST['name'])));
    $grid = remove_junk(escape($_POST['grid']));
    $egrid = remove_junk(escape($_POST['egrid']));
    $sgrid = remove_junk(escape($_POST['sgrid']));
    if($sgrid==''){$sgrid='NULL';}else{$sgrid="'".$sgrid."'";}
    if($grid===$egrid){
    $code = remove_junk(escape($_POST['code']));    
    }else{
    $code = get_ledgercode($grid);    
    }
    $description = remove_junk(escape($_POST['description']));
	
	if(isset($_POST['name'])){
	$ducode = mysqli_query($con,"SELECT * FROM tbl_acledger WHERE name = '$name' AND grid='$grid' AND id!='$legid'");
	}
	
	if($ducode->num_rows > 0) {
	save_msg('i','Ledger name alrady used under selected Group! Plz try another');
	echo "<script>window.location='acc_ledgerlist.php'</script>";
	}else{    
    $sql="UPDATE tbl_acledger SET code='$code',grid='$grid',sgrid=$sgrid,name='$name',description='$description' WHERE id='$legid'";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));	
    $efid=mysqli_affected_rows($con);    
    if($efid>0){ 
    $act =remove_junk(escape('Ledger name: '.$name));    
    write_activity($aid,'LED','Ledger has been updated',$act);    
    save_msg('s','Data Successfully Update!');
    echo "<script>window.location='acc_ledgerlist.php'</script>";
    }else{
    save_msg('w','Data Fail to Update!');
    echo "<script>window.location='acc_ledgerlist.php'</script>";    
    }
	}  
}
?>
<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">
    
<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Edit Ledger</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="acc_ledgeredit.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">

<div class="row ">
<div class="col-md-12">
<div class="col-md-2"></div>
<div class="col-md-8">
<?php 
if(isset($_POST['editled'])){
$ids = $_POST['editled'];
$sql="SELECT * FROM tbl_acledger WHERE id='".$ids."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
?>    
<div class="col-md-12">
<div class="row">
<div class="col-md-7">    
<div class="form-group">
<label>Name</label>
<input type="text" name="name" maxlength="45" value="<?php echo $adm['name'];?>" id="name" class="form-control" placeholder="e.g. Petty Cash" />
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="legid" autocomplete="off" readonly>    
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label>Code</label>
<input type="text" name="code" maxlength="3" value="<?php echo $adm['code'];?>" id="code" class="form-control" placeholder="e.g. 11-4040" readonly/>
</div>    
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group" >
<label>Select Group</label> 
<select class="form-control" name="grid" id="grid" onchange="getAllSubgroup(this.value)">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_acgroup ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['grid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>
</select>
<input type="hidden" maxlength="11" value="<?php echo $adm['grid'];?>" class="form-control" name="egrid" autocomplete="off" readonly>    
</div>    
</div>    
<div class="col-md-6">
<div class="form-group" >
<label>Select Sub-Group</label> 
<select class="form-control" name="sgrid" id="sgrid">
<option value="">-Select-</option>    
<?php if($adm['grid']!=''){?>
<?php
$sql="SELECT * FROM tbl_acsubgroup WHERE grid='".$adm['grid']."' ORDER BY id ASC";           
$querysc=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowsc=mysqli_fetch_array($querysc)){
?>
<?php if($rowsc['id']==$adm['sgrid']){ ?>
<option selected value="<?php echo $rowsc['id'];?>"><?php echo $rowsc['name'];?></option>
<?php }else{?>
<option value="<?php echo $rowsc['id'];?>"><?php echo $rowsc['name'];?></option>
<?php } ?>
<?php } ?>
<?php } ?>
</select>    
</div>    
</div>   
</div>    
<div class="row">
<div class="col-md-12">    
<div class="form-group">
<label>Description</label>
<textarea class="form-control" maxlength="150" rows="6" name="description" placeholder="Description"><?php echo $adm['description'];?></textarea>
</div>
</div>    
</div>    
</div>
<?php } ?>   
</div>    
<div class="col-md-2"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="submit" name="update_ledger" id="submit" class="btn btn-flat bg-purple btn-sm " value="Update"/> <a href="acc_ledgerlist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['access']=='1'){echo read_activity($aid,'LED','A');}else{echo read_activity($aid,'LED','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>    

<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function () {  
var name = new LiveValidation('name');
name.add(Validate.Presence);
var grid = new LiveValidation('grid');
grid.add(Validate.Presence);
var sgrid = new LiveValidation('sgrid');
sgrid.add(Validate.Presence);    
});
    
function getAllSubgroup(id){
$.ajax({
url: 'axe_getsub.php',
type: 'post',
data: {subgroup : id},
success:function(data) {
$('#sgrid').html(data);
}
});
};    
</script>    
<!-- /page script -->
</html>    