Subversion Repositories SmartDukaan

Rev

Rev 9416 | Rev 22451 | 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
 
5945 mandeep.dh 6
import in.shop2020.model.v1.catalog.CatalogServiceException;
4207 mandeep.dh 7
import in.shop2020.model.v1.catalog.Item;
8
import in.shop2020.model.v1.order.LineItem;
9
import in.shop2020.model.v1.order.Order;
7293 anupam.sin 10
import in.shop2020.model.v1.order.OrderSource;
4207 mandeep.dh 11
import in.shop2020.model.v1.order.OrderStatus;
5527 anupam.sin 12
import in.shop2020.model.v1.order.OrderType;
8925 amar.kumar 13
import in.shop2020.model.v1.order.ProductCondition;
9416 amar.kumar 14
import in.shop2020.model.v1.order.TaxType;
4207 mandeep.dh 15
import in.shop2020.model.v1.order.Transaction;
16
import in.shop2020.model.v1.order.TransactionService.Client;
17
import in.shop2020.model.v1.order.TransactionServiceException;
18
import in.shop2020.model.v1.order.TransactionStatus;
5527 anupam.sin 19
//import in.shop2020.model.v1.order.Attribute;
20
import in.shop2020.logistics.PickUpType;
4207 mandeep.dh 21
import in.shop2020.model.v1.user.Address;
22
import in.shop2020.model.v1.user.User;
23
import in.shop2020.model.v1.user.UserContextException;
24
import in.shop2020.payments.Attribute;
25
import in.shop2020.payments.Payment;
26
import in.shop2020.payments.PaymentException;
27
import in.shop2020.payments.PaymentStatus;
28
import in.shop2020.thrift.clients.CatalogClient;
29
import in.shop2020.thrift.clients.PaymentClient;
30
import in.shop2020.thrift.clients.TransactionClient;
31
import in.shop2020.thrift.clients.UserClient;
32
import in.shop2020.utils.ModelUtils;
33
 
34
import java.text.SimpleDateFormat;
35
import java.util.ArrayList;
36
import java.util.Collections;
37
import java.util.Date;
38
import java.util.HashMap;
39
import java.util.List;
40
import java.util.Map;
41
 
42
import org.apache.commons.logging.Log;
43
import org.apache.commons.logging.LogFactory;
44
import org.apache.thrift.TException;
45
import org.apache.thrift.transport.TTransportException;
46
 
47
import com.opensymphony.xwork2.ValidationAwareSupport;
48
 
49
/**
50
 * @author mandeep
51
 * 
52
 */
