| 33917 |
ranu |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
4 |
import com.spice.profitmandi.dao.model.*;
|
|
|
5 |
import org.apache.logging.log4j.LogManager;
|
|
|
6 |
import org.apache.logging.log4j.Logger;
|
|
|
7 |
import org.hibernate.Session;
|
|
|
8 |
import org.hibernate.SessionFactory;
|
|
|
9 |
import org.hibernate.query.NativeQuery;
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11 |
import org.springframework.cache.annotation.Cacheable;
|
|
|
12 |
import org.springframework.stereotype.Component;
|
|
|
13 |
|
|
|
14 |
import javax.persistence.TypedQuery;
|
|
|
15 |
import java.time.LocalDate;
|
|
|
16 |
import java.util.ArrayList;
|
|
|
17 |
import java.util.List;
|
|
|
18 |
|
|
|
19 |
@Component
|
|
|
20 |
public class RbmTargetServiceImpl implements RbmTargetService {
|
|
|
21 |
private static final Logger LOGGER = LogManager.getLogger(RbmTargetServiceImpl.class);
|
|
|
22 |
|
|
|
23 |
@Autowired
|
|
|
24 |
SessionFactory sessionFactory;
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
@Override
|
|
|
28 |
public List<WarehouseRbmTargetModel> getWarehouseWiseRbmMonthlyTarget() {
|
|
|
29 |
Session session = sessionFactory.getCurrentSession();
|
|
|
30 |
final TypedQuery<WarehouseRbmTargetModel> typedQuerySimilar = session.createNamedQuery("RbmTarget.getWarehouseWiseMonthlyTarget", WarehouseRbmTargetModel.class);
|
|
|
31 |
|
|
|
32 |
return typedQuerySimilar.getResultList();
|
|
|
33 |
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
@Override
|
|
|
37 |
public List<MTDAchievedTargetModel> getDateWiseAchievedTargetOfRbm(LocalDate startDate, LocalDate endDate) {
|
|
|
38 |
Session session = sessionFactory.getCurrentSession();
|
|
|
39 |
final TypedQuery<MTDAchievedTargetModel> typedQuerySimilar = session.createNamedQuery("RbmTarget.getRbmAchievedMonthlyTarget", MTDAchievedTargetModel.class);
|
|
|
40 |
typedQuerySimilar.setParameter("startDate", startDate);
|
|
|
41 |
typedQuerySimilar.setParameter("endDate", endDate);
|
|
|
42 |
return typedQuerySimilar.getResultList();
|
|
|
43 |
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
@Override
|
|
|
47 |
public List<TodayAchievedMovementModel> getTodayTargetByMovement(LocalDate startDate, LocalDate endDate) {
|
|
|
48 |
LOGGER.info("start date {}, end date {}", startDate, endDate);
|
|
|
49 |
Session session = sessionFactory.getCurrentSession();
|
|
|
50 |
final TypedQuery<TodayAchievedMovementModel> typedQuerySimilar = session.createNamedQuery("RBMTarget.TodayTargetByMovement", TodayAchievedMovementModel.class);
|
|
|
51 |
typedQuerySimilar.setParameter("startDate", startDate);
|
|
|
52 |
typedQuerySimilar.setParameter("endDate", endDate);
|
|
|
53 |
return typedQuerySimilar.getResultList();
|
|
|
54 |
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
@Cacheable(value = "warehouseMobileStockByMovement", cacheManager = "oneDayCacheManager")
|
|
|
59 |
public List<WarehouseMobileStockByMovementModel> getWarehouseMobileStockByMovement() {
|
|
|
60 |
Session session = sessionFactory.getCurrentSession();
|
|
|
61 |
final TypedQuery<WarehouseMobileStockByMovementModel> typedQuerySimilar = session.createNamedQuery("WarehouseStock.MovementWiseMobileStock", WarehouseMobileStockByMovementModel.class);
|
|
|
62 |
|
|
|
63 |
return typedQuerySimilar.getResultList();
|
|
|
64 |
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
public int getWorkingDaysCount(LocalDate startDate) {
|
|
|
69 |
Session session = sessionFactory.getCurrentSession();
|
|
|
70 |
|
|
|
71 |
// Convert the LocalDate to a format MySQL can interpret
|
|
|
72 |
String startDateString = startDate.toString();
|
|
|
73 |
|
|
|
74 |
final NativeQuery<?> nativeQuery = session.createNativeQuery(
|
|
|
75 |
"SELECT (DATEDIFF(LAST_DAY(:startDate), :startDate) + 1) " +
|
|
|
76 |
" - (FLOOR((DATEDIFF(LAST_DAY(:startDate), :startDate) + (WEEKDAY(:startDate) + 1)) / 7)) " +
|
|
|
77 |
" - (CASE WHEN WEEKDAY(:startDate) = 6 THEN 1 ELSE 0 END) " +
|
|
|
78 |
" - (SELECT COUNT(*) " +
|
|
|
79 |
" FROM logistics.publicholidays " +
|
|
|
80 |
" WHERE date BETWEEN :startDate AND LAST_DAY(:startDate) " +
|
|
|
81 |
" AND WEEKDAY(date) != 6) AS working_days"
|
|
|
82 |
);
|
|
|
83 |
|
|
|
84 |
// Set the start date parameter for each placeholder
|
|
|
85 |
nativeQuery.setParameter("startDate", startDateString);
|
|
|
86 |
|
|
|
87 |
Object result = nativeQuery.getSingleResult();
|
|
|
88 |
return result != null ? ((Number) result).intValue() : 0;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
@Override
|
|
|
93 |
public List<RbmArrViewModel> getRbmTodayArr() throws Exception {
|
|
|
94 |
LocalDate todayDate = LocalDate.now();
|
|
|
95 |
return getRbmTodayArr(todayDate);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
@Override
|
|
|
99 |
public List<RbmArrViewModel> getRbmTodayArr(LocalDate todayDate) throws Exception {
|
|
|
100 |
|
|
|
101 |
LocalDate startDateOfMonthDay1 = LocalDate.now().withDayOfMonth(1);
|
|
|
102 |
|
|
|
103 |
List<WarehouseRbmTargetModel> warehouseRbmTargetModels = this.getWarehouseWiseRbmMonthlyTarget();
|
|
|
104 |
|
|
|
105 |
List<TodayAchievedMovementModel> todayAchievedMovementModels = getTodayTargetByMovement(todayDate, todayDate.plusDays(1));
|
|
|
106 |
// LOGGER.info("todayAchievedMovementModels {} ", todayAchievedMovementModels);
|
|
|
107 |
|
|
|
108 |
List<MTDAchievedTargetModel> mtdAchievedTargetModels = getDateWiseAchievedTargetOfRbm(startDateOfMonthDay1, todayDate);
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
int remainingWorkingDaysCount = getWorkingDaysCount(todayDate);
|
|
|
112 |
|
|
|
113 |
List<WarehouseMobileStockByMovementModel> warehouseMobileStockByMovementModels = getWarehouseMobileStockByMovement();
|
|
|
114 |
// LOGGER.info("warehouseMobileStockByMovementModels {}",warehouseMobileStockByMovementModels);
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
List<RbmArrViewModel> rbmArrViewModels = new ArrayList<>();
|
|
|
118 |
|
|
|
119 |
for (WarehouseRbmTargetModel rbmTarget : warehouseRbmTargetModels) {
|
|
|
120 |
// LOGGER.info("rbmTarget {}",rbmTarget);
|
|
|
121 |
|
|
|
122 |
float monthlyTarget = rbmTarget.getMonthlyTarget();
|
|
|
123 |
float achievedSoFar = (float) mtdAchievedTargetModels.stream()
|
|
|
124 |
.filter(x -> x.getAuthId() == rbmTarget.getAuthId() && x.getWarehouseId() == rbmTarget.getWarehouseId())
|
|
|
125 |
.mapToDouble(MTDAchievedTargetModel::getAcheivedMonthlyTarget)
|
|
|
126 |
.sum();
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
float remainingTarget = monthlyTarget - achievedSoFar;
|
|
|
130 |
float todayTarget = remainingWorkingDaysCount > 0 ? remainingTarget / remainingWorkingDaysCount : 0;
|
|
|
131 |
|
|
|
132 |
// Get the warehouse stock data
|
|
|
133 |
WarehouseMobileStockByMovementModel warehouseMobileStockByMovementModel = warehouseMobileStockByMovementModels.stream()
|
|
|
134 |
.filter(x -> x.getWarehouseId() == rbmTarget.getWarehouseId()).findFirst().orElse(null);
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
if (warehouseMobileStockByMovementModel != null) {
|
|
|
138 |
|
|
|
139 |
// Total stock value for this warehouse
|
|
|
140 |
float totalStockValue = warehouseMobileStockByMovementModel.getTotalAvailabilityPrice();
|
|
|
141 |
|
|
|
142 |
// Calculate target allocation based on stock value proportion
|
|
|
143 |
float hidTarget = (warehouseMobileStockByMovementModel.getTotalHidCatalogPrice() / totalStockValue) * todayTarget;
|
|
|
144 |
float fastMovingTarget = (warehouseMobileStockByMovementModel.getTotalFastMovingCatalogPrice() / totalStockValue) * todayTarget;
|
|
|
145 |
float slowMovingTarget = (warehouseMobileStockByMovementModel.getTotalSlowMovingCatalogPrice() / totalStockValue) * todayTarget;
|
|
|
146 |
float eolTarget = (warehouseMobileStockByMovementModel.getTotalEolCatalogPrice() / totalStockValue) * todayTarget;
|
|
|
147 |
float otherTarget = (warehouseMobileStockByMovementModel.getTotalOtherCategoryCatalogPrice() / totalStockValue) * todayTarget;
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
// set all realated value in view model for dashboard
|
|
|
151 |
|
|
|
152 |
String warehouseName = ProfitMandiConstants.WAREHOUSE_MAP.getOrDefault(rbmTarget.getWarehouseId(), "Unknown");
|
|
|
153 |
|
|
|
154 |
TodayAchievedMovementModel todayAchievedMovementModel = todayAchievedMovementModels.stream().filter(x -> x.getAuthId() == rbmTarget.getAuthId() && x.getWarehouseId() == rbmTarget.getWarehouseId()).findFirst().get();
|
|
|
155 |
// LOGGER.info("todayAchievedMovementModel {}",todayAchievedMovementModel);
|
|
|
156 |
RbmArrViewModel viewModel = new RbmArrViewModel();
|
|
|
157 |
|
|
|
158 |
viewModel.setAuthId(rbmTarget.getAuthId());
|
|
|
159 |
viewModel.setRbmName(rbmTarget.getRbmName());
|
|
|
160 |
viewModel.setWarehouseName(warehouseName);
|
|
|
161 |
viewModel.setTodayTarget(Math.round(todayTarget));
|
|
|
162 |
viewModel.setMonthlyTarget(Math.round(monthlyTarget));
|
|
|
163 |
viewModel.setMtdAchievedTarget(Math.round(achievedSoFar));
|
|
|
164 |
viewModel.setTodayHidTarget(Math.round((hidTarget)));
|
|
|
165 |
viewModel.setTodayFastMovingTarget(Math.round(fastMovingTarget));
|
|
|
166 |
viewModel.setTodaySlowMovingTarget(Math.round(slowMovingTarget));
|
|
|
167 |
viewModel.setTodayEolTarget(Math.round(eolTarget));
|
|
|
168 |
viewModel.setTodayOtherMovingTarget(Math.round(otherTarget));
|
|
|
169 |
viewModel.setTodayAchievedHidTarget(Math.round(todayAchievedMovementModel.getHidBilled()));
|
|
|
170 |
viewModel.setTodayAchievedFastMovingTarget(Math.round(todayAchievedMovementModel.getFastMovingBilled()));
|
|
|
171 |
viewModel.setTodayAchievedSlowMovingTarget(Math.round(todayAchievedMovementModel.getSlowMovingBilled()));
|
|
|
172 |
viewModel.setTodayAchievedEolTarget(Math.round(todayAchievedMovementModel.getEolBilled()));
|
|
|
173 |
viewModel.setTodayAchievedOtherMovingTarget(Math.round(todayAchievedMovementModel.getOtherBilled()));
|
|
|
174 |
viewModel.setTotalAchievedTarget(Math.round(todayAchievedMovementModel.getHidBilled() + todayAchievedMovementModel.getFastMovingBilled() + todayAchievedMovementModel.getSlowMovingBilled() + todayAchievedMovementModel.getEolBilled() + todayAchievedMovementModel.getOtherBilled()));
|
|
|
175 |
|
|
|
176 |
rbmArrViewModels.add(viewModel);
|
|
|
177 |
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
LOGGER.info("rbmArrViewModels {}", rbmArrViewModels);
|
|
|
182 |
return rbmArrViewModels;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
}
|