<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/repfunctions.php');
include ('../inc/cartfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('3','creates','R');     
$_SESSION['cuPages']='dai_expenedit.php';   
$cuPage='dai_expenedit.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='daily';
$menuh='Daily Process';
$phead='expcre';
$page='Edit Expenses';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php 
if(isset($_POST['save_edexpenses'])){

    
	if(!isset($_SESSION['axes_edexpitem'])){
    save_msg('w','No Expenses Head found!!');
	echo "<script>window.location='dai_expenlist.php'</script>";
    exit;    
    }
    $invno= remove_junk(escape($_POST['invno']));

    
    $sql="DELETE FROM tbl_trarecord WHERE invno='$invno'";
    $query=mysqli_query($con,$sql)or die(mysqli_error($con));

    $sql="DELETE FROM tbl_traledger1 WHERE invno='$invno'";
    $query=mysqli_query($con,$sql)or die(mysqli_error($con));
        
    $sql="DELETE FROM tbl_expensesde WHERE seid='$id'";
    $query=mysqli_query($con,$sql)or die(mysqli_error($con));

    $sql="DELETE FROM tbl_expenses WHERE invno='$invno'";
    $query=mysqli_query($con,$sql)or die(mysqli_error($con));
    
    $sql="DELETE FROM tbl_cheqman WHERE refinv='$invno'";
    $query=mysqli_query($con,$sql)or die(mysqli_error($con)); 


    
    $expno = remove_junk(escape($_POST['invno']));
    $apdate = remove_junk(escape($_POST['apdate']));
    $note = remove_junk(escape($_POST['note']));
    $total = total_edexpvalue();
    
    $sql="INSERT INTO tbl_expenses (invno,note,amount,apdate,brid,uid,date) VALUES ('$expno','$note','$total','$apdate','$brid','$aid','$dtnow')";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $sid=$con->insert_id;
    $efid=mysqli_affected_rows($con);
    
    if($efid>0){
    if(isset($_SESSION['axes_edexpitem'])){
    if(is_array($_SESSION['axes_edexpitem'])){
    $max=count($_SESSION['axes_edexpitem']);
    for($i=0;$i<$max;$i++){
    $expid=$_SESSION['axes_edexpitem'][$i]['expid'];
    $amount=$_SESSION['axes_edexpitem'][$i]['amount'];    
    $ref=$_SESSION['axes_edexpitem'][$i]['ref'];
        
    $sql="INSERT INTO tbl_expensesde (seid,expid,amount,ref) VALUES ('$sid','$expid','$amount','$ref')";
    mysqli_query($con,$sql) or die(mysqli_error($con));
        
    $sql="INSERT INTO tbl_trarecord (invno,dty,did,amo,cid,cty,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$expno','LE','$expid','$amount','1','LE','$ref','0','0','$apdate','$brid','$aid','$dtnow')";
    mysqli_query($con,$sql) or die(mysqli_error($con)); 

    //new code add for traledger1 cash
    $sql="INSERT INTO tbl_traledger1 (invno,dty,did,amo,cid,cty,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$expno','LE','$expid','$amount','1','LE','$ref','0','0','$apdate','$brid','$aid','$dtnow')";
    mysqli_query($con,$sql) or die(mysqli_error($con)); 
        
    }}}
    unset($_SESSION['axes_edexpdata']);
    unset($_SESSION['axes_edexpitem']);    
    $act =remove_junk(escape('Expenses No: '.$expno));    
    write_activity($aid,'EXL','Expenses has been Updated',$act);    
    save_msg('s','Data Successfully Update!');
    }else{
    save_msg('w','Data Fail to Update!');    
    }
    echo "<script>window.location='dai_expenlist.php'</script>";  
	}  




if(isset($_POST['editexp'])){

    unset($_SESSION['axes_edexpdata']);
    unset($_SESSION['axes_edexpitem']);   

    $ids = $_POST['editexp'];
    $sql="SELECT * FROM tbl_expenses WHERE id='".$ids."' ORDER BY id ASC LIMIT 1";    
    $adm=mysqli_query($con,$sql) or die(mysqli_error($con));    
    $rows=mysqli_fetch_array($adm);    

    $invno=$rows['invno'];

    $_SESSION['axes_edexpdata']['invno']=$rows['invno'];
    $_SESSION['axes_edexpdata']['note']=$rows['note'];    
    $_SESSION['axes_edexpdata']['date']=$rows['apdate'];   

    $sql="SELECT * FROM tbl_trarecord WHERE invno='$invno' ORDER BY id ASC";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));
    while ($row=mysqli_fetch_array($result)){
    addtoedexp($row['did'],$row['amo'],$row['ref']);	
    }


}    
?>    




    
<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 Expenses</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="dai_expenedit.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">    
<center>
<h3 class="page-title">EXPENSES VOUCHER EDIT</h3>
</center>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<?php 
$ldebit=get_ledgervalnew('LE1','D','N','','',$brid);
$lcredit=get_ledgervalnew('LE1','C','N','','',$brid);
$lnet=($ldebit-$lcredit);    
?>    
<center>
<a id="addcash" style="cursor: pointer;font-size: 16px;margin-right: 4px;color: red;"><span class="fa fa-plus"></span></a>    
<span style="font-size: 20px;font-weight: bold;">Balance: </span><span style="font-size: 20px;font-weight: bold;color:<?php if($lnet>0){echo 'blue;';}else{echo 'red;';}?>" id="cbal"><?php echo number_format($lnet,2); ?></span>   
</center>    
</div>   
</div>
<div class="col-md-4"></div>    
<div class="col-md-4">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><b>Expenses No:</b></span>
<input type="text" class="form-control" maxlength="15" name="invno" id="invno" value="<?php echo $rows['invno'];?>" placeholder="e.g. EXP121119101" readonly>
</div>
</div>
<div class="form-group" >
<div class="input-group">
<span class="input-group-addon"><b>Date:</b></span>
<input type="text" class="form-control datetimepicker" name="apdate" id="apdate" value="<?php echo $rows['apdate'];?>" placeholder="Date:" autocomplete="off">
</div>
</div>    
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group" >
<label>Expenses Head</label>
<div class="input-group">
<select class="form-control select2" name="expid" id="expid">
<option value="">-Select-</option>
<?php
$sql="SELECT * FROM tbl_acledger WHERE grid IN (8,9) AND sgrid IN (20,21) ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</select>
<span class="input-group-addon"><a id="aexpi"><span class="fa fa-plus"></span></a></span>     
</div>   
</div>
</div>
<div class="col-md-3">
<div class="form-group" >
<label>Ref</label>
<input type="text" maxlength="25" class="form-control" name="ref" id="ref" placeholder="e.g. Sumon" autocomplete="off">    
</div>    
</div>
<div class="col-md-2">
<div class="form-group">
<label>Amount</label>    
<input type="text" maxlength="6" class="form-control" name="amount" id="amount"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div> 
</div>
<div class="col-md-1">
<div class="form-group">
<label>&nbsp;</label>    
<input type="button" id="addexp" class="btn btn-flat bg-red" value="Add"/>    
</div>
</div>    
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table-striped">
<thead>    
<th style="width:40px; text-align:center">SN</th>
<th>Expenses Head</th>
<th>Amount</th>
<th>Ref</th>
<th style="width:40px; text-align:center"><a class="empty" style="cursor: pointer;"><i class="fa fa-trash"></i></a></th>    
</thead>
<tbody id="itemdata">

