Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
4.5 kB
1
Indexable
Never
@Scheduled(cron = "${shop.earn.approved.orders.report.scheduler}")
public void shopAndEarnApprovedOrderMailSend() {
    LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler started");
    
    try {
        String[] header = {"Retailer Id", "Retailer Mobile Number", "Order id", "Total Commission", "TDS", "GST", "Final amount", "Created at"};
        HashMap<Integer, String[]> approvedOrderRecords = new HashMap<>();
        final String paynearbyMallOrderReports = "paynearby_mall_approved_orders_report";
        final String excel = KeyConstants.EXCEL_EXTENTION;

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime fifteenDaysAgo = now.minusDays(15);
        Long currentDateTime = commonUtils.generateLongTimeStamp(now);

        List<AffiliateOrdersReport> approvedOrders = affiliaOrdersReportRepository.findByStatusApproved(now, fifteenDaysAgo);

        ConfigParam configParamShopAndEarnReportMailReceivers = configParamRepository.findByNameIgnoreCase(paynearbyMallOrderReports).orElse(null);

        if (configParamShopAndEarnReportMailReceivers != null && !StringUtils.isEmpty(configParamShopAndEarnReportMailReceivers.getValue())) {
            if (approvedOrders.isEmpty()) {
                LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler Today no entry found");
            } else {
                LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler previous day entry found  ");
                int index = 1;

                for (AffiliateOrdersReport ordersReport : approvedOrders) {
                    String[] approveRecord = new String[8];
                    double commission = ordersReport.getAgentCashback();
                    double tds = commission * 0.05;
                    double gst = commission * 0.18;
                    double finalAmount = commission - (tds + gst);

                    DecimalFormat df = new DecimalFormat("#.##");

                    approveRecord[0] = ordersReport.getAgentId().toString();
                    approveRecord[1] = ordersReport.getPartnerName();
                    approveRecord[2] = ordersReport.getOrderId();
                    approveRecord[3] = df.format(commission);
                    approveRecord[4] = df.format(tds);
                    approveRecord[5] = df.format(gst);
                    approveRecord[6] = df.format(finalAmount);
                    approveRecord[7] = commonUtils.getYYYYMMDDHHMMSS(ordersReport.getCreatedAt());
                    approvedOrderRecords.put(index, approveRecord);
                    index++;
                }

                String fileName = paynearbyMallOrderReports + currentDateTime + excel;
                FileReaderAndWriter.excelFileCreate(filePath, fileName, localFileSheetName);
                FileReaderAndWriter.writeExcel(filePath, fileName, localFileSheetName, approvedOrderRecords);

                LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler file created successfully");

                String attachmentPath = filePath + fileName;
                String mailSubject = emailSubject + " " + currentDateTime;
                String mailBody = emailBody + " Shop_and_earn_approved_orders_report" + currentDateTime + KeyConstants.EXCEL_EXTENTION;

                emailSender.sendMailWithAttachment(senderEmailId, configParamShopAndEarnReportMailReceivers.getValue(), mailSubject, mailBody, attachmentPath);

                Files.deleteIfExists(Paths.get(filePath + fileName));

                LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler Deleting file");
            }
        } else {
            LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler config param data missing");
        }

        LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler end successfully");
    } catch (IOException e) {
        LOGGER.error(String.format("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] IOException while running Scheduler %s", e.getMessage()));
    } catch (Exception e) {
        LOGGER.error(String.format("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Exception while running Scheduler %s", e.getMessage()));
    } finally {
        LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler Completed");
    }
}
Leave a Comment