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

if(isset($_POST['tfdate'])){
$fdate=$_POST['tfdate'];	
}else{
$fdate=date('Y-m-d', strtotime('-15 days', time()));	
}
if(isset($_POST['ttdate'])){
$tdate=$_POST['ttdate'];	
}else{
$tdate=$today;	
}

if(isset($_POST['delexp'])){
$id=$_POST['delexp'];
$invno= get_fild_data('tbl_expenses',$id,'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 id='$id'";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
    
$efid=mysqli_affected_rows($con);    
if($efid>0){
$act =remove_junk(escape('Expenses no: '.$invno));    
write_activity($aid,'EXL','Expenses has been deleted',$act);        
save_msg('s','Expenses Successfully Deleted!!!');
}else{
save_msg('w','Expenses Fail to Delete!!!');    
}
echo "<script>window.location='dai_expenlist.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">Expenses Record</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>    
<div class="col-md-12 table-responsive">
<table class="table table-bordered table-striped" id="datarec">
<thead class="text-uppercase">
<tr>
<th style="width:40px;">SN</th>
<th>Date</th>    
<th>Expenses No</th>
<th>Amount</th>
<th>Note</th>    
<th style="width:40px; text-align:center;">Action</th>    
</tr>
</thead>    
<tbody>
<?php
$sql="SELECT * FROM tbl_expenses WHERE apdate BETWEEN '$fdate' AND '$tdate' AND brid='$brid' ORDER BY apdate DESC";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<td class="center"><?php echo count_id();?></td>
<td><?php echo date("d M Y", strtotime($row['apdate']));?></td>        
<td><?php echo $row['invno'];?></td>
<td><?php echo numtolocal($row['amount'],'');?></td>
<td><?php echo $row['note'];?></td>    
<td nowrap="">
<?php if(get_pagesecurity('3','prints','P')){ ?>     
<a class="btn btn-flat bg-purple details-invoice" href="#" id="inv_<?php echo $row['id']; ?>"><i class="fa fa-eye cat-child"></i></a>    
<!--<a class="btn btn-flat bg-purple" href="#" onclick="edit_item('ED_<?php echo $row['id'];?>')"><i class="fa fa-edit"></i></a>-->
<?php } 
if(get_pagesecurity('3','deletes','P')){ ?> 

<a class="btn btn-flat bg-purple" href="#" onclick="edit_item('ED_<?php echo $row['id'];?>')"><i class="fa fa-edit"></i></a>
<a class="btn btn-flat bg-purple" href="#" onclick="remove_item('DL_<?php echo $row['id'];?>')"><i class="fa fa-trash"></i></a>
<form action="dai_expenlist.php" id="DL_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="delexp" value="<?php echo $row['id']; ?>" />
</form>

<form action="dai_expenedit.php" id="ED_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="editexp" value="<?php echo $row['id']; ?>" />
</form> 

<?php } ?>  
</td>    
</tr>    
<?php } ?>    
</tbody>   
</table>
</div>
<div class="clearfix" ></div>  
<div class="row"style="margin-top: 15px" >
<div class="col-md-12 table-responsive">    
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<?php if(get_pagesecurity('3','creates','P')){ ?>    
<a href="dai_expensescreate.php" class="btn btn-flat bg-purple">Add Expenses</a>
<?php } ?>
</div>
</div>    
</div>    
</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">
<!-- tools box -->
<div class="pull-right box-tools">
<a class=" pull-right" data-widget="collapse" style="margin-right: 5px;">
<i class="fa fa-minus"></i></a>
</div>
<!-- /. tools -->
<i class="fa fa-filter" aria-hidden="true"></i>    
<h3 class="box-title">Date Range Filter</h3>
</div>
<!-- /.box-header -->
<div class="box-body" >

<form action="#" 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-1"></div>
<div class="col-md-10">    

<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Date From</label>
<div class="input-group date datetimepicker">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" name="tfdate" value="<?php if(isset($_POST['tfdate'])){echo $_POST['tfdate']; }else{echo date('Y-m-d', strtotime('-15 days', time()));} ?>"  class="form-control" placeholder="Date From" readonly="true">
</div>
</div>        
</div>
<div class="col-md-6">
<div class="form-group" >
<label>Date To</label>
<div class="input-group date datetimepicker">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" name="ttdate" value="<?php if(isset($_POST['ttdate'])){echo $_POST['ttdate']; }else{echo $today;} ?>"  class="form-control" placeholder="Date To" readonly="true">
</div>    
</div>    
</div>    
</div>     
    
</div>   
<div class="col-md-1"></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="date_filter" id="submit" class="btn btn-flat bg-purple btn-sm " value="Submit"/>
</div> 
</div>     
</form>    
    
</div>
</div>
</div>

</div>        
<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/print.php'); ?>    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function() {
$('#datarec').DataTable({stateSave: true});   
});
function edit_item(id) {
bootbox.confirm({
title: "Confirm",
message: "Are you sure to Update?",
buttons: {
confirm: {
label: 'Yes',
className: 'btn-flat bg-purple'
},
cancel: {
label: 'Cancel',
className: 'btn-flat bg-gray'
}
},
callback: function (result) {
if (result == true) {
document.getElementById(id).submit();
}
}
});
}
function remove_item(id) {
        bootbox.confirm({
            title: "Confirm",
            message: "Are you sure to delete ?",
            buttons: {
                confirm: {
                    label: 'Yes',
                    className: 'btn-flat bg-purple'
                },
                cancel: {
                    label: 'Cancel',
                    className: 'btn-flat bg-gray'
                }
            },
            callback: function (result) {
                if (result == true) {
                    document.getElementById(id).submit();
                }
            }
        });

    }
    
$(".details-invoice").click(function (e) {
id_arr = $(this).attr('id');
id = id_arr.split("_");

$.ajax({
url: 'dai_viewexplist.php',
method: "POST",
data:{ 
invid: id[1]
},
success: function(data){
$('#listitem').html(data);
pagelist();    
}
});    
    
$.ajax({
url: 'dai_viewexp.php',
method: "POST",
data:{ 
print: id[1]
},
success: function(data){
$('#invhold').html(data);
sethead();    
}
});
    
$('.right-side').toggle('slide', { direction: 'right' }, 300);    

e.preventDefault();
});

$(document).on('click', '#closepop', function() {
if($('.right-side').is(':visible')) { $('.right-side').toggle('slide', { direction: 'right' }, 300); }   
});    

$('#listitem').on('click','li', function(){
   $(this).addClass('active').siblings().removeClass('active');
});
    
$(document).on('click', '.invexpv', function() {
id_arr = $(this).attr('id');
id = id_arr.split("_");
var ids = id[1];
$.ajax({
url: 'dai_viewexp.php',
method: "POST",
data:{ 
print: ids
},
beforeSend: function() {
$("#invhold").fadeOut('slow');
},    
success: function(data){    
$('#invhold').html(data);
$("#invhold").fadeIn('slow');    
sethead();    
}
});     
});    
     
</script>    
<!-- /page script -->
</html>    