Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
493 rajveer 1
package in.shop2020.hotspot.dashbaord.server;
2
 
19015 manish.sha 3
import in.shop2020.hotspot.dashbaord.server.OrderPromisedShippingComparator;
493 rajveer 4
import in.shop2020.hotspot.dashbaord.shared.actions.Order;
19032 manish.sha 5
import in.shop2020.hotspot.dashbaord.shared.actions.OrderAlert;
493 rajveer 6
import in.shop2020.hotspot.dashbaord.shared.actions.OrderType;
2697 chandransh 7
import in.shop2020.hotspot.dashbaord.shared.actions.ReturnOrder;
19227 manish.sha 8
import in.shop2020.model.v1.inventory.InventoryService;
5945 mandeep.dh 9
import in.shop2020.model.v1.inventory.InventoryType;
18100 manish.sha 10
import in.shop2020.model.v1.inventory.WarehouseType;
5110 mandeep.dh 11
import in.shop2020.model.v1.catalog.Item;
12
import in.shop2020.model.v1.catalog.ItemType;
5945 mandeep.dh 13
import in.shop2020.model.v1.inventory.Warehouse;
914 chandransh 14
import in.shop2020.model.v1.order.LineItem;
7422 rajveer 15
import in.shop2020.model.v1.order.OrderSource;
493 rajveer 16
import in.shop2020.model.v1.order.OrderStatus;
17
import in.shop2020.model.v1.order.TransactionService.Client;
19050 manish.sha 18
import in.shop2020.hotspot.dashbaord.server.EhcacheWrapper;
5110 mandeep.dh 19
import in.shop2020.thrift.clients.CatalogClient;
5948 mandeep.dh 20
import in.shop2020.thrift.clients.InventoryClient;
3132 rajveer 21
import in.shop2020.thrift.clients.TransactionClient;
13146 manish.sha 22
import in.shop2020.thrift.clients.LogisticsClient;
493 rajveer 23
 
4411 rajveer 24
import java.io.ByteArrayOutputStream;
25
import java.io.File;
26
import java.io.FileNotFoundException;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
18938 manish.sha 29
import java.text.DecimalFormat;
30
import java.text.NumberFormat;
19054 manish.sha 31
import java.text.SimpleDateFormat;
493 rajveer 32
import java.util.ArrayList;
4411 rajveer 33
import java.util.Calendar;
19004 manish.sha 34
import java.util.Collections;
493 rajveer 35
import java.util.Date;
4411 rajveer 36
import java.util.GregorianCalendar;
37
import java.util.HashMap;
493 rajveer 38
import java.util.List;
4411 rajveer 39
import java.util.Map;
19004 manish.sha 40
import java.util.Map.Entry;
19110 manish.sha 41
import java.util.Set;
493 rajveer 42
 
19050 manish.sha 43
import net.sf.ehcache.CacheManager;
44
 
4411 rajveer 45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47
 
2449 chandransh 48
/**
49
 * This class is a facade to the Transaction service and should be used to
4133 chandransh 50
 * access all order specific data which requires some kind of transformation.
2449 chandransh 51
 * 
52
 * @author Chandranshu
53
 * 
54
 */
