Subversion Repositories SmartDukaan

Rev

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

Rev 33207 Rev 33212
Line 604... Line 604...
604
        Map<Integer, Integer> walletRetailerMap = retailerWalletMap.entrySet().stream().collect(Collectors.toMap(x -> x.getValue().getId(), x -> x.getKey()));
604
        Map<Integer, Integer> walletRetailerMap = retailerWalletMap.entrySet().stream().collect(Collectors.toMap(x -> x.getValue().getId(), x -> x.getKey()));
605
 
605
 
606
        Set<Integer> walletSet = walletRetailerMap.keySet();
606
        Set<Integer> walletSet = walletRetailerMap.keySet();
607
        Map<Integer, Float> closingBalanceMap = userWalletHistoryRepository.getSumTillDateExcludingPurchase(closingDateTime, walletSet);
607
        Map<Integer, Float> closingBalanceMap = userWalletHistoryRepository.getSumTillDateExcludingPurchase(closingDateTime, walletSet);
608
        Map<Integer, Float> closingWalletMap = userWalletHistoryRepository.getSumTillDate(closingDateTime, walletSet);
608
        Map<Integer, Float> closingWalletMap = userWalletHistoryRepository.getSumTillDate(closingDateTime, walletSet);
609
        Map<Integer, Float> peindingIndentMap = transactionService.getPendingIndentValueMap();
609
        Map<Integer, Float> peindingIndentMap = transactionService.getPendingIndentValueMap(closingDateTime, Optional.empty());
610
 
610
 
611
        List<List<?>> rows = new ArrayList<>();
611
        List<List<?>> rows = new ArrayList<>();
612
        for (Map.Entry<Integer, Float> closingBalance : closingBalanceMap.entrySet()) {
612
        for (Map.Entry<Integer, Float> closingBalance : closingBalanceMap.entrySet()) {
613
            int walletId = closingBalance.getKey();
613
            int walletId = closingBalance.getKey();
614
            int retailerId = walletRetailerMap.get(walletId);
614
            int retailerId = walletRetailerMap.get(walletId);
Line 691... Line 691...
691
                throw new ProfitMandiBusinessException("Unauthorised access", "PartnerId", "Permission Denied");
691
                throw new ProfitMandiBusinessException("Unauthorised access", "PartnerId", "Permission Denied");
692
            }
692
            }
693
        } else {
693
        } else {
694
            fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
694
            fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
695
        }
695
        }
696
        float openingBalance = walletService.getOpeningTillExcludingPurchase(fofoId, startDate);
696
        float openingBalance = walletService.getOpeningTill(fofoId, startDate);
697
        float closingBalance = walletService.getOpeningTillExcludingPurchase(fofoId, endDate);
697
        float closingBalance = walletService.getOpeningTill(fofoId, endDate);
698
        UserWallet uw = walletService.getUserWallet(fofoId);
698
        UserWallet uw = walletService.getUserWallet(fofoId);
699
        List<UserWalletHistory> history = userWalletHistoryRepository.selectPaginatedByWalletId(uw.getId(), startDate, endDate, 0, 0);
699
        List<UserWalletHistory> history = userWalletHistoryRepository.selectPaginatedByWalletId(uw.getId(), startDate, endDate, 0, 0);
700
        history = history.stream().filter(x -> !x.getReferenceType().equals(WalletReferenceType.PURCHASE))
700
        history = history.stream().filter(x -> !x.getReferenceType().equals(WalletReferenceType.PURCHASE))
701
                .filter(x -> !WalletService.CN_WALLET_REFERENCES.contains(x.getReferenceType())).sorted(Comparator.comparing(UserWalletHistory::getId)).collect(Collectors.toList());
701
                .filter(x -> !WalletService.CN_WALLET_REFERENCES.contains(x.getReferenceType())).sorted(Comparator.comparing(UserWalletHistory::getId)).collect(Collectors.toList());
702
 
702
 
703
        InputStream is = getClass().getClassLoader().getResourceAsStream("account-statement.xlsx");
703
        InputStream is = getClass().getClassLoader().getResourceAsStream("account-statement.xlsx");
704
        CustomRetailer customRetailer = retailerService.getAllFofoRetailers().get(fofoId);
704
        CustomRetailer customRetailer = retailerService.getAllFofoRetailers().get(fofoId);
705
        List<StatementDetailModel> details = orderRepository.selectDetailsBetween(fofoId, startDate, endDate);
705
        List<StatementDetailModel> details = orderRepository.selectDetailsBetween(fofoId, startDate, endDate);
-
 
706
        Map<Integer, Float> openingAmountMap = transactionService.getPendingIndentValueMap(startDate, Optional.of(fofoId));
