Revenue functionality added and Removed the Expenses page
This commit is contained in:
@@ -120,6 +120,24 @@ public class OrderController {
|
||||
}
|
||||
}
|
||||
|
||||
if (isStaff()) {
|
||||
Optional<User> posUserOpt = userRepository.findByMobileNumber("0000000000");
|
||||
User posUser;
|
||||
if (posUserOpt.isPresent()) {
|
||||
posUser = posUserOpt.get();
|
||||
} else {
|
||||
posUser = new User();
|
||||
posUser.setMobileNumber("0000000000");
|
||||
posUser.setName("POS");
|
||||
posUser.setPinHash("NOT_APPLICABLE");
|
||||
posUser.setCreatedAt(LocalDateTime.now());
|
||||
posUser.setUpdatedAt(LocalDateTime.now());
|
||||
posUser = userRepository.save(posUser);
|
||||
}
|
||||
order.setUserId(posUser.getId());
|
||||
order.setOrderType("POS");
|
||||
}
|
||||
|
||||
if (order.getItems() == null || order.getItems().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(Map.of("success", false, "message", "Order must have items"));
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ public class DashboardStats {
|
||||
private long totalExpenses;
|
||||
private long periodExpenses;
|
||||
private long suspendedUserCount;
|
||||
private long periodStoreRevenue;
|
||||
private long totalStoreRevenue;
|
||||
|
||||
public DashboardStats() {}
|
||||
|
||||
@@ -23,6 +25,19 @@ public class DashboardStats {
|
||||
this.suspendedUserCount = suspendedUserCount;
|
||||
}
|
||||
|
||||
public DashboardStats(long totalSales, long periodRevenue, int activeOrders, int dailyCustomers, double growth, long totalExpenses, long periodExpenses, long suspendedUserCount, long periodStoreRevenue, long totalStoreRevenue) {
|
||||
this.totalSales = totalSales;
|
||||
this.periodRevenue = periodRevenue;
|
||||
this.activeOrders = activeOrders;
|
||||
this.dailyCustomers = dailyCustomers;
|
||||
this.growth = growth;
|
||||
this.totalExpenses = totalExpenses;
|
||||
this.periodExpenses = periodExpenses;
|
||||
this.suspendedUserCount = suspendedUserCount;
|
||||
this.periodStoreRevenue = periodStoreRevenue;
|
||||
this.totalStoreRevenue = totalStoreRevenue;
|
||||
}
|
||||
|
||||
public long getTotalSales() { return totalSales; }
|
||||
public void setTotalSales(long totalSales) { this.totalSales = totalSales; }
|
||||
|
||||
@@ -46,4 +61,10 @@ public class DashboardStats {
|
||||
|
||||
public long getSuspendedUserCount() { return suspendedUserCount; }
|
||||
public void setSuspendedUserCount(long suspendedUserCount) { this.suspendedUserCount = suspendedUserCount; }
|
||||
|
||||
public long getPeriodStoreRevenue() { return periodStoreRevenue; }
|
||||
public void setPeriodStoreRevenue(long periodStoreRevenue) { this.periodStoreRevenue = periodStoreRevenue; }
|
||||
|
||||
public long getTotalStoreRevenue() { return totalStoreRevenue; }
|
||||
public void setTotalStoreRevenue(long totalStoreRevenue) { this.totalStoreRevenue = totalStoreRevenue; }
|
||||
}
|
||||
|
||||
@@ -25,6 +25,12 @@ public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecific
|
||||
@Query("SELECT SUM(o.totalAmount) FROM Order o")
|
||||
BigDecimal getTotalRevenue();
|
||||
|
||||
@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.status = 'COMPLETED'")
|
||||
BigDecimal getTotalCompletedRevenue();
|
||||
|
||||
@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.status = 'COMPLETED' AND (o.orderType = 'POS' OR o.orderType = 'STORE_ORDER' OR o.paymentMethod = 'CASH')")
|
||||
BigDecimal getCompletedStoreRevenue();
|
||||
|
||||
@Query("SELECT COUNT(DISTINCT o.userId) FROM Order o WHERE o.createdAt >= :startOfDay")
|
||||
long countUniqueUsersToday(@Param("startOfDay") LocalDateTime startOfDay);
|
||||
|
||||
@@ -34,6 +40,12 @@ public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecific
|
||||
@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.createdAt >= :start AND o.createdAt <= :end")
|
||||
BigDecimal getRevenuePerPeriod(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
|
||||
|
||||
@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.status = 'COMPLETED' AND o.createdAt >= :start AND o.createdAt <= :end")
|
||||
BigDecimal getCompletedRevenuePerPeriod(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
|
||||
|
||||
@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.status = 'COMPLETED' AND (o.orderType = 'POS' OR o.orderType = 'STORE_ORDER' OR o.paymentMethod = 'CASH') AND o.createdAt >= :start AND o.createdAt <= :end")
|
||||
BigDecimal getCompletedStoreRevenuePerPeriod(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
|
||||
|
||||
List<Order> findTop5ByOrderByCreatedAtDesc();
|
||||
|
||||
@Query("SELECT i.stallName, SUM(i.price * i.quantity), COUNT(DISTINCT o.id) " +
|
||||
|
||||
@@ -147,14 +147,14 @@ public class DashboardService {
|
||||
LocalDateTime startOfYesterday = LocalDate.now(zone).minusDays(1).atStartOfDay();
|
||||
LocalDateTime endOfYesterday = LocalDate.now(zone).minusDays(1).atTime(LocalTime.MAX);
|
||||
|
||||
System.out.println("[DIAGNOSTIC] Fetching Total Revenue...");
|
||||
BigDecimal totalRevenueRaw = tokenTransactionRepository.sumByType(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP);
|
||||
System.out.println("[DIAGNOSTIC] Raw Total Revenue: " + totalRevenueRaw);
|
||||
System.out.println("[DIAGNOSTIC] Fetching Total RITZ Spent...");
|
||||
BigDecimal totalRevenueRaw = tokenTransactionRepository.sumByType(com.rit.canteen.sales.model.TokenTransaction.TransactionType.SPEND);
|
||||
System.out.println("[DIAGNOSTIC] Raw Total RITZ Spent: " + totalRevenueRaw);
|
||||
long totalSales = totalRevenueRaw != null ? totalRevenueRaw.longValue() : 0;
|
||||
|
||||
System.out.println("[DIAGNOSTIC] Fetching Period Revenue for range: " + from + " to " + to);
|
||||
BigDecimal periodRevenueRaw = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, from, to);
|
||||
System.out.println("[DIAGNOSTIC] Raw Period Revenue: " + periodRevenueRaw);
|
||||
System.out.println("[DIAGNOSTIC] Fetching Period RITZ Spent for range: " + from + " to " + to);
|
||||
BigDecimal periodRevenueRaw = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.SPEND, from, to);
|
||||
System.out.println("[DIAGNOSTIC] Raw Period RITZ Spent: " + periodRevenueRaw);
|
||||
long periodRevenue = periodRevenueRaw != null ? periodRevenueRaw.longValue() : 0;
|
||||
|
||||
int activeOrders = (int) orderRepository.countByCreatedAtBetween(from, to);
|
||||
@@ -162,9 +162,9 @@ public class DashboardService {
|
||||
|
||||
System.out.println("[REVENUE-TRACE] Active Orders in Range: " + activeOrders + " | Unique Customers: " + dailyCustomers);
|
||||
|
||||
BigDecimal todayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, startOfToday, endOfToday);
|
||||
BigDecimal yesterdayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, startOfYesterday, endOfYesterday);
|
||||
System.out.println("[DIAGNOSTIC] Today vs Yesterday: " + todayRevenue + " / " + yesterdayRevenue);
|
||||
BigDecimal todayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.SPEND, startOfToday, endOfToday);
|
||||
BigDecimal yesterdayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.SPEND, startOfYesterday, endOfYesterday);
|
||||
System.out.println("[DIAGNOSTIC] Today vs Yesterday SPEND: " + todayRevenue + " / " + yesterdayRevenue);
|
||||
|
||||
BigDecimal totalExpensesRaw = purchaseOrderRepository.getTotalPurchaseAmount();
|
||||
long totalExpenses = totalExpensesRaw != null ? totalExpensesRaw.longValue() : 0;
|
||||
@@ -172,6 +172,18 @@ public class DashboardService {
|
||||
BigDecimal periodExpensesRaw = purchaseOrderRepository.getTotalPurchaseAmountInRange(from, to);
|
||||
long periodExpenses = periodExpensesRaw != null ? periodExpensesRaw.longValue() : 0;
|
||||
|
||||
BigDecimal periodPOSOrdersRaw = orderRepository.getCompletedStoreRevenuePerPeriod(from, to);
|
||||
long periodPOSOrders = periodPOSOrdersRaw != null ? periodPOSOrdersRaw.longValue() : 0;
|
||||
BigDecimal periodTopupsRaw = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, from, to);
|
||||
long periodTopups = periodTopupsRaw != null ? periodTopupsRaw.longValue() : 0;
|
||||
long periodStoreRevenue = periodPOSOrders + periodTopups;
|
||||
|
||||
BigDecimal totalPOSOrdersRaw = orderRepository.getCompletedStoreRevenue();
|
||||
long totalPOSOrders = totalPOSOrdersRaw != null ? totalPOSOrdersRaw.longValue() : 0;
|
||||
BigDecimal totalTopupsRaw = tokenTransactionRepository.sumByType(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP);
|
||||
long totalTopups = totalTopupsRaw != null ? totalTopupsRaw.longValue() : 0;
|
||||
long totalStoreRevenue = totalPOSOrders + totalTopups;
|
||||
|
||||
long suspendedUserCount = userRepository.countByIsSuspended(true);
|
||||
|
||||
double growth = 0;
|
||||
@@ -185,7 +197,7 @@ public class DashboardService {
|
||||
growth = 100.0;
|
||||
}
|
||||
|
||||
return new DashboardStats(totalSales, periodRevenue, activeOrders, dailyCustomers, growth, totalExpenses, periodExpenses, suspendedUserCount);
|
||||
return new DashboardStats(totalSales, periodRevenue, activeOrders, dailyCustomers, growth, totalExpenses, periodExpenses, suspendedUserCount, periodStoreRevenue, totalStoreRevenue);
|
||||
}
|
||||
|
||||
public ProcurementDashboardData getProcurementDashboardData() {
|
||||
|
||||
Reference in New Issue
Block a user