Counter dashboard global search added
This commit is contained in:
@@ -284,13 +284,28 @@ public class OrderController {
|
||||
|
||||
existingOrder.setTotalAmount(newAmount);
|
||||
existingOrder.setPaymentMethod(updatedOrder.getPaymentMethod());
|
||||
existingOrder.getItems().clear();
|
||||
if (updatedOrder.getStatus() != null) {
|
||||
existingOrder.setStatus(updatedOrder.getStatus().toUpperCase());
|
||||
}
|
||||
|
||||
// Safely copy items into new instances (without IDs) to trigger correct orphan removal
|
||||
List<OrderItem> newItems = new ArrayList<>();
|
||||
if (updatedOrder.getItems() != null) {
|
||||
for (OrderItem newItem : updatedOrder.getItems()) {
|
||||
for (OrderItem item : updatedOrder.getItems()) {
|
||||
OrderItem newItem = new OrderItem();
|
||||
newItem.setProductId(item.getProductId());
|
||||
newItem.setProductName(item.getProductName());
|
||||
newItem.setPrice(item.getPrice());
|
||||
newItem.setQuantity(item.getQuantity());
|
||||
newItem.setStallId(item.getStallId());
|
||||
newItem.setStallName(item.getStallName());
|
||||
newItem.setOrder(existingOrder);
|
||||
existingOrder.getItems().add(newItem);
|
||||
newItems.add(newItem);
|
||||
}
|
||||
}
|
||||
existingOrder.getItems().clear();
|
||||
existingOrder.getItems().addAll(newItems);
|
||||
|
||||
Order saved = orderRepository.save(existingOrder);
|
||||
return ResponseEntity.ok(saved);
|
||||
}).orElse(ResponseEntity.notFound().build());
|
||||
|
||||
@@ -28,7 +28,12 @@ public class PurchaseController {
|
||||
private SystemNotificationService notificationService;
|
||||
|
||||
@GetMapping("/orders")
|
||||
public List<PurchaseOrder> getAllOrders() {
|
||||
public List<PurchaseOrder> getAllOrders(
|
||||
@RequestParam(required = false) @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime from,
|
||||
@RequestParam(required = false) @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime to) {
|
||||
if (from != null && to != null) {
|
||||
return purchaseService.getAllOrdersInRange(from, to);
|
||||
}
|
||||
return purchaseService.getAllOrders();
|
||||
}
|
||||
|
||||
@@ -83,7 +88,12 @@ public class PurchaseController {
|
||||
}
|
||||
|
||||
@GetMapping("/summary")
|
||||
public Map<String, Object> getSummary() {
|
||||
public Map<String, Object> getSummary(
|
||||
@RequestParam(required = false) @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime from,
|
||||
@RequestParam(required = false) @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime to) {
|
||||
if (from != null && to != null) {
|
||||
return purchaseService.getPurchaseSummaryInRange(from, to);
|
||||
}
|
||||
return purchaseService.getPurchaseSummary();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,12 @@ public interface PurchaseOrderRepository extends JpaRepository<PurchaseOrder, Lo
|
||||
@org.springframework.data.jpa.repository.Query("SELECT SUM(p.amount) FROM PurchaseOrder p WHERE p.date >= :start AND p.date <= :end")
|
||||
java.math.BigDecimal getTotalPurchaseAmountInRange(@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start, @org.springframework.data.repository.query.Param("end") java.time.LocalDateTime end);
|
||||
|
||||
@org.springframework.data.jpa.repository.Query("SELECT SUM(COALESCE(p.amount, 0) - COALESCE(p.paidTotal, 0)) FROM PurchaseOrder p WHERE p.date >= :start AND p.date <= :end")
|
||||
java.math.BigDecimal getTotalBalanceAmountInRange(@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start, @org.springframework.data.repository.query.Param("end") java.time.LocalDateTime end);
|
||||
|
||||
@org.springframework.data.jpa.repository.Query("SELECT COUNT(p) FROM PurchaseOrder p WHERE p.status != 'PAID' AND p.date >= :start AND p.date <= :end")
|
||||
long countUnpaidBillsInRange(@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start, @org.springframework.data.repository.query.Param("end") java.time.LocalDateTime end);
|
||||
|
||||
@org.springframework.data.jpa.repository.Query("SELECT p.vendor.name, SUM(p.amount), COUNT(p) FROM PurchaseOrder p WHERE p.date >= :start AND p.date <= :end GROUP BY p.vendor.name")
|
||||
List<Object[]> getVendorSummary(@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start, @org.springframework.data.repository.query.Param("end") java.time.LocalDateTime end);
|
||||
|
||||
@@ -32,6 +38,9 @@ public interface PurchaseOrderRepository extends JpaRepository<PurchaseOrder, Lo
|
||||
@org.springframework.data.jpa.repository.Query("SELECT p.date, SUM(p.amount) FROM PurchaseOrder p GROUP BY p.date ORDER BY p.date ASC")
|
||||
List<Object[]> getPurchaseTrend();
|
||||
|
||||
@org.springframework.data.jpa.repository.Query("SELECT p FROM PurchaseOrder p WHERE p.date >= :start AND p.date <= :end ORDER BY p.date DESC")
|
||||
List<PurchaseOrder> findByDateInRange(@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start, @org.springframework.data.repository.query.Param("end") java.time.LocalDateTime end);
|
||||
|
||||
long countByStatus(String status);
|
||||
|
||||
@org.springframework.data.jpa.repository.Query("SELECT SUM(i.quantity) FROM PurchaseOrder p JOIN p.items i WHERE p.status = 'OPEN'")
|
||||
|
||||
@@ -66,18 +66,27 @@ public class DevicePairingService {
|
||||
public Map<String, Object> registerOtp(String otp, String deviceId) {
|
||||
PairingRequest existing = pendingPairings.get(otp);
|
||||
|
||||
// If this OTP was already linked by admin, return the apiKey
|
||||
if (existing != null && existing.completed && existing.deviceId.equals(deviceId)) {
|
||||
// Clean up after delivering the key
|
||||
pendingPairings.remove(otp);
|
||||
return Map.of(
|
||||
"status", "PAIRED",
|
||||
"apiKey", existing.apiKey,
|
||||
"terminalId", existing.terminalId
|
||||
);
|
||||
if (existing != null) {
|
||||
// If this OTP was already linked by admin, return the apiKey
|
||||
if (existing.completed) {
|
||||
// Clean up after delivering the key
|
||||
pendingPairings.remove(otp);
|
||||
return Map.of(
|
||||
"status", "PAIRED",
|
||||
"apiKey", existing.apiKey,
|
||||
"terminalId", existing.terminalId
|
||||
);
|
||||
}
|
||||
|
||||
// If the existing request has a placeholder device ID but this call has a real one, update it
|
||||
if (existing.deviceId.equals("ESP32-Device") && !deviceId.equals("ESP32-Device")) {
|
||||
pendingPairings.put(otp, new PairingRequest(otp, deviceId));
|
||||
}
|
||||
|
||||
return Map.of("status", "WAITING");
|
||||
}
|
||||
|
||||
// Register or refresh the OTP
|
||||
// Register new OTP
|
||||
pendingPairings.put(otp, new PairingRequest(otp, deviceId));
|
||||
|
||||
return Map.of("status", "WAITING");
|
||||
|
||||
@@ -40,6 +40,10 @@ public class PurchaseService {
|
||||
return purchaseOrderRepository.findAll();
|
||||
}
|
||||
|
||||
public List<PurchaseOrder> getAllOrdersInRange(java.time.LocalDateTime start, java.time.LocalDateTime end) {
|
||||
return purchaseOrderRepository.findByDateInRange(start, end);
|
||||
}
|
||||
|
||||
public List<Vendor> getAllVendors() {
|
||||
return vendorRepository.findAll();
|
||||
}
|
||||
@@ -132,6 +136,30 @@ public class PurchaseService {
|
||||
return summary;
|
||||
}
|
||||
|
||||
public Map<String, Object> getPurchaseSummaryInRange(java.time.LocalDateTime start, java.time.LocalDateTime end) {
|
||||
Map<String, Object> summary = new HashMap<>();
|
||||
BigDecimal total = purchaseOrderRepository.getTotalPurchaseAmountInRange(start, end);
|
||||
BigDecimal balance = purchaseOrderRepository.getTotalBalanceAmountInRange(start, end);
|
||||
long unpaidCount = purchaseOrderRepository.countUnpaidBillsInRange(start, end);
|
||||
|
||||
summary.put("totalAmount", total != null ? total : BigDecimal.ZERO);
|
||||
summary.put("balanceAmount", balance != null ? balance : BigDecimal.ZERO);
|
||||
summary.put("paidAmount", (total != null ? total : BigDecimal.ZERO).subtract(balance != null ? balance : BigDecimal.ZERO));
|
||||
summary.put("unpaidCount", unpaidCount);
|
||||
|
||||
List<Object[]> trendData = purchaseOrderRepository.getPurchaseTrendInRange(start, end);
|
||||
List<Map<String, Object>> trend = new ArrayList<>();
|
||||
for (Object[] row : trendData) {
|
||||
Map<String, Object> point = new HashMap<>();
|
||||
point.put("date", row[0].toString());
|
||||
point.put("amount", row[1]);
|
||||
trend.add(point);
|
||||
}
|
||||
summary.put("trend", trend);
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
public Map<String, Object> getIntentSummary() {
|
||||
Map<String, Object> summary = new HashMap<>();
|
||||
summary.put("openCount", purchaseOrderRepository.countByStatus("OPEN"));
|
||||
|
||||
Reference in New Issue
Block a user