Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
11349 manish.sha 1
package in.shop2020.inventory.controllers;
2
 
3
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.model.v1.catalog.ItemType;
5
import in.shop2020.model.v1.inventory.InventoryService;
6
import in.shop2020.model.v1.inventory.InventoryType;
7
import in.shop2020.model.v1.inventory.Warehouse;
8
import in.shop2020.model.v1.inventory.WarehouseType;
9
import in.shop2020.model.v1.order.LineItem;
10
import in.shop2020.purchase.Invoice;
11
import in.shop2020.purchase.PurchaseOrder;
12
import in.shop2020.purchase.PurchaseServiceException;
13
import in.shop2020.thrift.clients.CatalogClient;
14
import in.shop2020.thrift.clients.InventoryClient;
15
import in.shop2020.thrift.clients.PurchaseClient;
16
import in.shop2020.thrift.clients.WarehouseClient;
17
import in.shop2020.utils.ModelUtils;
18
import in.shop2020.warehouse.InventoryItem;
19
import in.shop2020.warehouse.ScanType;
20
import in.shop2020.warehouse.WarehouseService;
21
import in.shop2020.warehouse.WarehouseService.Client;
22
 
23
import java.io.BufferedInputStream;
24
import java.io.BufferedReader;
25
import java.io.BufferedWriter;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileReader;
29
import java.io.FileWriter;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.util.ArrayList;
33
import java.util.HashMap;
34
import java.util.List;
35
import java.util.Map;
36
 
37
import javax.servlet.ServletContext;
38
import javax.servlet.ServletOutputStream;
39
 
40
import org.apache.commons.io.FileUtils;
41
import org.apache.commons.lang.StringUtils;
42
import org.apache.struts2.convention.annotation.Result;
43
import org.apache.struts2.convention.annotation.Results;
44
import org.apache.thrift.TException;
45
import org.apache.thrift.transport.TTransportException;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48
 
49
@SuppressWarnings("serial")
50
@Results({ @Result(name = "redirect", type = "redirectAction", params = {
51
		"actionName", "warehouse" }) })
