<?php include ("../core/kfunctions.php");
//role("dos,hmp");
?>
<html>
<head>
<?php 

echo "

<!-- jQuery -->
<script src='../../plugins/jquery/jquery.min.js'></script>
<!-- Bootstrap -->
<script src='../../plugins/bootstrap/js/bootstrap.bundle.min.js'></script>

";

?>
<script src='../../js/jquery.js'></script>
<script src='../../js/thousandseparator.js'></script>

<style type="text/css">
table{ width: 100%;border-collapse: collapse;background:  }
tr:nth-of-type(odd){background: #eee;}
th{background: #333;color: #ffffff;font-weight: bold;}
td, th{padding: 6px;text-align: left;}  
@media
only screen and (max-width: 760px),
(min-device-width:768px) and (max-device-width:1024px ){
 
table,thead,tbody,th,td,tr{display: block;}
thead tr{position: absolute;top: -9999px;left: -9999px;}
tr{border: 1px solid #ccc; }
.td{border: none;border-bottom: 1px solid #eee;position: relative;padding-left: 50%;}
.td:before{position: absolute;top:6px;left: 6px;width: 45%;padding-right: 10px;white-space: nowrap;}
.td:nth-of-type(1):before{content: "No";}
.td:nth-of-type(2):before{content: "Given to";}
.td:nth-of-type(3):before{content: "Location";}
.td:nth-of-type(4):before{content: "Date";}
.td:nth-of-type(5):before{content: "Amount";}
.td:nth-of-type(6):before{content: "Category";}
.td:nth-of-type(7):before{content: "Details";}

} 

</style>

<?php kheader(); ?>

<script src="../js/print.js"></script>


<?php 
require_once("../core/connect.php");

?>
<link href="../../css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script src="../../js/sorttable.js"></script>

<script>
function show_dis(ai){$("#dis_edit"+ai).toggle(500);}
function display_row(ai){$("#rows"+ai).toggle();}
function show_2(ai){$("#attachment"+ai).toggle(500);}  
function show_dis2(ai){$("#dis_edit2"+ai).toggle(500);}
function display_row2(ai){$("#row2"+ai).toggle();}
function get_no(ai){document.getElementById("no").value=ai;}
</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 -->
                <div>&nbsp;</div>



<div id='myDiv'>


<form method='post'>
<div class="col-lg-2"> 
<select class='form-control' name='row_expense' onchange="this.form.submit()">
<option value='0'>Rows</option>
<?php for($h=1;$h<=10;$h++){ echo "<option value='".$h."'>".$h."</option>"; }?>
</select></div></form>

<form method='post'>
<div class='col-lg-12'><br></div>

<div class="col-lg-12">

<?php
if (isset($_POST['submit_expenses'])) {

$rp=$_POST["row_expense"];

//Plans
if($rp!=0){ for($x=1;$x<=$rp;$x++){
$recipient=$_POST["recipient".$x.""];
$date=$_POST["date".$x.""];

$amount=str_replace(',','',$_POST["amount".$x.""]);
$category=$_POST["category".$x.""];
$detail=$_POST["detail".$x.""];

$result_userss=$dbh->query("select * from users where rolenumber='$rolenumber'");
$row_userss=$result_userss->fetchObject();

if(!empty($date) && !empty($amount) ){
$yy=date("Y"); $fyy=substr($yy,2,2);
$mm=date("m"); $dd=date("d");
$hi=date("h"); $mi=date("i");
$sa=date("sa"); $fsa=substr($sa,0,2);   
$expid="ex".$rolenumber.$fyy.$mm.$dd.$hi.$mi.$fsa.$x;

$insert_expenses=$dbh->query("insert into expenses (expid,cost,requestfrom,comment,ouruser,datez,category) values('$expid','$amount','$recipient','$detail','$rolenumber','$date','$category')");

$stage='56';
$ip_ad2=$_SERVER['REMOTE_ADDR'];
$kbrowser2=$_SERVER['HTTP_USER_AGENT'];
$currentpage2=$_SERVER['SCRIPT_NAME'];
$userid2 = $_SESSION["rolenumber"];
$activity2="@".$userid2." added an expense of ".$amount." to ".$recipient." in the category of ".$category."  and reason ".$detail;

$insertactivity = $dbh->exec("insert into useractivity (activity,userid,deviceid,browser,currentpage,stage) 
    values('$activity2','$userid2','$ip_ad2','$kbrowser2','$currentpage2','$stage')");

} } 
echo "<div class='alert alert-success'>Success : Your expenses have been submitted successfully. </div>";}

}

?>

<div class='panel panel-yellow'><!--Initiative-->
<div class='panel-heading'>    

              <div class="card-header d-flex p-0">
                <h3 class="card-title p-3"><input type='submit' name='submit_expenses' style='float:right;' class='btn btn-md btn-primary' value='Submit'></h3>
                <ul class="nav nav-pills ml-auto p-2">
                  <li class="nav-item"><a class="nav-link active" href="entry_expense.php" >Expenses</a></li>
                  <li class="nav-item"><a class="nav-link active" href="expense_categories.php">Categories</a></li>  
                  <li class="nav-item"><a class="nav-link active" href="view_expenses.php">View expenses</a></li>

                </ul>
              </div>

          </div><!--  close panel-heading -->


<form method='post'>
<?php if(isset($_POST["row_expense"])){$rp=$_POST["row_expense"];}else{$rp=1;} ?>

<input type='hidden' name='row_expense' value='<?php echo $rp; ?>'>

<div class='col-lg-12'><br></div>
<div class='col-lg-12'><br></div>

            <div class="card">
<div class='card-body'>
<div class="tab-content">
    <div class="tab-pane active"  id="tab_1">
<table class='table table-bordered table-hover table-striped sortable' border='1'>
    <thead>
    <tr>
    <th width='3%'>No.</th>
    <th width='15%'>Given to</th>
    <th width='2%'>Date</th>
    <th width='15%'>Amount</th>
    <th width='15%'>Category</th>
    <th width="22.7%">Details</th>
    </tr>
    </thead>
                  <tbody>

<?php 
                  if($rp!=0){ for($x=1;$x<=$rp;$x++){
                  echo "<tr>
                    <td class='td'>".$x.". </td>
                     <td class='td'><input class='form-control' name='recipient".$x."' required placeholder='Recipient'>
                    </input></td>

                    <td class='td'>                                   
<input style='cursor:pointer;' type='date' name='date".$x."' placeholder='Date' class='form-control readonly' required></input>        
                    </td>

                    <td class='td'><input type='number' class='form-control' name='amount".$x."' placeholder='Amount'></input></td>

                    <td class='td'><select class='form-control' name='category".$x."'>
                      <option value=''>Category</option>";

$result_scrapps=$dbh->query("select * from scrap where type='expense'");
$row_scrapps=$result_scrapps->fetchObject();
$count_scrapps=$result_scrapps->rowCount();

if($count_scrapps>0){ do{
echo "<option value='".$row_scrapps->item."'>".$row_scrapps->item2."</option>";
}while($row_scrapps=$result_scrapps->fetchObject());
}else{echo "<option value=''>There is no data as yet. </option>";}

echo "
</select></td>

<td class='td'><textarea class='form-control' name='detail".$x."'  placeholder='Leave a Note'></textarea></td></tr>"; } }
else{echo "<div class='col-lg-12'>Add rows for plans</div>";}

?>
</tbody>
</table>
</div>
</form>


              <div class="tab-pane" id="tab_2">
 <form method='post'>
<div class='row' style='color:maroon;'>
<div class="col-md-1" style='margin-right:-35px;'><b>From :</b></div>
<div class="col-md-2"><input type='date' name='datefrom' class='form-control'></div>
<div class="col-md-1" style='margin-right:-60px;'><b>To :</b></div>
<div class="col-md-2"><input type='date' name='dateto' class='form-control'></div>
<div class='col-lg-2'>
<select class='form-control' name='category'>
<option value=''>Category</option>
<?php
$result_scrap=$dbh->query("select * from scrap where type='expense'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){ do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap=$result_scrap->fetchObject());
}else{echo "<option value=''>There is no data as yet. </option>";}

?>
</select></div>

 <div class='col-lg-1'>
<input type='submit' style='float:right;' class='btn btn-sm btn-success' name='filter' value='Filter'></div>
</div>
</form>
<?php
if(isset($_POST["filter"])){
$datefrom=$_POST["datefrom"];
$dateto=$_POST["dateto"];
if(!empty($_POST["category"])){$catid=$_POST["category"]; $dacat="and category='$catid'"; }
else{$dacat='';}
if(!empty($_POST["spentby"])){$userid=$_POST["spentby"]; $dauser="and requestfrom='$userid'"; }
else{$dauser='';}

if(!empty($datefrom)&&!empty($dateto)){$dadate="and (datez>='$datefrom' && datez<='$dateto')"; }
elseif(!empty($datefrom)&&empty($dateto)){$dadate="and datez='$datefrom'"; }
elseif(empty($datefrom)&&!empty($dateto)){$dadate="and datez='$dateto'"; }
elseif(empty($datefrom)&&empty($dateto)){$dadate='';}
}else{$dadate='and autoid!=0'; $dauser=''; $dacat='';}
?>
               
                  <table>
                    
                    <tr>
<?php
$result_scrap=$dbh->query("select * from scrap where type='expense' order by autoid desc");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();
if ($count_scrap>0) {do{
 echo "<th width='30%'>".$row_scrap->item2."</th>"; 
?>
<td width="5%"></td>
<td class='mycategorytd'>
<?php
$result_expenses=$dbh->query("select sum(cost) from expenses where category='$row_scrap->item' and status=2 $dacat $dadate $dauser");
$count_expenses=$result_expenses->rowCount();
$row_expenses=$result_expenses->fetch();
if ($count_expenses>0){$total=$row_expenses['0'];}else{$total=0;}
echo "<span style='color:maroon'>".number_format($total)."</span></td></tr>";
}while($row_scrap=$result_scrap->fetchObject()); }
?>         
</table>  
              </div><!-- /.card  -->
 <div class='col-lg-12'><br></div>             
              </div><!-- /.card-body -->


      </div><!--  close panel panel-yellow -->

            <!-- /.container-fluid -->
<?php useractivity("on page", "56"); ?>

        <!-- /#page-wrapper -->


    <!-- /#wrapper -->
    <script>
function checkall(){
<?php
include("../core/connect.php");
$result_citadel=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass'");
$row_citadel=$result_citadel->fetchObject();
$count_citadel=$result_citadel->rowCount();

if($count_citadel>0){
    do{
?>
if(document.getElementById('checkallz').checked){
document.getElementById('cidcheck<?php echo $row_citadel->autoid; ?>').checked=true;
}else{
document.getElementById('cidcheck<?php echo $row_citadel->autoid; ?>').checked=false; 
}
<?php
}while($row_citadel=$result_citadel->fetchObject());
}
?>
}
</script>

    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="../js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../js/jquery-1.8.3.min.js" charset="UTF-8"></script>

</div><!--  close col-lg-12 -->
</div><!--  close myDiv -->
</div><!--  close container fluid -->
</div><!--  close page wrapper -->
</div><!--  close wrapper -->

</body>

</html>
