Subversion Repositories SmartDukaan

Rev

Rev 34450 | Rev 34619 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34450 Rev 34606
Line 1... Line 1...
1
package com.smartdukaan.cron.scheduled;
1
package com.smartdukaan.cron.scheduled;
2
 
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
4
import com.spice.profitmandi.common.model.CustomRetailer;
4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
5
import com.spice.profitmandi.dao.cart.CartService;
6
import com.spice.profitmandi.dao.cart.CartService;
6
import com.spice.profitmandi.dao.cart.SmartCartService;
7
import com.spice.profitmandi.dao.cart.SmartCartService;
7
import com.spice.profitmandi.dao.entity.auth.AuthUser;
8
import com.spice.profitmandi.dao.entity.auth.AuthUser;
8
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
9
import com.spice.profitmandi.dao.entity.fofo.*;
9
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
10
import com.spice.profitmandi.dao.entity.logistics.AST;
10
import com.spice.profitmandi.dao.entity.fofo.RbmRating;
11
import com.spice.profitmandi.dao.entity.logistics.ASTRepository;
11
import com.spice.profitmandi.dao.entity.fofo.SalesRating;
12
import com.spice.profitmandi.dao.entity.logistics.AreaRepository;
12
import com.spice.profitmandi.dao.entity.transaction.Loan;
13
import com.spice.profitmandi.dao.entity.transaction.Loan;
-
 
14
import com.spice.profitmandi.dao.entity.transaction.Order;
13
import com.spice.profitmandi.dao.entity.transaction.Transaction;
15
import com.spice.profitmandi.dao.entity.transaction.Transaction;
14
import com.spice.profitmandi.dao.entity.user.User;
16
import com.spice.profitmandi.dao.entity.user.User;
15
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
17
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
-
 
18
import com.spice.profitmandi.dao.model.BIRetailerModel;
-
 
19
import com.spice.profitmandi.dao.model.BiSecondaryModel;
-
 
20
import com.spice.profitmandi.dao.model.ReturnOrderInfoModel;
-
 
21
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
16
import com.spice.profitmandi.dao.repository.cs.CsService;
22
import com.spice.profitmandi.dao.repository.cs.CsService;
17
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
23
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
18
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
-
 
19
import com.spice.profitmandi.dao.repository.fofo.RbmRatingRepository;
24
import com.spice.profitmandi.dao.repository.fofo.*;
20
import com.spice.profitmandi.dao.repository.fofo.SalesRatingRepository;
25
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
21
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
26
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
-
 
27
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
-
 
28
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderInfoRepository;
22
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
29
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
23
import com.spice.profitmandi.dao.repository.user.UserRepository;
30
import com.spice.profitmandi.dao.repository.user.UserRepository;
24
import com.spice.profitmandi.service.transaction.SDCreditService;
31
import com.spice.profitmandi.service.transaction.SDCreditService;
-
 
32
import com.spice.profitmandi.service.user.RetailerService;
-
 
33
import in.shop2020.model.v1.order.OrderStatus;
25
import org.apache.logging.log4j.LogManager;
34
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.Logger;
35
import org.apache.logging.log4j.Logger;
27
import org.springframework.beans.factory.annotation.Autowired;
36
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.beans.factory.annotation.Value;
37
import org.springframework.beans.factory.annotation.Value;
29
import org.springframework.mail.javamail.JavaMailSender;
38
import org.springframework.mail.javamail.JavaMailSender;
Line 35... Line 44...
35
import javax.mail.internet.InternetAddress;
44
import javax.mail.internet.InternetAddress;
36
import javax.mail.internet.MimeMessage;
45
import javax.mail.internet.MimeMessage;
37
import java.io.IOException;
46
import java.io.IOException;
38
import java.time.LocalDate;
47
import java.time.LocalDate;
39
import java.time.LocalDateTime;
48
import java.time.LocalDateTime;
-
 
49
import java.time.LocalTime;
-
 
50
import java.time.YearMonth;
40
import java.time.temporal.ChronoUnit;
51
import java.time.temporal.ChronoUnit;
41
import java.util.*;
52
import java.util.*;
42
import java.util.stream.Collectors;
53
import java.util.stream.Collectors;
43
 