52
		public class BulkPurchaseController extends BaseController {
53
	private static Logger logger = LoggerFactory.getLogger(BulkPurchaseController.class);
54
	private List<String> exceptionList = new ArrayList<String>();
55
	private static final int NUM_BULK__SCAN_ITEMS = 10;
11801 manish.sha 56
	private static final long DUMMY_VENDOR_ID = 40;
11349 manish.sha 57
 
58
	private ServletContext context;
59
	private String id="";
60
	private Long transferLotId;
61
	private String itemId;
62
	private String itemNo;
63
	private String errorMsg = "";
64
	private String successMsg = "";
65
	private List<Item> items;
66
	private List<LineItem> lineItems;
67
	private String bulkScanUploadItem;
18668 manish.sha 68
	private String selectItemType;
11349 manish.sha 69
	private File scanDataFile;
70
 
71
	private String invoiceNumber;
72
	private Double freightCharges ;
11801 manish.sha 73
	private String purchaseComments;
11349 manish.sha 74
	private Long poId;
75
	private String fileNameVal;
76
 
77
	private String purchaseId;
78
	private String purchaseOrderId;
79
	private Long warehouseId;
80
	private Long transferWarehouseId;
81
	private Warehouse warehouse;
11801 manish.sha 82
	private Warehouse thirdPartyWarehouse;
11349 manish.sha 83
 
84
	private Map<Long, Double> unfulfilledMap = new HashMap<Long, Double>(); 
18668 manish.sha 85
	private Map<Long, Item> nonserializedMap = new HashMap<Long, Item>();
86
	private Map<Long, Item> serializedMap = new HashMap<Long, Item>();
11349 manish.sha 87
 
88
 
18668 manish.sha 89
 
11349 manish.sha 90
	public String show() {
91
		resetLineItems();
92
		setItemsFromPO();
93
		return SHOW;
94
	}
95
 
96
	private void resetLineItems() {
97
		lineItems = new ArrayList<LineItem>();
98
 
99
		for (int i = 0; i < NUM_BULK__SCAN_ITEMS; i++) {
100
			LineItem lineItem = new LineItem();
101
			lineItem.setId(i);
102
			lineItem.setExtra_info("");
103
			lineItem.setSerial_number("");
104
			lineItem.setItem_number("");
105
			lineItem.setQuantity(1);
106
			lineItem.setItem_id(-1);
107
			lineItems.add(lineItem);
108
		}
109
	}
110
 
111
	public String create() {
112
		this.purchaseOrderId = request.getParameter("poId");
113
		poId = Long.parseLong(purchaseOrderId);
114
		invoiceNumber = request.getParameter("invoiceNumber");
115
		String fc = request.getParameter("freightCharges").trim();
116
		freightCharges = 0D;
117
		if(id == null ||StringUtils.isEmpty(id))
118
			id = "0";
119
		if (fc != null && !fc.isEmpty())
120
			freightCharges = Double.parseDouble(fc);
121
		return show();
122
		//return "create";
123
 
124
	}
125
 
126
	public boolean createPurchase(){
127
		try {
11801 manish.sha 128
			logger.info("poId="+poId+" invoiceNumber="+invoiceNumber+" freightCharges="+freightCharges+ " purchaseComments="+purchaseComments);
11349 manish.sha 129
			PurchaseClient purchaseClient = new PurchaseClient();
130
			in.shop2020.purchase.PurchaseService.Client client = purchaseClient
131
			.getClient();
11801 manish.sha 132
			id = "" + client.startPurchase(poId, invoiceNumber, freightCharges, purchaseComments);
11349 manish.sha 133
			logger.info("id = "+id);
134
		} catch (TTransportException e) {
135
			errorMsg = errorMsg + "<br> Error while establishing connection to the warehouse server";
136
			exceptionList.add("Error while establishing connection to the warehouse server");
137
			logger.error(errorMsg, e);
138
		} catch (TException e) {
139
			errorMsg = errorMsg+ "<br> Error while scanning in the item";
140
			exceptionList.add("Error while strating purchase");
141
			logger.error(errorMsg, e);
142
		} catch (PurchaseServiceException e) {
143
			errorMsg = errorMsg+ e.getMessage();
144
			exceptionList.add(e.getMessage());
145
			logger.error(errorMsg, e);
146
		}
147
 
148
		if (errorMsg.isEmpty())
149
			return true;
150
		else {
151
			addActionError(errorMsg);
152
			return false;
153
		}
154
	}
155
 
156
	public File getScanFileToRead(){
157
		File fileToCreate = null;
158
 
159
		if(scanDataFile!=null & fileNameVal !=null && !fileNameVal.isEmpty()){
160
 
161
			logger.info("File Name "+scanDataFile.getName());
162
			System.out.println("File Name "+scanDataFile.getName());
163
 
164
			logger.info("File Name Value "+fileNameVal);
165
			System.out.println("File Name Value "+fileNameVal);
166
			String fileName = fileNameVal;
167
			try {
168
				if(!fileName.substring(fileName.lastIndexOf(".")+1).equalsIgnoreCase("txt")){
169
					throw new Exception("File is not in expected TXT Format");
170
				}
171
				fileToCreate = new File("/tmp/", fileName);
172
 
173
				FileUtils.copyFile(this.scanDataFile, fileToCreate);
174
			} catch (Exception e) {
175
				logger.error("Error while writing file used to the local file system", e);
176
				errorMsg = e.getMessage();
177
				//addActionError(errorMsg);
178
				return null;
179
			}
180
		} else {
181
			errorMsg = errorMsg + "<br>Either No File Uploaded or file name is blank";
182
		}
183
		return fileToCreate;
184
	}
185
 
186
	public boolean transferLotCreate(PurchaseOrder po){
187
		String errMsg  = "";
188
		try{
189
			if(transferLotId==null || transferLotId==0) {
190
				if(transferWarehouseId!=null && transferWarehouseId!=0) {
191
					WarehouseClient warehouseClient = new WarehouseClient();
192
					Client client = warehouseClient.getClient();
193
					InventoryService.Client inventoryClient = new InventoryClient().getClient();
194
					Warehouse fulfilmentWarehouse = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, po.getSupplierId(), po.getWarehouseId(), 0L).get(0);
195
					transferLotId = client.createTransferLot(po.getWarehouseId(), transferWarehouseId);
196
				}
197
			}
198
		} catch(Exception e){
199
			errMsg = "<br> Error while creating Transfer Lot due to : "+ e.getMessage();
200
		}
201
		if (errMsg.isEmpty()){
202
			return true;
203
		}
204
		else {
205
			errorMsg = errMsg;
206
			addActionError(errorMsg);
207
			return false;
208
		}
209
	}
