Subversion Repositories SmartDukaan

Rev

Rev 992 | Rev 1066 | 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)	{
107
				List <Order> ordersForThisWarehouse = client.getOrdersByBillingDate(OrderStatus.BILLED, startDate.getTime(), endDate.getTime(), warehouse.getId());
108
				orders.addAll(ordersForThisWarehouse);
109
			}
110
 
111
			if (orders != null)	{
112
				System.out.println("Total number of Orders: " + orders.size());
113
			} else	{
114
				System.out.println("Total number of Orders: 0");
115
			}
116
 
117
			// Preparing XLS file for output
118
			response.setContentType("application/vnd.ms-excel");
119
 
120
			Calendar date = new GregorianCalendar();
121
			int year = date.get(Calendar.YEAR);
122
			int month = date.get(Calendar.MONTH) +1;
123
			int day = date.get(Calendar.DAY_OF_MONTH);
124
 
125
			response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-" + "-" + year + "-" + month + "-" + day + ".xls");
126
 
127
			ServletOutputStream sos;
128
			try {
129
				ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);
130
				sos = response.getOutputStream();
131
				baos.writeTo(sos);
132
				sos.flush();
133
			} catch (IOException e) {
134
				e.printStackTrace();
135
			}
136
 
137
		} catch (ParseException e)	{
138
			e.printStackTrace();
139
		} catch (TransactionServiceException e)	{
140
			e.printStackTrace();
141
		} catch (Exception e)	{
142
			e.printStackTrace();
143
		} finally	{
144
			System.out.println(startDate.getTime() + "  |  " + endDate.getTime());
145
		}
146
		return new DefaultHttpHeaders("report");
147
	}
148
 
149
	// Prepares the XLS worksheet object and fills in the data with proper formatting
150
	private ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate)	{
151
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
152
 
153
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
154
 
155
	    Workbook wb = new HSSFWorkbook();
156
	    CreationHelper createHelper = wb.getCreationHelper();
157
	    Sheet sheet = wb.createSheet("new sheet");
158
	    short serialNo = 0;
159
 
160
	    // Create the header row and put all the titles in it. Rows are 0 based.
161
	    Font font = wb.createFont();
162
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
163
	    CellStyle style = wb.createCellStyle();
164
	    style.setFont(font);
165
 
166
	    Row titleRow = sheet.createRow(serialNo ++);
167
	    Cell titleCell = titleRow.createCell(0);
168
	    titleCell.setCellValue("HotSpot Reconciliation Report (" + dateFormat.format(startDate) + " - " + dateFormat.format(endDate) + ")");
169
	    titleCell.setCellStyle(style);
170
 
171
	    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
172
 
173
	    sheet.createRow(serialNo ++);
174
 
175
	    Row headerRow = sheet.createRow(serialNo ++);
176
	    headerRow.createCell(0).setCellValue("Billing Number");
177
	    headerRow.createCell(1).setCellValue("Billing Date");
178
	    headerRow.createCell(2).setCellValue("Brand");
179
	    headerRow.createCell(3).setCellValue("Model Name");
180
	    headerRow.createCell(4).setCellValue("Model Number");
181
	    headerRow.createCell(5).setCellValue("Color");
182
	    headerRow.createCell(6).setCellValue("Transfer Price");
183
 
184
	    double totalTransferPrice = 0.0;
185
 
186
	    for(int i = 0; i < orders.size(); i ++)	{
187
	    	Order order = orders.get(i);
188
			serialNo ++;
189
	    	Row contentRow = sheet.createRow(serialNo);
190
 
191
		    LineItem lineItem = order.getLineitems().get(0);
192
		    double transferPrice = lineItem.getTransfer_price();
193
		    totalTransferPrice += transferPrice;
194
 
195
		    contentRow.createCell(0).setCellValue(order.getInvoice_number());
196
		    contentRow.createCell(1).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
197
		    contentRow.createCell(2).setCellValue(getValueForEmptyString(lineItem.getBrand()));
198
		    contentRow.createCell(3).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
199
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
200
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(lineItem.getColor()));
201
		    contentRow.createCell(6).setCellValue(transferPrice);
202
	    }
203
	    sheet.createRow(serialNo ++);
204
    	Row contentRow = sheet.createRow(serialNo);
205
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
206
    	contentRow.createCell(6).setCellValue(totalTransferPrice);
207
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
208
 
209
		// Write the workbook to the output stream
210
		try {
211
			wb.write(baosXLS);
212
			baosXLS.close();
213
		} catch (IOException e) {
214
			e.printStackTrace();
215
		}		
216
		return baosXLS;
217
	}
218
 
219
	public String getId(){
220
		return id;
221
	}
222
 
223
	public void setId(String id){
224
		this.id = id;
225
	}
226
 
227
	@Override
228
	public void setServletRequest(HttpServletRequest request) {
229
		this.request = request;
230
	}
231
 
232
	@Override
233
	public void setServletResponse(HttpServletResponse response) {
234
		this.response  = response;
235
	}
236
 
237
	private String getValueForEmptyString(String s){
238
		if(s==null || s.equals(""))
239
			return "-";
240
		else
241
			return s; 
242
	}
243
}