493 rajveer 55
public class TransactionUtils {
4411 rajveer 56
	private static String courierDetailsPath = "/CourierDetailReports";
57
	private static Logger logger = LoggerFactory.getLogger(TransactionUtils.class);
19196 manish.sha 58
	private static long ALL_GREEN = 1L;
59
	private static long GREEN_AND_PURPULE = 2L;
60
	private static long GREEN_AND_IN_RED_OR_WHITE = 3L;
61
	private static long ALL_RED = 4L;
62
	private static long ALL_PURPLE = 5L;
63
	private static long PURPLE_AND_IN_RED_OR_WHITE = 6L;
64
	private static long ALL_OTHERS = 7L;
4411 rajveer 65
 
19196 manish.sha 66
	private static enum ColorCode {
67
		GREEN,
68
		PURPLE,
69
		RED,
70
		OTHER
71
	}
72
 
73
 
2449 chandransh 74
	/**
75
	 * The human user is concerned only with a consolidated view of actionable
76
	 * orders. Orders with different statuses in the database can be part of the
77
	 * same consolidated view. This method uses a mapping of <i>type</i> to
78
	 * <i>status</i> to get all such orders and return them as order beans.
79
	 * 
80
	 * @param type
81
	 *            The type of orders to return.
4133 chandransh 82
	 * @param offset
83
	 *            Offset to start from
84
	 * @param limit
85
	 *            No. of orders to return
2449 chandransh 86
	 * @param warehouseId
87
	 *            The warehouse for which the orders should be queried.
88
	 * @return A list of orders of the given type to be fulfilled from the given
89
	 *         warehouse
90
	 */
8303 amar.kumar 91
	public static List<Order> getOrders(OrderType type, long offset, long limit, long warehouseId, long source){
2449 chandransh 92
		List<OrderStatus> statuses = getStatuses(type);
493 rajveer 93
 
94
		List<Order> orders = new ArrayList<Order>();
95
		try{
4133 chandransh 96
			TransactionClient txnClient = new TransactionClient();
97
			Client client = txnClient.getClient();
493 rajveer 98
 
99
			List<in.shop2020.model.v1.order.Order> t_orders = new ArrayList<in.shop2020.model.v1.order.Order>();
19004 manish.sha 100
			if(type==OrderType.NEW || type==OrderType.ALL_PENDING || type ==OrderType.LOW_INVENTORY || type == OrderType.PO_RAISED
101
					|| type == OrderType.REVERSAL_INITIATED || type == OrderType.NOT_AVAILABLE){
19228 manish.sha 102
				t_orders.addAll(client.getOrdersInBatchAsPromisedShipping(statuses, 0, 0, warehouseId, source));
19004 manish.sha 103
			}else{
104
				t_orders.addAll(client.getOrdersInBatch(statuses, offset, limit, warehouseId, source));
105
			}
493 rajveer 106
 
19224 manish.sha 107
			orders = getOrdersFromThirftOrders(t_orders);
108
			/*for (in.shop2020.model.v1.order.Order t_order: t_orders){
671 chandransh 109
				Order o = getOrderFromThriftOrder(t_order);
493 rajveer 110
				orders.add(o);
19224 manish.sha 111
			}*/
19004 manish.sha 112
			if(type==OrderType.NEW || type==OrderType.ALL_PENDING || type ==OrderType.LOW_INVENTORY || type == OrderType.PO_RAISED
113
					|| type == OrderType.REVERSAL_INITIATED || type == OrderType.NOT_AVAILABLE){
114
				Map<Long, List<Order>> transactionOrdersMap = new HashMap<Long, List<Order>>();
19196 manish.sha 115
				Map<Long, Map<String, Long>> transactionsColorMap = new HashMap<Long, Map<String, Long>>();
19004 manish.sha 116
				for(Order order : orders){
117
					if(transactionOrdersMap.containsKey(order.getTransactionId())){
118
						List<Order> txnOrders = transactionOrdersMap.get(order.getTransactionId());
119
						txnOrders.add(order);
120
						transactionOrdersMap.put(order.getTransactionId(), txnOrders);
121
					}else{
122
						List<Order> txnOrders = new ArrayList<Order>();
123
						txnOrders.add(order);
124
						transactionOrdersMap.put(order.getTransactionId(), txnOrders);
125
					}
19196 manish.sha 126
 
127
					if(transactionsColorMap.containsKey(order.getTransactionId())){
128
						Map<String, Long> colorMap = transactionsColorMap.get(order.getTransactionId());
129
						//SHIPPING_TIME_EXCEEDED, DELIVERY_TIME_EXCEEDED, ORDER_NOT_CONNECTED_FOR_TOO_LONG, ORDER_NOT_CONNECTED
130
						if(order.getAlert()==OrderAlert.TODAY_SHIPPING_IN_STOCK){
131
							if(colorMap.containsKey(ColorCode.GREEN.toString())){
132
								colorMap.put(ColorCode.GREEN.toString(),colorMap.get(ColorCode.GREEN.toString())+1);
133
							}else{
134
								colorMap.put(ColorCode.GREEN.toString(),1L);
135
							}
136
						}else if(order.getAlert()==OrderAlert.TODAY_SHIPPING_NOT_IN_STOCK){
137
							if(colorMap.containsKey(ColorCode.RED.toString())){
138
								colorMap.put(ColorCode.RED.toString(),colorMap.get(ColorCode.RED.toString())+1);
139
							}else{
140
								colorMap.put(ColorCode.RED.toString(),1L);
141
							}
142
						}else if(order.getAlert()==OrderAlert.LATER_SHIPPING_IN_STOCK){
143
							if(colorMap.containsKey(ColorCode.PURPLE.toString())){
144
								colorMap.put(ColorCode.PURPLE.toString(),colorMap.get(ColorCode.PURPLE.toString())+1);
145
							}else{
146
								colorMap.put(ColorCode.PURPLE.toString(),1L);
147
							}
148
						}else{
149
							if(colorMap.containsKey(ColorCode.OTHER.toString())){
150
								colorMap.put(ColorCode.OTHER.toString(),colorMap.get(ColorCode.OTHER.toString())+1);
151
							}else{
152
								colorMap.put(ColorCode.OTHER.toString(),1L);
153
							}
154
						}
155
						transactionsColorMap.put(order.getTransactionId(), colorMap);
156
					}else{
157
						Map<String, Long> colorMap = new HashMap<String, Long>();
158
						if(order.getAlert()==OrderAlert.TODAY_SHIPPING_IN_STOCK){
159
							colorMap.put(ColorCode.GREEN.toString(),1L);
160
						}else if(order.getAlert()==OrderAlert.TODAY_SHIPPING_NOT_IN_STOCK){
161
							colorMap.put(ColorCode.RED.toString(),1L);
162
						}else if(order.getAlert()==OrderAlert.LATER_SHIPPING_IN_STOCK){
163
							colorMap.put(ColorCode.PURPLE.toString(),1L);
164
						}else{
165
							colorMap.put(ColorCode.OTHER.toString(),1L);
166
						}
167
						transactionsColorMap.put(order.getTransactionId(), colorMap);
168
					}
169
 
19004 manish.sha 170
				}
19196 manish.sha 171
 
172
				Map<Long, List<Long>> orderCategoryMap = new HashMap<Long, List<Long>>();
173
 
174
				for(Entry<Long, Map<String, Long>> colorMapEntry : transactionsColorMap.entrySet()){
175
					Map<String, Long> colorMap = colorMapEntry.getValue();
176
					List<String> colorSet = new ArrayList<String>(colorMap.keySet());
177
					long colorMapSize = colorSet.size();
178
					if(colorMapSize==1){
179
						if(ColorCode.GREEN.toString().equalsIgnoreCase(colorSet.get(0))){
180
							if(orderCategoryMap.containsKey(ALL_GREEN)){
181
								List<Long> transactions = orderCategoryMap.get(ALL_GREEN);
182
								transactions.add(colorMapEntry.getKey());
183
								orderCategoryMap.put(ALL_GREEN, transactions);
184
							}else{
185
								List<Long> transactions = new ArrayList<Long>();
186
								transactions.add(colorMapEntry.getKey());
187
								orderCategoryMap.put(ALL_GREEN, transactions);
188
							}
189
						}else if(ColorCode.RED.toString().equalsIgnoreCase(colorSet.get(0))){
190
							if(orderCategoryMap.containsKey(ALL_RED)){
191
								List<Long> transactions = orderCategoryMap.get(ALL_RED);
192
								transactions.add(colorMapEntry.getKey());
193
								orderCategoryMap.put(ALL_RED, transactions);
194
							}else{
195
								List<Long> transactions = new ArrayList<Long>();
196
								transactions.add(colorMapEntry.getKey());
197
								orderCategoryMap.put(ALL_RED, transactions);
198
							}
199
						}else if(ColorCode.PURPLE.toString().equalsIgnoreCase(colorSet.get(0))){
200
							if(orderCategoryMap.containsKey(ALL_PURPLE)){
201
								List<Long> transactions = orderCategoryMap.get(ALL_PURPLE);
202
								transactions.add(colorMapEntry.getKey());
203
								orderCategoryMap.put(ALL_PURPLE, transactions);
204
							}else{
205
								List<Long> transactions = new ArrayList<Long>();
206
								transactions.add(colorMapEntry.getKey());
207
								orderCategoryMap.put(ALL_PURPLE, transactions);
208
							}
209
						}else{
210
							if(orderCategoryMap.containsKey(ALL_OTHERS)){
211
								List<Long> transactions = orderCategoryMap.get(ALL_OTHERS);
212
								transactions.add(colorMapEntry.getKey());
213
								orderCategoryMap.put(ALL_OTHERS, transactions);
214
							}else{
215
								List<Long> transactions = new ArrayList<Long>();
216
								transactions.add(colorMapEntry.getKey());
217
								orderCategoryMap.put(ALL_OTHERS, transactions);
218
							}
219
						}
220
					}else if(colorMapSize>1){
221
						if(colorSet.contains(ColorCode.GREEN.toString())){
222
							if(colorSet.contains(ColorCode.PURPLE.toString())){
223
								if(orderCategoryMap.containsKey(GREEN_AND_PURPULE)){
224
									List<Long> transactions = orderCategoryMap.get(GREEN_AND_PURPULE);
225
									transactions.add(colorMapEntry.getKey());
226
									orderCategoryMap.put(GREEN_AND_PURPULE, transactions);
227
								}else{
228
									List<Long> transactions = new ArrayList<Long>();
229
									transactions.add(colorMapEntry.getKey());
230
									orderCategoryMap.put(GREEN_AND_PURPULE, transactions);
231
								}
232
							}else{
233
								if(orderCategoryMap.containsKey(GREEN_AND_IN_RED_OR_WHITE)){
234
									List<Long> transactions = orderCategoryMap.get(GREEN_AND_IN_RED_OR_WHITE);
235
									transactions.add(colorMapEntry.getKey());
236
									orderCategoryMap.put(GREEN_AND_IN_RED_OR_WHITE, transactions);
237
								}else{
238
									List<Long> transactions = new ArrayList<Long>();
239
									transactions.add(colorMapEntry.getKey());
240
									orderCategoryMap.put(GREEN_AND_IN_RED_OR_WHITE, transactions);
241
								}
242
							}
243
						}else if(colorSet.contains(ColorCode.PURPLE.toString())){
244
							if(orderCategoryMap.containsKey(PURPLE_AND_IN_RED_OR_WHITE)){
245
								List<Long> transactions = orderCategoryMap.get(PURPLE_AND_IN_RED_OR_WHITE);
246
								transactions.add(colorMapEntry.getKey());
247
								orderCategoryMap.put(PURPLE_AND_IN_RED_OR_WHITE, transactions);
248
							}else{
249
								List<Long> transactions = new ArrayList<Long>();
250
								transactions.add(colorMapEntry.getKey());
251
								orderCategoryMap.put(PURPLE_AND_IN_RED_OR_WHITE, transactions);
252
							}
253
						}else{
254
							if(orderCategoryMap.containsKey(ALL_OTHERS)){
255
								List<Long> transactions = orderCategoryMap.get(ALL_OTHERS);
256
								transactions.add(colorMapEntry.getKey());
257
								orderCategoryMap.put(ALL_OTHERS, transactions);
258
							}else{
259
								List<Long> transactions = new ArrayList<Long>();
260
								transactions.add(colorMapEntry.getKey());
261
								orderCategoryMap.put(ALL_OTHERS, transactions);
262
							}
263
						}
264
					}
19004 manish.sha 265
				}
19110 manish.sha 266
 
19196 manish.sha 267
				List<Long> categoryList = new ArrayList<Long>(orderCategoryMap.keySet());
268
				Collections.sort(categoryList);
19110 manish.sha 269
 
19196 manish.sha 270
				orders = new ArrayList<Order>();
271
				for(Long category:categoryList){
272
					List<Long> transactions = orderCategoryMap.get(category);
273
					for(Long transactionId: transactions){
274
						List<Order> txnOrders = transactionOrdersMap.get(transactionId);
275
						Collections.sort(txnOrders,new OrderPromisedShippingComparator());
276
						orders.addAll(txnOrders);
277
					}
19004 manish.sha 278
				}
279
			}
280
 
493 rajveer 281
		}catch(Exception e){
2449 chandransh 282
			e.printStackTrace();
493 rajveer 283
		}
284
		return orders;
285
	}
286
 
2449 chandransh 287
	/**
4133 chandransh 288
	 * Wrapper around the method of same name in the transaction service. This
289
	 * method uses a mapping of <i>type</i> to <i>status</i> to get the count of
290
	 * all orders with a given status.
291
	 * 
292
	 * @param type
293
	 *            The type of orders to return.
294
	 * @param warehouseId
295
	 *            The warehouse for which the orders should be queried.
296
	 * @return The count of orders of the given type assigned to the given
297
	 *         warehouse.
298
	 */
8303 amar.kumar 299
	public static int getOrderCount(OrderType type, long warehouseId, long source){
4133 chandransh 300
		List<OrderStatus> statuses = getStatuses(type);
301
 
302
		int count = 0;
303
		try{
304
			TransactionClient txnClient = new TransactionClient();
305
			Client client = txnClient.getClient();
8303 amar.kumar 306
			count += client.getOrderCount(statuses, warehouseId, source);
4133 chandransh 307
		}catch(Exception e){
308
			e.printStackTrace();
309
		}
310
		return count;
311
	}
312
 
313
	/**
2449 chandransh 314
	 * Calls the same method of the transaction service and returns the status
315
	 * returned. Catches all exceptions that are raised and returns false in
316
	 * that case.
317
	 * 
318
	 * @param warehouseId
319
	 *            The warehouse for which the orders should be marked as
320
	 *            manifested.
321
	 * @param providerId
322
	 *            The provider for which the orders should be marked as
323
	 *            manifested.
3065 chandransh 324
	 * @param cod
325
	 *             Whether cod orders have to be marked as manifested
2449 chandransh 326
	 * @return True if everything goes fine, false otherwise.
327
	 */
5769 rajveer 328
	public static Map<Long, Long> getBilledOrders(long warehouseId, String providerId, boolean cod){
329
		Map<Long, Long> orders = new HashMap<Long, Long>();
760 chandransh 330
		try {
331
			long provider_id = Long.parseLong(providerId);
3132 rajveer 332
			TransactionClient client = new TransactionClient();
760 chandransh 333
			Client c = client.getClient();
4790 rajveer 334
 
335
			List<OrderStatus> statuses = new ArrayList<OrderStatus>();
336
			statuses.add(OrderStatus.BILLED);
8303 amar.kumar 337
			List<in.shop2020.model.v1.order.Order> torders = c.getOrdersInBatch(statuses, 0, 0, warehouseId, 0);
4790 rajveer 338
			for(in.shop2020.model.v1.order.Order torder: torders){
5556 rajveer 339
				if(torder.getLogistics_provider_id() == provider_id && torder.isLogisticsCod() == cod){
5769 rajveer 340
					orders.put(torder.getId(), torder.getPickupStoreId());
4790 rajveer 341
				}
4411 rajveer 342
			}
343
 
760 chandransh 344
		}catch(Exception e){
345
			e.printStackTrace();
346
		}
4790 rajveer 347
		return orders;
760 chandransh 348
	}
2449 chandransh 349
 
4790 rajveer 350
	public static boolean generateCourierDetailsFile(List<Long> orderIds, long providerId, long warehouseId, boolean isCod){
4411 rajveer 351
		Calendar date = new GregorianCalendar();
352
		int year = date.get(Calendar.YEAR);
353
		int month = date.get(Calendar.MONTH) +1;
354
		int day = date.get(Calendar.DAY_OF_MONTH);
355
 
356
		String fileNameSuffix = "-" + warehouseId + "-"+ providerId + "-" + year + "-" + month + "-" + day;
357
		if(isCod){
358
		    fileNameSuffix = "cod" + fileNameSuffix ;
359
		}
360
		else{
361
		    fileNameSuffix = "prepaid" + fileNameSuffix;
362
		}
363
 
364
		try {
4790 rajveer 365
			String fileName = courierDetailsPath + "/courier-details-" + fileNameSuffix + "-temp.xls";
4415 rajveer 366
			FileOutputStream f = new FileOutputStream(fileName);
4411 rajveer 367
			CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
4790 rajveer 368
			ByteArrayOutputStream baosXLS = courierDetailsGenerator.generateCourierDetails(orderIds, warehouseId, providerId, isCod);
4411 rajveer 369
			baosXLS.writeTo(f);
370
			f.close();
371
		} catch (FileNotFoundException e) {
372
			logger.error("Unable to create the courier details file", e);
373
		} catch (IOException e) {
374
			logger.error("Unable to create the courier details file", e);
375
		}
376
 
377
		CourierDetailsReportMerger merger = new CourierDetailsReportMerger();
378
		try {
379
			Map<Long, String> warehouseIdFileNames = new HashMap<Long, String>();
4790 rajveer 380
			String fName;
381
			fName = courierDetailsPath + "/courier-details-" + fileNameSuffix + "-temp.xls";
382
			if((new File(fName)).exists()){
383
				warehouseIdFileNames.put(0L, fName);
4411 rajveer 384
			}
4790 rajveer 385
			fName = courierDetailsPath + "/courier-details-" + fileNameSuffix + ".xls";
386
			if((new File(fName)).exists()){
387
				warehouseIdFileNames.put(1L, fName);
4411 rajveer 388
			}
4790 rajveer 389
			ByteArrayOutputStream binXLS = merger.mergeCourierDetailsReports(warehouseIdFileNames);
390
			FileOutputStream f = new FileOutputStream(courierDetailsPath + "/courier-details-" + fileNameSuffix + ".xls");
4411 rajveer 391
			binXLS.writeTo(f);
392
			f.close();
4790 rajveer 393
			File tempFile = new File(courierDetailsPath + "/courier-details-" + fileNameSuffix + "-temp.xls");
394
			if(tempFile.exists()){
395
				tempFile.delete();
396
			}
4411 rajveer 397
		} catch (FileNotFoundException e) {
398
			logger.error("Error while creating the Courier Details report", e);
399
		} catch (IOException e) {
400
			logger.error("IO error while writing the Courier Details report", e);
401
		}
402
		return true;
403
	}
404
 
19224 manish.sha 405
 
406
	public static List<Order> getOrdersFromThirftOrders(List<in.shop2020.model.v1.order.Order> orders){
407
		List<Order> ordersToReturn = new ArrayList<Order>();
19226 manish.sha 408
		Map<Long, Warehouse> warehousesMap = CatalogUtils.getAllWarehousesForBillingWarehouse(0); 
19224 manish.sha 409
		List<Long> itemIds = new ArrayList<Long>();
410
		Map<Long, List<in.shop2020.model.v1.order.Order>> txnOrdersMap = new HashMap<Long, List<in.shop2020.model.v1.order.Order>>();
411
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
412
		SimpleDateFormat newSdf = new SimpleDateFormat("yyyy-MM-dd");
413
		NumberFormat formatter = new DecimalFormat("#0.000");
414
		NumberFormat rformatter = new DecimalFormat("#0.00");
415
		Date date = new Date(System.currentTimeMillis());
416
		for(in.shop2020.model.v1.order.Order t_order : orders){
417
			if(!itemIds.contains(t_order.getLineitems().get(0).getItem_id())){
418
				itemIds.add(t_order.getLineitems().get(0).getItem_id());
419
			}
420
			List<in.shop2020.model.v1.order.Order> txnOrders = null;
421
			if(txnOrdersMap.containsKey(t_order.getTransactionId())){
422
				txnOrders = txnOrdersMap.get(t_order.getTransactionId());				
423
			}else{
424
				txnOrders = new ArrayList<in.shop2020.model.v1.order.Order>();
425
			}
426
			txnOrders.add(t_order);
427
			txnOrdersMap.put(t_order.getTransactionId(), txnOrders);
428
		}
429
		try {
430
			in.shop2020.model.v1.catalog.CatalogService.Client client = new CatalogClient().getClient();
431
			Map<Long, Item> itemsMap = client.getItems(itemIds);
432
			in.shop2020.logistics.LogisticsService.Client logisticsClient = new LogisticsClient().getClient();
433
			List<in.shop2020.logistics.Provider> providers = logisticsClient.getAllProviders();
434
			Map<Long,in.shop2020.logistics.Provider> providersMap = new HashMap<Long, in.shop2020.logistics.Provider>();
435
 
436
			for(in.shop2020.logistics.Provider provider : providers){
437
				providersMap.put(provider.getId(), provider);
438
			}
439
 
440
			in.shop2020.model.v1.order.TransactionService.Client tClient = new TransactionClient().getClient();
19227 manish.sha 441
			InventoryClient inventoryServiceClient = new InventoryClient();
442
			InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
19224 manish.sha 443
			for(in.shop2020.model.v1.order.Order t_order : orders){
444
				LineItem lineItem = t_order.getLineitems().get(0);
445
				String pickFromWarehouse = warehousesMap.get(t_order.getFulfilmentWarehouseId()).getDisplayName();
446
				Item item = itemsMap.get(lineItem.getItem_id());
447
				in.shop2020.logistics.Provider provider = providersMap.get(t_order.getLogistics_provider_id());
448
				Warehouse fulfillmentWarehouse = warehousesMap.get(t_order.getFulfilmentWarehouseId());
449
				Map<Long, Map<String, String>> acceptTogetherOrdersMap = null;
450
				Map<Long, Map<String, String>> billTogetherOrdersMap = null;
451
				if(!t_order.isSetAccepted_timestamp()){
452
					acceptTogetherOrdersMap = new HashMap<Long, Map<String, String>>();
453
					Map<String, String> orderValuesMap = new HashMap<String, String>();
454
					orderValuesMap.put("ProductName", getItemDisplayName(lineItem));
455
					orderValuesMap.put("Quantity", lineItem.getQuantity()+"");
456
					orderValuesMap.put("Promised_Shipping", sdf.format(new Date(t_order.getPromised_shipping_time())));     
457
					orderValuesMap.put("Weight", formatter.format(lineItem.getUnit_weight()));   
458
					orderValuesMap.put("UnitPrice", rformatter.format(lineItem.getUnit_price()));
459
					orderValuesMap.put("PackQuantity", item.getPackQuantity()+"");
460
					orderValuesMap.put("WarehouseType", fulfillmentWarehouse.getWarehouseType().toString());
461
					acceptTogetherOrdersMap.put(t_order.getId(), orderValuesMap);
462
					List<in.shop2020.model.v1.order.Order> taOrders = null;
463
					if(txnOrdersMap.containsKey(t_order.getTransactionId())){
464
						taOrders = txnOrdersMap.get(t_order.getTransactionId());
465
					}else{
466
						try{
467
							taOrders = tClient.getOrdersForTransaction(t_order.getTransactionId(), t_order.getCustomer_id());
468
						}catch(Exception te){
469
							tClient = new TransactionClient().getClient();
470
							taOrders = tClient.getOrdersForTransaction(t_order.getTransactionId(), t_order.getCustomer_id());
471
						}
472
					}
473
					for(in.shop2020.model.v1.order.Order taOrder : taOrders){
474
						LineItem lineItem1 = taOrder.getLineitems().get(0);
475
						if(t_order.getId()==taOrder.getId()){
476
							continue;
477
						}
478
						else if(taOrder.getSource()!=OrderSource.WEBSITE.getValue()){
479
							continue;
480
						}else{
481
							orderValuesMap = new HashMap<String, String>();
482
							if(provider.isGroupShipmentAllowed() && !taOrder.isSetAccepted_timestamp() && t_order.getStatus()==taOrder.getStatus() 
483
									&& taOrder.getLogistics_provider_id()==t_order.getLogistics_provider_id() 
484
									&& taOrder.isLogisticsCod()==t_order.isLogisticsCod() && taOrder.getWarehouse_id() == t_order.getWarehouse_id() 
485
									&& taOrder.getOrderType() == t_order.getOrderType() && taOrder.getPickupStoreId() == t_order.getPickupStoreId()){
486
								orderValuesMap.put("ProductName", getItemDisplayName(lineItem1));
487
								orderValuesMap.put("Quantity", lineItem1.getQuantity()+"");
19227 manish.sha 488
								if(warehousesMap.containsKey(taOrder.getFulfilmentWarehouseId())){
489
									fulfillmentWarehouse = warehousesMap.get(taOrder.getFulfilmentWarehouseId());
490
								}else{
491
									try{
492
										fulfillmentWarehouse = inventoryClient.getWarehouse(taOrder.getFulfilmentWarehouseId());
493
										warehousesMap.put(fulfillmentWarehouse.getId(), fulfillmentWarehouse);
494
									}
495
									catch(Exception ie){
496
										ie.printStackTrace();
497
										inventoryClient = inventoryServiceClient.getClient();
498
										fulfillmentWarehouse = inventoryClient.getWarehouse(taOrder.getFulfilmentWarehouseId());
499
										warehousesMap.put(fulfillmentWarehouse.getId(), fulfillmentWarehouse);
500
									}
501
								}
19224 manish.sha 502
								orderValuesMap.put("WarehouseType", fulfillmentWarehouse.getWarehouseType().toString());
503
								orderValuesMap.put("Promised_Shipping", sdf.format(new Date(taOrder.getPromised_shipping_time())));
504
								orderValuesMap.put("UnitPrice", rformatter.format(lineItem1.getUnit_price()));
505
								orderValuesMap.put("Weight", formatter.format(lineItem1.getUnit_weight()));
506
								if(itemsMap.containsKey(lineItem1.getItem_id())){
507
									Item orderItem = itemsMap.get(lineItem1.getItem_id());
508
									orderValuesMap.put("PackQuantity", orderItem.getPackQuantity()+"");
509
								}else{
510
									try{
511
										Item orderItem = client.getItem(lineItem1.getItem_id());
512
										orderValuesMap.put("PackQuantity", orderItem.getPackQuantity()+"");
513
										itemsMap.put(orderItem.getId(), orderItem);
514
									}catch(Exception ce){
515
										ce.printStackTrace();
516
										client = new CatalogClient().getClient();
517
										Item orderItem = client.getItem(lineItem1.getItem_id());
518
										orderValuesMap.put("PackQuantity", orderItem.getPackQuantity()+"");
519
										itemsMap.put(orderItem.getId(), orderItem);
520
									}
521
								}
522
							}
523
							if(orderValuesMap!=null && orderValuesMap.size()>0){
524
	            				acceptTogetherOrdersMap.put(taOrder.getId(), orderValuesMap);
525
	            			}
526
						}
527
					}
528
				}
529
 
530
				if(t_order.isSetLogisticsTransactionId()){
531
					billTogetherOrdersMap = new HashMap<Long, Map<String, String>>();
532
	            	Map<String, String> orderValuesMap = new HashMap<String, String>();
533
		            if(t_order.getStatus()==OrderStatus.ACCEPTED || t_order.getStatus()==OrderStatus.RTO_IN_TRANSIT ){
534
		            	orderValuesMap.put("ProductName", getItemDisplayName(lineItem));
535
		            	orderValuesMap.put("Quantity", lineItem.getQuantity()+"");
536
		            	if(ItemType.SERIALIZED==item.getType()){
537
		            		orderValuesMap.put("IsSerialized", "true");
538
		            	}else{
539
		            		orderValuesMap.put("IsSerialized", "false");
540
		            	}
541
		            	if(t_order.isSetFreebieItemId() && t_order.getFreebieItemId()!=0){
542
		            		orderValuesMap.put("IsFreebie", "true");
543
		            	} else {
544
		            		orderValuesMap.put("IsFreebie", "false");
545
		            	}
546
		            	billTogetherOrdersMap.put(t_order.getId(), orderValuesMap);
547
	            	}
548
 
549
		            List<in.shop2020.model.v1.order.Order> taOrders = null;
550
		            try{
551
		            	taOrders = tClient.getGroupOrdersByLogisticsTxnId(t_order.getLogisticsTransactionId());
552
		            }catch(Exception te){
553
		            	tClient = new TransactionClient().getClient();
554
		            	taOrders = tClient.getGroupOrdersByLogisticsTxnId(t_order.getLogisticsTransactionId());
555
		            }
556
 
557
		            for(in.shop2020.model.v1.order.Order taOrder: taOrders){
558
	            		if(taOrder.getStatus()==OrderStatus.ACCEPTED || taOrder.getStatus()==OrderStatus.RTO_IN_TRANSIT ){
559
		            		if(taOrder.getId()==t_order.getId()){
560
		            			continue;
561
		            		} else {
562
		            			orderValuesMap = new HashMap<String, String>();
563
		            			Item orderItem = null;
564
		            			if(itemsMap.containsKey(taOrder.getLineitems().get(0).getItem_id())){
565
		            				orderItem = itemsMap.get(taOrder.getId());
566
		            			}else{
567
									try{
568
										orderItem = client.getItem(taOrder.getLineitems().get(0).getItem_id());
569
										itemsMap.put(orderItem.getId(), orderItem);
570
									}catch(Exception ce){
571
										ce.printStackTrace();
572
										client = new CatalogClient().getClient();
573
										orderItem = client.getItem(taOrder.getLineitems().get(0).getItem_id());
574
										itemsMap.put(orderItem.getId(), orderItem);
575
									}
576
								}
577
 
578
		            			orderValuesMap.put("ProductName", getProductName(orderItem));
579
		                    	orderValuesMap.put("Quantity", taOrder.getLineitems().get(0).getQuantity()+"");
580
		                    	if(ItemType.SERIALIZED==orderItem.getType()){
581
		                    		orderValuesMap.put("IsSerialized", "true");
582
		                    	}else{
583
		                    		orderValuesMap.put("IsSerialized", "false");
584
		                    	}
585
		                    	if(taOrder.isSetFreebieItemId() && taOrder.getFreebieItemId()!=0){
586
		                    		orderValuesMap.put("IsFreebie", "true");
587
		                    	} else {
588
		                    		orderValuesMap.put("IsFreebie", "false");
589
		                    	}
590
		                    	billTogetherOrdersMap.put(taOrder.getId(), orderValuesMap);
591
		            		}
592
	            		}
593
	            	}
594
 
595
				}else{
596
	            	billTogetherOrdersMap = new HashMap<Long, Map<String, String>>();
597
	            	Map<String, String> orderValuesMap = new HashMap<String, String>();
598
	            	orderValuesMap.put("ProductName", getItemDisplayName(lineItem));
599
	            	orderValuesMap.put("Quantity", lineItem.getQuantity()+"");
600
	            	if(ItemType.SERIALIZED==item.getType()){
601
	            		orderValuesMap.put("IsSerialized", "true");
602
	            	}else{
603
	            		orderValuesMap.put("IsSerialized", "false");
604
	            	}
605
	            	if(t_order.isSetFreebieItemId() && t_order.getFreebieItemId()!=0){
606
	            		orderValuesMap.put("IsFreebie", "true");
607
	            	} else {
608
	            		orderValuesMap.put("IsFreebie", "false");
609
	            	}
610
	            	billTogetherOrdersMap.put(t_order.getId(), orderValuesMap);
611
	            }
612
				String delayReason = null;
613
				if(t_order.getDelayReason() != null)
614
				    delayReason = t_order.getDelayReason().name();
615
 
616
				String osource = OrderSource.findByValue((int)t_order.getSource()).toString();
617
 
618
				Order order = new Order(t_order.getId(),
619
						t_order.getCustomer_id(),
620
						t_order.getCustomer_name(),
621
						t_order.getCustomer_mobilenumber(),
622
						t_order.getCustomer_pincode(),
623
						t_order.getCustomer_address1(),
624
						t_order.getCustomer_address2(),
625
						t_order.getCustomer_city(),
626
						t_order.getCustomer_state(),
627
						t_order.getCustomer_email(),
628
						t_order.getCreated_timestamp(),
629
						t_order.getShipping_timestamp(),
630
						t_order.getVerification_timestamp(),
631
						t_order.getExpected_delivery_time(),
632
						t_order.getPromised_delivery_time(),
633
						t_order.getExpected_shipping_time(),
634
						t_order.getPromised_shipping_time(),
635
						t_order.getStatus().getValue(),
636
						t_order.getStatusDescription(),
637
						t_order.getOrderType().name(),
638
						lineItem.getItem_id(),
639
						lineItem.getProductGroup(),
640
						lineItem.getBrand(),
641
						lineItem.getModel_name(),
642
						lineItem.getModel_number(),
643
						lineItem.getColor(),
644
						lineItem.getExtra_info(),
645
						lineItem.getDealText(),
646
						lineItem.getQuantity(),
647
						t_order.getTotal_amount(),
648
						t_order.getTotal_weight(),
649
						t_order.getAirwaybill_no(),
650
						t_order.getBilled_by(),
651
						t_order.getInvoice_number(),
652
						t_order.getJacket_number(),
653
						lineItem.getItem_number(),
654
						lineItem.getSerial_number(),
655
						t_order.getBatchNo(),
656
						t_order.getSerialNo(),
657
						t_order.isDoaFlag(),
658
						t_order.getPickupRequestNo(),
659
						t_order.isLogisticsCod(),
660
						delayReason,
661
						pickFromWarehouse,
662
						ItemType.SERIALIZED.equals(item.getType()),
663
						item.isHasItemNo(),
664
						t_order.getFulfilmentWarehouseId(),
665
						t_order.getWarehouse_id(),
666
						t_order.getPickupStoreId(),
667
						t_order.getFreebieItemId(),
668
						osource, 
669
						new Long(t_order.getProductCondition().getValue()),
670
						t_order.getTransactionId());
671
				if(acceptTogetherOrdersMap!=null && acceptTogetherOrdersMap.size()>0){
672
					order.setAcceptTogetherOrdersMap(acceptTogetherOrdersMap);
673
				}
674
				if(billTogetherOrdersMap!=null && billTogetherOrdersMap.size()>0){
675
					order.setBillTogetherOrdersMap(billTogetherOrdersMap);
676
				}
677
				if(t_order.isSetLogisticsTransactionId()){
678
					order.setLogisticsTransactionId(t_order.getLogisticsTransactionId());
679
				}
680
 
681
 
682
				long currentDateTime = 0;
683
				long promisedShippingTime =0 ;
684
				long currentTime = date.getTime();
685
				try{
686
					currentDateTime = newSdf.parse(newSdf.format(date)).getTime();
687
					promisedShippingTime = newSdf.parse(newSdf.format(new Date(order.getPromisedShippingTime()))).getTime();
688
				}catch(Exception e){
689
					e.printStackTrace();
690
				}
691
				if(promisedShippingTime >0 && (order.getStatus()==3 || order.getStatus()==5 || order.getStatus()==41 ||
692
						order.getStatus()==35 || order.getStatus()==36 || order.getStatus()==37)){
693
					if(promisedShippingTime <= currentDateTime){
694
						if(loadVirtualWarehouseDetails().contains(order.getFulfilmentWarehouseId())){
695
							order.setAlert(OrderAlert.TODAY_SHIPPING_NOT_IN_STOCK);
696
						}else{
697
							order.setAlert(OrderAlert.TODAY_SHIPPING_IN_STOCK);
698
						}
699
					}else{
700
						if(!loadVirtualWarehouseDetails().contains(order.getFulfilmentWarehouseId())){
701
							order.setAlert(OrderAlert.LATER_SHIPPING_IN_STOCK);
702
						}
703
					}
704
				}
705
 
706
				ordersToReturn.add(order);
707
			}
708
		}
709
		catch (Exception e) {
710
			e.printStackTrace();
711
		}
712
 
713
		return ordersToReturn;
714
 
715
	}
716
 
717
 
671 chandransh 718
	/**
2449 chandransh 719
	 * 
671 chandransh 720
	 * @param t_order
2449 chandransh 721
	 *            A thrift order object with line items.
722
	 * @return an Order bean which can be used for rendering on the client side.
671 chandransh 723
	 */
724
	public static Order getOrderFromThriftOrder(in.shop2020.model.v1.order.Order t_order) {
914 chandransh 725
		LineItem lineItem = t_order.getLineitems().get(0);
5110 mandeep.dh 726
		String pickFromWarehouse = null;
727
		Item item = null;
19004 manish.sha 728
		String warehouseType = "";
13146 manish.sha 729
 
730
	    Map<Long, Map<String, String>> acceptTogetherOrdersMap = null;
731
	    Map<Long, Map<String, String>> billTogetherOrdersMap = null;
5110 mandeep.dh 732
 
733
        try {
5945 mandeep.dh 734
            in.shop2020.model.v1.catalog.CatalogService.Client client = new CatalogClient().getClient();
5948 mandeep.dh 735
            in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient().getClient();
5110 mandeep.dh 736
            item = client.getItem(lineItem.getItem_id());
737
            pickFromWarehouse = CatalogUtils.getWarehousesForBillingWarehouse(t_order.getWarehouse_id())
738
                                    .get(t_order.getFulfilmentWarehouseId());
739
 
740
            if (pickFromWarehouse == null) {
5948 mandeep.dh 741
                Warehouse warehouse = inventoryClient.getWarehouse(t_order.getFulfilmentWarehouseId());
742
                pickFromWarehouse = inventoryClient.getWarehouses(null, InventoryType.GOOD, warehouse.getVendor().getId(), t_order.getWarehouse_id(), 0).get(0).getDisplayName();
13146 manish.sha 743
            }											
744
            if(!t_order.isSetAccepted_timestamp()){
745
            	acceptTogetherOrdersMap = new HashMap<Long, Map<String, String>>();
746
            	Map<String, String> orderValuesMap = new HashMap<String, String>();
747
            	orderValuesMap.put("ProductName", getItemDisplayName(lineItem));
748
				orderValuesMap.put("Quantity", lineItem.getQuantity()+"");
18100 manish.sha 749
				if(!inventoryClient.isAlive()){
750
					inventoryClient = new InventoryClient().getClient();
751
				}
752
				Warehouse fulfillmentWarehouse = inventoryClient.getWarehouse(t_order.getFulfilmentWarehouseId());
19004 manish.sha 753
				warehouseType = fulfillmentWarehouse.getWarehouseType().toString();
18100 manish.sha 754
				orderValuesMap.put("WarehouseType", fulfillmentWarehouse.getWarehouseType().toString());
19054 manish.sha 755
				SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
18938 manish.sha 756
				orderValuesMap.put("Promised_Shipping", sdf.format(new Date(t_order.getPromised_shipping_time())));
18964 manish.sha 757
				NumberFormat formatter = new DecimalFormat("#0.000");     
758
				orderValuesMap.put("Weight", formatter.format(t_order.getLineitems().get(0).getUnit_weight()));
759
				formatter = new DecimalFormat("#0.00");   
18938 manish.sha 760
				orderValuesMap.put("UnitPrice", formatter.format(t_order.getLineitems().get(0).getUnit_price()));
18964 manish.sha 761
				orderValuesMap.put("PackQuantity", item.getPackQuantity()+"");
13146 manish.sha 762
				acceptTogetherOrdersMap.put(t_order.getId(), orderValuesMap);
763
            	List<in.shop2020.model.v1.order.Order> taOrders = new TransactionClient().getClient().getOrdersForTransaction(t_order.getTransactionId(), t_order.getCustomer_id());
764
            	in.shop2020.logistics.Provider provider = new LogisticsClient().getClient().getProvider(t_order.getLogistics_provider_id());
765
            	for(in.shop2020.model.v1.order.Order taOrder : taOrders){
766
            		LineItem lineItem1 = taOrder.getLineitems().get(0);
767
            		if(t_order.getId()==taOrder.getId()){
768
            			continue;
13156 manish.sha 769
            		}
770
            		else if(taOrder.getSource()!=OrderSource.WEBSITE.getValue()){
771
            			continue;
772
            		}
773
            		else{
13146 manish.sha 774
            			orderValuesMap = new HashMap<String, String>();
13323 manish.sha 775
            			if(provider.isGroupShipmentAllowed() && !taOrder.isSetAccepted_timestamp() && t_order.getStatus()==taOrder.getStatus() 
776
            					&& taOrder.getLogistics_provider_id()==t_order.getLogistics_provider_id() 
777
            					&& taOrder.isLogisticsCod()==t_order.isLogisticsCod() && taOrder.getWarehouse_id() == t_order.getWarehouse_id() 
778
            					&& taOrder.getOrderType() == t_order.getOrderType() && taOrder.getPickupStoreId() == t_order.getPickupStoreId()){
13146 manish.sha 779
            				orderValuesMap.put("ProductName", getItemDisplayName(lineItem1));
780
            				orderValuesMap.put("Quantity", lineItem1.getQuantity()+"");
18100 manish.sha 781
            				if(!inventoryClient.isAlive()){
782
            					inventoryClient = new InventoryClient().getClient();
783
            				}
784
            				fulfillmentWarehouse = inventoryClient.getWarehouse(taOrder.getFulfilmentWarehouseId());
785
            				orderValuesMap.put("WarehouseType", fulfillmentWarehouse.getWarehouseType().toString());
18938 manish.sha 786
            				orderValuesMap.put("Promised_Shipping", sdf.format(new Date(taOrder.getPromised_shipping_time())));
787
            				orderValuesMap.put("UnitPrice", formatter.format(taOrder.getLineitems().get(0).getUnit_price()));
18964 manish.sha 788
            				formatter = new DecimalFormat("#0.000");   
789
            				orderValuesMap.put("Weight", formatter.format(taOrder.getLineitems().get(0).getUnit_weight()));
790
            				Item orderItem = client.getItem(taOrder.getLineitems().get(0).getItem_id());
791
            				orderValuesMap.put("PackQuantity", orderItem.getPackQuantity()+"");
13146 manish.sha 792
            			}
793
            			if(orderValuesMap!=null && orderValuesMap.size()>0){
794
            				acceptTogetherOrdersMap.put(taOrder.getId(), orderValuesMap);
795
            			}
796
            		}
797
            	}
5110 mandeep.dh 798
            }
13146 manish.sha 799
 
800
            if(t_order.isSetLogisticsTransactionId()){
801
            	billTogetherOrdersMap = new HashMap<Long, Map<String, String>>();
802
            	Map<String, String> orderValuesMap = new HashMap<String, String>();
19091 manish.sha 803
	            if(t_order.getStatus()==OrderStatus.ACCEPTED || t_order.getStatus()==OrderStatus.RTO_IN_TRANSIT ){
18100 manish.sha 804
	            	orderValuesMap.put("ProductName", getItemDisplayName(lineItem));
805
	            	orderValuesMap.put("Quantity", lineItem.getQuantity()+"");
806
	            	if(ItemType.SERIALIZED==item.getType()){
807
	            		orderValuesMap.put("IsSerialized", "true");
808
	            	}else{
809
	            		orderValuesMap.put("IsSerialized", "false");
810
	            	}
811
	            	if(t_order.isSetFreebieItemId() && t_order.getFreebieItemId()!=0){
812
	            		orderValuesMap.put("IsFreebie", "true");
813
	            	} else {
814
	            		orderValuesMap.put("IsFreebie", "false");
815
	            	}
816
	            	billTogetherOrdersMap.put(t_order.getId(), orderValuesMap);
13146 manish.sha 817
            	}
818
            	Map<Long, Item> orderItemMap = new HashMap<Long, Item>();
819
            	List<in.shop2020.model.v1.order.Order> taOrders = new TransactionClient().getClient().getGroupOrdersByLogisticsTxnId(t_order.getLogisticsTransactionId());
820
            	for(in.shop2020.model.v1.order.Order ord1: taOrders){
19091 manish.sha 821
            		if(ord1.getStatus()==OrderStatus.ACCEPTED || ord1.getStatus()==OrderStatus.RTO_IN_TRANSIT ){
18100 manish.sha 822
	            		if(ord1.getId()== t_order.getId()){
823
	            			orderItemMap.put(ord1.getId(), item);
824
	            		} else {
825
		            		if(!client.isAlive()){
826
		            			client = new CatalogClient().getClient();
827
		            		}
828
		            		Item it = client.getItem(ord1.getLineitems().get(0).getItem_id());
829
		            		orderItemMap.put(ord1.getId(), it);
13146 manish.sha 830
	            		}
831
            		}
832
            	}
833
 
834
            	for(in.shop2020.model.v1.order.Order taOrder: taOrders){
19091 manish.sha 835
            		if(taOrder.getStatus()==OrderStatus.ACCEPTED || taOrder.getStatus()==OrderStatus.RTO_IN_TRANSIT ){
18100 manish.sha 836
	            		if(taOrder.getId()==t_order.getId()){
837
	            			continue;
838
	            		} else {
839
	            			orderValuesMap = new HashMap<String, String>();
840
	            			Item orderItem = orderItemMap.get(taOrder.getId());
841
	            			orderValuesMap.put("ProductName", getProductName(orderItem));
842
	                    	orderValuesMap.put("Quantity", taOrder.getLineitems().get(0).getQuantity()+"");
843
	                    	if(ItemType.SERIALIZED==orderItem.getType()){
844
	                    		orderValuesMap.put("IsSerialized", "true");
845
	                    	}else{
846
	                    		orderValuesMap.put("IsSerialized", "false");
847
	                    	}
848
	                    	if(taOrder.isSetFreebieItemId() && taOrder.getFreebieItemId()!=0){
849
	                    		orderValuesMap.put("IsFreebie", "true");
850
	                    	} else {
851
	                    		orderValuesMap.put("IsFreebie", "false");
852
	                    	}
853
	                    	billTogetherOrdersMap.put(taOrder.getId(), orderValuesMap);
854
	            		}
13146 manish.sha 855
            		}
856
            	}
857
            }
858
            else{
859
            	billTogetherOrdersMap = new HashMap<Long, Map<String, String>>();
860
            	Map<String, String> orderValuesMap = new HashMap<String, String>();
861
            	orderValuesMap.put("ProductName", getItemDisplayName(lineItem));
862
            	orderValuesMap.put("Quantity", lineItem.getQuantity()+"");
863
            	if(ItemType.SERIALIZED==item.getType()){
864
            		orderValuesMap.put("IsSerialized", "true");
865
            	}else{
866
            		orderValuesMap.put("IsSerialized", "false");
867
            	}
868
            	if(t_order.isSetFreebieItemId() && t_order.getFreebieItemId()!=0){
869
            		orderValuesMap.put("IsFreebie", "true");
870
            	} else {
871
            		orderValuesMap.put("IsFreebie", "false");
872
            	}
873
            	billTogetherOrdersMap.put(t_order.getId(), orderValuesMap);
874
            }
5110 mandeep.dh 875
		}
876
        catch (Exception e) {
877
            logger.error("Error looking up warehouse: " + t_order.getVendorId(), e);
878
        }
879
 
3553 chandransh 880
		String delayReason = null;
881
		if(t_order.getDelayReason() != null)
7433 rajveer 882
		    delayReason = t_order.getDelayReason().name();
3553 chandransh 883
 
7556 rajveer 884
		String osource = OrderSource.findByValue((int)t_order.getSource()).toString();
7433 rajveer 885
 
966 chandransh 886
		Order order = new Order(t_order.getId(),
887
				t_order.getCustomer_id(),
888
				t_order.getCustomer_name(),
889
				t_order.getCustomer_mobilenumber(),
890
				t_order.getCustomer_pincode(),
891
				t_order.getCustomer_address1(),
892
				t_order.getCustomer_address2(),
893
				t_order.getCustomer_city(),
894
				t_order.getCustomer_state(),
895
				t_order.getCustomer_email(),
896
				t_order.getCreated_timestamp(),
4004 chandransh 897
				t_order.getShipping_timestamp(),
898
				t_order.getVerification_timestamp(),
966 chandransh 899
				t_order.getExpected_delivery_time(),
3994 chandransh 900
				t_order.getPromised_delivery_time(),
4004 chandransh 901
				t_order.getExpected_shipping_time(),
4666 rajveer 902
				t_order.getPromised_shipping_time(),
966 chandransh 903
				t_order.getStatus().getValue(),
904
				t_order.getStatusDescription(),
5527 anupam.sin 905
				t_order.getOrderType().name(),
966 chandransh 906
				lineItem.getItem_id(),
907
				lineItem.getProductGroup(),
908
				lineItem.getBrand(),
909
				lineItem.getModel_name(),
910
				lineItem.getModel_number(),
911
				lineItem.getColor(),
912
				lineItem.getExtra_info(),
4172 rajveer 913
				lineItem.getDealText(),
5387 rajveer 914
				lineItem.getQuantity(),
2782 chandransh 915
				t_order.getTotal_amount(),
916
				t_order.getTotal_weight(),
917
				t_order.getAirwaybill_no(),
918
				t_order.getBilled_by(),
919
				t_order.getInvoice_number(),
920
				t_order.getJacket_number(),
921
				lineItem.getItem_number(),
4659 mandeep.dh 922
				lineItem.getSerial_number(),
3065 chandransh 923
				t_order.getBatchNo(),
924
				t_order.getSerialNo(),
2509 chandransh 925
				t_order.isDoaFlag(),
3065 chandransh 926
				t_order.getPickupRequestNo(),
5556 rajveer 927
				t_order.isLogisticsCod(),
5110 mandeep.dh 928
				delayReason,
929
				pickFromWarehouse,
6028 rajveer 930
				ItemType.SERIALIZED.equals(item.getType()),
931
				item.isHasItemNo(),
5769 rajveer 932
				t_order.getFulfilmentWarehouseId(),
9263 amar.kumar 933
				t_order.getWarehouse_id(),
7190 amar.kumar 934
				t_order.getPickupStoreId(),
7422 rajveer 935
				t_order.getFreebieItemId(),
8717 amar.kumar 936
				osource, 
13146 manish.sha 937
				new Long(t_order.getProductCondition().getValue()),
938
				t_order.getTransactionId());
939
		if(acceptTogetherOrdersMap!=null && acceptTogetherOrdersMap.size()>0){
940
			order.setAcceptTogetherOrdersMap(acceptTogetherOrdersMap);
941
		}
942
		if(billTogetherOrdersMap!=null && billTogetherOrdersMap.size()>0){
943
			order.setBillTogetherOrdersMap(billTogetherOrdersMap);
944
		}
945
		if(t_order.isSetLogisticsTransactionId()){
946
			order.setLogisticsTransactionId(t_order.getLogisticsTransactionId());
947
		}
19054 manish.sha 948
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
19032 manish.sha 949
		Date date = new Date(System.currentTimeMillis());
950
		long currentDateTime = 0;
19196 manish.sha 951
		long promisedShippingTime =0 ;
19032 manish.sha 952
		long currentTime = date.getTime();
19054 manish.sha 953
		try{
954
			currentDateTime = sdf.parse(sdf.format(date)).getTime();
19196 manish.sha 955
			promisedShippingTime = sdf.parse(sdf.format(new Date(order.getPromisedShippingTime()))).getTime();
19054 manish.sha 956
		}catch(Exception e){
957
			e.printStackTrace();
958
		}
19196 manish.sha 959
		if(promisedShippingTime >0 && (order.getStatus()==3 || order.getStatus()==5 || order.getStatus()==41 ||
19058 manish.sha 960
				order.getStatus()==35 || order.getStatus()==36 || order.getStatus()==37)){
19196 manish.sha 961
			if(promisedShippingTime <= currentDateTime){
19050 manish.sha 962
				if(loadVirtualWarehouseDetails().contains(order.getFulfilmentWarehouseId())){
19032 manish.sha 963
					order.setAlert(OrderAlert.TODAY_SHIPPING_NOT_IN_STOCK);
964
				}else{
965
					order.setAlert(OrderAlert.TODAY_SHIPPING_IN_STOCK);
966
				}
967
			}else{
19162 manish.sha 968
				if(!loadVirtualWarehouseDetails().contains(order.getFulfilmentWarehouseId())){
19164 manish.sha 969
					order.setAlert(OrderAlert.LATER_SHIPPING_IN_STOCK);
19032 manish.sha 970
				}
971
			}
972
		}
966 chandransh 973
		return order;
671 chandransh 974
	}
975
 
2449 chandransh 976
	/**
2697 chandransh 977
	 * Queries the transction server to get the list of all return orders that
978
	 * have to be processed.
979
	 * 
980
	 * @return A list of all return orders to be processed.
981
	 */
982
	public static List<ReturnOrder> getReturnOrders(long warehouseId){
983
		List<ReturnOrder> retOrders = new ArrayList<ReturnOrder>();
984
		try{
3132 rajveer 985
			TransactionClient client = new TransactionClient();
2697 chandransh 986
			Client c = client.getClient();
987
			List<in.shop2020.model.v1.order.ReturnOrder> tRetOrders =  c.getReturnOrders(warehouseId, 0L, new Date().getTime());
988
			for(in.shop2020.model.v1.order.ReturnOrder retOrder : tRetOrders){
989
				retOrders.add(getReturnOrderFromThriftRO(retOrder));
990
			}
991
		}catch(Exception e){
992
			e.printStackTrace();
993
		}
994
		return retOrders;
995
	}
996
 
2700 chandransh 997
	public static ReturnOrder getReturnOrderFromThriftRO(in.shop2020.model.v1.order.ReturnOrder tRetOrder){
998
		ReturnOrder retOrder = new ReturnOrder(tRetOrder.getOrderId(),
999
				tRetOrder.getWarehouseId(),
1000
				tRetOrder.getItemId(),
1001
				tRetOrder.getProductGroup(),
1002
				tRetOrder.getBrand(),
1003
				tRetOrder.getModelName(),
1004
				tRetOrder.getModelNumber(),
1005
				tRetOrder.getColor(),
1006
				tRetOrder.getInvoiceNumber(),
1007
				tRetOrder.getJacketNumber(),
1008
				tRetOrder.getTotalPrice(),
1009
				tRetOrder.getTransferPrice(),
1010
				false,
1011
				tRetOrder.getCreatedAt());
2697 chandransh 1012
		return retOrder;
1013
	}
1014
 
1015
	/**
2449 chandransh 1016
	 * This method maps a given type to a list of statuses.
1017
	 * 
1018
	 * @param type
1019
	 *            The type of orders to fetch.
1020
	 * @return The list of Thrift statuses associated with a particular order
1021
	 *         type.
1022
	 */
4361 rajveer 1023
	public static List<OrderStatus> getStatuses(OrderType type) {
2449 chandransh 1024
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
1025
 
1026
		switch (type) {
1027
		case ACCEPTED:
1028
			statuses.add(OrderStatus.ACCEPTED);
1029
			break;
1030
 
4361 rajveer 1031
		case ALL_PENDING:
1032
			statuses.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
4421 mandeep.dh 1033
			statuses.add(OrderStatus.CAPTURE_IN_PROCESS);
4361 rajveer 1034
			statuses.add(OrderStatus.INVENTORY_LOW);
1035
			statuses.add(OrderStatus.LOW_INV_PO_RAISED);
1036
			statuses.add(OrderStatus.LOW_INV_REVERSAL_IN_PROCESS);
1037
			statuses.add(OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT);
2449 chandransh 1038
			break;
1039
 
1040
		case NEW:
1041
			statuses.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
4421 mandeep.dh 1042
			statuses.add(OrderStatus.CAPTURE_IN_PROCESS);
2449 chandransh 1043
			break;
1044
 
1045
		case BILLED:
1046
			statuses.add(OrderStatus.BILLED);
1047
			break;
1048
 
4308 rajveer 1049
		case LOW_INVENTORY:
2449 chandransh 1050
			statuses.add(OrderStatus.INVENTORY_LOW);
1051
			break;
4248 rajveer 1052
 
4308 rajveer 1053
		case PO_RAISED:
1054
			statuses.add(OrderStatus.LOW_INV_PO_RAISED);
1055
			break;
1056
 
1057
		case REVERSAL_INITIATED:
1058
			statuses.add(OrderStatus.LOW_INV_REVERSAL_IN_PROCESS);
1059
			break;
1060
 
1061
		case NOT_AVAILABLE:
1062
			statuses.add(OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT);
1063
			break;
1064
 
4248 rajveer 1065
		case CANCEL_CONFIRMED:
1066
			statuses.add(OrderStatus.CANCEL_REQUEST_CONFIRMED);
1067
			break;
2449 chandransh 1068
 
4459 rajveer 1069
		case DOA_REQUEST_AUTHORIZED:
1070
			statuses.add(OrderStatus.DOA_REQUEST_AUTHORIZED);
4515 mandeep.dh 1071
			statuses.add(OrderStatus.DOA_PICKUP_REQUEST_RAISED);
4459 rajveer 1072
			break;
1073
 
4666 rajveer 1074
		case LOST_IN_TRANSIT:
1075
			statuses.add(OrderStatus.LOST_IN_TRANSIT);
1076
			break;
1077
 
4459 rajveer 1078
		case DOA_LOST_IN_TRANSIT:
1079
			statuses.add(OrderStatus.DOA_LOST_IN_TRANSIT);
4478 rajveer 1080
			break;
1081
 
1082
		case DOA_RECEIVED_DAMAGED:
4459 rajveer 1083
			statuses.add(OrderStatus.DOA_RECEIVED_DAMAGED);
1084
			break;
4495 rajveer 1085
 
1086
		case RET_REQUEST_AUTHORIZED:
1087
			statuses.add(OrderStatus.RET_REQUEST_AUTHORIZED);
4515 mandeep.dh 1088
			statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
4495 rajveer 1089
			break;
1090
 
1091
		case RET_AWAITED:
1092
			statuses.add(OrderStatus.RET_RETURN_IN_TRANSIT);
4515 mandeep.dh 1093
			statuses.add(OrderStatus.RET_PICKUP_CONFIRMED);
4495 rajveer 1094
			break;
4459 rajveer 1095
 
4495 rajveer 1096
		case RET_RECEIVED_PRESTINE:
1097
			statuses.add(OrderStatus.RET_RECEIVED_PRESTINE);
4515 mandeep.dh 1098
			statuses.add(OrderStatus.RET_PRODUCT_USABLE);
4495 rajveer 1099
			break;
1100
 
1101
		case RET_LOST_IN_TRANSIT:
1102
			statuses.add(OrderStatus.RET_LOST_IN_TRANSIT);
1103
			break;
1104
 
1105
		case RET_RECEIVED_DAMAGED:
1106
			statuses.add(OrderStatus.RET_RECEIVED_DAMAGED);
4515 mandeep.dh 1107
			statuses.add(OrderStatus.RET_PRODUCT_UNUSABLE);
4495 rajveer 1108
			break;
1109
 
2449 chandransh 1110
		case REJECTED:
1111
			statuses.add(OrderStatus.REJECTED);
1112
			break;
1113
 
1114
		case SHIPPED:
1115
			statuses.add(OrderStatus.SHIPPED_FROM_WH);
1116
			statuses.add(OrderStatus.SHIPPED_TO_LOGST);
5016 phani.kuma 1117
			statuses.add(OrderStatus.SHIPPED_TO_DESTINATION_CITY);
1118
			statuses.add(OrderStatus.REACHED_DESTINATION_CITY);
1119
			statuses.add(OrderStatus.FIRST_DELIVERY_ATTEMPT_MADE);
2449 chandransh 1120
			break;
1121
 
1122
		case DELIVERED:
1123
			statuses.add(OrderStatus.DELIVERY_SUCCESS);
4452 rajveer 1124
			statuses.add(OrderStatus.DOA_PICKUP_REQUEST_RAISED);
2449 chandransh 1125
			break;
2610 chandransh 1126
 
1127
		case DOA_AWAITED:
4452 rajveer 1128
			statuses.add(OrderStatus.DOA_PICKUP_CONFIRMED);
2610 chandransh 1129
			statuses.add(OrderStatus.DOA_RETURN_IN_TRANSIT);
4452 rajveer 1130
			statuses.add(OrderStatus.DOA_RECEIVED_PRESTINE);
2610 chandransh 1131
			break;
2449 chandransh 1132
 
4495 rajveer 1133
		case RTO_AWAITED:
4484 rajveer 1134
			statuses.add(OrderStatus.RTO_IN_TRANSIT);
2509 chandransh 1135
			break;
1136
 
4515 mandeep.dh 1137
        case RTO_RECEIVED_DAMAGED:
1138
            statuses.add(OrderStatus.RTO_RECEIVED_DAMAGED);
1139
            break;
1140
 
1141
        case DOA_RECEIVED_PRESTINE:
2610 chandransh 1142
			statuses.add(OrderStatus.DOA_CERT_VALID);
4515 mandeep.dh 1143
			statuses.add(OrderStatus.DOA_CERT_INVALID);
2610 chandransh 1144
			break;
1145
 
4495 rajveer 1146
		case RTO_RETURNED:
4484 rajveer 1147
			statuses.add(OrderStatus.RTO_RECEIVED_PRESTINE);
2509 chandransh 1148
			break;
1149
 
2628 chandransh 1150
		case RESHIPPED:
4484 rajveer 1151
			statuses.add(OrderStatus.RTO_RESHIPPED);
2628 chandransh 1152
			statuses.add(OrderStatus.DOA_INVALID_RESHIPPED);
4452 rajveer 1153
			statuses.add(OrderStatus.DOA_VALID_RESHIPPED);
2628 chandransh 1154
			break;
1155
 
1156
		case REFUNDED:
4682 rajveer 1157
		    statuses.add(OrderStatus.COD_VERIFICATION_FAILED);
4484 rajveer 1158
			statuses.add(OrderStatus.RTO_REFUNDED);
2628 chandransh 1159
			statuses.add(OrderStatus.DOA_INVALID_REFUNDED);
1160
			statuses.add(OrderStatus.DOA_VALID_REFUNDED);
4682 rajveer 1161
			statuses.add(OrderStatus.CANCELLED_DUE_TO_LOW_INVENTORY);
2628 chandransh 1162
			break;
2509 chandransh 1163
 
3065 chandransh 1164
		case VERIFICATION_PENDING:
4664 rajveer 1165
		    statuses.add(OrderStatus.COD_VERIFICATION_PENDING);
3065 chandransh 1166
		    break;
2449 chandransh 1167
		default:
1168
			break;
1169
		}
1170
		return statuses;
1171
	}
13146 manish.sha 1172
 
1173
	public static List<Long> getEligibleOrdersToBeAccepted(Order order){
1174
		List<Long> orders = new ArrayList<Long>();
1175
		try {
1176
 
1177
			TransactionClient client = new TransactionClient();
1178
			Client c = client.getClient();
1179
 
1180
			in.shop2020.model.v1.order.Order thriftOrder = c.getOrder(order.getOrderId());
1181
			long provider_id = thriftOrder.getLogistics_provider_id();
1182
			boolean cod = thriftOrder.isLogisticsCod();
1183
 
1184
			List<in.shop2020.model.v1.order.Order> torders = c.getOrdersForTransaction(thriftOrder.getTransactionId(), thriftOrder.getCustomer_id());
1185
			for(in.shop2020.model.v1.order.Order torder: torders){
1186
				if(torder.getLogistics_provider_id() == provider_id && torder.isLogisticsCod() == cod){
1187
					orders.add(torder.getId());
1188
				}
1189
			}
1190
 
1191
		}catch(Exception e){
1192
			e.printStackTrace();
1193
		}
1194
		return orders;
1195
	}
1196
 
1197
	public static String getItemDisplayName(LineItem lineitem){
1198
		StringBuffer itemName = new StringBuffer();
1199
        if (lineitem.getBrand() != null)
1200
            itemName.append(lineitem.getBrand() + " ");
1201
        if (lineitem.getModel_name() != null)
1202
            itemName.append(lineitem.getModel_name() + " ");
1203
        if (lineitem.getModel_number() != null)
1204
            itemName.append(lineitem.getModel_number() + " ");
1205
        if (lineitem.getColor() != null
1206
                && !lineitem.getColor().trim().equals("NA"))
1207
            itemName.append("(" + lineitem.getColor() + ")");
1208
 
1209
        return itemName.toString();
1210
	}
1211
 
1212
	public static String getProductName(in.shop2020.model.v1.catalog.Item item){
1213
		StringBuffer itemName = new StringBuffer();
1214
        if (item.getBrand() != null)
1215
            itemName.append(item.getBrand() + " ");
1216
        if (item.getModelName() != null)
1217
            itemName.append(item.getModelName() + " ");
1218
        if (item.getModelNumber() != null)
1219
            itemName.append(item.getModelNumber() + " ");
1220
        if (item.getColor() != null
1221
                && !item.getColor().trim().equals("NA"))
1222
            itemName.append("(" + item.getColor() + ")");
1223
 
1224
        return itemName.toString();
1225
	}
19050 manish.sha 1226
 
1227
	public static List<Long> loadVirtualWarehouseDetails(){
1228
		EhcacheWrapper<String, List<Long>> virtualWhCache = new EhcacheWrapper<String, List<Long>>(
1229
				"VirtualWarehouses", CacheManager.create());
1230
 
1231
		List<Long> virtualWarehouseIds = virtualWhCache.get("virtual_warehouses");
1232
 
1233
		if(virtualWarehouseIds!=null && virtualWarehouseIds.size()>0){
19052 manish.sha 1234
			System.out.println("Virtual Warehouse Exists");
1235
			logger.info("Virtual Warehouse Exists");
19050 manish.sha 1236
			return virtualWarehouseIds;
1237
		}else{
19052 manish.sha 1238
			System.out.println("Virtual Warehouse Not Exists");
1239
			logger.info("Virtual Warehouse Not Exists");
19050 manish.sha 1240
			virtualWarehouseIds = new ArrayList<Long>();
1241
			try{
1242
				in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient().getClient();
1243
				List<Warehouse> allWarehouses = inventoryClient.getWarehouses(null, null, 0, 0, 0);
1244
				for(Warehouse wh:allWarehouses){
1245
					if(wh.getWarehouseType()==WarehouseType.THIRD_PARTY){
1246
						virtualWarehouseIds.add(wh.getId());
1247
					}
1248
				}
1249
			}catch(Exception e){
1250
				e.printStackTrace();
1251
			}
1252
			virtualWhCache.put("virtual_warehouses", virtualWarehouseIds);
1253
		}
1254
 
1255
		return virtualWarehouseIds;
1256
	}
493 rajveer 1257
}