210
 
211
	public File createErrorFile(List<String> errorList){
212
		try{
213
			String tmpDir = System.getProperty("java.io.tmpdir");
214
			File file = new File(tmpDir + "/ScanRecordResult.xls");
215
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
216
					file));
217
			bufferedWriter.write(StringUtils.join(new String[] { "----Scan Response----"}, '\t'));
218
			//bufferedWriter.newLine();
219
			for(String record : errorList){
220
				bufferedWriter.newLine();
221
				bufferedWriter.write(StringUtils.join(new String[] { record }, '\t'));
222
			}
223
			bufferedWriter.close();
224
			return file;
225
		} catch (Exception e) {
226
			logger.error("Could not create file for Scan Record Result", e);
227
			return null;
228
		}
229
	}
230
 
231
	public void setResponseErrorFile(File errorFile) throws IOException{
232
		byte[] buffer = null;
233
		buffer = new byte[(int) errorFile.length()];
234
		InputStream input = null;
235
		try {
236
			int totalBytesRead = 0;
237
			input = new BufferedInputStream(new FileInputStream(errorFile));
238
			while (totalBytesRead < buffer.length) {
239
				int bytesRemaining = buffer.length - totalBytesRead;
240
				// input.read() returns -1, 0, or more :
241
					int bytesRead = input.read(buffer, totalBytesRead,
242
							bytesRemaining);
243
					if (bytesRead > 0) {
244
						totalBytesRead = totalBytesRead + bytesRead;
245
					}
246
			}
247
			/*
248
			 * the above style is a bit tricky: it places bytes into the
249
			 * 'buffer' array; 'buffer' is an output parameter; the while
250
			 * loop usually has a single iteration only.
251
			 */
252
		} finally {
253
			input.close();
254
		}
255
 
256
		response.setContentType("application/vnd.ms-excel");
257
		response.setHeader("Content-disposition", "inline; filename="
258
				+ errorFile.getName());
259
 
260
		ServletOutputStream sos = response.getOutputStream();
261
		sos.write(buffer);
262
		sos.flush();
263
	}
264
 
265
	public boolean checkForException() throws IOException{
266
		if(exceptionList!=null && exceptionList.size()>0){
267
			File errorFile = createErrorFile(exceptionList);
268
			if(errorFile.isFile()){
269
				setResponseErrorFile(errorFile);
270
				return false;
271
			}
272
		}
273
		return true;
274
	}
275
 
276
	private void areValidSerializedItems() throws Exception {
277
		Client warehouseClient = new WarehouseClient().getClient();
278
		for (LineItem lineItem : lineItems) {
279
			if (StringUtils.isNotBlank(lineItem.getSerial_number())) {
280
				InventoryItem inventoryItem = new InventoryItem();
281
				try{
282
					inventoryItem = warehouseClient.getInventoryItem(lineItem.getSerial_number());
283
				} catch(Exception e){
284
					continue;
285
				}
286
				if(inventoryItem.getLastScanType()!=ScanType.PURCHASE_RETURN && inventoryItem.getLastScanType()!=ScanType.DOA_REPLACED) {
287
					lineItem.setExtra_info("Item scanned already.");
288
					exceptionList.add("Items Scanned already : "+ lineItem.getSerial_number());
289
					continue;
290
				}    			
291
			}
292
		}
293
	}
294
 
