Subversion Repositories SmartDukaan

Rev

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