Subversion Repositories SmartDukaan

Rev

Rev 4004 | Rev 4172 | 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
 
11
import java.util.ArrayList;
12
import java.util.Date;
13
import java.util.List;
14
 
2449 chandransh 15
/**
16
 * This class is a facade to the Transaction service and should be used to
4133 chandransh 17
 * access all order specific data which requires some kind of transformation.
2449 chandransh 18
 * 
19
 * @author Chandranshu
20
 * 
21
 */
493 rajveer 22
public class TransactionUtils {
2449 chandransh 23
	/**
24
	 * The human user is concerned only with a consolidated view of actionable
25
	 * orders. Orders with different statuses in the database can be part of the
26
	 * same consolidated view. This method uses a mapping of <i>type</i> to
27
	 * <i>status</i> to get all such orders and return them as order beans.
28
	 * 
29
	 * @param type
30
	 *            The type of orders to return.
4133 chandransh 31
	 * @param offset
32
	 *            Offset to start from
33
	 * @param limit
34
	 *            No. of orders to return
2449 chandransh 35
	 * @param warehouseId
36
	 *            The warehouse for which the orders should be queried.
37
	 * @return A list of orders of the given type to be fulfilled from the given
38
	 *         warehouse
39
	 */
4133 chandransh 40
	public static List<Order> getOrders(OrderType type, long offset, long limit, long warehouseId){
2449 chandransh 41
		List<OrderStatus> statuses = getStatuses(type);
493 rajveer 42
 
43
		List<Order> orders = new ArrayList<Order>();
44
		try{
4133 chandransh 45
			TransactionClient txnClient = new TransactionClient();
46
			Client client = txnClient.getClient();
493 rajveer 47
 
48
			List<in.shop2020.model.v1.order.Order> t_orders = new ArrayList<in.shop2020.model.v1.order.Order>();
4133 chandransh 49
			t_orders.addAll(client.getOrdersInBatch(statuses, offset, limit, warehouseId));
493 rajveer 50
 
51
			for (in.shop2020.model.v1.order.Order t_order: t_orders){
671 chandransh 52
				Order o = getOrderFromThriftOrder(t_order);
493 rajveer 53
				orders.add(o);
54
			}
55
		}catch(Exception e){
2449 chandransh 56
			e.printStackTrace();
493 rajveer 57
		}
58
		return orders;
59
	}
60
 
2449 chandransh 61
	/**
4133 chandransh 62
	 * Wrapper around the method of same name in the transaction service. This
63
	 * method uses a mapping of <i>type</i> to <i>status</i> to get the count of
64
	 * all orders with a given status.
65
	 * 
66
	 * @param type
67
	 *            The type of orders to return.
68
	 * @param warehouseId
69
	 *            The warehouse for which the orders should be queried.
70
	 * @return The count of orders of the given type assigned to the given
71
	 *         warehouse.
72
	 */
73
	public static int getOrderCount(OrderType type, long warehouseId){
74
		List<OrderStatus> statuses = getStatuses(type);
75
 
76
		int count = 0;
77
		try{
78
			TransactionClient txnClient = new TransactionClient();
79
			Client client = txnClient.getClient();
80
			count += client.getOrderCount(statuses, warehouseId);
81
		}catch(Exception e){
82
			e.printStackTrace();
83
		}
84
		return count;
85
	}
86
 
87
	/**
2449 chandransh 88
	 * Calls the same method of the transaction service and returns the status
89
	 * returned. Catches all exceptions that are raised and returns false in
90
	 * that case.
91
	 * 
92
	 * @param warehouseId
93
	 *            The warehouse for which the orders should be marked as
94
	 *            manifested.
95
	 * @param providerId
96
	 *            The provider for which the orders should be marked as
97
	 *            manifested.
3065 chandransh 98
	 * @param cod
99
	 *             Whether cod orders have to be marked as manifested
2449 chandransh 100
	 * @return True if everything goes fine, false otherwise.
101
	 */
3065 chandransh 102
	public static boolean markOrdersAsManifested(long warehouseId, String providerId, boolean cod){
760 chandransh 103
		try {
104
			long provider_id = Long.parseLong(providerId);
3132 rajveer 105
			TransactionClient client = new TransactionClient();
760 chandransh 106
			Client c = client.getClient();
3065 chandransh 107
			return c.markOrdersAsManifested(warehouseId, provider_id, cod);
760 chandransh 108
		}catch(Exception e){
109
			e.printStackTrace();
110
			return false;
111
		}
112
	}
2449 chandransh 113
 
671 chandransh 114
	/**
2449 chandransh 115
	 * 
671 chandransh 116
	 * @param t_order
2449 chandransh 117
	 *            A thrift order object with line items.
118
	 * @return an Order bean which can be used for rendering on the client side.
671 chandransh 119
	 */
120
	public static Order getOrderFromThriftOrder(in.shop2020.model.v1.order.Order t_order) {
914 chandransh 121
		LineItem lineItem = t_order.getLineitems().get(0);
3553 chandransh 122
 
123
		String delayReason = null;
124
		if(t_order.getDelayReason() != null)
125
		    delayReason = t_order.getDelayReason().name(); 
126
 
966 chandransh 127
		Order order = new Order(t_order.getId(),
128
				t_order.getCustomer_id(),
129
				t_order.getCustomer_name(),
130
				t_order.getCustomer_mobilenumber(),
131
				t_order.getCustomer_pincode(),
132
				t_order.getCustomer_address1(),
133
				t_order.getCustomer_address2(),
134
				t_order.getCustomer_city(),
135
				t_order.getCustomer_state(),
136
				t_order.getCustomer_email(),
137
				t_order.getCreated_timestamp(),
4004 chandransh 138
				t_order.getShipping_timestamp(),
139
				t_order.getVerification_timestamp(),
966 chandransh 140
				t_order.getExpected_delivery_time(),
3994 chandransh 141
				t_order.getPromised_delivery_time(),
4004 chandransh 142
				t_order.getExpected_shipping_time(),
966 chandransh 143
				t_order.getStatus().getValue(),
144
				t_order.getStatusDescription(),
145
				lineItem.getItem_id(),
146
				lineItem.getProductGroup(),
147
				lineItem.getBrand(),
148
				lineItem.getModel_name(),
149
				lineItem.getModel_number(),
150
				lineItem.getColor(),
151
				lineItem.getExtra_info(),
2782 chandransh 152
				t_order.getTotal_amount(),
153
				t_order.getTotal_weight(),
154
				t_order.getAirwaybill_no(),
155
				t_order.getBilled_by(),
156
				t_order.getInvoice_number(),
157
				t_order.getJacket_number(),
158
				lineItem.getItem_number(),
159
				lineItem.getImei_number(),
3065 chandransh 160
				t_order.getBatchNo(),
161
				t_order.getSerialNo(),
2509 chandransh 162
				t_order.isDoaFlag(),
3065 chandransh 163
				t_order.getPickupRequestNo(),
3553 chandransh 164
				t_order.isCod(),
165
				delayReason);
966 chandransh 166
		return order;
671 chandransh 167
	}
168
 
2449 chandransh 169
	/**
2697 chandransh 170
	 * Queries the transction server to get the list of all return orders that
171
	 * have to be processed.
172
	 * 
173
	 * @return A list of all return orders to be processed.
174
	 */
175
	public static List<ReturnOrder> getReturnOrders(long warehouseId){
176
		List<ReturnOrder> retOrders = new ArrayList<ReturnOrder>();
177
		try{
3132 rajveer 178
			TransactionClient client = new TransactionClient();
2697 chandransh 179
			Client c = client.getClient();
180
			List<in.shop2020.model.v1.order.ReturnOrder> tRetOrders =  c.getReturnOrders(warehouseId, 0L, new Date().getTime());
181
			for(in.shop2020.model.v1.order.ReturnOrder retOrder : tRetOrders){
182
				retOrders.add(getReturnOrderFromThriftRO(retOrder));
183
			}
184
		}catch(Exception e){
185
			e.printStackTrace();
186
		}
187
		return retOrders;
188
	}
189
 
2700 chandransh 190
	public static ReturnOrder getReturnOrderFromThriftRO(in.shop2020.model.v1.order.ReturnOrder tRetOrder){
191
		ReturnOrder retOrder = new ReturnOrder(tRetOrder.getOrderId(),
192
				tRetOrder.getWarehouseId(),
193
				tRetOrder.getItemId(),
194
				tRetOrder.getProductGroup(),
195
				tRetOrder.getBrand(),
196
				tRetOrder.getModelName(),
197
				tRetOrder.getModelNumber(),
198
				tRetOrder.getColor(),
199
				tRetOrder.getInvoiceNumber(),
200
				tRetOrder.getJacketNumber(),
201
				tRetOrder.getTotalPrice(),
202
				tRetOrder.getTransferPrice(),
203
				false,
204
				tRetOrder.getCreatedAt());
2697 chandransh 205
		return retOrder;
206
	}
207
 
208
	/**
2449 chandransh 209
	 * This method maps a given type to a list of statuses.
210
	 * 
211
	 * @param type
212
	 *            The type of orders to fetch.
213
	 * @return The list of Thrift statuses associated with a particular order
214
	 *         type.
215
	 */
216
	private static List<OrderStatus> getStatuses(OrderType type) {
217
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
218
 
219
		switch (type) {
220
		case ACCEPTED:
221
			statuses.add(OrderStatus.ACCEPTED);
222
			break;
223
 
224
		case ALL:
225
			break;
226
 
227
		case NEW:
228
			statuses.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
229
			statuses.add(OrderStatus.INVENTORY_LOW);
230
			break;
231
 
232
		case BILLED:
233
			statuses.add(OrderStatus.BILLED);
234
			break;
235
 
236
		case NO_STOCK:
237
			statuses.add(OrderStatus.INVENTORY_LOW);
238
			break;
239
 
240
		case REJECTED:
241
			statuses.add(OrderStatus.REJECTED);
242
			break;
243
 
244
		case SHIPPED:
245
			statuses.add(OrderStatus.SHIPPED_FROM_WH);
246
			statuses.add(OrderStatus.SHIPPED_TO_LOGST);
247
			break;
248
 
249
		case DELIVERED:
250
			statuses.add(OrderStatus.DELIVERY_SUCCESS);
2509 chandransh 251
			statuses.add(OrderStatus.DOA_PICKUP_REQUESTED);
2449 chandransh 252
			break;
2610 chandransh 253
 
254
		case DOA_AWAITED:
255
			statuses.add(OrderStatus.DOA_RETURN_AUTHORIZED);
256
			statuses.add(OrderStatus.DOA_RETURN_IN_TRANSIT);
257
			statuses.add(OrderStatus.DOA_RECEIVED);
258
			break;
2449 chandransh 259
 
2509 chandransh 260
		case SALES_RETURN_AWAITED:
261
			statuses.add(OrderStatus.SALES_RETURN_IN_TRANSIT);
262
			break;
263
 
2610 chandransh 264
		case DOA_RETURNED:
265
			statuses.add(OrderStatus.DOA_CERT_VALID);
266
			break;
267
 
2509 chandransh 268
		case SALES_RETURNED:
2610 chandransh 269
			statuses.add(OrderStatus.SALES_RET_RECEIVED);
270
			statuses.add(OrderStatus.DOA_CERT_INVALID);
2509 chandransh 271
			break;
272
 
2628 chandransh 273
		case RESHIPPED:
274
			statuses.add(OrderStatus.SALES_RET_RESHIPPED);
275
			statuses.add(OrderStatus.DOA_INVALID_RESHIPPED);
276
			statuses.add(OrderStatus.DOA_RESHIPPED);
277
			break;
278
 
279
		case REFUNDED:
3196 chandransh 280
		    statuses.add(OrderStatus.CANCELED);
2628 chandransh 281
			statuses.add(OrderStatus.SALES_RET_REFUNDED);
282
			statuses.add(OrderStatus.DOA_INVALID_REFUNDED);
283
			statuses.add(OrderStatus.DOA_VALID_REFUNDED);
284
			statuses.add(OrderStatus.REFUNDED);
285
			break;
2509 chandransh 286
 
3065 chandransh 287
		case VERIFICATION_PENDING:
288
		    statuses.add(OrderStatus.INIT);
289
		    break;
2449 chandransh 290
		default:
291
			break;
292
		}
293
		return statuses;
294
	}
493 rajveer 295
}