Subversion Repositories SmartDukaan

Rev

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