| 34306 |
ranu |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34619 |
ranu |
4 |
import com.spice.profitmandi.common.model.BrandStockPrice;
|
| 34606 |
ranu |
5 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 34321 |
ranu |
6 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 34619 |
ranu |
7 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
8 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
|
|
9 |
import com.spice.profitmandi.common.util.Utils;
|
| 34321 |
ranu |
10 |
import com.spice.profitmandi.dao.cart.CartService;
|
| 34450 |
ranu |
11 |
import com.spice.profitmandi.dao.cart.SmartCartService;
|
| 34321 |
ranu |
12 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 34606 |
ranu |
13 |
import com.spice.profitmandi.dao.entity.fofo.*;
|
|
|
14 |
import com.spice.profitmandi.dao.entity.logistics.AST;
|
|
|
15 |
import com.spice.profitmandi.dao.entity.logistics.ASTRepository;
|
|
|
16 |
import com.spice.profitmandi.dao.entity.logistics.AreaRepository;
|
| 34619 |
ranu |
17 |
import com.spice.profitmandi.dao.entity.transaction.*;
|
| 34321 |
ranu |
18 |
import com.spice.profitmandi.dao.entity.user.User;
|
|
|
19 |
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
|
| 34619 |
ranu |
20 |
import com.spice.profitmandi.dao.enumuration.transaction.LoanReferenceType;
|
| 34606 |
ranu |
21 |
import com.spice.profitmandi.dao.model.BIRetailerModel;
|
|
|
22 |
import com.spice.profitmandi.dao.model.BiSecondaryModel;
|
| 34619 |
ranu |
23 |
import com.spice.profitmandi.dao.model.BrandWiseModel;
|
| 34606 |
ranu |
24 |
import com.spice.profitmandi.dao.model.ReturnOrderInfoModel;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 34321 |
ranu |
26 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 34606 |
ranu |
28 |
import com.spice.profitmandi.dao.repository.fofo.*;
|
|
|
29 |
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
|
| 34619 |
ranu |
30 |
import com.spice.profitmandi.dao.repository.transaction.*;
|
| 34321 |
ranu |
31 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 34619 |
ranu |
32 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 34308 |
ranu |
33 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
| 34606 |
ranu |
34 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 34619 |
ranu |
35 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 34606 |
ranu |
36 |
import in.shop2020.model.v1.order.OrderStatus;
|
| 34619 |
ranu |
37 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
38 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 34306 |
ranu |
39 |
import org.apache.logging.log4j.LogManager;
|
|
|
40 |
import org.apache.logging.log4j.Logger;
|
| 34619 |
ranu |
41 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
42 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
43 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
44 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| 34306 |
ranu |
45 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 34619 |
ranu |
46 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 34306 |
ranu |
47 |
import org.springframework.beans.factory.annotation.Value;
|
| 34619 |
ranu |
48 |
import org.springframework.core.io.ByteArrayResource;
|
| 34321 |
ranu |
49 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
50 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 34306 |
ranu |
51 |
import org.springframework.stereotype.Component;
|
|
|
52 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
53 |
|
| 34321 |
ranu |
54 |
import javax.mail.MessagingException;
|
|
|
55 |
import javax.mail.internet.InternetAddress;
|
|
|
56 |
import javax.mail.internet.MimeMessage;
|
| 34619 |
ranu |
57 |
import java.io.*;
|
|
|
58 |
import java.math.BigDecimal;
|
| 34321 |
ranu |
59 |
import java.time.LocalDate;
|
| 34306 |
ranu |
60 |
import java.time.LocalDateTime;
|
| 34606 |
ranu |
61 |
import java.time.LocalTime;
|
|
|
62 |
import java.time.YearMonth;
|
| 34306 |
ranu |
63 |
import java.time.temporal.ChronoUnit;
|
|
|
64 |
import java.util.*;
|
| 34321 |
ranu |
65 |
import java.util.stream.Collectors;
|
| 34619 |
ranu |
66 |
import java.util.stream.Stream;
|
| 34306 |
ranu |
67 |
|
|
|
68 |
@Component
|
|
|
69 |
@Transactional(rollbackFor = {Throwable.class, ProfitMandiBusinessException.class})
|
|
|
70 |
public class ScheduledTasksTest {
|
|
|
71 |
|
|
|
72 |
private static final Logger LOGGER = LogManager.getLogger(ScheduledTasksTest.class);
|
|
|
73 |
|
|
|
74 |
@Autowired
|
|
|
75 |
TransactionRepository transactionRepository;
|
|
|
76 |
|
|
|
77 |
@Autowired
|
| 34619 |
ranu |
78 |
@Qualifier(value = "googleMailSender")
|
|
|
79 |
private JavaMailSender googleMailSender;
|
|
|
80 |
|
|
|
81 |
@Autowired
|
| 34306 |
ranu |
82 |
LoanRepository loanRepository;
|
|
|
83 |
|
| 34308 |
ranu |
84 |
@Autowired
|
|
|
85 |
SDCreditService sdCreditService;
|
|
|
86 |
|
| 34321 |
ranu |
87 |
@Autowired
|
|
|
88 |
UserRepository userRepository;
|
|
|
89 |
|
|
|
90 |
@Autowired
|
|
|
91 |
CsService csService;
|
|
|
92 |
|
|
|
93 |
@Autowired
|
|
|
94 |
RbmRatingRepository rbmRatingRepository;
|
|
|
95 |
|
|
|
96 |
@Autowired
|
|
|
97 |
private JavaMailSender mailSender;
|
|
|
98 |
|
|
|
99 |
@Autowired
|
|
|
100 |
SalesRatingRepository salesRatingRepository;
|
|
|
101 |
|
|
|
102 |
@Autowired
|
|
|
103 |
FofoStoreRepository fofoStoreRepository;
|
|
|
104 |
|
| 34450 |
ranu |
105 |
@Autowired
|
|
|
106 |
SmartCartService smartCartService;
|
|
|
107 |
|
| 34606 |
ranu |
108 |
@Autowired
|
|
|
109 |
RetailerService retailerService;
|
|
|
110 |
|
|
|
111 |
@Autowired
|
|
|
112 |
ASTRepository astRepository;
|
|
|
113 |
|
|
|
114 |
@Autowired
|
|
|
115 |
AuthRepository authRepository;
|
|
|
116 |
|
|
|
117 |
@Autowired
|
|
|
118 |
StateRepository stateRepository;
|
|
|
119 |
|
|
|
120 |
@Autowired
|
|
|
121 |
MonthlyTargetRepository monthlyTargetRepository;
|
|
|
122 |
|
|
|
123 |
@Autowired
|
|
|
124 |
PartnerTypeChangeService partnerTypeChangeService;
|
|
|
125 |
|
|
|
126 |
@Autowired
|
|
|
127 |
ReturnOrderInfoRepository returnOrderInfoRepository;
|
|
|
128 |
|
|
|
129 |
@Autowired
|
|
|
130 |
OrderRepository orderRepository;
|
|
|
131 |
|
| 34619 |
ranu |
132 |
@Autowired
|
|
|
133 |
FofoOrderItemRepository fofoOrderItemRepository;
|
|
|
134 |
|
|
|
135 |
@Autowired
|
|
|
136 |
InventoryService inventoryService;
|
|
|
137 |
|
|
|
138 |
@Autowired
|
|
|
139 |
UserWalletRepository userWalletRepository;
|
|
|
140 |
|
|
|
141 |
@Autowired
|
|
|
142 |
LoanStatementRepository loanStatementRepository;
|
|
|
143 |
|
|
|
144 |
@Autowired
|
|
|
145 |
WalletService walletService;
|
|
|
146 |
|
| 34321 |
ranu |
147 |
public void test() throws Exception {
|
| 34366 |
ranu |
148 |
System.out.println("test start");
|
| 34619 |
ranu |
149 |
//this.generateBiReportExcel();
|
|
|
150 |
this.loanSettle();
|
| 34366 |
ranu |
151 |
System.out.println("test end");
|
| 34306 |
ranu |
152 |
|
|
|
153 |
}
|
|
|
154 |
|
| 34308 |
ranu |
155 |
public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
|
|
|
156 |
sdCreditService.createLoanForBilling(transactionId, invoiceAmount, invoiceNumber);
|
| 34306 |
ranu |
157 |
|
| 34308 |
ranu |
158 |
}
|
| 34306 |
ranu |
159 |
|
| 34619 |
ranu |
160 |
public void loanSettle() throws Exception {
|
|
|
161 |
List<Integer> refrences = Arrays.asList(25807,36003,38938,39506,42219,45084);
|
|
|
162 |
for(Integer ref : refrences){
|
|
|
163 |
List<LoanStatement> loanStatements = loanStatementRepository.selectByLoanId(ref);
|
|
|
164 |
double amountSum = loanStatements.stream().map(LoanStatement::getAmount).mapToDouble(BigDecimal::doubleValue).sum();
|
|
|
165 |
if(amountSum > 0){
|
|
|
166 |
walletService.addAmountToWallet(loanStatements.get(0).getFofoId(),ref, WalletReferenceType.CREDIT_LIMIT,"Amount reversal against credit limit deduction",(float) amountSum,LocalDateTime.now());
|
| 34308 |
ranu |
167 |
|
| 34619 |
ranu |
168 |
// Loan statement entry
|
|
|
169 |
BigDecimal adjustAmount = BigDecimal.valueOf(amountSum).negate(); // or multiply by -1
|
|
|
170 |
LoanStatement loanStatement = new LoanStatement();
|
|
|
171 |
loanStatement.setAmount(adjustAmount);
|
|
|
172 |
loanStatement.setFofoId(loanStatements.get(0).getFofoId());
|
|
|
173 |
loanStatement.setLoanReferenceType(LoanReferenceType.PRINCIPAL);
|
|
|
174 |
loanStatement.setCreatedAt(LocalDateTime.now());
|
|
|
175 |
loanStatement.setDescription("Amount reversal due to access debit against limit");
|
|
|
176 |
loanStatement.setLoanId(ref);
|
|
|
177 |
loanStatement.setBusinessDate(LocalDateTime.now());
|
|
|
178 |
loanStatementRepository.persist(loanStatement);
|
|
|
179 |
|
|
|
180 |
Loan loan = loanRepository.selectByLoanId(ref);
|
|
|
181 |
loan.setPendingAmount(BigDecimal.valueOf(0));
|
|
|
182 |
loan.setSettledOn(LocalDateTime.now());
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
| 34321 |
ranu |
191 |
private void sendMailHtmlFormat(String email[], String body, String cc[], String bcc[], String subject)
|
|
|
192 |
throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
193 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
194 |
MimeMessageHelper helper = new MimeMessageHelper(message);
|
|
|
195 |
helper.setSubject(subject);
|
|
|
196 |
helper.setText(body, true);
|
|
|
197 |
helper.setTo(email);
|
|
|
198 |
if (cc != null) {
|
|
|
199 |
helper.setCc(cc);
|
|
|
200 |
}
|
|
|
201 |
if (bcc != null) {
|
|
|
202 |
helper.setBcc(bcc);
|
| 34308 |
ranu |
203 |
|
| 34321 |
ranu |
204 |
}
|
|
|
205 |
|
|
|
206 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
|
|
|
207 |
helper.setFrom(senderAddress);
|
|
|
208 |
mailSender.send(message);
|
|
|
209 |
}
|
|
|
210 |
|
| 34307 |
ranu |
211 |
public Map<Integer,Integer> findLoanTransactionMapingAccordingLoan(List<Integer> loanIds) throws ProfitMandiBusinessException {
|
| 34306 |
ranu |
212 |
|
|
|
213 |
Map<Integer, Integer> transactionLoanMap = new HashMap<>();
|
|
|
214 |
|
|
|
215 |
for(int loanId : loanIds){
|
|
|
216 |
Transaction transaction = null;
|
|
|
217 |
Loan loan = loanRepository.selectByLoanId(loanId);
|
|
|
218 |
List<Transaction> transactions = transactionRepository.selectByRetailerId(loan.getFofoId());
|
|
|
219 |
|
|
|
220 |
LocalDateTime nearestDateTime = transactions.stream().map(x -> x.getCreateTimestamp())
|
|
|
221 |
.min(Comparator.comparingLong(x -> Math.abs(ChronoUnit.MILLIS.between(x, loan.getCreatedOn()))))
|
|
|
222 |
.orElse(null);
|
|
|
223 |
|
|
|
224 |
if (nearestDateTime != null && loan.getCreatedOn().plusMinutes(2).isAfter(nearestDateTime) &&
|
|
|
225 |
loan.getCreatedOn().minusMinutes(1).isBefore(nearestDateTime)) {
|
|
|
226 |
// Here transaction is still null
|
|
|
227 |
transaction = transactions.stream()
|
|
|
228 |
.filter(x -> x.getCreateTimestamp().equals(nearestDateTime))
|
|
|
229 |
.findFirst().get();
|
|
|
230 |
transactionLoanMap.put(transaction.getId(), loanId);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
}
|
|
|
234 |
LOGGER.info("transactionLoanMap {}",transactionLoanMap);
|
|
|
235 |
return transactionLoanMap;
|
|
|
236 |
}
|
| 34321 |
ranu |
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
public void sendRbmFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
241 |
LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
242 |
LocalDateTime endOfMonth = LocalDateTime.now();
|
| 34323 |
ranu |
243 |
String[] bcc = {"tarun.verma@smartdukaan.com"};
|
| 34321 |
ranu |
244 |
|
|
|
245 |
// Get all RBM users
|
|
|
246 |
List<AuthUser> authUsers = csService.getAuthUserIds(
|
|
|
247 |
ProfitMandiConstants.TICKET_CATEGORY_RBM,
|
|
|
248 |
Arrays.asList(EscalationType.L1)
|
|
|
249 |
);
|
|
|
250 |
|
|
|
251 |
if (authUsers.isEmpty()) {
|
|
|
252 |
LOGGER.info("No RBMs found.");
|
|
|
253 |
return;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
List<Integer> rbmIds = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
|
|
|
257 |
|
|
|
258 |
// Fetch ratings for all RBMs for current month
|
|
|
259 |
List<RbmRating> feedbackList = rbmRatingRepository.selectByRbmIdsAndDateRange(rbmIds, startOfMonth, endOfMonth);
|
|
|
260 |
|
|
|
261 |
if (feedbackList.isEmpty()) {
|
|
|
262 |
LOGGER.info("No feedback entries found for RBMs.");
|
|
|
263 |
return;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
// Sort feedback by createTimeStamp DESC
|
|
|
267 |
feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
|
|
|
268 |
|
|
|
269 |
// Fetch and map FOFO (partner) names
|
|
|
270 |
Map<Integer, String> fofoNameMap = new HashMap<>();
|
|
|
271 |
for (RbmRating rating : feedbackList) {
|
|
|
272 |
int fofoId = rating.getFofoId();
|
|
|
273 |
if (!fofoNameMap.containsKey(fofoId)) {
|
|
|
274 |
User fofoUser = userRepository.selectById(fofoId);
|
|
|
275 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
276 |
|
|
|
277 |
String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
|
|
|
278 |
String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
|
|
|
279 |
|
|
|
280 |
String displayName = partnerName + " (" + storeCode + ")";
|
|
|
281 |
fofoNameMap.put(fofoId, displayName);
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
// Map RBM ID to name for quick lookup
|
|
|
286 |
Map<Integer, String> rbmNameMap = authUsers.stream()
|
|
|
287 |
.collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
|
|
|
288 |
|
|
|
289 |
// Generate HTML content
|
|
|
290 |
StringBuilder emailContent = new StringBuilder();
|
|
|
291 |
emailContent.append("<html><body>");
|
|
|
292 |
emailContent.append("<p>Dear Team,</p>");
|
|
|
293 |
emailContent.append("<p>Here is the <b>latest RBM Rating and Feedback Summary</b> for ")
|
|
|
294 |
.append(LocalDate.now().getMonth()).append(":</p>");
|
|
|
295 |
|
|
|
296 |
emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
|
|
|
297 |
emailContent.append("<tr>")
|
|
|
298 |
.append("<th>RBM Name</th>")
|
|
|
299 |
.append("<th>Partner Name</th>")
|
|
|
300 |
.append("<th>Rating</th>")
|
|
|
301 |
.append("<th>Comment</th>")
|
|
|
302 |
.append("<th>Date</th>")
|
|
|
303 |
.append("</tr>");
|
|
|
304 |
|
|
|
305 |
for (RbmRating rating : feedbackList) {
|
|
|
306 |
String rbmName = rbmNameMap.getOrDefault(rating.getRbmId(), "Unknown RBM");
|
|
|
307 |
String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
|
|
|
308 |
emailContent.append("<tr>")
|
|
|
309 |
.append("<td>").append(rbmName).append("</td>")
|
|
|
310 |
.append("<td>").append(partnerName).append("</td>")
|
|
|
311 |
.append("<td>").append(rating.getRating()).append("</td>")
|
|
|
312 |
.append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
|
|
|
313 |
.append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
|
|
|
314 |
.append("</tr>");
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
emailContent.append("</table>");
|
|
|
318 |
emailContent.append("<br><p>Regards,<br>Smart Dukaan Team</p>");
|
|
|
319 |
emailContent.append("</body></html>");
|
|
|
320 |
|
|
|
321 |
String subject = "Monthly RBM Feedback Summary - " + LocalDate.now().getMonth();
|
|
|
322 |
|
|
|
323 |
List<String> sendTo = new ArrayList<>();
|
| 34323 |
ranu |
324 |
sendTo.add("sm@smartdukaan.com"); //
|
|
|
325 |
sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
|
|
|
326 |
sendTo.add("kamini.sharma@smartdukaan.com"); //
|
| 34321 |
ranu |
327 |
|
|
|
328 |
String[] emailRecipients = sendTo.toArray(new String[0]);
|
|
|
329 |
|
|
|
330 |
|
|
|
331 |
this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
|
|
|
332 |
|
|
|
333 |
LOGGER.info("Consolidated RBM feedback summary email sent.");
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
public void sendSalesFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
338 |
LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
339 |
LocalDateTime endOfMonth = LocalDateTime.now();
|
| 34323 |
ranu |
340 |
String[] bcc = {"tarun.verma@smartdukaan.com"};
|
| 34411 |
tejus.loha |
341 |
// String[] bcc = {"tejus.lohani@smartdukaan.com"};
|
| 34321 |
ranu |
342 |
|
|
|
343 |
// Get all RBM users
|
|
|
344 |
List<AuthUser> authUsers = csService.getAuthUserIds(
|
|
|
345 |
ProfitMandiConstants.TICKET_CATEGORY_SALES,
|
|
|
346 |
Arrays.asList(EscalationType.L1)
|
|
|
347 |
);
|
|
|
348 |
|
|
|
349 |
if (authUsers.isEmpty()) {
|
|
|
350 |
LOGGER.info("No sales person found.");
|
|
|
351 |
return;
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
List<Integer> salesL1Ids = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
|
|
|
355 |
|
|
|
356 |
// Fetch ratings for all RBMs for current month
|
|
|
357 |
List<SalesRating> feedbackList = salesRatingRepository.selectBySalesL1IdsAndDateRange(salesL1Ids, startOfMonth, endOfMonth);
|
|
|
358 |
|
|
|
359 |
if (feedbackList.isEmpty()) {
|
|
|
360 |
LOGGER.info("No feedback entries found for Sales.");
|
|
|
361 |
return;
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
// Sort feedback by createTimeStamp DESC
|
|
|
365 |
feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
|
|
|
366 |
|
|
|
367 |
// Fetch and map FOFO (partner) names
|
|
|
368 |
Map<Integer, String> fofoNameMap = new HashMap<>();
|
|
|
369 |
for (SalesRating rating : feedbackList) {
|
|
|
370 |
int fofoId = rating.getFofoId();
|
|
|
371 |
if (!fofoNameMap.containsKey(fofoId)) {
|
|
|
372 |
User fofoUser = userRepository.selectById(fofoId);
|
|
|
373 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
374 |
|
|
|
375 |
String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
|
|
|
376 |
String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
|
|
|
377 |
|
|
|
378 |
String displayName = partnerName + " (" + storeCode + ")";
|
|
|
379 |
fofoNameMap.put(fofoId, displayName);
|
|
|
380 |
}
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
// Map RBM ID to name for quick lookup
|
|
|
384 |
Map<Integer, String> salesL1NameMap = authUsers.stream()
|
|
|
385 |
.collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
|
|
|
386 |
|
|
|
387 |
// Generate HTML content
|
|
|
388 |
StringBuilder emailContent = new StringBuilder();
|
|
|
389 |
emailContent.append("<html><body>");
|
|
|
390 |
emailContent.append("<p>Dear Team,</p>");
|
|
|
391 |
emailContent.append("<p>Here is the <b>latest Sales L1 Rating and Feedback Summary</b> for ")
|
|
|
392 |
.append(LocalDate.now().getMonth()).append(":</p>");
|
|
|
393 |
|
|
|
394 |
emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
|
|
|
395 |
emailContent.append("<tr>")
|
|
|
396 |
.append("<th>Sales L1 Name</th>")
|
|
|
397 |
.append("<th>Partner Name</th>")
|
| 34411 |
tejus.loha |
398 |
.append("<th>Partner Category</th>")
|
| 34321 |
ranu |
399 |
.append("<th>Rating</th>")
|
|
|
400 |
.append("<th>Comment</th>")
|
|
|
401 |
.append("<th>Date</th>")
|
|
|
402 |
.append("</tr>");
|
|
|
403 |
|
|
|
404 |
for (SalesRating rating : feedbackList) {
|
|
|
405 |
String salesL1 = salesL1NameMap.getOrDefault(rating.getSalesL1Id(), "Unknown Sales Person");
|
|
|
406 |
String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
|
| 34411 |
tejus.loha |
407 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(rating.getFofoId(), LocalDate.now());
|
| 34321 |
ranu |
408 |
emailContent.append("<tr>")
|
|
|
409 |
.append("<td>").append(salesL1).append("</td>")
|
|
|
410 |
.append("<td>").append(partnerName).append("</td>")
|
| 34411 |
tejus.loha |
411 |
.append("<td>").append(partnerType).append("</td>")
|
| 34321 |
ranu |
412 |
.append("<td>").append(rating.getRating()).append("</td>")
|
|
|
413 |
.append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
|
|
|
414 |
.append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
|
|
|
415 |
.append("</tr>");
|
|
|
416 |
}
|
|
|
417 |
|
|
|
418 |
emailContent.append("</table>");
|
|
|
419 |
emailContent.append("<br><p>Regards,<br>Smartdukaan Team</p>");
|
|
|
420 |
emailContent.append("</body></html>");
|
|
|
421 |
|
| 34411 |
tejus.loha |
422 |
String subject = "Monthly Sales L1 Feedback Summary Test test - " + LocalDate.now().getMonth();
|
| 34321 |
ranu |
423 |
|
|
|
424 |
List<String> sendTo = new ArrayList<>();
|
| 34323 |
ranu |
425 |
sendTo.add("sm@smartdukaan.com"); //
|
|
|
426 |
sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
|
| 34606 |
ranu |
427 |
sendTo.add("kamini.sharma@smartdukaan.com"); //
|
| 34321 |
ranu |
428 |
|
|
|
429 |
String[] emailRecipients = sendTo.toArray(new String[0]);
|
|
|
430 |
|
|
|
431 |
|
|
|
432 |
this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
|
|
|
433 |
|
|
|
434 |
LOGGER.info("Consolidated Sales L1 feedback summary email sent.");
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
|
| 34606 |
ranu |
438 |
public void generateBiReportExcel() throws Exception {
|
| 34619 |
ranu |
439 |
|
| 34606 |
ranu |
440 |
LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
|
| 34321 |
ranu |
441 |
|
| 34619 |
ranu |
442 |
Map<Integer, CustomRetailer> customRetailers = retailerService.getAllFofoRetailersInternalFalse();
|
| 34321 |
ranu |
443 |
|
| 34606 |
ranu |
444 |
List<Integer> retailerIds = customRetailers.values().stream().map(CustomRetailer::getPartnerId).collect(Collectors.toList());
|
|
|
445 |
|
| 34619 |
ranu |
446 |
|
|
|
447 |
// this month return data
|
|
|
448 |
YearMonth currentMonth = YearMonth.now();
|
|
|
449 |
String currentMonthStringValue = String.valueOf(currentMonth);
|
| 34606 |
ranu |
450 |
LocalDateTime currentMonthStartDate = YearMonth.now().atDay(1).atStartOfDay();
|
|
|
451 |
LocalDateTime currentMonthEndDate = LocalDateTime.now();
|
|
|
452 |
|
|
|
453 |
List<ReturnOrderInfoModel> currentMonthReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(currentMonthStartDate, currentMonthEndDate);
|
|
|
454 |
Map<Integer, Long> currentMonthPartnerReturnOrderInfoModelMap = currentMonthReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
|
|
|
455 |
|
|
|
456 |
List<Order> currentMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(currentMonthStartDate, currentMonthEndDate, OrderStatus.RTO_REFUNDED);
|
|
|
457 |
Map<Integer, Long> currentMonthRtoRefundOrderMap = currentMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
|
|
|
458 |
|
|
|
459 |
|
| 34619 |
ranu |
460 |
// last month return data
|
| 34606 |
ranu |
461 |
YearMonth lastMonth = YearMonth.now().minusMonths(1);
|
| 34619 |
ranu |
462 |
String lastMonthStringValue = String.valueOf(lastMonth);
|
| 34606 |
ranu |
463 |
LocalDateTime lastMontStartDate = lastMonth.atDay(1).atStartOfDay();
|
|
|
464 |
LocalDateTime lastMonthEndDate = lastMonth.atEndOfMonth().atTime(23, 59, 59);
|
|
|
465 |
|
|
|
466 |
List<ReturnOrderInfoModel> lastMonthReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(lastMontStartDate, lastMonthEndDate);
|
|
|
467 |
Map<Integer, Long> lastMonthPartnerReturnOrderInfoModelMap = lastMonthReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
|
|
|
468 |
|
|
|
469 |
List<Order> lastMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(lastMontStartDate, lastMonthEndDate, OrderStatus.RTO_REFUNDED);
|
|
|
470 |
Map<Integer, Long> lastMonthRtoRefundOrderMap = lastMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
|
|
|
471 |
|
|
|
472 |
|
| 34619 |
ranu |
473 |
// twoMonthsAgo return data
|
| 34606 |
ranu |
474 |
YearMonth twoMonthsAgo = YearMonth.now().minusMonths(2);
|
| 34619 |
ranu |
475 |
String twoMonthAgoStringValue = String.valueOf(twoMonthsAgo);
|
| 34606 |
ranu |
476 |
LocalDateTime twoMonthsAgoStartDate = twoMonthsAgo.atDay(1).atStartOfDay();
|
|
|
477 |
LocalDateTime twoMonthsAgoEndDate = twoMonthsAgo.atEndOfMonth().atTime(23, 59, 59);
|
|
|
478 |
|
|
|
479 |
List<ReturnOrderInfoModel> twoMonthAgoReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(twoMonthsAgoStartDate, twoMonthsAgoEndDate);
|
|
|
480 |
Map<Integer, Long> twoMonthAgoPartnerReturnOrderInfoModelMap = twoMonthAgoReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
|
|
|
481 |
|
|
|
482 |
List<Order> twoMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(twoMonthsAgoStartDate, twoMonthsAgoEndDate, OrderStatus.RTO_REFUNDED);
|
| 34619 |
ranu |
483 |
Map<Integer, Long> twoMonthAgoRtoRefundOrderMap = twoMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
|
| 34606 |
ranu |
484 |
|
|
|
485 |
|
| 34619 |
ranu |
486 |
Map<Integer , BIRetailerModel> biRetailerModelMap = new HashMap<>();
|
| 34606 |
ranu |
487 |
|
| 34619 |
ranu |
488 |
Map<Integer, Map<YearMonth, BiSecondaryModel>> allRetailerMonthlyData = new HashMap<>();
|
| 34606 |
ranu |
489 |
|
| 34619 |
ranu |
490 |
Map<Integer,Double> fofoTotalStockPriceMap = new HashMap<>();
|
|
|
491 |
|
|
|
492 |
Map<Integer,Map<String, BrandStockPrice>> fofoBrandStockPriceMap = new HashMap<>();
|
|
|
493 |
|
|
|
494 |
|
|
|
495 |
Map<Integer,Double> fofoTotalTertiaryMap = new HashMap<>();
|
|
|
496 |
|
|
|
497 |
Map<Integer,Map<String, Double>> fofoBrandTertiaryMap = new HashMap<>();
|
|
|
498 |
|
| 34606 |
ranu |
499 |
for(Integer fofoId: retailerIds){
|
| 34619 |
ranu |
500 |
String rbmName = "";
|
| 34606 |
ranu |
501 |
int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM,EscalationType.L1,fofoId);
|
| 34619 |
ranu |
502 |
if(rbmL1 != 0){
|
|
|
503 |
rbmName = authRepository.selectById(rbmL1).getFullName();
|
|
|
504 |
}
|
|
|
505 |
String bmName ="";
|
| 34606 |
ranu |
506 |
int bmId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES,EscalationType.L2,fofoId);
|
| 34619 |
ranu |
507 |
if(bmId !=0){
|
|
|
508 |
bmName = authRepository.selectById(bmId).getFullName();
|
|
|
509 |
}
|
| 34606 |
ranu |
510 |
|
|
|
511 |
int managerId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES,EscalationType.L1,fofoId);
|
|
|
512 |
String managerName = " ";
|
|
|
513 |
if(managerId != 0){
|
|
|
514 |
managerName = authRepository.selectById(managerId).getFullName();
|
|
|
515 |
}else {
|
|
|
516 |
managerName = bmName;
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
AST ast = astRepository.selectById(customRetailers.get(fofoId).getAstId());
|
|
|
520 |
|
|
|
521 |
PartnerType partnerTypeThisMonth = partnerTypeChangeService.getTypeOnMonth(fofoId, YearMonth.now());
|
|
|
522 |
|
|
|
523 |
// generate retaile detail
|
|
|
524 |
|
|
|
525 |
BIRetailerModel biRetailerModel = new BIRetailerModel();
|
|
|
526 |
biRetailerModel.setBmName(bmName);
|
|
|
527 |
biRetailerModel.setCode(customRetailers.get(fofoId).getCode());
|
| 34619 |
ranu |
528 |
if(ast != null){
|
|
|
529 |
biRetailerModel.setArea(ast.getArea());
|
|
|
530 |
}else {
|
|
|
531 |
biRetailerModel.setArea("-");
|
|
|
532 |
}
|
| 34606 |
ranu |
533 |
biRetailerModel.setCity(customRetailers.get(fofoId).getAddress().getCity());
|
|
|
534 |
biRetailerModel.setStoreName(customRetailers.get(fofoId).getBusinessName());
|
| 34619 |
ranu |
535 |
biRetailerModel.setStatus(String.valueOf(customRetailers.get(fofoId).getActivationType()));
|
| 34606 |
ranu |
536 |
biRetailerModel.setCategory(String.valueOf(partnerTypeThisMonth));
|
|
|
537 |
biRetailerModel.setSalesManager(managerName);
|
|
|
538 |
biRetailerModel.setRbm(rbmName);
|
|
|
539 |
|
| 34619 |
ranu |
540 |
biRetailerModelMap.put(fofoId,biRetailerModel);
|
|
|
541 |
|
| 34606 |
ranu |
542 |
// generate secondary data
|
|
|
543 |
|
|
|
544 |
// this month secondary target
|
|
|
545 |
|
| 34619 |
ranu |
546 |
double currentSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(YearMonth.now(), fofoId) != null ? monthlyTargetRepository.selectByDateAndFofoId(YearMonth.now(), fofoId).getPurchaseTarget() : 0;
|
| 34606 |
ranu |
547 |
|
| 34619 |
ranu |
548 |
|
|
|
549 |
long currentMonthReturn = currentMonthPartnerReturnOrderInfoModelMap.getOrDefault(fofoId, 0L) + currentMonthRtoRefundOrderMap.getOrDefault(fofoId, 0L);
|
|
|
550 |
|
|
|
551 |
|
| 34606 |
ranu |
552 |
Map<Integer, Double> secondaryMtd = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
|
|
|
553 |
Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
|
|
|
554 |
startOfToday.withDayOfMonth(1), startOfToday.with(LocalTime.MAX)).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
|
|
|
555 |
|
|
|
556 |
double secondaryAchievedMtd = secondaryMtd.getOrDefault(fofoId, 0.0);
|
|
|
557 |
|
| 34619 |
ranu |
558 |
double currentMonthNetSecondary = secondaryAchievedMtd - currentMonthReturn;
|
| 34606 |
ranu |
559 |
|
| 34619 |
ranu |
560 |
double currentMonthSecondaryPercent = currentSecondaryTarget == 0 ? 0.0 : (secondaryAchievedMtd / currentSecondaryTarget) * 100;
|
|
|
561 |
|
|
|
562 |
double currentMonthUnbilled = 0;
|
|
|
563 |
|
|
|
564 |
// this month tertiary----------
|
|
|
565 |
|
|
|
566 |
LocalDateTime now = LocalDateTime.now();
|
|
|
567 |
double todaySale = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday, now, fofoId, false).get(fofoId);
|
|
|
568 |
double mtdSaleTillYesterDay = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday.withDayOfMonth(1), startOfToday, fofoId, false).get(fofoId);
|
|
|
569 |
double mtdSale = mtdSaleTillYesterDay + todaySale;
|
|
|
570 |
|
|
|
571 |
|
| 34606 |
ranu |
572 |
// last month secondary target
|
|
|
573 |
|
| 34619 |
ranu |
574 |
double lastMonthSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId) != null ? monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId).getPurchaseTarget() : 0;
|
| 34606 |
ranu |
575 |
|
| 34619 |
ranu |
576 |
long lastMonthReturn = (lastMonthPartnerReturnOrderInfoModelMap.getOrDefault(fofoId,0L) + lastMonthRtoRefundOrderMap.getOrDefault(fofoId,0L));
|
|
|
577 |
|
| 34606 |
ranu |
578 |
Map<Integer, Double> lastMonthSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
|
|
|
579 |
Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
|
|
|
580 |
lastMontStartDate, lastMonthEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
|
|
|
581 |
|
|
|
582 |
double lastMonthSecondaryAchieved = lastMonthSecondary.getOrDefault(fofoId, 0.0);
|
|
|
583 |
|
| 34619 |
ranu |
584 |
double lastMonthNetSecondary = lastMonthSecondaryAchieved - lastMonthReturn;
|
| 34606 |
ranu |
585 |
|
| 34619 |
ranu |
586 |
double lastMonthSecondaryPercent = lastMonthSecondaryTarget == 0 ? 0.0 : (lastMonthSecondaryAchieved / lastMonthSecondaryTarget) * 100;
|
| 34606 |
ranu |
587 |
|
| 34619 |
ranu |
588 |
double lastMonthUnbilled = 0;
|
| 34606 |
ranu |
589 |
|
| 34619 |
ranu |
590 |
// last month tertiary
|
|
|
591 |
Double lastMonthSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
|
|
592 |
lastMontStartDate, lastMonthEndDate, fofoId, false).get(fofoId);
|
|
|
593 |
|
|
|
594 |
|
|
|
595 |
// two month ago secondary target
|
|
|
596 |
|
|
|
597 |
double twoMonthAgoSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId) != null ? monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId).getPurchaseTarget() : 0;
|
|
|
598 |
|
|
|
599 |
long twoMonthAgoReturn = (twoMonthAgoPartnerReturnOrderInfoModelMap.getOrDefault(fofoId,0L) + twoMonthAgoRtoRefundOrderMap.getOrDefault(fofoId,0L));
|
|
|
600 |
|
| 34606 |
ranu |
601 |
Map<Integer, Double> twoMonthAgoSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
|
|
|
602 |
Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
|
|
|
603 |
twoMonthsAgoStartDate, twoMonthsAgoEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
|
|
|
604 |
|
|
|
605 |
double twoMonthAgoSecondaryAchieved = twoMonthAgoSecondary.getOrDefault(fofoId, 0.0);
|
|
|
606 |
|
| 34619 |
ranu |
607 |
double twoMonthAgoNetSecondary = twoMonthAgoSecondaryAchieved - twoMonthAgoReturn;
|
| 34606 |
ranu |
608 |
|
| 34619 |
ranu |
609 |
double twoMonthAgoSecondaryPercent = twoMonthAgoSecondaryTarget == 0 ? 0.0 : (twoMonthAgoSecondaryAchieved / twoMonthAgoSecondaryTarget) * 100;
|
|
|
610 |
|
|
|
611 |
double twoMonthAgoUnbilled = 0;
|
|
|
612 |
|
|
|
613 |
// second Month Tertiary
|
|
|
614 |
double twoMonthAgoSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
|
|
615 |
twoMonthsAgoStartDate, twoMonthsAgoEndDate, fofoId, false).get(fofoId);
|
|
|
616 |
|
|
|
617 |
|
|
|
618 |
Map<YearMonth, BiSecondaryModel> monthlySecondaryModels = new HashMap<>();
|
|
|
619 |
|
|
|
620 |
BiSecondaryModel currentMonthSecondaryModel = new BiSecondaryModel(
|
|
|
621 |
currentSecondaryTarget,
|
|
|
622 |
secondaryAchievedMtd,
|
|
|
623 |
currentMonthReturn,
|
|
|
624 |
currentMonthNetSecondary,
|
|
|
625 |
currentMonthSecondaryPercent,
|
|
|
626 |
mtdSale,
|
|
|
627 |
currentMonthUnbilled // for now, unbilled tertiary value
|
|
|
628 |
);
|
|
|
629 |
|
|
|
630 |
BiSecondaryModel lastMonthSecondaryModel = new BiSecondaryModel(
|
|
|
631 |
lastMonthSecondaryTarget,
|
|
|
632 |
lastMonthSecondaryAchieved,
|
|
|
633 |
lastMonthReturn,
|
|
|
634 |
lastMonthNetSecondary,
|
|
|
635 |
lastMonthSecondaryPercent,
|
|
|
636 |
lastMonthSale,
|
|
|
637 |
lastMonthUnbilled // for now, unbilled tertiary value
|
|
|
638 |
);
|
|
|
639 |
|
|
|
640 |
BiSecondaryModel twoMonthAgoSecondaryModel = new BiSecondaryModel(
|
|
|
641 |
twoMonthAgoSecondaryTarget,
|
|
|
642 |
twoMonthAgoSecondaryAchieved,
|
|
|
643 |
twoMonthAgoReturn,
|
|
|
644 |
twoMonthAgoNetSecondary,
|
|
|
645 |
twoMonthAgoSecondaryPercent,
|
|
|
646 |
twoMonthAgoSale,
|
|
|
647 |
twoMonthAgoUnbilled // for now, unbilled tertiary value
|
|
|
648 |
);
|
|
|
649 |
|
|
|
650 |
monthlySecondaryModels.put(currentMonth, currentMonthSecondaryModel);
|
|
|
651 |
monthlySecondaryModels.put(lastMonth, lastMonthSecondaryModel);
|
|
|
652 |
monthlySecondaryModels.put(twoMonthsAgo, twoMonthAgoSecondaryModel);
|
|
|
653 |
|
|
|
654 |
allRetailerMonthlyData.put(fofoId, monthlySecondaryModels);
|
|
|
655 |
|
|
|
656 |
// brandwiseStock value price
|
|
|
657 |
|
|
|
658 |
Map<String, BrandStockPrice> brandStockPriceMap = inventoryService.getBrandWiseStockValue(fofoId);
|
|
|
659 |
|
|
|
660 |
fofoBrandStockPriceMap.put(fofoId,brandStockPriceMap);
|
|
|
661 |
|
|
|
662 |
double totalStockPrice = brandStockPriceMap.values().stream().mapToDouble(x->x.getTotalValue()).sum();
|
|
|
663 |
|
|
|
664 |
fofoTotalStockPriceMap.put(fofoId,totalStockPrice);
|
|
|
665 |
|
|
|
666 |
Map<String, Double> brandMtdTertiaryAmount = fofoOrderItemRepository.selectSumAmountGroupByBrand(
|
|
|
667 |
currentMonthStartDate, currentMonthEndDate, fofoId);
|
|
|
668 |
|
|
|
669 |
fofoBrandTertiaryMap.put(fofoId,brandMtdTertiaryAmount);
|
|
|
670 |
|
|
|
671 |
double totalMtdTertiaryAmount = brandMtdTertiaryAmount.values().stream().mapToDouble(Double::doubleValue).sum();
|
|
|
672 |
|
|
|
673 |
fofoTotalTertiaryMap.put(fofoId,totalMtdTertiaryAmount);
|
|
|
674 |
|
|
|
675 |
// List<BrandWiseModel> brandMtdSecondary = orderRepository.selectAllBilledOrderGroupByBrandFofoId(fofoId, curDate.withDayOfMonth(1).minusMonths(6));
|
|
|
676 |
|
|
|
677 |
|
| 34606 |
ranu |
678 |
}
|
|
|
679 |
|
| 34619 |
ranu |
680 |
LOGGER.info("Total BI Retailers processed: {}", biRetailerModelMap.size());
|
| 34606 |
ranu |
681 |
|
| 34619 |
ranu |
682 |
//generate excel and sent to mail
|
|
|
683 |
List<List<String>> headerGroup = new ArrayList<>();
|
| 34606 |
ranu |
684 |
|
| 34619 |
ranu |
685 |
List<String> headers1 = Arrays.asList(
|
|
|
686 |
"Retailer Detail", "", "", "", "", "", "", "", "", "",
|
|
|
687 |
currentMonthStringValue, "", "", "", "", "", "",
|
|
|
688 |
lastMonthStringValue, "", "", "", "", "", "",
|
|
|
689 |
twoMonthAgoStringValue, "", "", "", "", "", ""
|
|
|
690 |
);
|
| 34606 |
ranu |
691 |
|
| 34619 |
ranu |
692 |
List<String> headers2 = Arrays.asList(
|
|
|
693 |
"Code", "Store Name", "City", "Area", "BM", "Sales Manager", "RBM", "Status", "Category",
|
|
|
694 |
"Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
|
|
|
695 |
"Tertiary Sale", "Unbilled",
|
|
|
696 |
"Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
|
|
|
697 |
"Tertiary Sale", "Unbilled",
|
|
|
698 |
"Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
|
|
|
699 |
"Tertiary Sale", "Unbilled",
|
| 34606 |
ranu |
700 |
|
| 34619 |
ranu |
701 |
"Total Stock", "Apple","Xiaomi","Vivo","Tecno","Samsung","Realme","Oppo","OnePlus","Poco","Lava","Itel","Almost New"
|
|
|
702 |
);
|
|
|
703 |
|
|
|
704 |
headerGroup.add(headers1);
|
|
|
705 |
headerGroup.add(headers2);
|
|
|
706 |
|
|
|
707 |
|
|
|
708 |
List<List<?>> rows = new ArrayList<>();
|
|
|
709 |
for (Map.Entry<Integer, BIRetailerModel> entry : biRetailerModelMap.entrySet()) {
|
|
|
710 |
Integer fofoId = entry.getKey();
|
|
|
711 |
BIRetailerModel retailer = entry.getValue();
|
|
|
712 |
Map<YearMonth, BiSecondaryModel> monthlyData = allRetailerMonthlyData.get(fofoId);
|
|
|
713 |
|
|
|
714 |
BiSecondaryModel current = monthlyData.getOrDefault(YearMonth.now(), new BiSecondaryModel(0,0,0,0,0,0,0));
|
|
|
715 |
BiSecondaryModel last = monthlyData.getOrDefault(YearMonth.now().minusMonths(1), new BiSecondaryModel(0,0,0,0,0,0,0));
|
|
|
716 |
BiSecondaryModel twoAgo = monthlyData.getOrDefault(YearMonth.now().minusMonths(2), new BiSecondaryModel(0,0,0,0,0,0,0));
|
|
|
717 |
|
|
|
718 |
List<Object> row = new ArrayList<>();
|
|
|
719 |
row.addAll(Arrays.asList(
|
|
|
720 |
retailer.getCode(), retailer.getStoreName(), retailer.getCity(), retailer.getArea(),
|
|
|
721 |
retailer.getBmName(), retailer.getSalesManager(), retailer.getRbm(),
|
|
|
722 |
retailer.getStatus(), retailer.getCategory()
|
|
|
723 |
));
|
|
|
724 |
|
|
|
725 |
// Current Month
|
|
|
726 |
row.addAll(Arrays.asList(
|
|
|
727 |
current.getSecondaryTarget(),
|
|
|
728 |
current.getSecondaryAchieved(),
|
|
|
729 |
current.getSecondaryReturn(),
|
|
|
730 |
current.getNetSecondary(),
|
|
|
731 |
current.getSecondaryAchievedPercent(),
|
|
|
732 |
current.getTertiary(),
|
|
|
733 |
current.getTertiaryUnBilled()
|
|
|
734 |
));
|
|
|
735 |
|
|
|
736 |
// Last Month
|
|
|
737 |
row.addAll(Arrays.asList(
|
|
|
738 |
last.getSecondaryTarget(),
|
|
|
739 |
last.getSecondaryAchieved(),
|
|
|
740 |
last.getSecondaryReturn(),
|
|
|
741 |
last.getNetSecondary(),
|
|
|
742 |
last.getSecondaryAchievedPercent(),
|
|
|
743 |
last.getTertiary(),
|
|
|
744 |
last.getTertiaryUnBilled()
|
|
|
745 |
));
|
|
|
746 |
|
|
|
747 |
// Two Months Ago
|
|
|
748 |
row.addAll(Arrays.asList(
|
|
|
749 |
twoAgo.getSecondaryTarget(),
|
|
|
750 |
twoAgo.getSecondaryAchieved(),
|
|
|
751 |
twoAgo.getSecondaryReturn(),
|
|
|
752 |
twoAgo.getNetSecondary(),
|
|
|
753 |
twoAgo.getSecondaryAchievedPercent(),
|
|
|
754 |
twoAgo.getTertiary(),
|
|
|
755 |
twoAgo.getTertiaryUnBilled()
|
|
|
756 |
));
|
|
|
757 |
Map<String, BrandStockPrice> brandMap = fofoBrandStockPriceMap.get(fofoId);
|
|
|
758 |
row.addAll(Arrays.asList(
|
|
|
759 |
fofoTotalStockPriceMap.getOrDefault(fofoId, 0.0),
|
|
|
760 |
brandMap.get("Apple") != null ? brandMap.get("Apple").getTotalValue() : 0.0,
|
|
|
761 |
brandMap.get("Xiaomi") != null ? brandMap.get("Xiaomi").getTotalValue() : 0.0,
|
|
|
762 |
brandMap.get("Vivo") != null ? brandMap.get("Vivo").getTotalValue() : 0.0,
|
|
|
763 |
brandMap.get("Tecno") != null ? brandMap.get("Tecno").getTotalValue() : 0.0,
|
|
|
764 |
brandMap.get("Samsung") != null ? brandMap.get("Samsung").getTotalValue() : 0.0,
|
|
|
765 |
brandMap.get("Realme") != null ? brandMap.get("Realme").getTotalValue() : 0.0,
|
|
|
766 |
brandMap.get("Oppo") != null ? brandMap.get("Oppo").getTotalValue() : 0.0,
|
|
|
767 |
brandMap.get("OnePlus") != null ? brandMap.get("OnePlus").getTotalValue() : 0.0,
|
|
|
768 |
brandMap.get("Poco") != null ? brandMap.get("Poco").getTotalValue() : 0.0,
|
|
|
769 |
brandMap.get("Lava") != null ? brandMap.get("Lava").getTotalValue() : 0.0,
|
|
|
770 |
brandMap.get("Itel") != null ? brandMap.get("Itel").getTotalValue() : 0.0,
|
|
|
771 |
brandMap.get("Almost New") != null ? brandMap.get("Almost New").getTotalValue() : 0.0
|
|
|
772 |
));
|
|
|
773 |
|
|
|
774 |
rows.add(row);
|
|
|
775 |
}
|
|
|
776 |
|
|
|
777 |
|
|
|
778 |
// Send to email
|
|
|
779 |
ByteArrayOutputStream csvStream = FileUtil.getCSVByteStreamWithMultiHeaders(headerGroup, rows);
|
|
|
780 |
String fileName = "BI-Retailer-Monthly-Report-" + FormattingUtils.formatDate(LocalDateTime.now()) + ".csv";
|
|
|
781 |
String[] sendToArray = new String[]{"ranu.rajput@smartdukaan.com"};
|
|
|
782 |
|
|
|
783 |
Utils.sendMailWithAttachment(googleMailSender, sendToArray, new String[]{}, "BI Retailer Monthly Report", "Please find attached the BI retailer secondary/tertiary monthly report.", fileName, new ByteArrayResource(csvStream.toByteArray()));
|
|
|
784 |
|
|
|
785 |
|
| 34606 |
ranu |
786 |
}
|
|
|
787 |
|
|
|
788 |
|
|
|
789 |
|
|
|
790 |
|
| 34619 |
ranu |
791 |
|
| 34306 |
ranu |
792 |
}
|