295
	private void setItemsFromPO() {
296
		try {
297
			items = new ArrayList<Item>();
298
			in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();
299
			//PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(id);
300
			PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrder(poId);
301
			warehouseId = purchaseOrder.getWarehouseId();
302
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
303
 
304
			for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {
18668 manish.sha 305
				Item it = catalogClient.getItem(lineItem.getItemId());
306
				if(it.getType()==ItemType.SERIALIZED){
307
					serializedMap.put(it.getId(), it);
308
				}else{
309
					nonserializedMap.put(it.getId(), it);
310
				}
311
				items.add(it);
11349 manish.sha 312
				unfulfilledMap.put(lineItem.getItemId(), lineItem.getUnfulfilledQuantity());
313
			}
314
 
315
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient().getClient();
11801 manish.sha 316
            thirdPartyWarehouse = inventoryClient.getWarehouse(warehouseId);
317
            if(thirdPartyWarehouse.getVendor().getId()==DUMMY_VENDOR_ID && thirdPartyWarehouse.getInventoryType()==InventoryType.GOOD && thirdPartyWarehouse.getWarehouseType()==WarehouseType.OURS_THIRDPARTY){
318
            	warehouse = thirdPartyWarehouse;
319
            } else {
320
            	warehouse = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, purchaseOrder.getSupplierId(), warehouseId, warehouseId).get(0);
321
            }
11349 manish.sha 322
		} catch (Exception e) {
323
			logger.error("Could not find items in PO with purchase: " + id, e);
324
		}
325
	}
326
 
