From bcdcbbfff439f0ff9b7c589565e446289a8b4e99 Mon Sep 17 00:00:00 2001 From: QuocAnh Date: Sat, 4 Nov 2023 23:19:48 +0700 Subject: [PATCH 01/17] [update] view order log by press button --- .../java/Controllers/AdminController.java | 204 +++++++++++------- .../java/Controllers/CheckoutController.java | 1 - src/main/java/Models/Order.java | 11 +- .../jspf/admin/components/adminSidebar.jspf | 15 -- .../jspf/admin/components/history.jspf | 40 ++++ .../jspf/admin/components/success.jspf | 3 +- .../jspf/admin/components/updateOrder.jspf | 4 +- src/main/webapp/admin.jsp | 3 +- src/main/webapp/assets/js/admin.js | 18 +- src/main/webapp/assets/js/adminNotify.js | 4 + 10 files changed, 187 insertions(+), 116 deletions(-) create mode 100644 src/main/webapp/WEB-INF/jspf/admin/components/history.jspf diff --git a/src/main/java/Controllers/AdminController.java b/src/main/java/Controllers/AdminController.java index 875e60fe..6e0866bb 100644 --- a/src/main/java/Controllers/AdminController.java +++ b/src/main/java/Controllers/AdminController.java @@ -29,9 +29,13 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigDecimal; +import java.net.HttpURLConnection; +import java.net.URL; import java.sql.Timestamp; import java.time.LocalDateTime; import java.util.ArrayList; @@ -39,18 +43,73 @@ import java.util.Comparator; import java.util.Date; import java.util.List; +import org.json.JSONArray; +import org.json.JSONObject; public class AdminController extends HttpServlet { - + + private String sendGetRequest(String apiURL) { + try { + URL url = new URL(apiURL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("GET"); + int responseCode = connection.getResponseCode(); + + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // Parse the JSON array response + JSONArray jsonArray = new JSONArray(response.toString()); + + // Check if the array is not empty + if (jsonArray.length() > 0) { + // Get the first object from the array + JSONObject jsonObject = jsonArray.getJSONObject(0); + // Extract payment_time from the object + String paymentTime = jsonObject.getString("payment_time"); + return paymentTime; + } else { + // Handle empty JSON array (no elements found) + return null; + } + + + } else { + // Xử lý trường hợp không thành công khi gọi API + return null; + } + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + private void doGetList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - FoodDAO foodDAO = new FoodDAO(); - List foodList = foodDAO.getAllList(); + CustomerDAO customerDAO = new CustomerDAO(); + FoodDAO foodDAO = new FoodDAO(); AccountDAO accountDAO = new AccountDAO(); + StaffDAO staffDAO = new StaffDAO(); + PromotionManagerDAO promotionManagerDAO = new PromotionManagerDAO(); + OrderDAO orderDAO = new OrderDAO(); + VoucherDAO voucherDAO = new VoucherDAO(); + + List foodList = foodDAO.getAllList(); List userAccountList = accountDAO.getAllUser(); - CustomerDAO customerDAO = new CustomerDAO(); List customerList = customerDAO.getAllCustomer(); + List accountList = accountDAO.getAllRole(); + List StaffList = staffDAO.getAllStaff(); + List PromotionManagerList = promotionManagerDAO.getAllPromotionManager(); + List voucherList = voucherDAO.getAllList(); List userList = new ArrayList<>(); for (Account account : userAccountList) { @@ -87,13 +146,6 @@ private void doGetList(HttpServletRequest request, HttpServletResponse response) } } - StaffDAO staffDAO = new StaffDAO(); - PromotionManagerDAO promotionManagerDAO = new PromotionManagerDAO(); - - List accountList = accountDAO.getAllRole(); - List StaffList = staffDAO.getAllStaff(); - List PromotionManagerList = promotionManagerDAO.getAllPromotionManager(); - List roleList = new ArrayList<>(); for (Account a : accountList) { if (a.getAccountType().equals("staff")) { @@ -119,33 +171,24 @@ private void doGetList(HttpServletRequest request, HttpServletResponse response) } } } - OrderDAO orderDAO = new OrderDAO(); + List orderList = orderDAO.getAllList(); - // Sorting orderList based on status in ascending order and ID in descending order -// Collections.sort(orderList, new Comparator() { -// @Override -// public int compare(Order o1, Order o2) { -// // Compare status in ascending order -// int statusComparison = Integer.compare(o1.getOrderStatusID(), o2.getOrderStatusID()); -// -// // If status is equal, sort by ID in descending order -// if (statusComparison == 0) { -// return Integer.compare(o2.getOrderID(), o1.getOrderID()); -// } -// -// // Else sort by status in ascending order -// return statusComparison; -// } -// }); - for (int i = 0; i < orderList.size(); i++){ + for (int i = 0; i < orderList.size(); i++) { String Orderfirstname = customerDAO.getCustomer(orderList.get(i).getCustomerID()).getFirstName(); String Orderlastname = customerDAO.getCustomer(orderList.get(i).getCustomerID()).getLastName(); + String payment_status = "Chưa thanh toán"; + // Tạo URL cho việc gọi API + String apiURL = "http://localhost:8001/check_order_payment/" + orderList.get(i).getOrderID(); + // Thực hiện HTTP request để lấy vnpay_payment_url + String payment_time = sendGetRequest(apiURL); + if (payment_time != null) { + payment_status = "Đã thanh toán"; + } + orderList.get(i).setPayment_status(payment_status); orderList.get(i).setFirstname(Orderfirstname); orderList.get(i).setLastname(Orderlastname); } - - VoucherDAO voucherDAO = new VoucherDAO(); - List voucherList = voucherDAO.getAllList(); + request.setAttribute("foodList", foodList); request.setAttribute("userList", userList); request.setAttribute("roleList", roleList); @@ -278,7 +321,7 @@ private void doPostAddUser(HttpServletRequest request, HttpServletResponse respo } } - + private void doPostUpdateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int userID = Integer.parseInt(request.getParameter("txtUserID")); @@ -300,14 +343,14 @@ private void doPostUpdateUser(HttpServletRequest request, HttpServletResponse re account.setAccountID(userID); Customer customer = new Customer(firstname, lastname, gender, phoneNumber, address); int result1 = 0; - if (customerID == 0){ + if (customerID == 0) { result1 = customerDAO.add(customer); account.setCustomerID(customerDAO.getLatestCustomer().getCustomerID()); } else { customer.setCustomerID(customerID); - result1 = customerDAO.update(customer); + result1 = customerDAO.update(customer); } - + if (result1 == 1) { int result = accountDAO.update(account); int result2 = accountDAO.updateCustomerID(account); @@ -318,7 +361,7 @@ private void doPostUpdateUser(HttpServletRequest request, HttpServletResponse re return; } } - + private void doPostDeleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -614,7 +657,7 @@ private void doPostDeleteVoucher(HttpServletRequest request, HttpServletResponse return; } } - + private void doPostUpdateOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int orderID = Integer.parseInt(request.getParameter("txtOrderID")); @@ -626,31 +669,31 @@ private void doPostUpdateOrder(HttpServletRequest request, HttpServletResponse r Double orderTotal = Double.parseDouble(request.getParameter("txtOrderTotal")); BigDecimal orderTotalPay = BigDecimal.valueOf(orderTotal); - + byte orderStatusID = 5; - if (status.equals("Chờ xác nhận")){ + if (status.equals("Chờ xác nhận")) { orderStatusID = 1; - } else if (status.equals("Đang chuẩn bị món")){ + } else if (status.equals("Đang chuẩn bị món")) { orderStatusID = 2; - } else if (status.equals("Đang giao")){ + } else if (status.equals("Đang giao")) { orderStatusID = 3; - } else if (status.equals("Đã giao")){ + } else if (status.equals("Đã giao")) { orderStatusID = 4; - } - + } + byte paymentMethodID = 3; - if (paymentmethod.equals("Thẻ tín dụng")){ + if (paymentmethod.equals("Thẻ tín dụng")) { paymentMethodID = 1; - } else if (paymentmethod.equals("Thẻ ghi nợ")){ + } else if (paymentmethod.equals("Thẻ ghi nợ")) { paymentMethodID = 2; } - + HttpSession session = request.getSession(); OrderDAO orderDAO = new OrderDAO(); Order order = new Order(orderID, orderStatusID, paymentMethodID, phonenumber, address, note, orderTotalPay); - + Order oldOrder = orderDAO.getOrder(orderID); - + int result = orderDAO.updateForAdmin(order); session.setAttribute("tabID", 6); if (result == 1) { @@ -661,13 +704,13 @@ private void doPostUpdateOrder(HttpServletRequest request, HttpServletResponse r OrderLog log = new OrderLog(orderID, "Cập nhật thông tin đơn hàng", logTime); log.setAdmin_id(adminID); logDAO.addAdminLog(log); - if (oldOrder.getOrderStatusID() != orderStatusID){ + if (oldOrder.getOrderStatusID() != orderStatusID) { OrderLog logStatusOrder = new OrderLog(orderID, "Cập nhật trạng thái đơn hàng: " + status, logTime); logStatusOrder.setAdmin_id(adminID); logDAO.addAdminLog(logStatusOrder); } - - if (oldOrder.getOrderTotal() != orderTotalPay){ + + if (oldOrder.getOrderTotal() != orderTotalPay) { OrderLog logTotalOrder = new OrderLog(orderID, "Cập nhật thanh toán đơn hàng: " + orderTotalPay, logTime); logTotalOrder.setAdmin_id(adminID); logDAO.addAdminLog(logTotalOrder); @@ -680,7 +723,7 @@ private void doPostUpdateOrder(HttpServletRequest request, HttpServletResponse r return; } } - + private void doPostDeleteOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -697,7 +740,7 @@ private void doPostDeleteOrder(HttpServletRequest request, HttpServletResponse r HttpSession session = request.getSession(); OrderDAO dao = new OrderDAO(); int result = dao.deleteMultiple(orderIDList); - + session.setAttribute("tabID", 6); // TODO implement a deletion status message after page reload // Redirect or forward to another page if necessary @@ -706,9 +749,9 @@ private void doPostDeleteOrder(HttpServletRequest request, HttpServletResponse r LocalDateTime currentTime = LocalDateTime.now(); byte adminID = (byte) session.getAttribute("adminID"); Timestamp logTime = Timestamp.valueOf(currentTime); - for (int i = 0; i < orderIDList.size(); i++) { + for (int i = 0; i < orderIDList.size(); i++) { OrderLog log = new OrderLog(orderIDList.get(i), "Xóa đơn hàng", logTime); - log.setAdmin_id(adminID); + log.setAdmin_id(adminID); logDAO.addAdminLog(log); } response.sendRedirect("/admin#success_delete_order"); @@ -718,7 +761,7 @@ private void doPostDeleteOrder(HttpServletRequest request, HttpServletResponse r return; } } - + private void doPostNextOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -735,7 +778,7 @@ private void doPostNextOrder(HttpServletRequest request, HttpServletResponse res HttpSession session = request.getSession(); OrderDAO dao = new OrderDAO(); int result = dao.changeStatusMultiple(orderIDList); - + session.setAttribute("tabID", 6); // TODO implement a deletion status message after page reload // Redirect or forward to another page if necessary @@ -744,9 +787,9 @@ private void doPostNextOrder(HttpServletRequest request, HttpServletResponse res LocalDateTime currentTime = LocalDateTime.now(); byte adminID = (byte) session.getAttribute("adminID"); Timestamp logTime = Timestamp.valueOf(currentTime); - for (int i = 0; i < orderIDList.size(); i++) { + for (int i = 0; i < orderIDList.size(); i++) { OrderLog log = new OrderLog(orderIDList.get(i), "Cập nhật trạng thái đơn hàng", logTime); - log.setAdmin_id(adminID); + log.setAdmin_id(adminID); logDAO.addAdminLog(log); } response.sendRedirect("/admin#success_next_order"); @@ -756,27 +799,32 @@ private void doPostNextOrder(HttpServletRequest request, HttpServletResponse res return; } } - + private void doGetOrderHistory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); - session.setAttribute("tabID", 7); - int orderID = Integer.parseInt(request.getParameter("txtOrderID")); - OrderLogDAO dao = new OrderLogDAO(); - StaffDAO staffDAO = new StaffDAO(); - AdminDAO adminDAO = new AdminDAO(); - List logList = new ArrayList<>(); - logList = dao.getAllListByOrderID(orderID); - for (int i = 0; i < logList.size(); i++){ - if (logList.get(i).getAdmin_id() != 0) { - logList.get(i).setFullname(adminDAO.getAdmin(logList.get(i).getAdmin_id()).getFullName()); - } if (logList.get(i).getStaff_id()!= 0) { - logList.get(i).setFullname(staffDAO.getStaff(logList.get(i).getStaff_id()).getFullName()); + session.setAttribute("tabID", 6); + String path = request.getRequestURI(); + if (path.startsWith("/admin/history/")) { + String[] s = path.split("/"); + int orderID = Integer.parseInt(s[s.length - 1]); + OrderLogDAO dao = new OrderLogDAO(); + StaffDAO staffDAO = new StaffDAO(); + AdminDAO adminDAO = new AdminDAO(); + List logList = new ArrayList<>(); + logList = dao.getAllListByOrderID(orderID); + for (int i = 0; i < logList.size(); i++) { + if (logList.get(i).getAdmin_id() != 0) { + logList.get(i).setFullname(adminDAO.getAdmin(logList.get(i).getAdmin_id()).getFullName()); + } + if (logList.get(i).getStaff_id() != 0) { + logList.get(i).setFullname(staffDAO.getStaff(logList.get(i).getStaff_id()).getFullName()); + } } - } - - session.setAttribute("logList", logList); - response.sendRedirect("/admin"); + System.out.println("Thuaaaaaaaaaaa"); + session.setAttribute("logList", logList); + response.sendRedirect("/admin#history"); + } } // -

@@ -157,14 +150,6 @@ Đơn món -
diff --git a/src/main/webapp/WEB-INF/jspf/admin/components/history.jspf b/src/main/webapp/WEB-INF/jspf/admin/components/history.jspf new file mode 100644 index 00000000..54cdcdfb --- /dev/null +++ b/src/main/webapp/WEB-INF/jspf/admin/components/history.jspf @@ -0,0 +1,40 @@ +<%-- any content can be specified here e.g.: --%> +<%@ page pageEncoding="UTF-8" %> + + + diff --git a/src/main/webapp/WEB-INF/jspf/admin/components/success.jspf b/src/main/webapp/WEB-INF/jspf/admin/components/success.jspf index 7cc4821b..12a828c4 100644 --- a/src/main/webapp/WEB-INF/jspf/admin/components/success.jspf +++ b/src/main/webapp/WEB-INF/jspf/admin/components/success.jspf @@ -2,8 +2,7 @@ <%@ page pageEncoding="UTF-8" %> - diff --git a/src/main/webapp/assets/js/admin.js b/src/main/webapp/assets/js/admin.js index cdebb69a..1c42f65e 100644 --- a/src/main/webapp/assets/js/admin.js +++ b/src/main/webapp/assets/js/admin.js @@ -288,20 +288,6 @@ $(document).on("click", "#btn-next-order", function () { $(document).on("click", "#btn-history-order", function () { let orderID = $(this).attr("data-order-id"); - // Send AJAX request to addToCart servlet endpoint - $.ajax({ - type: "GET", - url: "admin/history", - data: { - orderID: orderID - }, - success: function (response) { - console.log("Change order status successfully."); - }, - error: function (error) { - console.error("Error occurred: " + error.responseText); - } - }); -// window.location.href = 'admin#history'; - window.location.reload(); + window.location.href = '/admin/history/' + orderID; +// window.location.reload(); }); \ No newline at end of file diff --git a/src/main/webapp/assets/js/adminNotify.js b/src/main/webapp/assets/js/adminNotify.js index cb06998a..53b0a29d 100644 --- a/src/main/webapp/assets/js/adminNotify.js +++ b/src/main/webapp/assets/js/adminNotify.js @@ -266,4 +266,8 @@ $(document).ready(function () { $('#failure_next_order').modal('hide'); }, 3000); }; + + if (window.location.hash === '#history') { + $('#history-order-modal').modal('show'); + }; }); From e109292be33ec5e2e445a6c4b05bf6518f9ac90e Mon Sep 17 00:00:00 2001 From: QuocAnh Date: Sat, 4 Nov 2023 23:20:17 +0700 Subject: [PATCH 02/17] [fix] datatable config --- src/main/webapp/assets/js/dataTablesConfig.js | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/webapp/assets/js/dataTablesConfig.js b/src/main/webapp/assets/js/dataTablesConfig.js index f1c964a8..d0cd0958 100644 --- a/src/main/webapp/assets/js/dataTablesConfig.js +++ b/src/main/webapp/assets/js/dataTablesConfig.js @@ -608,11 +608,19 @@ $(document).ready(function () { btnUpdate.addClass("disabled"); } } + + function disableHistoryOrderBtn() { + let btnHistory = $("#btn-history-order"); + if (btnHistory) { + btnHistory.removeAttr("data-order-id"); + btnHistory.addClass("disabled"); + } + } function disableDeleteOrderBtn() { let btnDelete = $("#btn-delete-order"); if (btnDelete) { - btnDelete.removeAttr("data-order-id"); + btnDelete.removeAttr("data-orders"); btnDelete.addClass("disabled"); } } @@ -624,7 +632,7 @@ $(document).ready(function () { btnNext.addClass("disabled"); } } - + orderTable.on( "select selectItems deselect", function (e, dt, type, indexes) { @@ -632,6 +640,7 @@ $(document).ready(function () { let btnUpdate = $("#btn-update-order"); let btnDelete = $("#btn-delete-order"); let btnNext = $("#btn-next-order"); + let btnHistory = $("#btn-history-order"); // Retrieves selected rows let data4 = orderTable.rows({selected: true}).data(); @@ -641,6 +650,7 @@ $(document).ready(function () { // data's type is a 2D array since the table's data is DOM-sourced // https://datatables.net/reference/api/row().data() btnUpdate.attr("data-order-id", data4[0][0]); + btnHistory.attr("data-order-id", data4[0][0]); btnUpdate.attr("data-order-phonenumber", data4[0][3]); btnUpdate.attr("data-order-address", data4[0][6]); btnUpdate.attr("data-order-paymentmethod", data4[0][7]); @@ -649,22 +659,25 @@ $(document).ready(function () { .substring(0, data4[0][10].length - 1) // Removes currency symbol .replace(",", ""); // Removes thousand separators btnUpdate.attr("data-order-total", total); - btnUpdate.attr("data-order-status", data4[0][11]); + btnUpdate.attr("data-order-status", data4[0][12]); btnUpdate.removeClass("disabled"); + btnHistory.removeClass("disabled"); let orders = {}; - if (data4[0][11] != "Đã giao" && data4[0][11] != "Đã hủy") { + if (data4[0][12] !== "Đã giao" && data4[0][12] !== "Đã hủy") { orders[data4[0][0]] = data4[0][0]; } + orders[data4[0][0]] = data4[0][0]; btnDelete.attr("data-orders", JSON.stringify(orders)); btnNext.attr("data-orders", JSON.stringify(orders)); btnDelete.removeClass("disabled"); btnNext.removeClass("disabled"); + } else if (data4.length > 1) { let orders = {}; for (let i = 0; i < data4.length; i++) { - if (data4[i][11] != "Đã giao" && data4[i][11] != "Đã hủy") { + if (data4[i][12] !== "Đã giao" && data4[i][12] !== "Đã hủy") { let orderId = data4[i][0]; orders[orderId] = data4[i][0]; } @@ -674,8 +687,10 @@ $(document).ready(function () { btnDelete.removeClass("disabled"); btnNext.removeClass("disabled"); disableUpdateOrderBtn(); + disableHistoryOrderBtn(); } else { disableUpdateOrderBtn(); + disableHistoryOrderBtn(); disableDeleteOrderBtn(); disableNextOrderBtn(); } @@ -703,11 +718,16 @@ $(document).ready(function () { || target.classList.contains("btn-cancel") || target.classList.contains("btn-confirm") || target.id === "update-order-modal" - || target.id === "delete-order-modal") { + || target.id === "delete-order-modal" + || target.id === "next-order-modal" + || target.id === "history-order-modal") + { e.preventDefault(); } else { disableUpdateOrderBtn(); + disableHistoryOrderBtn(); disableDeleteOrderBtn(); + disableNextOrderBtn(); } }); From e263724d5988d23ad65aa077bad8b1ce1c38f2dd Mon Sep 17 00:00:00 2001 From: QuocAnh Date: Sat, 4 Nov 2023 23:20:52 +0700 Subject: [PATCH 03/17] [update] change color of buttons on admin dashboard --- src/main/webapp/WEB-INF/jspf/admin/foods.jspf | 2 +- .../webapp/WEB-INF/jspf/admin/history.jspf | 57 ------------------- .../webapp/WEB-INF/jspf/admin/orders.jspf | 31 +++++----- 3 files changed, 19 insertions(+), 71 deletions(-) delete mode 100644 src/main/webapp/WEB-INF/jspf/admin/history.jspf diff --git a/src/main/webapp/WEB-INF/jspf/admin/foods.jspf b/src/main/webapp/WEB-INF/jspf/admin/foods.jspf index cf1c029d..6db70e81 100644 --- a/src/main/webapp/WEB-INF/jspf/admin/foods.jspf +++ b/src/main/webapp/WEB-INF/jspf/admin/foods.jspf @@ -12,7 +12,7 @@ Thêm món +