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