327
	public String update() {
18668 manish.sha 328
		if(selectItemType.equalsIgnoreCase("SERIALIZED")){
329
			if(("-1").equalsIgnoreCase(bulkScanUploadItem)){
330
				errorMsg = errorMsg +" <br> No Item Selected to Scan In";
331
				return show();
332
			}
11349 manish.sha 333
		}
334
 
335
		File fileToCreate = getScanFileToRead();
336
		Map<Long, String> correctRowsMap = new HashMap<Long, String>();
337
		Map<Long, String> errorRowsMap = new HashMap<Long, String>();		
338
 
339
		if(fileToCreate==null){
340
			errorMsg = errorMsg +" <br> No File to read";
341
			return create();
342
		}
343
 
344
		ItemType itemType = ItemType.SERIALIZED;
345
		setItemsFromPO();
346
		lineItems = new ArrayList<LineItem>();
18668 manish.sha 347
 
11349 manish.sha 348
		if(fileToCreate.isFile()){
18668 manish.sha 349
 
350
			try {
11349 manish.sha 351
				in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();
352
				PurchaseOrder po = purchaseClient.getPurchaseOrder(poId);
353
				List<in.shop2020.purchase.LineItem> poLineItems = po.getLineitems();
354
				in.shop2020.purchase.LineItem poLineItem = new in.shop2020.purchase.LineItem();
355
 
356
				if(!transferLotCreate(po)){
357
					return create();
358
				}
18668 manish.sha 359
 
360
				if(selectItemType.equalsIgnoreCase("SERIALIZED")){
361
					Item item = serializedMap.get(Long.parseLong(bulkScanUploadItem));
362
					itemType = item.getType();
363
 
364
					for(in.shop2020.purchase.LineItem pItem : poLineItems){
365
						if(pItem.getItemId() == item.getId()){
366
							poLineItem = pItem;
367
							break;
11349 manish.sha 368
						}
369
					}
18668 manish.sha 370
 
371
					BufferedReader br = new BufferedReader(new FileReader(fileToCreate));
372
					long line = 1;
373
					String strLine;
374
					String[] values;
375
					while((strLine = br.readLine())!=null){
376
						if(line==1){
377
							values = strLine.split("\t");						
378
							if(values.length !=3){
379
								errorMsg = errorMsg + "<br> File contains Inappropriate Content ";
380
								addActionError(errorMsg);
381
								return create();
11349 manish.sha 382
							}
18668 manish.sha 383
							line++;
384
							continue;
385
						}
386
						values = strLine.split("\t");
387
						for(String s : values){
388
							logger.info("Row No "+line+" :"+s);
389
							System.out.println("Row No "+line+" :"+s);
390
						}
391
						LineItem lineItem = new LineItem();
392
						lineItem.setId(line);
393
						lineItem.setItem_id(item.getId());
394
						lineItem.setBrand(item.getBrand());
395
						lineItem.setColor(item.getColor());
396
						lineItem.setModel_name(item.getModelName());
397
						lineItem.setModel_number(item.getModelNumber());
398
						lineItem.setProductGroup(item.getProductGroup());
399
						lineItem.setNlc(poLineItem.getNlc());
400
						lineItem.setTransfer_price(poLineItem.getUnitPrice());
401
						lineItem.setUnit_price(poLineItem.getUnitPrice());
402
						if(itemType==ItemType.SERIALIZED){
403
							if(StringUtils.isNotBlank(values[0]) && StringUtils.isNotEmpty(values[0])){
404
								if(StringUtils.isNotBlank(values[1]) && StringUtils.isNotEmpty(values[1])){
405
									lineItem.setItem_number(values[0].trim());
406
									lineItem.setSerial_number(values[1].trim());
407
									lineItem.setQuantity(1);
408
									correctRowsMap.put(line, "Entries are correct in the row");
409
								}
410
								else{
411
									errorRowsMap.put(line, "Error: Serial Number Missing ");
412
									line++;
413
									continue;
414
								}
415
							}
11349 manish.sha 416
							else{
18668 manish.sha 417
								errorRowsMap.put(line, "Error: Item Number Missing ");
11349 manish.sha 418
								line++;
419
								continue;
420
							}
421
						}
18668 manish.sha 422
 
423
						if(correctRowsMap.containsKey(line) && !errorRowsMap.containsKey(line)){
424
							lineItems.add(lineItem);
425
						}
426
 
427
						line++;
428
					}
429
 
430
					areValidSerializedItems();
431
 
432
				}else{
433
 
434
					BufferedReader br = new BufferedReader(new FileReader(fileToCreate));
435
					long line = 1;
436
					String strLine;
437
					String[] values;
438
					while((strLine = br.readLine())!=null){
439
						if(line==1){
440
							values = strLine.split("\t");						
441
							if(values.length !=4){
442
								errorMsg = errorMsg + "<br> File contains Inappropriate Content ";
443
								addActionError(errorMsg);
444
								return create();
445
							}
11349 manish.sha 446
							line++;
447
							continue;
448
						}
18668 manish.sha 449
						values = strLine.split("\t");
450
						for(String s : values){
451
							logger.info("Row No "+line+" :"+s);
452
							System.out.println("Row No "+line+" :"+s);
453
						}
454
						Item item = nonserializedMap.get(Long.parseLong(values[0]));
455
						itemType = item.getType();
456
 
457
						for(in.shop2020.purchase.LineItem pItem : poLineItems){
458
							if(pItem.getItemId() == item.getId()){
459
								poLineItem = pItem;
460
								break;
461
							}
462
						}
463
						LineItem lineItem = new LineItem();
464
						lineItem.setId(line);
465
						lineItem.setItem_id(item.getId());
466
						lineItem.setBrand(item.getBrand());
467
						lineItem.setColor(item.getColor());
468
						lineItem.setModel_name(item.getModelName());
469
						lineItem.setModel_number(item.getModelNumber());
470
						lineItem.setProductGroup(item.getProductGroup());
471
						lineItem.setNlc(poLineItem.getNlc());
472
						lineItem.setTransfer_price(poLineItem.getUnitPrice());
473
						lineItem.setUnit_price(poLineItem.getUnitPrice());
11349 manish.sha 474
 
18668 manish.sha 475
						if(itemType==ItemType.NON_SERIALIZED){
476
							if(StringUtils.isNotBlank(values[1]) && StringUtils.isNotEmpty(values[1])){
477
								lineItem.setItem_number(values[1].trim());
478
								lineItem.setSerial_number("");
479
								if(StringUtils.isNotBlank(values[3]) && StringUtils.isNumeric(values[3]) && Double.parseDouble(values[3]) > 0){
480
									lineItem.setQuantity(Double.parseDouble(values[3].trim()));
481
									correctRowsMap.put(line, "Entries are correct in the row ");
482
								}
483
								else{
484
									errorRowsMap.put(line, "Error: Invalid Item Quantity ");
485
									line++;
486
									continue;
487
								}
488
							} else{
489
								errorRowsMap.put(line, "Error: Item Number Missing ");
11349 manish.sha 490
								line++;
491
								continue;
492
							}
18668 manish.sha 493
 
11349 manish.sha 494
						}
495
 
18668 manish.sha 496
						if(correctRowsMap.containsKey(line) && !errorRowsMap.containsKey(line)){
497
							lineItems.add(lineItem);
498
						}
11349 manish.sha 499
 
18668 manish.sha 500
						line++;
11349 manish.sha 501
					}
502
				}
18668 manish.sha 503
 
11349 manish.sha 504
				if(correctRowsMap!=null && correctRowsMap.size()>0){
505
					if(errorRowsMap!=null && errorRowsMap.size()>0){
506
						for(Long row : errorRowsMap.keySet()){
507
							exceptionList.add("At Row "+ row +" "+ errorRowsMap.get(row));
508
						}
509
					}
510
				}
511
				else{
512
					exceptionList.add("-- File Contains Zero Correct Records --");
513
					if(errorRowsMap!=null && errorRowsMap.size()>0){
514
						exceptionList.add("-- Data in uploaded sheet is Inappropriate --");
515
					}
516
				}
517
 
518
				if(id == null || Long.parseLong(id)==0) {
519
					if(!createPurchase()) {
520
						if(!checkForException()){
521
							return create();
522
						}
523
					}
18668 manish.sha 524
				}
525
 
11349 manish.sha 526
				WarehouseClient warehouseClient = new WarehouseClient();
527
				Client client = warehouseClient.getClient();
16490 manish.sha 528
				InventoryService.Client inventoryClient = new InventoryClient().getClient();
11801 manish.sha 529
				boolean checkDirectScanWarehouse = false;
530
 
531
	            if(thirdPartyWarehouse.getVendor().getId()==DUMMY_VENDOR_ID && thirdPartyWarehouse.getInventoryType()==InventoryType.GOOD && thirdPartyWarehouse.getWarehouseType()==WarehouseType.OURS_THIRDPARTY){
532
	            	checkDirectScanWarehouse = true;
533
	            }
16490 manish.sha 534
 
535
	            long currentWarehouseId = 0;
536
	            if(!checkDirectScanWarehouse){
537
	            	Warehouse fulfilmentWarehouse = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, po.getSupplierId(), po.getWarehouseId(), 0).get(0);
538
	            	currentWarehouseId = fulfilmentWarehouse.getId();
539
	            }
11801 manish.sha 540
 
11349 manish.sha 541
				for (LineItem lineItem : lineItems) {
542
 
543
					InventoryItem inventoryItem = new InventoryItem();
544
					inventoryItem.setItemId(lineItem.getItem_id());
545
					inventoryItem.setPurchaseId(Long.parseLong(id));
546
					inventoryItem.setCurrentQuantity(0);
547
					inventoryItem.setItemNumber(lineItem.getItem_number());
548
					inventoryItem.setSerialNumber(lineItem.getSerial_number());
549
					inventoryItem.setInitialQuantity(new Double(lineItem.getQuantity()).longValue());
11801 manish.sha 550
	                if(checkDirectScanWarehouse){
551
	                	inventoryItem.setPhysicalWarehouseId(po.getWarehouseId());
552
	                	inventoryItem.setCurrentWarehouseId(po.getWarehouseId());
553
	                }else{
554
	                	inventoryItem.setPhysicalWarehouseId(po.getWarehouseId());
16490 manish.sha 555
	                	inventoryItem.setCurrentWarehouseId(currentWarehouseId);
11801 manish.sha 556
	                }
11349 manish.sha 557
					if(!client.isAlive()){
558
						client = warehouseClient.getClient();
559
					}
560
					try{
18668 manish.sha 561
						if(lineItem.getSerial_number()!=null && !lineItem.getSerial_number().isEmpty()){
13089 manish.sha 562
							if(lineItem.getExtra_info()!=null && "Item scanned already.".equalsIgnoreCase(lineItem.getExtra_info())){
563
								continue;
564
							}
565
						}
11349 manish.sha 566
						client.scan(inventoryItem, ScanType.PURCHASE, new Double(lineItem.getQuantity()).longValue(), warehouseId, (transferLotId!=null)?transferLotId:0L);
567
						if(lineItem.getSerial_number()!=null && !lineItem.getSerial_number().isEmpty()){
568
							exceptionList.add("Item with Serial Number "+ lineItem.getSerial_number() + " Scanned Successfully");
569
						}
570
						else{
571
							exceptionList.add("Item with Item Number "+ lineItem.getItem_number() + " with "+ lineItem.getQuantity() + " units Scanned Successfully");
572
						}
573
					}
574
					catch(Exception e){
575
						exceptionList.add("Error "+lineItem.getId()+" "+e.getMessage());
576
						continue;
577
					}
578
				}
579
 
580
				if(lineItems!= null && lineItems.size()!=exceptionList.size()){
581
					if(!checkForException()){
582
						return null;
583
					} 
584
				}else{
585
					successMsg = "Items scanned Successfully.<br>Check the unfulfilled Quantity for further details";
586
				}
18668 manish.sha 587
 
588
			} catch (Exception e) {
11349 manish.sha 589
				errorMsg = errorMsg + " <br> Error while scanning items";
590
				logger.error(errorMsg, e);
18668 manish.sha 591
				e.printStackTrace();
11349 manish.sha 592
			}
593
		}
