Subversion Repositories SmartDukaan

Rev

Rev 428 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 428 Rev 485
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import java.io.IOException;
3
import java.io.IOException;
-
 
4
import java.util.ArrayList;
-
 
5
import java.util.Date;
4
import java.util.HashMap;
6
import java.util.HashMap;
5
import java.util.List;
7
import java.util.List;
6
import java.util.Map;
8
import java.util.Map;
7
import java.util.Set;
9
import java.util.Set;
8
 
10
 
9
import org.apache.struts2.rest.DefaultHttpHeaders;
11
import org.apache.struts2.rest.DefaultHttpHeaders;
10
import org.apache.struts2.rest.HttpHeaders;
12
import org.apache.struts2.rest.HttpHeaders;
11
import org.apache.thrift.TException;
13
import org.apache.thrift.TException;
12
 
14
 
-
 
15
import in.shop2020.logistics.LogisticsInfo;
-
 
16
import in.shop2020.logistics.LogisticsServiceException;
13
import in.shop2020.model.v1.catalog.InventoryServiceException;
17
import in.shop2020.model.v1.catalog.InventoryServiceException;
14
import in.shop2020.model.v1.catalog.Item;
18
import in.shop2020.model.v1.catalog.Item;
15
import in.shop2020.model.v1.order.BillingInfo;
19
import in.shop2020.model.v1.order.BillingInfo;
16
import in.shop2020.model.v1.order.LineItem;
20
import in.shop2020.model.v1.order.LineItem;
-
 
21
import in.shop2020.model.v1.order.Order;
17
import in.shop2020.model.v1.order.OrderInfo;
22
import in.shop2020.model.v1.order.OrderInfo;
-
 
23
import in.shop2020.model.v1.order.OrderStatus;
18
import in.shop2020.model.v1.order.Payment;
24
import in.shop2020.model.v1.order.Payment;
19
import in.shop2020.model.v1.order.PaymentInfo;
25
import in.shop2020.model.v1.order.PaymentInfo;
20
import in.shop2020.model.v1.order.PaymentStatus;
26
import in.shop2020.model.v1.order.PaymentStatus;
21
import in.shop2020.model.v1.order.Shipment;
27
import in.shop2020.model.v1.order.Shipment;
22
import in.shop2020.model.v1.order.ShipmentInfo;
28
import in.shop2020.model.v1.order.ShipmentInfo;
Line 127... Line 133...
127
			// TODO Auto-generated catch block
133
			// TODO Auto-generated catch block
128
			e.printStackTrace();
134
			e.printStackTrace();
129
		}
135
		}
130
		return new DefaultHttpHeaders(result);
136
		return new DefaultHttpHeaders(result);
131
	}
137
	}
-
 
138
 
132
	
139
	
133
	private Transaction getTransaction(){
140
	private Transaction getTransaction(){
-
 
141
		try {
-
 
142
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
-
 
143
		} catch (NumberFormatException e1) {
-
 
144
			// TODO Auto-generated catch block
-
 
145
			e1.printStackTrace();
-
 
146
		} catch (PaymentException e1) {
-
 
147
			// TODO Auto-generated catch block
-
 
148
			e1.printStackTrace();
-
 
149
		} catch (TException e1) {
-
 
150
			// TODO Auto-generated catch block
-
 
151
			e1.printStackTrace();
-
 
152
		}
-
 
153
		Transaction t = new Transaction();
-
 
154
		PaymentInfo paymentInfo = new PaymentInfo();
-
 
155
		t.setPaymentInfo(paymentInfo);
-
 
156
		t.setShoppingCartid(pmnt.getCart_id());
-
 
157
		t.setCustomer_id(pmnt.getUser_id());
-
 
158
		t.setCreatedOn(pmnt.getBank_ack_timestamp());
-
 
159
		t.setTransactionStatus(TransactionStatus.INIT);
-
 
160
		t.setStatusDescription("New order");
-
 
161
		
-
 
162
		//create payment
-
 
163
		Payment payment = new Payment();
-
 
164
		payment.setAmount(Double.parseDouble(amount));
-
 
165
		payment.setBank_tx_id(tranId);
-
 
166
		payment.setMerchant_tx_id(trackId);
-
 
167
		payment.setStatus(PaymentStatus.SUCCESS);
-
 
168
		payment.setMode("credit card");
-
 
169
		payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());
-
 
170
		payment.setSubmissionTimestamp(pmnt.getInit_timestamp());
-
 
171
		
-
 
172
		Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();
-
 
173
		paymentMap.put(payment.getCompletionTimestamp(), payment);
-
 
174
		paymentInfo.setPayments(paymentMap);
-
 
175
		
-
 
176
		t.setOrders(getOrders());
-
 
177
		
-
 
178
		return t;
-
 
179
	}
-
 
180
 
-
 
