Add getSaleLineItems and createSale methods
This commit is contained in:
@@ -3,6 +3,9 @@ package org.example.petshopdesktop.database;
|
|||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import org.example.petshopdesktop.DTOs.SaleDTO;
|
import org.example.petshopdesktop.DTOs.SaleDTO;
|
||||||
|
import org.example.petshopdesktop.models.SaleCartItem;
|
||||||
|
import org.example.petshopdesktop.models.SaleLineItem;
|
||||||
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
@@ -110,4 +113,121 @@ public class SaleDB {
|
|||||||
conn.close();
|
conn.close();
|
||||||
return sales;
|
return sales;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ObservableList<SaleLineItem> getSaleLineItems() throws SQLException {
|
||||||
|
ObservableList<SaleLineItem> saleItems = FXCollections.observableArrayList();
|
||||||
|
Connection conn = ConnectionDB.getConnection();
|
||||||
|
|
||||||
|
String sql = """
|
||||||
|
SELECT
|
||||||
|
s.saleId,
|
||||||
|
DATE_FORMAT(s.saleDate, '%Y-%m-%d %H:%i') as saleDate,
|
||||||
|
CONCAT(e.firstName, ' ', e.lastName) as employeeName,
|
||||||
|
p.prodName,
|
||||||
|
si.quantity,
|
||||||
|
si.unitPrice,
|
||||||
|
(si.quantity * si.unitPrice) as total,
|
||||||
|
s.paymentMethod
|
||||||
|
FROM sale s
|
||||||
|
JOIN saleItem si ON s.saleId = si.saleId
|
||||||
|
JOIN product p ON si.prodId = p.prodId
|
||||||
|
JOIN employee e ON s.employeeId = e.employeeId
|
||||||
|
ORDER BY s.saleDate DESC, s.saleId, si.saleItemId
|
||||||
|
""";
|
||||||
|
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(sql);
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
saleItems.add(new SaleLineItem(
|
||||||
|
rs.getInt("saleId"),
|
||||||
|
rs.getString("saleDate"),
|
||||||
|
rs.getString("employeeName"),
|
||||||
|
rs.getString("prodName"),
|
||||||
|
rs.getInt("quantity"),
|
||||||
|
rs.getDouble("unitPrice"),
|
||||||
|
rs.getDouble("total"),
|
||||||
|
rs.getString("paymentMethod")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.close();
|
||||||
|
return saleItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int createSale(int employeeId, String paymentMethod, ObservableList<SaleCartItem> cartItems) throws SQLException {
|
||||||
|
if (cartItems.isEmpty()) {
|
||||||
|
throw new SQLException("Cannot create sale with empty cart");
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection conn = ConnectionDB.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
double totalAmount = cartItems.stream().mapToDouble(SaleCartItem::getTotal).sum();
|
||||||
|
|
||||||
|
String insertSale = """
|
||||||
|
INSERT INTO sale (saleDate, totalAmount, paymentMethod, employeeId, storeId)
|
||||||
|
VALUES (NOW(), ?, ?, ?, 1)
|
||||||
|
""";
|
||||||
|
|
||||||
|
PreparedStatement saleStmt = conn.prepareStatement(insertSale, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
saleStmt.setDouble(1, totalAmount);
|
||||||
|
saleStmt.setString(2, paymentMethod);
|
||||||
|
saleStmt.setInt(3, employeeId);
|
||||||
|
saleStmt.executeUpdate();
|
||||||
|
|
||||||
|
ResultSet rs = saleStmt.getGeneratedKeys();
|
||||||
|
if (!rs.next()) {
|
||||||
|
throw new SQLException("Failed to get generated sale ID");
|
||||||
|
}
|
||||||
|
int saleId = rs.getInt(1);
|
||||||
|
|
||||||
|
String insertItem = """
|
||||||
|
INSERT INTO saleItem (saleId, prodId, quantity, unitPrice)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
""";
|
||||||
|
|
||||||
|
String updateInventory = """
|
||||||
|
UPDATE inventory
|
||||||
|
SET quantity = quantity - ?
|
||||||
|
WHERE prodId = ?
|
||||||
|
""";
|
||||||
|
|
||||||
|
PreparedStatement itemStmt = conn.prepareStatement(insertItem);
|
||||||
|
PreparedStatement invStmt = conn.prepareStatement(updateInventory);
|
||||||
|
|
||||||
|
for (SaleCartItem item : cartItems) {
|
||||||
|
itemStmt.setInt(1, saleId);
|
||||||
|
itemStmt.setInt(2, item.getProdId());
|
||||||
|
itemStmt.setInt(3, item.getQuantity());
|
||||||
|
itemStmt.setDouble(4, item.getUnitPrice());
|
||||||
|
itemStmt.executeUpdate();
|
||||||
|
|
||||||
|
invStmt.setInt(1, item.getQuantity());
|
||||||
|
invStmt.setInt(2, item.getProdId());
|
||||||
|
int updated = invStmt.executeUpdate();
|
||||||
|
|
||||||
|
if (updated == 0) {
|
||||||
|
throw new SQLException("Failed to update inventory for product ID " + item.getProdId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.commit();
|
||||||
|
|
||||||
|
ActivityLogger.getInstance().logInsert("sale",
|
||||||
|
String.format("Sale ID: %d", saleId),
|
||||||
|
String.format("Created sale with %d items, total: $%.2f", cartItems.size(), totalAmount));
|
||||||
|
|
||||||
|
return saleId;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
conn.rollback();
|
||||||
|
ActivityLogger.getInstance().logException("SaleDB.createSale", e, "Creating sale");
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
conn.setAutoCommit(true);
|
||||||
|
conn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user