diff --git a/controllers/bookingController.js b/controllers/bookingController.js index bfe3816520503a2f02158c1777e0589ed4d004e5..bdc1cae05609b6bb79e29dac470ff96aa2d149b3 100644 --- a/controllers/bookingController.js +++ b/controllers/bookingController.js @@ -120,25 +120,44 @@ exports.bookingSummary = async (req, res) => { return res.status(400).send("ไม่พบรหัสการจอง"); } - // ดึงข้อมูลการจองจากทั้ง appointments และ appointment_history - let appointment = await bookingModel.getAppointmentById(appointmentId); - + let appointment = null; + + // ลองดึงข้อมูลจากตาราง appointments ก่อน + try { + appointment = await bookingModel.getAppointmentById(appointmentId); + } catch (err) { + // หากไม่พบใน appointments ให้ข้ามไปลองดึงจาก appointment_history + } + if (!appointment) { - throw new Error("ไม่พบการจองในฐานข้อมูล"); + appointment = await bookingModel.getAppointmentHistoryById(appointmentId); + if (!appointment) { + return res.status(404).send("ไม่พบการจองทั้งใน appointments และ appointment_history"); + } } const user = await bookingModel.getUserById(appointment.user_id); const service = await bookingModel.getServiceById(appointment.service_id); - const slot = await bookingModel.getBookingSlotById(appointment.booking_slot_id); + + // ดึง slot เฉพาะเมื่อมี booking_slot_id + let slot = null; + if (appointment.booking_slot_id) { + try { + slot = await bookingModel.getBookingSlotById(appointment.booking_slot_id); + } catch (err) { + // หากไม่พบ slot ก็ให้ slot ยังคงเป็น null + } + } res.render('customer/bookingSummary', { appointment, user, service, slot }); } catch (error) { console.error(error); - res.status(500).send("เกิดข้อผิดพลาดในการแสดงสรุปการจอง"); + res.status(500).send("เกิดข้อผิดพลาดในการแสดงสรุปการจอง: " + error.message); } }; + exports.getAllAppointmentPage = async (req, res) => { try { const user_id = req.session.userId; @@ -185,3 +204,18 @@ exports.cancelAppointment = async (req, res) => { } }; +exports.updateNote = async (req, res) => { + try { + const { appointmentId, note } = req.body; + if (!appointmentId) { + return res.status(400).send("ไม่พบรหัสการจอง"); + } + await bookingModel.updateAppointmentNote(appointmentId, note); + res.redirect('/customer/bookingSummary?id=' + appointmentId); + } catch (error) { + console.error(error); + res.status(500).send('เกิดข้อผิดพลาดในการอัพเดทหมายเหตุ'); + } +}; + + diff --git a/models/bookingModel.js b/models/bookingModel.js index 0e1d412a6c13f45e8115aceb8b045c5a364eaa39..94c34422cb837cc3e92e2529682f26a4213c76dc 100644 --- a/models/bookingModel.js +++ b/models/bookingModel.js @@ -74,21 +74,16 @@ module.exports = { }); }, // ฟังก์ชันเพิ่มเติมสำหรับหน้า bookingSummary - getAppointmentById: (appointmentId) => { - return new Promise((resolve, reject) => { - const sql = ` - SELECT * FROM appointments WHERE appointment_id = ? - UNION - SELECT * FROM appointment_history WHERE appointment_id = ? - `; - db.query(sql, [appointmentId, appointmentId], (err, results) => { - if (err) return reject(err); - if (results.length === 0) return reject(new Error('ไม่พบการจอง')); - resolve(results[0]); - }); - }); - }, - + getAppointmentById: (appointmentId) => { + return new Promise((resolve, reject) => { + const sql = 'SELECT * FROM appointments WHERE appointment_id = ?'; + db.query(sql, [appointmentId], (err, results) => { + if (err) return reject(err); + if (results.length === 0) return reject(new Error('ไม่พบการจอง')); + resolve(results[0]); + }); + }); + }, getUserById: (user_id) => { return new Promise((resolve, reject) => { @@ -135,6 +130,21 @@ module.exports = { }); }); }, + + getAppointmentHistoryById: (appointmentId) => { + return new Promise((resolve, reject) => { + const sql = 'SELECT * FROM appointment_history WHERE appointment_id = ?'; + db.query(sql, [appointmentId], (err, results) => { + if (err) return reject(err); + if (results.length === 0) { + // ไม่พบใน history + return resolve(null); + } + resolve(results[0]); + }); + }); + }, + getUserById: (user_id) => { return new Promise((resolve, reject) => { @@ -194,10 +204,10 @@ module.exports = { const sql = 'SELECT * FROM appointment_history WHERE user_id = ? AND appointment_status = ?'; db.query(sql, [user_id, status], (err, results) => { if (err) return reject(err); - resolve(results); // ส่งข้อมูลการจองจาก history ที่ตรงกับสถานะ + resolve(results); }); }); - }, + }, getPaymentStatusByAppointmentId: (appointmentId) => { return new Promise((resolve, reject) => { @@ -210,47 +220,68 @@ module.exports = { }); }, - addToHistory: (appointmentId) => { + addToHistory: (appointmentId) => { return new Promise((resolve, reject) => { - - const selectSql = ` - SELECT a.appointment_id, a.user_id, a.service_id, a.note, - p.payment_status - FROM appointments a - LEFT JOIN payments p ON a.appointment_id = p.appointment_id - WHERE a.appointment_id = ? + const selectSql = ` + SELECT a.appointment_id, a.user_id, a.service_id, a.booking_slot_id, a.note, + p.payment_status + FROM appointments a + LEFT JOIN payments p ON a.appointment_id = p.appointment_id + WHERE a.appointment_id = ? + `; + db.query(selectSql, [appointmentId], (err, results) => { + if (err) return reject(err); + if (results.length === 0) return reject(new Error('ไม่พบการจอง')); + + const appointment = results[0]; + const bookingSlotId = appointment.booking_slot_id; // เก็บไว้ใช้อัปเดต slot + + const insertSql = ` + INSERT INTO appointment_history + (appointment_id, user_id, service_id, booking_slot_id, payment_status, note, appointment_status) + VALUES (?, ?, ?, ?, ?, ?, ?) `; - db.query(selectSql, [appointmentId], (err, results) => { + db.query(insertSql, [ + appointment.appointment_id, + appointment.user_id, + appointment.service_id, + appointment.booking_slot_id, // ต้องมีคอลัมน์ booking_slot_id ใน appointment_history + appointment.payment_status || 'Pending', + appointment.note, + 'Cancelled' + ], (err, result) => { + if (err) return reject(err); + + // เมื่อย้ายข้อมูลเสร็จแล้ว อัปเดต booking_slots ให้ is_available = 1 + const updateSlotSql = ` + UPDATE booking_slots + SET is_available = 1 + WHERE booking_slot_id = ? + `; + db.query(updateSlotSql, [bookingSlotId], (err, result) => { if (err) return reject(err); - if (results.length === 0) return reject(new Error('ไม่พบการจอง')); - - const appointment = results[0]; - - const insertSql = ` - INSERT INTO appointment_history - (appointment_id, user_id, service_id, payment_status, note, appointment_status) - VALUES (?, ?, ?, ?, ?, ?) - `; - db.query(insertSql, [ - appointment.appointment_id, - appointment.user_id, - appointment.service_id, - appointment.payment_status || 'Pending', - appointment.note, - 'Cancelled' - ], (err, result) => { - if (err) return reject(err); - - // ลบข้อมูลออกจาก appointments หลังย้ายสำเร็จ - const deleteSql = 'DELETE FROM appointments WHERE appointment_id = ?'; - db.query(deleteSql, [appointmentId], (err, result) => { - if (err) return reject(err); - resolve(result); - }); + + // ลบข้อมูลออกจาก appointments + const deleteSql = 'DELETE FROM appointments WHERE appointment_id = ?'; + db.query(deleteSql, [appointmentId], (err, result) => { + if (err) return reject(err); + resolve(result); }); + }); }); + }); }); - }, + }, + + updateAppointmentNote: (appointmentId, note) => { + return new Promise((resolve, reject) => { + const sql = 'UPDATE appointments SET note = ? WHERE appointment_id = ?'; + db.query(sql, [note, appointmentId], (err, result) => { + if (err) return reject(err); + resolve(result); + }); + }); + }, diff --git a/routes/customer.js b/routes/customer.js index ec4773ee39918b07462b2c0b81cb708ceb35eecb..434580d73fec6bbda5b491b454099990b96f0e26 100644 --- a/routes/customer.js +++ b/routes/customer.js @@ -36,5 +36,8 @@ router.get('/allAppointments', checkCustomer, bookingController.getAllAppointmen // เส้นทางสำหรับยกเลิกการจอง router.post('/cancelAppointment', checkCustomer, bookingController.cancelAppointment); +// แก้ไขnote หน้า bookingSummary +router.post('/updateNote', checkCustomer, bookingController.updateNote); + module.exports = router; diff --git a/views/customer/allAppointments.ejs b/views/customer/allAppointments.ejs index bbf5ab22ebe9972ae189a893fd65fd935c5d3814..d0ea6016e282e1986be90e81a35b9553e37dc773 100644 --- a/views/customer/allAppointments.ejs +++ b/views/customer/allAppointments.ejs @@ -167,6 +167,7 @@ nav { <% }) %> </div> + <nav> <a href="/customer/profile"><i class="fa-solid fa-user"></i> โปรไฟล์</a> <a href="/customer/appointment"><i class="fa-solid fa-calendar-check"></i> จอง</a> diff --git a/views/customer/bookingSummary.ejs b/views/customer/bookingSummary.ejs index e4898c48043042d72c357462e866c809b662a3a0..24f2f8e03ff75377b262bf344e4f922bc40caa76 100644 --- a/views/customer/bookingSummary.ejs +++ b/views/customer/bookingSummary.ejs @@ -88,11 +88,42 @@ <p><strong>หมายเลขการจอง:</strong> <%= appointment.appointment_id %></p> <p><strong>ชื่อผู้จอง:</strong> <%= user.first_name %> <%= user.last_name %></p> <p><strong>บริการ:</strong> <%= service.service_name %></p> - <p><strong>วันที่เข้ารับบริการ:</strong> <%= new Date(slot.date).toLocaleDateString('th-TH') %></p> <!-- Display only date --> - <p><strong>เวลาเข้ารับบริการ:</strong> <%= slot.start_time %></p> - <p><strong>สถานะการจอง:</strong> <%= appointment.appointment_status %></p> <!-- Display appointment status --> + <% if (slot) { %> + <p><strong>วันที่เข้ารับบริการ:</strong> <%= new Date(slot.date).toLocaleDateString('th-TH') %></p> + <p><strong>เวลาเข้ารับบริการ:</strong> <%= slot.start_time %></p> + <% } else { %> + <p><strong>วันที่เข้ารับบริการ:</strong> ไม่พบข้อมูล (อาจถูกยกเลิก)</p> + <% } %> + + <p><strong>สถานะการจอง:</strong> <%= appointment.appointment_status %></p> + + + <!-- ส่วนแสดงหมายเหตุ --> + <% if (appointment.appointment_status === 'Cancelled') { %> + <!-- กรณีถูกยกเลิก: แสดง Note เฉย ๆ แต่ไม่ให้แก้ไข --> + <p><strong>หมายเหตุ:</strong> <%= appointment.note %></p> + <p style="color:red;">ไม่สามารถแก้ไขหมายเหตุได้ เนื่องจากการจองถูกยกเลิก</p> + <% } else { %> + <!-- กรณีสถานะอื่น ๆ: แสดงฟอร์มแก้ไข Note ได้ตามปกติ --> + <p><strong>หมายเหตุ:</strong></p> + <form action="/customer/updateNote" method="POST"> + <input type="hidden" name="appointmentId" value="<%= appointment.appointment_id %>"> + <textarea name="note" rows="3" cols="50"><%= appointment.note || '' %></textarea> + <br> + <button type="submit" class="btn btn-primary">บันทึกหมายเหตุ</button> + </form> + <% } %> + + <form action="/customer/updateNote" method="POST"> + <input type="hidden" name="appointmentId" value="<%= appointment.appointment_id %>"> + <textarea name="note" rows="3" cols="50"><%= appointment.note || '' %></textarea> + <br> + <button type="submit" class="btn btn-primary">บันทึกหมายเหตุ</button> + </form> + <p class="alert-info">กรุณามาก่อนเวลา 10 นาที</p> </div> + <div class="actions"> <!-- ปุ่มไปยังหน้าชำระมัดจำ --> <a href="#" class="btn btn-primary">ชำระมัดจำทันที</a>