| 34306 |
ranu |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34606 |
ranu |
4 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 34321 |
ranu |
5 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
6 |
import com.spice.profitmandi.dao.cart.CartService;
|
| 34450 |
ranu |
7 |
import com.spice.profitmandi.dao.cart.SmartCartService;
|
| 34321 |
ranu |
8 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 34606 |
ranu |
9 |
import com.spice.profitmandi.dao.entity.fofo.*;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.logistics.AST;
|
|
|
11 |
import com.spice.profitmandi.dao.entity.logistics.ASTRepository;
|
|
|
12 |
import com.spice.profitmandi.dao.entity.logistics.AreaRepository;
|
| 34306 |
ranu |
13 |
import com.spice.profitmandi.dao.entity.transaction.Loan;
|
| 34606 |
ranu |
14 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 34306 |
ranu |
15 |
import com.spice.profitmandi.dao.entity.transaction.Transaction;
|
| 34321 |
ranu |
16 |
import com.spice.profitmandi.dao.entity.user.User;
|
|
|
17 |
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
|
| 34606 |
ranu |
18 |
import com.spice.profitmandi.dao.model.BIRetailerModel;
|
|
|
19 |
import com.spice.profitmandi.dao.model.BiSecondaryModel;
|
|
|
20 |
import com.spice.profitmandi.dao.model.ReturnOrderInfoModel;
|
|
|
21 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 34321 |
ranu |
22 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 34606 |
ranu |
24 |
import com.spice.profitmandi.dao.repository.fofo.*;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
|
| 34306 |
ranu |
26 |
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
|
| 34606 |
ranu |
27 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
28 |
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderInfoRepository;
|
| 34306 |
ranu |
29 |
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
|
| 34321 |
ranu |
30 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 34308 |
ranu |
31 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
| 34606 |
ranu |
32 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
33 |
import in.shop2020.model.v1.order.OrderStatus;
|
| 34306 |
ranu |
34 |
import org.apache.logging.log4j.LogManager;
|
|
|
35 |
import org.apache.logging.log4j.Logger;
|
|
|
36 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
37 |
import org.springframework.beans.factory.annotation.Value;
|
| 34321 |
ranu |
38 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
39 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 34306 |
ranu |
40 |
import org.springframework.stereotype.Component;
|
|
|
41 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
42 |
|
| 34321 |
ranu |
43 |
import javax.mail.MessagingException;
|
|
|
44 |
import javax.mail.internet.InternetAddress;
|
|
|
45 |
import javax.mail.internet.MimeMessage;
|
|
|
46 |
import java.io.IOException;
|
|
|
47 |
import java.time.LocalDate;
|
| 34306 |
ranu |
48 |
import java.time.LocalDateTime;
|
| 34606 |
ranu |
49 |
import java.time.LocalTime;
|
|
|
50 |
import java.time.YearMonth;
|
| 34306 |
ranu |
51 |
import java.time.temporal.ChronoUnit;
|
|
|
52 |
import java.util.*;
|
| 34321 |
ranu |
53 |
import java.util.stream.Collectors;
|
| 34306 |
ranu |
54 |
|
|
|
55 |
@Component
|
|
|
56 |
@Transactional(rollbackFor = {Throwable.class, ProfitMandiBusinessException.class})
|
|
|
57 |
public class ScheduledTasksTest {
|
|
|
58 |
|
|
|
59 |
private static final Logger LOGGER = LogManager.getLogger(ScheduledTasksTest.class);
|
|
|
60 |
|
|
|
61 |
@Autowired
|
|
|
62 |
TransactionRepository transactionRepository;
|
|
|
63 |
|
|
|
64 |
@Autowired
|
|
|
65 |
LoanRepository loanRepository;
|
|
|
66 |
|
| 34308 |
ranu |
67 |
@Autowired
|
|
|
68 |
SDCreditService sdCreditService;
|
|
|
69 |
|
| 34321 |
ranu |
70 |
@Autowired
|
|
|
71 |
UserRepository userRepository;
|
|
|
72 |
|
|
|
73 |
@Autowired
|
|
|
74 |
CsService csService;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
|
|
77 |
RbmRatingRepository rbmRatingRepository;
|
|
|
78 |
|
|
|
79 |
@Autowired
|
|
|
80 |
private JavaMailSender mailSender;
|
|
|
81 |
|
|
|
82 |
@Autowired
|
|
|
83 |
SalesRatingRepository salesRatingRepository;
|
|
|
84 |
|
|
|
85 |
@Autowired
|
|
|
86 |
FofoStoreRepository fofoStoreRepository;
|
|
|
87 |
|
| 34450 |
ranu |
88 |
@Autowired
|
|
|
89 |
SmartCartService smartCartService;
|
|
|
90 |
|
| 34606 |
ranu |
91 |
@Autowired
|
|
|
92 |
RetailerService retailerService;
|
|
|
93 |
|
|
|
94 |
@Autowired
|
|
|
95 |
ASTRepository astRepository;
|
|
|
96 |
|
|
|
97 |
@Autowired
|
|
|
98 |
AuthRepository authRepository;
|
|
|
99 |
|
|
|
100 |
@Autowired
|
|
|
101 |
StateRepository stateRepository;
|
|
|
102 |
|
|
|
103 |
@Autowired
|
|
|
104 |
MonthlyTargetRepository monthlyTargetRepository;
|
|
|
105 |
|
|
|
106 |
@Autowired
|
|
|
107 |
PartnerTypeChangeService partnerTypeChangeService;
|
|
|
108 |
|
|
|
109 |
@Autowired
|
|
|
110 |
ReturnOrderInfoRepository returnOrderInfoRepository;
|
|
|
111 |
|
|
|
112 |
@Autowired
|
|
|
113 |
OrderRepository orderRepository;
|
|
|
114 |
|
| 34321 |
ranu |
115 |
public void test() throws Exception {
|
| 34366 |
ranu |
116 |
System.out.println("test start");
|
| 34606 |
ranu |
117 |
// smartCartService.activatedButNotBilledCatalogQtyMap();
|
| 34366 |
ranu |
118 |
System.out.println("test end");
|
| 34306 |
ranu |
119 |
|
|
|
120 |
}
|
|
|
121 |
|
| 34308 |
ranu |
122 |
public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
|
|
|
123 |
sdCreditService.createLoanForBilling(transactionId, invoiceAmount, invoiceNumber);
|
| 34306 |
ranu |
124 |
|
| 34308 |
ranu |
125 |
}
|
| 34306 |
ranu |
126 |
|
| 34308 |
ranu |
127 |
|
| 34321 |
ranu |
128 |
private void sendMailHtmlFormat(String email[], String body, String cc[], String bcc[], String subject)
|
|
|
129 |
throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
130 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
131 |
MimeMessageHelper helper = new MimeMessageHelper(message);
|
|
|
132 |
helper.setSubject(subject);
|
|
|
133 |
helper.setText(body, true);
|
|
|
134 |
helper.setTo(email);
|
|
|
135 |
if (cc != null) {
|
|
|
136 |
helper.setCc(cc);
|
|
|
137 |
}
|
|
|
138 |
if (bcc != null) {
|
|
|
139 |
helper.setBcc(bcc);
|
| 34308 |
ranu |
140 |
|
| 34321 |
ranu |
141 |
}
|
|
|
142 |
|
|
|
143 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
|
|
|
144 |
helper.setFrom(senderAddress);
|
|
|
145 |
mailSender.send(message);
|
|
|
146 |
}
|
|
|
147 |
|
| 34307 |
ranu |
148 |
public Map<Integer,Integer> findLoanTransactionMapingAccordingLoan(List<Integer> loanIds) throws ProfitMandiBusinessException {
|
| 34306 |
ranu |
149 |
|
|
|
150 |
Map<Integer, Integer> transactionLoanMap = new HashMap<>();
|
|
|
151 |
|
|
|
152 |
for(int loanId : loanIds){
|
|
|
153 |
Transaction transaction = null;
|
|
|
154 |
Loan loan = loanRepository.selectByLoanId(loanId);
|
|
|
155 |
List<Transaction> transactions = transactionRepository.selectByRetailerId(loan.getFofoId());
|
|
|
156 |
|
|
|
157 |
LocalDateTime nearestDateTime = transactions.stream().map(x -> x.getCreateTimestamp())
|
|
|
158 |
.min(Comparator.comparingLong(x -> Math.abs(ChronoUnit.MILLIS.between(x, loan.getCreatedOn()))))
|
|
|
159 |
.orElse(null);
|
|
|
160 |
|
|
|
161 |
if (nearestDateTime != null && loan.getCreatedOn().plusMinutes(2).isAfter(nearestDateTime) &&
|
|
|
162 |
loan.getCreatedOn().minusMinutes(1).isBefore(nearestDateTime)) {
|
|
|
163 |
// Here transaction is still null
|
|
|
164 |
transaction = transactions.stream()
|
|
|
165 |
.filter(x -> x.getCreateTimestamp().equals(nearestDateTime))
|
|
|
166 |
.findFirst().get();
|
|
|
167 |
transactionLoanMap.put(transaction.getId(), loanId);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
}
|
|
|
171 |
LOGGER.info("transactionLoanMap {}",transactionLoanMap);
|
|
|
172 |
return transactionLoanMap;
|
|
|
173 |
}
|
| 34321 |
ranu |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
public void sendRbmFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
178 |
LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
179 |
LocalDateTime endOfMonth = LocalDateTime.now();
|
| 34323 |
ranu |
180 |
String[] bcc = {"tarun.verma@smartdukaan.com"};
|
| 34321 |
ranu |
181 |
|
|
|
182 |
// Get all RBM users
|
|
|
183 |
List<AuthUser> authUsers = csService.getAuthUserIds(
|
|
|
184 |
ProfitMandiConstants.TICKET_CATEGORY_RBM,
|
|
|
185 |
Arrays.asList(EscalationType.L1)
|
|
|
186 |
);
|
|
|
187 |
|
|
|
188 |
if (authUsers.isEmpty()) {
|
|
|
189 |
LOGGER.info("No RBMs found.");
|
|
|
190 |
return;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
List<Integer> rbmIds = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
|
|
|
194 |
|
|
|
195 |
// Fetch ratings for all RBMs for current month
|
|
|
196 |
List<RbmRating> feedbackList = rbmRatingRepository.selectByRbmIdsAndDateRange(rbmIds, startOfMonth, endOfMonth);
|
|
|
197 |
|
|
|
198 |
if (feedbackList.isEmpty()) {
|
|
|
199 |
LOGGER.info("No feedback entries found for RBMs.");
|
|
|
200 |
return;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
// Sort feedback by createTimeStamp DESC
|
|
|
204 |
feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
|
|
|
205 |
|
|
|
206 |
// Fetch and map FOFO (partner) names
|
|
|
207 |
Map<Integer, String> fofoNameMap = new HashMap<>();
|
|
|
208 |
for (RbmRating rating : feedbackList) {
|
|
|
209 |
int fofoId = rating.getFofoId();
|
|
|
210 |
if (!fofoNameMap.containsKey(fofoId)) {
|
|
|
211 |
User fofoUser = userRepository.selectById(fofoId);
|
|
|
212 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
213 |
|
|
|
214 |
String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
|
|
|
215 |
String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
|
|
|
216 |
|
|
|
217 |
String displayName = partnerName + " (" + storeCode + ")";
|
|
|
218 |
fofoNameMap.put(fofoId, displayName);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
// Map RBM ID to name for quick lookup
|
|
|
223 |
Map<Integer, String> rbmNameMap = authUsers.stream()
|
|
|
224 |
.collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
|
|
|
225 |
|
|
|
226 |
// Generate HTML content
|
|
|
227 |
StringBuilder emailContent = new StringBuilder();
|
|
|
228 |
emailContent.append("<html><body>");
|
|
|
229 |
emailContent.append("<p>Dear Team,</p>");
|
|
|
230 |
emailContent.append("<p>Here is the <b>latest RBM Rating and Feedback Summary</b> for ")
|
|
|
231 |
.append(LocalDate.now().getMonth()).append(":</p>");
|
|
|
232 |
|
|
|
233 |
emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
|
|
|
234 |
emailContent.append("<tr>")
|
|
|
235 |
.append("<th>RBM Name</th>")
|
|
|
236 |
.append("<th>Partner Name</th>")
|
|
|
237 |
.append("<th>Rating</th>")
|
|
|
238 |
.append("<th>Comment</th>")
|
|
|
239 |
.append("<th>Date</th>")
|
|
|
240 |
.append("</tr>");
|
|
|
241 |
|
|
|
242 |
for (RbmRating rating : feedbackList) {
|
|
|
243 |
String rbmName = rbmNameMap.getOrDefault(rating.getRbmId(), "Unknown RBM");
|
|
|
244 |
String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
|
|
|
245 |
emailContent.append("<tr>")
|
|
|
246 |
.append("<td>").append(rbmName).append("</td>")
|
|
|
247 |
.append("<td>").append(partnerName).append("</td>")
|
|
|
248 |
.append("<td>").append(rating.getRating()).append("</td>")
|
|
|
249 |
.append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
|
|
|
250 |
.append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
|
|
|
251 |
.append("</tr>");
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
emailContent.append("</table>");
|
|
|
255 |
emailContent.append("<br><p>Regards,<br>Smart Dukaan Team</p>");
|
|
|
256 |
emailContent.append("</body></html>");
|
|
|
257 |
|
|
|
258 |
String subject = "Monthly RBM Feedback Summary - " + LocalDate.now().getMonth();
|
|
|
259 |
|
|
|
260 |
List<String> sendTo = new ArrayList<>();
|
| 34323 |
ranu |
261 |
sendTo.add("sm@smartdukaan.com"); //
|
|
|
262 |
sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
|
|
|
263 |
sendTo.add("kamini.sharma@smartdukaan.com"); //
|
| 34321 |
ranu |
264 |
|
|
|
265 |
String[] emailRecipients = sendTo.toArray(new String[0]);
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
|
|
|
269 |
|
|
|
270 |
LOGGER.info("Consolidated RBM feedback summary email sent.");
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
|
|
|
274 |
public void sendSalesFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
275 |
LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
276 |
LocalDateTime endOfMonth = LocalDateTime.now();
|
| 34323 |
ranu |
277 |
String[] bcc = {"tarun.verma@smartdukaan.com"};
|
| 34411 |
tejus.loha |
278 |
// String[] bcc = {"tejus.lohani@smartdukaan.com"};
|
| 34321 |
ranu |
279 |
|
|
|
280 |
// Get all RBM users
|
|
|
281 |
List<AuthUser> authUsers = csService.getAuthUserIds(
|
|
|
282 |
ProfitMandiConstants.TICKET_CATEGORY_SALES,
|
|
|
283 |
Arrays.asList(EscalationType.L1)
|
|
|
284 |
);
|
|
|
285 |
|
|
|
286 |
if (authUsers.isEmpty()) {
|
|
|
287 |
LOGGER.info("No sales person found.");
|
|
|
288 |
return;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
List<Integer> salesL1Ids = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
|
|
|
292 |
|
|
|
293 |
// Fetch ratings for all RBMs for current month
|
|
|
294 |
List<SalesRating> feedbackList = salesRatingRepository.selectBySalesL1IdsAndDateRange(salesL1Ids, startOfMonth, endOfMonth);
|
|
|
295 |
|
|
|
296 |
if (feedbackList.isEmpty()) {
|
|
|
297 |
LOGGER.info("No feedback entries found for Sales.");
|
|
|
298 |
return;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
// Sort feedback by createTimeStamp DESC
|
|
|
302 |
feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
|
|
|
303 |
|
|
|
304 |
// Fetch and map FOFO (partner) names
|
|
|
305 |
Map<Integer, String> fofoNameMap = new HashMap<>();
|
|
|
306 |
for (SalesRating rating : feedbackList) {
|
|
|
307 |
int fofoId = rating.getFofoId();
|
|
|
308 |
if (!fofoNameMap.containsKey(fofoId)) {
|
|
|
309 |
User fofoUser = userRepository.selectById(fofoId);
|
|
|
310 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
311 |
|
|
|
312 |
String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
|
|
|
313 |
String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
|
|
|
314 |
|
|
|
315 |
String displayName = partnerName + " (" + storeCode + ")";
|
|
|
316 |
fofoNameMap.put(fofoId, displayName);
|
|
|
317 |
}
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
// Map RBM ID to name for quick lookup
|
|
|
321 |
Map<Integer, String> salesL1NameMap = authUsers.stream()
|
|
|
322 |
.collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
|
|
|
323 |
|
|
|
324 |
// Generate HTML content
|
|
|
325 |
StringBuilder emailContent = new StringBuilder();
|
|
|
326 |
emailContent.append("<html><body>");
|
|
|
327 |
emailContent.append("<p>Dear Team,</p>");
|
|
|
328 |
emailContent.append("<p>Here is the <b>latest Sales L1 Rating and Feedback Summary</b> for ")
|
|
|
329 |
.append(LocalDate.now().getMonth()).append(":</p>");
|
|
|
330 |
|
|
|
331 |
emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
|
|
|
332 |
emailContent.append("<tr>")
|
|
|
333 |
.append("<th>Sales L1 Name</th>")
|
|
|
334 |
.append("<th>Partner Name</th>")
|
| 34411 |
tejus.loha |
335 |
.append("<th>Partner Category</th>")
|
| 34321 |
ranu |
336 |
.append("<th>Rating</th>")
|
|
|
337 |
.append("<th>Comment</th>")
|
|
|
338 |
.append("<th>Date</th>")
|
|
|
339 |
.append("</tr>");
|
|
|
340 |
|
|
|
341 |
for (SalesRating rating : feedbackList) {
|
|
|
342 |
String salesL1 = salesL1NameMap.getOrDefault(rating.getSalesL1Id(), "Unknown Sales Person");
|
|
|
343 |
String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
|
| 34411 |
tejus.loha |
344 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(rating.getFofoId(), LocalDate.now());
|
| 34321 |
ranu |
345 |
emailContent.append("<tr>")
|
|
|
346 |
.append("<td>").append(salesL1).append("</td>")
|
|
|
347 |
.append("<td>").append(partnerName).append("</td>")
|
| 34411 |
tejus.loha |
348 |
.append("<td>").append(partnerType).append("</td>")
|
| 34321 |
ranu |
349 |
.append("<td>").append(rating.getRating()).append("</td>")
|
|
|
350 |
.append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
|
|
|
351 |
.append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
|
|
|
352 |
.append("</tr>");
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
emailContent.append("</table>");
|
|
|
356 |
emailContent.append("<br><p>Regards,<br>Smartdukaan Team</p>");
|
|
|
357 |
emailContent.append("</body></html>");
|
|
|
358 |
|
| 34411 |
tejus.loha |
359 |
String subject = "Monthly Sales L1 Feedback Summary Test test - " + LocalDate.now().getMonth();
|
| 34321 |
ranu |
360 |
|
|
|
361 |
List<String> sendTo = new ArrayList<>();
|
| 34323 |
ranu |
362 |
sendTo.add("sm@smartdukaan.com"); //
|
|
|
363 |
sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
|
| 34606 |
ranu |
364 |
sendTo.add("kamini.sharma@smartdukaan.com"); //
|
| 34321 |
ranu |
365 |
|
|
|
366 |
String[] emailRecipients = sendTo.toArray(new String[0]);
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
|
|
|
370 |
|
|
|
371 |
LOGGER.info("Consolidated Sales L1 feedback summary email sent.");
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
|
| 34606 |
ranu |
375 |
public void generateBiReportExcel() throws Exception {
|
|
|
376 |
LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
|
| 34321 |
ranu |
377 |
|
| 34606 |
ranu |
378 |
Map<Integer, CustomRetailer> customRetailers = retailerService.getAllFofoRetailers();
|
| 34321 |
ranu |
379 |
|
| 34606 |
ranu |
380 |
List<Integer> retailerIds = customRetailers.values().stream().map(CustomRetailer::getPartnerId).collect(Collectors.toList());
|
|
|
381 |
|
|
|
382 |
// this month return data
|
|
|
383 |
LocalDateTime currentMonthStartDate = YearMonth.now().atDay(1).atStartOfDay();
|
|
|
384 |
LocalDateTime currentMonthEndDate = LocalDateTime.now();
|
|
|
385 |
|
|
|
386 |
List<ReturnOrderInfoModel> currentMonthReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(currentMonthStartDate, currentMonthEndDate);
|
|
|
387 |
Map<Integer, Long> currentMonthPartnerReturnOrderInfoModelMap = currentMonthReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
|
|
|
388 |
|
|
|
389 |
List<Order> currentMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(currentMonthStartDate, currentMonthEndDate, OrderStatus.RTO_REFUNDED);
|
|
|
390 |
Map<Integer, Long> currentMonthRtoRefundOrderMap = currentMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
// last month return data
|
|
|
394 |
YearMonth lastMonth = YearMonth.now().minusMonths(1);
|
|
|
395 |
LocalDateTime lastMontStartDate = lastMonth.atDay(1).atStartOfDay();
|
|
|
396 |
LocalDateTime lastMonthEndDate = lastMonth.atEndOfMonth().atTime(23, 59, 59);
|
|
|
397 |
|
|
|
398 |
List<ReturnOrderInfoModel> lastMonthReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(lastMontStartDate, lastMonthEndDate);
|
|
|
399 |
Map<Integer, Long> lastMonthPartnerReturnOrderInfoModelMap = lastMonthReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
|
|
|
400 |
|
|
|
401 |
List<Order> lastMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(lastMontStartDate, lastMonthEndDate, OrderStatus.RTO_REFUNDED);
|
|
|
402 |
Map<Integer, Long> lastMonthRtoRefundOrderMap = lastMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
|
|
|
403 |
|
|
|
404 |
|
|
|
405 |
// twoMonthsAgo return data
|
|
|
406 |
YearMonth twoMonthsAgo = YearMonth.now().minusMonths(2);
|
|
|
407 |
LocalDateTime twoMonthsAgoStartDate = twoMonthsAgo.atDay(1).atStartOfDay();
|
|
|
408 |
LocalDateTime twoMonthsAgoEndDate = twoMonthsAgo.atEndOfMonth().atTime(23, 59, 59);
|
|
|
409 |
|
|
|
410 |
List<ReturnOrderInfoModel> twoMonthAgoReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(twoMonthsAgoStartDate, twoMonthsAgoEndDate);
|
|
|
411 |
Map<Integer, Long> twoMonthAgoPartnerReturnOrderInfoModelMap = twoMonthAgoReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
|
|
|
412 |
|
|
|
413 |
List<Order> twoMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(twoMonthsAgoStartDate, twoMonthsAgoEndDate, OrderStatus.RTO_REFUNDED);
|
|
|
414 |
Map<Integer, Long> twoMonthRtoRefundOrderMap = twoMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
|
|
|
415 |
|
|
|
416 |
|
|
|
417 |
List<BIRetailerModel> biRetailerModels = new ArrayList<>();
|
|
|
418 |
|
|
|
419 |
List<BiSecondaryModel> biSecondaryModels = new ArrayList<>();
|
|
|
420 |
|
|
|
421 |
for(Integer fofoId: retailerIds){
|
|
|
422 |
|
|
|
423 |
int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM,EscalationType.L1,fofoId);
|
|
|
424 |
String rbmName = authRepository.selectById(rbmL1).getFullName();
|
|
|
425 |
|
|
|
426 |
int bmId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES,EscalationType.L2,fofoId);
|
|
|
427 |
String bmName = authRepository.selectById(bmId).getFullName();
|
|
|
428 |
|
|
|
429 |
int managerId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES,EscalationType.L1,fofoId);
|
|
|
430 |
String managerName = " ";
|
|
|
431 |
if(managerId != 0){
|
|
|
432 |
managerName = authRepository.selectById(managerId).getFullName();
|
|
|
433 |
}else {
|
|
|
434 |
managerName = bmName;
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
AST ast = astRepository.selectById(customRetailers.get(fofoId).getAstId());
|
|
|
438 |
|
|
|
439 |
PartnerType partnerTypeThisMonth = partnerTypeChangeService.getTypeOnMonth(fofoId, YearMonth.now());
|
|
|
440 |
|
|
|
441 |
// generate retaile detail
|
|
|
442 |
|
|
|
443 |
BIRetailerModel biRetailerModel = new BIRetailerModel();
|
|
|
444 |
biRetailerModel.setBmName(bmName);
|
|
|
445 |
biRetailerModel.setCode(customRetailers.get(fofoId).getCode());
|
|
|
446 |
biRetailerModel.setArea(ast.getArea());
|
|
|
447 |
biRetailerModel.setCity(customRetailers.get(fofoId).getAddress().getCity());
|
|
|
448 |
biRetailerModel.setStoreName(customRetailers.get(fofoId).getBusinessName());
|
|
|
449 |
biRetailerModel.setStoreName(String.valueOf(customRetailers.get(fofoId).getActivationType()));
|
|
|
450 |
biRetailerModel.setCategory(String.valueOf(partnerTypeThisMonth));
|
|
|
451 |
biRetailerModel.setSalesManager(managerName);
|
|
|
452 |
biRetailerModel.setRbm(rbmName);
|
|
|
453 |
biRetailerModels.add(biRetailerModel);
|
|
|
454 |
|
|
|
455 |
// generate secondary data
|
|
|
456 |
|
|
|
457 |
// this month secondary target
|
|
|
458 |
|
|
|
459 |
MonthlyTarget secondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(YearMonth.now(), fofoId);
|
|
|
460 |
|
|
|
461 |
Map<Integer, Double> secondaryMtd = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
|
|
|
462 |
Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
|
|
|
463 |
startOfToday.withDayOfMonth(1), startOfToday.with(LocalTime.MAX)).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
|
|
|
464 |
|
|
|
465 |
double secondaryAchievedMtd = secondaryMtd.getOrDefault(fofoId, 0.0);
|
|
|
466 |
|
|
|
467 |
|
|
|
468 |
// last month secondary target
|
|
|
469 |
|
|
|
470 |
MonthlyTarget lastMonthSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId);
|
|
|
471 |
|
|
|
472 |
Map<Integer, Double> lastMonthSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
|
|
|
473 |
Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
|
|
|
474 |
lastMontStartDate, lastMonthEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
|
|
|
475 |
|
|
|
476 |
double lastMonthSecondaryAchieved = lastMonthSecondary.getOrDefault(fofoId, 0.0);
|
|
|
477 |
|
|
|
478 |
|
|
|
479 |
// last month secondary target
|
|
|
480 |
|
|
|
481 |
MonthlyTarget twoMonthAgoSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId);
|
|
|
482 |
|
|
|
483 |
Map<Integer, Double> twoMonthAgoSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
|
|
|
484 |
Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
|
|
|
485 |
twoMonthsAgoStartDate, twoMonthsAgoEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
|
|
|
486 |
|
|
|
487 |
double twoMonthAgoSecondaryAchieved = twoMonthAgoSecondary.getOrDefault(fofoId, 0.0);
|
|
|
488 |
|
|
|
489 |
//
|
|
|
490 |
//// this month tertiary
|
|
|
491 |
// Double todaySale = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday, now, fofoId, false).get(fofoId);
|
|
|
492 |
// Double mtdSaleTillYesterDay = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday.withDayOfMonth(1), startOfToday, fofoId, false).get(fofoId);
|
|
|
493 |
// Double mtdSale = mtdSaleTillYesterDay + todaySale;
|
|
|
494 |
// Double lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
|
|
495 |
// startOfToday.withDayOfMonth(1).minusMonths(1), now.minusMonths(1), fofoId, false).get(fofoId);
|
|
|
496 |
//
|
|
|
497 |
//
|
|
|
498 |
// BiSecondaryModel biSecondaryModel = new BiSecondaryModel();
|
|
|
499 |
|
|
|
500 |
}
|
|
|
501 |
|
|
|
502 |
LOGGER.info("biRetailerModels {}",biRetailerModels);
|
|
|
503 |
|
|
|
504 |
|
|
|
505 |
|
|
|
506 |
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
|
|
|
510 |
|
|
|
511 |
|
| 34306 |
ranu |
512 |
}
|