Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1194 chandransh 1
package in.shop2020.support.services;
2
 
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.TransactionServiceException;
6
import in.shop2020.thrift.clients.TransactionServiceClient;
7
 
8
import java.io.ByteArrayOutputStream;
9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11
import java.io.IOException;
1218 chandransh 12
import java.util.Calendar;
13
import java.util.GregorianCalendar;
14
import java.util.HashMap;
1194 chandransh 15
import java.util.List;
1218 chandransh 16
import java.util.Map;
17
import java.util.Map.Entry;
1194 chandransh 18
 
19
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1389 ankur.sing 20
import org.apache.poi.ss.usermodel.CellStyle;
21
import org.apache.poi.ss.usermodel.CreationHelper;
1194 chandransh 22
import org.apache.poi.ss.usermodel.Row;
23
import org.apache.poi.ss.usermodel.Sheet;
24
import org.apache.poi.ss.usermodel.Workbook;
25
import org.apache.thrift.TException;
26
 
27
public class PendingOrdersGenerator {
28
	private TransactionServiceClient tsc = null;
29
 
30
	public PendingOrdersGenerator(){
31
		try {
32
			tsc = new TransactionServiceClient();
33
		} catch (Exception e) {
34
			e.printStackTrace();
35
		}
36
	}
37
 
38
	public ByteArrayOutputStream generatePendingOrdersDetails(long warehouseId){
1389 ankur.sing 39
		final int COL_SNO = 0,
40
			COL_ORDER_ID = 1,
41
			COL_CUSTOMER_NAME = 2,
42
			COL_CUTOMER_PHONE = 3, 
43
			COL_SHIPPING_LOCATION = 4, 
44
			COL_PRODUCT_GROUP = 5,
45
			COL_MODEL = 6, 
46
			COL_COLOUR = 7,
47
			COL_QTY = 8,
48
			COL_PRICE = 9;
49
		final int COL_SUMMARY_SNO = 0, COL_SUMMARY_PRODUCT = 1, COL_SUMMARY_QTY = 2;
50
 
1194 chandransh 51
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
52
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
53
		List<Order> orders = null;
54
 
55
		try {
1222 chandransh 56
			orders = txnClient.batchOrders(warehouseId);
57
			//getAllOrders(OrderStatus.SUBMITTED_FOR_PROCESSING, 0L, new Date().getTime(), warehouseId);
1194 chandransh 58
		} catch (TransactionServiceException e) {
59
			e.printStackTrace();
60
			return baosXLS;
61
		} catch (TException e) {
62
			e.printStackTrace();
63
			return baosXLS;
64
		}
65
 
66
		Workbook wb = new HSSFWorkbook();
1389 ankur.sing 67
		CreationHelper createHelper = wb.getCreationHelper();
1218 chandransh 68
	    Sheet detailedSheet = wb.createSheet("Detailed sheet");
1389 ankur.sing 69
	    detailedSheet.getPrintSetup().setLandscape(true); // Sets the print orientation as Landscape, by default its Portrait.
70
	    detailedSheet.setMargin(Sheet.RightMargin, 0.5);
71
	    detailedSheet.setPrintGridlines(true);
1218 chandransh 72
	    Sheet summarySheet = wb.createSheet("Summary sheet");
1389 ankur.sing 73
	    summarySheet.setPrintGridlines(true);
1218 chandransh 74
	    Map<String, Double> summaryMap = new HashMap<String, Double>(); 
1389 ankur.sing 75
 
76
        CellStyle csRA = wb.createCellStyle();
77
        csRA.setAlignment(CellStyle.ALIGN_RIGHT);
78
 
79
        CellStyle csWT = wb.createCellStyle();
80
        csWT.setWrapText(true);
81
 
82
        CellStyle weightStyle = wb.createCellStyle();
83
	    weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.00"));
84
 
85
        Calendar date = new GregorianCalendar();
1218 chandransh 86
		int year = date.get(Calendar.YEAR);
87
		int month = date.get(Calendar.MONTH) +1;
88
		int day = date.get(Calendar.DAY_OF_MONTH);
89
		int hour = date.get(Calendar.HOUR_OF_DAY);
90
		int minute = date.get(Calendar.MINUTE);
91
 
92
	    Row dateRow = detailedSheet.createRow(1);
1389 ankur.sing 93
	    dateRow.createCell(2).setCellValue("Date");
94
	    dateRow.createCell(3).setCellValue(String.format("%4d-%02d-%02d", year, month, day));
1218 chandransh 95
 
96
	    Row timeRow = detailedSheet.createRow(2);
1389 ankur.sing 97
	    timeRow.createCell(2).setCellValue("Time");
98
	    timeRow.createCell(3).setCellValue(String.format("%02d:%02d", hour, minute));
1218 chandransh 99
 
100
	    Row batchRow = detailedSheet.createRow(3);
1389 ankur.sing 101
	    batchRow.createCell(2).setCellValue("Batch No.");
102
	    batchRow.createCell(3).setCellValue(orders.get(0).getBatchNo() + "");
1218 chandransh 103
 
104
	    Row detailsHeaderRow = detailedSheet.createRow(5);
1389 ankur.sing 105
	    detailsHeaderRow.createCell(COL_SNO).setCellValue("SNo");
106
	    detailsHeaderRow.createCell(COL_ORDER_ID).setCellValue("Order ID");
107
 
108
	    detailsHeaderRow.createCell(COL_CUSTOMER_NAME).setCellValue("Customer Name");
109
	    detailedSheet.setColumnWidth(COL_CUSTOMER_NAME, 5000);
1218 chandransh 110
 
1389 ankur.sing 111
	    detailsHeaderRow.createCell(COL_CUTOMER_PHONE).setCellValue("Customer Phone");
112
 
113
	    detailsHeaderRow.createCell(COL_SHIPPING_LOCATION).setCellValue("Shipping Location");
114
	    detailedSheet.setColumnWidth(COL_SHIPPING_LOCATION, 5000);
115
 
116
	    detailsHeaderRow.createCell(COL_PRODUCT_GROUP).setCellValue("Product Group");
117
 
118
	    detailsHeaderRow.createCell(COL_MODEL).setCellValue("Brand / Model");
119
	    detailedSheet.setColumnWidth(COL_MODEL, 5000);
120
 
121
	    detailsHeaderRow.createCell(COL_COLOUR).setCellValue("Colour");
122
	    detailsHeaderRow.createCell(COL_QTY).setCellValue("QTY.");
123
	    detailsHeaderRow.getCell(COL_QTY).setCellStyle(csRA);
124
	    detailsHeaderRow.createCell(COL_PRICE).setCellValue("Price");
125
	    detailsHeaderRow.getCell(COL_PRICE).setCellStyle(csRA);
126
 
127
 
1218 chandransh 128
	    Row summaryHeaderRow = summarySheet.createRow((short)0);
1389 ankur.sing 129
	    summaryHeaderRow.createCell(COL_SUMMARY_SNO).setCellValue("Sl No.");
130
	    summaryHeaderRow.createCell(COL_SUMMARY_PRODUCT).setCellValue("Product");
131
	    summaryHeaderRow.createCell(COL_SUMMARY_QTY).setCellValue("Quantity");
1218 chandransh 132
 
1222 chandransh 133
	    short rowNum = 5;
1389 ankur.sing 134
	    String customerName, shippingLocation, brandModel;
135
	    float rowHeight = detailedSheet.getDefaultRowHeightInPoints();
1194 chandransh 136
	    for(int i = 0; i<orders.size(); i++){
137
	    	Order order = orders.get(i);
1222 chandransh 138
			rowNum++;
139
	    	Row contentRow = detailedSheet.createRow(rowNum);
1389 ankur.sing 140
		    contentRow.createCell(COL_SNO).setCellValue(order.getSerialNo() + "");		    
141
		    contentRow.createCell(COL_ORDER_ID).setCellValue(order.getId() + "");
1194 chandransh 142
 
1389 ankur.sing 143
		    customerName = getValueForEmptyString(order.getCustomer_name());
144
		    contentRow.createCell(COL_CUSTOMER_NAME).setCellValue(customerName);
145
		    contentRow.getCell(COL_CUSTOMER_NAME).setCellStyle(csWT);
146
 
147
		    contentRow.createCell(COL_CUTOMER_PHONE).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));
148
 
149
		    shippingLocation = getValueForEmptyString(order.getCustomer_city()) + " (" + 
150
		    		getValueForEmptyString(order.getCustomer_state()) + ")";
151
		    contentRow.createCell(COL_SHIPPING_LOCATION).setCellValue(shippingLocation);
152
		    contentRow.getCell(COL_SHIPPING_LOCATION).setCellStyle(csWT);
153
 
1194 chandransh 154
		    List<LineItem> lineItems = order.getLineitems();
155
		    LineItem lineItem = lineItems.get(0);
1389 ankur.sing 156
		    contentRow.createCell(COL_PRODUCT_GROUP).setCellValue(getValueForEmptyString(lineItem.getProductGroup()));
1218 chandransh 157
 
1389 ankur.sing 158
		    brandModel = getValueForEmptyString(lineItem.getBrand()) + " / " + 
159
    			lineItem.getModel_number() + " " + getValueForEmptyString(lineItem.getModel_name());
160
		    contentRow.createCell(COL_MODEL).setCellValue(brandModel);
161
		    contentRow.getCell(COL_MODEL).setCellStyle(csWT);
162
 
163
		    contentRow.createCell(COL_COLOUR).setCellValue(lineItem.getColor());
164
		    contentRow.createCell(COL_QTY).setCellValue(lineItem.getQuantity());
165
		    contentRow.createCell(COL_PRICE).setCellValue(lineItem.getTotal_price());
166
		    contentRow.getCell(COL_PRICE).setCellStyle(weightStyle);
167
 
168
		    int maxLength = Math.max(brandModel.length(), Math.max(customerName.length(), shippingLocation.length()));
169
		    contentRow.setHeightInPoints((maxLength / (5000/256) + 1) * rowHeight);  // Set Row Height
170
 
1218 chandransh 171
		    //Add this item to the summary map to print the summary sheet
172
		    String itemKey = lineItem.getProductGroup() + " " + 
173
		    				lineItem.getBrand() + " " +
174
		    				lineItem.getModel_number() + " " +
175
		    				getValueForEmptyString(lineItem.getModel_name()) + " " +
176
		    				getValueForEmptyString(lineItem.getColor());
177
		    Double quantity = summaryMap.get(itemKey);
178
		    if(quantity != null){
179
		    	quantity = quantity + lineItem.getQuantity();
180
		    }else{
181
		    	quantity = lineItem.getQuantity();
182
		    }
183
		    summaryMap.put(itemKey, quantity);
1194 chandransh 184
	    }
