Subversion Repositories SmartDukaan

Rev

Rev 1066 | Rev 1884 | 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;
1075 chandransh 66
	//FIXME: Read this configuration from the config server
992 varun.gupt 67
	private String hotspotReconciliationReportPath = "/HotspotReports";
68
 
69
	private HttpServletRequest request;
70
	private HttpServletResponse response;
71
 
72
	private String id;
73
 
74
	public HotspotReconciliationController(){
75
 
76
	}
77
 
78
	public HttpHeaders index()	{
79
		return new DefaultHttpHeaders("report");
80
	}
81
 
82
	public HttpHeaders show(){
83
		return new DefaultHttpHeaders("report");
84
	}
85
 
86
	// Handles the POST request (Form Submission)
87
	public HttpHeaders create(){
88
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
89
		Date startDate = null;
90
		Date endDate = null;
91
 
92
		try	{
93
			//Formatting Form input parameters
94
			startDate = dateFormat.parse(request.getParameter("start"));
95
			endDate = dateFormat.parse(request.getParameter("end"));
96
 
97
			CatalogServiceClient csc = new CatalogServiceClient();
98
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
99
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
100
 
101
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
102
			in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();
103
 
104
			List <Order> orders = new ArrayList<Order>();
105
 
106
			//Retrieving all the orders across all the warehouses
107
			for(Warehouse warehouse : warehouses)	{
1066 varun.gupt 108
				orders.addAll(client.getOrdersByBillingDate(OrderStatus.BILLED, startDate.getTime(), endDate.getTime(), warehouse.getId()));
992 varun.gupt 109
			}
1066 varun.gupt 110
			System.out.println("Total number of Orders: " + orders.size());
992 varun.gupt 111
 
112
			// Preparing XLS file for output
113
			response.setContentType("application/vnd.ms-excel");
114
 
115
			Calendar date = new GregorianCalendar();
116
			int year = date.get(Calendar.YEAR);
117
			int month = date.get(Calendar.MONTH) +1;
118
			int day = date.get(Calendar.DAY_OF_MONTH);
119
 
120
			response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-" + "-" + year + "-" + month + "-" + day + ".xls");
121
 
122
			ServletOutputStream sos;
123
			try {
124
				ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);
125
				sos = response.getOutputStream();
126
				baos.writeTo(sos);
127
				sos.flush();
128
			} catch (IOException e) {
129
				e.printStackTrace();
130
			}
131
 
132
		} catch (ParseException e)	{
133
			e.printStackTrace();
134
		} catch (TransactionServiceException e)	{
135
			e.printStackTrace();
136
		} catch (Exception e)	{
137
			e.printStackTrace();
138
		} finally	{
139
			System.out.println(startDate.getTime() + "  |  " + endDate.getTime());
140
		}
141
		return new DefaultHttpHeaders("report");
142
	}
143
 
144
	// Prepares the XLS worksheet object and fills in the data with proper formatting
145
	private ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate)	{
146
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
147
 
148
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
149
 
150
	    Workbook wb = new HSSFWorkbook();
151
	    CreationHelper createHelper = wb.getCreationHelper();
152
	    Sheet sheet = wb.createSheet("new sheet");
153
	    short serialNo = 0;
154
 
155
	    // Create the header row and put all the titles in it. Rows are 0 based.
156
	    Font font = wb.createFont();
157
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
158
	    CellStyle style = wb.createCellStyle();
159
	    style.setFont(font);
160
 
161
	    Row titleRow = sheet.createRow(serialNo ++);
162
	    Cell titleCell = titleRow.createCell(0);
163
	    titleCell.setCellValue("HotSpot Reconciliation Report (" + dateFormat.format(startDate) + " - " + dateFormat.format(endDate) + ")");
164
	    titleCell.setCellStyle(style);
165
 
166
	    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
167
 
168
	    sheet.createRow(serialNo ++);
169
 
170
	    Row headerRow = sheet.createRow(serialNo ++);
171
	    headerRow.createCell(0).setCellValue("Billing Number");
172
	    headerRow.createCell(1).setCellValue("Billing Date");
173
	    headerRow.createCell(2).setCellValue("Brand");
174
	    headerRow.createCell(3).setCellValue("Model Name");
175
	    headerRow.createCell(4).setCellValue("Model Number");
176
	    headerRow.createCell(5).setCellValue("Color");
177
	    headerRow.createCell(6).setCellValue("Transfer Price");
178
 
179
	    double totalTransferPrice = 0.0;
180
 
181
	    for(int i = 0; i < orders.size(); i ++)	{
182
	    	Order order = orders.get(i);
183
			serialNo ++;
184
	    	Row contentRow = sheet.createRow(serialNo);
185
 
186
		    LineItem lineItem = order.getLineitems().get(0);
187
		    double transferPrice = lineItem.getTransfer_price();
188
		    totalTransferPrice += transferPrice;
189
 
190
		    contentRow.createCell(0).setCellValue(order.getInvoice_number());
191
		    contentRow.createCell(1).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
192
		    contentRow.createCell(2).setCellValue(getValueForEmptyString(lineItem.getBrand()));
193
		    contentRow.createCell(3).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
194
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
195
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(lineItem.getColor()));
196
		    contentRow.createCell(6).setCellValue(transferPrice);
197
	    }
198
	    sheet.createRow(serialNo ++);
199
    	Row contentRow = sheet.createRow(serialNo);
200
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
201
    	contentRow.createCell(6).setCellValue(totalTransferPrice);
202
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
203
 
204
		// Write the workbook to the output stream
205
		try {
206
			wb.write(baosXLS);
207
			baosXLS.close();
208
		} catch (IOException e) {
209
			e.printStackTrace();
210
		}		
211
		return baosXLS;
212
	}
213
 
214
	public String getId(){
215
		return id;
216
	}
217
 
218
	public void setId(String id){
219
		this.id = id;
220
	}
221
 
222
	@Override
223
	public void setServletRequest(HttpServletRequest request) {
224
		this.request = request;
225
	}
226
 
227
	@Override
228
	public void setServletResponse(HttpServletResponse response) {
229
		this.response  = response;
230
	}
231
 
232
	private String getValueForEmptyString(String s){
233
		if(s==null || s.equals(""))
234
			return "-";
235
		else
236
			return s; 
237
	}
238
}