54
 
44
@Component
55
@Component
Line 75... Line 86...
75
    FofoStoreRepository fofoStoreRepository;
86
    FofoStoreRepository fofoStoreRepository;
76
 
87
 
77
    @Autowired
88
    @Autowired
78
    SmartCartService smartCartService;
89
    SmartCartService smartCartService;
79
 
90
 
-
 
91
    @Autowired
-
 
92
    RetailerService retailerService;
-
 
93
 
-
 
94
    @Autowired
-
 
95
    ASTRepository astRepository;
-
 
96
 
-
 
97
    @Autowired
-
 
98
    AuthRepository authRepository;
-
 
99
 
-
 
100
    @Autowired
-
 
101
    StateRepository stateRepository;
-
 
102
 
-
 
103
    @Autowired
-
 
104
    MonthlyTargetRepository monthlyTargetRepository;
-
 
105
 
-
 
106
    @Autowired
-
 
107
    PartnerTypeChangeService partnerTypeChangeService;
-
 
108
 
-
 
109
    @Autowired
-
 
110
    ReturnOrderInfoRepository returnOrderInfoRepository;
-
 
111
 
-
 
112
    @Autowired
-
 
113
    OrderRepository orderRepository;
-
 
114
 
80
    public void test() throws Exception {
115
    public void test() throws Exception {
81
        System.out.println("test start");
116
        System.out.println("test start");
82
        smartCartService.activatedButNotBilledCatalogQtyMap();
117
//        smartCartService.activatedButNotBilledCatalogQtyMap();
83
        System.out.println("test end");
118
        System.out.println("test end");
84
 
119
 
85
    }
120
    }
86
 
121
 
87
    public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
122
    public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
Line 233... Line 268...
233
        this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
268
        this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
234
 
269
 
235
        LOGGER.info("Consolidated RBM feedback summary email sent.");
270
        LOGGER.info("Consolidated RBM feedback summary email sent.");
236
    }
271
    }
237
 
272
 
238
@Autowired
-
 
239
    PartnerTypeChangeService partnerTypeChangeService;
-
 
240
 
273
 
241
    public void sendSalesFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
274
    public void sendSalesFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
242
        LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
275
        LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
243
        LocalDateTime endOfMonth = LocalDateTime.now();
276
        LocalDateTime endOfMonth = LocalDateTime.now();
244
        String[] bcc = {"tarun.verma@smartdukaan.com"};
277
        String[] bcc = {"tarun.verma@smartdukaan.com"};
Line 326... Line 359...
326
        String subject = "Monthly Sales L1 Feedback Summary Test test - " + LocalDate.now().getMonth();
359
        String subject = "Monthly Sales L1 Feedback Summary Test test - " + LocalDate.now().getMonth();
327
 
360
 
328
        List<String> sendTo = new ArrayList<>();
361
        List<String> sendTo = new ArrayList<>();
329
         sendTo.add("sm@smartdukaan.com"); //
362
         sendTo.add("sm@smartdukaan.com"); //
330
         sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
363
         sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
331
        sendTo.add("kamini.sharma@smartdukaan.com"); //
364
         sendTo.add("kamini.sharma@smartdukaan.com"); //
332
 
365
 
333
        String[] emailRecipients = sendTo.toArray(new String[0]);
366
        String[] emailRecipients = sendTo.toArray(new String[0]);
334
 
367
 
335
 
368
 
336
        this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
369
        this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
337
 
370
 
338
        LOGGER.info("Consolidated Sales L1 feedback summary email sent.");
371
        LOGGER.info("Consolidated Sales L1 feedback summary email sent.");
339
    }
372
    }
340
 
373
 
341
 
374
 
-
 
