Subversion Repositories SmartDukaan

Rev

Rev 7448 | Go to most recent revision | Details | 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.inventory.controllers.PurchaseController.ScanRecordType;
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;
8
import in.shop2020.model.v1.inventory.InventoryServiceException;
9
import in.shop2020.model.v1.inventory.Vendor;
10
import in.shop2020.model.v1.inventory.InventoryService.Client;
11
import in.shop2020.model.v1.inventory.Warehouse;
12
import in.shop2020.model.v1.inventory.WarehouseType;
13
import in.shop2020.model.v1.order.LineItem;
14
import in.shop2020.purchase.PurchaseReturn;
15
import in.shop2020.purchase.PurchaseServiceException;
16
import in.shop2020.thrift.clients.CatalogClient;
17
import in.shop2020.thrift.clients.InventoryClient;
18
import in.shop2020.thrift.clients.PurchaseClient;
19
import in.shop2020.thrift.clients.WarehouseClient;
20
import in.shop2020.warehouse.InventoryItem;
21
import in.shop2020.warehouse.Scan;
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
 
27
import java.text.DateFormat;
28
import java.text.ParseException;
29
import java.text.SimpleDateFormat;
30
import java.util.Calendar;
31
import java.util.HashMap;
32
import java.util.Hashtable;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Map.Entry;
36
import java.util.Set;
37
import java.util.ArrayList;
38
 
39
import org.apache.thrift.TException;
40
import org.apache.thrift.transport.TTransportException;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43
 
