Subversion Repositories SmartDukaan

Rev

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