375
    public void generateBiReportExcel() throws Exception {
-
 
376
        LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
-
 
377
 
-
 
378
        Map<Integer, CustomRetailer> customRetailers = retailerService.getAllFofoRetailers();
-
 
379
 
-
 
380
        List<Integer> retailerIds = customRetailers.values().stream().map(CustomRetailer::getPartnerId).collect(Collectors.toList());
-
 
381
 
-
 
382
//        this month return data
-
 
383
        LocalDateTime currentMonthStartDate = YearMonth.now().atDay(1).atStartOfDay();
-
 
384
        LocalDateTime currentMonthEndDate = LocalDateTime.now();
-
 
385
 
-
 
386
        List<ReturnOrderInfoModel> currentMonthReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(currentMonthStartDate, currentMonthEndDate);
-
 
387
        Map<Integer, Long> currentMonthPartnerReturnOrderInfoModelMap = currentMonthReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
-
 
388
 
-
 
389
        List<Order> currentMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(currentMonthStartDate, currentMonthEndDate, OrderStatus.RTO_REFUNDED);
-
 
390
        Map<Integer, Long> currentMonthRtoRefundOrderMap = currentMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
-
 
391
 
-
 
392
 
-
 
393
//        last month return data
-
 
394
        YearMonth lastMonth = YearMonth.now().minusMonths(1);
-
 
395
        LocalDateTime lastMontStartDate = lastMonth.atDay(1).atStartOfDay();
-
 
396
        LocalDateTime lastMonthEndDate = lastMonth.atEndOfMonth().atTime(23, 59, 59);
-
 
397
 
-
 
398
        List<ReturnOrderInfoModel> lastMonthReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(lastMontStartDate, lastMonthEndDate);
-
 
399
        Map<Integer, Long> lastMonthPartnerReturnOrderInfoModelMap = lastMonthReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
-
 
400
 
-
 
401
        List<Order> lastMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(lastMontStartDate, lastMonthEndDate, OrderStatus.RTO_REFUNDED);
-
 
402
        Map<Integer, Long> lastMonthRtoRefundOrderMap = lastMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
-
 
403
 
-
 
404
 
-
 
405
//        twoMonthsAgo return data
-
 
406
        YearMonth twoMonthsAgo = YearMonth.now().minusMonths(2);
-
 
407
        LocalDateTime twoMonthsAgoStartDate = twoMonthsAgo.atDay(1).atStartOfDay();
-
 
408
        LocalDateTime twoMonthsAgoEndDate = twoMonthsAgo.atEndOfMonth().atTime(23, 59, 59);
-
 
409
 
-
 
410
        List<ReturnOrderInfoModel> twoMonthAgoReturnOrderInfoModels = returnOrderInfoRepository.selectAllByBetweenDate(twoMonthsAgoStartDate, twoMonthsAgoEndDate);
-
 
411
        Map<Integer, Long> twoMonthAgoPartnerReturnOrderInfoModelMap = twoMonthAgoReturnOrderInfoModels.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getRefundAmount()))));
-
 
412
 
-
 
413
        List<Order> twoMonthRtoRefundOrders = orderRepository.selectAllOrderDatesBetweenByStatus(twoMonthsAgoStartDate, twoMonthsAgoEndDate, OrderStatus.RTO_REFUNDED);
-
 
414
        Map<Integer, Long> twoMonthRtoRefundOrderMap = twoMonthRtoRefundOrders.stream().collect(Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingLong(x -> Math.round(x.getTotalAmount()))));
-
 
415
 
-
 
416
 
-
 
417
        List<BIRetailerModel> biRetailerModels = new ArrayList<>();
-
 
418
 
-
 
419
        List<BiSecondaryModel> biSecondaryModels = new ArrayList<>();
-
 
420
 
-
 
421
        for(Integer fofoId: retailerIds){
-
 
422
 
-
 
423
            int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM,EscalationType.L1,fofoId);
-
 
424
            String rbmName = authRepository.selectById(rbmL1).getFullName();
-
 
425
 
-
 
426
            int bmId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES,EscalationType.L2,fofoId);
-
 
427
            String bmName = authRepository.selectById(bmId).getFullName();
-
 
428
 
-
 
429
            int managerId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES,EscalationType.L1,fofoId);
-
 
430
            String managerName = " ";
-
 
431
            if(managerId != 0){
-
 
432
                 managerName = authRepository.selectById(managerId).getFullName();
-
 
433
            }else {
-
 
434
                managerName = bmName;
-
 
435
            }
