Subversion Repositories SmartDukaan

Rev

Rev 5387 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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