594
		return create();
595
	}
596
 
597
	public String destroy() {
598
		long id = Long.parseLong(this.id);
599
 
600
		try {
601
			PurchaseClient warehouseClient = new PurchaseClient();
602
			in.shop2020.purchase.PurchaseService.Client client = warehouseClient
603
			.getClient();
604
			client.closePurchase(id);
605
		} catch (TTransportException e) {
606
			errorMsg = errorMsg + "<br> Error while establishing connection to the warehouse server";
607
			logger.error(errorMsg, e);
608
		} catch (TException e) {
609
			errorMsg = errorMsg + "<br> Error while scanning in the item";
610
			logger.error(errorMsg, e);
611
		} catch (PurchaseServiceException e) {
612
			errorMsg = errorMsg + e.getMessage();
613
			logger.error(errorMsg, e);
614
		}
615
		return "redirect";
616
	}
617
 
618
	public List<Warehouse> getAllowedDestinationWarehousesForTransfer(long originWarehouseId){
619
		try {
620
			WarehouseService.Client warehouseClient = new WarehouseClient().getClient();
621
			List<Long> allowedWarehouseIds = warehouseClient.getAllowedDestinationWarehousesForTransfer(originWarehouseId);
622
			List<Warehouse> allowedWarehouses = new ArrayList<Warehouse>();
623
			InventoryService.Client inventoryClient = new InventoryClient().getClient();
624
			for(Long allowedWarehouseId : allowedWarehouseIds) {
625
				allowedWarehouses.add(inventoryClient.getWarehouse(allowedWarehouseId));
626
			}
627
			return allowedWarehouses;
628
		} catch(Exception e) {
629
			logger.error("Error while getting all destination warehouses for warehouseId " + warehouseId, e);
630
			return new ArrayList<Warehouse>();
631
		}
632
	}
