Subversion Repositories SmartDukaan

Rev

Rev 7293 | Rev 8925 | 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());
7623 anupam.sin 292
            t_order.setSource(1);//Source : Website
4207 mandeep.dh 293
 
294
            orders.add(t_order);
295
        }
296
 
297
        return orders;
298
    }
299
 
300
    private LineItem enrichLineItem(LineItem lineItem)
5945 mandeep.dh 301
            throws CatalogServiceException, TException {
302
        in.shop2020.model.v1.catalog.CatalogService.Client client = new CatalogClient().getClient();
4207 mandeep.dh 303
 
304
        Item item = client.getItem(lineItem.getItem_id());
305
 
306
        lineItem.setProductGroup(item.getProductGroup());
307
        lineItem.setBrand(item.getBrand());
308
        lineItem.setModel_number(item.getModelNumber());
309
        lineItem.setModel_name(item.getModelName());
310
        lineItem.setExtra_info(item.getFeatureDescription());
311
        lineItem.setItem_id(item.getId());
312
        lineItem.setUnit_weight(item.getWeight());
313
        lineItem.setTotal_weight(item.getWeight() * lineItem.getQuantity());
314
        lineItem.setDealText(item.getBestDealText());
315
        lineItem.setTotal_price(lineItem.getUnit_price() * lineItem.getQuantity());
316
 
317
        if (item.getColor() == null || "NA".equals(item.getColor())) {
318
            lineItem.setColor("");
319
        } else {
320
            lineItem.setColor(item.getColor());
321
        }
322
 
323
        return lineItem;
324
    }
325
 
326
    private boolean validate() {
327
        return false;
328
    }
329
 
330
    public String convertDate(long date) {
331
        return SDF.format(new Date(date));
332
    }
333
 
334
    public String extractProductName(LineItem lineItem) {
335
        if (lineItem == null) {
336
            return "N/A";
337
        }
338
 
339
        return ModelUtils.extractProductNameFromLineItem(lineItem);
340
    }
341
 
342
    // Getters and Setters
343
    public String getCustomerEmailId() {
344
        return customerEmailId;
345
    }
346
 
347
    public void setCustomerEmailId(String customerEmailId) {
348
        this.customerEmailId = customerEmailId;
349
    }
350
 
351
    public String getRtgsId() {
352
        return rtgsId;
353
    }
354
 
355
    public void setRtgsId(String rtgsId) {
356
        this.rtgsId = rtgsId;
357
    }
358
 
359
    public String getRtgsBank() {
360
        return rtgsBank;
361
    }
362
 
363
    public void setRtgsBank(String rtgsBank) {
364
        this.rtgsBank = rtgsBank;
365
    }
366
 
367
    public String getRtgsBranch() {
368
        return rtgsBranch;
369
    }
370
 
371
    public void setRtgsBranch(String rtgsBranch) {
372
        this.rtgsBranch = rtgsBranch;
373
    }
374
 
375
    public String getIfscCode() {
376
        return ifscCode;
377
    }
378
 
379
    public void setIfscCode(String ifscCode) {
380
        this.ifscCode = ifscCode;
381
    }
382
 
383
    public String getAmount() {
384
        return amount;
385
    }
386
 
387
    public void setAmount(String amount) {
388
        this.amount = amount;
389
    }
390
 
391
    public List<LineItem> getLineItems() {
392
        return lineItems;
393
    }
394
 
395
    public void setLineItems(List<LineItem> lineItems) {
396
        this.lineItems = lineItems;
397
    }
398
 
399
    public Map<Transaction, Double> getTransactions() {
400
        return transactions;
401
    }
402
 
403
    public void setTransactions(Map<Transaction, Double> transactions) {
404
        this.transactions = transactions;
405
    }
406
 
407
    public String getTransactionId() {
408
        return transactionId;
409
    }
410
 
411
    public void setTransactionId(String transactionId) {
412
        this.transactionId = transactionId;
413
    }
414
 
415
    public Transaction getTransaction() {
416
        return transaction;
417
    }
418
 
419
    public void setTransaction(Transaction transaction) {
420
        this.transaction = transaction;
421
    }
422
 
423
    public Payment getPayment() {
424
        return payment;
425
    }
426
 
427
    public void setPayment(Payment payment) {
428
        this.payment = payment;
429
    }
430
 
431
    public String getId() {
432
        return id;
433
    }
434
 
435
    public void setId(String id) {
436
        this.id = id;
437
    }
438
 
439
    public String getItemId() {
440
        return itemId;
441
    }
442
 
443
    public void setItemId(String itemId) {
444
        this.itemId = itemId;
445
    }
446
 
447
    public String getOutput() {
448
        return output;
449
    }
450
 
451
    public void setOutput(String output) {
452
        this.output = output;
453
    }
5387 rajveer 454
 
5527 anupam.sin 455
    public String getPickUp() {
456
        return pickUp;
457
    }
5387 rajveer 458
 
5527 anupam.sin 459
    public void setPickUp(String pickUp) {
460
        this.pickUp = pickUp;
461
    }
462
 
463
    public String getOrderType() {
464
        return orderType;
465
    }
466
 
467
    public void setOrderType(String orderType) {
468
        this.orderType = orderType;
469
    }
470
 
471
    public String getTinNumber() {
472
        return tinNumber;
473
    }
474
 
475
    public void setTinNumber(String tinNumber) {
476
        this.tinNumber = tinNumber;
477
    }
4207 mandeep.dh 478
}