-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCashierOrderManagementPage.java
More file actions
154 lines (131 loc) · 6.63 KB
/
Copy pathCashierOrderManagementPage.java
File metadata and controls
154 lines (131 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.pdmodel.common.*;
public class CashierOrderManagementPage {
private JFrame frame;
private DefaultTableModel tableModel;
public CashierOrderManagementPage() {
frame = new JFrame("Wee Ken Shin Bakers - Cashier Dashboard");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(null);
JLabel titleLabel = new JLabel("Order Management");
titleLabel.setBounds(300, 20, 200, 30);
frame.add(titleLabel);
tableModel = new DefaultTableModel(new String[]{"Order Number", "Customer ID", "Product ID", "Quantity", "Status"}, 0);
JTable orderTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(orderTable);
scrollPane.setBounds(50, 70, 700, 400);
frame.add(scrollPane);
JButton confirmPaymentButton = new JButton("Confirm Payment");
confirmPaymentButton.setBounds(300, 500, 200, 30);
frame.add(confirmPaymentButton);
// Load unpaid orders
loadOrders();
confirmPaymentButton.addActionListener(e -> {
int selectedRow = orderTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(frame, "Please select an order to confirm payment.");
return;
}
int orderNumber = Integer.parseInt(tableModel.getValueAt(selectedRow, 0).toString());
confirmPayment(orderNumber);
});
frame.setVisible(true);
}
private void loadOrders() {
String sql = "SELECT * FROM Orders WHERE status = 'Unpaid'";
try (Connection conn = DatabaseConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
int orderNumber = rs.getInt("orderID");
int customerId = rs.getInt("customerID");
int productId = rs.getInt("productID");
int quantity = rs.getInt("quantity");
String status = rs.getString("status");
tableModel.addRow(new Object[]{orderNumber, customerId, productId, quantity, status});
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error loading orders.");
}
}
private void confirmPayment(int orderNumber) {
// Fetch order details from database
String fetchDetailsSQL = "SELECT o.orderID, o.customerID, u.username AS customerName, p.name AS productName, o.quantity, p.price " +
"FROM Orders o " +
"JOIN Users u ON o.customerID = u.userID " +
"JOIN Products p ON o.productID = p.productID " +
"WHERE o.orderID = ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement fetchStmt = conn.prepareStatement(fetchDetailsSQL)) {
fetchStmt.setInt(1, orderNumber);
ResultSet rs = fetchStmt.executeQuery();
if (rs.next()) {
// Extract order details
String customerName = rs.getString("customerName");
String productName = rs.getString("productName");
int quantity = rs.getInt("quantity");
double price = rs.getDouble("price");
double totalAmount = quantity * price;
// Update order status to 'Paid'
String updateSQL = "UPDATE Orders SET status = 'Paid' WHERE orderID = ?";
try (PreparedStatement updateStmt = conn.prepareStatement(updateSQL)) {
updateStmt.setInt(1, orderNumber);
int rowsUpdated = updateStmt.executeUpdate();
if (rowsUpdated > 0) {
// Generate Receipt
generateReceipt(orderNumber, customerName, productName, quantity, totalAmount);
JOptionPane.showMessageDialog(frame, "Payment confirmed and receipt generated.");
loadOrders(); // Reload the order list
} else {
JOptionPane.showMessageDialog(frame, "Error confirming payment.");
}
}
} else {
JOptionPane.showMessageDialog(frame, "Order not found.");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error processing payment.");
}
}
private void generateReceipt(int orderNumber, String customerName, String productName, int quantity, double totalAmount) {
String fileName = "Receipt_" + orderNumber + ".pdf";
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Title
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 20);
contentStream.beginText();
contentStream.newLineAtOffset(100, 750);
contentStream.showText("Wee Ken Shin Bakers - Receipt");
contentStream.endText();
// Order details
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.beginText();
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Order Number: " + orderNumber);
contentStream.newLineAtOffset(0, -20);
contentStream.showText("Customer Name: " + customerName);
contentStream.newLineAtOffset(0, -20);
contentStream.showText("Product Name: " + productName);
contentStream.newLineAtOffset(0, -20);
contentStream.showText("Quantity: " + quantity);
contentStream.newLineAtOffset(0, -20);
contentStream.showText("Total Amount: $" + totalAmount);
contentStream.endText();
contentStream.close();
document.save(fileName);
JOptionPane.showMessageDialog(frame, "Receipt generated: " + fileName);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error generating receipt.");
}
}
}