181
private List<Order> getOrders(){
-
 
182
	List<Order> orders = new ArrayList<Order>();
-
 
183
	
-
 
184
	Cart c = null;
-
 
185
	try {
-
 
186
		c = cl.getClient().getCart(pmnt.getCart_id());
-
 
187
	} catch (ShoppingCartException e) {
-
 
188
		// TODO Auto-generated catch block
-
 
189
		e.printStackTrace();
-
 
190
	} catch (TException e) {
-
 
191
		// TODO Auto-generated catch block
-
 
192
		e.printStackTrace();
-
 
193
	} catch (Exception e) {
-
 
194
		// TODO Auto-generated catch block
-
 
195
		e.printStackTrace();
-
 
196
	}
-
 
197
	List<Line> lines = c.getLines();
-
 
198
	
-
 
199
	for(Line line : lines){
-
 
200
		if(line.getLineStatus() == LineStatus.LINE_ACTIVE){
-
 
201
			//line is active
-
 
202
			LineItem lineItem = getLineItem(line.getItemId());
-
 
203
			// create orders equivalent to quantity. Create one order per item.
-
 
204
			for(int i= 0; i<line.getQuantity(); i++){
-
 
205
				//  set order
-
 
206
				Order order = getOrder(c.getAddressId(), lineItem);
-
 
207
				order.addToLineitems(lineItem);
-
 
208
				orders.add(order);
-
 
209
			}
-
 
210
		}
-
 
211
	}
-
 
212
	return orders;
-
 
213
}
-
 
214
 
-
 
215
 
-
 
216
private Order getOrder(long addressId, LineItem lineItem){
-
 
217
	String sku_id = lineItem.getSku_id();
-
 
218
	double total_amount = lineItem.getTotal_price();
-
 
219
	double total_weight = lineItem.getTotal_weight();
-
 
220
	
-
 
221
	long customer_id = pmnt.getUser_id();
-
 
222
	Address address = getAddress(addressId);
-
 
223
	String customer_email = getEmail(customer_id);
-
 
224
	String customer_name = address.getName();
-
 
225
	String customer_address = getAddressString(address);
-
 
226
	String customer_mobilenumber = address.getPhone();
-
 
227
	String customer_pincode = address.getPin();
-
 
228
	
-
 
229
	
-
 
230
	
-
 
231
	// get logistics information
-
 
232
	LogisticsInfo logistics_info = null;
-
 
233
	try {
-
 
234
		logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);
-
 
235
	} catch (TException e) {
-
 
236
		// TODO Auto-generated catch block
-
 
237
		e.printStackTrace();
-
 
238
	} catch (LogisticsServiceException e) {
-
 
239
		// TODO Auto-generated catch block
-
 
240
		e.printStackTrace();
-
 
241
	}
-
 
242
 
-
 
243
	long warehouse_id = logistics_info.getWarehouse_id();
-
 
244
	long logistics_provider_id= logistics_info.getProvider_id();
-
 
245
	String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?
-
 
246
	String tracking_id = airwaybill_no; //right now both are same
-
 
247
	long expected_delivery_time = (new Date()).getTime() + 60*60*1000*logistics_info.getDelivery_estimate(); 
-
 
248
	
-
 
249
	long created_timestamp = (new Date()).getTime();
-
 
250
	OrderStatus status = OrderStatus.INIT; // to get from orderstatus file
-
 
251
	String statusDescription = "New Order";
-
 
252
	
-
 
253
	Order order = new Order();
-
 
254
	order.setLogistics_provider_id(logistics_provider_id);
-
 
255
	order.setAirwaybill_no(airwaybill_no);
-
 
256
	order.setExpected_delivery_time(expected_delivery_time);
-
 
257
	order.setTracking_id(tracking_id);
-
 
258
	
-
 
259
	order.setWarehouse_id(warehouse_id);
-
 
260
	order.setCreated_timestamp(created_timestamp);
-
 
261
	order.setStatus(status);
-
 
262
	order.setStatusDescription(statusDescription);
-
 
263
	
-
 
264
	order.setCustomer_id(customer_id);
-
 
265
	order.setCustomer_name(customer_name);
-
 
266
	order.setCustomer_address(customer_address);
-
 
267
	order.setCustomer_email(customer_email);
-
 
268
	order.setCustomer_mobilenumber(customer_mobilenumber);
-
 
269
	order.setCustomer_pincode(customer_pincode);
-
 
270
	
-
 
271
	order.setTotal_amount(total_amount);
-
 
272
	order.setTotal_weight(total_weight);
-
 
273
	
-
 
274
	return order;
-
 
275
}
-
 
276
 
-
 
277
private String getEmail(long userId){
-
 
278
	String email = null;
-
 
279
	try {
-
 
280
		email = usc.getClient().getPrimaryInfo(userId, false).getEmail();
-
 
281
	} catch (UserContextException e) {
-
 
282
		// TODO Auto-generated catch block
-
 
283
		e.printStackTrace();
-
 
284
	} catch (TException e) {
-
 
285
		// TODO Auto-generated catch block
-
 
286
		e.printStackTrace();
-
 
287
	}
-
 
288
	return email;
-
 
289
}
-
 
290
 
-
 
