Subversion Repositories SmartDukaan

Rev

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

Rev 34704 Rev 34715
Line 35... Line 35...
35
import in.shop2020.model.v1.order.OrderStatus;
35
import in.shop2020.model.v1.order.OrderStatus;
36
import in.shop2020.model.v1.order.WalletReferenceType;
36
import in.shop2020.model.v1.order.WalletReferenceType;
37
import org.apache.commons.io.output.ByteArrayOutputStream;
37
import org.apache.commons.io.output.ByteArrayOutputStream;
38
import org.apache.logging.log4j.LogManager;
38
import org.apache.logging.log4j.LogManager;
39
import org.apache.logging.log4j.Logger;
39
import org.apache.logging.log4j.Logger;
-
 
40
import org.apache.poi.common.usermodel.HyperlinkType;
40
import org.apache.poi.ss.usermodel.*;
41
import org.apache.poi.ss.usermodel.*;
41
import org.apache.poi.xssf.usermodel.XSSFColor;
42
import org.apache.poi.xssf.usermodel.XSSFColor;
42
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
43
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
43
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Qualifier;
45
import org.springframework.beans.factory.annotation.Qualifier;
Line 149... Line 150...
149
    RbmTargetService rbmTargetService;
150
    RbmTargetService rbmTargetService;
150
 
151
 
151
    @Autowired
152
    @Autowired
152
    PartnerStatsService partnerStatsService;
153
    PartnerStatsService partnerStatsService;
153
 
154
 
-
 
155
    @Autowired
-
 
156
    UserWalletHistoryRepository userWalletHistoryRepository;
-
 
157
 
154
    public void test() throws Exception {
158
    public void test() throws Exception {
155
        System.out.println("test start");
159
        System.out.println("test start");
156
        //partnerStatsService.getAllPartnerStats();
160
        //partnerStatsService.getAllPartnerStats();
157
        this.generateBiReportExcel();
161
        this.generateBiReportExcel();
158
        System.out.println("test end");
162
        System.out.println("test end");
Line 593... Line 597...
593
 
597
 
594
 
598
 
595
            long currentMonthReturn = currentMonthPartnerReturnOrderInfoModelMap.getOrDefault(fofoId, 0L) + currentMonthRtoRefundOrderMap.getOrDefault(fofoId, 0L);
599
            long currentMonthReturn = currentMonthPartnerReturnOrderInfoModelMap.getOrDefault(fofoId, 0L) + currentMonthRtoRefundOrderMap.getOrDefault(fofoId, 0L);
596
 
600
 
597
 
601
 
598
            Map<Integer, Double> secondaryMtd = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
602
            Map<Integer, Double> secondaryMtd = orderRepository.selectOrderValueBetweenBillingDatesGroupByFofoId(Arrays.asList(fofoId),
599
                    Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS),
-
 
600
                    startOfToday.withDayOfMonth(1), startOfToday.with(LocalTime.MAX).minusDays(1)).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
603
                    startOfToday.withDayOfMonth(1), startOfToday.with(LocalTime.MAX).minusDays(1)).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
601
 
604
 
602
            double secondaryAchievedMtd = secondaryMtd.getOrDefault(fofoId, 0.0);
605
            double secondaryAchievedMtd = secondaryMtd.getOrDefault(fofoId, 0.0);
603
 
606
 
604
            double currentMonthNetSecondary = secondaryAchievedMtd - currentMonthReturn;
607
            double currentMonthNetSecondary = secondaryAchievedMtd - currentMonthReturn;
Line 619... Line 622...
619
 
622
 
620
            double lastMonthSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId) != null ?  monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId).getPurchaseTarget() : 0;
623
            double lastMonthSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId) != null ?  monthlyTargetRepository.selectByDateAndFofoId(lastMonth, fofoId).getPurchaseTarget() : 0;
621
 
624
 
