Subversion Repositories SmartDukaan

Rev

Rev 2042 | Rev 2492 | 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.Date;
18
import java.util.List;
19
 
20
import javax.servlet.ServletOutputStream;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
23
 
24
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25
import org.apache.poi.ss.usermodel.Cell;
26
import org.apache.poi.ss.usermodel.CellStyle;
27
import org.apache.poi.ss.usermodel.Font;
28
import org.apache.poi.ss.usermodel.Row;
29
import org.apache.poi.ss.usermodel.Sheet;
30
import org.apache.poi.ss.usermodel.Workbook;
31
import org.apache.poi.ss.util.CellRangeAddress;
32
import org.apache.struts2.interceptor.ServletRequestAware;
33
import org.apache.struts2.interceptor.ServletResponseAware;
34
import org.apache.struts2.rest.DefaultHttpHeaders;
35
import org.apache.struts2.rest.HttpHeaders;
36
 
37
/**
38
 * 
39
 * @author Varun Gupta
40
 * @version 1.0
41
 * @description HotspotReconciliationController handles requests to generate a reconciliation
42
 * report in XLS format for all the transactions with HotSpot within a given date range.
43
 * 
44
 */
45
public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware {
46
 
2008 chandransh 47
	private enum ReportColumn{
2360 ankur.sing 48
	    ORDER_ID(0),
49
		BILLING_NUMBER(1),
50
		BILLING_DATE(2),
51
		CUSTOMER_NAME(3),
52
		BRAND(4),
53
		MODEL_NAME(5),
54
		MODEL_NUMBER(6),
55
		COLOR(7),
56
		XFER_PRICE(8),
57
		SELLING_PRICE(9);
2008 chandransh 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 ++);
2360 ankur.sing 170
	    headerRow.createCell(ReportColumn.ORDER_ID.getValue()).setCellValue("Order Id");
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
 
2014 rajveer 181
	    sheet.createRow(serialNo ++);
992 varun.gupt 182
	    double totalTransferPrice = 0.0;
2011 rajveer 183
	    double totalSellingPrice = 0.0;
992 varun.gupt 184
 
185
	    for(int i = 0; i < orders.size(); i ++)	{
186
	    	Order order = orders.get(i);
2014 rajveer 187
 
188
	    	Row contentRow = sheet.createRow(serialNo++);
992 varun.gupt 189
 
190
		    LineItem lineItem = order.getLineitems().get(0);
191
		    double transferPrice = lineItem.getTransfer_price();
192
		    totalTransferPrice += transferPrice;
193
 
2014 rajveer 194
            double sellingPrice = lineItem.getTotal_price();
195
            totalSellingPrice += sellingPrice;
2011 rajveer 196
 
2360 ankur.sing 197
            contentRow.createCell(ReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
2008 chandransh 198
		    contentRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
199
		    contentRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
200
		    contentRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue(order.getCustomer_name());
201
		    contentRow.createCell(ReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
202
		    contentRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
203
		    contentRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
204
		    contentRow.createCell(ReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
205
		    contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(transferPrice);
2011 rajveer 206
		    contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(sellingPrice);
992 varun.gupt 207
	    }
208
	    sheet.createRow(serialNo ++);
209
    	Row contentRow = sheet.createRow(serialNo);
210
    	contentRow.createCell(0).setCellValue("Total Transfer Price");
2008 chandransh 211
    	contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(totalTransferPrice);
2011 rajveer 212
    	contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
992 varun.gupt 213
    	sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
214
 
215
		// Write the workbook to the output stream
216
		try {
217
			wb.write(baosXLS);
218
			baosXLS.close();
219
		} catch (IOException e) {
220
			e.printStackTrace();
221
		}		
222
		return baosXLS;
223
	}
224
 
225
	public String getId(){
226
		return id;
227
	}
228
 
229
	public void setId(String id){
230
		this.id = id;
231
	}
232
 
233
	@Override
234
	public void setServletRequest(HttpServletRequest request) {
235
		this.request = request;
236
	}
237
 
238
	@Override
239
	public void setServletResponse(HttpServletResponse response) {
240
		this.response  = response;
241
	}
242
 
243
	private String getValueForEmptyString(String s){
244
		if(s==null || s.equals(""))
245
			return "-";
246
		else
247
			return s; 
248
	}
2011 rajveer 249
}