| 34240 |
tejus.loha |
1 |
package com.spice.profitmandi.dao.service;
|
|
|
2 |
|
| 34275 |
tejus.loha |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34979 |
ranu |
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 35102 |
amit |
5 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
6 |
import com.spice.profitmandi.common.util.Utils;
|
| 34275 |
tejus.loha |
7 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 34979 |
ranu |
8 |
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
|
| 35050 |
amit |
9 |
import com.spice.profitmandi.dao.model.WarehouseItemAgeingModel;
|
| 34240 |
tejus.loha |
10 |
import com.spice.profitmandi.dao.model.purchaseSaleRatio.CatalogAgedModel;
|
| 34275 |
tejus.loha |
11 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 34979 |
ranu |
12 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
| 34240 |
tejus.loha |
13 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseSupplierInvoiceRepository;
|
| 34275 |
tejus.loha |
14 |
import com.spice.profitmandi.service.AuthService;
|
| 34249 |
tejus.loha |
15 |
import org.apache.logging.log4j.LogManager;
|
|
|
16 |
import org.apache.logging.log4j.Logger;
|
| 34240 |
tejus.loha |
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 35102 |
amit |
18 |
import org.springframework.core.io.ByteArrayResource;
|
| 34240 |
tejus.loha |
19 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
20 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
21 |
import org.springframework.stereotype.Service;
|
|
|
22 |
|
|
|
23 |
import javax.mail.MessagingException;
|
|
|
24 |
import javax.mail.internet.InternetAddress;
|
|
|
25 |
import javax.mail.internet.MimeMessage;
|
|
|
26 |
import java.io.UnsupportedEncodingException;
|
|
|
27 |
import java.time.LocalDateTime;
|
| 35050 |
amit |
28 |
import java.util.ArrayList;
|
|
|
29 |
import java.util.Arrays;
|
|
|
30 |
import java.util.Comparator;
|
|
|
31 |
import java.util.List;
|
| 34240 |
tejus.loha |
32 |
import java.util.stream.Collectors;
|
|
|
33 |
|
|
|
34 |
@Service
|
|
|
35 |
public class PurSaleServiceImpl implements PurSaleService {
|
|
|
36 |
|
| 34249 |
tejus.loha |
37 |
private static final Logger LOGGER = LogManager.getLogger(PurSaleServiceImpl.class);
|
|
|
38 |
|
|
|
39 |
|
| 34240 |
tejus.loha |
40 |
@Autowired
|
|
|
41 |
WarehouseSupplierInvoiceRepository warehouseSupplierInvoiceRepository;
|
|
|
42 |
|
|
|
43 |
@Autowired
|
| 35102 |
amit |
44 |
JavaMailSender googleMailSender;
|
| 34240 |
tejus.loha |
45 |
|
| 34275 |
tejus.loha |
46 |
@Autowired
|
|
|
47 |
AuthService authService;
|
|
|
48 |
@Autowired
|
|
|
49 |
AuthRepository authRepository;
|
|
|
50 |
|
| 34979 |
ranu |
51 |
@Autowired
|
|
|
52 |
CsService csService;
|
|
|
53 |
|
| 34240 |
tejus.loha |
54 |
@Override
|
| 34275 |
tejus.loha |
55 |
public void moreThen10DaysEgedStockReport() throws MessagingException, UnsupportedEncodingException, ProfitMandiBusinessException {
|
|
|
56 |
LocalDateTime startDate = LocalDateTime.now().minusMonths(3);
|
|
|
57 |
LocalDateTime endDate = LocalDateTime.now();
|
| 34299 |
tejus.loha |
58 |
List<CatalogAgedModel> moreThen10DaysAgedCatalogModels = getCatalogModelsOlderThan10Days(startDate, endDate);
|
| 34240 |
tejus.loha |
59 |
StringBuilder sb = new StringBuilder();
|
| 35009 |
ranu |
60 |
sb.append("<html><body><b>Aging Stock List: Items that are 4 days old or older (Date Range - " + startDate.toLocalDate() + " To " + endDate.toLocalDate() + " )</b>" + "<br/><table style='border:1px solid black ;padding: 5px ;bgColor=green'>");
|
| 34240 |
tejus.loha |
61 |
sb.append("<tbody>" +
|
|
|
62 |
"<tr>" +
|
|
|
63 |
"<th style='border:1px solid black;padding: 5px'>No.</th>" +
|
| 35577 |
aman |
64 |
"<th style='border:1px solid black;padding: 5px'>Item Id.</th>" +
|
| 34240 |
tejus.loha |
65 |
"<th style='border:1px solid black;padding: 5px'>Brand</th>" +
|
|
|
66 |
"<th style='border:1px solid black;padding: 5px'>Vendor</th>" +
|
|
|
67 |
"<th style='border:1px solid black;padding: 5px'>Category</th>" +
|
|
|
68 |
"<th style='border:1px solid black;padding: 5px'>Model</th>" +
|
| 35241 |
ranu |
69 |
"<th style='border:1px solid black;padding: 5px'>Model Name</th>" +
|
| 34297 |
tejus.loha |
70 |
"<th style='border:1px solid black;padding: 5px'>Colour</th>" +
|
| 34240 |
tejus.loha |
71 |
"<th style='border:1px solid black;padding: 5px'>Purchase</th>" +
|
|
|
72 |
"<th style='border:1px solid black;padding: 5px'>Sold</th>" +
|
|
|
73 |
"<th style='border:1px solid black;padding: 5px'>Unsold</th>" +
|
|
|
74 |
"<th style='border:1px solid black;padding: 5px'>Age</th>" +
|
|
|
75 |
|
|
|
76 |
"</tr>");
|
|
|
77 |
|
| 34299 |
tejus.loha |
78 |
|
|
|
79 |
moreThen10DaysAgedCatalogModels.sort(Comparator.comparingInt(CatalogAgedModel::getAge).reversed());
|
| 35577 |
aman |
80 |
|
|
|
81 |
// Log first few records to verify itemId is populated
|
|
|
82 |
LOGGER.info("moreThen10DaysEgedStockReport - Total records: {}", moreThen10DaysAgedCatalogModels.size());
|
|
|
83 |
moreThen10DaysAgedCatalogModels.stream().limit(5).forEach(model ->
|
|
|
84 |
LOGGER.info("ItemId: {}, Brand: {}, Model: {}", model.getItemId(), model.getBrand(), model.getModel())
|
|
|
85 |
);
|
|
|
86 |
|
| 34240 |
tejus.loha |
87 |
int i = 1;
|
|
|
88 |
for (CatalogAgedModel catalogAgedModel : moreThen10DaysAgedCatalogModels) {
|
|
|
89 |
sb.append("<tr>");
|
|
|
90 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + (i++) + "</td>");
|
| 35577 |
aman |
91 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getItemId() + "</td>");
|
| 34240 |
tejus.loha |
92 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getBrand() + "</td>");
|
|
|
93 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getSupplier() + "</td>");
|
|
|
94 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getStatus() + "</td>");
|
| 35242 |
ranu |
95 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getModelName() + "</td>");
|
| 35241 |
ranu |
96 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getModel() + "</td>");
|
| 34297 |
tejus.loha |
97 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getColor().replaceFirst("f_", "") + "</td>");
|
| 34240 |
tejus.loha |
98 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getPurchase() + "</td>");
|
|
|
99 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getSold() + "</td>");
|
|
|
100 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getUnsold() + "</td>");
|
|
|
101 |
sb.append("<td style='border:1px solid black;padding: 5px'>" + catalogAgedModel.getAge() + "</td>");
|
|
|
102 |
sb.append("</tr>");
|
|
|
103 |
|
|
|
104 |
}
|
| 35050 |
amit |
105 |
sb.append("</tbody>");
|
|
|
106 |
sb.append("</table>");
|
|
|
107 |
|
|
|
108 |
|
| 35102 |
amit |
109 |
List<WarehouseItemAgeingModel> warehouseAgeQtyModels = getAgeingModelsGtDays(startDate, endDate, 4);
|
| 35050 |
amit |
110 |
|
| 35102 |
amit |
111 |
org.apache.commons.io.output.ByteArrayOutputStream byteArrayOutputStream = null;
|
|
|
112 |
try {
|
|
|
113 |
byteArrayOutputStream = FileUtil.getCSVByteStream(Arrays.asList("Vendor", "Category", "Brand", "Model", "Color",
|
|
|
114 |
"Age", "Warehouse", "Grn On", "In Stock"),
|
|
|
115 |
warehouseAgeQtyModels.stream().map(x -> x.toRow()).collect(Collectors.toList()));
|
|
|
116 |
} catch (Exception e) {
|
|
|
117 |
throw new RuntimeException(e);
|
| 35050 |
amit |
118 |
}
|
|
|
119 |
|
| 35102 |
amit |
120 |
sb.append("</body></html>");
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
Utils.Attachment attachment1 = new Utils.Attachment("warehousewise-aged.csv", new ByteArrayResource(byteArrayOutputStream.toByteArray()));
|
|
|
124 |
System.out.println("Sending mail ....");
|
|
|
125 |
//Utils.sendMailWithAttachments(mailSender, new String[]{"amit.gupta@smartdukaan.com"}, new String[]{"cnc.center@shop2020.in"}, mailSubject, "Dear Partner, \nPlease find attached Credit Note(s)", attachments.toArray(new Utils.Attachment[0]));
|
|
|
126 |
//System.out.println("Mail sent ....");
|
|
|
127 |
//break;
|
|
|
128 |
//Utils.sendMailWithAttachments(mailSender, new String[]{customRetailer.getEmail()}, new String[]{"cnc.center@shop2020.in"}, mailSubject, "Dear Partner, \nPlease find attached Credit Note(s)", attachments.toArray(new Utils.Attachment[0]));
|
| 34996 |
ranu |
129 |
String subject = "Aging Stock Report: 4 Days or Older";
|
| 34240 |
tejus.loha |
130 |
String messageText = sb.toString();
|
| 35102 |
amit |
131 |
MimeMessage message = googleMailSender.createMimeMessage();
|
| 34240 |
tejus.loha |
132 |
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
| 34996 |
ranu |
133 |
|
|
|
134 |
List<AuthUser> accountsAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_ACCOUNTS, Arrays.asList(EscalationType.L1, EscalationType.L2));
|
| 35050 |
amit |
135 |
List<String> accountTeamMails = accountsAuthUserList.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
| 35008 |
ranu |
136 |
accountTeamMails.add("neeraj.gupta@smartdukaan.com");
|
| 34996 |
ranu |
137 |
|
| 34979 |
ranu |
138 |
List<AuthUser> salesAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Arrays.asList(EscalationType.L1, EscalationType.L2));
|
| 35050 |
amit |
139 |
List<String> salesTeamMails = salesAuthUserList.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
| 35008 |
ranu |
140 |
|
|
|
141 |
List<AuthUser> abmAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_ABM, Arrays.asList(EscalationType.L1, EscalationType.L2));
|
| 35050 |
amit |
142 |
List<String> abmTeamMails = abmAuthUserList.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
| 35008 |
ranu |
143 |
|
| 35050 |
amit |
144 |
List<AuthUser> categoryAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_ABM, Arrays.asList(EscalationType.L1, EscalationType.L2, EscalationType.L3));
|
|
|
145 |
List<String> categoryTeamMails = categoryAuthUserList.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
| 35008 |
ranu |
146 |
|
| 34275 |
tejus.loha |
147 |
List<String> allEmails = new ArrayList<>(Arrays.asList(
|
| 35052 |
amit |
148 |
"sm@smartdukaan.com",
|
| 35078 |
ranu |
149 |
"raj.singh@smartdukaan.com",
|
| 35094 |
ranu |
150 |
"ranu.rajput@smartdukaan.com",
|
|
|
151 |
"tarun.verma@smartdukaan.com"
|
| 34299 |
tejus.loha |
152 |
));
|
| 34254 |
tejus.loha |
153 |
|
| 34297 |
tejus.loha |
154 |
// niranjan kala(authId -54) Team Emails,
|
| 34275 |
tejus.loha |
155 |
List<Integer> allReportees = authService.getAllReportees(54);
|
| 35394 |
amit |
156 |
List<AuthUser> authUsers = authRepository.selectByIds(allReportees);
|
| 34275 |
tejus.loha |
157 |
List<String> categotyTeamEmails = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
|
|
158 |
|
| 35052 |
amit |
159 |
allEmails.addAll(categotyTeamEmails);
|
| 34275 |
tejus.loha |
160 |
String[] emailTo = allEmails.toArray(new String[allEmails.size()]);
|
| 34240 |
tejus.loha |
161 |
helper.setSubject(subject);
|
|
|
162 |
helper.setText(messageText, true);
|
|
|
163 |
helper.setTo(emailTo);
|
| 35008 |
ranu |
164 |
|
|
|
165 |
List<String> bccEmails = new ArrayList<>();
|
|
|
166 |
bccEmails.addAll(salesTeamMails);
|
|
|
167 |
bccEmails.addAll(accountTeamMails);
|
|
|
168 |
bccEmails.addAll(categoryTeamMails);
|
|
|
169 |
bccEmails.addAll(abmTeamMails);
|
|
|
170 |
|
|
|
171 |
String[] emailBcc = bccEmails.toArray(new String[0]);
|
|
|
172 |
helper.setBcc(emailBcc);
|
|
|
173 |
|
| 34275 |
tejus.loha |
174 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
|
| 34240 |
tejus.loha |
175 |
helper.setFrom(senderAddress);
|
| 35102 |
amit |
176 |
try {
|
|
|
177 |
Utils.sendMailWithAttachments(googleMailSender, emailTo, null, emailBcc, subject, messageText, true, attachment1);
|
|
|
178 |
//Utils.sendMailWithAttachments(googleMailSender, new String[]{"amit.gupta@smartdukaan.com"}, null, null, subject, messageText, true, attachment1);
|
|
|
179 |
} catch (Exception e) {
|
|
|
180 |
throw new RuntimeException(e);
|
|
|
181 |
}
|
| 34240 |
tejus.loha |
182 |
}
|
|
|
183 |
|
|
|
184 |
@Override
|
| 34275 |
tejus.loha |
185 |
public List<CatalogAgedModel> getCatalogModelsOlderThan10Days(LocalDateTime startDate, LocalDateTime endDate) {
|
| 35577 |
aman |
186 |
// Using selectCatalogAgeWithItemId to get itemId for email report
|
| 34240 |
tejus.loha |
187 |
List<CatalogAgedModel> fiterCatalogAgedModels =
|
| 35577 |
aman |
188 |
warehouseSupplierInvoiceRepository.selectCatalogAgeWithItemId(startDate, endDate, null, 0, null)
|
| 34996 |
ranu |
189 |
.stream().filter(x -> x.getAge() >= 4 && x.getUnsold() > 0).collect(Collectors.toList());
|
| 34275 |
tejus.loha |
190 |
LOGGER.info("fiterCatalogAgedModels - " + fiterCatalogAgedModels);
|
| 34240 |
tejus.loha |
191 |
return fiterCatalogAgedModels;
|
|
|
192 |
}
|
| 35050 |
amit |
193 |
|
|
|
194 |
|
|
|
195 |
private List<WarehouseItemAgeingModel> getAgeingModelsGtDays(LocalDateTime startDate, LocalDateTime endDate, int days) {
|
|
|
196 |
List<WarehouseItemAgeingModel> fiterCatalogAgedModels =
|
|
|
197 |
warehouseSupplierInvoiceRepository.selectWarehouseItemAgeing(startDate, endDate, null, 0, null)
|
|
|
198 |
.stream().filter(x -> x.getAge() >= days).sorted(Comparator.comparing(WarehouseItemAgeingModel::getAge).reversed()).collect(Collectors.toList());
|
|
|
199 |
return fiterCatalogAgedModels;
|
|
|
200 |
}
|
| 34240 |
tejus.loha |
201 |
}
|