622
            long lastMonthReturn = (lastMonthPartnerReturnOrderInfoModelMap.getOrDefault(fofoId,0L) + lastMonthRtoRefundOrderMap.getOrDefault(fofoId,0L));
625
            long lastMonthReturn = (lastMonthPartnerReturnOrderInfoModelMap.getOrDefault(fofoId,0L) + lastMonthRtoRefundOrderMap.getOrDefault(fofoId,0L));
623
 
626
 
624
            Map<Integer, Double> lastMonthSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
627
            Map<Integer, Double> lastMonthSecondary = orderRepository.selectOrderValueBetweenBillingDatesGroupByFofoId(Arrays.asList(fofoId),
625
                    Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS),
-
 
626
                    lastMontStartDate, lastMonthEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
628
                    lastMontStartDate, lastMonthEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
627
 
629
 
628
            double lastMonthSecondaryAchieved = lastMonthSecondary.getOrDefault(fofoId, 0.0);
630
            double lastMonthSecondaryAchieved = lastMonthSecondary.getOrDefault(fofoId, 0.0);
629
 
631
 
630
            double lastMonthNetSecondary = lastMonthSecondaryAchieved - lastMonthReturn;
632
            double lastMonthNetSecondary = lastMonthSecondaryAchieved - lastMonthReturn;
Line 642... Line 644...
642
 
644
 
643
            double twoMonthAgoSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(twoMonthsAgo, fofoId) != null ? monthlyTargetRepository.selectByDateAndFofoId(twoMonthsAgo, fofoId).getPurchaseTarget() : 0;
645
            double twoMonthAgoSecondaryTarget = monthlyTargetRepository.selectByDateAndFofoId(twoMonthsAgo, fofoId) != null ? monthlyTargetRepository.selectByDateAndFofoId(twoMonthsAgo, fofoId).getPurchaseTarget() : 0;
644
 
646
 
645
            long twoMonthAgoReturn = (twoMonthAgoPartnerReturnOrderInfoModelMap.getOrDefault(fofoId,0L) + twoMonthAgoRtoRefundOrderMap.getOrDefault(fofoId,0L));
647
            long twoMonthAgoReturn = (twoMonthAgoPartnerReturnOrderInfoModelMap.getOrDefault(fofoId,0L) + twoMonthAgoRtoRefundOrderMap.getOrDefault(fofoId,0L));
646
 
648
 
647
            Map<Integer, Double> twoMonthAgoSecondary = orderRepository.selectOrderValueBetweenDatesGroupByFofoId(Arrays.asList(fofoId),
649
            Map<Integer, Double> twoMonthAgoSecondary = orderRepository.selectOrderValueBetweenBillingDatesGroupByFofoId(Arrays.asList(fofoId),
648
                    Arrays.asList(OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.DELIVERY_SUCCESS),
-
 
649
                    twoMonthsAgoStartDate, twoMonthsAgoEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
650
                    twoMonthsAgoStartDate, twoMonthsAgoEndDate).stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getAmount()));
650
 
651
 
651
            double twoMonthAgoSecondaryAchieved = twoMonthAgoSecondary.getOrDefault(fofoId, 0.0);
652
            double twoMonthAgoSecondaryAchieved = twoMonthAgoSecondary.getOrDefault(fofoId, 0.0);
652
 
653
 
653
            double twoMonthAgoNetSecondary = twoMonthAgoSecondaryAchieved - twoMonthAgoReturn;
654
            double twoMonthAgoNetSecondary = twoMonthAgoSecondaryAchieved - twoMonthAgoReturn;
Line 774... Line 775...
774
            String alertLevel = "-";
775
            String alertLevel = "-";
775
            int lastPurchaseDays = 0;
776
            int lastPurchaseDays = 0;
