Subversion Repositories SmartDukaan

Rev

Rev 26002 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4207 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.support.controllers;
5
 
24033 amit.gupta 6
import java.text.SimpleDateFormat;
7
import java.util.ArrayList;
8
import java.util.Collections;
9
import java.util.Date;
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13
 
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.apache.thrift.TException;
17
 
18
import com.opensymphony.xwork2.ValidationAwareSupport;
19
 
22451 amit.gupta 20
import in.shop2020.logistics.PickUpType;
5945 mandeep.dh 21
import in.shop2020.model.v1.catalog.CatalogServiceException;
24033 amit.gupta 22
import in.shop2020.model.v1.catalog.HsItem;
4207 mandeep.dh 23
import in.shop2020.model.v1.catalog.Item;
24
import in.shop2020.model.v1.order.LineItem;
23684 amit.gupta 25
import in.shop2020.model.v1.order.Order;
7293 anupam.sin 26
import in.shop2020.model.v1.order.OrderSource;
5527 anupam.sin 27
import in.shop2020.model.v1.order.OrderType;
8925 amar.kumar 28
import in.shop2020.model.v1.order.ProductCondition;
4207 mandeep.dh 29
import in.shop2020.model.v1.order.Transaction;
30
import in.shop2020.model.v1.order.TransactionService.Client;
31
import in.shop2020.model.v1.order.TransactionServiceException;
32
import in.shop2020.model.v1.order.TransactionStatus;
24419 amit.gupta 33
import in.shop2020.model.v1.user.Address;
22451 amit.gupta 34
import in.shop2020.model.v1.user.ItemPriceQuantity;
23684 amit.gupta 35
import in.shop2020.model.v1.user.ShoppingCartException;
4207 mandeep.dh 36
import in.shop2020.model.v1.user.User;
37
import in.shop2020.payments.Attribute;
38
import in.shop2020.payments.Payment;
39
import in.shop2020.payments.PaymentException;
40
import in.shop2020.payments.PaymentStatus;
41
import in.shop2020.thrift.clients.CatalogClient;
42
import in.shop2020.thrift.clients.PaymentClient;
43
import in.shop2020.thrift.clients.TransactionClient;
44
import in.shop2020.thrift.clients.UserClient;
45
import in.shop2020.utils.ModelUtils;
46
 
47
/**
48
 * @author mandeep
49
 * 
50
 */
