Subversion Repositories SmartDukaan

Rev

Rev 2697 | Rev 2782 | 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;
493 rajveer 6
import in.shop2020.model.v1.order.Alert;
914 chandransh 7
import in.shop2020.model.v1.order.LineItem;
493 rajveer 8
import in.shop2020.model.v1.order.OrderStatus;
9
import in.shop2020.model.v1.order.TransactionService.Client;
10
import in.shop2020.thrift.clients.TransactionServiceClient;
11
 
12
import java.util.ArrayList;
13
import java.util.Date;
14
import java.util.List;
15
 
2449 chandransh 16
/**
17
 * This class is a facade to the Transaction service and should be used to
18
 * access all order specific data.
19
 * 
20
 * @author Chandranshu
21
 * 
22
 */
493 rajveer 23
public class TransactionUtils {
2449 chandransh 24
	/**
25
	 * The human user is concerned only with a consolidated view of actionable
26
	 * orders. Orders with different statuses in the database can be part of the
27
	 * same consolidated view. This method uses a mapping of <i>type</i> to
28
	 * <i>status</i> to get all such orders and return them as order beans.
29
	 * 
30
	 * @param type
31
	 *            The type of orders to return.
32
	 * @param warehouseId
33
	 *            The warehouse for which the orders should be queried.
34
	 * @return A list of orders of the given type to be fulfilled from the given
35
	 *         warehouse
36
	 */
493 rajveer 37
	public static List<Order> getOrders(OrderType type, long warehouseId){
2449 chandransh 38
		List<OrderStatus> statuses = getStatuses(type);
493 rajveer 39
 
40
		List<Order> orders = new ArrayList<Order>();
41
		try{
42
			TransactionServiceClient client = new TransactionServiceClient();
43
			Client c = client.getClient();
44
 
45
			List<in.shop2020.model.v1.order.Order> t_orders = new ArrayList<in.shop2020.model.v1.order.Order>();
46
			if(statuses.isEmpty()){
47
				t_orders.addAll(c.getAllOrders(null, 0L, new Date().getTime(), warehouseId));
48
			}
49
			else {
50
				for(OrderStatus status: statuses){
51
					t_orders.addAll(c.getAllOrders(status, 0L, new Date().getTime(), warehouseId));
52
				}
53
			}
54
 
55
			for (in.shop2020.model.v1.order.Order t_order: t_orders){
671 chandransh 56
				Order o = getOrderFromThriftOrder(t_order);
493 rajveer 57
 
58
				orders.add(o);
59
				//check if it has an associated alert
60
				List<Alert> alerts = c.getAlerts(t_order.getId(), true);
2449 chandransh 61
				if(alerts != null && alerts.size() != 0){
62
					o.setAlert(true);
63
					o.setStatusMessage(alerts.iterator().next().getComment());
64
				} else {
493 rajveer 65
					o.setAlert(false);
66
				}
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.
85
	 * @return True if everything goes fine, false otherwise.
86
	 */
760 chandransh 87
	public static boolean markOrdersAsManifested(long warehouseId, String providerId){
88
		try {
89
			long provider_id = Long.parseLong(providerId);
90
			TransactionServiceClient client = new TransactionServiceClient();
91
			Client c = client.getClient();
92
			return c.markOrdersAsManifested(warehouseId, provider_id);
93
		}catch(Exception e){
94
			e.printStackTrace();
95
			return false;
96
		}
97
	}
2449 chandransh 98
 
671 chandransh 99
	/**
2449 chandransh 100
	 * 
671 chandransh 101
	 * @param t_order
2449 chandransh 102
	 *            A thrift order object with line items.
103
	 * @return an Order bean which can be used for rendering on the client side.
671 chandransh 104
	 */
105
	public static Order getOrderFromThriftOrder(in.shop2020.model.v1.order.Order t_order) {
914 chandransh 106
		LineItem lineItem = t_order.getLineitems().get(0);
966 chandransh 107
		Order order = new Order(t_order.getId(),
108
				t_order.getCustomer_id(),
109
				t_order.getCustomer_name(),
110
				t_order.getCustomer_mobilenumber(),
111
				t_order.getCustomer_pincode(),
112
				t_order.getCustomer_address1(),
113
				t_order.getCustomer_address2(),
114
				t_order.getCustomer_city(),
115
				t_order.getCustomer_state(),
116
				t_order.getCustomer_email(),
117
				t_order.getCreated_timestamp(),
118
				t_order.getExpected_delivery_time(),
119
				t_order.getStatus().getValue(),
120
				t_order.getStatusDescription(),
121
				lineItem.getItem_id(),
122
				lineItem.getProductGroup(),
123
				lineItem.getBrand(),
124
				lineItem.getModel_name(),
125
				lineItem.getModel_number(),
126
				lineItem.getColor(),
127
				lineItem.getExtra_info(),
128
				t_order.getTotal_amount(), t_order.getTotal_weight(), t_order.getAirwaybill_no(),
2452 chandransh 129
				t_order.getBilled_by(), t_order.getInvoice_number(), t_order.getJacket_number(),
130
				-1, //FIXME This parameter should be lineItem.getImei_number()
1224 chandransh 131
				t_order.getBatchNo(), t_order.getSerialNo(),
2509 chandransh 132
				false,
133
				t_order.isDoaFlag(),
134
				t_order.getPickupRequestNo());
966 chandransh 135
		return order;
671 chandransh 136
	}
137
 
2449 chandransh 138
	/**
2697 chandransh 139
	 * Queries the transction server to get the list of all return orders that
140
	 * have to be processed.
141
	 * 
142
	 * @return A list of all return orders to be processed.
143
	 */
144
	public static List<ReturnOrder> getReturnOrders(long warehouseId){
145
		List<ReturnOrder> retOrders = new ArrayList<ReturnOrder>();
146
		try{
147
			TransactionServiceClient client = new TransactionServiceClient();
148
			Client c = client.getClient();
149
			List<in.shop2020.model.v1.order.ReturnOrder> tRetOrders =  c.getReturnOrders(warehouseId, 0L, new Date().getTime());
150
			for(in.shop2020.model.v1.order.ReturnOrder retOrder : tRetOrders){
151
				retOrders.add(getReturnOrderFromThriftRO(retOrder));
152
			}
153
		}catch(Exception e){
154
			e.printStackTrace();
155
		}
156
		return retOrders;
157
	}
158
 
2700 chandransh 159
	public static ReturnOrder getReturnOrderFromThriftRO(in.shop2020.model.v1.order.ReturnOrder tRetOrder){
160
		ReturnOrder retOrder = new ReturnOrder(tRetOrder.getOrderId(),
161
				tRetOrder.getWarehouseId(),
162
				tRetOrder.getItemId(),
163
				tRetOrder.getProductGroup(),
164
				tRetOrder.getBrand(),
165
				tRetOrder.getModelName(),
166
				tRetOrder.getModelNumber(),
167
				tRetOrder.getColor(),
168
				tRetOrder.getInvoiceNumber(),
169
				tRetOrder.getJacketNumber(),
170
				tRetOrder.getTotalPrice(),
171
				tRetOrder.getTransferPrice(),
172
				false,
173
				tRetOrder.getCreatedAt());
2697 chandransh 174
		return retOrder;
175
	}
176
 
177
	/**
2449 chandransh 178
	 * This method maps a given type to a list of statuses.
179
	 * 
180
	 * @param type
181
	 *            The type of orders to fetch.
182
	 * @return The list of Thrift statuses associated with a particular order
183
	 *         type.
184
	 */
185
	private static List<OrderStatus> getStatuses(OrderType type) {
186
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
187
 
188
		switch (type) {
189
		case ACCEPTED:
190
			statuses.add(OrderStatus.ACCEPTED);
191
			break;
192
 
193
		case ALL:
194
			break;
195
 
196
		case NEW:
197
			statuses.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
198
			statuses.add(OrderStatus.INVENTORY_LOW);
199
			break;
200
 
201
		case BILLED:
202
			statuses.add(OrderStatus.BILLED);
203
			break;
204
 
205
		case NO_STOCK:
206
			statuses.add(OrderStatus.INVENTORY_LOW);
207
			break;
208
 
209
		case REJECTED:
210
			statuses.add(OrderStatus.REJECTED);
211
			break;
212
 
213
		case SHIPPED:
214
			statuses.add(OrderStatus.SHIPPED_FROM_WH);
215
			statuses.add(OrderStatus.SHIPPED_TO_LOGST);
216
			break;
217
 
218
		case DELIVERED:
219
			statuses.add(OrderStatus.DELIVERY_SUCCESS);
2509 chandransh 220
			statuses.add(OrderStatus.DOA_PICKUP_REQUESTED);
2449 chandransh 221
			break;
2610 chandransh 222
 
223
		case DOA_AWAITED:
224
			statuses.add(OrderStatus.DOA_RETURN_AUTHORIZED);
225
			statuses.add(OrderStatus.DOA_RETURN_IN_TRANSIT);
226
			statuses.add(OrderStatus.DOA_RECEIVED);
227
			break;
2449 chandransh 228
 
2509 chandransh 229
		case SALES_RETURN_AWAITED:
230
			statuses.add(OrderStatus.SALES_RETURN_IN_TRANSIT);
231
			break;
232
 
2610 chandransh 233
		case DOA_RETURNED:
234
			statuses.add(OrderStatus.DOA_CERT_VALID);
235
			break;
236
 
2509 chandransh 237
		case SALES_RETURNED:
2610 chandransh 238
			statuses.add(OrderStatus.SALES_RET_RECEIVED);
239
			statuses.add(OrderStatus.DOA_CERT_INVALID);
2509 chandransh 240
			break;
241
 
2628 chandransh 242
		case RESHIPPED:
243
			statuses.add(OrderStatus.SALES_RET_RESHIPPED);
244
			statuses.add(OrderStatus.DOA_INVALID_RESHIPPED);
245
			statuses.add(OrderStatus.DOA_RESHIPPED);
246
			break;
247
 
248
		case REFUNDED:
249
			statuses.add(OrderStatus.SALES_RET_REFUNDED);
250
			statuses.add(OrderStatus.DOA_INVALID_REFUNDED);
251
			statuses.add(OrderStatus.DOA_VALID_REFUNDED);
252
			statuses.add(OrderStatus.REFUNDED);
253
			break;
2509 chandransh 254
 
2449 chandransh 255
		default:
256
			break;
257
		}
258
		return statuses;
259
	}
493 rajveer 260
}