<?php

session_start();


if(!isset($_SESSION['user_id'])){

    header("Location:login.php");
    exit;

}


require_once "config.php";



// ==========================
// FILTERS
// ==========================


$search=$_GET['search'] ?? '';

$category=$_GET['category'] ?? '';

$vendor=$_GET['vendor'] ?? '';

$payment=$_GET['payment'] ?? '';

$start=$_GET['start'] ?? '';

$end=$_GET['end'] ?? '';

$min=$_GET['min'] ?? '';

$max=$_GET['max'] ?? '';





$where=[];

$params=[];





// ==========================
// SEARCH
// ==========================


if($search!=""){


$where[]="

(

expenses.description LIKE ?

OR vendors.vendor_name LIKE ?

OR expense_categories.category_name LIKE ?

OR expense_items.item_name LIKE ?

OR expense_items.registration_no LIKE ?

)

";


$params[]="%$search%";

$params[]="%$search%";

$params[]="%$search%";

$params[]="%$search%";

$params[]="%$search%";


}






// ==========================
// CATEGORY FILTER
// ==========================


if($category!=""){


$where[]="expenses.category_id=?";

$params[]=$category;


}







// ==========================
// VENDOR FILTER
// ==========================


if($vendor!=""){


$where[]="expenses.vendor_id=?";

$params[]=$vendor;


}







// ==========================
// PAYMENT FILTER
// ==========================


if($payment!=""){


$where[]="expenses.payment_method=?";

$params[]=$payment;


}






// ==========================
// DATE FILTER
// ==========================


if($start!=""){


$where[]="expenses.expense_date >= ?";

$params[]=$start;


}



if($end!=""){


$where[]="expenses.expense_date <= ?";

$params[]=$end;


}







// ==========================
// AMOUNT FILTER
// ==========================


if($min!=""){


$where[]="expenses.total >= ?";

$params[]=$min;


}




if($max!=""){


$where[]="expenses.total <= ?";

$params[]=$max;


}


// ==========================
// PAGINATION
// ==========================

$limit = 10;

$page = $_GET['page'] ?? 1;

$page = max(1,(int)$page);

$offset = ($page - 1) * $limit;


// ==========================
// TOTAL RECORDS FOR PAGINATION
// ==========================


$countSql="

SELECT COUNT(*)

FROM expenses


LEFT JOIN expense_categories

ON expenses.category_id = expense_categories.id


LEFT JOIN vendors

ON expenses.vendor_id = vendors.id


LEFT JOIN expense_items

ON expenses.item_id = expense_items.id


";


if($where){

$countSql.=" WHERE ".implode(" AND ",$where);

}



$stmt=$pdo->prepare($countSql);

$stmt->execute($params);


$totalRecords=$stmt->fetchColumn();



$totalPages=ceil($totalRecords/$limit);



// ==========================
// MAIN QUERY
// ==========================


$sql="

SELECT


expenses.*,


expense_categories.category_name,


vendors.vendor_name,


expense_items.item_name,


expense_items.registration_no



FROM expenses



LEFT JOIN expense_categories

ON expenses.category_id = expense_categories.id




LEFT JOIN vendors

ON expenses.vendor_id = vendors.id




LEFT JOIN expense_items

ON expenses.item_id = expense_items.id



";






if($where){


$sql.=" WHERE ".implode(" AND ",$where);


}





$sql.=" 

ORDER BY expenses.id DESC

LIMIT $limit OFFSET $offset

";







$stmt=$pdo->prepare($sql);


$stmt->execute($params);


$expenses=$stmt->fetchAll(PDO::FETCH_ASSOC);









// ==========================
// SUMMARY CARDS
// ==========================