53
@SuppressWarnings("serial")
54
public class BulkOrderController extends ValidationAwareSupport {
55
    private static final int RTGS_GATEWAY_ID = 6;
56
 
57
    private static Log log = LogFactory.getLog(BulkOrderController.class);
58
    private SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
59
 
60
    private static final String IFSC_CODE   = "ifscCode";
61
    private static final String RTGS_BRANCH = "rtgsBranch";
62
    private static final String RTGS_BANK   = "rtgsBank";
63
    private static final String RTGS_ID     = "rtgsId";
64
    private static final String PAY_METHOD  = "payMethod";
65
 
66
    private String id;
67
    private String transactionId;
68
    private String customerEmailId;
69
    private String rtgsId;
70
    private String rtgsBank;
71
    private String rtgsBranch;
72
    private String ifscCode;
73
    private String amount;
74
    private String itemId;
75
    private String output;
5527 anupam.sin 76
    private String pickUp;
77
    private String orderType;
78
    private String tinNumber;
12653 manish.sha 79
    private String poRefNumber;
8925 amar.kumar 80
    private int productCondition;
9416 amar.kumar 81
    private int taxType;
4207 mandeep.dh 82
    private Transaction transaction;
83
    private Payment payment;
84
 
85
    private Map<Transaction, Double> transactions;
86
    private List<LineItem> lineItems;
87
 
88
    public String index() {
89
        transactions = new HashMap<Transaction, Double>();
90
 
91
        try {
92
            Client transactionClient = new TransactionClient().getClient();
93
            in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
94
            List<Payment> payments = paymentClient.getPayments(0, new Date().getTime(), PaymentStatus.SUCCESS, RTGS_GATEWAY_ID);
95
 
96
            log.info("Loaded " + payments.size() + " payments");
97
            for (Payment payment : payments) {
98
                Transaction txn = transactionClient.getTransaction(payment.getMerchantTxnId());
99
                log.info("Got " + txn);
100
                transactions.put(txn, payment.getAmount());
101
            }
102
        } catch (TTransportException e) {
103
            log.error("Could not create transaction client", e);
104
        } catch (TransactionServiceException e) {
105
            log.error("Could not lookup transaction", e);
106
        } catch (TException e) {
107
            log.error("Could not find payment", e);
108
        } catch (PaymentException e) {
109
            log.error("Could not find payment", e);
110
        }
111
 
112
        return "index";
113
    }
114
 
115
    public String editNew() {
116
        return "editNew";
117
    }
118
 
119
    public String show() {
120
        try {
121
            transactionId = id;
122
 
123
            Client transactionClient = new TransactionClient().getClient();
124
            transaction = transactionClient.getTransaction(Long.valueOf(transactionId));
125
 
126
            in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
127
            payment = paymentClient.getPaymentForTxnId(Long.valueOf(transactionId)).get(0);
128
 
129
            for (Attribute attribute : payment.getAttributes()) {
130
                if (RTGS_ID.equals(attribute.getName())) {
131
                    rtgsId = attribute.getValue();
132
                }
133
                else if (RTGS_BANK.equals(attribute.getName())) {
134
                    rtgsBank = attribute.getValue();
135
                }
136
                else if (RTGS_BRANCH.equals(attribute.getName())) {
137
                    rtgsBranch = attribute.getValue();
138
                }
139
                else if (IFSC_CODE.equals(attribute.getName())) {
140
                    ifscCode = attribute.getValue();
141
                }
142
            }
143
        } catch (NumberFormatException e) {
144
            log.error("Could not lookup transaction Id: " + transactionId, e);
145
        } catch (TransactionServiceException e) {
146
            log.error("Could not lookup transaction Id: " + transactionId, e);
147
        } catch (TException e) {
148
            log.error("Could not lookup transaction Id: " + transactionId, e);
149
        } catch (PaymentException e) {
150
            log.error("Could not lookup payment for transaction Id: " + transactionId, e);
151
        }
152
 
153
        return "show";
154
    }
155
 
156
    public String create() {
157
        log.info("Creating orders for " + lineItems);
158
 
159
        boolean hasError = false;
160
 
161
        try {
162
            hasError = validate();
163
 
164
            in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
165
            User user = userClient.getUserByEmail(customerEmailId);
166
 
167
            if (user == null) {
168
                addActionError("Could not find user by email: " + customerEmailId);
169
                hasError = true;
170
            }
171
            else {
5527 anupam.sin 172
                /*
5387 rajveer 173
            	boolean selfPickupFlag = false;
174
            	if(selfPickup.equals("on")){
175
            		selfPickupFlag = true;
176
            	}
5527 anupam.sin 177
            	*/
4207 mandeep.dh 178
                transactionId = String.valueOf(createTransaction(user));
179
                createPayment(user);
5527 anupam.sin 180
 
181
                in.shop2020.model.v1.order.Attribute orderAttribute = new in.shop2020.model.v1.order.Attribute();
182
                orderAttribute.setName("tinNumber");
183
                orderAttribute.setValue(tinNumber);
12653 manish.sha 184
 
185
                in.shop2020.model.v1.order.Attribute orderAttribute1 = new in.shop2020.model.v1.order.Attribute();
186
                orderAttribute1.setName("poRefNumber");
187
                orderAttribute1.setValue(poRefNumber);
4207 mandeep.dh 188
 
189
                Client transactionClient = new TransactionClient().getClient();
5527 anupam.sin 190
                if(!tinNumber.equals("") && !(tinNumber == null)) {
191
                    transactionClient.setOrderAttributeForTransaction(Long.parseLong(transactionId), orderAttribute);
192
                }
12653 manish.sha 193
                if(!poRefNumber.equals("") && !(poRefNumber == null)) {
194
                    transactionClient.setOrderAttributeForTransaction(Long.parseLong(transactionId), orderAttribute1);
195
                }
7293 anupam.sin 196
                transactionClient.changeTransactionStatus(Long.valueOf(transactionId), TransactionStatus.AUTHORIZED, "Payment received for the order", PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);
197
                transactionClient.changeTransactionStatus(Long.valueOf(transactionId), TransactionStatus.IN_PROCESS, "RTGS Payment accepted", PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);
4207 mandeep.dh 198
                log.info("Successfully created transaction: " + transactionId + " for amount: " + amount);
199
            }
200
        } catch (UserContextException e) {
201
            addActionError("Error while finding customer: " + customerEmailId);
202
            log.error("Could not fetch user for email Id: " + customerEmailId, e);
203
        } catch (TException e) {
204
            addActionError("Error while finding customer: " + customerEmailId);
205
            log.error("Could not fetch user for email Id: " + customerEmailId, e);
206
        } catch (TransactionServiceException e) {
207
            addActionError("Error creating transaction");
208
            log.error("Could not create transction Id", e);
5945 mandeep.dh 209
        } catch (CatalogServiceException e) {
4207 mandeep.dh 210
            addActionError("Error updating inventory");
211
            log.error("Error in CatalogService", e);
212
        } catch (NumberFormatException e) {
213
            addActionError("Error parsing transaction Id: " + transactionId);
214
            log.error("Error parsing transaction Id: " + transactionId, e);
215
        } catch (PaymentException e) {
216
            addActionError("Error creating payment");
217
            log.error("Could not create payment", e);
218
        }
219
 
220
        if (hasError) {
221
            return "error";
222
        }
223
 
224
        id = transactionId;
225
        return show();
226
    }
227
 
228
    public String getModelName() {
229
        output = "Invalid Item Id: " + itemId;
230
 
231
        try {
5945 mandeep.dh 232
            in.shop2020.model.v1.catalog.CatalogService.Client client = new CatalogClient().getClient();
4207 mandeep.dh 233
            Item item = client.getItem(Long.parseLong(itemId));
234
            if (item != null && item.getId() != 0) {
235
                output = ModelUtils.extractProductNameFromItem(item);
236
            }
237
        } catch (NumberFormatException e) {
238
            log.error("Could not parse itemId: " + itemId, e);
5945 mandeep.dh 239
        } catch (CatalogServiceException e) {
4207 mandeep.dh 240
            log.error("Could not lookup itemId: " + itemId, e);
241
        } catch (TException e) {
242
            log.error("Could not find itemId: " + itemId, e);
243
        }
244
 
245
        return "output";
246
    }
247
 
248
    private void createPayment(User user) throws NumberFormatException, PaymentException, TException {
249
        List<Attribute> paymentAttributes = new ArrayList<Attribute>();
250
        paymentAttributes.add(new Attribute(PAY_METHOD, "4000"));
251
        paymentAttributes.add(new Attribute(RTGS_ID, rtgsId));
252
        paymentAttributes.add(new Attribute(RTGS_BANK, rtgsBank));
253
        paymentAttributes.add(new Attribute(RTGS_BRANCH, rtgsBranch));
254
        paymentAttributes.add(new Attribute(IFSC_CODE, ifscCode));
255
 
256
        in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
6051 amit.gupta 257
        long paymentId = client.createPayment(user.getUserId(), Double.valueOf(amount), RTGS_GATEWAY_ID, Long.valueOf(transactionId), false);
4207 mandeep.dh 258
        client.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.SUCCESS, null, paymentAttributes);
259
    }
