38244-vm/vouchers.php
2026-02-07 11:39:06 +00:00

278 lines
13 KiB
PHP

<?php
declare(strict_types=1);
require_once 'db/config.php';
$db = db();
$current_branch_id = (int)($_GET['branch_id'] ?? 1);
// Handle voucher creation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create') {
$code = strtoupper(bin2hex(random_bytes(4))); // Generate a random 8-character code
if (!empty($_POST['custom_code'])) {
$code = strtoupper(htmlspecialchars($_POST['custom_code']));
}
$discount = (float)$_POST['discount_amount'];
$expires_at = !empty($_POST['expires_at']) ? $_POST['expires_at'] : null;
try {
$stmt = $db->prepare("INSERT INTO vouchers (code, discount_amount, expires_at) VALUES (?, ?, ?)");
$stmt->execute([$code, $discount, $expires_at]);
$success_msg = "Voucher berhasil dibuat!";
} catch (PDOException $e) {
$error_msg = "Gagal membuat voucher: " . $e->getMessage();
}
}
// Fetch vouchers
$vouchers = $db->query("SELECT * FROM vouchers ORDER BY created_at DESC")->fetchAll();
// Fetch branches for sidebar
$branches = $db->query("SELECT * FROM branches")->fetchAll();
$current_branch = array_filter($branches, fn($b) => $b['id'] == $current_branch_id);
$current_branch = reset($current_branch);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manajemen Voucher - POS Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
<style>
.voucher-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 15px;
padding: 20px;
position: relative;
overflow: hidden;
width: 350px;
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
.voucher-card::before {
content: '';
position: absolute;
top: 0;
right: -20px;
bottom: 0;
width: 40px;
background: radial-gradient(circle at center, #f8f9fa 10px, transparent 11px);
background-size: 40px 40px;
}
.voucher-card::after {
content: '';
position: absolute;
top: 0;
left: -20px;
bottom: 0;
width: 40px;
background: radial-gradient(circle at center, #f8f9fa 10px, transparent 11px);
background-size: 40px 40px;
}
.qr-code {
background: white;
padding: 5px;
border-radius: 8px;
width: 80px;
height: 80px;
}
@media print {
body * { visibility: hidden; }
#printArea, #printArea * { visibility: visible; }
#printArea { position: absolute; left: 0; top: 0; width: 100%; }
.no-print { display: none !important; }
}
</style>
</head>
<body>
<div class="wrapper text-white">
<!-- Sidebar -->
<nav id="sidebar" class="bg-dark text-white">
<div class="sidebar-header p-3">
<h4 class="mb-0">POS<span class="text-success">PRO</span></h4>
<small class="text-muted">Sistem Multi-Cabang</small>
</div>
<ul class="list-unstyled components p-2">
<li>
<a href="index.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-speedometer2 me-2"></i> Dashboard
</a>
</li>
<li>
<a href="pos.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-cart3 me-2"></i> Kasir (POS)
</a>
</li>
<li>
<a href="products.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-box-seam me-2"></i> Produk
</a>
</li>
<li>
<a href="members.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-people me-2"></i> Member
</a>
</li>
<li class="active">
<a href="vouchers.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded bg-primary">
<i class="bi bi-ticket-perforated me-2"></i> Voucher
</a>
</li>
<li>
<a href="#" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-graph-up me-2"></i> Laporan
</a>
</li>
</ul>
</nav>
<!-- Page Content -->
<div id="content" class="text-dark">
<nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom p-3">
<div class="container-fluid">
<button type="button" id="sidebarCollapse" class="btn btn-outline-dark me-3">
<i class="bi bi-list"></i>
</button>
<h4 class="mb-0">Manajemen Voucher</h4>
</div>
</nav>
<div class="container-fluid p-4">
<?php if (isset($success_msg)): ?>
<div class="alert alert-success"><?php echo $success_msg; ?></div>
<?php endif; ?>
<?php if (isset($error_msg)): ?>
<div class="alert alert-danger"><?php echo $error_msg; ?></div>
<?php endif; ?>
<div class="row g-4">
<div class="col-md-4">
<div class="card border-0 shadow-sm rounded-4">
<div class="card-header bg-white border-0 p-4 pb-0">
<h5 class="mb-0">Buat Voucher Baru</h5>
</div>
<div class="card-body p-4">
<form method="POST">
<input type="hidden" name="action" value="create">
<div class="mb-3">
<label class="form-label">Kode Kustom (Opsional)</label>
<input type="text" name="custom_code" class="form-control" placeholder="cth: PROMO2024">
<small class="text-muted">Kosongkan untuk pembuatan otomatis</small>
</div>
<div class="mb-3">
<label class="form-label">Jumlah Diskon (Rp)</label>
<input type="number" name="discount_amount" class="form-control" required placeholder="5000">
</div>
<div class="mb-3">
<label class="form-label">Tanggal Kedaluwarsa (Opsional)</label>
<input type="date" name="expires_at" class="form-control">
</div>
<button type="submit" class="btn btn-primary w-100 rounded-pill">Buat Voucher</button>
</form>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card border-0 shadow-sm rounded-4">
<div class="card-header bg-white border-0 p-4 pb-0">
<h5 class="mb-0">Voucher Aktif</h5>
</div>
<div class="card-body p-4">
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead>
<tr>
<th>Kode</th>
<th>Diskon</th>
<th>Status</th>
<th>Kedaluwarsa</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php foreach ($vouchers as $v): ?>
<tr>
<td><span class="badge bg-light text-dark border p-2"><?php echo htmlspecialchars($v['code']); ?></span></td>
<td class="fw-bold text-success">Rp <?php echo number_format((float)$v['discount_amount'], 0, ',', '.'); ?></td>
<td>
<?php if ($v['is_used']): ?>
<span class="badge bg-soft-danger text-danger">Terpakai</span>
<?php else: ?>
<span class="badge bg-soft-success text-success">Tersedia</span>
<?php endif; ?>
</td>
<td class="small text-muted"><?php echo $v['expires_at'] ?? 'Selamanya'; ?></td>
<td>
<button class="btn btn-sm btn-outline-primary rounded-pill" onclick="printVoucher('<?php echo $v['code']; ?>', '<?php echo number_format((float)$v['discount_amount'], 0, ',', '.'); ?>', '<?php echo htmlspecialchars($current_branch['name']); ?>')">
<i class="bi bi-printer"></i> Cetak
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Hidden Print Area -->
<div id="printArea" class="d-none">
<div class="d-flex justify-content-center p-5">
<div class="voucher-card">
<div class="d-flex justify-content-between align-items-center mb-3">
<div>
<h4 class="mb-0 fw-bold" id="printBranch"></h4>
<small>VOUCHER RESMI</small>
</div>
<div class="qr-code">
<img id="printQR" src="" alt="QR Code" style="width: 100%; height: 100%;">
</div>
</div>
<div class="text-center my-4">
<div class="display-6 fw-bold">Rp <span id="printDiscount"></span></div>
<div class="text-uppercase tracking-widest small">Voucher Diskon</div>
</div>
<div class="border-top border-white border-opacity-25 pt-3 d-flex justify-content-between align-items-center">
<div>
<small class="d-block opacity-75">KODE VOUCHER</small>
<h5 class="mb-0 fw-bold tracking-widest" id="printCode"></h5>
</div>
<div class="text-end">
<small class="d-block opacity-75">PINDAI UNTUK MENUKAR</small>
<i class="bi bi-stars"></i>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function printVoucher(code, discount, branch) {
document.getElementById('printCode').innerText = code;
document.getElementById('printDiscount').innerText = discount;
document.getElementById('printBranch').innerText = branch;
// Generate QR Code using Google Charts API
const qrUrl = `https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=${encodeURIComponent(code)}&choe=UTF-8`;
document.getElementById('printQR').src = qrUrl;
// Wait for image to load before printing
document.getElementById('printQR').onload = function() {
window.print();
};
}
document.getElementById('sidebarCollapse').addEventListener('click', function() {
document.getElementById('sidebar').classList.toggle('active');
});
</script>
</body>
</html>