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