Subversion Repositories SmartDukaan

Rev

Rev 23684 | Rev 24033 | 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();
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
	}
23684 amit.gupta 202
 
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 {
22452 amit.gupta 240
 
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
	}
255
	/**
256
	 As it is always going to be wallet
257
	 **/
258
	private void createPayment(User user) throws NumberFormatException, PaymentException, TException {
259
		List<Attribute> paymentAttributes = new ArrayList<Attribute>();
260
		paymentAttributes.add(new Attribute(PAY_METHOD, "7890"));
22452 amit.gupta 261
		long paymentId = this.paymentClient.createPayment(user.getUserId(), Double.valueOf(amount), WALLET_GATEWAY_ID, Long.valueOf(transactionId),
22451 amit.gupta 262
				false);
22452 amit.gupta 263
		this.paymentClient.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.SUCCESS, null,
22451 amit.gupta 264
				paymentAttributes);
265
	}
4207 mandeep.dh 266
 
22451 amit.gupta 267
	public String convertDate(long date) {
268
		return SDF.format(new Date(date));
269
	}
4207 mandeep.dh 270
 
22451 amit.gupta 271
	public String extractProductName(LineItem lineItem) {
272
		if (lineItem == null) {
273
			return "N/A";
274
		}
4207 mandeep.dh 275
 
22451 amit.gupta 276
		return ModelUtils.extractProductNameFromLineItem(lineItem);
277
	}
4207 mandeep.dh 278
 
22451 amit.gupta 279
	// Getters and Setters
280
	public String getCustomerEmailId() {
281
		return customerEmailId;
282
	}
4207 mandeep.dh 283
 
22451 amit.gupta 284
	public void setCustomerEmailId(String customerEmailId) {
285
		this.customerEmailId = customerEmailId;
286
	}
4207 mandeep.dh 287
 
22451 amit.gupta 288
	public String getRtgsId() {
289
		return rtgsId;
290
	}
4207 mandeep.dh 291
 
22451 amit.gupta 292
	public void setRtgsId(String rtgsId) {
293
		this.rtgsId = rtgsId;
294
	}
4207 mandeep.dh 295
 
22451 amit.gupta 296
	public String getRtgsBank() {
297
		return rtgsBank;
298
	}
4207 mandeep.dh 299
 
22451 amit.gupta 300
	public void setRtgsBank(String rtgsBank) {
301
		this.rtgsBank = rtgsBank;
302
	}
4207 mandeep.dh 303
 
22451 amit.gupta 304
	public String getRtgsBranch() {
305
		return rtgsBranch;
306
	}
4207 mandeep.dh 307
 
22451 amit.gupta 308
	public void setRtgsBranch(String rtgsBranch) {
309
		this.rtgsBranch = rtgsBranch;
310
	}
4207 mandeep.dh 311
 
22451 amit.gupta 312
	public String getIfscCode() {
313
		return ifscCode;
314
	}
4207 mandeep.dh 315
 
22451 amit.gupta 316
	public void setIfscCode(String ifscCode) {
317
		this.ifscCode = ifscCode;
318
	}
4207 mandeep.dh 319
 
22451 amit.gupta 320
	public String getAmount() {
321
		return amount;
322
	}
4207 mandeep.dh 323
 
22451 amit.gupta 324
	public void setAmount(String amount) {
325
		this.amount = amount;
326
	}
4207 mandeep.dh 327
 
22451 amit.gupta 328
	public List<LineItem> getLineItems() {
329
		return lineItems;
330
	}
4207 mandeep.dh 331
 
22451 amit.gupta 332
	public void setLineItems(List<LineItem> lineItems) {
333
		this.lineItems = lineItems;
334
	}
4207 mandeep.dh 335
 
22451 amit.gupta 336
	public Map<Transaction, Double> getTransactions() {
337
		return transactions;
338
	}
4207 mandeep.dh 339
 
22451 amit.gupta 340
	public void setTransactions(Map<Transaction, Double> transactions) {
341
		this.transactions = transactions;
342
	}
4207 mandeep.dh 343
 