260
 
5945 mandeep.dh 261
    private long createTransaction(User user) throws TransactionServiceException, TException, CatalogServiceException {
4207 mandeep.dh 262
        Transaction txn = new Transaction();
263
        txn.setShoppingCartid(user.getActiveCartId());
264
        txn.setCustomer_id(user.getUserId());
265
        txn.setCreatedOn(new Date().getTime());
266
        txn.setTransactionStatus(TransactionStatus.INIT);
267
        txn.setStatusDescription("New Order");
268
        txn.setOrders(createOrders(user));
269
        Client transaction_client = new TransactionClient().getClient();
270
        return transaction_client.createTransaction(txn);
271
    }
272
 
5945 mandeep.dh 273
    private List<Order> createOrders(User user) throws CatalogServiceException, TException {
4207 mandeep.dh 274
        List<Order> orders = new ArrayList<Order>();
275
 
276
        // Extracting default address
277
        Address defaultAddress = null;
278
        for (Address address : user.getAddresses()) {
279
            if (address.getId() == user.getDefaultAddressId()) {
280
                defaultAddress = address;
281
                break;
282
            }
283
        }
284
 
285
        for (LineItem lineItem : lineItems) {
286
            enrichLineItem(lineItem);
287
 
288
            Order t_order = new Order();
289
            t_order.setCustomer_id(user.getUserId());
290
            t_order.setCustomer_email(user.getEmail());
291
            t_order.setCustomer_name(defaultAddress.getName());
292
            t_order.setCustomer_pincode(defaultAddress.getPin());
293
            t_order.setCustomer_address1(defaultAddress.getLine1());
294
            t_order.setCustomer_address2(defaultAddress.getLine2());
295
            t_order.setCustomer_city(defaultAddress.getCity());
296
            t_order.setCustomer_state(defaultAddress.getState());
297
            t_order.setCustomer_mobilenumber(defaultAddress.getPhone());
298
            t_order.setTotal_amount(lineItem.getTotal_price());            
299
            t_order.setTotal_weight(lineItem.getTotal_weight());
300
            t_order.setLineitems(Collections.singletonList(lineItem));            
301
            t_order.setStatus(OrderStatus.PAYMENT_PENDING);
302
            t_order.setStatusDescription("Payment Pending");
303
            t_order.setCreated_timestamp(new Date().getTime());
7623 anupam.sin 304
            t_order.setSource(1);//Source : Website
8925 amar.kumar 305
            t_order.setProductCondition(ProductCondition.findByValue(productCondition));
9416 amar.kumar 306
            t_order.setTaxType(TaxType.findByValue(taxType));
8925 amar.kumar 307
            //t_order.setProductCondition(ProductCondition.findByValue(productCondition.intValue()));
4207 mandeep.dh 308
 
309
            orders.add(t_order);
310
        }
311
 
312
        return orders;
313
    }
