-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaleDAO.java
More file actions
190 lines (158 loc) · 7.6 KB
/
SaleDAO.java
File metadata and controls
190 lines (158 loc) · 7.6 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package services;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import DB.DBConnection;
import model.Product;
import model.Sale;
public class SaleDAO {
// Create a new sale
public boolean createSale(Sale sale) {
String insertSaleQuery = "INSERT INTO Sale (SaleID, TotalAmount, SaleDate, branchcode) VALUES (?, ?, ?, ?)";
String insertSaleProductQuery = "INSERT INTO SaleProduct (SaleID, ProductID, Quantity, branchcode) VALUES (?, ?, ?, ?)";
try (Connection conn = DBConnection.getInstance().getConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement saleStmt = conn.prepareStatement(insertSaleQuery);
PreparedStatement saleProductStmt = conn.prepareStatement(insertSaleProductQuery)) {
// Insert into Sale table
saleStmt.setString(1, sale.getSaleId());
saleStmt.setDouble(2, sale.getTotalAmount());
saleStmt.setDate(3, new Date(sale.getDate().getTime()));
saleStmt.setString(4, sale.getBranchCode());
saleStmt.executeUpdate();
// Insert into SaleProduct table
for (Product product : sale.getProducts()) {
saleProductStmt.setString(1, sale.getSaleId());
saleProductStmt.setString(2, product.getProductId());
saleProductStmt.setInt(3, product.getSoldQuantity());
saleProductStmt.setString(4, sale.getBranchCode());
saleProductStmt.addBatch();
}
saleProductStmt.executeBatch();
conn.commit();
return true;
} catch (SQLException e) {
conn.rollback();
throw e; // Propagate exception
} finally {
conn.setAutoCommit(true);
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public List<Sale> getSalesByBranchCode(String branchCode) {
String salesByBranchQuery = "SELECT * FROM Sale WHERE branchcode = ?";
String saleProductQuery = "SELECT sp.ProductID, sp.Quantity FROM SaleProduct sp WHERE sp.SaleID = ?";
List<Sale> sales = new ArrayList<>();
Connection conn = DBConnection.getInstance().getConnection();
try (PreparedStatement salesStmt = conn.prepareStatement(salesByBranchQuery)) {
salesStmt.setString(1, branchCode);
ResultSet salesRs = salesStmt.executeQuery();
while (salesRs.next()) {
String saleId = salesRs.getString("SaleID");
double totalAmount = salesRs.getDouble("TotalAmount");
Date date = salesRs.getDate("SaleDate");
// Fetch products associated with this sale (no ProductName anymore)
List<Product> products = new ArrayList<>();
try (PreparedStatement saleProductStmt = conn.prepareStatement(saleProductQuery)) {
saleProductStmt.setString(1, saleId);
ResultSet productRs = saleProductStmt.executeQuery();
while (productRs.next()) {
String productId = productRs.getString("ProductID");
int quantity = productRs.getInt("Quantity");
products.add(new Product(productId, quantity)); // Adjusted constructor to exclude product name
}
}
// Add the sale with associated products to the list
sales.add(new Sale(saleId, products, totalAmount, date, branchCode));
}
} catch (SQLException e) {
e.printStackTrace();
}
return sales;
}
// Get a sale by SaleID
public Sale getSaleById(String saleId) {
String saleQuery = "SELECT * FROM Sale WHERE SaleID = ?";
String saleProductQuery = "SELECT p.ProductID, p.ProductName, sp.Quantity FROM SaleProduct sp " +
"JOIN Product p ON sp.ProductID = p.ProductID WHERE sp.SaleID = ?";
Connection conn = DBConnection.getInstance().getConnection();
try {
// Fetch Sale details
try (PreparedStatement saleStmt = conn.prepareStatement(saleQuery)) {
saleStmt.setString(1, saleId);
ResultSet saleRs = saleStmt.executeQuery();
if (saleRs.next()) {
String id = saleRs.getString("SaleID");
double totalAmount = saleRs.getDouble("TotalAmount");
Date date = saleRs.getDate("SaleDate");
String bc=saleRs.getString("branchode");
// Fetch products associated with this sale
List<Product> products = new ArrayList<>();
try (PreparedStatement saleProductStmt = conn.prepareStatement(saleProductQuery)) {
saleProductStmt.setString(1, saleId);
ResultSet productRs = saleProductStmt.executeQuery();
while (productRs.next()) {
String productId = productRs.getString("ProductID");
String productName = productRs.getString("ProductName");
int quantity = productRs.getInt("Quantity");
products.add(new Product(productId, productName, quantity)); // Adjust Product constructor
}
}
return new Sale(id, products, totalAmount, date,bc);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// Delete a sale
public boolean deleteSale(String saleId) {
String deleteSaleQuery = "DELETE FROM Sale WHERE SaleID = ?";
Connection conn = DBConnection.getInstance().getConnection();
try (PreparedStatement stmt = conn.prepareStatement(deleteSaleQuery)) {
stmt.setString(1, saleId);
int rowsAffected = stmt.executeUpdate();
return rowsAffected > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// Update a sale
public boolean updateSale(String saleId, double newTotalAmount) {
String updateSaleQuery = "UPDATE Sale SET TotalAmount = ? WHERE SaleID = ?";
Connection conn = DBConnection.getInstance().getConnection();
try (PreparedStatement stmt = conn.prepareStatement(updateSaleQuery)) {
stmt.setDouble(1, newTotalAmount);
stmt.setString(2, saleId);
int rowsAffected = stmt.executeUpdate();
return rowsAffected > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// Get all sales
public List<Sale> getAllSales() {
String saleQuery = "SELECT * FROM Sale";
List<Sale> sales = new ArrayList<>();
Connection conn = DBConnection.getInstance().getConnection();
try (PreparedStatement stmt = conn.prepareStatement(saleQuery);
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String saleId = rs.getString("SaleID");
double totalAmount = rs.getDouble("TotalAmount");
Date date = rs.getDate("SaleDate");
String bc=rs.getString("branhcode");
sales.add(new Sale(saleId, null, totalAmount,date,bc)); // Add products later if required
}
} catch (SQLException e) {
e.printStackTrace();
}
return sales;
}
}