Subversion Repositories SmartDukaan

Rev

Rev 7448 | Rev 7452 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7410 amar.kumar 1
package in.shop2020.inventory.controllers;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.catalog.ItemType;
7451 amar.kumar 7
import in.shop2020.model.v1.inventory.InventoryService.Client;
7410 amar.kumar 8
import in.shop2020.model.v1.inventory.InventoryServiceException;
9
import in.shop2020.model.v1.inventory.Warehouse;
10
import in.shop2020.model.v1.inventory.WarehouseType;
11
import in.shop2020.model.v1.order.LineItem;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.InventoryClient;
14
import in.shop2020.thrift.clients.WarehouseClient;
15
import in.shop2020.warehouse.InventoryItem;
16
import in.shop2020.warehouse.ScanType;
17
import in.shop2020.warehouse.TransferLot;
18
import in.shop2020.warehouse.WarehouseService;
19
import in.shop2020.warehouse.WarehouseServiceException;
20
 
7451 amar.kumar 21
import java.io.BufferedInputStream;
22
import java.io.ByteArrayOutputStream;
23
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.FileNotFoundException;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
7410 amar.kumar 29
import java.text.ParseException;
30
import java.text.SimpleDateFormat;
7451 amar.kumar 31
import java.util.ArrayList;
7410 amar.kumar 32
import java.util.Calendar;
7451 amar.kumar 33
import java.util.Date;
7410 amar.kumar 34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Map.Entry;
38
 
7451 amar.kumar 39
import javax.servlet.ServletOutputStream;
40
 
41
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
42
import org.apache.poi.ss.usermodel.Row;
43
import org.apache.poi.ss.usermodel.Sheet;
44
import org.apache.poi.ss.usermodel.Workbook;
7410 amar.kumar 45
import org.apache.thrift.TException;
46
import org.apache.thrift.transport.TTransportException;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49
 
