Subversion Repositories SmartDukaan

Rev

Rev 2008 | Rev 2014 | 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.model.v1.catalog.Warehouse;
4
import in.shop2020.model.v1.order.LineItem;
5
import in.shop2020.model.v1.order.Order;
6
import in.shop2020.model.v1.order.TransactionServiceException;
7
import in.shop2020.thrift.clients.CatalogServiceClient;
8
import in.shop2020.thrift.clients.TransactionServiceClient;
9
 
10
import java.text.DateFormat;
11
import java.text.SimpleDateFormat;
12
import java.text.ParseException;
13
 
14
import java.io.ByteArrayOutputStream;
15
import java.io.IOException;
16
import java.util.ArrayList;
17
import java.util.Calendar;
18
import java.util.Date;
19
import java.util.GregorianCalendar;
20
import java.util.List;
21
 
22
import javax.servlet.ServletOutputStream;
23
import javax.servlet.http.HttpServletRequest;
24
import javax.servlet.http.HttpServletResponse;
25
 
26
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.poi.ss.usermodel.CellStyle;
29
import org.apache.poi.ss.usermodel.Font;
30
import org.apache.poi.ss.usermodel.Row;
31
import org.apache.poi.ss.usermodel.Sheet;
32
import org.apache.poi.ss.usermodel.Workbook;
33
import org.apache.poi.ss.util.CellRangeAddress;
34
import org.apache.struts2.interceptor.ServletRequestAware;
35
import org.apache.struts2.interceptor.ServletResponseAware;
36
import org.apache.struts2.rest.DefaultHttpHeaders;
37
import org.apache.struts2.rest.HttpHeaders;
38
 
39
/**
40
 * 
41
 * @author Varun Gupta
42
 * @version 1.0
43
 * @description HotspotReconciliationController handles requests to generate a reconciliation
44
 * report in XLS format for all the transactions with HotSpot within a given date range.
45
 * 
46
 */
47
public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware {
48
 
2008 chandransh 49
	private enum ReportColumn{
50
		BILLING_NUMBER(0),
51
		BILLING_DATE(1),
52
		CUSTOMER_NAME(2),
53
		BRAND(3),
54
		MODEL_NAME(4),
55
		MODEL_NUMBER(5),
56
		COLOR(6),
2011 rajveer 57
		XFER_PRICE(7),
58
		SELLING_PRICE(8);
2008 chandransh 59
 
60
		private int value;
61
 
62
		ReportColumn(int value) {
63
			this.value = value;
64
		}
65
		public int getValue(){
66
			return this.value;
67
		}
68
	}
69
 
1075 chandransh 70
	//FIXME: Read this configuration from the config server
1884 chandransh 71
	//private String hotspotReconciliationReportPath = "/HotspotReports";
992 varun.gupt 72
 
73
	private HttpServletRequest request;
74
	private HttpServletResponse response;
75
 
76
	private String id;
77
 
78
	public HotspotReconciliationController(){
79
 
80
	}
81
 
82
	public HttpHeaders index()	{
83
		return new DefaultHttpHeaders("report");
84
	}
85
 
86
	public HttpHeaders show(){
87
		return new DefaultHttpHeaders("report");
88
	}
89
 
90
	// Handles the POST request (Form Submission)
91
	public HttpHeaders create(){
92
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
93
		Date startDate = null;
94
		Date endDate = null;
95
 
96
		try	{
97
			//Formatting Form input parameters
98
			startDate = dateFormat.parse(request.getParameter("start"));
99
			endDate = dateFormat.parse(request.getParameter("end"));
100
 
101
			CatalogServiceClient csc = new CatalogServiceClient();
102
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
103
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
104
 
105
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
106
			in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();
107
 
108
			List <Order> orders = new ArrayList<Order>();
109
 
110
			//Retrieving all the orders across all the warehouses
111
			for(Warehouse warehouse : warehouses)	{
2008 chandransh 112
				orders.addAll(client.getOrdersByBillingDate(null, startDate.getTime(), endDate.getTime(), warehouse.getId()));
992 varun.gupt 113
			}
1066 varun.gupt 114
			System.out.println("Total number of Orders: " + orders.size());
992 varun.gupt 115
 
116
			// Preparing XLS file for output
117
			response.setContentType("application/vnd.ms-excel");
118
 
119
 
2008 chandransh 120
			DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");
121
			response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-from-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");
122
 
992 varun.gupt 123
			ServletOutputStream sos;
124
			try {
125
				ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);
126
				sos = response.getOutputStream();
127
				baos.writeTo(sos);
128
				sos.flush();
129
			} catch (IOException e) {
130
				e.printStackTrace();
131
			}
132
 
133
		} catch (ParseException e)	{
134
			e.printStackTrace();
135
		} catch (TransactionServiceException e)	{
136
			e.printStackTrace();
137
		} catch (Exception e)	{
138
			e.printStackTrace();
139
		} finally	{
140
			System.out.println(startDate.getTime() + "  |  " + endDate.getTime());
141
		}
142
		return new DefaultHttpHeaders("report");
143
	}