633
 
634
	public boolean isItemScannedIn(long id){
635
		if(unfulfilledMap.get(id)> 0){
636
			return true;
637
		}
638
		return false;
639
	}
640
 
641
	public String getName(Item item){
642
		return ModelUtils.extractProductNameFromItem(item);
643
	}
644
 
645
	public void setId(String id) {
646
		this.id = id;
647
	}
648
 
649
	public String getId() {
650
		return id;
651
	}
652
 
653
	public String getErrorMessage() {
654
		return errorMsg;
655
	}
656
 
657
	public String getPurchaseOrderId() {
658
		return purchaseOrderId;
659
	}
660
 
661
	public String getServletContextPath() {
662
		return context.getContextPath();
663
	}
664
 
665
	public String getItemId() {
666
		return itemId;
667
	}
668
 
669
	public void setItemId(String itemId) {
670
		this.itemId = itemId;
671
	}
672
 
673
	public String getItemNo() {
674
		return itemNo;
675
	}
676
 
677
	public void setItemNo(String itemNo) {
678
		this.itemNo = itemNo;
679
	}
680
 
681
	public List<Item> getItems() {
682
		return items;
683
	}
684
 
685
	public void setItems(List<Item> items) {
686
		this.items = items;
687
	}
688
 
689
	public List<LineItem> getLineItems() {
690
		return lineItems;
691
	}
