Subversion Repositories SmartDukaan

Rev

Rev 35956 | Details | Compare with Previous | Last modification | View Log | RSS feed

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