776
            if (orderId != 0) {
777
            if (orderId != 0) {
777
                Order order = orderRepository.selectById(orderId);
778
                Order order = orderRepository.selectById(orderId);
778
 
779
 
-
 
780
                LOGGER.info("last billing order - {}",order);
-
 
781
 
779
                // Calculate the number of days since the last purchase (billing)
782
                // Calculate the number of days since the last purchase (billing)
780
                lastPurchaseDays = (int) Duration.between(order.getCreateTimestamp().plusDays(1), LocalDateTime.now()).toDays();
783
                lastPurchaseDays = (int) Duration.between(order.getCreateTimestamp().plusDays(1), LocalDateTime.now()).toDays();
781
 
784
 
782
                if (lastPurchaseDays >= 11) {
785
                if (lastPurchaseDays >= 11) {
783
                    alertLevel = "Alert for Management";
786
                    alertLevel = "Alert for Management";
Line 852... Line 855...
852
        //generate excel and sent to mail
855
        //generate excel and sent to mail
853
        List<List<String>> headerGroup = new ArrayList<>();
856
        List<List<String>> headerGroup = new ArrayList<>();
854
 
857
 
855
        List<String> headers1 = Arrays.asList(
858
        List<String> headers1 = Arrays.asList(
856
                "","","","",
859
                "","","","",
857
                "Retailer Detail", "","", "", "", "", "", "", "", "","",
860
                "Retailer Detail", "","", "", "", "", "", "", "", "","","","",
858
 
861
 
859
                twoMonthAgoStringValue, "", "", "", "", "", "",
862
                twoMonthAgoStringValue, "", "", "", "", "", "",
860
                lastMonthStringValue, "", "", "", "", "", "",
863
                lastMonthStringValue, "", "", "", "", "", "",
861
                currentMonthStringValue, "", "", "", "", "", "",
864
                currentMonthStringValue, "", "", "", "", "", "",
862
 
865
 
Line 867... Line 870...
867
 
870
 
868
        );
871
        );
869
 
872
 
870
        List<String> headers2 = Arrays.asList(
873
        List<String> headers2 = Arrays.asList(
871
                "Assessment","Zero billing","Billing needed","Counta",
874
                "Assessment","Zero billing","Billing needed","Counta",
872
                "BM","Partner Id","Link","Code","Area",  "City", "Store Name", "Status","Category","Sales Manager", "RBM",
875
                "BM","Partner Id","Link","Wallet Date","Creation Date","Code","Area",  "City", "Store Name", "Status","Category","Sales Manager", "RBM",
873
                "Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
876
                "Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
874
                "Tertiary Sale", "Unbilled",
877
                "Tertiary Sale", "Unbilled",
875
                "Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
878
                "Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
876
                "Tertiary Sale", "Unbilled",
879
                "Tertiary Sale", "Unbilled",
877
                "Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
880
                "Secondary Target", "Secondary Achieved", "Returns", "Net Secondary", "Secondary %",
Line 889... Line 892...
889
 
892
 
890
 
893
 
891
        List<List<?>> rows = new ArrayList<>();
894
        List<List<?>> rows = new ArrayList<>();
892
        for (Map.Entry<Integer, BIRetailerModel> entry : biRetailerModelMap.entrySet()) {
895
        for (Map.Entry<Integer, BIRetailerModel> entry : biRetailerModelMap.entrySet()) {
893
            Integer fofoId = entry.getKey();
896
            Integer fofoId = entry.getKey();
-
 
897
            User user = userRepository.selectById(fofoId);
-
 
898
            LocalDateTime walletCreationDate = userWalletHistoryRepository.selectFirstCreatedDate(fofoId);
894
            BIRetailerModel retailer = entry.getValue();
899
            BIRetailerModel retailer = entry.getValue();
895
 
900
 
896
            Map<YearMonth, BiSecondaryModel> monthlyData = allRetailerMonthlyData.get(fofoId);
901
            Map<YearMonth, BiSecondaryModel> monthlyData = allRetailerMonthlyData.get(fofoId);
897
 
902
 
898
            BiSecondaryModel current = monthlyData.getOrDefault(YearMonth.now(), new BiSecondaryModel(0,0,0,0,0,0,0));
903
            BiSecondaryModel current = monthlyData.getOrDefault(YearMonth.now(), new BiSecondaryModel(0,0,0,0,0,0,0));
Line 900... Line 905...
900
            BiSecondaryModel twoAgo = monthlyData.getOrDefault(YearMonth.now().minusMonths(2), new BiSecondaryModel(0,0,0,0,0,0,0));
905
            BiSecondaryModel twoAgo = monthlyData.getOrDefault(YearMonth.now().minusMonths(2), new BiSecondaryModel(0,0,0,0,0,0,0));
901
 
906
 
902
            List<Object> row = new ArrayList<>();
907
            List<Object> row = new ArrayList<>();
903
            row.addAll(Arrays.asList(
908
            row.addAll(Arrays.asList(
904
                    assessmentMap.get(fofoId),zeroBillingMap.get(fofoId),billingNeededMap.get(fofoId),countAMap.get(fofoId),
909
                    assessmentMap.get(fofoId),zeroBillingMap.get(fofoId),billingNeededMap.get(fofoId),countAMap.get(fofoId),
905
                    retailer.getBmName(),fofoId ,"https://partners.smartdukaan.com/partnerPerformance?fofoId="+fofoId, retailer.getCode(), retailer.getArea(), retailer.getCity(),  retailer.getStoreName(), retailer.getStatus(),
910
                    retailer.getBmName(),fofoId ,"https://partners.smartdukaan.com/partnerPerformance?fofoId="+fofoId,walletCreationDate,user.getCreateTimestamp(), retailer.getCode(), retailer.getArea(), retailer.getCity(),  retailer.getStoreName(), retailer.getStatus(),
906
                    retailer.getCategory(),  retailer.getSalesManager(), retailer.getRbm()
911
                    retailer.getCategory(),  retailer.getSalesManager(), retailer.getRbm()
907
 
912
 
908
            ));
913
            ));
909
 
914
 
910
 
915
 
Line 948... Line 953...
948
                row.addAll(Arrays.asList(
953
                row.addAll(Arrays.asList(
949
                        fofoInvestmentModelValue.getCounterPotential(),
954
                        fofoInvestmentModelValue.getCounterPotential(),
950
                        fofoInvestmentModelValue.getShortInvestment(),
955
                        fofoInvestmentModelValue.getShortInvestment(),
951
                        fofoInvestmentModelValue.getDefaultLoan(),
956
                        fofoInvestmentModelValue.getDefaultLoan(),
952
                        fofoInvestmentModelValue.getInvestmentLevel(),
957
                        fofoInvestmentModelValue.getInvestmentLevel(),
953
                        fofoInvestmentModelValue.getActiveLoan(),
958
                       "-"+fofoInvestmentModelValue.getActiveLoan(),
954
                        fofoInvestmentModelValue.getPoValue(),
959
                        fofoInvestmentModelValue.getPoValue(),
955
                        fofoInvestmentModelValue.getAgreedInvestment(),
960
                        fofoInvestmentModelValue.getAgreedInvestment(),
956
                        fofoInvestmentModelValue.getWallet(),
961
                        fofoInvestmentModelValue.getWallet(),
957
                        fofoInvestmentModelValue.getPoAndBilled(),
962
                        fofoInvestmentModelValue.getPoAndBilled(),
958
                        fofoInvestmentModelValue.getMonthBeginingDrr(),
963
                        fofoInvestmentModelValue.getMonthBeginingDrr(),
Line 1034... Line 1039...
1034
    }
1039
    }
1035
 
1040
 
1036
    public static ByteArrayOutputStream getExcelStreamWithMultiHeaders(List<List<String>> headerGroup, List<List<?>> rows) {
1041
    public static ByteArrayOutputStream getExcelStreamWithMultiHeaders(List<List<String>> headerGroup, List<List<?>> rows) {
1037
        Workbook workbook = new XSSFWorkbook();
1042
        Workbook workbook = new XSSFWorkbook();
1038
        Sheet sheet = workbook.createSheet("BI Report");
1043
        Sheet sheet = workbook.createSheet("BI Report");
-
 
1044
        CreationHelper creationHelper = workbook.getCreationHelper();
1039
        int rowIndex = 0;
1045
        int rowIndex = 0;
1040
 
1046
 
1041
        CellStyle centeredStyle = workbook.createCellStyle();
1047
        CellStyle centeredStyle = workbook.createCellStyle();
1042
        centeredStyle.setAlignment(HorizontalAlignment.CENTER); // Center horizontally
1048
        centeredStyle.setAlignment(HorizontalAlignment.CENTER); // Center horizontally
1043
        centeredStyle.setVerticalAlignment(VerticalAlignment.CENTER); // Center vertically
1049
        centeredStyle.setVerticalAlignment(VerticalAlignment.CENTER); // Center vertically
Line 1077... Line 1083...
1077
        headerStyles.put("BM", blueStyle);
1083
        headerStyles.put("BM", blueStyle);
1078
        headerStyles.put("RBM", blueStyle);
1084
        headerStyles.put("RBM", blueStyle);
1079
        headerStyles.put("Sales Manager", blueStyle);
1085
        headerStyles.put("Sales Manager", blueStyle);
1080
        headerStyles.put("Status", blueStyle);
1086
        headerStyles.put("Status", blueStyle);
1081
        headerStyles.put("Category", blueStyle);
1087
        headerStyles.put("Category", blueStyle);
-
 
1088
        headerStyles.put("Wallet Date", blueStyle);
-
 
1089
        headerStyles.put("Creation Date", blueStyle);
-
 
1090
        headerStyles.put("Partner Id", blueStyle);
-
 
1091
 
-
 
1092
        //for link
-
 
1093
        // Create hyperlink style
-
 
1094
        CellStyle hyperlinkStyle = workbook.createCellStyle();
-
 
1095
        Font hlinkFont = workbook.createFont();
-
 
1096
        hlinkFont.setUnderline(Font.U_SINGLE);
-
 
1097
        hlinkFont.setColor(IndexedColors.BLUE.getIndex());
-
 
1098
        hyperlinkStyle.setFont(hlinkFont);
-
 
1099
 
1082
 
1100
 
1083
        // Light Yellow
1101
        // Light Yellow
1084
        CellStyle yellowStyle = workbook.createCellStyle();
1102
        CellStyle yellowStyle = workbook.createCellStyle();
1085
        yellowStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
1103
        yellowStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
1086
        yellowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
1104
        yellowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Line 1183... Line 1201...
1183
                if (value instanceof Number) {
1201
                if (value instanceof Number) {
1184
                    cell.setCellValue(((Number) value).doubleValue());
1202
                    cell.setCellValue(((Number) value).doubleValue());
1185
                } else {
1203
                } else {
1186
                    cell.setCellValue(value != null ? value.toString() : "");
1204
                    cell.setCellValue(value != null ? value.toString() : "");
1187
                }
1205
                }
-
 
1206
 
-
 
1207
                if (i == 6 && value != null) { // Assuming column 6 is "Link"
-
 
1208
                    Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
-
 
1209
                    hyperlink.setAddress(value.toString());
-
 
1210
                    cell.setCellValue("View Link"); // Text to display in Excel
-
 
1211
                    cell.setHyperlink(hyperlink);
-
 
1212
                    cell.setCellStyle(hyperlinkStyle);
-
 
1213
                } else {
-
 
1214
                    cell.setCellValue(value != null ? value.toString() : "");
-
 
1215
                }
1188
            }
1216
            }
1189
        }
1217
        }
1190
 
1218
 
1191
        // Auto-size columns
1219
        // Auto-size columns
1192
        if (!rows.isEmpty()) {
1220
        if (!rows.isEmpty()) {