<?php 
include ("kfunctions.php");
adminall("overadmin","priaccount","secaccount");
?>
<html>
<head>
<?php kheader(); ?>

<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script src="js/sorttable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

</head>
<body>


    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>                    
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">

            <div class="container-fluid">

                <!-- Page Heading -->
                
                <!-- /.row -->

                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                            

<form method='post'>
    <div class='input-append date form_date col-lg-3' data-date='' data-date-format='yyyy-mm-dd' data-link-field='dtp_input2' data-link-format='yyyy-mm-dd'>
<input size='16' type='text' style='cursor:pointer;' name='date1' placeholder='Date 1 (From)' class='form-control readonly'>
<span class='add-on'><i class='icon-th'></i></span>
</div>

<div class='input-append date form_date col-lg-3' data-date='' data-date-format='yyyy-mm-dd' data-link-field='dtp_input2' data-link-format='yyyy-mm-dd'>
<input size='16' type='text' name='date2' style='cursor:pointer;' placeholder='Date 2 (To)' class='form-control readonly'>
<span class='add-on'><i class='icon-th'></i></span>
</div>
<input type='submit' name='submit400' class='btn btn-primary' name='submit' value='Filter'>
</form>


<?php
  include ("connect.php");
$ouruser = $_SESSION["rolenumber"];
$role = $_SESSION["role"];

if($role=="overadmin"){
$all = "";

}else{
  $all = " and ouruser='$ouruser'";
}

if(isset($_POST["submit400"])){
  $date1 = $_POST["date1"];
$date2 = $_POST["date2"];

useractivity("(Date1 ".$date1.")(Date2 ".$date2.") have been selected","6");

  $daterange = " and (datez>='$date1' && datez<='$date2') order by autoid desc";
}else{
  $daterange = " order by autoid desc limit 0,10";
}

  $result = $dbh->query("select * from expenses where autoid>0 $all $daterange");
$row = $result->fetchObject();
$count = $result->rowCount();


$result5 = $dbh->query("select sum(cost) from expenses where autoid>0 $all $daterange");
$row5 = $result5->fetch();


echo "
<div class='panel panel-yellow'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>View expenses ( Expenses : ".$count.", Total sum : ".number_format($row5[0])." )</h3></div>
<div class='panel-body'>";

if(isset($_POST["submit45"])){
 $slipid = $_POST["slipid"];
 $banked = $_POST["banked"];
 $comment = $_POST["comment"];
 $expid = $_POST["expid"];
include ("connect.php");

$result70 = $dbh->query("select * from expenses where expid='$expid'");
$row70 = $result70->fetchObject();
$count70 = $result70->rowCount();

if($banked > $row70->remain){
echo "<div class='alert alert-danger'>Sorry: Amount is greater than the expense cost. </div>";

}else{

if($row70->remain>0){
$amount = $row70->remain - $banked;
$updateexpenses = $dbh->query("update expenses set remain='$amount' where expid='$expid'");
echo "<script>window.location='viewexpenses.php'</script>";

}



$insertbanked = $dbh->query("insert into banked (expid,amount,comment,slipid) values('$expid','$banked','$comment','$slipid')");
echo "<div class='alert alert-success'>Success: Banked has been submitted. </div>";


}


}


echo "
<a href='expenses.php'>Add another expense </a>
    <table class='table table-bordered table-hover table-striped sortable'>
<thead>
  <tr>
    
    <th>Expense date</th>
    <th>Bal <span style='color:green;'>(Banked)</span></th>
    <th>Spent</th>
  <th>Authorised by</th>
  <th>Released by</th>
  <th>Comment</th>
  <th>Banked</th>
  </tr>
</thead>
<tbody>";
  
if($count>0){
do{

$result59 = $dbh->query("select sum(amount) from banked where expid='$row->expid'");
  $row59 = $result59->fetch();
  $count59 = $result59->rowCount();

if($count59>0){
  $title2 = " <span style='color:green;'>(".number_format($row59[0]).")</span>";
}else{
  $title2 = "";
}


  $result5 = $dbh->query("select * from users where rolenumber='$row->ouruser'");
$row5 = $result5->fetchObject();

$result16 = $dbh->query("select sum(cost) from expenses2 where expid='$row->expid'");
  $row16 = $result16->fetch();
  $count16 = $result16->rowCount();

  if($count16>0){
  $cost = $row->remain;
  $title = number_format($row16[0]);

  }

echo "
<tr>
<td>".$row->datez."</td>
<td>".number_format($row->remain)."

<input type='hidden' id='cost".$row->autoid."' value='".$row->cost."'>
";

$result56 = $dbh->query("select sum(amount) from banked where expid='$row->expid'");
$row56 = $result56->fetch();

echo " <span style='color:green;'>(+ ".number_format($row56[0]).")</span>"; echo "</td>
<td>".$title."</td>
<td>".$row->requestfrom."</td>
<td>".$row5->firstname." ".$row5->lastname."</td>
<td>".$row->comment."</td>
<td>";

$result3 = $dbh->query("select * from expenses where expid='$row->expid' order by autoid desc");
$row3 = $result3->fetchObject();
if($row3->cost>0){
echo "
<form method='post'>
<input type='hidden' name='expid' value='".$row->expid."'>
<input type='text' name='slipid' class='form-control' placeholder='Slip ID' required>
<input type='number' name='banked' max='1000000000' min='0' class='form-control' id='amount".$row->autoid."' placeholder='Amount' required>
<textarea name='comment' class='form-control' placeholder='Comment'></textarea>
<input type='submit' class='btn btn-sm btn-primary' name='submit45' value='Submit'>
</form>";
}else {echo "So far, Cleared";}
echo "
</td>
</tr>
";
}while($row = $result->fetchObject());
}else{
  echo "So far, there are no expenses";
}
  ?>
  </tbody>
</table>

</div></div>


                            </div> </div>
                    </div>
                </div>
                <!-- /.row -->

               
            </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->
        <?php useractivity("on page","6"); ?>

    </div>
    <!-- /#wrapper -->

    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript">
    $('.form_datetime').datetimepicker({
        //language:  'fr',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });
    $('.form_date').datetimepicker({
        language:  'en',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    });
    $('.form_time').datetimepicker({
        language:  'en',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 1,
        minView: 0,
        maxView: 1,
        forceParse: 0
    });
</script>


</body>

</html>