314
 
315
    private LineItem enrichLineItem(LineItem lineItem)
5945 mandeep.dh 316
            throws CatalogServiceException, TException {
317
        in.shop2020.model.v1.catalog.CatalogService.Client client = new CatalogClient().getClient();
4207 mandeep.dh 318
 
319
        Item item = client.getItem(lineItem.getItem_id());
320
 
321
        lineItem.setProductGroup(item.getProductGroup());
322
        lineItem.setBrand(item.getBrand());
323
        lineItem.setModel_number(item.getModelNumber());
324
        lineItem.setModel_name(item.getModelName());
325
        lineItem.setExtra_info(item.getFeatureDescription());
326
        lineItem.setItem_id(item.getId());
327
        lineItem.setUnit_weight(item.getWeight());
328
        lineItem.setTotal_weight(item.getWeight() * lineItem.getQuantity());
329
        lineItem.setDealText(item.getBestDealText());
330
        lineItem.setTotal_price(lineItem.getUnit_price() * lineItem.getQuantity());
331
 
332
        if (item.getColor() == null || "NA".equals(item.getColor())) {
333
            lineItem.setColor("");
334
        } else {
335
            lineItem.setColor(item.getColor());
336
        }
337
 
338
        return lineItem;
339
    }
340
 
341
    private boolean validate() {
342
        return false;
343
    }
344
 
345
    public String convertDate(long date) {
346
        return SDF.format(new Date(date));
347
    }
348
 
349
    public String extractProductName(LineItem lineItem) {
350
        if (lineItem == null) {
351
            return "N/A";
352
        }
353
 
354
        return ModelUtils.extractProductNameFromLineItem(lineItem);
355
    }
356
 
357
    // Getters and Setters
358
    public String getCustomerEmailId() {
359
        return customerEmailId;
360
    }
361
 
362
    public void setCustomerEmailId(String customerEmailId) {
363
        this.customerEmailId = customerEmailId;
364
    }
365
 
366
    public String getRtgsId() {
367
        return rtgsId;
368
    }
369
 
370
    public void setRtgsId(String rtgsId) {
371
        this.rtgsId = rtgsId;
372
    }
373
 
374
    public String getRtgsBank() {
375
        return rtgsBank;
376
    }
377
 
378
    public void setRtgsBank(String rtgsBank) {
379
        this.rtgsBank = rtgsBank;
380
    }
381
 
382
    public String getRtgsBranch() {
383
        return rtgsBranch;
384
    }
385
 
386
    public void setRtgsBranch(String rtgsBranch) {
387
        this.rtgsBranch = rtgsBranch;
388
    }
389
 
390
    public String getIfscCode() {
391
        return ifscCode;
392
    }
393
 
394
    public void setIfscCode(String ifscCode) {
395
        this.ifscCode = ifscCode;
396
    }
397
 
398
    public String getAmount() {
399
        return amount;
400
    }
401
 