-
 
707
        Map<Integer, Float> closingAmountMap = transactionService.getPendingIndentValueMap(endDate, Optional.of(fofoId));
-
 
708
 
706
        float openingAmount = orderRepository.selectOpeningAmount(fofoId, startDate);
709
        float openingPendingAmount = openingAmountMap.get(fofoId) == null ? 0 : openingAmountMap.get(fofoId);
707
        float closingAmount = orderRepository.selectOpeningAmount(fofoId, endDate);
710
        float closingPendingAmount = closingAmountMap.get(fofoId) == null ? 0 : closingAmountMap.get(fofoId);
708
        LOGGER.info("Opening - {}, Closing - {}", openingAmount, closingAmount);
711
        LOGGER.info("Opening - {}, Closing - {}", openingPendingAmount, closingPendingAmount);
709
        ByteArrayOutputStream byteArrayOutputStream = this.populateData(is, openingBalance - openingAmount, closingBalance - closingAmount, customRetailer, history, startDate, endDate, details);
712
        ByteArrayOutputStream byteArrayOutputStream = this.populateData(is, openingBalance + openingPendingAmount, closingBalance + closingPendingAmount,
-
 
713
                customRetailer, history, startDate, endDate, details);
710
 
714
 
711
        final HttpHeaders headers = new HttpHeaders();
715
        final HttpHeaders headers = new HttpHeaders();
712
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
716
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
713
        headers.set("Content-disposition", "inline; filename=account-statement." + StringUtils.toHyphenatedString(startDate.toLocalDate()) + "-" + StringUtils.toHyphenatedString(endDate.toLocalDate()) + ".xlsx");
717
        headers.set("Content-disposition", "inline; filename=account-statement." + StringUtils.toHyphenatedString(startDate.toLocalDate()) + "-" + StringUtils.toHyphenatedString(endDate.toLocalDate()) + ".xlsx");
714
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
718
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
Line 727... Line 731...
727
 
731
 
