diff --git a/src/main/ManagerController.java b/src/main/ManagerController.java index d3c475c..b3558a6 100644 --- a/src/main/ManagerController.java +++ b/src/main/ManagerController.java @@ -26,6 +26,7 @@ public class ManagerController { ObservableList> excessReportData = FXCollections.observableArrayList(); ObservableList> menuData = FXCollections.observableArrayList(); ObservableList> salesReportData = FXCollections.observableArrayList(); + ObservableList> salesFrequentReportData = FXCollections.observableArrayList(); ObservableList> xReportData = FXCollections.observableArrayList(); ObservableList> zReportData = FXCollections.observableArrayList(); @@ -45,6 +46,8 @@ public class ManagerController { @FXML private TableView salesReportTableView; @FXML + private TableView salesFrequentReportTableView; + @FXML private TableView excessReportTableView; @FXML @@ -299,6 +302,56 @@ private void inventoryChangeMinAmt(ActionEvent event) { } } + /** + * Generates a sales report of what frequently bought together as pairs based on + * the selected start and end dates. + * + * If no dates are selected or the start date is after the end date, an error + * message is displayed. + * + * @param event the ActionEvent triggered by clicking the "Generate Frequent + * Sales Report" button + */ + @FXML + private void generateFrequentSalesReport(ActionEvent event) { + System.out.println("Manager has tried to generate a Sales Report"); + + salesFrequentReportTableView.getItems().clear(); + salesFrequentReportTableView.getColumns().clear(); + Alert a = new Alert(AlertType.NONE); + + // Error handling + if (startDatePicker.getValue() == null || endDatePicker.getValue() == null) { + System.out.println("Error: No dates selected"); + a.setAlertType(AlertType.ERROR); + a.setContentText("Error: No dates selected"); + a.show(); + return; + } + if (startDatePicker.getValue().isAfter(endDatePicker.getValue())) { + System.out.println("Start date is after end date"); + a.setAlertType(AlertType.ERROR); + a.setContentText("Error: Start date is after end date"); + a.show(); + return; + } + if (startDatePicker.getValue().isEqual(endDatePicker.getValue())) { + System.out.println("Start date is equal to end date"); + a.setAlertType(AlertType.ERROR); + a.setContentText("Error: Start date is equal to end date"); + a.show(); + return; + } + LocalDate startDate = startDatePicker.getValue(); + LocalDate endDate = endDatePicker.getValue(); + setTableResult( + db.generateFrequentSalesReport( + startDate.toString() + " 00:00:00", + endDate.toString() + " 23:59:59"), + salesFrequentReportData, + salesFrequentReportTableView); + } + /** * Handles the action event for adding a new menu item to the menu table. * Parses the input name, ID, class, and price from the corresponding fields, diff --git a/src/main/jdbcpostgreSQL.java b/src/main/jdbcpostgreSQL.java index 3315d73..f03a434 100644 --- a/src/main/jdbcpostgreSQL.java +++ b/src/main/jdbcpostgreSQL.java @@ -574,6 +574,31 @@ public ResultSet getSalesReport(Date startTime, Date endTime) { return r; } + /* + * Given a time window, display a list of pairs of menu + * items that sell together often, popular or not, sorted by most frequent. + * + * @param startTime The start time of the time window + * + * @param endTime The end time of the time window + * + * @return A ResultSet containing the sales report + */ + public ResultSet generateFrequentSalesReport(String startTime, String endTime) { + ResultSet r = null; + try { + Statement stmt = conn.createStatement(); + String sqlStatement = "select t1.firstitem, t1.seconditem, t1.concat as menuitemstogether, COUNT(t1.concat) from (SELECT a.menuitemid as firstitem, b.menuitemid as seconditem, a.orderid, CONCAT(a.menuitemid, ' ', b.menuitemid) FROM orderlineitems a JOIN orderlineitems b ON a.orderid = b.orderid and a.menuitemid < b.menuitemid) t1 INNER JOIN (select orders.ordertime, orders.orderid from orders where orders.ordertime between '" + + startTime + "' AND '" + endTime + + "' group by orders.orderid) t2 ON t1.orderid = t2.orderid GROUP BY t1.firstitem, t1.seconditem, t1.concat ORDER BY COUNT(t1.concat) DESC;"; + System.out.println(sqlStatement); + r = stmt.executeQuery(sqlStatement); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return r; + } + /** * Retrieves a result set containing the X report for the most recent day. * diff --git a/src/main/manager.fxml b/src/main/manager.fxml index 3b7c29b..c04b2cf 100644 --- a/src/main/manager.fxml +++ b/src/main/manager.fxml @@ -39,7 +39,7 @@