Subversion Repositories SmartDukaan

Rev

Rev 4411 | Rev 4421 | 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
 
3
import in.shop2020.hotspot.dashbaord.shared.actions.Order;
4
import in.shop2020.hotspot.dashbaord.shared.actions.OrderType;
2697 chandransh 5
import in.shop2020.hotspot.dashbaord.shared.actions.ReturnOrder;
914 chandransh 6
import in.shop2020.model.v1.order.LineItem;
493 rajveer 7
import in.shop2020.model.v1.order.OrderStatus;
8
import in.shop2020.model.v1.order.TransactionService.Client;
3132 rajveer 9
import in.shop2020.thrift.clients.TransactionClient;
493 rajveer 10
 
4411 rajveer 11
import java.io.ByteArrayOutputStream;
12
import java.io.File;
13
import java.io.FileNotFoundException;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
493 rajveer 16
import java.util.ArrayList;
4411 rajveer 17
import java.util.Calendar;
493 rajveer 18
import java.util.Date;
4411 rajveer 19
import java.util.GregorianCalendar;
20
import java.util.HashMap;
493 rajveer 21
import java.util.List;
4411 rajveer 22
import java.util.Map;
493 rajveer 23
 
4411 rajveer 24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26
 
2449 chandransh 27
/**
28
 * This class is a facade to the Transaction service and should be used to
4133 chandransh 29
 * access all order specific data which requires some kind of transformation.
2449 chandransh 30
 * 
31
 * @author Chandranshu
32
 * 
33
 */