</tbody>
<tfoot id="itemfoot">

</tfoot>    
</table> 
</div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Note</label>
<textarea class="form-control" name="note" id="note" maxlength="250" rows="3" placeholder="e.g. Note"><?php echo$_SESSION['axes_edexpdata']['note'];?></textarea>
</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="button" id="expreset" class="btn btn-flat bg-red btn-sm " value="Reset"/>    
<input type="submit" name="save_edexpenses" id="submit" class="btn btn-flat bg-purple btn-sm" value="Save"/> <a href="dai_expenlist.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,'EXL','A');}else{echo read_activity($aid,'EXL','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>

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

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var apdate = new LiveValidation('apdate');
apdate.add(Validate.Presence);
});
    
ReadData();
function ReadData(){
$.ajax({
url: "dai_edexpview.php",
method: "POST",
data:{ 
body:1
},
success: function(data){
$('#itemdata').html(data);
}
})

$.ajax({
url: "dai_edexpview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})    
};    

function ReadFoot(){
$.ajax({
url: "dai_edexpview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})	
}
 
$(document).on('blur change', '#apdate', function() {
apdate=$(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
edexpdate: apdate
},
dataType: 'json',
success: function(data){
$('#apdate').val(data[0]);
}
});     
});    

$(document).on('blur', '#note', function() {
note = $(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
edexpnote: note
},
dataType: 'json',
success: function(data){
$('#note').val(data[0]);
}
});     
});    

$(document).on('click','.empty',function() {
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
edexpdata: 1
},
success: function(data){
ReadData();    
}
});    
});    

$(document).on('click', '.remove', function () {
var id = $(this).attr('id');
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
edexpremove: id
},
success: function(data){
ReadData();
}
});    
});    

$(document).on('click','#expreset',function() {
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
edexpclear: 1
},
success: function(data){
$("#note").val("");   
ReadData();    
}
});    
});     
    
$(document).on('click', '#addexp', function() {
var expid = $('#expid').val();    
var amount = parseFloat($('#amount').val());
var ref = $('#ref').val();    
toastr.options = {'positionClass': 'toast-top-center'};    

if(expid == '-Select-' || expid == ''){
toastr.info('Please Select Expen!');
return;
}    
    
if(isNaN(amount) != false || amount <= 0){
toastr.info('Please Enter Amount!');
return;
}

$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {edexpid:expid, amount:amount, ref:ref},
success:function(response){
$("#expid").val("").trigger("change");    
$('#ref').val("");
$('#amount').val("");    
ReadData();
}
});    
    
});
    
$(document).on('click', '#aexpi', function() {
$.ajax({
url: "axe_additem.php",
method: "POST",
data:{addexpi:1},
success: function(data){
$('#addsitem').html(data);
$('.right-side-add').toggle('slide', { direction: 'right' }, 300);     
}
})       
});
    
$(document).on('click', '#addcash', function() {
$.ajax({
url: "axe_additem.php",
method: "POST",
data:{addcash:1},
success: function(data){
$('#addsitem').html(data);
$('.right-side-add').toggle('slide', { direction: 'right' }, 300);     
}
})       
});    

$(document).on('click', '#closepop', function() {    
if($('.right-side-add').is(':visible')) { $('.right-side-add').toggle('slide', { direction: 'right' }, 300); }
$('#addsitem').html('');    
});    
</script>    
<!-- /page script -->
</html>    