Subversion Repositories SmartDukaan

Rev

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