| Server IP : 173.209.174.21 / Your IP : 216.73.216.89 Web Server : Apache/2.4.58 (Ubuntu) System : Linux wcfs-server 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : nodor ( 1000) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/wcfs/ |
Upload File : |
<?php
include 'protect.php';
include 'db.php';
require_once __DIR__ . '/brand_header.php';
require_once __DIR__ . '/store_expense_helpers.php';
ensureStoreExpensesTable($conn);
$selectedMonth = normalizeExpenseMonth((string) ($_GET['month'] ?? $_POST['month'] ?? date('Y-m')));
$message = '';
$error = '';
function h($value): string
{
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = trim((string) ($_POST['action'] ?? ''));
try {
if ($action === 'add_expense') {
addStoreExpense(
$conn,
(string) ($_POST['expense_date'] ?? ''),
(string) ($_POST['supplier_name'] ?? ''),
(string) ($_POST['qbo_account'] ?? ''),
(string) ($_POST['line_description'] ?? ''),
(string) ($_POST['amount'] ?? ''),
(string) ($_POST['tax_code'] ?? 'NON'),
(string) ($_POST['notes'] ?? '')
);
$message = 'Store expense saved.';
} elseif ($action === 'delete_expense') {
deleteStoreExpense($conn, (int) ($_POST['expense_id'] ?? 0));
$message = 'Store expense deleted.';
}
} catch (Throwable $e) {
$error = $e->getMessage();
}
}
$expenses = fetchStoreExpensesByMonth($conn, $selectedMonth);
$monthlyTotal = storeExpenseMonthlyTotal($expenses);
$totalsByAccount = storeExpenseTotalsByAccount($expenses);
$months = [];
for ($i = 0; $i < 24; $i++) {
$months[] = date('Y-m', strtotime("-{$i} months"));
}
if (!empty($_GET['export']) && $_GET['export'] === 'qbo') {
$csv = exportStoreExpensesQboBillsCsv($expenses);
$filename = storeExpenseExportFilename((string) ($_SESSION['store_name'] ?? $_SESSION['store_db'] ?? 'HouseTabPro'), $selectedMonth);
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
echo $csv;
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Store Expenses</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 30px;
background: #f8fafc;
color: #1f2937;
}
.container {
max-width: 1100px;
margin: 0 auto;
background: #fff;
padding: 28px;
border-radius: 16px;
box-shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
}
.toolbar {
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 16px;
flex-wrap: wrap;
margin-bottom: 24px;
}
.toolbar form {
display: flex;
gap: 12px;
align-items: flex-end;
flex-wrap: wrap;
}
label {
display: block;
font-weight: 700;
margin-bottom: 6px;
color: #475569;
}
input, select, textarea {
width: 100%;
padding: 10px 12px;
border: 1px solid #cbd5e1;
border-radius: 10px;
font: inherit;
box-sizing: border-box;
}
textarea {
min-height: 88px;
resize: vertical;
}
.btn {
display: inline-block;
padding: 11px 16px;
background: #1d4ed8;
color: #fff;
text-decoration: none;
border: none;
border-radius: 10px;
font-weight: 700;
cursor: pointer;
}
.btn-secondary {
background: #64748b;
}
.btn-danger {
background: #b91c1c;
}
.grid {
display: grid;
grid-template-columns: 1.1fr 0.9fr;
gap: 22px;
margin-bottom: 24px;
}
.card {
background: #f8fafc;
border: 1px solid #e5e7eb;
border-radius: 16px;
padding: 20px;
}
.card h2 {
margin-top: 0;
}
.form-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
}
.form-grid .full {
grid-column: 1 / -1;
}
.summary-total {
font-size: 32px;
font-weight: 800;
color: #0f172a;
}
.msg {
margin-bottom: 16px;
padding: 12px 14px;
border-radius: 10px;
}
.msg.ok {
background: #ecfdf5;
border: 1px solid #86efac;
color: #166534;
}
.msg.err {
background: #fef2f2;
border: 1px solid #fca5a5;
color: #991b1b;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
th, td {
text-align: left;
padding: 10px 8px;
border-bottom: 1px solid #e5e7eb;
vertical-align: top;
}
th {
background: #eff6ff;
white-space: nowrap;
}
.muted {
color: #64748b;
font-size: 0.92rem;
}
.empty {
padding: 18px 0;
color: #64748b;
}
@media (max-width: 900px) {
.grid {
grid-template-columns: 1fr;
}
.form-grid {
grid-template-columns: 1fr;
}
}
</style>
<?php echo appBrandStyles(); ?>
</head>
<body>
<div class="container">
<?php renderAppBrandHeader(); ?>
<div class="toolbar">
<div>
<h1 style="margin:0;">Store Expenses</h1>
<div class="muted" style="margin-top:6px;">
Track monthly store supplies, maintenance, cleaning items, and similar operating costs. Export a QBO-ready CSV by month when you are ready to import.
</div>
</div>
<form method="get">
<div>
<label for="month">Month</label>
<select id="month" name="month">
<?php foreach ($months as $month): ?>
<option value="<?php echo h($month); ?>" <?php echo $selectedMonth === $month ? 'selected' : ''; ?>>
<?php echo h(date('F Y', strtotime($month . '-01'))); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<button class="btn btn-secondary" type="submit">View Month</button>
</div>
<div>
<a class="btn" href="store_expenses.php?month=<?php echo h(urlencode($selectedMonth)); ?>&export=qbo">Export QBO CSV</a>
</div>
</form>
</div>
<?php if ($message !== ''): ?>
<div class="msg ok"><?php echo h($message); ?></div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="msg err"><?php echo h($error); ?></div>
<?php endif; ?>
<div class="grid">
<div class="card">
<h2>Add Expense</h2>
<form method="post">
<input type="hidden" name="action" value="add_expense">
<input type="hidden" name="month" value="<?php echo h($selectedMonth); ?>">
<div class="form-grid">
<div>
<label for="expense_date">Expense Date</label>
<input id="expense_date" name="expense_date" type="date" value="<?php echo h(date('Y-m-d')); ?>" required>
</div>
<div>
<label for="amount">Amount</label>
<input id="amount" name="amount" type="number" step="0.01" min="0.01" placeholder="0.00" required>
</div>
<div>
<label for="supplier_name">Supplier / Vendor</label>
<input id="supplier_name" name="supplier_name" type="text" placeholder="Costco, Amazon, Local Hardware..." required>
</div>
<div>
<label for="qbo_account">QBO Account / Category</label>
<input id="qbo_account" name="qbo_account" type="text" placeholder="Supplies, Repairs & Maintenance, Office Supplies..." required>
</div>
<div class="full">
<label for="line_description">Description</label>
<input id="line_description" name="line_description" type="text" placeholder="Cleaning supplies, toilet paper, repair parts..." required>
</div>
<div>
<label for="tax_code">QBO Tax Code</label>
<input id="tax_code" name="tax_code" type="text" value="NON">
</div>
<div class="full">
<label for="notes">Notes</label>
<textarea id="notes" name="notes" placeholder="Optional note for yourself or for the QBO memo field"></textarea>
</div>
</div>
<div style="margin-top:18px;">
<button class="btn" type="submit">Save Expense</button>
</div>
</form>
</div>
<div class="card">
<h2><?php echo h(date('F Y', strtotime($selectedMonth . '-01'))); ?> Summary</h2>
<div class="summary-total">$<?php echo h(number_format($monthlyTotal, 2)); ?></div>
<div class="muted" style="margin-top:8px;">Monthly total for store operating expenses entered here.</div>
<h3 style="margin:24px 0 12px;">Totals by QBO Account</h3>
<?php if (!$totalsByAccount): ?>
<div class="empty">No expenses entered for this month yet.</div>
<?php else: ?>
<table>
<thead>
<tr>
<th>QBO Account</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php foreach ($totalsByAccount as $account => $total): ?>
<tr>
<td><?php echo h($account); ?></td>
<td>$<?php echo h(number_format($total, 2)); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
<div class="card">
<h2 style="margin-top:0;"><?php echo h(date('F Y', strtotime($selectedMonth . '-01'))); ?> Entries</h2>
<div class="muted" style="margin-bottom:10px;">
The QBO export from this page is set up as a bill-style CSV with the fields QuickBooks maps during import. Your QBO account/category names and tax code names should match what exists in your QuickBooks company.
</div>
<?php if (!$expenses): ?>
<div class="empty">No store expenses have been entered for this month.</div>
<?php else: ?>
<table>
<thead>
<tr>
<th>Date</th>
<th>Supplier</th>
<th>Account</th>
<th>Description</th>
<th>Amount</th>
<th>Tax Code</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($expenses as $expense): ?>
<tr>
<td><?php echo h($expense['expense_date']); ?></td>
<td>
<?php echo h($expense['supplier_name']); ?>
<?php if (!empty($expense['notes'])): ?>
<div class="muted" style="margin-top:4px;"><?php echo h($expense['notes']); ?></div>
<?php endif; ?>
</td>
<td><?php echo h($expense['qbo_account']); ?></td>
<td><?php echo h($expense['line_description']); ?></td>
<td>$<?php echo h(number_format((float) $expense['amount'], 2)); ?></td>
<td><?php echo h($expense['tax_code']); ?></td>
<td>
<form method="post" onsubmit="return confirm('Delete this store expense entry?');">
<input type="hidden" name="action" value="delete_expense">
<input type="hidden" name="month" value="<?php echo h($selectedMonth); ?>">
<input type="hidden" name="expense_id" value="<?php echo h((string) $expense['id']); ?>">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</body>
</html>