144
 
145
	// Prepares the XLS worksheet object and fills in the data with proper formatting
146
	private ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate)	{
147
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
148
 
149
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
150
 
151
	    Workbook wb = new HSSFWorkbook();
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 ++);
2008 chandransh 171
	    headerRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue("Billing Number");
172
	    headerRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue("Billing Date");
173
	    headerRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue("Customer Name");
174
	    headerRow.createCell(ReportColumn.BRAND.getValue()).setCellValue("Brand");
175
	    headerRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue("Model Name");
176
	    headerRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue("Model Number");
177
	    headerRow.createCell(ReportColumn.COLOR.getValue()).setCellValue("Color");
178
	    headerRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue("Transfer Price");
2011 rajveer 179
	    headerRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue("Selling Price");
992 varun.gupt 180
 
181
	    double totalTransferPrice = 0.0;
2011 rajveer 182
	    double totalSellingPrice = 0.0;
992 varun.gupt 183
 
184
	    for(int i = 0; i < orders.size(); i ++)	{
185
	    	Order order = orders.get(i);
186
			serialNo ++;
187
	    	Row contentRow = sheet.createRow(serialNo);
188
 
189
		    LineItem lineItem = order.getLineitems().get(0);
190
		    double transferPrice = lineItem.getTransfer_price();
191
		    totalTransferPrice += transferPrice;
192
 
2011 rajveer 193
                    double sellingPrice = lineItem.getTotal_price();
194
                    totalSellingPrice += sellingPrice;
195
 
2008 chandransh 196
		    contentRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
197
		    contentRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
198
		    contentRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue(order.getCustomer_name());
199
		    contentRow.createCell(ReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
200
		    contentRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
201
		    contentRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
202
		    contentRow.createCell(ReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
203
		    contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(transferPrice);
2011 rajveer 204
		    contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(sellingPrice);
992 varun.gupt 205
	    }
206
	    sheet.createRow(serialNo ++);
207
    	Row contentRow = sheet.createRow(serialNo);
208
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
2008 chandransh 209
    	contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(totalTransferPrice);
2011 rajveer 210
    	contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
992 varun.gupt 211
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
212
 
213
		// Write the workbook to the output stream
214
		try {
215
			wb.write(baosXLS);
216
			baosXLS.close();
217
		} catch (IOException e) {
218
			e.printStackTrace();
219
		}		
220
		return baosXLS;
221
	}
222
 
223
	public String getId(){
224
		return id;
225
	}
226
 
227
	public void setId(String id){
228
		this.id = id;
229
	}
230
 
231
	@Override
232
	public void setServletRequest(HttpServletRequest request) {
233
		this.request = request;
234
	}
235
 
236
	@Override
237
	public void setServletResponse(HttpServletResponse response) {
238
		this.response  = response;
239
	}
240
 
241
	private String getValueForEmptyString(String s){
242
		if(s==null || s.equals(""))
243
			return "-";
244
		else
245
			return s; 
246
	}
2008 chandransh 247
 
248
	private String getDateString(Calendar date){
249
		int year = date.get(Calendar.YEAR);
250
		int month = date.get(Calendar.MONTH) +1;
251
		int day = date.get(Calendar.DAY_OF_MONTH);
252
		return "" + year + "." + month + "." + day;
253
	}
2011 rajveer 254
}