206 lines
9.0 KiB
PHP
206 lines
9.0 KiB
PHP
<?php
|
|
require_once __DIR__ . "/../includes/functions.php";
|
|
require_permission("payment_types_view");
|
|
require_once __DIR__ . '/../db/config.php';
|
|
$pdo = db();
|
|
|
|
$message = '';
|
|
|
|
// Handle Add/Edit Payment Type
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
$action = $_POST['action'];
|
|
$name = trim($_POST['name']);
|
|
$code = trim($_POST['code']);
|
|
$is_active = isset($_POST['is_active']) ? 1 : 0;
|
|
$id = isset($_POST['id']) ? (int)$_POST['id'] : null;
|
|
|
|
if (empty($name) || empty($code)) {
|
|
$message = '<div class="alert alert-danger">Name and code are required.</div>';
|
|
} else {
|
|
try {
|
|
if ($action === 'edit_payment_type' && $id) {
|
|
if (!has_permission('payment_types_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE payment_types SET name = ?, code = ?, is_active = ? WHERE id = ?");
|
|
$stmt->execute([$name, $code, $is_active, $id]);
|
|
$message = '<div class="alert alert-success">Payment type updated successfully!</div>';
|
|
}
|
|
} elseif ($action === 'add_payment_type') {
|
|
if (!has_permission('payment_types_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO payment_types (name, code, is_active) VALUES (?, ?, ?)");
|
|
$stmt->execute([$name, $code, $is_active]);
|
|
$message = '<div class="alert alert-success">Payment type created successfully!</div>';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
if ($e->getCode() == 23000) {
|
|
$message = '<div class="alert alert-danger">Code already exists.</div>';
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Delete
|
|
if (isset($_GET['delete'])) {
|
|
if (!has_permission('payment_types_del')) {
|
|
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to delete payment types.</div>';
|
|
} else {
|
|
$id = $_GET['delete'];
|
|
$pdo->prepare("DELETE FROM payment_types WHERE id = ?")->execute([$id]);
|
|
header("Location: payment_types.php");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$query = "SELECT * FROM payment_types ORDER BY id ASC";
|
|
$payments_pagination = paginate_query($pdo, $query);
|
|
$payment_types = $payments_pagination['data'];
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="fw-bold mb-0">Payment Types</h2>
|
|
<?php if (has_permission('payment_types_add')): ?>
|
|
<button class="btn btn-primary" onclick="openAddModal()">
|
|
<i class="bi bi-plus-lg"></i> Add Payment Type
|
|
</button>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?= $message ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body p-0">
|
|
<!-- Pagination Controls -->
|
|
<div class="p-3 border-bottom bg-light">
|
|
<?php render_pagination_controls($payments_pagination); ?>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th class="ps-4">ID</th>
|
|
<th>Name</th>
|
|
<th>Code</th>
|
|
<th>Status</th>
|
|
<th class="text-end pe-4">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($payment_types as $type): ?>
|
|
<tr>
|
|
<td class="ps-4 fw-medium">#<?= $type['id'] ?></td>
|
|
<td class="fw-bold"><?= htmlspecialchars($type['name']) ?></td>
|
|
<td><code><?= htmlspecialchars($type['code']) ?></code></td>
|
|
<td>
|
|
<?php if ($type['is_active']): ?>
|
|
<span class="badge bg-success-subtle text-success px-3">Active</span>
|
|
<?php else: ?>
|
|
<span class="badge bg-secondary-subtle text-secondary px-3">Inactive</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-end pe-4">
|
|
<?php if (has_permission('payment_types_add')): ?>
|
|
<button type="button" class="btn btn-sm btn-outline-primary me-1"
|
|
onclick='openEditModal(<?= htmlspecialchars(json_encode($type), ENT_QUOTES, "UTF-8") ?>)' title="Edit"><i class="bi bi-pencil"></i></button>
|
|
<?php endif; ?>
|
|
|
|
<?php if (has_permission('payment_types_del')): ?>
|
|
<a href="?delete=<?= $type['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this payment type?')"><i class="bi bi-trash"></i></a>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($payment_types)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center py-4 text-muted">No payment types found.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- Bottom Pagination -->
|
|
<div class="p-3 border-top bg-light">
|
|
<?php render_pagination_controls($payments_pagination); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Payment Type Modal -->
|
|
<?php if (has_permission('payment_types_add')): ?>
|
|
<div class="modal fade" id="paymentTypeModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-primary text-white">
|
|
<h5 class="modal-title" id="paymentTypeModalTitle">Add New Payment Type</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form method="POST" id="paymentTypeForm">
|
|
<div class="modal-body">
|
|
<input type="hidden" name="action" id="paymentTypeAction" value="add_payment_type">
|
|
<input type="hidden" name="id" id="paymentTypeId">
|
|
<div class="mb-3">
|
|
<label class="form-label">Name <span class="text-danger">*</span></label>
|
|
<input type="text" name="name" id="paymentTypeName" class="form-control" required placeholder="e.g. Cash, Credit Card">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Code <span class="text-danger">*</span></label>
|
|
<input type="text" name="code" id="paymentTypeCode" class="form-control" required placeholder="e.g. cash, card, loyalty">
|
|
</div>
|
|
<div class="mb-3 form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" name="is_active" id="paymentTypeIsActive" checked>
|
|
<label class="form-check-label" for="paymentTypeIsActive">Is Active</label>
|
|
</div>
|
|
</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 Payment Type</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function getPaymentTypeModal() {
|
|
if (typeof bootstrap === 'undefined') return null;
|
|
const el = document.getElementById('paymentTypeModal');
|
|
return el ? bootstrap.Modal.getOrCreateInstance(el) : null;
|
|
}
|
|
|
|
function openAddModal() {
|
|
const modal = getPaymentTypeModal();
|
|
if (!modal) return;
|
|
|
|
document.getElementById('paymentTypeModalTitle').innerText = 'Add New Payment Type';
|
|
document.getElementById('paymentTypeAction').value = 'add_payment_type';
|
|
document.getElementById('paymentTypeForm').reset();
|
|
document.getElementById('paymentTypeId').value = '';
|
|
modal.show();
|
|
}
|
|
|
|
function openEditModal(type) {
|
|
if (!type) return;
|
|
const modal = getPaymentTypeModal();
|
|
if (!modal) return;
|
|
|
|
document.getElementById('paymentTypeModalTitle').innerText = 'Edit Payment Type';
|
|
document.getElementById('paymentTypeAction').value = 'edit_payment_type';
|
|
document.getElementById('paymentTypeId').value = type.id;
|
|
document.getElementById('paymentTypeName').value = type.name || '';
|
|
document.getElementById('paymentTypeCode').value = type.code || '';
|
|
document.getElementById('paymentTypeIsActive').checked = type.is_active == 1;
|
|
|
|
modal.show();
|
|
}
|
|
</script>
|
|
<?php endif; ?>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|