เทคโนโลยี: PHP + MySQL


ฟีเจอร์ของระบบ

  1. 🛒 ลูกค้าจองชุดพร้อมกำหนดวันรับ-คืน
  2. ลูกค้านัดลองชุด
  3. 💰 ออกใบเสร็จและคำนวณค่าเช่า
  4. 🔄 คืนชุดและอัปเดตสถานะ
  5. 📊 ดูประวัติการเช่าและคืนชุด

🚀 1. สร้างฐานข้อมูล MySQL

CREATE DATABASE dress_rental;
USE dress_rental;

-- ตารางชุดให้เช่า
CREATE TABLE dresses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    size VARCHAR(50),
    price DECIMAL(10,2) NOT NULL,
    status ENUM('available', 'reserved', 'rented') DEFAULT 'available'
);

-- ตารางลูกค้า
CREATE TABLE customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    phone VARCHAR(15) NOT NULL
);

-- ตารางการจองชุด
CREATE TABLE reservations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_id INT,
    dress_id INT,
    try_date DATETIME NOT NULL,   -- วันที่นัดลองชุด
    rent_date DATE NOT NULL,      -- วันที่เช่าชุด
    return_date DATE NOT NULL,    -- วันที่คืนชุด
    status ENUM('reserved', 'rented', 'returned') DEFAULT 'reserved',
    total_price DECIMAL(10,2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (customer_id) REFERENCES customers(id),
    FOREIGN KEY (dress_id) REFERENCES dresses(id)
);

🔗 2. เชื่อมต่อฐานข้อมูล (db.php)

<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "dress_rental";

$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

👗 3. เพิ่มชุดให้เช่า (add_dress.php)

<?php
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $size = $_POST['size'];
    $price = $_POST['price'];

    $stmt = $conn->prepare("INSERT INTO dresses (name, size, price) VALUES (?, ?, ?)");
    $stmt->bind_param("ssd", $name, $size, $price);
    $stmt->execute();

    echo "เพิ่มชุดสำเร็จ!";
}
?>
<form method="post">
    ชื่อชุด: <input type="text" name="name" required><br>
    ขนาด: <input type="text" name="size"><br>
    ราคาเช่า: <input type="number" name="price" required><br>
    <button type="submit">เพิ่มชุด</button>
</form>
<a href="index.php">กลับ</a>

📅 4. จองชุด & นัดลองชุด (reserve.php)

<?php
include 'db.php';

$dresses = $conn->query("SELECT * FROM dresses WHERE status = 'available'");
$customers = $conn->query("SELECT * FROM customers");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $customer_id = $_POST['customer_id'];
    $dress_id = $_POST['dress_id'];
    $try_date = $_POST['try_date'];
    $rent_date = $_POST['rent_date'];
    $return_date = $_POST['return_date'];

    $price_query = $conn->query("SELECT price FROM dresses WHERE id = $dress_id");
    $dress = $price_query->fetch_assoc();
    $price = $dress['price'];

    $days = (strtotime($return_date) - strtotime($rent_date)) / (60 * 60 * 24);
    $total_price = $price * $days;

    $stmt = $conn->prepare("INSERT INTO reservations (customer_id, dress_id, try_date, rent_date, return_date, total_price) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("iisssd", $customer_id, $dress_id, $try_date, $rent_date, $return_date, $total_price);
    $stmt->execute();

    $conn->query("UPDATE dresses SET status='reserved' WHERE id=$dress_id");

    echo "จองชุดสำเร็จ!";
}
?>
<form method="post">
    ลูกค้า: <select name="customer_id">
        <?php while ($row = $customers->fetch_assoc()): ?>
            <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
        <?php endwhile; ?>
    </select><br>
    ชุด: <select name="dress_id">
        <?php while ($row = $dresses->fetch_assoc()): ?>
            <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?> (฿<?php echo $row['price']; ?>)</option>
        <?php endwhile; ?>
    </select><br>
    นัดลองชุด: <input type="datetime-local" name="try_date" required><br>
    วันรับชุด: <input type="date" name="rent_date" required><br>
    วันคืนชุด: <input type="date" name="return_date" required><br>
    <button type="submit">จองชุด</button>
</form>
<a href="index.php">กลับ</a>

🏷 5. คืนชุด (return.php)

<?php
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $reservation_id = $_POST['reservation_id'];

    $stmt = $conn->prepare("UPDATE reservations SET status='returned' WHERE id=?");
    $stmt->bind_param("i", $reservation_id);
    $stmt->execute();

    $conn->query("UPDATE dresses SET status='available' WHERE id=(SELECT dress_id FROM reservations WHERE id=$reservation_id)");

    echo "คืนชุดเรียบร้อย!";
}

$rented = $conn->query("SELECT * FROM reservations WHERE status='rented'");
?>
<form method="post">
    คืนชุด: <select name="reservation_id">
        <?php while ($row = $rented->fetch_assoc()): ?>
            <option value="<?php echo $row['id']; ?>">ลูกค้า <?php echo $row['customer_id']; ?> - ชุด <?php echo $row['dress_id']; ?></option>
        <?php endwhile; ?>
    </select><br>
    <button type="submit">คืนชุด</button>
</form>
<a href="index.php">กลับ</a>

🧾 6. ออกใบเสร็จ (receipt.php)

<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;

include 'db.php';
$id = $_GET['id'];

$reservation = $conn->query("SELECT customers.name AS customer, dresses.name AS dress, reservations.total_price, reservations.rent_date, reservations.return_date FROM reservations
JOIN customers ON reservations.customer_id = customers.id
JOIN dresses ON reservations.dress_id = dresses.id
WHERE reservations.id = $id")->fetch_assoc();

$dompdf = new Dompdf();
$html = "
    <h1>ใบเสร็จ</h1>
    <p>ลูกค้า: {$reservation['customer']}</p>
    <p>ชุดที่เช่า: {$reservation['dress']}</p>
    <p>เช่าวันที่: {$reservation['rent_date']}</p>
    <p>คืนวันที่: {$reservation['return_date']}</p>
    <h2>รวม: ฿{$reservation['total_price']}</h2>
";
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("receipt_$id.pdf");
?>

🎯 ฟีเจอร์เพิ่มเติม

  • 📅 ลูกค้าจองและนัดลองชุดออนไลน์
  • 🔔 แจ้งเตือนลูกค้าผ่าน SMS หรือ LINE
  • 📊 สร้างรายงานรายเดือน
  • 💳 รองรับการชำระเงินออนไลน์

🔥 ระบบนี้ช่วยให้ร้านเช่าชุดทำงานง่ายขึ้น และลดข้อผิดพลาดจากการจอง! 🚀

,


ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *