Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
992 varun.gupt 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.logistics.LogisticsServiceException;
4
import in.shop2020.logistics.Provider;
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
6
import in.shop2020.model.v1.catalog.Warehouse;
7
import in.shop2020.model.v1.order.LineItem;
8
import in.shop2020.model.v1.order.Order;
9
import in.shop2020.model.v1.order.OrderStatus;
10
import in.shop2020.model.v1.order.TransactionServiceException;
11
import in.shop2020.model.v1.user.ShoppingCartException;
12
import in.shop2020.thrift.clients.CatalogServiceClient;
13
import in.shop2020.thrift.clients.LogisticsServiceClient;
14
import in.shop2020.thrift.clients.TransactionServiceClient;
15
 
16
import java.text.DateFormat;
17
import java.text.SimpleDateFormat;
18
import java.text.ParseException;
19
 
20
import java.io.ByteArrayOutputStream;
21
import java.io.File;
22
import java.io.FileInputStream;
23
import java.io.IOException;
24
import java.util.ArrayList;
25
import java.util.Calendar;
26
import java.util.Date;
27
import java.util.GregorianCalendar;
28
import java.util.HashMap;
29
import java.util.List;
30
import java.util.Map;
31
 
32
import javassist.expr.NewArray;
33
 
34
import javax.servlet.ServletOutputStream;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpSession;
38
 
39
import org.apache.poi.hssf.usermodel.HSSFCell;
40
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
41
import org.apache.poi.ss.usermodel.Cell;
42
import org.apache.poi.ss.usermodel.CellStyle;
43
import org.apache.poi.ss.usermodel.CreationHelper;
44
import org.apache.poi.ss.usermodel.Font;
45
import org.apache.poi.ss.usermodel.Row;
46
import org.apache.poi.ss.usermodel.Sheet;
47
import org.apache.poi.ss.usermodel.Workbook;
48
import org.apache.poi.ss.util.CellRangeAddress;
49
import org.apache.struts2.interceptor.ServletRequestAware;
50
import org.apache.struts2.interceptor.ServletResponseAware;
51
import org.apache.struts2.rest.DefaultHttpHeaders;
52
import org.apache.struts2.rest.HttpHeaders;
53
import org.apache.thrift.TException;
54
 
55
/**
56
 * 
57
 * @author Varun Gupta
58
 * @version 1.0
59
 * @description HotspotReconciliationController handles requests to generate a reconciliation
60
 * report in XLS format for all the transactions with HotSpot within a given date range.
61
 * 
62
 */
63
public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware {
64
 
65
	private String timestamp;
66
	private String hotspotReconciliationReportPath = "/HotspotReports";
67
 
68
	private HttpServletRequest request;
69
	private HttpServletResponse response;
70
 
71
	private String id;
72
 
73
	public HotspotReconciliationController(){
74
 
75
	}
76
 
77
	public HttpHeaders index()	{
78
		return new DefaultHttpHeaders("report");
79
	}
80
 
81
	public HttpHeaders show(){
82
		return new DefaultHttpHeaders("report");
83
	}
84
 
85
	// Handles the POST request (Form Submission)
86
	public HttpHeaders create(){
87
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
88
		Date startDate = null;
89
		Date endDate = null;
90
 
91
		try	{
92
			//Formatting Form input parameters
93
			startDate = dateFormat.parse(request.getParameter("start"));
94
			endDate = dateFormat.parse(request.getParameter("end"));
95
 
96
			CatalogServiceClient csc = new CatalogServiceClient();
97
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
98
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
99
 
100
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
101
			in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();
102
 
103
			List <Order> orders = new ArrayList<Order>();
104
 
105
			//Retrieving all the orders across all the warehouses
106
			for(Warehouse warehouse : warehouses)	{
1066 varun.gupt 107
				orders.addAll(client.getOrdersByBillingDate(OrderStatus.BILLED, startDate.getTime(), endDate.getTime(), warehouse.getId()));
992 varun.gupt 108
			}
1066 varun.gupt 109
			System.out.println("Total number of Orders: " + orders.size());
992 varun.gupt 110
 
111
			// Preparing XLS file for output
112
			response.setContentType("application/vnd.ms-excel");
113
 
114
			Calendar date = new GregorianCalendar();
115
			int year = date.get(Calendar.YEAR);
116
			int month = date.get(Calendar.MONTH) +1;
117
			int day = date.get(Calendar.DAY_OF_MONTH);
118
 
119
			response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-" + "-" + year + "-" + month + "-" + day + ".xls");
120
 
121
			ServletOutputStream sos;
122
			try {
123
				ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);
124
				sos = response.getOutputStream();
125
				baos.writeTo(sos);
126
				sos.flush();
127
			} catch (IOException e) {
128
				e.printStackTrace();
129
			}
130
 
131
		} catch (ParseException e)	{
132
			e.printStackTrace();
133
		} catch (TransactionServiceException e)	{
134
			e.printStackTrace();
135
		} catch (Exception e)	{
136
			e.printStackTrace();
137
		} finally	{
138
			System.out.println(startDate.getTime() + "  |  " + endDate.getTime());
139
		}
140
		return new DefaultHttpHeaders("report");
141
	}
142
 
143
	// Prepares the XLS worksheet object and fills in the data with proper formatting
144
	private ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate)	{
145
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
146
 
147
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
148
 
149
	    Workbook wb = new HSSFWorkbook();
150
	    CreationHelper createHelper = wb.getCreationHelper();
151
	    Sheet sheet = wb.createSheet("new sheet");
152
	    short serialNo = 0;
153
 
154
	    // Create the header row and put all the titles in it. Rows are 0 based.
155
	    Font font = wb.createFont();
156
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
157
	    CellStyle style = wb.createCellStyle();
158
	    style.setFont(font);
159
 
160
	    Row titleRow = sheet.createRow(serialNo ++);
161
	    Cell titleCell = titleRow.createCell(0);
162
	    titleCell.setCellValue("HotSpot Reconciliation Report (" + dateFormat.format(startDate) + " - " + dateFormat.format(endDate) + ")");
163
	    titleCell.setCellStyle(style);
164
 
165
	    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
166
 
167
	    sheet.createRow(serialNo ++);
168
 
169
	    Row headerRow = sheet.createRow(serialNo ++);
170
	    headerRow.createCell(0).setCellValue("Billing Number");
171
	    headerRow.createCell(1).setCellValue("Billing Date");
172
	    headerRow.createCell(2).setCellValue("Brand");
173
	    headerRow.createCell(3).setCellValue("Model Name");
174
	    headerRow.createCell(4).setCellValue("Model Number");
175
	    headerRow.createCell(5).setCellValue("Color");
176
	    headerRow.createCell(6).setCellValue("Transfer Price");
177
 
178
	    double totalTransferPrice = 0.0;
179
 
180
	    for(int i = 0; i < orders.size(); i ++)	{
181
	    	Order order = orders.get(i);
182
			serialNo ++;
183
	    	Row contentRow = sheet.createRow(serialNo);
184
 
185
		    LineItem lineItem = order.getLineitems().get(0);
186
		    double transferPrice = lineItem.getTransfer_price();
187
		    totalTransferPrice += transferPrice;
188
 
189
		    contentRow.createCell(0).setCellValue(order.getInvoice_number());
190
		    contentRow.createCell(1).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
191
		    contentRow.createCell(2).setCellValue(getValueForEmptyString(lineItem.getBrand()));
192
		    contentRow.createCell(3).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
193
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
194
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(lineItem.getColor()));
195
		    contentRow.createCell(6).setCellValue(transferPrice);
196
	    }
197
	    sheet.createRow(serialNo ++);
198
    	Row contentRow = sheet.createRow(serialNo);
199
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
200
    	contentRow.createCell(6).setCellValue(totalTransferPrice);
201
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
202
 
203
		// Write the workbook to the output stream
204
		try {
205
			wb.write(baosXLS);
206
			baosXLS.close();
207
		} catch (IOException e) {
208
			e.printStackTrace();
209
		}		
210
		return baosXLS;
211
	}
212
 
213
	public String getId(){
214
		return id;
215
	}
216
 
217
	public void setId(String id){
218
		this.id = id;
219
	}
220
 
221
	@Override
222
	public void setServletRequest(HttpServletRequest request) {
223
		this.request = request;
224
	}
225
 
226
	@Override
227
	public void setServletResponse(HttpServletResponse response) {
228
		this.response  = response;
229
	}
230
 
231
	private String getValueForEmptyString(String s){
232
		if(s==null || s.equals(""))
233
			return "-";
234
		else
235
			return s; 
236
	}
237
}