Subversion Repositories SmartDukaan

Rev

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