Subversion Repositories SmartDukaan

Rev

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