34959-vm/fuel_receipts.php
Flatlogic Bot e428ea4534 v1
2025-10-14 17:08:20 +00:00

178 lines
8.6 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
$pdoconn = db();
// Handle form submission for adding a new fuel receipt
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_receipt'])) {
$bunk_id = trim($_POST['bunk_id']);
$supplier = trim($_POST['supplier']);
$invoice_number = trim($_POST['invoice_number']);
$fuel_type_id = trim($_POST['fuel_type_id']);
$quantity = trim($_POST['quantity']);
$rate = trim($_POST['rate']);
$date = trim($_POST['date']);
$amount = $quantity * $rate;
if (!empty($bunk_id) && !empty($fuel_type_id) && !empty($quantity) && !empty($rate) && !empty($date)) {
try {
$sql = "INSERT INTO fuel_receipts (bunk_id, supplier, invoice_number, fuel_type_id, quantity, rate, amount, date) VALUES (:bunk_id, :supplier, :invoice_number, :fuel_type_id, :quantity, :rate, :amount, :date)";
$stmt = $pdoconn->prepare($sql);
$stmt->execute([
':bunk_id' => $bunk_id,
':supplier' => $supplier,
':invoice_number' => $invoice_number,
':fuel_type_id' => $fuel_type_id,
':quantity' => $quantity,
':rate' => $rate,
':amount' => $amount,
':date' => $date
]);
$success_message = "Fuel receipt recorded successfully!";
} catch (PDOException $e) {
$error_message = "Error recording receipt: " . $e->getMessage();
}
} else {
$error_message = "Bunk, fuel type, quantity, rate, and date are required.";
}
}
// Fetch master data for dropdowns
try {
$bunks_stmt = $pdoconn->query("SELECT id, name FROM bunks ORDER BY name");
$bunks = $bunks_stmt->fetchAll(PDO::FETCH_ASSOC);
$fuel_types_stmt = $pdoconn->query("SELECT id, name FROM fuel_types ORDER BY name");
$fuel_types = $fuel_types_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$bunks = [];
$fuel_types = [];
$page_error = "Error fetching master data: " . $e->getMessage();
}
// Fetch recent fuel receipts
try {
$receipts_stmt = $pdoconn->query("
SELECT fr.*, b.name as bunk_name, ft.name as fuel_type_name
FROM fuel_receipts fr
JOIN bunks b ON fr.bunk_id = b.id
JOIN fuel_types ft ON fr.fuel_type_id = ft.id
ORDER BY fr.date DESC, fr.id DESC
LIMIT 20
");
$receipts = $receipts_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$receipts = [];
$page_error = "Error fetching receipts: " . $e->getMessage();
}
?>
<div class="container-fluid">
<div class="card">
<div class="card-header">
<h4 class="card-title">Record Fuel Receipt</h4>
</div>
<div class="card-body">
<?php if (isset($success_message)): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<?php if (isset($page_error)): ?>
<div class="alert alert-danger"><?php echo $page_error; ?></div>
<?php endif; ?>
<div class="row">
<div class="col-md-4">
<h5>New Receipt Entry</h5>
<form method="POST" action="fuel_receipts.php">
<div class="mb-3">
<label for="date" class="form-label">Date</label>
<input type="date" class="form-control" id="date" name="date" value="<?php echo date('Y-m-d'); ?>" required>
</div>
<div class="mb-3">
<label for="bunk_id" class="form-label">Bunk</label>
<select class="form-select" id="bunk_id" name="bunk_id" required>
<option value="">Select Bunk</option>
<?php foreach ($bunks as $bunk): ?>
<option value="<?php echo htmlspecialchars($bunk['id']); ?>"><?php echo htmlspecialchars($bunk['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="supplier" class="form-label">Supplier (e.g., IOCL)</label>
<input type="text" class="form-control" id="supplier" name="supplier">
</div>
<div class="mb-3">
<label for="invoice_number" class="form-label">Invoice Number</label>
<input type="text" class="form-control" id="invoice_number" name="invoice_number">
</div>
<div class="mb-3">
<label for="fuel_type_id" class="form-label">Fuel Type</label>
<select class="form-select" id="fuel_type_id" name="fuel_type_id" required>
<option value="">Select Fuel Type</option>
<?php foreach ($fuel_types as $fuel_type): ?>
<option value="<?php echo htmlspecialchars($fuel_type['id']); ?>"><?php echo htmlspecialchars($fuel_type['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="quantity" class="form-label">Quantity (Ltr)</label>
<input type="number" step="0.01" class="form-control" id="quantity" name="quantity" required>
</div>
<div class="mb-3">
<label for="rate" class="form-label">Rate</label>
<input type="number" step="0.01" class="form-control" id="rate" name="rate" required>
</div>
<button type="submit" name="add_receipt" class="btn btn-primary">Record Receipt</button>
</form>
</div>
<div class="col-md-8">
<h5>Recent Fuel Receipts</h5>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Date</th>
<th>Bunk</th>
<th>Supplier</th>
<th>Invoice #</th>
<th>Fuel</th>
<th>Qty</th>
<th>Rate</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php if (empty($receipts)): ?>
<tr>
<td colspan="8" class="text-center">No fuel receipts recorded yet.</td>
</tr>
<?php else: ?>
<?php foreach ($receipts as $receipt): ?>
<tr>
<td><?php echo htmlspecialchars($receipt['date']); ?></td>
<td><?php echo htmlspecialchars($receipt['bunk_name']); ?></td>
<td><?php echo htmlspecialchars($receipt['supplier']); ?></td>
<td><?php echo htmlspecialchars($receipt['invoice_number']); ?></td>
<td><?php echo htmlspecialchars($receipt['fuel_type_name']); ?></td>
<td><?php echo htmlspecialchars($receipt['quantity']); ?></td>
<td><?php echo htmlspecialchars(number_format($receipt['rate'], 2)); ?></td>
<td><?php echo htmlspecialchars(number_format($receipt['amount'], 2)); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>