51
@SuppressWarnings("serial")
52
public class BulkOrderController extends ValidationAwareSupport {
22451 amit.gupta 53
	private static final int WALLET_GATEWAY_ID = 8;
4207 mandeep.dh 54
 
22451 amit.gupta 55
	private static Log log = LogFactory.getLog(BulkOrderController.class);
56
	private SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
4207 mandeep.dh 57
 
22451 amit.gupta 58
	private static final String PAY_METHOD = "payMethod";
4207 mandeep.dh 59
 
22451 amit.gupta 60
	private String id;
61
	private String transactionId;
62
	private String customerEmailId;
63
	private String rtgsId;
64
	private String rtgsBank;
65
	private String rtgsBranch;
66
	private String ifscCode;
67
	private String amount;
68
	private String itemId;
69
	private String output;
70
	private String pickUp;
71
	private String orderType;
72
	private String tinNumber;
73
	private String poRefNumber;
74
	private int productCondition;
75
	private Transaction transaction;
76
	private Payment payment;
77
	private long userWalletAmount;
78
	private int userId;
24419 amit.gupta 79
	private List<Address> userAddresses;
80
	private int addressId;
4207 mandeep.dh 81
 
22451 amit.gupta 82
	private Map<Transaction, Double> transactions;
83
	private List<LineItem> lineItems;
26002 amit.gupta 84
	private String secret; 
24033 amit.gupta 85
 
26002 amit.gupta 86
	public String getSecret() {
87
		return secret;
88
	}
89
 
90
	public void setSecret(String secret) {
91
		this.secret = secret;
92
	}
93
 
22452 amit.gupta 94
	private Client transactionClient;
95
	private in.shop2020.model.v1.user.UserContextService.Client userClient;
96
	private in.shop2020.payments.PaymentService.Client paymentClient;
97
	private in.shop2020.model.v1.catalog.CatalogService.Client catalogClient;
4207 mandeep.dh 98
 
22451 amit.gupta 99
	public String index() {
26002 amit.gupta 100
			return "index";
101
		} 
24033 amit.gupta 102
 
22451 amit.gupta 103
	public String editNew() throws Throwable {
26003 amit.gupta 104
		if(this.secret != null && this.secret.equals("shazam!")) {
105
			this.userClient = new UserClient().getClient();
106
			this.transactionClient = new TransactionClient().getClient();
107
			User user = userClient.getUserByEmail(customerEmailId);
108
			List<Address> addresses = this.userClient.getAllAddressesForUser(user.getUserId());
109
			this.setUserAddresses(addresses);
110
			this.userWalletAmount = this.transactionClient.getUserWallet(user.getUserId()).getAmount();
111
			return "editNew";
112
		}
113
		return "index1";
22451 amit.gupta 114
	}
4207 mandeep.dh 115
 
23571 amit.gupta 116
	public String show() throws Throwable {
117
		this.paymentClient = new PaymentClient().getClient();
118
		this.transactionClient = new TransactionClient().getClient();
22451 amit.gupta 119
		try {
120
			transactionId = id;
4207 mandeep.dh 121
 
22452 amit.gupta 122
			transaction = this.transactionClient.getTransaction(Long.valueOf(transactionId));
123
			payment = this.paymentClient.getPaymentForTxnId(Long.valueOf(transactionId)).get(0);
4207 mandeep.dh 124
 
22451 amit.gupta 125
		} catch (NumberFormatException e) {
126
			log.error("Could not lookup transaction Id: " + transactionId, e);
127
		} catch (TransactionServiceException e) {
128
			log.error("Could not lookup transaction Id: " + transactionId, e);
129
		} catch (TException e) {
130
			log.error("Could not lookup transaction Id: " + transactionId, e);
131
		} catch (PaymentException e) {
132
			log.error("Could not lookup payment for transaction Id: " + transactionId, e);
133
		}
4207 mandeep.dh 134
 
22451 amit.gupta 135
		return "show";
136
	}
24419 amit.gupta 137
 
138
 
139
 
4207 mandeep.dh 140
 
23571 amit.gupta 141
	public String create() throws Throwable {
142
		this.userClient = new UserClient().getClient();
143
		this.paymentClient = new PaymentClient().getClient();
144
		this.transactionClient = new TransactionClient().getClient();
145
		this.catalogClient = new CatalogClient().getClient();
24033 amit.gupta 146
 
22451 amit.gupta 147
		log.info("Creating orders for " + lineItems);
148
		boolean hasError = true;
4207 mandeep.dh 149
 
22451 amit.gupta 150
		try {
22452 amit.gupta 151
			User user = this.userClient.getUserByEmail(customerEmailId);
22451 amit.gupta 152
			List<ItemPriceQuantity> ipqList = new ArrayList<ItemPriceQuantity>();
153
			double totalAmount = 0;
4207 mandeep.dh 154
 
22451 amit.gupta 155
			if (user == null) {
156
				addActionError("Could not find user by email: " + customerEmailId);
157
			} else {
22452 amit.gupta 158
				double walletAmount = this.transactionClient.getUserWallet(user.getUserId()).getAmount();
22451 amit.gupta 159
				for (LineItem li : lineItems) {
160
					ItemPriceQuantity ipq = new ItemPriceQuantity();
161
					ipq.setQty((long) li.getQuantity());
22452 amit.gupta 162
					ipq.setItemId(li.getItem_id());
163
					log.info("Item ID ----------- " + li.getItem_id());
22451 amit.gupta 164
					ipq.setPrice(li.getUnit_price());
165
					ipqList.add(ipq);
22635 amit.gupta 166
					totalAmount += li.getUnit_price() * li.getQuantity();
22451 amit.gupta 167
				}
168
				if (totalAmount > walletAmount) {
169
					addActionError("Total Amount not be greater than wallet amount");
22452 amit.gupta 170
				} else if (!this.userClient.addItemPricingToCart(user.getActiveCartId(), ipqList)) {
22633 amit.gupta 171
					addActionError("Failed to add pricing to cart, please try again");
24419 amit.gupta 172
				} else if (this.addressId == 0 && user.getDefaultAddressId() == 0) {
22633 amit.gupta 173
					addActionError("Please make sure default address is set");
22451 amit.gupta 174
				} else {
175
					log.info("Setting wallet amount in cart");
22633 amit.gupta 176
					this.userClient.addAddressToCart(user.getActiveCartId(), user.getDefaultAddressId());
177
					this.userClient.setWalletAmountInCart(user.getActiveCartId(), totalAmount);
24228 amit.gupta 178
					transactionId = String.valueOf(this.userClient.createOrders(user.getActiveCartId(), "BACKEND", 0, "", 0,
24033 amit.gupta 179
							user.getUserId(), 7890, OrderSource.WEBSITE.getValue(), true));
22451 amit.gupta 180
					log.info("Creating wallet payment for transactionId - " + transactionId);
181
					createPayment(user);
4207 mandeep.dh 182
 
22451 amit.gupta 183
					in.shop2020.model.v1.order.Attribute orderAttribute1 = new in.shop2020.model.v1.order.Attribute();
22452 amit.gupta 184
					if (poRefNumber != null && !poRefNumber.equals("")) {
185
						orderAttribute1.setName("poRefNumber");
186
						orderAttribute1.setValue(poRefNumber);
24033 amit.gupta 187
						this.transactionClient.setOrderAttributeForTransaction(Long.parseLong(transactionId),
188
								orderAttribute1);
22451 amit.gupta 189
					}
24033 amit.gupta 190
					this.transactionClient.changeTransactionStatus(Long.valueOf(transactionId),
191
							TransactionStatus.AUTHORIZED, "Payment received for the order",
192
							PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);
193
					this.transactionClient.changeTransactionStatus(Long.valueOf(transactionId),
194
							TransactionStatus.IN_PROCESS, "Paid fully through wallet",
195
							PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);
22451 amit.gupta 196
					log.info("Successfully created transaction: " + transactionId + " for amount: " + amount);
23684 amit.gupta 197
					this.transactionClient = new TransactionClient().getClient();
24033 amit.gupta 198
					this.transactionClient
199
							.markOrderForRegisteredGstInvoice(Collections.singletonList(Long.valueOf(transactionId)));
23684 amit.gupta 200
					try {
201
						this.transactionClient.enqueueTransactionInfoEmail(Long.parseLong(transactionId));
202
					} catch (Exception e1) {
203
						log.error("Unable to update status of transaction. Thrift Exception:", e1);
204
					}
205
					this.userClient = new UserClient().getClient();
23709 amit.gupta 206
					transaction = this.transactionClient.getTransaction(Long.valueOf(transactionId));
23684 amit.gupta 207
					this.resetCart(transaction);
22451 amit.gupta 208
					hasError = false;
209
				}
210
			}
211
		} catch (Exception e) {
22452 amit.gupta 212
			addActionError("Error occerred - " + e.getMessage());
22451 amit.gupta 213
			e.printStackTrace();
214
		}
215
		if (hasError) {
216
			return "error";
217
		}
4207 mandeep.dh 218
 
22451 amit.gupta 219
		id = transactionId;
220
		return show();
221
	}
24033 amit.gupta 222
 
23684 amit.gupta 223
	private long resetCart(Transaction transaction) {
224
		long sum = 0;
225
		Map<Long, Double> items = new HashMap<Long, Double>();
226
		for (Order order : transaction.getOrders()) {
227
			sum += order.getGvAmount();
228
			for (LineItem lineitem : order.getLineitems()) {
229
				Long itemId = lineitem.getItem_id();
230
				Double quantity = items.get(itemId);
231
				if (quantity == null) {
232
					quantity = lineitem.getQuantity();
233
				} else {
234
					quantity = quantity + lineitem.getQuantity();
235
				}
236
				items.put(itemId, quantity);
237
			}
238
		}
4207 mandeep.dh 239
 
23684 amit.gupta 240
		log.debug("Items to reset in cart are: " + items);
241
 
242
		try {
243
			// TODO Optimize the function to send less data over the wire
23709 amit.gupta 244
			this.userClient.resetCart(transaction.getShoppingCartid(), items);
23684 amit.gupta 245
		} catch (TException e) {
246
			log.error("Error while updating information in payment database.", e);
247
		} catch (ShoppingCartException e) {
248
			log.error("Error while reseting the cart in cart database.", e);
249
		} catch (Exception e) {
250
			log.error("Unexpected exception", e);
251
		}
252
		return sum;
253
	}
254
 
23571 amit.gupta 255
	public String getModelName() throws Throwable {
256
		this.catalogClient = new CatalogClient().getClient();
22451 amit.gupta 257
		output = "Invalid Item Id: " + itemId;
4207 mandeep.dh 258
 
22451 amit.gupta 259
		try {
24033 amit.gupta 260
 
22452 amit.gupta 261
			Item item = this.catalogClient.getItem(Long.parseLong(itemId));
22451 amit.gupta 262
			if (item != null && item.getId() != 0) {
263
				output = ModelUtils.extractProductNameFromItem(item);
264
			}
265
		} catch (NumberFormatException e) {
266
			log.error("Could not parse itemId: " + itemId, e);
267
		} catch (CatalogServiceException e) {
268
			log.error("Could not lookup itemId: " + itemId, e);
269
		} catch (TException e) {
270
			log.error("Could not find itemId: " + itemId, e);
271
		}
4207 mandeep.dh 272
 
22451 amit.gupta 273
		return "output";
274
	}
24033 amit.gupta 275
 
276
	public String getPartnerPrice() throws Throwable {
277
		this.catalogClient = new CatalogClient().getClient();
278
		output = "Invalid Item Id: " + itemId;
279
		HsItem item = this.catalogClient.getHsItem(itemId);
280
		if (item != null && item.getItemId() != 0) {
281
			output = String.valueOf(item.getListingPrice());
282
		}
283
 
284
		return "output";
285
	}
286
 
22451 amit.gupta 287
	/**
24033 amit.gupta 288
	 * As it is always going to be wallet
22451 amit.gupta 289
	 **/
290
	private void createPayment(User user) throws NumberFormatException, PaymentException, TException {
291
		List<Attribute> paymentAttributes = new ArrayList<Attribute>();
292
		paymentAttributes.add(new Attribute(PAY_METHOD, "7890"));
24033 amit.gupta 293
		long paymentId = this.paymentClient.createPayment(user.getUserId(), Double.valueOf(amount), WALLET_GATEWAY_ID,
294
				Long.valueOf(transactionId), false);
295
		this.paymentClient.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null,
296
				PaymentStatus.SUCCESS, null, paymentAttributes);
22451 amit.gupta 297
	}