1218 chandransh 185
 
1389 ankur.sing 186
	    detailedSheet.autoSizeColumn(COL_SNO);
187
	    detailedSheet.autoSizeColumn(COL_ORDER_ID);
188
	    detailedSheet.autoSizeColumn(COL_CUTOMER_PHONE);
189
	    detailedSheet.autoSizeColumn(COL_PRODUCT_GROUP);
190
	    detailedSheet.autoSizeColumn(COL_COLOUR);
191
	    detailedSheet.autoSizeColumn(COL_QTY);
192
	    detailedSheet.autoSizeColumn(COL_PRICE);
193
 
194
 
1222 chandransh 195
	    rowNum = 1;
1218 chandransh 196
	    for(Entry<String, Double> entry : summaryMap.entrySet()){
1222 chandransh 197
	    	Row summaryRow = summarySheet.createRow(rowNum);
1389 ankur.sing 198
	    	summaryRow.createCell(COL_SUMMARY_SNO).setCellValue(rowNum);
199
	    	summaryRow.createCell(COL_SUMMARY_PRODUCT).setCellValue(entry.getKey());
200
	    	summaryRow.createCell(COL_SUMMARY_QTY).setCellValue(entry.getValue());
1222 chandransh 201
	    	rowNum++;
1218 chandransh 202
	    }
203
 
1389 ankur.sing 204
	    summarySheet.autoSizeColumn(COL_SUMMARY_PRODUCT);
1194 chandransh 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
	private String getValueForEmptyString(String s){
216
		if(s==null || s.equals(""))
217
			return " ";
218
		else
219
			return s; 
220
	}
221
 
222
	public static void main(String[] args) {
223
		PendingOrdersGenerator pendingOrdersGenerator = new PendingOrdersGenerator();
224
		try {
1389 ankur.sing 225
			String userHome = System.getProperty("user.home");
226
			FileOutputStream f = new FileOutputStream(userHome + "/pending-orders.xls");
1194 chandransh 227
			ByteArrayOutputStream baosXLS = pendingOrdersGenerator.generatePendingOrdersDetails(1);
228
			baosXLS.writeTo(f);
229
			f.close();
230
		} catch (FileNotFoundException e) {
231
			e.printStackTrace();
232
		} catch (IOException e) {
233
			e.printStackTrace();
234
		}
235
		System.out.println("Successfully generated the pending orders report");
236
	}
237
}