-
 
436
 
-
 
437
            AST ast = astRepository.selectById(customRetailers.get(fofoId).getAstId());
-
 
438
 
-
 
439
            PartnerType partnerTypeThisMonth = partnerTypeChangeService.getTypeOnMonth(fofoId, YearMonth.now());
-
 
440
 
-
 
441
//            generate retaile detail
-
 
442
 
-
 
443
            BIRetailerModel biRetailerModel = new BIRetailerModel();
-
 
444
            biRetailerModel.setBmName(bmName);
-
 
445
            biRetailerModel.setCode(customRetailers.get(fofoId).getCode());
-
 
446
            biRetailerModel.setArea(ast.getArea());
-
 
447
            biRetailerModel.setCity(customRetailers.get(fofoId).getAddress().getCity());
-
 
448
            biRetailerModel.setStoreName(customRetailers.get(fofoId).getBusinessName());
-
 
449
            biRetailerModel.setStoreName(String.valueOf(customRetailers.get(fofoId).getActivationType()));
-
 
450
            biRetailerModel.setCategory(String.valueOf(partnerTypeThisMonth));
-
 
451
            biRetailerModel.setSalesManager(managerName);
-
 
452
            biRetailerModel.setRbm(rbmName);
-
 
453
            biRetailerModels.add(biRetailerModel);
-
 
454
 
-
 
455
//            generate secondary data
-
 
456
 
-
 
457
//            this month secondary target
-
 
458
 
-
 
459
            MonthlyTarget secondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(YearMonth.now(), fofoId);
-
 
460
 
-
 
461
            Map<Integer, Double> secondaryMtd = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
-
 
462
                    Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
-
 
463
                    startOfToday.withDayOfMonth(1), startOfToday.with(LocalTime.MAX)).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
-
 
464
 
-
 
465
            double secondaryAchievedMtd = secondaryMtd.getOrDefault(fofoId, 0.0);
-
 
466
 
-
 
467
 
-
 
468
//            last month secondary target
-
 
469
 
-
 
470
            MonthlyTarget lastMonthSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId);
-
 
471
 
-
 
472
            Map<Integer, Double> lastMonthSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
-
 
473
                    Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
-
 
474
                    lastMontStartDate, lastMonthEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
-
 
475
 
-
 
476
            double lastMonthSecondaryAchieved = lastMonthSecondary.getOrDefault(fofoId, 0.0);
-
 
477
 
-
 
478
 
-
 
479
//            last month secondary target
-
 
480
 
-
 
481
            MonthlyTarget twoMonthAgoSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId);
-
 
482
 
-
 
483
            Map<Integer, Double> twoMonthAgoSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
-
 
484
                    Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS, OrderStatus.ACCEPTED, OrderStatus.SUBMITTED_FOR_PROCESSING),
-
 
485
                    twoMonthsAgoStartDate, twoMonthsAgoEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
-
 
486
 
-
 
487
            double twoMonthAgoSecondaryAchieved = twoMonthAgoSecondary.getOrDefault(fofoId, 0.0);
-
 
488
 
-
 
489
//
-
 
490
////            this month tertiary
-
 
491
//            Double todaySale = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday, now, fofoId, false).get(fofoId);
-
 
492
//            Double mtdSaleTillYesterDay = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday.withDayOfMonth(1), startOfToday, fofoId, false).get(fofoId);
-
 
493
//            Double mtdSale = mtdSaleTillYesterDay + todaySale;
-
 
494
//            Double lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
-
 
495
//                    startOfToday.withDayOfMonth(1).minusMonths(1), now.minusMonths(1), fofoId, false).get(fofoId);
-
 
496
//
-
 
497
//
-
 
498
//            BiSecondaryModel biSecondaryModel = new BiSecondaryModel();
-
 
499
 
-
 
500
        }
-
 
501
 
-
 
502
        LOGGER.info("biRetailerModels {}",biRetailerModels);
-
 
503
 
-
 
504
 
-
 
505
 
-
 
506
 
-
 
507
    }
-
 
508
 
-
 
509
 
342
 
510
 
343
 
511
 
344
}
512
}