Subversion Repositories SmartDukaan

Rev

Rev 26003 | 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());
26908 amit.gupta 164
					if(li.getUnit_price() == 0) {
165
						addActionError("Unit price should not be zero for any items");
166
					}
22451 amit.gupta 167
					ipq.setPrice(li.getUnit_price());
168
					ipqList.add(ipq);
22635 amit.gupta 169
					totalAmount += li.getUnit_price() * li.getQuantity();
22451 amit.gupta 170
				}
171
				if (totalAmount > walletAmount) {
172
					addActionError("Total Amount not be greater than wallet amount");
22452 amit.gupta 173
				} else if (!this.userClient.addItemPricingToCart(user.getActiveCartId(), ipqList)) {
22633 amit.gupta 174
					addActionError("Failed to add pricing to cart, please try again");
24419 amit.gupta 175
				} else if (this.addressId == 0 && user.getDefaultAddressId() == 0) {
22633 amit.gupta 176
					addActionError("Please make sure default address is set");
22451 amit.gupta 177
				} else {
178
					log.info("Setting wallet amount in cart");
22633 amit.gupta 179
					this.userClient.addAddressToCart(user.getActiveCartId(), user.getDefaultAddressId());
180
					this.userClient.setWalletAmountInCart(user.getActiveCartId(), totalAmount);
24228 amit.gupta 181
					transactionId = String.valueOf(this.userClient.createOrders(user.getActiveCartId(), "BACKEND", 0, "", 0,
24033 amit.gupta 182
							user.getUserId(), 7890, OrderSource.WEBSITE.getValue(), true));
22451 amit.gupta 183
					log.info("Creating wallet payment for transactionId - " + transactionId);
184
					createPayment(user);
4207 mandeep.dh 185
 
22451 amit.gupta 186
					in.shop2020.model.v1.order.Attribute orderAttribute1 = new in.shop2020.model.v1.order.Attribute();
22452 amit.gupta 187
					if (poRefNumber != null && !poRefNumber.equals("")) {
188
						orderAttribute1.setName("poRefNumber");
189
						orderAttribute1.setValue(poRefNumber);
24033 amit.gupta 190
						this.transactionClient.setOrderAttributeForTransaction(Long.parseLong(transactionId),
191
								orderAttribute1);
22451 amit.gupta 192
					}
24033 amit.gupta 193
					this.transactionClient.changeTransactionStatus(Long.valueOf(transactionId),
194
							TransactionStatus.AUTHORIZED, "Payment received for the order",
195
							PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);
196
					this.transactionClient.changeTransactionStatus(Long.valueOf(transactionId),
197
							TransactionStatus.IN_PROCESS, "Paid fully through wallet",
198
							PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);
22451 amit.gupta 199
					log.info("Successfully created transaction: " + transactionId + " for amount: " + amount);
23684 amit.gupta 200
					this.transactionClient = new TransactionClient().getClient();
24033 amit.gupta 201
					this.transactionClient
202
							.markOrderForRegisteredGstInvoice(Collections.singletonList(Long.valueOf(transactionId)));
23684 amit.gupta 203
					try {
204
						this.transactionClient.enqueueTransactionInfoEmail(Long.parseLong(transactionId));
205
					} catch (Exception e1) {
206
						log.error("Unable to update status of transaction. Thrift Exception:", e1);
207
					}
208
					this.userClient = new UserClient().getClient();
23709 amit.gupta 209
					transaction = this.transactionClient.getTransaction(Long.valueOf(transactionId));
23684 amit.gupta 210
					this.resetCart(transaction);
22451 amit.gupta 211
					hasError = false;
212
				}
213
			}
214
		} catch (Exception e) {
22452 amit.gupta 215
			addActionError("Error occerred - " + e.getMessage());
22451 amit.gupta 216
			e.printStackTrace();
217
		}
218
		if (hasError) {
219
			return "error";
220
		}
4207 mandeep.dh 221
 
22451 amit.gupta 222
		id = transactionId;
223
		return show();
224
	}
24033 amit.gupta 225
 
23684 amit.gupta 226
	private long resetCart(Transaction transaction) {
227
		long sum = 0;
228
		Map<Long, Double> items = new HashMap<Long, Double>();
229
		for (Order order : transaction.getOrders()) {
230
			sum += order.getGvAmount();
231
			for (LineItem lineitem : order.getLineitems()) {
232
				Long itemId = lineitem.getItem_id();
233
				Double quantity = items.get(itemId);
234
				if (quantity == null) {
235
					quantity = lineitem.getQuantity();
236
				} else {
237
					quantity = quantity + lineitem.getQuantity();
238
				}
239
				items.put(itemId, quantity);
240
			}
241
		}
4207 mandeep.dh 242
 
23684 amit.gupta 243
		log.debug("Items to reset in cart are: " + items);
244
 
245
		try {
246
			// TODO Optimize the function to send less data over the wire
23709 amit.gupta 247
			this.userClient.resetCart(transaction.getShoppingCartid(), items);
23684 amit.gupta 248
		} catch (TException e) {
249
			log.error("Error while updating information in payment database.", e);
250
		} catch (ShoppingCartException e) {
251
			log.error("Error while reseting the cart in cart database.", e);
252
		} catch (Exception e) {
253
			log.error("Unexpected exception", e);
254
		}
255
		return sum;
256
	}
