Subversion Repositories SmartDukaan

Rev

Rev 24512 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.view.xls;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import com.spice.profitmandi.common.util.FormattingUtils;
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;

import in.shop2020.model.v1.order.WalletReferenceType;

@Component("walletStatement")
public class WalletStatementView extends AbstractXlsxView {

        @Override
        protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
                // TODO Auto-generated method stub
              List<UserWalletHistory> history = (List<UserWalletHistory>) model.get("history");
              LocalDateTime startDate = (LocalDateTime)model.get("startDate");
              LocalDateTime endDate = (LocalDateTime)model.get("endDate");
              Float opening = (float)model.get("opening");
              Float closing = (float)model.get("closing");
              Sheet sheet = workbook.createSheet("Today Currency Rates");
              UserWalletHistory uwhOpening = getOpeningEntry(opening, startDate);
              UserWalletHistory closingEntry = getClosingEntry(closing, endDate);
              history.add(0, uwhOpening);
              history.add(closingEntry);

              
              sheet.setFitToPage(true);

              int rowCount = 0;
              Row header = sheet.createRow(rowCount++);
              header.createCell(0).setCellValue("Date");
              header.createCell(1).setCellValue("Transact Type");
              header.createCell(2).setCellValue("Transact Reference");
              header.createCell(3).setCellValue("Transact Description");
              header.createCell(4).setCellValue("Amount Dr");
              header.createCell(5).setCellValue("Amount Cr");
              header.createCell(6).setCellValue("Balance");
              int calculatedClosing = 0;
              for (UserWalletHistory walletEntry : history) {
                  Row walletEntryRow = sheet.createRow(rowCount++);
                  walletEntryRow.createCell(0).setCellValue(walletEntry.getFormattedDate());
                  if(walletEntry.getReferenceType()==null) {
                          walletEntryRow.createCell(1).setCellValue("Closing Balance");
                          walletEntryRow.createCell(6).setCellValue(walletEntry.getAmount());
                  } else {
                          walletEntryRow.createCell(1).setCellValue(walletEntry.getReferenceType().name());
                  }
                  walletEntryRow.createCell(2).setCellValue(walletEntry.getReference());
                  walletEntryRow.createCell(3).setCellValue(walletEntry.getDescription());
                  if(walletEntry.getAmount() < 0) {
                          walletEntryRow.createCell(4).setCellValue(-walletEntry.getAmount());
                          walletEntryRow.createCell(5).setCellValue("");
                          
                  } else if (walletEntry.getAmount() > 0) {
                          walletEntryRow.createCell(4).setCellValue("");
                          walletEntryRow.createCell(5).setCellValue(walletEntry.getAmount());
                  }
                  calculatedClosing = calculatedClosing + walletEntry.getAmount();
                  walletEntryRow.createCell(6).setCellValue(calculatedClosing);
              }
              String fileName = "wallet-" + FormattingUtils.formatDate(startDate) + " " + FormattingUtils.formatDate(endDate) + ".xlsx";
              response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

        }

        private UserWalletHistory getClosingEntry(Float closing, LocalDateTime endDate) {
                UserWalletHistory uwh = new UserWalletHistory();
                uwh.setAmount((int)closing.floatValue());
                uwh.setDescription("Closing balance");
                uwh.setReferenceType(null);
                uwh.setTimestamp(endDate);
                uwh.setReference(0);
                return uwh;
        }

        private UserWalletHistory getOpeningEntry(Float opening, LocalDateTime startDate) {
                UserWalletHistory uwh = new UserWalletHistory();
                uwh.setAmount((int)opening.floatValue());
                uwh.setDescription("Opening Balance Credit");
                uwh.setReferenceType(WalletReferenceType.OPENING_BALANCE);
                uwh.setTimestamp(startDate);
                uwh.setReference(0);
                return uwh;
        }
        

}