Subversion Repositories SmartDukaan

Rev

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