257
 
23571 amit.gupta 258
	public String getModelName() throws Throwable {
259
		this.catalogClient = new CatalogClient().getClient();
22451 amit.gupta 260
		output = "Invalid Item Id: " + itemId;
4207 mandeep.dh 261
 
22451 amit.gupta 262
		try {
24033 amit.gupta 263
 
22452 amit.gupta 264
			Item item = this.catalogClient.getItem(Long.parseLong(itemId));
22451 amit.gupta 265
			if (item != null && item.getId() != 0) {
266
				output = ModelUtils.extractProductNameFromItem(item);
267
			}
268
		} catch (NumberFormatException e) {
269
			log.error("Could not parse itemId: " + itemId, e);
270
		} catch (CatalogServiceException e) {
271
			log.error("Could not lookup itemId: " + itemId, e);
272
		} catch (TException e) {
273
			log.error("Could not find itemId: " + itemId, e);
274
		}
4207 mandeep.dh 275
 
22451 amit.gupta 276
		return "output";
277
	}
24033 amit.gupta 278
 
279
	public String getPartnerPrice() throws Throwable {
280
		this.catalogClient = new CatalogClient().getClient();
281
		output = "Invalid Item Id: " + itemId;
282
		HsItem item = this.catalogClient.getHsItem(itemId);
283
		if (item != null && item.getItemId() != 0) {
284
			output = String.valueOf(item.getListingPrice());
285
		}
286
 
287
		return "output";
288
	}
289
 
22451 amit.gupta 290
	/**
24033 amit.gupta 291
	 * As it is always going to be wallet
22451 amit.gupta 292
	 **/
293
	private void createPayment(User user) throws NumberFormatException, PaymentException, TException {
294
		List<Attribute> paymentAttributes = new ArrayList<Attribute>();
295
		paymentAttributes.add(new Attribute(PAY_METHOD, "7890"));
24033 amit.gupta 296
		long paymentId = this.paymentClient.createPayment(user.getUserId(), Double.valueOf(amount), WALLET_GATEWAY_ID,
297
				Long.valueOf(transactionId), false);
298
		this.paymentClient.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null,
299
				PaymentStatus.SUCCESS, null, paymentAttributes);
22451 amit.gupta 300
	}
4207 mandeep.dh 301
 
22451 amit.gupta 302
	public String convertDate(long date) {
303
		return SDF.format(new Date(date));
304
	}
4207 mandeep.dh 305
 
22451 amit.gupta 306
	public String extractProductName(LineItem lineItem) {
307
		if (lineItem == null) {
308
			return "N/A";
309
		}
4207 mandeep.dh 310
 
22451 amit.gupta 311
		return ModelUtils.extractProductNameFromLineItem(lineItem);
312
	}
4207 mandeep.dh 313
 
22451 amit.gupta 314
	// Getters and Setters
315
	public String getCustomerEmailId() {
316
		return customerEmailId;
317
	}
4207 mandeep.dh 318
 
22451 amit.gupta 319
	public void setCustomerEmailId(String customerEmailId) {
320
		this.customerEmailId = customerEmailId;
321
	}
4207 mandeep.dh 322
 
22451 amit.gupta 323
	public String getRtgsId() {
324
		return rtgsId;
325
	}
4207 mandeep.dh 326
 
22451 amit.gupta 327
	public void setRtgsId(String rtgsId) {
328
		this.rtgsId = rtgsId;
329
	}
4207 mandeep.dh 330
 
22451 amit.gupta 331
	public String getRtgsBank() {
332
		return rtgsBank;
333
	}
4207 mandeep.dh 334
 
22451 amit.gupta 335
	public void setRtgsBank(String rtgsBank) {
336
		this.rtgsBank = rtgsBank;
337
	}
4207 mandeep.dh 338
 
22451 amit.gupta 339
	public String getRtgsBranch() {
340
		return rtgsBranch;
341
	}
4207 mandeep.dh 342
 
22451 amit.gupta 343
	public void setRtgsBranch(String rtgsBranch) {
344
		this.rtgsBranch = rtgsBranch;
345
	}
4207 mandeep.dh 346
 
22451 amit.gupta 347
	public String getIfscCode() {
348
		return ifscCode;
349
	}
4207 mandeep.dh 350
 
22451 amit.gupta 351
	public void setIfscCode(String ifscCode) {
352
		this.ifscCode = ifscCode;
353
	}
4207 mandeep.dh 354
 
22451 amit.gupta 355
	public String getAmount() {
356
		return amount;
357
	}
4207 mandeep.dh 358
 
22451 amit.gupta 359
	public void setAmount(String amount) {
360
		this.amount = amount;
361
	}
