Php
<?php
// Koneksi database
$conn = mysqli_connect("localhost", "root", "", "kasir");
$struk_data = null;
// Proses tambah barang
if(isset($_POST["tambah"])) {
$nama = $_POST["nama"];
$harga = $_POST["harga"];
$qty = $_POST["qty"];
$subtotal = $harga * $qty;
$sql = "INSERT INTO transaksi (nama_barang, harga, qty, subtotal)
VALUES ('$nama', $harga, $qty, $subtotal)";
mysqli_query($conn, $sql);
}
// Proses hapus barang
if(isset($_POST["hapus"])) {
$id = $_POST["id"];
mysqli_query($conn, "DELETE FROM transaksi WHERE id = $id");
}
// Proses reset semua
if(isset($_POST["reset"])) {
mysqli_query($conn, "DELETE FROM transaksi");
}
// Proses selesai transaksi
if(isset($_POST["selesai"])) {
$ambil_struk = mysqli_query($conn, "SELECT * FROM transaksi");
$items_struk = [];
$total_struk = 0;
while($row = mysqli_fetch_assoc($ambil_struk)) {
$items_struk[] = $row;
$total_struk += $row['subtotal'];
}
$struk_data = [
"tanggal" => date("d/m/Y H:i:s"),
"items" => $items_struk,
"total" => $total_struk,
"kode" => "TRX" . date("YmdHis")
];
mysqli_query($conn, "DELETE FROM transaksi");
}
// Ambil semua data
$hasil = mysqli_query($conn, "SELECT * FROM transaksi");
// Hitung total
$total = 0;
while($row = mysqli_fetch_assoc($hasil)) {
$total += $row["subtotal"];
}
mysqli_data_seek($hasil, 0);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Kasir Sederhana</title>
</head>
<body>
<h2>Aplikasi Kasir Sederhana</h2>
<hr>
<!-- Total Belanja -->
<h3>TOTAL BELANJA: Rp <?php echo number_format($total, 0, ',', '.'); ?></h3>
<hr>
<!-- Form Tambah Barang -->
<h4>Tambah Barang</h4>
<form method="POST">
<label>Nama Barang:</label>
<input type="text" name="nama" required>
<label>Harga:</label>
<input type="number" name="harga" required>
<label>Jumlah:</label>
<input type="number" name="qty" value="1" required>
<button type="submit" name="tambah">Tambah ke Keranjang</button>
</form>
<hr>
<!-- Tabel Daftar Belanja -->
<h4>Daftar Belanja</h4>
<table border="1" cellpadding="8" cellspacing="0">
<tr style="background: #ccc;">
<th>No</th>
<th>Barang</th>
<th>Harga</th>
<th>Qty</th>
<th>Subtotal</th>
<th>Aksi</th>
</tr>
<?php if(mysqli_num_rows($hasil) == 0): ?>
<tr>
<td colspan="6" align="center">Belum ada barang</td>
</tr>
<?php else: ?>
<?php $no = 1; while($row = mysqli_fetch_assoc($hasil)): ?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $row['nama_barang']; ?></td>
<td>Rp <?php echo number_format($row['harga'], 0, ',', '.'); ?></td>
<td><?php echo $row['qty']; ?></td>
<td>Rp <?php echo number_format($row['subtotal'], 0, ',', '.'); ?></td>
<td>
<form method="POST" style="display: inline;">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<button type="submit" name="hapus" onclick="return confirm('Hapus item ini?')">Hapus</button>
</form>
</td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</table>
<br>
<!-- Tombol Reset dan Selesai -->
<form method="POST" style="display: inline;">
<button type="submit" name="reset" onclick="return confirm('Reset semua transaksi?')">Reset Semua</button>
</form>
<?php if(mysqli_num_rows($hasil) > 0): ?>
<form method="POST" style="display: inline;">
<button type="submit" name="selesai" onclick="return confirm('Selesaikan transaksi?')">Selesai & Cetak Struk</button>
</form>
<?php endif; ?>
<hr>
<!-- Preview Struk -->
<h4>Struk Belanja</h4>
<div style="border: 1px solid #000; padding: 10px; width: 300px;">
<div align="center">
<strong>TOKO KITA</strong><br>
Jl. Belajar No. 123<br>
<small><?php echo date('d/m/Y H:i'); ?></small>
<hr>
</div>
<?php
$data_struk = mysqli_query($conn, "SELECT * FROM transaksi");
if(mysqli_num_rows($data_struk) == 0):
?>
<div align="center">- Belum ada transaksi -</div>
<?php else: ?>
<?php
$total_struk = 0;
while($row = mysqli_fetch_assoc($data_struk)):
$total_struk += $row['subtotal'];
?>
<div style="display: flex; justify-content: space-between;">
<span><?php echo $row['nama_barang']; ?> x<?php echo $row['qty']; ?></span>
<span>Rp <?php echo number_format($row['subtotal'], 0, ',', '.'); ?></span>
</div>
<?php endwhile; ?>
<hr>
<div style="display: flex; justify-content: space-between; font-weight: bold;">
<span>TOTAL</span>
<span>Rp <?php echo number_format($total_struk, 0, ',', '.'); ?></span>
</div>
<?php endif; ?>
<hr>
<div align="center">Terima kasih</div>
</div>
<!-- Modal Struk untuk Print -->
<?php if($struk_data): ?>
<div id="modalStruk" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 999;">
<div style="background: white; padding: 20px; width: 320px;">
<div id="strukPrint">
<div align="center">
<strong>TOKO KITA</strong><br>
Jl. Belajar No. 123<br>
<small><?php echo $struk_data["tanggal"]; ?></small><br>
<small><?php echo $struk_data["kode"]; ?></small>
<hr>
</div>
<?php foreach($struk_data["items"] as $item): ?>
<div style="display: flex; justify-content: space-between;">
<span><?php echo $item['nama_barang']; ?> x<?php echo $item['qty']; ?></span>
<span>Rp <?php echo number_format($item['subtotal'], 0, ',', '.'); ?></span>
</div>
<?php endforeach; ?>
<hr>
<div style="display: flex; justify-content: space-between; font-weight: bold;">
<span>TOTAL</span>
<span>Rp <?php echo number_format($struk_data["total"], 0, ',', '.'); ?></span>
</div>
<hr>
<div align="center">
Terima kasih!<br>
Selamat berbelanja kembali
</div>
</div>
<br>
<div align="center">
<button onclick="cetakStruk()">Cetak Struk</button>
<button onclick="tutupModal()">Tutup</button>
</div>
</div>
</div>
<script>
function cetakStruk() {
var isi = document.getElementById('strukPrint').innerHTML;
var win = window.open('', '', 'width=400,height=600');
win.document.write('<html><head><title>Struk Belanja</title></head><body>');
win.document.write(isi);
win.document.write('</body></html>');
win.print();
win.close();
}
function tutupModal() {
document.getElementById('modalStruk').style.display = 'none';
}
</script>
<?php endif; ?>
</body>
</html>
Geser ke samping untuk lihat kode lengkap