| Line 9... |
Line 9... |
| 9 |
import java.util.Calendar;
|
9 |
import java.util.Calendar;
|
| 10 |
import java.util.GregorianCalendar;
|
10 |
import java.util.GregorianCalendar;
|
| 11 |
import java.util.List;
|
11 |
import java.util.List;
|
| 12 |
import java.util.Map;
|
12 |
import java.util.Map;
|
| 13 |
|
13 |
|
| - |
|
14 |
import in.shop2020.logistics.DeliveryType;
|
| - |
|
15 |
import in.shop2020.logistics.LogisticsService.Client;
|
| - |
|
16 |
import in.shop2020.logistics.LogisticsServiceException;
|
| 14 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
17 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
| 15 |
import in.shop2020.model.v1.catalog.Item;
|
18 |
import in.shop2020.model.v1.catalog.Item;
|
| - |
|
19 |
import in.shop2020.model.v1.catalog.ItemInventory;
|
| 16 |
import in.shop2020.model.v1.catalog.status;
|
20 |
import in.shop2020.model.v1.catalog.status;
|
| 17 |
import in.shop2020.support.utils.FileUtils;
|
21 |
import in.shop2020.support.utils.FileUtils;
|
| 18 |
import in.shop2020.support.utils.ReportsUtils;
|
22 |
import in.shop2020.support.utils.ReportsUtils;
|
| 19 |
import in.shop2020.thrift.clients.CatalogClient;
|
23 |
import in.shop2020.thrift.clients.CatalogClient;
|
| - |
|
24 |
import in.shop2020.thrift.clients.LogisticsClient;
|
| 20 |
|
25 |
|
| 21 |
import javax.servlet.ServletContext;
|
26 |
import javax.servlet.ServletContext;
|
| 22 |
import javax.servlet.ServletOutputStream;
|
27 |
import javax.servlet.ServletOutputStream;
|
| 23 |
import javax.servlet.http.HttpServletRequest;
|
28 |
import javax.servlet.http.HttpServletRequest;
|
| 24 |
import javax.servlet.http.HttpServletResponse;
|
29 |
import javax.servlet.http.HttpServletResponse;
|
| Line 50... |
Line 55... |
| 50 |
})
|
55 |
})
|
| 51 |
public class StockReportsController implements ServletRequestAware, ServletResponseAware, ServletContextAware{
|
56 |
public class StockReportsController implements ServletRequestAware, ServletResponseAware, ServletContextAware{
|
| 52 |
|
57 |
|
| 53 |
private static Logger logger = LoggerFactory.getLogger(StockReportsController.class);
|
58 |
private static Logger logger = LoggerFactory.getLogger(StockReportsController.class);
|
| 54 |
|
59 |
|
| 55 |
private static final int ID = 0, BRAND = 1, MODEL_NUMBER = 2, MODEL_NAME = 3, COLOR = 4, QUANTITY = 5;
|
- |
|
| 56 |
private static final String REPORT_DIR = "/inventory-report";
|
60 |
private static final String REPORT_DIR = "/inventory-report";
|
| 57 |
|
61 |
|
| - |
|
62 |
private static final String DEFAULT_PINCODE = "110001";
|
| - |
|
63 |
private static final long REPORT_GENERATION_INTERVAL = 1;
|
| - |
|
64 |
|
| - |
|
65 |
private static final int ID = 0, BRAND = 1, MODEL_NUMBER = 2, MODEL_NAME = 3, COLOR = 4, TOTAL_AVAILABILITY = 5, TOTAL_RESERVED = 6;
|
| - |
|
66 |
private static final int WH1_AVAILABILITY = 7, WH1_RESERVED = 8, WH5_AVALABILITY = 9, WH5_RESERVED = 10, WH7_AVALABILITY = 11, WH7_RESERVED = 12;
|
| - |
|
67 |
|
| 58 |
private HttpSession session;
|
68 |
private HttpSession session;
|
| 59 |
private HttpServletRequest request;
|
69 |
private HttpServletRequest request;
|
| 60 |
private HttpServletResponse response;
|
70 |
private HttpServletResponse response;
|
| 61 |
private ServletContext context;
|
71 |
private ServletContext context;
|
| 62 |
|
72 |
|
| Line 67... |
Line 77... |
| 67 |
return "authfail";
|
77 |
return "authfail";
|
| 68 |
}
|
78 |
}
|
| 69 |
return "index";
|
79 |
return "index";
|
| 70 |
}
|
80 |
}
|
| 71 |
|
81 |
|
| 72 |
public String create(){
|
82 |
public String create() throws NumberFormatException, IOException{
|
| 73 |
|
83 |
|
| 74 |
Calendar date = new GregorianCalendar();
|
84 |
Calendar date = new GregorianCalendar();
|
| 75 |
int year = date.get(Calendar.YEAR);
|
85 |
int year = date.get(Calendar.YEAR);
|
| 76 |
int month = date.get(Calendar.MONTH) +1;
|
86 |
int month = date.get(Calendar.MONTH) +1;
|
| 77 |
int day = date.get(Calendar.DAY_OF_MONTH);
|
87 |
int day = date.get(Calendar.DAY_OF_MONTH);
|
| 78 |
String filename = "inventory-report." + year + "-" + month + "-" + day + ".xls";
|
88 |
long currentTimestamp = date.getTimeInMillis();
|
| 79 |
|
- |
|
| 80 |
File inventoryReportFile = new File(REPORT_DIR + File.separator + filename);
|
89 |
long lastTimestamp = Long.parseLong(org.apache.commons.io.FileUtils.readFileToString(new File(REPORT_DIR + File.separator + "last.timestamp")));
|
| 81 |
|
90 |
|
| - |
|
91 |
String filename = "inventory-report." + year + "-" + month + "-" + day + "-" + lastTimestamp + ".xls";
|
| 82 |
if(!inventoryReportFile.exists()){
|
92 |
if(lastTimestamp-currentTimestamp > REPORT_GENERATION_INTERVAL*60*60*1000){
|
| - |
|
93 |
filename = "inventory-report." + year + "-" + month + "-" + day + "-" + currentTimestamp + ".xls";
|
| 83 |
//Report doesn't exist. Need to generate it.
|
94 |
File inventoryReportFile = new File(REPORT_DIR + File.separator + filename);
|
| 84 |
ByteArrayOutputStream baosXLS = generateInventoryStockReport();
|
95 |
ByteArrayOutputStream baosXLS = generateInventoryStockReport();
|
| 85 |
if (baosXLS == null) {
|
96 |
if (baosXLS == null) {
|
| 86 |
errorMsg = "Could not get output";
|
97 |
errorMsg = "Could not get output";
|
| 87 |
return "index";
|
98 |
return "index";
|
| 88 |
}
|
99 |
}
|
| 89 |
|
100 |
|
| 90 |
try {
|
101 |
try {
|
| 91 |
FileOutputStream f = new FileOutputStream(inventoryReportFile);
|
102 |
FileOutputStream f = new FileOutputStream(inventoryReportFile);
|
| 92 |
baosXLS.writeTo(f);
|
103 |
baosXLS.writeTo(f);
|
| 93 |
f.close();
|
104 |
f.close();
|
| - |
|
105 |
org.apache.commons.io.FileUtils.writeStringToFile(new File(REPORT_DIR + File.separator + "last.timestamp"), currentTimestamp+"");
|
| 94 |
} catch (FileNotFoundException e) {
|
106 |
} catch (FileNotFoundException e) {
|
| 95 |
logger.error("Error while writing the inventory report", e);
|
107 |
logger.error("Error while writing the inventory report", e);
|
| 96 |
return "index";
|
108 |
return "index";
|
| 97 |
} catch (IOException e) {
|
109 |
} catch (IOException e) {
|
| 98 |
logger.error("Error while writing the inventory report", e);
|
110 |
logger.error("Error while writing the inventory report", e);
|
| Line 119... |
Line 131... |
| 119 |
private ByteArrayOutputStream generateInventoryStockReport(){
|
131 |
private ByteArrayOutputStream generateInventoryStockReport(){
|
| 120 |
List<RowItem> outOfStockItems = new ArrayList<RowItem>();
|
132 |
List<RowItem> outOfStockItems = new ArrayList<RowItem>();
|
| 121 |
List<RowItem> itemsDeliverableNextDay = new ArrayList<RowItem>();
|
133 |
List<RowItem> itemsDeliverableNextDay = new ArrayList<RowItem>();
|
| 122 |
List<RowItem> itemsNotDeliverableNextDay = new ArrayList<RowItem>();
|
134 |
List<RowItem> itemsNotDeliverableNextDay = new ArrayList<RowItem>();
|
| 123 |
|
135 |
|
| - |
|
136 |
LogisticsClient logisticsServiceClient;
|
| 124 |
CatalogClient catalogClientService;
|
137 |
CatalogClient catalogClientService;
|
| 125 |
try {
|
138 |
try {
|
| 126 |
catalogClientService = new CatalogClient();
|
139 |
catalogClientService = new CatalogClient();
|
| - |
|
140 |
logisticsServiceClient = new LogisticsClient();
|
| 127 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
141 |
in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
|
| 128 |
List<Item> items = client.getAllItems(false);
|
142 |
List<Item> items = client.getAllItems(false);
|
| - |
|
143 |
Client logisticsClient = logisticsServiceClient.getClient();
|
| 129 |
|
144 |
|
| 130 |
for(Item item : items){
|
145 |
for(Item item : items){
|
| 131 |
if(!"Handsets".equals(item.getProductGroup()))
|
146 |
if(!"Handsets".equals(item.getProductGroup()))
|
| 132 |
continue;
|
147 |
continue;
|
| 133 |
status state = item.getItemStatus();
|
148 |
status state = item.getItemStatus();
|
| 134 |
switch(state){
|
149 |
switch(state){
|
| 135 |
case IN_PROCESS:
|
150 |
case IN_PROCESS:
|
| Line 140... |
Line 155... |
| 140 |
case PAUSED:
|
155 |
case PAUSED:
|
| 141 |
case PAUSED_BY_RISK:
|
156 |
case PAUSED_BY_RISK:
|
| 142 |
case ACTIVE:
|
157 |
case ACTIVE:
|
| 143 |
RowItem rowItem = new RowItem(item);
|
158 |
RowItem rowItem = new RowItem(item);
|
| 144 |
if(state == status.ACTIVE){
|
159 |
if(state == status.ACTIVE){
|
| - |
|
160 |
long deliveryDays = logisticsClient.getLogisticsEstimation(item.getId(), DEFAULT_PINCODE, DeliveryType.PREPAID).getDeliveryTime();
|
| 145 |
if(rowItem.quantity >= 2)
|
161 |
if(deliveryDays == 1)
|
| 146 |
itemsDeliverableNextDay.add(rowItem);
|
162 |
itemsDeliverableNextDay.add(rowItem);
|
| 147 |
else
|
163 |
else
|
| 148 |
itemsNotDeliverableNextDay.add(rowItem);
|
164 |
itemsNotDeliverableNextDay.add(rowItem);
|
| 149 |
}else
|
165 |
}else
|
| 150 |
outOfStockItems.add(rowItem);
|
166 |
outOfStockItems.add(rowItem);
|
| Line 157... |
Line 173... |
| 157 |
logger.error("Error while getting the items from the inventory", e);
|
173 |
logger.error("Error while getting the items from the inventory", e);
|
| 158 |
return null;
|
174 |
return null;
|
| 159 |
} catch (TException e) {
|
175 |
} catch (TException e) {
|
| 160 |
logger.error("Error while getting the items from the inventory", e);
|
176 |
logger.error("Error while getting the items from the inventory", e);
|
| 161 |
return null;
|
177 |
return null;
|
| - |
|
178 |
} catch (LogisticsServiceException e) {
|
| - |
|
179 |
logger.error("Error while getting estimate of the inventory", e);
|
| 162 |
}
|
180 |
return null;
|
| - |
|
181 |
|
| - |
|
182 |
}
|
| 163 |
|
183 |
|
| 164 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
184 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
| 165 |
|
185 |
|
| 166 |
Workbook wb = new HSSFWorkbook();
|
186 |
Workbook wb = new HSSFWorkbook();
|
| 167 |
|
187 |
|
| Line 186... |
Line 206... |
| 186 |
headerRow.createCell(ID).setCellValue("Item Id");
|
206 |
headerRow.createCell(ID).setCellValue("Item Id");
|
| 187 |
headerRow.createCell(BRAND).setCellValue("Brand");
|
207 |
headerRow.createCell(BRAND).setCellValue("Brand");
|
| 188 |
headerRow.createCell(MODEL_NUMBER).setCellValue("Model Number");
|
208 |
headerRow.createCell(MODEL_NUMBER).setCellValue("Model Number");
|
| 189 |
headerRow.createCell(MODEL_NAME).setCellValue("Model Name");
|
209 |
headerRow.createCell(MODEL_NAME).setCellValue("Model Name");
|
| 190 |
headerRow.createCell(COLOR).setCellValue("Color");
|
210 |
headerRow.createCell(COLOR).setCellValue("Color");
|
| - |
|
211 |
headerRow.createCell(TOTAL_AVAILABILITY).setCellValue("Total Availability");
|
| - |
|
212 |
headerRow.createCell(TOTAL_RESERVED).setCellValue("Total Reserved");
|
| - |
|
213 |
headerRow.createCell(WH1_AVAILABILITY).setCellValue("WH1 Availabality");
|
| - |
|
214 |
headerRow.createCell(WH1_RESERVED).setCellValue("WH1 Reserved");
|
| - |
|
215 |
headerRow.createCell(WH5_AVALABILITY).setCellValue("WH5 Availabality");
|
| - |
|
216 |
headerRow.createCell(WH5_RESERVED).setCellValue("WH5 Reserved");
|
| 191 |
headerRow.createCell(QUANTITY).setCellValue("Quantity");
|
217 |
headerRow.createCell(WH7_AVALABILITY).setCellValue("WH7 Availabality");
|
| - |
|
218 |
headerRow.createCell(WH7_RESERVED).setCellValue("WH7 Reserved");
|
| - |
|
219 |
|
| - |
|
220 |
// headerRow.createCell(WH2_A).setCellValue("WH2 Availabality");
|
| - |
|
221 |
// headerRow.createCell(WH3_A).setCellValue("WH3 Availabality");
|
| - |
|
222 |
// headerRow.createCell(WH4_A).setCellValue("WH4 Availabality");
|
| - |
|
223 |
// headerRow.createCell(WH2_R).setCellValue("WH2 Reserved");
|
| - |
|
224 |
// headerRow.createCell(WH3_R).setCellValue("WH3 Reserved");
|
| - |
|
225 |
// headerRow.createCell(WH4_R).setCellValue("WH4 Reserved");
|
| 192 |
|
226 |
|
| 193 |
for(RowItem item : items){
|
227 |
for(RowItem item : items){
|
| 194 |
Row contentRow = sheet.createRow(serialNo++);
|
228 |
Row contentRow = sheet.createRow(serialNo++);
|
| 195 |
contentRow.createCell(ID).setCellValue(item.id);
|
229 |
contentRow.createCell(ID).setCellValue(item.id);
|
| 196 |
contentRow.createCell(BRAND).setCellValue(item.brand);
|
230 |
contentRow.createCell(BRAND).setCellValue(item.brand);
|
| 197 |
contentRow.createCell(MODEL_NUMBER).setCellValue(item.modelNumber);
|
231 |
contentRow.createCell(MODEL_NUMBER).setCellValue(item.modelNumber);
|
| 198 |
contentRow.createCell(MODEL_NAME).setCellValue(item.modelName);
|
232 |
contentRow.createCell(MODEL_NAME).setCellValue(item.modelName);
|
| 199 |
contentRow.createCell(COLOR).setCellValue(item.color);
|
233 |
contentRow.createCell(COLOR).setCellValue(item.color);
|
| - |
|
234 |
contentRow.createCell(TOTAL_AVAILABILITY).setCellValue(item.totalAvailability);
|
| - |
|
235 |
contentRow.createCell(TOTAL_RESERVED).setCellValue(item.totalReserved);
|
| - |
|
236 |
contentRow.createCell(WH1_AVAILABILITY).setCellValue(item.wh1Availability);
|
| - |
|
237 |
contentRow.createCell(WH1_RESERVED).setCellValue(item.wh1Reserved);
|
| 200 |
contentRow.createCell(QUANTITY).setCellValue(item.quantity);
|
238 |
contentRow.createCell(WH5_AVALABILITY).setCellValue(item.wh5Availability);
|
| - |
|
239 |
contentRow.createCell(WH5_RESERVED).setCellValue(item.wh5Reserved);
|
| - |
|
240 |
contentRow.createCell(WH7_AVALABILITY).setCellValue(item.wh7Availability);
|
| - |
|
241 |
contentRow.createCell(WH7_RESERVED).setCellValue(item.wh7Reserved);
|
| 201 |
}
|
242 |
}
|
| 202 |
}
|
243 |
}
|
| 203 |
|
244 |
|
| 204 |
public String getErrorMsg() {
|
245 |
public String getErrorMsg() {
|
| 205 |
return errorMsg;
|
246 |
return errorMsg;
|
| Line 245... |
Line 286... |
| 245 |
public long id;
|
286 |
public long id;
|
| 246 |
public String brand;
|
287 |
public String brand;
|
| 247 |
public String modelNumber;
|
288 |
public String modelNumber;
|
| 248 |
public String modelName;
|
289 |
public String modelName;
|
| 249 |
public String color;
|
290 |
public String color;
|
| - |
|
291 |
public long totalAvailability = 0;
|
| - |
|
292 |
public long totalReserved = 0;
|
| 250 |
public long quantity;
|
293 |
public long wh1Availability = 0;
|
| - |
|
294 |
public long wh1Reserved = 0;
|
| - |
|
295 |
public long wh5Availability = 0;
|
| - |
|
296 |
public long wh5Reserved = 0;
|
| - |
|
297 |
public long wh7Availability = 0;
|
| - |
|
298 |
public long wh7Reserved = 0;
|
| 251 |
|
299 |
|
| 252 |
public RowItem(Item item){
|
300 |
public RowItem(Item item){
|
| 253 |
id = item.getId();
|
301 |
id = item.getId();
|
| 254 |
brand = item.getBrand();
|
302 |
brand = item.getBrand();
|
| 255 |
modelNumber = item.getModelNumber();
|
303 |
modelNumber = item.getModelNumber();
|
| 256 |
modelName = item.getModelName();
|
304 |
modelName = item.getModelName();
|
| 257 |
color = item.getColor();
|
305 |
color = item.getColor();
|
| 258 |
quantity = 0;
|
306 |
totalAvailability = 0;
|
| - |
|
307 |
ItemInventory itemInventory = item.getItemInventory();
|
| 259 |
for(Map.Entry<Long, Long> entry : item.getItemInventory().getAvailability().entrySet()){
|
308 |
for(Map.Entry<Long, Long> entry : itemInventory.getAvailability().entrySet()){
|
| - |
|
309 |
long value = entry.getValue();
|
| - |
|
310 |
switch (entry.getKey().intValue()) {
|
| - |
|
311 |
case 1:
|
| - |
|
312 |
wh1Availability = value;
|
| - |
|
313 |
break;
|
| - |
|
314 |
case 5:
|
| - |
|
315 |
wh5Availability = value;
|
| - |
|
316 |
break;
|
| - |
|
317 |
case 7:
|
| - |
|
318 |
wh7Availability = value;
|
| - |
|
319 |
break;
|
| - |
|
320 |
default:
|
| - |
|
321 |
break;
|
| - |
|
322 |
}
|
| - |
|
323 |
totalAvailability += value;
|
| - |
|
324 |
}
|
| - |
|
325 |
for(Map.Entry<Long, Long> entry : itemInventory.getReserved().entrySet()){
|
| 260 |
quantity += entry.getValue();
|
326 |
long value = entry.getValue();
|
| - |
|
327 |
switch (entry.getKey().intValue()) {
|
| - |
|
328 |
case 1:
|
| - |
|
329 |
wh1Reserved = value;
|
| - |
|
330 |
break;
|
| - |
|
331 |
case 5:
|
| - |
|
332 |
wh5Reserved = value;
|
| - |
|
333 |
break;
|
| - |
|
334 |
case 7:
|
| - |
|
335 |
wh7Reserved = value;
|
| - |
|
336 |
break;
|
| - |
|
337 |
default:
|
| - |
|
338 |
break;
|
| - |
|
339 |
}
|
| - |
|
340 |
totalReserved += value;
|
| 261 |
}
|
341 |
}
|
| 262 |
}
|
342 |
}
|
| 263 |
}
|
343 |
}
|
| 264 |
}
|
344 |
}
|