402
    public void setAmount(String amount) {
403
        this.amount = amount;
404
    }
405
 
406
    public List<LineItem> getLineItems() {
407
        return lineItems;
408
    }
409
 
410
    public void setLineItems(List<LineItem> lineItems) {
411
        this.lineItems = lineItems;
412
    }
413
 
414
    public Map<Transaction, Double> getTransactions() {
415
        return transactions;
416
    }
417
 
418
    public void setTransactions(Map<Transaction, Double> transactions) {
419
        this.transactions = transactions;
420
    }
421
 
422
    public String getTransactionId() {
423
        return transactionId;
424
    }
425
 
426
    public void setTransactionId(String transactionId) {
427
        this.transactionId = transactionId;
428
    }
429
 
430
    public Transaction getTransaction() {
431
        return transaction;
432
    }
433
 
434
    public void setTransaction(Transaction transaction) {
435
        this.transaction = transaction;
436
    }
437
 
438
    public Payment getPayment() {
439
        return payment;
440
    }
441
 
442
    public void setPayment(Payment payment) {
443
        this.payment = payment;
444
    }
445
 
446
    public String getId() {
447
        return id;
448
    }
449
 
450
    public void setId(String id) {
451
        this.id = id;
452
    }
453
 
454
    public String getItemId() {
455
        return itemId;
456
    }
457
 
458
    public void setItemId(String itemId) {
459
        this.itemId = itemId;
460
    }
461
 
462
    public String getOutput() {
463
        return output;
464
    }
465
 
466
    public void setOutput(String output) {
467
        this.output = output;
468
    }
5387 rajveer 469
 
5527 anupam.sin 470
    public String getPickUp() {
471
        return pickUp;
472
    }
5387 rajveer 473
 
5527 anupam.sin 474
    public void setPickUp(String pickUp) {
475
        this.pickUp = pickUp;
476
    }
477
 
478
    public String getOrderType() {
479
        return orderType;
480
    }
481
 
482
    public void setOrderType(String orderType) {
483
        this.orderType = orderType;
484
    }
485
 
486
    public String getTinNumber() {
487
        return tinNumber;
488
    }
489
 
490
    public void setTinNumber(String tinNumber) {
491
        this.tinNumber = tinNumber;
492
    }
8925 amar.kumar 493
 
494
	public int getProductCondition() {
495
		return productCondition;
496
	}
497
 
498
	public void setProductCondition(int productCondition) {
499
		this.productCondition = productCondition;
500
	}
9416 amar.kumar 501
 
502
	public int getTaxType() {
503
		return taxType;
504
	}
505
 
506
	public void setTaxType(int taxType) {
507
		this.taxType = taxType;
508
	}
12653 manish.sha 509
 
510
	public String getPoRefNumber() {
511
		return poRefNumber;
512
	}
513
 
514
	public void setPoRefNumber(String poRefNumber) {
515
		this.poRefNumber = poRefNumber;
516
	}
517
 
518
	public static void main(String[] args) {
519
		BulkOrderController blc = new BulkOrderController();
520
		blc.setCustomerEmailId("neavins@amazon.com");
521
		blc.setRtgsId("rtgsId");
522
		blc.setRtgsBank("rtgsBank");
523
		blc.setRtgsBranch("rtgsBranch");
524
		blc.setIfscCode("ifscCode");
525
		blc.setAmount("3400");
526
		blc.setItemId("10661");
527
		//blc.setOutput();
528
		blc.setPickUp("COURIER");
529
		blc.setOrderType("B2Cbulk");
530
		//blc.setTinNumber();
531
		blc.setProductCondition(0);
532
		blc.setTaxType(0);
533
 
534
 
535
		blc.index();
536
		/*
537
		 * = "ifscCode";
538
    private static final String RTGS_BRANCH = "rtgsBranch";
539
    private static final String RTGS_BANK   = "rtgsBank";
540
    private static final String RTGS_ID     = "rtgsId";
541
		 * private String customerEmailId;
542
	    private String rtgsId;
543
	    private String rtgsBank;
544
	    private String rtgsBranch;
545
	    private String ifscCode;
546
	    private String amount;
547
	    private String itemId;
548
	    private String output;
549
	    private String pickUp;
550
	    private String orderType;
551
	    private String tinNumber;
552
	    private int productCondition;
553
	    private int taxType;*/
554
		//blc.set
555
	}
8925 amar.kumar 556
 
4207 mandeep.dh 557
}