Subversion Repositories SmartDukaan

Rev

Rev 1884 | Rev 2011 | 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),
57
		XFER_PRICE(7);
58
 
59
		private int value;
60
 
61
		ReportColumn(int value) {
62
			this.value = value;
63
		}
64
		public int getValue(){
65
			return this.value;
66
		}
67
	}
68
 
1075 chandransh 69
	//FIXME: Read this configuration from the config server
1884 chandransh 70
	//private String hotspotReconciliationReportPath = "/HotspotReports";
992 varun.gupt 71
 
72
	private HttpServletRequest request;
73
	private HttpServletResponse response;
74
 
75
	private String id;
76
 
77
	public HotspotReconciliationController(){
78
 
79
	}
80
 
81
	public HttpHeaders index()	{
82
		return new DefaultHttpHeaders("report");
83
	}
84
 
85
	public HttpHeaders show(){
86
		return new DefaultHttpHeaders("report");
87
	}
88
 
89
	// Handles the POST request (Form Submission)
90
	public HttpHeaders create(){
91
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
92
		Date startDate = null;
93
		Date endDate = null;
94
 
95
		try	{
96
			//Formatting Form input parameters
97
			startDate = dateFormat.parse(request.getParameter("start"));
98
			endDate = dateFormat.parse(request.getParameter("end"));
99
 
100
			CatalogServiceClient csc = new CatalogServiceClient();
101
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
102
			List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
103
 
104
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
105
			in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();
106
 
107
			List <Order> orders = new ArrayList<Order>();
108
 
109
			//Retrieving all the orders across all the warehouses
110
			for(Warehouse warehouse : warehouses)	{
2008 chandransh 111
				orders.addAll(client.getOrdersByBillingDate(null, startDate.getTime(), endDate.getTime(), warehouse.getId()));
992 varun.gupt 112
			}
1066 varun.gupt 113
			System.out.println("Total number of Orders: " + orders.size());
992 varun.gupt 114
 
115
			// Preparing XLS file for output
116
			response.setContentType("application/vnd.ms-excel");
117
 
118
 
2008 chandransh 119
			DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");
120
			response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-from-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");
121
 
992 varun.gupt 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
	    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 ++);
2008 chandransh 170
	    headerRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue("Billing Number");
171
	    headerRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue("Billing Date");
172
	    headerRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue("Customer Name");
173
	    headerRow.createCell(ReportColumn.BRAND.getValue()).setCellValue("Brand");
174
	    headerRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue("Model Name");
175
	    headerRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue("Model Number");
176
	    headerRow.createCell(ReportColumn.COLOR.getValue()).setCellValue("Color");
177
	    headerRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue("Transfer Price");
992 varun.gupt 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
 
2008 chandransh 190
		    contentRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
191
		    contentRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
192
		    contentRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue(order.getCustomer_name());
193
		    contentRow.createCell(ReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
194
		    contentRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
195
		    contentRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
196
		    contentRow.createCell(ReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
197
		    contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(transferPrice);
992 varun.gupt 198
	    }
199
	    sheet.createRow(serialNo ++);
200
    	Row contentRow = sheet.createRow(serialNo);
201
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
2008 chandransh 202
    	contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(totalTransferPrice);
992 varun.gupt 203
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
204
 
205
		// Write the workbook to the output stream
206
		try {
207
			wb.write(baosXLS);
208
			baosXLS.close();
209
		} catch (IOException e) {
210
			e.printStackTrace();
211
		}		
212
		return baosXLS;
213
	}
214
 
215
	public String getId(){
216
		return id;
217
	}
218
 
219
	public void setId(String id){
220
		this.id = id;
221
	}
222
 
223
	@Override
224
	public void setServletRequest(HttpServletRequest request) {
225
		this.request = request;
226
	}
227
 
228
	@Override
229
	public void setServletResponse(HttpServletResponse response) {
230
		this.response  = response;
231
	}
232
 
233
	private String getValueForEmptyString(String s){
234
		if(s==null || s.equals(""))
235
			return "-";
236
		else
237
			return s; 
238
	}
2008 chandransh 239
 
240
	private String getDateString(Calendar date){
241
		int year = date.get(Calendar.YEAR);
242
		int month = date.get(Calendar.MONTH) +1;
243
		int day = date.get(Calendar.DAY_OF_MONTH);
244
		return "" + year + "." + month + "." + day;
245
	}
992 varun.gupt 246
}