728
    private ByteArrayOutputStream populateData(InputStream is, float openingBalance, float closingBalance, CustomRetailer customRetailer, List<UserWalletHistory> history,
732
    private ByteArrayOutputStream populateData(InputStream is, float openingBalance, float closingBalance, CustomRetailer customRetailer, List<UserWalletHistory> history,
729
                                               LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime, List<StatementDetailModel> invoiceDetails) throws Exception {
733
                                               LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime, List<StatementDetailModel> invoiceDetails) throws Exception {
730
        Map<LocalDate, List<StatementDetailModel>> dateInvoiceMap = invoiceDetails.stream().collect(Collectors.groupingBy(x -> x.getOnDate().toLocalDate()));
734
        Map<LocalDate, List<StatementDetailModel>> dateInvoiceMap = invoiceDetails.stream().collect(Collectors.groupingBy(x -> x.getOnDate().toLocalDate()));
731
        LOGGER.info("dateInvoiceMap - {}", dateInvoiceMap);
735
        LOGGER.info("dateInvoiceMap - {}", dateInvoiceMap);
-
 
736
        Map<LocalDate, List<UserWalletHistory>> dateWalletMap = history.stream().collect(Collectors.groupingBy(x -> x.getBusinessTimestamp().toLocalDate()));
732
        XSSFWorkbook workbook = new XSSFWorkbook(is);
737
        XSSFWorkbook workbook = new XSSFWorkbook(is);
733
        CreationHelper creationHelper = workbook.getCreationHelper();
738
        CreationHelper creationHelper = workbook.getCreationHelper();
734
        CellStyle style2 = workbook.createCellStyle();
739
        CellStyle style2 = workbook.createCellStyle();
735
        style2.setDataFormat(creationHelper.createDataFormat().getFormat("dd/mm/yyyy"));
740
        style2.setDataFormat(creationHelper.createDataFormat().getFormat("dd/mm/yyyy"));
736
 
741
 
Line 741... Line 746...
741
        sheet.getRow(1).getCell(2).setCellValue(new Date(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
746
        sheet.getRow(1).getCell(2).setCellValue(new Date(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
742
        sheet.getRow(2).getCell(2).setCellValue(openingBalance);
747
        sheet.getRow(2).getCell(2).setCellValue(openingBalance);
743
        long grandTotalDebit = 0l;
748
        long grandTotalDebit = 0l;
744
        long grandTotalCredit = 0l;
749
        long grandTotalCredit = 0l;
745
        int row = 4;
750
        int row = 4;
746
        LocalDate currentLocalDate = startLocalDateTime.toLocalDate();
751
        LocalDate startDate = startLocalDateTime.toLocalDate();
747
        LocalDate endDate = endLocalDateTime.toLocalDate();
752
        LocalDate endDate = endLocalDateTime.toLocalDate();
-
 
753
        for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
-
 
754
            List<UserWalletHistory> datewiseWalletHistory = dateWalletMap.get(date);
-
 
755
            if (datewiseWalletHistory != null) {
748
        for (UserWalletHistory walletEntry : history) {
756
                for (UserWalletHistory walletEntry : datewiseWalletHistory) {
-
 
757
                    Cell dateCell = sheet.createRow(row).createCell(0);
-
 
758
                    dateCell.setCellValue(new Date(walletEntry.getTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
-
 
759
                    dateCell.setCellStyle(style2);
-
 
760
                    sheet.getRow(row).createCell(1).setCellValue(walletEntry.getReferenceType().toString());
-
 
761
                    if (walletEntry.getAmount() > 0) {
-
 
762
                        sheet.getRow(row).createCell(2).setCellValue(0);
-
 
763
                        sheet.getRow(row).createCell(3).setCellValue(walletEntry.getAmount());
-
 
764
                        grandTotalCredit += walletEntry.getAmount();
-
 
765
                    } else {
-
 
766
                        sheet.getRow(row).createCell(2).setCellValue(-walletEntry.getAmount());
-
 
767
                        sheet.getRow(row).createCell(3).setCellValue(0);
749
            while (!currentLocalDate.equals(walletEntry.getTimestamp().toLocalDate())) {
768
                        grandTotalDebit -= walletEntry.getAmount();
-
 
769
                    }
-
 
770
                    sheet.getRow(row).createCell(4).setCellValue(walletEntry.getDescription());
-
 
771
                    sheet.getRow(row).createCell(5).setCellValue(walletEntry.getReference());
-
 
772
                    row++;
-
 
773
                }
-
 
774
            }
750
                List<StatementDetailModel> statementDetailModels = dateInvoiceMap.get(currentLocalDate);
775
            List<StatementDetailModel> statementDetailModels = dateInvoiceMap.get(date);
751
                LOGGER.info("statementDetailModels - {}", statementDetailModels);
776
            LOGGER.info("statementDetailModels - {}", statementDetailModels);
752
                if (statementDetailModels != null) {
777
            if (statementDetailModels != null) {
753
                    for (StatementDetailModel statementDetailModel : statementDetailModels) {
778
                for (StatementDetailModel statementDetailModel : statementDetailModels) {
-
 
779
                    Cell dateCell = sheet.createRow(row).createCell(0);
-
 
780
                    dateCell.setCellValue(statementDetailModel.getOnDate());
-
 
781
                    dateCell.setCellStyle(style2);
-
 
782
                    sheet.getRow(row).createCell(1).setCellValue("BILLING");
-
 
783
                    if (statementDetailModel.getAmount() > 0) {
-
 
784
                        sheet.getRow(row).createCell(2).setCellValue(statementDetailModel.getAmount());
-
 
785
                        sheet.getRow(row).createCell(3).setCellValue(0);
-
 
786
                        grandTotalDebit += statementDetailModel.getAmount();
-
 
787
                    } else {
-
 
788
                        sheet.getRow(row).createCell(2).setCellValue(0);
-
 
789
                        sheet.getRow(row).createCell(3).setCellValue(-statementDetailModel.getAmount());
-
 
790
                        grandTotalCredit += -statementDetailModel.getAmount();
-
 
791
                    }
-
 
792
 
-
 
793
                    sheet.getRow(row).createCell(4).setCellValue(statementDetailModel.getReferenceType());
-
 
794
                    sheet.getRow(row).createCell(5).setCellValue(statementDetailModel.getInvoiceNumber());
-
 
795
 
-
 
796
                    row += 1;
-
 
797
                }
-
 
798
            }
-
 
799
            if (YearMonth.from(date).atEndOfMonth().equals(date)) {
-
 
800
                List<CreditNote> creditNotes = creditNoteRepository.selectAll(customRetailer.getPartnerId(), YearMonth.from(date));
-
 
801
                if (creditNotes != null) {
-
 
802
                    for (CreditNote creditNote : creditNotes) {
-
 
803
                        double cnAmount = creditNoteLineRepository.selectAllByCreditNote(creditNote.getId()).stream().collect(Collectors.summingDouble(x -> x.getAmount()));
754
                        Cell dateCell = sheet.createRow(row).createCell(0);
804
                        Cell dateCell = sheet.createRow(row).createCell(0);
755
                        dateCell.setCellValue(statementDetailModel.getOnDate());
805
                        dateCell.setCellValue(new Date(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
756
                        dateCell.setCellStyle(style2);
806
                        dateCell.setCellStyle(style2);
757
                        sheet.getRow(row).createCell(1).setCellValue("BILLING");
807
                        sheet.getRow(row).createCell(1).setCellValue("CREDIT NOTE");
758
                        if (statementDetailModel.getAmount() > 0) {
-
 
759
                            sheet.getRow(row).createCell(2).setCellValue(statementDetailModel.getAmount());
-
 
760
                            sheet.getRow(row).createCell(3).setCellValue(0);
-
 
761
                            grandTotalDebit += statementDetailModel.getAmount();
-
 
762
                        } else {
808
                        if (cnAmount > 0) {
763
                            sheet.getRow(row).createCell(2).setCellValue(0);
809
                            sheet.getRow(row).createCell(2).setCellValue(0);
764
                            sheet.getRow(row).createCell(3).setCellValue(-statementDetailModel.getAmount());
810
                            sheet.getRow(row).createCell(3).setCellValue(cnAmount);
765
                            grandTotalCredit += -statementDetailModel.getAmount();
811
                            grandTotalCredit += cnAmount;
-
 
812
                        } else {
-
 
813
                            sheet.getRow(row).createCell(2).setCellValue(-cnAmount);
-
 
814
                            sheet.getRow(row).createCell(3).setCellValue(0);
-
 
815
                            grandTotalDebit += -cnAmount;
766
                        }
816
                        }
767
 
817
 
768
                        sheet.getRow(row).createCell(4).setCellValue(statementDetailModel.getReferenceType());
818
                        sheet.getRow(row).createCell(4).setCellValue("Credit Note Issued");
769
                        sheet.getRow(row).createCell(5).setCellValue(statementDetailModel.getInvoiceNumber());
819
                        sheet.getRow(row).createCell(5).setCellValue(creditNote.getCreditNoteNumber());
770
 
820
 
771
                        row += 1;
821
                        row += 1;
772
                    }
822
                    }
773
                }
823
                }
774
                if (YearMonth.from(currentLocalDate).atEndOfMonth().equals(currentLocalDate)) {
-
 
775
                    List<CreditNote> creditNotes = creditNoteRepository.selectAll(customRetailer.getPartnerId(), YearMonth.from(currentLocalDate));
-
 
776
                    if (creditNotes != null) {
-
 
777
                        for (CreditNote creditNote : creditNotes) {
-
 
778
                            double cnAmount = creditNoteLineRepository.selectAllByCreditNote(creditNote.getId()).stream().collect(Collectors.summingDouble(x -> x.getAmount()));
-
 
779
                            Cell dateCell = sheet.createRow(row).createCell(0);
-
 
780
                            dateCell.setCellValue(new Date(currentLocalDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
-
 
781
                            dateCell.setCellStyle(style2);
-
 
782
                            sheet.getRow(row).createCell(1).setCellValue("CREDIT NOTE");
-
 
783
                            if (cnAmount > 0) {
-
 
784
                                sheet.getRow(row).createCell(2).setCellValue(0);
-
 
785
                                sheet.getRow(row).createCell(3).setCellValue(cnAmount);
-
 
786
                                grandTotalCredit += cnAmount;
-
 
787
                            } else {
-
 
788
                                sheet.getRow(row).createCell(2).setCellValue(-cnAmount);
-
 
789
                                sheet.getRow(row).createCell(3).setCellValue(0);
-
 
790
                                grandTotalDebit += -cnAmount;
-
 
791
                            }
-
 
792
 
-
 
793
                            sheet.getRow(row).createCell(4).setCellValue("Credit Note Issued");
-
 
794
                            sheet.getRow(row).createCell(5).setCellValue(creditNote.getCreditNoteNumber());
-
 
795
 
-
 
796
                            row += 1;
-
 
797
                        }
-
 
798
                    }
-
 
799
                }
-
 
800
                currentLocalDate = currentLocalDate.plusDays(1);
-
 
801
            }
-
 
802
            Cell dateCell = sheet.createRow(row).createCell(0);
-
 
803
            dateCell.setCellValue(new Date(walletEntry.getTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
-
 
804
            dateCell.setCellStyle(style2);
-
 
805
            sheet.getRow(row).createCell(1).setCellValue(walletEntry.getReferenceType().toString());
-
 
806
            if (walletEntry.getAmount() > 0) {
-
 
807
                sheet.getRow(row).createCell(2).setCellValue(0);
-
 
808
                sheet.getRow(row).createCell(3).setCellValue(walletEntry.getAmount());
-
 
809
                grandTotalCredit += walletEntry.getAmount();
-
 
810
            } else {
-
 
811
                sheet.getRow(row).createCell(2).setCellValue(-walletEntry.getAmount());
-
 
812
                sheet.getRow(row).createCell(3).setCellValue(0);
-
 
813
                grandTotalDebit -= walletEntry.getAmount();
-
 
814
            }
-
 
815
            sheet.getRow(row).createCell(4).setCellValue(walletEntry.getDescription());
-
 
816
            sheet.getRow(row).createCell(5).setCellValue(walletEntry.getReference());
-
 
817
            row++;
-
 
818
        }
-
 
819
        List<StatementDetailModel> statementDetailModels = dateInvoiceMap.get(endDate);
-
 
820
        if (statementDetailModels != null) {
-
 
821
            for (StatementDetailModel statementDetailModel : statementDetailModels) {
-
 
822
                Cell dateCell = sheet.createRow(row).createCell(0);
-
 
823
                dateCell.setCellValue(statementDetailModel.getOnDate());
-
 
824
                dateCell.setCellStyle(style2);
-
 
825
                sheet.getRow(row).createCell(1).setCellValue("BILLING");
-
 
826
                if (statementDetailModel.getAmount() > 0) {
-
 
827
                    sheet.getRow(row).createCell(2).setCellValue(statementDetailModel.getAmount());
-
 
828
                    sheet.getRow(row).createCell(3).setCellValue(0);
-
 
829
                    grandTotalDebit += statementDetailModel.getAmount();
-
 
830
                } else {
-
 
831
                    sheet.getRow(row).createCell(2).setCellValue(0);
-
 
832
                    sheet.getRow(row).createCell(3).setCellValue(-statementDetailModel.getAmount());
-
 
833
                    grandTotalCredit += -statementDetailModel.getAmount();
-
 
834
                }
-
 
835
 
-
 
836
                sheet.getRow(row).createCell(4).setCellValue(statementDetailModel.getReferenceType());
-
 
837
                sheet.getRow(row).createCell(5).setCellValue(statementDetailModel.getInvoiceNumber());
-
 
838
 
-
 
839
                row += 1;
-
 
840
            }
824
            }
841
        }
825
        }
842
        List<CreditNote> creditNotes = creditNoteRepository.selectAll(customRetailer.getPartnerId(), YearMonth.from(endDate));
-
 
843
        if (creditNotes != null) {
-
 
844
            for (CreditNote creditNote : creditNotes) {
-
 
845
                double cnAmount = creditNoteLineRepository.selectAllByCreditNote(creditNote.getId()).stream().collect(Collectors.summingDouble(x -> x.getAmount()));
-
 
846
                Cell dateCell = sheet.createRow(row).createCell(0);
-
 
847
                dateCell.setCellValue(new Date(currentLocalDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
-
 
848
                dateCell.setCellStyle(style2);
-
 
849
                sheet.getRow(row).createCell(1).setCellValue("CREDIT NOTE");
-
 
850
                if (cnAmount > 0) {
-
 
851
                    sheet.getRow(row).createCell(2).setCellValue(0);
-
 
852
                    sheet.getRow(row).createCell(3).setCellValue(cnAmount);
-
 
853
                    grandTotalCredit += cnAmount;
-
 
854
                } else {
-
 
855
                    sheet.getRow(row).createCell(2).setCellValue(-cnAmount);
-
 
856
                    sheet.getRow(row).createCell(3).setCellValue(0);
-
 
857
                    grandTotalDebit += -cnAmount;
-
 
858
                }
-
 
859
 
826
 
860
                sheet.getRow(row).createCell(4).setCellValue("Credit Note Issued");
-
 
861
                sheet.getRow(row).createCell(5).setCellValue(creditNote.getCreditNoteNumber());
-
 
862
 
-
 
863
                row += 1;
-
 
864
            }
-
 
865
        }
-
 
866
        sheet.createRow(row).createCell(0).setCellValue("Grand Total");
827
        sheet.createRow(row).createCell(0).setCellValue("Grand Total");
867
        sheet.getRow(row).createCell(2).setCellValue(grandTotalDebit);
828
        sheet.getRow(row).createCell(2).setCellValue(grandTotalDebit);
868
        sheet.getRow(row).createCell(3).setCellValue(grandTotalCredit);
829
        sheet.getRow(row).createCell(3).setCellValue(grandTotalCredit);
869
        row += 2;
830
        row += 2;
870
        sheet.createRow(row).createCell(0).setCellValue("Closing Balance");
831
        sheet.createRow(row).createCell(0).setCellValue("Closing Balance");