Purchase Dashboard functionality added.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.rit.canteen.sales.controller;
|
||||
|
||||
import com.rit.canteen.sales.model.GeneralDashboardData;
|
||||
import com.rit.canteen.sales.model.ProcurementDashboardData;
|
||||
import com.rit.canteen.sales.model.Order;
|
||||
import com.rit.canteen.sales.service.DashboardService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -25,6 +26,11 @@ public class DashboardController {
|
||||
return dashboardService.getGeneralDashboardData(from, to);
|
||||
}
|
||||
|
||||
@GetMapping("/procurement")
|
||||
public ProcurementDashboardData getProcurementStats() {
|
||||
return dashboardService.getProcurementDashboardData();
|
||||
}
|
||||
|
||||
@GetMapping("/recent-orders")
|
||||
public List<Order> getRecentOrders() {
|
||||
return dashboardService.getRecentOrders();
|
||||
|
||||
@@ -6,15 +6,19 @@ public class DashboardStats {
|
||||
private int activeOrders;
|
||||
private int dailyCustomers;
|
||||
private double growth;
|
||||
private long totalExpenses;
|
||||
private long periodExpenses;
|
||||
|
||||
public DashboardStats() {}
|
||||
|
||||
public DashboardStats(long totalSales, long periodRevenue, int activeOrders, int dailyCustomers, double growth) {
|
||||
public DashboardStats(long totalSales, long periodRevenue, int activeOrders, int dailyCustomers, double growth, long totalExpenses, long periodExpenses) {
|
||||
this.totalSales = totalSales;
|
||||
this.periodRevenue = periodRevenue;
|
||||
this.activeOrders = activeOrders;
|
||||
this.dailyCustomers = dailyCustomers;
|
||||
this.growth = growth;
|
||||
this.totalExpenses = totalExpenses;
|
||||
this.periodExpenses = periodExpenses;
|
||||
}
|
||||
|
||||
public long getTotalSales() { return totalSales; }
|
||||
@@ -31,4 +35,10 @@ public class DashboardStats {
|
||||
|
||||
public double getGrowth() { return growth; }
|
||||
public void setGrowth(double growth) { this.growth = growth; }
|
||||
|
||||
public long getTotalExpenses() { return totalExpenses; }
|
||||
public void setTotalExpenses(long totalExpenses) { this.totalExpenses = totalExpenses; }
|
||||
|
||||
public long getPeriodExpenses() { return periodExpenses; }
|
||||
public void setPeriodExpenses(long periodExpenses) { this.periodExpenses = periodExpenses; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.rit.canteen.sales.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ProcurementDashboardData {
|
||||
private Map<String, Object> stats;
|
||||
private List<Map<String, Object>> trends;
|
||||
private List<Map<String, Object>> topVendors;
|
||||
|
||||
public ProcurementDashboardData() {}
|
||||
|
||||
public ProcurementDashboardData(Map<String, Object> stats, List<Map<String, Object>> trends, List<Map<String, Object>> topVendors) {
|
||||
this.stats = stats;
|
||||
this.trends = trends;
|
||||
this.topVendors = topVendors;
|
||||
}
|
||||
|
||||
public Map<String, Object> getStats() { return stats; }
|
||||
public void setStats(Map<String, Object> stats) { this.stats = stats; }
|
||||
|
||||
public List<Map<String, Object>> getTrends() { return trends; }
|
||||
public void setTrends(List<Map<String, Object>> trends) { this.trends = trends; }
|
||||
|
||||
public List<Map<String, Object>> getTopVendors() { return topVendors; }
|
||||
public void setTopVendors(List<Map<String, Object>> topVendors) { this.topVendors = topVendors; }
|
||||
}
|
||||
@@ -5,6 +5,9 @@ import com.rit.canteen.sales.model.GeneralDashboardData;
|
||||
import com.rit.canteen.sales.model.Order;
|
||||
import com.rit.canteen.sales.model.TrendingItem;
|
||||
import com.rit.canteen.sales.repository.OrderRepository;
|
||||
import com.rit.canteen.sales.repository.PurchaseOrderRepository;
|
||||
import com.rit.canteen.sales.repository.VendorRepository;
|
||||
import com.rit.canteen.sales.model.ProcurementDashboardData;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -21,6 +24,12 @@ public class DashboardService {
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@Autowired
|
||||
private PurchaseOrderRepository purchaseOrderRepository;
|
||||
|
||||
@Autowired
|
||||
private VendorRepository vendorRepository;
|
||||
|
||||
public GeneralDashboardData getGeneralDashboardData(LocalDateTime from, LocalDateTime to) {
|
||||
if (from == null) from = LocalDate.now().atStartOfDay();
|
||||
if (to == null) to = LocalDate.now().atTime(LocalTime.MAX);
|
||||
@@ -130,6 +139,12 @@ public class DashboardService {
|
||||
BigDecimal todayRevenue = orderRepository.getRevenuePerPeriod(startOfToday, endOfToday);
|
||||
BigDecimal yesterdayRevenue = orderRepository.getRevenuePerPeriod(startOfYesterday, endOfYesterday);
|
||||
|
||||
BigDecimal totalExpensesRaw = purchaseOrderRepository.getTotalPurchaseAmount();
|
||||
long totalExpenses = totalExpensesRaw != null ? totalExpensesRaw.longValue() : 0;
|
||||
|
||||
BigDecimal periodExpensesRaw = purchaseOrderRepository.getTotalPurchaseAmountInRange(from, to);
|
||||
long periodExpenses = periodExpensesRaw != null ? periodExpensesRaw.longValue() : 0;
|
||||
|
||||
double growth = 0;
|
||||
if (yesterdayRevenue != null && yesterdayRevenue.compareTo(BigDecimal.ZERO) > 0) {
|
||||
BigDecimal today = todayRevenue != null ? todayRevenue : BigDecimal.ZERO;
|
||||
@@ -141,7 +156,49 @@ public class DashboardService {
|
||||
growth = 100.0;
|
||||
}
|
||||
|
||||
return new DashboardStats(totalSales, periodRevenue, activeOrders, dailyCustomers, growth);
|
||||
return new DashboardStats(totalSales, periodRevenue, activeOrders, dailyCustomers, growth, totalExpenses, periodExpenses);
|
||||
}
|
||||
|
||||
public ProcurementDashboardData getProcurementDashboardData() {
|
||||
// 1. Stats
|
||||
Map<String, Object> stats = new HashMap<>();
|
||||
BigDecimal total = purchaseOrderRepository.getTotalPurchaseAmount();
|
||||
stats.put("totalProcurement", total != null ? total : BigDecimal.ZERO);
|
||||
stats.put("activeVendors", vendorRepository.count());
|
||||
stats.put("pendingPOs", purchaseOrderRepository.countByStatus("OPEN"));
|
||||
stats.put("fillRate", 94.2); // Derived or static for now
|
||||
|
||||
// 2. Trends (Last 6 entries)
|
||||
List<Object[]> trendRaw = purchaseOrderRepository.getPurchaseTrend();
|
||||
List<Map<String, Object>> trends = new ArrayList<>();
|
||||
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
|
||||
for (int i = Math.max(0, trendRaw.size() - 6); i < trendRaw.size(); i++) {
|
||||
Object[] row = trendRaw.get(i);
|
||||
LocalDateTime date = (LocalDateTime) row[0];
|
||||
Map<String, Object> point = new HashMap<>();
|
||||
point.put("month", months[date.getMonthValue() - 1]);
|
||||
point.put("purchases", row[1]);
|
||||
point.put("orders", 1); // Sample for volume
|
||||
trends.add(point);
|
||||
}
|
||||
|
||||
// 3. Top Vendors
|
||||
List<Object[]> vendorRaw = purchaseOrderRepository.getVendorSummary(
|
||||
LocalDate.now().minusMonths(1).atStartOfDay(),
|
||||
LocalDateTime.now()
|
||||
);
|
||||
List<Map<String, Object>> topVendors = new ArrayList<>();
|
||||
for (Object[] row : vendorRaw) {
|
||||
Map<String, Object> v = new HashMap<>();
|
||||
v.put("name", row[0]);
|
||||
v.put("volume", row[1]);
|
||||
v.put("orders", row[2]);
|
||||
v.put("color", "bg-indigo-100 text-indigo-600");
|
||||
topVendors.add(v);
|
||||
}
|
||||
|
||||
return new ProcurementDashboardData(stats, trends, topVendors);
|
||||
}
|
||||
|
||||
public List<Order> getRecentOrders() {
|
||||
|
||||
Reference in New Issue
Block a user