4207 mandeep.dh 298
 
22451 amit.gupta 299
	public String convertDate(long date) {
300
		return SDF.format(new Date(date));
301
	}
4207 mandeep.dh 302
 
22451 amit.gupta 303
	public String extractProductName(LineItem lineItem) {
304
		if (lineItem == null) {
305
			return "N/A";
306
		}
4207 mandeep.dh 307
 
22451 amit.gupta 308
		return ModelUtils.extractProductNameFromLineItem(lineItem);
309
	}
4207 mandeep.dh 310
 
22451 amit.gupta 311
	// Getters and Setters
312
	public String getCustomerEmailId() {
313
		return customerEmailId;
314
	}
4207 mandeep.dh 315
 
22451 amit.gupta 316
	public void setCustomerEmailId(String customerEmailId) {
317
		this.customerEmailId = customerEmailId;
318
	}
4207 mandeep.dh 319
 
22451 amit.gupta 320
	public String getRtgsId() {
321
		return rtgsId;
322
	}
4207 mandeep.dh 323
 
22451 amit.gupta 324
	public void setRtgsId(String rtgsId) {
325
		this.rtgsId = rtgsId;
326
	}
4207 mandeep.dh 327
 
22451 amit.gupta 328
	public String getRtgsBank() {
329
		return rtgsBank;
330
	}
