Subversion Repositories SmartDukaan

Rev

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