44
public class TransferLotController extends BaseController{
45
 
46
	/**
47
	 * 
48
	 */
49
	private static final long serialVersionUID = 3052114881399915348L;
50
 
51
	private Long id;
52
	private TransferLot transferLot;
53
	private List<TransferLot>transferLots;
54
	private String remoteTransferRefNumber;
55
	private String fromDate;
56
	private String toDate;
57
	private String errorMessage = "";
58
	private Calendar transferLotFromDate;
59
	private Calendar transferLotToDate;
60
	private Long originWarehouseId;
61
	private List<LineItem> lineItems;
62
	private Long destinationWarehouseId;
63
 
64
	private String output;
65
	private static Map<Long, Warehouse> warehouseMap;
66
 
67
	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
68
 
69
    private static Logger logger = LoggerFactory.getLogger(TransferLotController.class);
70
 
71
    static {
72
		warehouseMap = new HashMap<Long, Warehouse>();
73
		InventoryClient inventoryServiceClient;
74
		try {
75
			inventoryServiceClient = new InventoryClient();
76
	        Client inventoryClient = inventoryServiceClient.getClient();
77
	        List<Warehouse> warehouses;
78
			warehouses = inventoryClient.getAllWarehouses(true);
79
	        for(Warehouse warehouse : warehouses){
80
	        	warehouseMap.put(warehouse.getId(), warehouse);
81
	        }
82
		} catch (TTransportException e) {
83
			logger.error("Error in populating warehouse map", e);
84
		} catch (TException e) {
85
			logger.error("Error in populating warehouse map", e);
86
		} catch (InventoryServiceException isex) {
87
			logger.error("Error in populating warehouse map", isex);
88
		}
89
	}
90
 
91
	public String index(){
92
		if(warehouseMap==null||warehouseMap.size()<1){
93
	//TODO		fetchWarehouseMap();
94
		}
95
		try{
96
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
97
			transferLotFromDate = Calendar.getInstance();
98
			transferLotToDate = Calendar.getInstance();
99
			if(fromDate==null) {
100
				transferLotFromDate.add(Calendar.MONTH, -1);
101
			} else {
102
				transferLotFromDate.setTime(formatter.parse(fromDate));
103
				if(toDate!=null) {
104
					transferLotToDate.setTime(formatter.parse(toDate));
105
				}
106
			}
107
			transferLots = warehouseClient.getTransferLotsByDate(transferLotFromDate.getTimeInMillis(), transferLotToDate.getTimeInMillis());
108
		} catch(TException e){
109
			logger.error("Error in getting transfer lots", e);
110
		} catch(ParseException pex) {
111
			logger.error("Error in parsing time", pex);
112
		} catch(WarehouseServiceException wsex) {
113
			logger.error("Error in getting transfer lots", wsex);
114
		}
115
		return INDEX;
116
	}
117
 
118
    public String create() {
119
    	originWarehouseId = Long.parseLong(request.getParameter("originWarehouseId"));
120
    	destinationWarehouseId = Long.parseLong(request.getParameter("destinationWarehouseId"));
121
    	try {
122
	    	WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
123
	        id = warehouseClient.createTransferLot(originWarehouseId, destinationWarehouseId);
124
    	} catch (TException tex) {
125
    		logger.error("Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId, tex);
126
    		errorMessage = "Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId;
127
    	} catch (WarehouseServiceException wex) {
128
    		logger.error("Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId, wex);
129
    		errorMessage = "Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId;
130
    	}
131
        return show();
132
    }
133
 
134
	public String show(){
135
		resetLineItems();
136
		return SHOW;
137
	}
138
	public String update() {
139
		try {
140
			if(!areValidScans()){
141
				errorMessage = "Invalid Scans";
142
				return show();
143
			}
144
			if(id == null || id==0) {
145
	            //if(!createPurchase()) {
146
	            	errorMessage = "No transferLot-Id selected for Transfer";
147
	        		return "new";
148
	        	//}
149
	        }
150
			List<InventoryItem> inventoryItems = new ArrayList<InventoryItem>();
151
			CatalogService.Client catalogClient = new CatalogClient().getClient();
152
			for (LineItem lineItem : lineItems) {
153
                if (lineItem.getItem_id() == 0) {
154
                    continue;
155
                }
156
                InventoryItem inventoryItem = new InventoryItem();
157
                inventoryItem.setItemId(lineItem.getItem_id());
158
                inventoryItem.setCurrentQuantity(new Double(lineItem.getQuantity()).longValue());
159
                inventoryItem.setItemNumber(lineItem.getItem_number());
160
                if(catalogClient.getItem(lineItem.getItem_id()).getType()==ItemType.SERIALIZED) {
161
                	inventoryItem.setSerialNumber(lineItem.getSerial_number());
162
                }
163
                inventoryItems.add(inventoryItem);
164
            }
165
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
166
			warehouseClient.scanForTransfer(inventoryItems, ScanType.WAREHOUSE_TRANSFER_OUT, id);
167
		} catch (TTransportException e) {
168
            errorMessage = "Error while establishing connection to the warehouse server";
169
            logger.error(errorMessage, e);
170
        } catch (WarehouseServiceException e) {
171
        	errorMessage = e.getMessage();
172
            logger.error(errorMessage, e);
173
        } catch (TException e) {
174
        	errorMessage = "Error while scanning in the item";
175
            logger.error(errorMessage, e);
176
        } catch (CatalogServiceException csex) {
177
        	errorMessage = "Error while getting the serialized details of item";
178
            logger.error(errorMessage, csex);
179
        }
180
        return show();
181
	}
182
 
183
	public String editNew(){
184
		return "new";
185
	}
186
 
187
	private boolean areValidScans() throws NumberFormatException, TException {
188
        boolean areValidScans = true;
189
        return areValidScans;
190
	}
191
 
192
	private void resetLineItems() {
193
        lineItems = new ArrayList<LineItem>();
194
 
195
        for (int i = 0; i < 11; i++) {
196
            LineItem lineItem = new LineItem();
197
            lineItem.setId(i);
198
            lineItem.setExtra_info("");
199
            lineItem.setSerial_number("");
200
            lineItem.setItem_number("");
201
            lineItem.setQuantity(1);
202
            lineItem.setItem_id(-1);
203
            lineItems.add(lineItem);
204
        }
205
    }
206
 
207
	public String getTransferLotItems() {
208
		try {
209
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
210
			Map<Long, Long> transferLotItems = warehouseClient.getItemsInTransferLot(id);
211
			CatalogService.Client catalogClient = new CatalogClient().getClient();
212
			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>";
213
			for(Long itemId : transferLotItems.keySet()){
214
				Item item = catalogClient.getItem(itemId);
215
				transferLotItemDiv = transferLotItemDiv + "<tr><td>" + item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + " " + item.getColor();
216
				transferLotItemDiv = transferLotItemDiv  + "</td><td>" + transferLotItems.get(itemId) +"</td></tr>";
217
			}
218
			transferLotItemDiv = transferLotItemDiv + "</table></div>";
219
			setOutput(transferLotItemDiv);
220
		} catch (TException tex) {
221
			logger.error("Error in marking transfer lot as received",tex);
222
		} catch (CatalogServiceException csex) {
223
			logger.error("Error in marking transfer lot as received",csex);
224
		}
225
		return OUTPUT;
226
	}
227
 
228
	public String markTransferLotAsReceived() throws Exception {
229
		try{
230
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
231
			warehouseClient.markItemsAsReceivedForTransferLot(id);
232
			warehouseClient.markTransferLotAsReceived(id, remoteTransferRefNumber);
233
		} catch(TException e){
234
			logger.error("Error in marking transfer lot as received",e);
235
			throw new Exception("Error in marking transfer lot as received");
236
		} catch(WarehouseServiceException wsex) {
237
			logger.error("Error in marking transfer lot as received",wsex);
238
			throw new Exception("Error in marking transfer lot as received");
239
		}
240
		return INDEX;
241
	}
242
 
243
	public String getWarehouseName(Long warehouseId){
244
		return warehouseMap.get(warehouseId).getDisplayName();
245
	}
246
 
247
	public boolean isTransferReceivable(TransferLot transferLot) {
248
		if(warehouseMap.get(transferLot.getDestinationWarehouseId()).getWarehouseType()==WarehouseType.OURS_THIRDPARTY||
249
				warehouseMap.get(transferLot.getDestinationWarehouseId()).getWarehouseType()==WarehouseType.THIRD_PARTY) {
250
			return false;
251
		} else {
252
			return true;
253
		}
254
	}
255
 
256
 
257
 
258
 
259
	public String getDateTime(long milliseconds) {
260
        Calendar cal = Calendar.getInstance();
261
        cal.setTimeInMillis(milliseconds);
262
        return formatter.format(cal.getTime());
263
    }
264
 
265
	public static void setWarehouseMap(Map<Long, Warehouse> warehouseMap) {
266
		TransferLotController.warehouseMap = warehouseMap;
267
	}
268
 
269
	public TransferLot getTransferLot() {
270
		return transferLot;
271
	}
272
 
273
	public void setTransferLot(TransferLot transferLot) {
274
		this.transferLot = transferLot;
275
	}
276
 
277
	public List<TransferLot> getTransferLots() {
278
		return transferLots;
279
	}
280
 
281
	public void setTransferLots(List<TransferLot> transferLots) {
282
		this.transferLots = transferLots;
283
	}
284
 
285
	public String getRemoteTransferRefNumber() {
286
		return remoteTransferRefNumber;
287
	}
288
 
289
	public void setRemoteTransferRefNumber(String remoteTransferRefNumber) {
290
		this.remoteTransferRefNumber = remoteTransferRefNumber;
291
	}
292
 
293
	public Long getId() {
294
		return id;
295
	}
296
 
297
	public void setId(Long id) {
298
		this.id = id;
299
	}
300
 
301
	public String getOutput() {
302
		return output;
303
	}
304
 
305
	public void setOutput(String output) {
306
		this.output = output;
307
	}
308
 
309
	public Long getOriginWarehouseId() {
310
		return originWarehouseId;
311
	}
312
 
313
	public void setOriginWarehouseId(Long originWarehouseId) {
314
		this.originWarehouseId = originWarehouseId;
315
	}
316
 
317
	public Long getDestinationWarehouseId() {
318
		return destinationWarehouseId;
319
	}
320
 
321
	public void setDestinationWarehouseId(Long destinationWarehouseId) {
322
		this.destinationWarehouseId = destinationWarehouseId;
323
	}
324
 
325
	public String getErrorMessage() {
326
		return errorMessage;
327
	}
328
 
329
	public void setErrorMsg(String errorMessage) {
330
		this.errorMessage = errorMessage;
331
	}
332
 
333
	public List<LineItem> getLineItems() {
334
		return lineItems;
335
	}
336
 
337
	public void setLineItems(List<LineItem> lineItems) {
338
		this.lineItems = lineItems;
339
	}
340
 
341
 
342
 
343
}