4207 mandeep.dh 362
 
22451 amit.gupta 363
	public List<LineItem> getLineItems() {
364
		return lineItems;
365
	}
4207 mandeep.dh 366
 
22451 amit.gupta 367
	public void setLineItems(List<LineItem> lineItems) {
368
		this.lineItems = lineItems;
369
	}
4207 mandeep.dh 370
 
22451 amit.gupta 371
	public Map<Transaction, Double> getTransactions() {
372
		return transactions;
373
	}
4207 mandeep.dh 374
 
22451 amit.gupta 375
	public void setTransactions(Map<Transaction, Double> transactions) {
376
		this.transactions = transactions;
377
	}
4207 mandeep.dh 378
 
22451 amit.gupta 379
	public String getTransactionId() {
380
		return transactionId;
381
	}
4207 mandeep.dh 382
 
22451 amit.gupta 383
	public void setTransactionId(String transactionId) {
384
		this.transactionId = transactionId;
385
	}
4207 mandeep.dh 386
 
22451 amit.gupta 387
	public Transaction getTransaction() {
388
		return transaction;
389
	}
4207 mandeep.dh 390
 
22451 amit.gupta 391
	public void setTransaction(Transaction transaction) {
392
		this.transaction = transaction;
393
	}
4207 mandeep.dh 394
 
22451 amit.gupta 395
	public Payment getPayment() {
396
		return payment;
397
	}
4207 mandeep.dh 398
 
22451 amit.gupta 399
	public void setPayment(Payment payment) {
400
		this.payment = payment;
401
	}
4207 mandeep.dh 402
 
22451 amit.gupta 403
	public String getId() {
404
		return id;
405
	}
4207 mandeep.dh 406
 
22451 amit.gupta 407
	public void setId(String id) {
408
		this.id = id;
409
	}
4207 mandeep.dh 410
 
22451 amit.gupta 411
	public String getItemId() {
412
		return itemId;
413
	}
4207 mandeep.dh 414
 
22451 amit.gupta 415
	public void setItemId(String itemId) {
416
		this.itemId = itemId;
417
	}
4207 mandeep.dh 418
 
22451 amit.gupta 419
	public String getOutput() {
420
		return output;
421
	}
4207 mandeep.dh 422
 
22451 amit.gupta 423
	public void setOutput(String output) {
424
		this.output = output;
425
	}
4207 mandeep.dh 426
 
22451 amit.gupta 427
	public String getPickUp() {
428
		return pickUp;
429
	}
4207 mandeep.dh 430
 
22451 amit.gupta 431
	public void setPickUp(String pickUp) {
432
		this.pickUp = pickUp;
433
	}
4207 mandeep.dh 434
 
22451 amit.gupta 435
	public String getOrderType() {
436
		return orderType;
437
	}
4207 mandeep.dh 438
 
22451 amit.gupta 439
	public void setOrderType(String orderType) {
440
		this.orderType = orderType;
441
	}
5387 rajveer 442
 
22451 amit.gupta 443
	public String getTinNumber() {
444
		return tinNumber;
445
	}
5387 rajveer 446
 
22451 amit.gupta 447
	public void setTinNumber(String tinNumber) {
448
		this.tinNumber = tinNumber;
449
	}
5527 anupam.sin 450
 
8925 amar.kumar 451
	public int getProductCondition() {
452
		return productCondition;
453
	}
454
 
455
	public void setProductCondition(int productCondition) {
456
		this.productCondition = productCondition;
457
	}
9416 amar.kumar 458
 
12653 manish.sha 459
	public String getPoRefNumber() {
460
		return poRefNumber;
461
	}
462
 
463
	public void setPoRefNumber(String poRefNumber) {
464
		this.poRefNumber = poRefNumber;
465
	}
466
 
467
	public static void main(String[] args) {
468
		BulkOrderController blc = new BulkOrderController();
469
		blc.setCustomerEmailId("neavins@amazon.com");
470
		blc.setRtgsId("rtgsId");
471
		blc.setRtgsBank("rtgsBank");
472
		blc.setRtgsBranch("rtgsBranch");
473
		blc.setIfscCode("ifscCode");
474
		blc.setAmount("3400");
475
		blc.setItemId("10661");
22451 amit.gupta 476
		// blc.setOutput();
12653 manish.sha 477
		blc.setPickUp("COURIER");
478
		blc.setOrderType("B2Cbulk");
22451 amit.gupta 479
		// blc.setTinNumber();
480
		blc.setProductCondition(ProductCondition.GOOD.getValue());
12653 manish.sha 481
		blc.index();
482
	}
22451 amit.gupta 483
 
484
	public double getWalletAmount() {
485
		return this.userWalletAmount;
486
	}
487
 
24419 amit.gupta 488
	public List<Address> getUserAddresses() {
489
		return userAddresses;
490
	}
491
 
492
	public void setUserAddresses(List<Address> userAddresses) {
493
		this.userAddresses = userAddresses;
494
	}
495
 
4207 mandeep.dh 496
}