Subversion Repositories SmartDukaan

Rev

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