Subversion Repositories SmartDukaan

Rev

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