Subversion Repositories SmartDukaan

Rev

Rev 24508 | Rev 24521 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24508 amit.gupta 1
package com.spice.profitmandi.web.view.xls;
2
 
3
import java.time.LocalDateTime;
4
import java.util.List;
5
import java.util.Map;
6
 
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
 
10
import org.apache.poi.ss.usermodel.Row;
11
import org.apache.poi.ss.usermodel.Sheet;
12
import org.apache.poi.ss.usermodel.Workbook;
13
import org.springframework.stereotype.Component;
14
import org.springframework.web.servlet.view.document.AbstractXlsxView;
15
 
16
import com.spice.profitmandi.common.util.FormattingUtils;
17
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
18
 
19
import in.shop2020.model.v1.order.WalletReferenceType;
20
 
21
@Component("walletStatement")
22
public class WalletStatementView extends AbstractXlsxView {
23
 
24
	@Override
25
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
26
			HttpServletResponse response) throws Exception {
27
		// TODO Auto-generated method stub
28
	      List<UserWalletHistory> history = (List<UserWalletHistory>) model.get("history");
29
	      LocalDateTime startDate = (LocalDateTime)model.get("startDate");
30
	      LocalDateTime endDate = (LocalDateTime)model.get("endDate");
31
	      Float opening = (float)model.get("opening");
32
	      Float closing = (float)model.get("closing");
33
	      Sheet sheet = workbook.createSheet("Today Currency Rates");
34
	      UserWalletHistory uwhOpening = getOpeningEntry(opening, startDate);
35
	      UserWalletHistory closingEntry = getClosingEntry(closing, endDate);
36
	      history.add(0, uwhOpening);
24512 amit.gupta 37
	      //history.add(closingEntry);
24508 amit.gupta 38
 
39
 
40
	      sheet.setFitToPage(true);
41
 
42
	      int rowCount = 0;
43
	      Row header = sheet.createRow(rowCount++);
44
	      header.createCell(0).setCellValue("Date");
45
	      header.createCell(1).setCellValue("Transact Type");
46
	      header.createCell(2).setCellValue("Transact Reference");
47
	      header.createCell(3).setCellValue("Transact Description");
48
	      header.createCell(4).setCellValue("Amount Dr");
49
	      header.createCell(5).setCellValue("Amount Cr");
50
	      header.createCell(6).setCellValue("Balance");
51
	      int calculatedClosing = 0;
52
	      for (UserWalletHistory walletEntry : history) {
53
	          Row walletEntryRow = sheet.createRow(rowCount++);
54
	          walletEntryRow.createCell(0).setCellValue(walletEntry.getFormattedDate());
55
	          if(walletEntry.getReferenceType()==null) {
56
	        	  walletEntryRow.createCell(1).setCellValue("Closing Balance");
57
	        	  walletEntryRow.createCell(6).setCellValue(walletEntry.getAmount());
58
	          } else {
59
	        	  walletEntryRow.createCell(1).setCellValue(walletEntry.getReferenceType().name());
60
	          }
61
	          walletEntryRow.createCell(2).setCellValue(walletEntry.getReference());
62
	          walletEntryRow.createCell(3).setCellValue(walletEntry.getDescription());
63
	          if(walletEntry.getAmount() < 0) {
64
	        	  walletEntryRow.createCell(4).setCellValue(-walletEntry.getAmount());
65
	        	  walletEntryRow.createCell(5).setCellValue("");
66
 
67
	          } else if (walletEntry.getAmount() > 0) {
68
	        	  walletEntryRow.createCell(4).setCellValue("");
69
	        	  walletEntryRow.createCell(5).setCellValue(walletEntry.getAmount());
70
	          }
71
	          calculatedClosing = calculatedClosing + walletEntry.getAmount();
72
	          walletEntryRow.createCell(6).setCellValue(calculatedClosing);
73
	      }
74
	      String fileName = "wallet-" + FormattingUtils.formatDate(startDate) + " " + FormattingUtils.formatDate(endDate) + ".xlsx";
75
	      response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
76
 
77
	}
78
 
79
	private UserWalletHistory getClosingEntry(Float closing, LocalDateTime endDate) {
80
		UserWalletHistory uwh = new UserWalletHistory();
24512 amit.gupta 81
		uwh.setAmount(0);
82
		uwh.setDescription("Final Closing balance");
24508 amit.gupta 83
		uwh.setReferenceType(null);
84
		uwh.setTimestamp(endDate);
85
		uwh.setReference(0);
86
		return uwh;
87
	}
88
 
89
	private UserWalletHistory getOpeningEntry(Float opening, LocalDateTime startDate) {
90
		UserWalletHistory uwh = new UserWalletHistory();
91
		uwh.setAmount((int)opening.floatValue());
92
		uwh.setDescription("Opening Balance Credit");
93
		uwh.setReferenceType(WalletReferenceType.OPENING_BALANCE);
94
		uwh.setTimestamp(startDate);
95
		uwh.setReference(0);
96
		return uwh;
97
	}
98
 
99
 
100
}