4207 mandeep.dh 331
 
22451 amit.gupta 332
	public void setRtgsBank(String rtgsBank) {
333
		this.rtgsBank = rtgsBank;
334
	}
4207 mandeep.dh 335
 
22451 amit.gupta 336
	public String getRtgsBranch() {
337
		return rtgsBranch;
338
	}
4207 mandeep.dh 339
 
22451 amit.gupta 340
	public void setRtgsBranch(String rtgsBranch) {
341
		this.rtgsBranch = rtgsBranch;
342
	}
4207 mandeep.dh 343
 
22451 amit.gupta 344
	public String getIfscCode() {
345
		return ifscCode;
346
	}
4207 mandeep.dh 347
 
22451 amit.gupta 348
	public void setIfscCode(String ifscCode) {
349
		this.ifscCode = ifscCode;
350
	}
4207 mandeep.dh 351
 
22451 amit.gupta 352
	public String getAmount() {
353
		return amount;
354
	}
4207 mandeep.dh 355
 
22451 amit.gupta 356
	public void setAmount(String amount) {
357
		this.amount = amount;
358
	}
4207 mandeep.dh 359
 
22451 amit.gupta 360
	public List<LineItem> getLineItems() {
361
		return lineItems;
362
	}
4207 mandeep.dh 363
 
22451 amit.gupta 364
	public void setLineItems(List<LineItem> lineItems) {
365
		this.lineItems = lineItems;
366
	}
4207 mandeep.dh 367
 
22451 amit.gupta 368
	public Map<Transaction, Double> getTransactions() {
369
		return transactions;
370
	}
