Subversion Repositories SmartDukaan

Rev

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