35005-vm/prices.php
Flatlogic Bot d9ae8f552d v3
2025-10-16 12:45:17 +00:00

269 lines
13 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$notification = null;
// Handle Add, Edit, Delete actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$db = db();
// Add Price
if (isset($_POST['add_price'])) {
$tank_id = $_POST['tank_id'];
$price = $_POST['price'];
$effective_date = $_POST['effective_date'];
if (!empty($tank_id) && !empty($price) && !empty($effective_date)) {
try {
$stmt = $db->prepare("INSERT INTO prices (tank_id, price, effective_date) VALUES (?, ?, ?)");
$stmt->execute([$tank_id, $price, $effective_date]);
$_SESSION['notification'] = ['text' => 'Price added successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error adding price: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'All fields are required.', 'type' => 'warning'];
}
}
// Edit Price
elseif (isset($_POST['edit_price'])) {
$id = $_POST['price_id'];
$tank_id = $_POST['tank_id'];
$price = $_POST['price'];
$effective_date = $_POST['effective_date'];
if (!empty($tank_id) && !empty($price) && !empty($effective_date) && !empty($id)) {
try {
$stmt = $db->prepare("UPDATE prices SET tank_id = ?, price = ?, effective_date = ? WHERE id = ?");
$stmt->execute([$tank_id, $price, $effective_date, $id]);
$_SESSION['notification'] = ['text' => 'Price updated successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error updating price: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'All fields and ID are required.', 'type' => 'warning'];
}
}
header("Location: prices.php");
exit;
}
// Soft Delete Price
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
try {
$db = db();
$stmt = $db->prepare("UPDATE prices SET deleted_at = NOW() WHERE id = ?");
$stmt->execute([$_GET['id']]);
$_SESSION['notification'] = ['text' => 'Price deleted successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error deleting price: ' . $e->getMessage(), 'type' => 'danger'];
}
header("Location: prices.php");
exit;
}
if (isset($_SESSION['notification'])) {
$notification = $_SESSION['notification'];
unset($_SESSION['notification']);
}
$db = db();
$prices = $db->query("SELECT pr.*, t.name as tank_name, ft.name as fuel_type_name FROM prices pr JOIN tanks t ON pr.tank_id = t.id JOIN fuel_types ft ON t.fuel_type_id = ft.id WHERE pr.deleted_at IS NULL ORDER BY pr.effective_date DESC")->fetchAll(PDO::FETCH_ASSOC);
$tanks = $db->query("SELECT t.id, t.name, ft.name as fuel_type_name FROM tanks t JOIN fuel_types ft ON t.fuel_type_id = ft.id WHERE t.deleted_at IS NULL")->fetchAll(PDO::FETCH_ASSOC);
$page_title = "Price Management";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($page_title) ?> - Petrol Pump Management</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
</head>
<body>
<?php include 'includes/header.php'; ?>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h1><i class="bi bi-tags-fill"></i> <?= htmlspecialchars($page_title) ?></h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPriceModal">
<i class="bi bi-plus-circle"></i> Add New Price
</button>
</div>
<div class="card">
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Tank</th>
<th>Price</th>
<th>Effective Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($prices as $price): ?>
<tr>
<td><?= htmlspecialchars($price['tank_name'] . ' (' . $price['fuel_type_name'] . ')') ?></td>
<td>₹ <?= htmlspecialchars(number_format($price['price'], 2)) ?></td>
<td><?= date('d M, Y', strtotime($price['effective_date'])) ?></td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary edit-btn"
data-bs-toggle="modal" data-bs-target="#editPriceModal"
data-id="<?= $price['id'] ?>"
data-tank-id="<?= $price['tank_id'] ?>"
data-price="<?= htmlspecialchars($price['price']) ?>"
data-effective-date="<?= htmlspecialchars($price['effective_date']) ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button type="button" class="btn btn-sm btn-outline-danger delete-btn"
data-bs-toggle="modal" data-bs-target="#deleteConfirmModal"
data-id="<?= $price['id'] ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add Price Modal -->
<div class="modal fade" id="addPriceModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add New Price</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Tank*</label>
<select class="form-select" name="tank_id" required>
<option value="">Select Tank</option>
<?php foreach ($tanks as $tank): ?>
<option value="<?= $tank['id'] ?>"><?= htmlspecialchars($tank['name'] . ' (' . $tank['fuel_type_name'] . ')') ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Price*</label>
<input type="number" step="0.01" class="form-control" name="price" required>
</div>
<div class="mb-3">
<label class="form-label">Effective Date*</label>
<input type="date" class="form-control" name="effective_date" value="<?= date('Y-m-d') ?>" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="add_price" class="btn btn-primary">Save Price</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Price Modal -->
<div class="modal fade" id="editPriceModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<input type="hidden" name="price_id" id="edit_price_id">
<div class="modal-header">
<h5 class="modal-title">Edit Price</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Tank*</label>
<select class="form-select" id="edit_tank_id" name="tank_id" required>
<?php foreach ($tanks as $tank): ?>
<option value="<?= $tank['id'] ?>"><?= htmlspecialchars($tank['name'] . ' (' . $tank['fuel_type_name'] . ')') ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Price*</label>
<input type="number" step="0.01" class="form-control" id="edit_price" name="price" required>
</div>
<div class="mb-3">
<label class="form-label">Effective Date*</label>
<input type="date" class="form-control" id="edit_effective_date" name="effective_date" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="edit_price" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Deletion</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">Are you sure you want to delete this price record? This is a soft delete.</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<a href="#" id="delete-confirm-btn" class="btn btn-danger">Delete</a>
</div>
</div>
</div>
</div>
<!-- Toast Notification -->
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
<div id="notificationToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header"></div>
<div class="toast-body"></div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
const notification = <?php echo json_encode($notification); ?>;
if (notification) {
const toastEl = document.getElementById('notificationToast');
const toastHeader = toastEl.querySelector('.toast-header');
const toastBody = toastEl.querySelector('.toast-body');
toastHeader.innerHTML = `<strong class="me-auto">Notification</strong><button type="button" class="btn-close" data-bs-dismiss="toast"></button>`;
toastBody.textContent = notification.text;
toastEl.classList.remove('bg-success', 'bg-danger', 'bg-warning');
toastEl.classList.add('bg-' + notification.type, 'text-white');
new bootstrap.Toast(toastEl).show();
}
const editModal = document.getElementById('editPriceModal');
editModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
editModal.querySelector('#edit_price_id').value = id;
editModal.querySelector('#edit_tank_id').value = button.getAttribute('data-tank-id');
editModal.querySelector('#edit_price').value = button.getAttribute('data-price');
editModal.querySelector('#edit_effective_date').value = button.getAttribute('data-effective-date');
});
const deleteModal = document.getElementById('deleteConfirmModal');
deleteModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
deleteModal.querySelector('#delete-confirm-btn').href = 'prices.php?action=delete&id=' + id;
});
});
</script>
</body>
</html>