4207 mandeep.dh 371
 
22451 amit.gupta 372
	public void setTransactions(Map<Transaction, Double> transactions) {
373
		this.transactions = transactions;
374
	}
4207 mandeep.dh 375
 
22451 amit.gupta 376
	public String getTransactionId() {
377
		return transactionId;
378
	}
4207 mandeep.dh 379
 
22451 amit.gupta 380
	public void setTransactionId(String transactionId) {
381
		this.transactionId = transactionId;
382
	}
4207 mandeep.dh 383
 
22451 amit.gupta 384
	public Transaction getTransaction() {
385
		return transaction;
386
	}
4207 mandeep.dh 387
 
22451 amit.gupta 388
	public void setTransaction(Transaction transaction) {
389
		this.transaction = transaction;
390
	}
4207 mandeep.dh 391
 
22451 amit.gupta 392
	public Payment getPayment() {
393
		return payment;
394
	}
4207 mandeep.dh 395
 
22451 amit.gupta 396
	public void setPayment(Payment payment) {
397
		this.payment = payment;
398
	}
4207 mandeep.dh 399
 
22451 amit.gupta 400
	public String getId() {
401
		return id;
402
	}
4207 mandeep.dh 403
 
22451 amit.gupta 404
	public void setId(String id) {
405
		this.id = id;
406
	}
4207 mandeep.dh 407
 
22451 amit.gupta 408
	public String getItemId() {
409
		return itemId;
410
	}
4207 mandeep.dh 411
 
22451 amit.gupta 412
	public void setItemId(String itemId) {
413
		this.itemId = itemId;
414
	}
4207 mandeep.dh 415
 
22451 amit.gupta 416
	public String getOutput() {
417
		return output;
418
	}
4207 mandeep.dh 419
 
22451 amit.gupta 420
	public void setOutput(String output) {
421
		this.output = output;
422
	}
4207 mandeep.dh 423
 
22451 amit.gupta 424
	public String getPickUp() {
425
		return pickUp;
426
	}
4207 mandeep.dh 427
 
22451 amit.gupta 428
	public void setPickUp(String pickUp) {
429
		this.pickUp = pickUp;
430
	}
4207 mandeep.dh 431
 
22451 amit.gupta 432
	public String getOrderType() {
433
		return orderType;
434
	}
4207 mandeep.dh 435
 
22451 amit.gupta 436
	public void setOrderType(String orderType) {
437
		this.orderType = orderType;
438
	}
5387 rajveer 439
 
22451 amit.gupta 440
	public String getTinNumber() {
441
		return tinNumber;
442
	}
5387 rajveer 443
 
22451 amit.gupta 444
	public void setTinNumber(String tinNumber) {
445
		this.tinNumber = tinNumber;
446
	}
5527 anupam.sin 447
 
8925 amar.kumar 448
	public int getProductCondition() {
449
		return productCondition;
450
	}
451
 
452
	public void setProductCondition(int productCondition) {
453
		this.productCondition = productCondition;
454
	}
9416 amar.kumar 455
 
12653 manish.sha 456
	public String getPoRefNumber() {
457
		return poRefNumber;
458
	}
459
 
460
	public void setPoRefNumber(String poRefNumber) {
461
		this.poRefNumber = poRefNumber;
462
	}
463
 
464
	public static void main(String[] args) {
465
		BulkOrderController blc = new BulkOrderController();
466
		blc.setCustomerEmailId("neavins@amazon.com");
467
		blc.setRtgsId("rtgsId");
468
		blc.setRtgsBank("rtgsBank");
469
		blc.setRtgsBranch("rtgsBranch");
470
		blc.setIfscCode("ifscCode");
471
		blc.setAmount("3400");
472
		blc.setItemId("10661");
22451 amit.gupta 473
		// blc.setOutput();
12653 manish.sha 474
		blc.setPickUp("COURIER");
475
		blc.setOrderType("B2Cbulk");
22451 amit.gupta 476
		// blc.setTinNumber();
477
		blc.setProductCondition(ProductCondition.GOOD.getValue());
12653 manish.sha 478
		blc.index();
479
	}
22451 amit.gupta 480
 
481
	public double getWalletAmount() {
482
		return this.userWalletAmount;
483
	}
484
 
24419 amit.gupta 485
	public List<Address> getUserAddresses() {
486
		return userAddresses;
487
	}
488
 
489
	public void setUserAddresses(List<Address> userAddresses) {
490
		this.userAddresses = userAddresses;
491
	}
492
 
4207 mandeep.dh 493
}