$total=$pdo->query("

SELECT COALESCE(SUM(total),0)

FROM expenses

")->fetchColumn();






$thisMonth=$pdo->query("

SELECT COALESCE(SUM(total),0)

FROM expenses


WHERE MONTH(expense_date)=MONTH(CURRENT_DATE())

AND YEAR(expense_date)=YEAR(CURRENT_DATE())


")->fetchColumn();






$records=$pdo->query("

SELECT COUNT(*)

FROM expenses


")->fetchColumn();







// ==========================
// CATEGORIES
// ==========================


$categories=$pdo->query("

SELECT *

FROM expense_categories

WHERE status=1

ORDER BY category_name

")->fetchAll(PDO::FETCH_ASSOC);









// ==========================
// VENDORS
// ==========================


$vendors=$pdo->query("

SELECT *

FROM vendors

WHERE status=1

ORDER BY vendor_name

")->fetchAll(PDO::FETCH_ASSOC);








// ==========================
// ITEMS
// ==========================


$items=$pdo->query("

SELECT *

FROM expense_items

WHERE status=1

ORDER BY item_name

")->fetchAll(PDO::FETCH_ASSOC);



?>
<!DOCTYPE html>
<html>

<head>

<meta name="viewport" content="width=device-width,initial-scale=1">

<title>
Expenses Management
</title>


<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">


<link rel="stylesheet" href="assets/css/sidebar.css">


<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">


<style>


*{

margin:0;
padding:0;
box-sizing:border-box;
font-family:Poppins, sans-serif;

}



body{

background:#f4f7f9;

transition:.3s;

}



body.dark{

background:#111827;

color:white;

}



.content{

margin-left:260px;

padding:25px;

}





.topbar{

display:flex;

align-items:center;

justify-content:space-between;

margin-bottom:20px;

}





.mobile-btn{

display:none;

font-size:25px;

cursor:pointer;

color:#065f46;

}



.toggle{

font-size:24px;

cursor:pointer;

color:#059669;

}


/* Expense Action Buttons */

.view,
.edit,
.delete{

width:28px;
height:28px;
display:inline-flex;
align-items:center;
justify-content:center;
border-radius:8px;
color:white;
text-decoration:none;
font-size:12px;
margin:2px;

}


.view{

background:#059669;

}


.edit{

background:#f59e0b;

}


.delete{

background:#ef4444;

}


.view:hover,
.edit:hover,
.delete:hover{

transform:scale(1.1);

}


.view i,
.edit i,
.delete i{

font-size:12px;

}

.btn{

background:#059669;

color:white;

padding:10px 15px;

border-radius:10px;

text-decoration:none;

display:inline-flex;

align-items:center;

gap:8px;

cursor:pointer;

border:none;

}



.btn:hover{

background:#047857;

}


.pagination{

display:flex;

justify-content:center;

gap:8px;

margin-top:20px;

flex-wrap:wrap;

}


.pagination a{

padding:8px 14px;

background:#059669;

color:white;

border-radius:8px;

text-decoration:none;

font-size:14px;

}


.pagination a.active{

background:#065f46;

font-weight:bold;

}


.pagination a:hover{

opacity:.8;

}


.pdf{

background:#dc2626;

}



.excel{

background:#16a34a;

}







.cards{

display:grid;

grid-template-columns:
repeat(auto-fit,minmax(220px,1fr));

gap:15px;

margin-bottom:20px;

}





.card{


background:white;

padding:18px;

border-radius:15px;

display:flex;

align-items:center;

gap:15px;

box-shadow:0 5px 20px #0002;

}



body.dark .card{

background:#1f2937;

}





.icon{


width:45px;

height:45px;

border-radius:12px;

background:#ecfdf5;

color:#059669;

display:flex;

align-items:center;

justify-content:center;

font-size:20px;

}




.number{

font-size:22px;

font-weight:bold;

color:#059669;

}





.filter-box{


background:white;

padding:20px;

border-radius:15px;

box-shadow:0 5px 20px #0001;

margin-bottom:20px;

}




body.dark .filter-box{

background:#1f2937;

}






.filter{

display:grid;

grid-template-columns:

repeat(auto-fit,minmax(180px,1fr));

gap:12px;

}







input,
select{


padding:12px;

border-radius:10px;

border:1px solid #ddd;

outline:none;

width:100%;

}




button{

background:#065f46;

color:white;

border:none;

padding:12px;

border-radius:10px;

cursor:pointer;

}







.table-box{

background:white;

padding:8px;

border-radius:8px;

overflow:auto;

box-shadow:0 2px 8px #0001;

font-size:12px;

}


body.dark .table-box{

background:#1f2937;

}






table{

width:100%;

border-collapse:collapse;

}





th{

background:#065f46;

color:white;

padding:12px;

text-align:left;

}





td{

padding:12px;

border-bottom:1px solid #eee;

}





body.dark td{

border-color:#374151;

}







.statement-link{

color:#059669;

font-weight:600;

text-decoration:none;

display:inline-flex;

align-items:center;

gap:8px;

}



.statement-link:hover{

color:#065f46;

}





.statement-link i{

width:28px;

height:28px;

background:#ecfdf5;

border-radius:8px;

display:flex;

align-items:center;

justify-content:center;

}





small{

color:#64748b;

}






.badge{

background:#dcfce7;

color:#166534;

padding:5px 12px;

border-radius:20px;

font-size:12px;

}





.action a{

margin-right:10px;

font-size:18px;

}





.view{

color:#059669;

}



.edit{

color:#0284c7;

}



.delete{

color:#dc2626;

}






.overlay{

display:none;

position:fixed;

inset:0;

background:#0008;

z-index:999;

}



.overlay.show{

display:block;

}







@media(max-width:768px){



.content{

margin-left:0;

padding:15px;

}





.mobile-btn{

display:block;

}






.sidebar{

left:-260px;

transition:.3s;

}



.sidebar.active{

left:0;

}







table,
thead,
tbody,
th,
td,
tr{

display:block;

}




thead{

display:none;

}




tbody tr{


background:white;

margin-bottom:15px;

padding:15px;

border-radius:15px;

box-shadow:0 5px 20px #0002;


}



body.dark tbody tr{

background:#1f2937;

}







tbody td{

display:flex;

justify-content:space-between;

border:none;

padding:10px;

}



tbody td:before{

font-weight:600;

color:#065f46;

}





tbody td:nth-child(1):before{

content:"Date";

}



tbody td:nth-child(2):before{

content:"Category";

}



tbody td:nth-child(3):before{

content:"Item";

}



tbody td:nth-child(4):before{

content:"Vendor";

}



tbody td:nth-child(5):before{

content:"Description";

}



tbody td:nth-child(6):before{

content:"Amount";

}



tbody td:nth-child(7):before{

content:"Payment";

}



tbody td:nth-child(8):before{

content:"Action";

}




}

</style>


</head>



<body>



<?php include "sidebar.php"; ?>



<div class="overlay"
id="overlay"
onclick="closeMenu()">
</div>





<div class="content">



<div class="topbar">


<i class="fa fa-bars mobile-btn"
onclick="openMenu()"></i>



<h2>

<i class="fa fa-wallet"></i>

Expenses

</h2>




<div>


<i class="fa fa-moon toggle"
onclick="theme()"></i>



<a href="add_expense.php"
class="btn">

<i class="fa fa-plus"></i>

Add Expense

</a>


</div>



</div>









<!-- =====================
SUMMARY CARDS
===================== -->


<div class="cards">



<div class="card">


<div class="icon">

<i class="fa fa-money-bill"></i>

</div>


<div>

<p>Total Expenses</p>


<h2 class="number">

 <?=number_format($total)?>

</h2>


</div>


</div>







<div class="card">


<div class="icon">

<i class="fa fa-calendar"></i>

</div>


<div>


<p>This Month</p>


<h2 class="number">

 <?=number_format($thisMonth)?>

</h2>


</div>


</div>







<div class="card">


<div class="icon">

<i class="fa fa-file"></i>

</div>


<div>


<p>Transactions</p>


<h2 class="number">

<?=$records?>

</h2>


</div>


</div>




</div>










<!-- =====================
FILTER BOX
===================== -->


<div class="filter-box">


<h3>

<i class="fa fa-filter"></i>

Expense Filters

</h3>


<br>




<form class="filter">







<input type="date"
name="start"
value="<?=$start?>">





<input type="date"
name="end"
value="<?=$end?>">







<input type="text"
name="search"
placeholder="Search description, vendor, category..."
value="<?=htmlspecialchars($search)?>">








<select name="category">


<option value="">
All Categories
</option>



<?php foreach($categories as $c){ ?>


<option value="<?=$c['id']?>"
<?=$category==$c['id']?'selected':''?> >


<?=$c['category_name']?>


</option>


<?php } ?>



</select>







<select name="vendor">


<option value="">
All Vendors
</option>



<?php foreach($vendors as $v){ ?>


<option value="<?=$v['id']?>"
<?=$vendor==$v['id']?'selected':''?> >


<?=$v['vendor_name']?>


</option>



<?php } ?>


</select>







<select name="payment">


<option value="">
Payment Method
</option>


<option value="Cash">
Cash
</option>


<option value="Bank">
Bank
</option>


<option value="Mobile Money">
Mobile Money
</option>


<option value="Card">
Card
</option>


</select>







<input type="number"
name="min"
placeholder="Minimum Amount"
value="<?=$min?>">





<input type="number"
name="max"
placeholder="Maximum Amount"
value="<?=$max?>">







<button>

<i class="fa fa-search"></i>

Filter

</button>






<a class="btn pdf"

href="export_expenses_pdf.php?<?=http_build_query($_GET)?>">

<i class="fa fa-file-pdf"></i>

PDF

</a>








<a class="btn excel"

href="export_expenses_excel.php?<?=http_build_query($_GET)?>">


<i class="fa fa-file-excel"></i>

Excel


</a>






</form>


</div>









<!-- =====================
EXPENSE TABLE
===================== -->



<div class="table-box">


<table>



<thead>


<tr>


<th>
Date
</th>


<th>
Category
</th>



<th>
Item
</th>



<th>
Vendor
</th>






<th>
Amount
</th>



<th>
Payment
</th>



<th>
Action
</th>


</tr>


</thead>






<tbody>



<?php foreach($expenses as $e){ ?>



<tr>






<td>


<?=date(
"d/m/Y",
strtotime($e['expense_date'])
)?>


</td>








<td>


<a class="statement-link"

href="category_statement.php?id=<?=$e['category_id']?>">


<i class="fa fa-folder"></i>


<?=$e['category_name']?>


</a>


</td>










<td>



<?php if($e['item_id']){ ?>


<a class="statement-link"

href="item_statement.php?id=<?=$e['item_id']?>">


<i class="fa fa-car"></i>


<?=$e['item_name']?>


<?php if($e['registration_no']){ ?>

<br>

<small>

<?=$e['registration_no']?>

</small>


<?php } ?>


</a>



<?php }else{ ?>


No Item


<?php } ?>


</td>









<td>


<?php if($e['vendor_id']){ ?>


<a class="statement-link"

href="vendor_statement.php?id=<?=$e['vendor_id']?>">


<i class="fa fa-building"></i>


<?=$e['vendor_name']?>


</a>


<?php }else{ ?>


Unknown


<?php } ?>


</td>



















<td>


<strong>

 <?=number_format($e['total'])?>

</strong>


</td>








<td>


<span class="badge">


<?=$e['payment_method']?>


</span>


</td>








<td class="action">

<a class="view"
href="view_expenses_details.php?id=<?=$e['id']?>">
<i class="fa fa-eye"></i>
</a>


<a class="edit"
href="edit_expense.php?id=<?=$e['id']?>">
<i class="fa fa-edit"></i>
</a>


<a class="delete"
href="delete_expense.php?id=<?=$e['id']?>"
onclick="return confirm('Delete this expense?')">
<i class="fa fa-trash"></i>
</a>


</td>






</tr>



<?php } ?>




</tbody>


</table>



</div>





</div>
<!-- PAGINATION -->


<div class="pagination">


<?php if($page>1){ ?>


<a href="?<?=http_build_query(array_merge($_GET,['page'=>$page-1]))?>">

<i class="fa fa-arrow-left"></i>

Prev

</a>


<?php } ?>





<?php for($i=1;$i<=$totalPages;$i++){ ?>


<a 

class="<?=($page==$i)?'active':''?>"

href="?<?=http_build_query(array_merge($_GET,['page'=>$i]))?>">


<?=$i?>


</a>



<?php } ?>







<?php if($page<$totalPages){ ?>


<a href="?<?=http_build_query(array_merge($_GET,['page'=>$page+1]))?>">

Next

<i class="fa fa-arrow-right"></i>

</a>


<?php } ?>



</div>