291
private Address getAddress(long addressId){
-
 
292
	Set<Address> addresses = null;
-
 
293
	try {
-
 
294
		addresses = usc.getClient().getPrimaryInfo(pmnt.getUser_id(), false).getAddresses();
-
 
295
	} catch (UserContextException e) {
-
 
296
		// TODO Auto-generated catch block
-
 
297
		e.printStackTrace();
-
 
298
	} catch (TException e) {
-
 
299
		// TODO Auto-generated catch block
-
 
300
		e.printStackTrace();
-
 
301
	}
-
 
302
	Address address = null;
-
 
303
	for(Address a : addresses){
-
 
304
		if(a.getId() == addressId){
-
 
305
			address = a;
-
 
306
		}
-
 
307
	}
-
 
308
	return address;
-
 
309
}
-
 
310
 
-
 
311
	private LineItem getLineItem(long catelogItemId){ 
-
 
312
	LineItem lineItem = new LineItem();
-
 
313
	Item item = null;
-
 
314
	try {
-
 
315
		item = csc.getClient().getItemByCatalogId(catelogItemId);
-
 
316
	} catch (InventoryServiceException e) {
-
 
317
		// TODO Auto-generated catch block
-
 
318
		e.printStackTrace();
-
 
319
	} catch (TException e) {
-
 
320
		// TODO Auto-generated catch block
-
 
321
		e.printStackTrace();
-
 
322
	}
-
 
323
 
-
 
324
	String model_name = item.getModelName();
-
 
325
	String model_number = item.getModelNumber();
-
 
326
	String brand = item.getManfucturerName();
-
 
327
	String sku_id = item.getVendorItemId();
-
 
328
	double unit_price = item.getSellingPrice();
-
 
329
	double unit_weight = item.getWeight();
-
 
330
	double total_price = item.getSellingPrice();
-
 
331
	double total_weight = item.getWeight();
-
 
332
	String  extra_info = item.getFeatureDescription();
-
 
333
	double quantity = 1; // because now we will create one order per item
-
 
334
	
-
 
335
	lineItem.setSku_id(sku_id);
-
 
336
	lineItem.setBrand(brand);
-
 
337
	lineItem.setExtra_info(extra_info);
-
 
338
	lineItem.setModel_name(model_name);
-
 
339
	lineItem.setModel_number(model_number);
-
 
340
	lineItem.setQuantity(quantity);
-
 
341
	lineItem.setSku_id(sku_id);
-
 
342
	lineItem.setUnit_price(unit_price);
-
 
343
	lineItem.setUnit_weight(unit_weight);
-
 
344
	lineItem.setTotal_price(total_price);
-
 
345
	lineItem.setTotal_weight(total_weight);
-
 
346
	return lineItem;
-
 
347
}
-
 
348
 
-
 
349
	private String getAddressString(Address a){
-
 
350
		String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();
-
 
351
		return add;
-
 
352
	}
-
 
353
	
-
 
354
	private String getAddress(Set<Address> address, long id){
-
 
355
		for(Address a : address){
-
 
356
			if(a.getId() == id){
-
 
357
				//Prepare String
-
 
358
				String add = a.getName()+",\n"+a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry()+",\n"+a.getPin()+",\n Phone :- "+a.getPhone();
-
 
359
				return add;
-
 
360
			}
-
 
361
		}
-
 
362
		return "";
-
 
363
	}
-
 
364
	
-
 
365
	public String getMessage(){
-
 
366
		return this.message;
-
 
367
	}
-
 
368
	
-
 
369
	public long getTransactionId(){
-
 
370
		return this.transaction_id;
-
 
371
	}
-
 
372
	
-
 
373
	
-
 
374
}
-
 
375
 
-
 
376
/*
-
 
377
	private Transaction getTransaction(){
134
		
378
		
135
		try {
379
		try {
136
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
380
			pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));
137
		} catch (NumberFormatException e1) {
381
		} catch (NumberFormatException e1) {
138
			// TODO Auto-generated catch block
382
			// TODO Auto-generated catch block
Line 246... Line 490...
246
			}
490
			}
247
		}
491
		}
248
		return t;
492
		return t;
249
		
493
		
250
	}
494
	}
251
	
-
 
252
	private String getAddress(Set<Address> address, long id){
-
 
253
		for(Address a : address){
-
 
254
			if(a.getId() == id){
-
 
255
				//Prepare String
-
 
256
				String add = a.getName()+",\n"+a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry()+",\n"+a.getPin()+",\n Phone :- "+a.getPhone();
-
 
257
				return add;
-
 
258
			}
-
 
259
		}
495
	*/
260
		return "";
-
 
261
	}
-
 
262
	
-
 
263
	private long getWarehouse(){
-
 
264
		return 1l;
-
 
265
	}
-
 
266
	
-
 
267
	public String getMessage(){
-
 
268
		return this.message;
-
 
269
	}
-
 
270
	
-
 
271
	public long getTransactionId(){
-
 
272
		return this.transaction_id;
-
 
273
	}
-
 
274
	
-
 
275
	
-
 
276
}
-