50
public class TransferLotController extends BaseController{
51
 
52
	/**
53
	 * 
54
	 */
55
	private static final long serialVersionUID = 3052114881399915348L;
56
 
57
	private Long id;
58
	private TransferLot transferLot;
59
	private List<TransferLot>transferLots;
60
	private String remoteTransferRefNumber;
61
	private String fromDate;
62
	private String toDate;
63
	private String errorMessage = "";
64
	private Calendar transferLotFromDate;
65
	private Calendar transferLotToDate;
66
	private Long originWarehouseId;
67
	private List<LineItem> lineItems;
68
	private Long destinationWarehouseId;
69
 
70
	private String output;
71
	private static Map<Long, Warehouse> warehouseMap;
72
 
73
	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
74
 
75
    private static Logger logger = LoggerFactory.getLogger(TransferLotController.class);
76
 
77
    static {
78
		warehouseMap = new HashMap<Long, Warehouse>();
79
		InventoryClient inventoryServiceClient;
80
		try {
81
			inventoryServiceClient = new InventoryClient();
82
	        Client inventoryClient = inventoryServiceClient.getClient();
83
	        List<Warehouse> warehouses;
84
			warehouses = inventoryClient.getAllWarehouses(true);
85
	        for(Warehouse warehouse : warehouses){
86
	        	warehouseMap.put(warehouse.getId(), warehouse);
87
	        }
88
		} catch (TTransportException e) {
89
			logger.error("Error in populating warehouse map", e);
90
		} catch (TException e) {
91
			logger.error("Error in populating warehouse map", e);
92
		} catch (InventoryServiceException isex) {
93
			logger.error("Error in populating warehouse map", isex);
94
		}
95
	}
96
 
97
	public String index(){
98
		if(warehouseMap==null||warehouseMap.size()<1){
7448 amar.kumar 99
			fetchWarehouseMap();
7410 amar.kumar 100
		}
101
		try{
102
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
103
			transferLotFromDate = Calendar.getInstance();
104
			transferLotToDate = Calendar.getInstance();
105
			if(fromDate==null) {
106
				transferLotFromDate.add(Calendar.MONTH, -1);
107
			} else {
108
				transferLotFromDate.setTime(formatter.parse(fromDate));
109
				if(toDate!=null) {
110
					transferLotToDate.setTime(formatter.parse(toDate));
111
				}
112
			}
113
			transferLots = warehouseClient.getTransferLotsByDate(transferLotFromDate.getTimeInMillis(), transferLotToDate.getTimeInMillis());
114
		} catch(TException e){
115
			logger.error("Error in getting transfer lots", e);
116
		} catch(ParseException pex) {
117
			logger.error("Error in parsing time", pex);
118
		} catch(WarehouseServiceException wsex) {
119
			logger.error("Error in getting transfer lots", wsex);
120
		}
121
		return INDEX;
122
	}
123
 
7448 amar.kumar 124
    private void fetchWarehouseMap() {
125
    	warehouseMap = new HashMap<Long, Warehouse>();
126
		InventoryClient inventoryServiceClient;
127
		try {
128
			inventoryServiceClient = new InventoryClient();
129
	        Client inventoryClient = inventoryServiceClient.getClient();
130
	        List<Warehouse> warehouses;
131
			warehouses = inventoryClient.getAllWarehouses(true);
132
	        for(Warehouse warehouse : warehouses){
133
	        	warehouseMap.put(warehouse.getId(), warehouse);
134
	        }
135
		} catch (TTransportException e) {
136
			logger.error("Error in populating warehouse map", e);
137
		} catch (TException e) {
138
			logger.error("Error in populating warehouse map", e);
139
		} catch (InventoryServiceException isex) {
140
			logger.error("Error in populating warehouse map", isex);
141
		}
142
	}
143
 
144
	public String create() {
7410 amar.kumar 145
    	originWarehouseId = Long.parseLong(request.getParameter("originWarehouseId"));
146
    	destinationWarehouseId = Long.parseLong(request.getParameter("destinationWarehouseId"));
147
    	try {
148
	    	WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
149
	        id = warehouseClient.createTransferLot(originWarehouseId, destinationWarehouseId);
150
    	} catch (TException tex) {
151
    		logger.error("Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId, tex);
152
    		errorMessage = "Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId;
153
    	} catch (WarehouseServiceException wex) {
154
    		logger.error("Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId, wex);
155
    		errorMessage = "Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId;
156
    	}
157
        return show();
158
    }
159
 
160
	public String show(){
161
		resetLineItems();
162
		return SHOW;
163
	}
164
	public String update() {
165
		try {
166
			if(!areValidScans()){
167
				errorMessage = "Invalid Scans";
168
				return show();
169
			}
170
			if(id == null || id==0) {
171
	            	errorMessage = "No transferLot-Id selected for Transfer";
172
	        		return "new";
173
	        }
174
			List<InventoryItem> inventoryItems = new ArrayList<InventoryItem>();
175
			CatalogService.Client catalogClient = new CatalogClient().getClient();
176
			for (LineItem lineItem : lineItems) {
177
                if (lineItem.getItem_id() == 0) {
178
                    continue;
179
                }
180
                InventoryItem inventoryItem = new InventoryItem();
181
                inventoryItem.setItemId(lineItem.getItem_id());
182
                inventoryItem.setCurrentQuantity(new Double(lineItem.getQuantity()).longValue());
183
                inventoryItem.setItemNumber(lineItem.getItem_number());
184
                if(catalogClient.getItem(lineItem.getItem_id()).getType()==ItemType.SERIALIZED) {
185
                	inventoryItem.setSerialNumber(lineItem.getSerial_number());
186
                }
187
                inventoryItems.add(inventoryItem);
188
            }
189
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
7448 amar.kumar 190
			warehouseClient.scanForTransferOut(inventoryItems, ScanType.WAREHOUSE_TRANSFER_OUT, id);
7410 amar.kumar 191
		} catch (TTransportException e) {
192
            errorMessage = "Error while establishing connection to the warehouse server";
193
            logger.error(errorMessage, e);
194
        } catch (WarehouseServiceException e) {
195
        	errorMessage = e.getMessage();
196
            logger.error(errorMessage, e);
197
        } catch (TException e) {
198
        	errorMessage = "Error while scanning in the item";
199
            logger.error(errorMessage, e);
200
        } catch (CatalogServiceException csex) {
201
        	errorMessage = "Error while getting the serialized details of item";
202
            logger.error(errorMessage, csex);
203
        }
204
        return show();
205
	}
206
 
207
	public String editNew(){
208
		return "new";
209
	}
210
 
211
	private boolean areValidScans() throws NumberFormatException, TException {
212
        boolean areValidScans = true;
213
        return areValidScans;
214
	}
215
 
216
	private void resetLineItems() {
217
        lineItems = new ArrayList<LineItem>();
218
 
219
        for (int i = 0; i < 11; i++) {
220
            LineItem lineItem = new LineItem();
221
            lineItem.setId(i);
222
            lineItem.setExtra_info("");
223
            lineItem.setSerial_number("");
224
            lineItem.setItem_number("");
225
            lineItem.setQuantity(1);
226
            lineItem.setItem_id(-1);
227
            lineItems.add(lineItem);
228
        }
229
    }
230
 
231
	public String getTransferLotItems() {
232
		try {
233
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
234
			Map<Long, Long> transferLotItems = warehouseClient.getItemsInTransferLot(id);
235
			CatalogService.Client catalogClient = new CatalogClient().getClient();
236
			String transferLotItemDiv = "<div id = 'transfer-lot-item-list-div' align='center'><table id = 'transfer-lot-item-list-table'><tr><th>Item</th><th>Quantity</th></tr>";
237
			for(Long itemId : transferLotItems.keySet()){
238
				Item item = catalogClient.getItem(itemId);
239
				transferLotItemDiv = transferLotItemDiv + "<tr><td>" + item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + " " + item.getColor();
240
				transferLotItemDiv = transferLotItemDiv  + "</td><td>" + transferLotItems.get(itemId) +"</td></tr>";
241
			}
242
			transferLotItemDiv = transferLotItemDiv + "</table></div>";
243
			setOutput(transferLotItemDiv);
244
		} catch (TException tex) {
7451 amar.kumar 245
			logger.error("Error in getting transfer lot items",tex);
7410 amar.kumar 246
		} catch (CatalogServiceException csex) {
7451 amar.kumar 247
			logger.error("Error in getting transfer lot items",csex);
7410 amar.kumar 248
		}
249
		return OUTPUT;
250
	}
251
 
252
	public String markTransferLotAsReceived() throws Exception {
253
		try{
254
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
255
			warehouseClient.markItemsAsReceivedForTransferLot(id);
256
			warehouseClient.markTransferLotAsReceived(id, remoteTransferRefNumber);
257
		} catch(TException e){
258
			logger.error("Error in marking transfer lot as received",e);
259
			throw new Exception("Error in marking transfer lot as received");
260
		} catch(WarehouseServiceException wsex) {
261
			logger.error("Error in marking transfer lot as received",wsex);
262
			throw new Exception("Error in marking transfer lot as received");
263
		}
264
		return INDEX;
265
	}
266
 
7451 amar.kumar 267
	public void downloadFBASheet() {
268
		try {
269
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
270
			Map<Long, Long> transferLotItems = warehouseClient.getItemsInTransferLot(id);
271
 
272
			File fbaSheetFile = new File("transfer-lot-id-sheet"+new Date());
273
			FileOutputStream fStream = null;
274
			try {
275
				fStream = new FileOutputStream(fbaSheetFile);
276
			} catch (FileNotFoundException e1) {
277
				logger.error(e1.getMessage());
278
			}
279
	    	ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
280
		    Workbook wb = new HSSFWorkbook();
281
		    Sheet fbaSheet = wb.createSheet("FBA Sheet");
282
 
283
		    Row merchantShipmentName = fbaSheet.createRow((short)0);
284
		    merchantShipmentName.createCell(0).setCellValue("MerchantShipmentName");
285
		    Row labelPrepPreference = fbaSheet.createRow((short)1);
286
		    labelPrepPreference.createCell(0).setCellValue("LabelPrepPreference");
287
		    Row addressName = fbaSheet.createRow((short)2);
288
		    addressName.createCell(0).setCellValue("AddressName");
289
		    Row addressFieldOne = fbaSheet.createRow((short)3);
290
		    addressFieldOne.createCell(0).setCellValue("AddressFieldOne");
291
		    Row addressFieldTwo = fbaSheet.createRow((short)4);
292
		    addressFieldTwo.createCell(0).setCellValue("AddressFieldTwo");
293
		    Row addressCity = fbaSheet.createRow((short)5);
294
		    addressCity.createCell(0).setCellValue("AddressCity");
295
		    Row addressCountryCode = fbaSheet.createRow((short)6);
296
		    addressCountryCode.createCell(0).setCellValue("AddressCountryCode");
297
		    Row addressStateOrRegion = fbaSheet.createRow((short)7);
298
		    addressStateOrRegion.createCell(0).setCellValue("AddressStateOrRegion");
299
		    Row addressPostalCode = fbaSheet.createRow((short)8);
300
		    addressPostalCode.createCell(0).setCellValue("AddressPostalCode");
301
		    Row addressDistrict = fbaSheet.createRow((short)9);
302
		    addressDistrict.createCell(0).setCellValue("AddressDistrict");
303
 
304
		    Row blankRow = fbaSheet.createRow((short)10);
305
		    //blankRow.createCell(0).setCellValue("AddressPostalCode");
306
 
307
		    Row skuHeaders = fbaSheet.createRow((short)11);
308
		    skuHeaders.createCell(0).setCellValue("MerchantSKU");
309
		    skuHeaders.createCell(1).setCellValue("Quantity");
310
 
311
		    int rowCount = 12;
312
		    for(Entry<Long, Long> itemQuantity : transferLotItems.entrySet()) {
313
		    	Row newRow = fbaSheet.createRow((short)rowCount);
314
		    	newRow.createCell(0).setCellValue("FBA"+itemQuantity.getKey());
315
			    newRow.createCell(1).setCellValue(itemQuantity.getValue());
316
			    rowCount++;
317
		    }
318
 
319
		    try {
320
			    wb.write(baosXLS);
321
			    baosXLS.close();
322
			    baosXLS.writeTo(fStream);
323
			    fStream.flush();
324
				fStream.close();
325
		    } catch(IOException e) {
326
		    	//TODO
327
		    }
328
		    byte[] buffer = null;
329
            buffer = new byte[(int) fbaSheetFile.length()];
330
            InputStream input = null;
331
            try {
332
                int totalBytesRead = 0;
333
                input = new BufferedInputStream(new FileInputStream(fbaSheetFile));
334
                while (totalBytesRead < buffer.length) {
335
                    int bytesRemaining = buffer.length - totalBytesRead;
336
                    // input.read() returns -1, 0, or more :
337
                    int bytesRead = input.read(buffer, totalBytesRead,
338
                            bytesRemaining);
339
                    if (bytesRead > 0) {
340
                        totalBytesRead = totalBytesRead + bytesRead;
341
                    }
342
                }
343
 
344
                 /* the above style is a bit tricky: it places bytes into the
345
                 * 'buffer' array; 'buffer' is an output parameter; the while
346
                 * loop usually has a single iteration only.*/
347
 
348
            } finally {
349
                input.close();
350
            }
351
 
352
            response.setContentType("application/vnd.ms-excel");
353
            response.setHeader("Content-disposition", "inline; filename="
354
                    + fbaSheetFile.getName());
355
 
356
            ServletOutputStream sos = response.getOutputStream();
357
            sos.write(buffer);
358
            sos.flush();
359
 
360
		} catch (TException tex) {
361
			logger.error("Error in generating fba sheet for transfer Lot Id " + id,tex);
362
		}  catch (FileNotFoundException fnfex) {
363
			logger.error("Error in generating fba sheet for transfer Lot Id " + id,fnfex);
364
		} catch (IOException ioex) {
365
			logger.error("Error in generating fba sheet for transfer Lot Id " + id,ioex);
366
		}
367
 
368
	}
369
 
7410 amar.kumar 370
	public String getWarehouseName(Long warehouseId){
371
		return warehouseMap.get(warehouseId).getDisplayName();
372
	}
373
 
374
	public boolean isTransferReceivable(TransferLot transferLot) {
375
		if(warehouseMap.get(transferLot.getDestinationWarehouseId()).getWarehouseType()==WarehouseType.OURS_THIRDPARTY||
376
				warehouseMap.get(transferLot.getDestinationWarehouseId()).getWarehouseType()==WarehouseType.THIRD_PARTY) {
377
			return false;
378
		} else {
379
			return true;
380
		}
381
	}
382
 
383
 
384
 
385
 
386
	public String getDateTime(long milliseconds) {
387
        Calendar cal = Calendar.getInstance();
388
        cal.setTimeInMillis(milliseconds);
389
        return formatter.format(cal.getTime());
390
    }
391
 
392
	public static void setWarehouseMap(Map<Long, Warehouse> warehouseMap) {
393
		TransferLotController.warehouseMap = warehouseMap;
394
	}
395
 
396
	public TransferLot getTransferLot() {
397
		return transferLot;
398
	}
399
 
400
	public void setTransferLot(TransferLot transferLot) {
401
		this.transferLot = transferLot;
402
	}
403
 
404
	public List<TransferLot> getTransferLots() {
405
		return transferLots;
406
	}
407
 
408
	public void setTransferLots(List<TransferLot> transferLots) {
409
		this.transferLots = transferLots;
410
	}
411
 
412
	public String getRemoteTransferRefNumber() {
413
		return remoteTransferRefNumber;
414
	}
415
 
416
	public void setRemoteTransferRefNumber(String remoteTransferRefNumber) {
417
		this.remoteTransferRefNumber = remoteTransferRefNumber;
418
	}
419
 
420
	public Long getId() {
421
		return id;
422
	}
423
 
424
	public void setId(Long id) {
425
		this.id = id;
426
	}
427
 
428
	public String getOutput() {
429
		return output;
430
	}
431
 
432
	public void setOutput(String output) {
433
		this.output = output;
434
	}
435
 
436
	public Long getOriginWarehouseId() {
437
		return originWarehouseId;
438
	}
439
 
440
	public void setOriginWarehouseId(Long originWarehouseId) {
441
		this.originWarehouseId = originWarehouseId;
442
	}
443
 
444
	public Long getDestinationWarehouseId() {
445
		return destinationWarehouseId;
446
	}
447
 
448
	public void setDestinationWarehouseId(Long destinationWarehouseId) {
449
		this.destinationWarehouseId = destinationWarehouseId;
450
	}
451
 
452
	public String getErrorMessage() {
453
		return errorMessage;
454
	}
455
 
456
	public void setErrorMsg(String errorMessage) {
457
		this.errorMessage = errorMessage;
458
	}
459
 
460
	public List<LineItem> getLineItems() {
461
		return lineItems;
462
	}
463
 
464
	public void setLineItems(List<LineItem> lineItems) {
465
		this.lineItems = lineItems;
466
	}
467
 
468
 
469
 
470
}