493 rajveer 34
public class TransactionUtils {
4411 rajveer 35
	private static String courierDetailsPath = "/CourierDetailReports";
36
	private static Logger logger = LoggerFactory.getLogger(TransactionUtils.class);
37
 
2449 chandransh 38
	/**
39
	 * The human user is concerned only with a consolidated view of actionable
40
	 * orders. Orders with different statuses in the database can be part of the
41
	 * same consolidated view. This method uses a mapping of <i>type</i> to
42
	 * <i>status</i> to get all such orders and return them as order beans.
43
	 * 
44
	 * @param type
45
	 *            The type of orders to return.
4133 chandransh 46
	 * @param offset
47
	 *            Offset to start from
48
	 * @param limit
49
	 *            No. of orders to return
2449 chandransh 50
	 * @param warehouseId
51
	 *            The warehouse for which the orders should be queried.
52
	 * @return A list of orders of the given type to be fulfilled from the given
53
	 *         warehouse
54
	 */
4133 chandransh 55
	public static List<Order> getOrders(OrderType type, long offset, long limit, long warehouseId){
2449 chandransh 56
		List<OrderStatus> statuses = getStatuses(type);
493 rajveer 57
 
58
		List<Order> orders = new ArrayList<Order>();
59
		try{
4133 chandransh 60
			TransactionClient txnClient = new TransactionClient();
61
			Client client = txnClient.getClient();
493 rajveer 62
 
63
			List<in.shop2020.model.v1.order.Order> t_orders = new ArrayList<in.shop2020.model.v1.order.Order>();
4133 chandransh 64
			t_orders.addAll(client.getOrdersInBatch(statuses, offset, limit, warehouseId));
493 rajveer 65
 
66
			for (in.shop2020.model.v1.order.Order t_order: t_orders){
671 chandransh 67
				Order o = getOrderFromThriftOrder(t_order);
493 rajveer 68
				orders.add(o);
69
			}
70
		}catch(Exception e){
2449 chandransh 71
			e.printStackTrace();
493 rajveer 72
		}
73
		return orders;
74
	}
75
 
2449 chandransh 76
	/**
4133 chandransh 77
	 * Wrapper around the method of same name in the transaction service. This
78
	 * method uses a mapping of <i>type</i> to <i>status</i> to get the count of
79
	 * all orders with a given status.
80
	 * 
81
	 * @param type
82
	 *            The type of orders to return.
83
	 * @param warehouseId
84
	 *            The warehouse for which the orders should be queried.
85
	 * @return The count of orders of the given type assigned to the given
86
	 *         warehouse.
87
	 */
88
	public static int getOrderCount(OrderType type, long warehouseId){
89
		List<OrderStatus> statuses = getStatuses(type);
90
 
91
		int count = 0;
92
		try{
93
			TransactionClient txnClient = new TransactionClient();
94
			Client client = txnClient.getClient();
95
			count += client.getOrderCount(statuses, warehouseId);
96
		}catch(Exception e){
97
			e.printStackTrace();
98
		}
99
		return count;
100
	}
101
 
102
	/**
2449 chandransh 103
	 * Calls the same method of the transaction service and returns the status
104
	 * returned. Catches all exceptions that are raised and returns false in
105
	 * that case.
106
	 * 
107
	 * @param warehouseId
108
	 *            The warehouse for which the orders should be marked as
109
	 *            manifested.
110
	 * @param providerId
111
	 *            The provider for which the orders should be marked as
112
	 *            manifested.
3065 chandransh 113
	 * @param cod
114
	 *             Whether cod orders have to be marked as manifested
2449 chandransh 115
	 * @return True if everything goes fine, false otherwise.
116
	 */
3065 chandransh 117
	public static boolean markOrdersAsManifested(long warehouseId, String providerId, boolean cod){
760 chandransh 118
		try {
119
			long provider_id = Long.parseLong(providerId);
3132 rajveer 120
			TransactionClient client = new TransactionClient();
760 chandransh 121
			Client c = client.getClient();
4411 rajveer 122
			if(generateCourierDetailsFile(warehouseId, provider_id, cod)){
123
				return c.markOrdersAsShippedFromWarehouse(warehouseId, provider_id, cod);	
124
			}else{
125
				return false;
126
			}
127
 
760 chandransh 128
		}catch(Exception e){
129
			e.printStackTrace();
130
			return false;
131
		}
132
	}
2449 chandransh 133
 
4411 rajveer 134
	private static boolean generateCourierDetailsFile(long warehouseId, long providerId, boolean isCod){
135
		Calendar date = new GregorianCalendar();
136
		int year = date.get(Calendar.YEAR);
137
		int month = date.get(Calendar.MONTH) +1;
138
		int day = date.get(Calendar.DAY_OF_MONTH);
139
 
140
		String fileNameSuffix = "-" + warehouseId + "-"+ providerId + "-" + year + "-" + month + "-" + day;
141
		String mergedFileNameSuffix = "-" + 0 + "-"+ providerId + "-" + year + "-" + month + "-" + day;
142
		if(isCod){
143
		    fileNameSuffix = "cod" + fileNameSuffix ;
144
			mergedFileNameSuffix = "cod" + mergedFileNameSuffix;
145
		}
146
		else{
147
		    fileNameSuffix = "prepaid" + fileNameSuffix;
148
			mergedFileNameSuffix = "prepaid" + mergedFileNameSuffix;
149
		}
150
 
151
		try {
152
			String fileName = courierDetailsPath + "/courier-details-" + fileNameSuffix + ".xls";
153
			File file = new File(fileName);
154
			if(file.exists()){
155
				return false;
156
			}
4415 rajveer 157
			FileOutputStream f = new FileOutputStream(fileName);
4411 rajveer 158
			CourierDetailsGenerator courierDetailsGenerator = new CourierDetailsGenerator();
159
			ByteArrayOutputStream baosXLS = courierDetailsGenerator.generateCourierDetails(warehouseId, providerId, isCod);
160
			baosXLS.writeTo(f);
161
			f.close();
162
		} catch (FileNotFoundException e) {
163
			logger.error("Unable to create the courier details file", e);
164
		} catch (IOException e) {
165
			logger.error("Unable to create the courier details file", e);
166
		}
167
 
168
		// FIXME This is not a useful way. We need to fix it asap. This is done just to give ease to courier company.
169
		CourierDetailsReportMerger merger = new CourierDetailsReportMerger();
170
		try {
171
			FileOutputStream f = new FileOutputStream(courierDetailsPath + "/courier-details-" + mergedFileNameSuffix + ".xls");
172
			Map<Long, String> warehouseIdFileNames = new HashMap<Long, String>();
173
 
174
			String p1;
175
			if(isCod){
176
				p1 = "cod";
177
			}
178
			else{
179
				p1 = "prepaid";
180
			}
181
			String fName;
182
			fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 1 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
183
			warehouseIdFileNames.put(1L, fName);
184
			fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 2 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
185
			warehouseIdFileNames.put(2L, fName);
186
			fName = courierDetailsPath + "/courier-details-" + p1 + "-" + 5 + "-"+ providerId + "-" + year + "-" + month + "-" + day + ".xls";
187
			warehouseIdFileNames.put(5L, fName);
188
			ByteArrayOutputStream binXLS = merger.mergeCourierDetailsReports(warehouseIdFileNames, 1, true);
189
			binXLS.writeTo(f);
190
			f.close();
191
		} catch (FileNotFoundException e) {
192
			logger.error("Error while creating the Courier Details report", e);
193
		} catch (IOException e) {
194
			logger.error("IO error while writing the Courier Details report", e);
195
		}
196
		return true;
197
	}
198
 
671 chandransh 199
	/**
2449 chandransh 200
	 * 
671 chandransh 201
	 * @param t_order
2449 chandransh 202
	 *            A thrift order object with line items.
203
	 * @return an Order bean which can be used for rendering on the client side.
671 chandransh 204
	 */
205
	public static Order getOrderFromThriftOrder(in.shop2020.model.v1.order.Order t_order) {
914 chandransh 206
		LineItem lineItem = t_order.getLineitems().get(0);
3553 chandransh 207
 
208
		String delayReason = null;
209
		if(t_order.getDelayReason() != null)
210
		    delayReason = t_order.getDelayReason().name(); 
211
 
966 chandransh 212
		Order order = new Order(t_order.getId(),
213
				t_order.getCustomer_id(),
214
				t_order.getCustomer_name(),
215
				t_order.getCustomer_mobilenumber(),
216
				t_order.getCustomer_pincode(),
217
				t_order.getCustomer_address1(),
218
				t_order.getCustomer_address2(),
219
				t_order.getCustomer_city(),
220
				t_order.getCustomer_state(),
221
				t_order.getCustomer_email(),
222
				t_order.getCreated_timestamp(),
4004 chandransh 223
				t_order.getShipping_timestamp(),
224
				t_order.getVerification_timestamp(),
966 chandransh 225
				t_order.getExpected_delivery_time(),
3994 chandransh 226
				t_order.getPromised_delivery_time(),
4004 chandransh 227
				t_order.getExpected_shipping_time(),
966 chandransh 228
				t_order.getStatus().getValue(),
229
				t_order.getStatusDescription(),
230
				lineItem.getItem_id(),
231
				lineItem.getProductGroup(),
232
				lineItem.getBrand(),
233
				lineItem.getModel_name(),
234
				lineItem.getModel_number(),
235
				lineItem.getColor(),
236
				lineItem.getExtra_info(),
4172 rajveer 237
				lineItem.getDealText(),
2782 chandransh 238
				t_order.getTotal_amount(),
239
				t_order.getTotal_weight(),
240
				t_order.getAirwaybill_no(),
241
				t_order.getBilled_by(),
242
				t_order.getInvoice_number(),
243
				t_order.getJacket_number(),
244
				lineItem.getItem_number(),
245
				lineItem.getImei_number(),
3065 chandransh 246
				t_order.getBatchNo(),
247
				t_order.getSerialNo(),
2509 chandransh 248
				t_order.isDoaFlag(),
3065 chandransh 249
				t_order.getPickupRequestNo(),
3553 chandransh 250
				t_order.isCod(),
251
				delayReason);
966 chandransh 252
		return order;
671 chandransh 253
	}
254
 
2449 chandransh 255
	/**
2697 chandransh 256
	 * Queries the transction server to get the list of all return orders that
257
	 * have to be processed.
258
	 * 
259
	 * @return A list of all return orders to be processed.
260
	 */
261
	public static List<ReturnOrder> getReturnOrders(long warehouseId){
262
		List<ReturnOrder> retOrders = new ArrayList<ReturnOrder>();
263
		try{
3132 rajveer 264
			TransactionClient client = new TransactionClient();
2697 chandransh 265
			Client c = client.getClient();
266
			List<in.shop2020.model.v1.order.ReturnOrder> tRetOrders =  c.getReturnOrders(warehouseId, 0L, new Date().getTime());
267
			for(in.shop2020.model.v1.order.ReturnOrder retOrder : tRetOrders){
268
				retOrders.add(getReturnOrderFromThriftRO(retOrder));
269
			}
270
		}catch(Exception e){
271
			e.printStackTrace();
272
		}
273
		return retOrders;
274
	}
275
 
2700 chandransh 276
	public static ReturnOrder getReturnOrderFromThriftRO(in.shop2020.model.v1.order.ReturnOrder tRetOrder){
277
		ReturnOrder retOrder = new ReturnOrder(tRetOrder.getOrderId(),
278
				tRetOrder.getWarehouseId(),
279
				tRetOrder.getItemId(),
280
				tRetOrder.getProductGroup(),
281
				tRetOrder.getBrand(),
282
				tRetOrder.getModelName(),
283
				tRetOrder.getModelNumber(),
284
				tRetOrder.getColor(),
285
				tRetOrder.getInvoiceNumber(),
286
				tRetOrder.getJacketNumber(),
287
				tRetOrder.getTotalPrice(),
288
				tRetOrder.getTransferPrice(),
289
				false,
290
				tRetOrder.getCreatedAt());
2697 chandransh 291
		return retOrder;
292
	}
293
 
294
	/**
2449 chandransh 295
	 * This method maps a given type to a list of statuses.
296
	 * 
297
	 * @param type
298
	 *            The type of orders to fetch.
299
	 * @return The list of Thrift statuses associated with a particular order
300
	 *         type.
301
	 */
4361 rajveer 302
	public static List<OrderStatus> getStatuses(OrderType type) {
2449 chandransh 303
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
304
 
305
		switch (type) {
306
		case ACCEPTED:
307
			statuses.add(OrderStatus.ACCEPTED);
308
			break;
309
 
4361 rajveer 310
		case ALL_PENDING:
311
			statuses.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
312
			statuses.add(OrderStatus.INVENTORY_LOW);
313
			statuses.add(OrderStatus.LOW_INV_PO_RAISED);
314
			statuses.add(OrderStatus.LOW_INV_REVERSAL_IN_PROCESS);
315
			statuses.add(OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT);
2449 chandransh 316
			break;
317
 
318
		case NEW:
319
			statuses.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
320
			break;
321
 
322
		case BILLED:
323
			statuses.add(OrderStatus.BILLED);
324
			break;
325
 
4308 rajveer 326
		case LOW_INVENTORY:
2449 chandransh 327
			statuses.add(OrderStatus.INVENTORY_LOW);
328
			break;
4248 rajveer 329
 
4308 rajveer 330
		case PO_RAISED:
331
			statuses.add(OrderStatus.LOW_INV_PO_RAISED);
332
			break;
333
 
334
		case REVERSAL_INITIATED:
335
			statuses.add(OrderStatus.LOW_INV_REVERSAL_IN_PROCESS);
336
			break;
337
 
338
		case NOT_AVAILABLE:
339
			statuses.add(OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT);
340
			break;
341
 
4248 rajveer 342
		case CANCEL_CONFIRMED:
343
			statuses.add(OrderStatus.CANCEL_REQUEST_CONFIRMED);
344
			break;
2449 chandransh 345
 
346
		case REJECTED:
347
			statuses.add(OrderStatus.REJECTED);
348
			break;
349
 
350
		case SHIPPED:
351
			statuses.add(OrderStatus.SHIPPED_FROM_WH);
352
			statuses.add(OrderStatus.SHIPPED_TO_LOGST);
353
			break;
354
 
355
		case DELIVERED:
356
			statuses.add(OrderStatus.DELIVERY_SUCCESS);
2509 chandransh 357
			statuses.add(OrderStatus.DOA_PICKUP_REQUESTED);
2449 chandransh 358
			break;
2610 chandransh 359
 
360
		case DOA_AWAITED:
361
			statuses.add(OrderStatus.DOA_RETURN_AUTHORIZED);
362
			statuses.add(OrderStatus.DOA_RETURN_IN_TRANSIT);
363
			statuses.add(OrderStatus.DOA_RECEIVED);
364
			break;
2449 chandransh 365
 
2509 chandransh 366
		case SALES_RETURN_AWAITED:
367
			statuses.add(OrderStatus.SALES_RETURN_IN_TRANSIT);
368
			break;
369
 
2610 chandransh 370
		case DOA_RETURNED:
371
			statuses.add(OrderStatus.DOA_CERT_VALID);
372
			break;
373
 
2509 chandransh 374
		case SALES_RETURNED:
2610 chandransh 375
			statuses.add(OrderStatus.SALES_RET_RECEIVED);
376
			statuses.add(OrderStatus.DOA_CERT_INVALID);
2509 chandransh 377
			break;
378
 
2628 chandransh 379
		case RESHIPPED:
380
			statuses.add(OrderStatus.SALES_RET_RESHIPPED);
381
			statuses.add(OrderStatus.DOA_INVALID_RESHIPPED);
382
			statuses.add(OrderStatus.DOA_RESHIPPED);
383
			break;
384
 
385
		case REFUNDED:
3196 chandransh 386
		    statuses.add(OrderStatus.CANCELED);
2628 chandransh 387
			statuses.add(OrderStatus.SALES_RET_REFUNDED);
388
			statuses.add(OrderStatus.DOA_INVALID_REFUNDED);
389
			statuses.add(OrderStatus.DOA_VALID_REFUNDED);
390
			statuses.add(OrderStatus.REFUNDED);
391
			break;
2509 chandransh 392
 
3065 chandransh 393
		case VERIFICATION_PENDING:
394
		    statuses.add(OrderStatus.INIT);
395
		    break;
2449 chandransh 396
		default:
397
			break;
398
		}
399
		return statuses;
400
	}
493 rajveer 401
}