34894-vm/quotations.php
Flatlogic Bot da0815949e finavo
2025-10-12 05:26:53 +00:00

285 lines
15 KiB
PHP

<?php
require_once 'includes/auth.php';
$active_page = 'quotations';
require_once 'db/config.php';
// Handle delete request
if (isset($_GET['delete_id'])) {
$delete_id = $_GET['delete_id'];
$pdo = db();
$stmt = $pdo->prepare("DELETE FROM quotations WHERE id = ?");
$stmt->execute([$delete_id]);
header("Location: quotations.php");
exit;
}
// Handle form submission for adding a new quotation
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_quotation'])) {
$customer_id = $_POST['customer_id'];
$quotation_number = $_POST['quotation_number'];
$quotation_date = $_POST['quotation_date'];
$total_amount = $_POST['total_amount'];
if (!empty($customer_id) && !empty($quotation_number) && !empty($quotation_date) && !empty($total_amount)) {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO quotations (customer_id, quotation_number, quotation_date, total_amount) VALUES (?, ?, ?, ?)");
$stmt->execute([$customer_id, $quotation_number, $quotation_date, $total_amount]);
}
header("Location: quotations.php");
exit();
}
// Handle form submission for updating a quotation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_quotation'])) {
$id = $_POST['quotation_id'];
$customer_id = $_POST['customer_id'];
$quotation_number = $_POST['quotation_number'];
$quotation_date = $_POST['quotation_date'];
$total_amount = $_POST['total_amount'];
$status = $_POST['status'];
if (!empty($id)) {
$pdo = db();
$stmt = $pdo->prepare("UPDATE quotations SET customer_id = ?, quotation_number = ?, quotation_date = ?, total_amount = ?, status = ? WHERE id = ?");
$stmt->execute([$customer_id, $quotation_number, $quotation_date, $total_amount, $status, $id]);
header("Location: quotations.php");
exit;
}
}
// Fetch all customers for the dropdown
$pdo = db();
$customers_stmt = $pdo->query("SELECT id, companyName FROM customers ORDER BY companyName ASC");
$customers = $customers_stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch all quotations with customer names
$quotations_stmt = $pdo->query("
SELECT
q.id,
q.customer_id,
q.quotation_number,
q.quotation_date,
q.total_amount,
q.status,
c.companyName AS customer_name
FROM quotations q
JOIN customers c ON q.customer_id = c.id
ORDER BY q.quotation_date DESC
");
$quotations = $quotations_stmt->fetchAll(PDO::FETCH_ASSOC);
include 'includes/header.php';
?>
<div class="d-flex" id="wrapper">
<?php include 'includes/sidebar.php'; ?>
<!-- Page content wrapper-->
<div id="page-content-wrapper">
<!-- Top navigation-->
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<div class="container-fluid">
<button class="btn btn-primary" id="sidebarToggle"><i class="bi bi-list"></i></button>
<ul class="navbar-nav ms-auto mt-2 mt-lg-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo htmlspecialchars($_SESSION["username"]); ?></a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#!">Profile</a>
<a class="dropdown-item" href="#!">Settings</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- Page content-->
<main class="container-fluid p-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Quotations</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addQuotationModal">
<i class="bi bi-plus-circle"></i>
Add Quotation
</button>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Customer</th>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Status</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($quotations)): ?>
<tr>
<td colspan="6" class="text-center">No quotations found.</td>
</tr>
<?php else: ?>
<?php foreach ($quotations as $quotation): ?>
<tr>
<td><?php echo htmlspecialchars($quotation['quotation_number']); ?></td>
<td><?php echo htmlspecialchars($quotation['customer_name']); ?></td>
<td><?php echo htmlspecialchars($quotation['quotation_date']); ?></td>
<td>$<?php echo htmlspecialchars(number_format($quotation['total_amount'], 2)); ?></td>
<td>
<?php
$status = htmlspecialchars($quotation['status']);
$badge_class = 'bg-secondary';
if ($status === 'Sent') {
$badge_class = 'bg-info text-dark';
} elseif ($status === 'Accepted') {
$badge_class = 'bg-success';
} elseif ($status === 'Rejected') {
$badge_class = 'bg-danger';
} elseif ($status === 'Draft') {
$badge_class = 'bg-warning text-dark';
}
?>
<span class="badge <?php echo $badge_class; ?>"><?php echo $status; ?></span>
</td>
<td>
<button class="btn btn-sm btn-outline-primary edit-quotation-btn"
data-id="<?php echo $quotation['id']; ?>"
data-customer-id="<?php echo $quotation['customer_id']; ?>"
data-quotation-number="<?php echo htmlspecialchars($quotation['quotation_number']); ?>"
data-quotation-date="<?php echo htmlspecialchars($quotation['quotation_date']); ?>"
data-total-amount="<?php echo htmlspecialchars($quotation['total_amount']); ?>"
data-status="<?php echo htmlspecialchars($quotation['status']); ?>"
data-bs-toggle="modal" data-bs-target="#editQuotationModal">
<i class="bi bi-pencil-square"></i>
</button>
<a href="quotations.php?delete_id=<?php echo $quotation['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this quotation?');"><i class="bi bi-trash"></i></a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</main>
</div>
</div>
<!-- Add Quotation Modal -->
<div class="modal fade" id="addQuotationModal" tabindex="-1" aria-labelledby="addQuotationModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addQuotationModalLabel">Add New Quotation</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="quotations.php" method="POST">
<input type="hidden" name="add_quotation" value="1">
<div class="mb-3">
<label for="customer_id" class="form-label">Customer</label>
<select class="form-select" id="customer_id" name="customer_id" required>
<option value="" disabled selected>Select a customer</option>
<?php foreach ($customers as $customer): ?>
<option value="<?php echo $customer['id']; ?>"><?php echo htmlspecialchars($customer['companyName']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="quotation_number" class="form-label">Quotation Number</label>
<input type="text" class="form-control" id="quotation_number" name="quotation_number" required>
</div>
<div class="mb-3">
<label for="quotation_date" class="form-label">Quotation Date</label>
<input type="date" class="form-control" id="quotation_date" name="quotation_date" required>
</div>
<div class="mb-3">
<label for="total_amount" class="form-label">Total Amount</label>
<input type="number" step="0.01" class="form-control" id="total_amount" name="total_amount" required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Quotation</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Edit Quotation Modal -->
<div class="modal fade" id="editQuotationModal" tabindex="-1" aria-labelledby="editQuotationModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editQuotationModalLabel">Edit Quotation</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST" action="quotations.php">
<input type="hidden" name="update_quotation" value="1">
<input type="hidden" name="quotation_id" id="edit_quotation_id">
<div class="mb-3">
<label for="edit_customer_id" class="form-label">Customer</label>
<select class="form-select" id="edit_customer_id" name="customer_id" required>
<?php foreach ($customers as $customer): ?>
<option value="<?php echo $customer['id']; ?>"><?php echo htmlspecialchars($customer['companyName']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="edit_quotation_number" class="form-label">Quotation Number</label>
<input type="text" class="form-control" id="edit_quotation_number" name="quotation_number" required>
</div>
<div class="mb-3">
<label for="edit_quotation_date" class="form-label">Quotation Date</label>
<input type="date" class="form-control" id="edit_quotation_date" name="quotation_date" required>
</div>
<div class="mb-3">
<label for="edit_total_amount" class="form-label">Total Amount</label>
<input type="number" step="0.01" class="form-control" id="edit_total_amount" name="total_amount" required>
</div>
<div class="mb-3">
<label for="edit_status" class="form-label">Status</label>
<select class="form-select" id="edit_status" name="status" required>
<option value="Draft">Draft</option>
<option value="Sent">Sent</option>
<option value="Accepted">Accepted</option>
<option value="Rejected">Rejected</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
var editQuotationModal = document.getElementById('editQuotationModal');
editQuotationModal.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var quotationId = button.getAttribute('data-id');
var customerId = button.getAttribute('data-customer-id');
var quotationNumber = button.getAttribute('data-quotation-number');
var quotationDate = button.getAttribute('data-quotation-date');
var totalAmount = button.getAttribute('data-total-amount');
var status = button.getAttribute('data-status');
var modal = this;
modal.querySelector('#edit_quotation_id').value = quotationId;
modal.querySelector('#edit_customer_id').value = customerId;
modal.querySelector('#edit_quotation_number').value = quotationNumber;
modal.querySelector('#edit_quotation_date').value = quotationDate;
modal.querySelector('#edit_total_amount').value = totalAmount;
modal.querySelector('#edit_status').value = status;
});
});
</script>