Subversion Repositories SmartDukaan

Rev

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