692
 
693
	public void setLineItems(List<LineItem> lineItems) {
694
		this.lineItems = lineItems;
695
	}
696
 
697
	public Warehouse getWarehouse() {
698
		return warehouse;
699
	}
700
 
701
	public String getInvoiceNumber() {
702
		return invoiceNumber;
703
	}
704
 
705
	public void setInvoiceNumber(String invoiceNumber) {
706
		this.invoiceNumber = invoiceNumber;
707
	}
708
 
709
	public Double getFreightCharges() {
710
		return freightCharges;
711
	}
712
 
713
	public void setFreightCharges(Double freightCharges) {
714
		this.freightCharges = freightCharges;
715
	}
716
 
717
	public Long getPoId() {
718
		return poId;
719
	}
720
 
721
	public void setPoId(Long poId) {
722
		this.poId = poId;
723
	}
724
 
725
	public Long getWarehouseId() {
726
		return warehouseId;
727
	}
728
 
729
	public void setWarehouseId(Long warehouseId) {
730
		this.warehouseId = warehouseId;
731
	}
732
 
733
	public Long getTransferWarehouseId() {
734
		return transferWarehouseId;
735
	}
736
 
737
	public void setTransferWarehouseId(Long transferWarehouseId) {
738
		this.transferWarehouseId = transferWarehouseId;
739
	}
740
 
741
	public Long getTransferLotId() {
742
		return transferLotId;
743
	}
744
 
745
	public void setTransferLotId(Long transferLotId) {
746
		this.transferLotId = transferLotId;
747
	}
748
 
749
	public String getBulkScanUploadItem() {
750
		return bulkScanUploadItem;
751
	}
752
 
753
	public void setBulkScanUploadItem(String bulkScanUploadItem) {
754
		this.bulkScanUploadItem = bulkScanUploadItem;
755
	}
756
 
18679 manish.sha 757
	public String getSelectItemType() {
758
		return selectItemType;
759
	}
760
 
761
	public void setSelectItemType(String selectItemType) {
762
		this.selectItemType = selectItemType;
763
	}
764
 
11349 manish.sha 765
	public File getScanDataFile() {
766
		return scanDataFile;
767
	}
768
 
769
	public void setScanDataFile(File scanDataFile) {
770
		this.scanDataFile = scanDataFile;
771
	}
772
 
773
	public String getPurchaseId() {
774
		return purchaseId;
775
	}
776
 
777
	public void setPurchaseId(String purchaseId) {
778
		this.purchaseId = purchaseId;
779
	}
780
 
781
	public String getFileNameVal() {
782
		return fileNameVal;
783
	}
784
 
785
	public void setFileNameVal(String fileNameVal) {
786
		this.fileNameVal = fileNameVal;
787
	}
788
 
789
	public String getSuccessMsg() {
790
		return successMsg;
791
	}
792
 
793
	public void setSuccessMsg(String successMsg) {
794
		this.successMsg = successMsg;
795
	}
796
 
11801 manish.sha 797
	public String getPurchaseComments() {
798
		return purchaseComments;
799
	}
800
 
801
	public void setPurchaseComments(String purchaseComments) {
802
		this.purchaseComments = purchaseComments;
803
	}
804
 
805
	public Warehouse getThirdPartyWarehouse() {
806
		return thirdPartyWarehouse;
807
	}
808
 
809
	public void setThirdPartyWarehouse(Warehouse thirdPartyWarehouse) {
810
		this.thirdPartyWarehouse = thirdPartyWarehouse;
811
	}
11349 manish.sha 812
}