Subversion Repositories SmartDukaan

Rev

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