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