22451 amit.gupta 344
	public String getTransactionId() {
345
		return transactionId;
346
	}
4207 mandeep.dh 347
 
22451 amit.gupta 348
	public void setTransactionId(String transactionId) {
349
		this.transactionId = transactionId;
350
	}
4207 mandeep.dh 351
 
22451 amit.gupta 352
	public Transaction getTransaction() {
353
		return transaction;
354
	}
4207 mandeep.dh 355
 
22451 amit.gupta 356
	public void setTransaction(Transaction transaction) {
357
		this.transaction = transaction;
358
	}
4207 mandeep.dh 359
 
22451 amit.gupta 360
	public Payment getPayment() {
361
		return payment;
362
	}
4207 mandeep.dh 363
 
22451 amit.gupta 364
	public void setPayment(Payment payment) {
365
		this.payment = payment;
366
	}
4207 mandeep.dh 367
 
22451 amit.gupta 368
	public String getId() {
369
		return id;
370
	}
4207 mandeep.dh 371
 
22451 amit.gupta 372
	public void setId(String id) {
373
		this.id = id;
374
	}
4207 mandeep.dh 375
 
22451 amit.gupta 376
	public String getItemId() {
377
		return itemId;
378
	}
4207 mandeep.dh 379
 
22451 amit.gupta 380
	public void setItemId(String itemId) {
381
		this.itemId = itemId;
382
	}
4207 mandeep.dh 383
 
22451 amit.gupta 384
	public String getOutput() {
385
		return output;
386
	}
4207 mandeep.dh 387
 
22451 amit.gupta 388
	public void setOutput(String output) {
389
		this.output = output;
390
	}
4207 mandeep.dh 391
 
22451 amit.gupta 392
	public String getPickUp() {
393
		return pickUp;
394
	}
4207 mandeep.dh 395
 
22451 amit.gupta 396
	public void setPickUp(String pickUp) {
397
		this.pickUp = pickUp;
398
	}
4207 mandeep.dh 399
 
22451 amit.gupta 400
	public String getOrderType() {
401
		return orderType;
402
	}
4207 mandeep.dh 403
 
22451 amit.gupta 404
	public void setOrderType(String orderType) {
405
		this.orderType = orderType;
406
	}
5387 rajveer 407
 
22451 amit.gupta 408
	public String getTinNumber() {
409
		return tinNumber;
410
	}
5387 rajveer 411
 
22451 amit.gupta 412
	public void setTinNumber(String tinNumber) {
413
		this.tinNumber = tinNumber;
414
	}
5527 anupam.sin 415
 
8925 amar.kumar 416
	public int getProductCondition() {
417
		return productCondition;
418
	}
419
 
420
	public void setProductCondition(int productCondition) {
421
		this.productCondition = productCondition;
422
	}
9416 amar.kumar 423
 
12653 manish.sha 424
	public String getPoRefNumber() {
425
		return poRefNumber;
426
	}
427
 
428
	public void setPoRefNumber(String poRefNumber) {
429
		this.poRefNumber = poRefNumber;
430
	}
431
 
432
	public static void main(String[] args) {
433
		BulkOrderController blc = new BulkOrderController();
434
		blc.setCustomerEmailId("neavins@amazon.com");
435
		blc.setRtgsId("rtgsId");
436
		blc.setRtgsBank("rtgsBank");
437
		blc.setRtgsBranch("rtgsBranch");
438
		blc.setIfscCode("ifscCode");
439
		blc.setAmount("3400");
440
		blc.setItemId("10661");
22451 amit.gupta 441
		// blc.setOutput();
12653 manish.sha 442
		blc.setPickUp("COURIER");
443
		blc.setOrderType("B2Cbulk");
22451 amit.gupta 444
		// blc.setTinNumber();
445
		blc.setProductCondition(ProductCondition.GOOD.getValue());
12653 manish.sha 446
		blc.index();
447
	}
22451 amit.gupta 448
 
449
	public double getWalletAmount() {
450
		return this.userWalletAmount;
451
	}
452
 
4207 mandeep.dh 453
}