Subversion Repositories SmartDukaan

Rev

Rev 5917 | Rev 7595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3137 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
3339 mandeep.dh 6
import in.shop2020.crm.Activity;
7
import in.shop2020.crm.ActivityType;
3137 mandeep.dh 8
import in.shop2020.crm.Agent;
3390 mandeep.dh 9
import in.shop2020.crm.SearchFilter;
3137 mandeep.dh 10
import in.shop2020.crm.Ticket;
3339 mandeep.dh 11
import in.shop2020.crm.TicketCategory;
12
import in.shop2020.crm.TicketPriority;
3137 mandeep.dh 13
import in.shop2020.crm.TicketStatus;
5791 anupam.sin 14
import in.shop2020.model.v1.order.Attribute;
3546 mandeep.dh 15
import in.shop2020.model.v1.order.LineItem;
16
import in.shop2020.model.v1.order.Order;
17
import in.shop2020.model.v1.order.OrderStatus;
4267 anupam.sin 18
import in.shop2020.model.v1.order.Transaction;
3546 mandeep.dh 19
import in.shop2020.model.v1.order.TransactionServiceException;
4267 anupam.sin 20
import in.shop2020.model.v1.order.TransactionStatus;
3137 mandeep.dh 21
import in.shop2020.model.v1.user.User;
22
import in.shop2020.model.v1.user.UserContextException;
3578 mandeep.dh 23
import in.shop2020.payments.Payment;
24
import in.shop2020.payments.PaymentException;
25
import in.shop2020.payments.PaymentStatus;
3390 mandeep.dh 26
import in.shop2020.serving.auth.CRMAuthorizingRealm;
27
import in.shop2020.thrift.clients.CRMClient;
3405 mandeep.dh 28
import in.shop2020.thrift.clients.HelperClient;
3578 mandeep.dh 29
import in.shop2020.thrift.clients.PaymentClient;
3546 mandeep.dh 30
import in.shop2020.thrift.clients.TransactionClient;
3390 mandeep.dh 31
import in.shop2020.thrift.clients.UserClient;
3405 mandeep.dh 32
import in.shop2020.util.CRMConstants;
4020 mandeep.dh 33
import in.shop2020.util.CRMConstants.EscalationMatrix;
3405 mandeep.dh 34
import in.shop2020.utils.HelperService.Client;
35
import in.shop2020.utils.HelperServiceException;
3546 mandeep.dh 36
import in.shop2020.utils.ModelUtils;
3137 mandeep.dh 37
 
5791 anupam.sin 38
import java.text.DateFormat;
3397 mandeep.dh 39
import java.text.ParseException;
5791 anupam.sin 40
import java.text.SimpleDateFormat;
3137 mandeep.dh 41
import java.util.ArrayList;
5791 anupam.sin 42
import java.util.Arrays;
3578 mandeep.dh 43
import java.util.Calendar;
3390 mandeep.dh 44
import java.util.Collections;
3405 mandeep.dh 45
import java.util.Date;
4490 anupam.sin 46
import java.util.HashMap;
3137 mandeep.dh 47
import java.util.List;
5909 amar.kumar 48
import java.util.Map;
5791 anupam.sin 49
import java.util.TimeZone;
3137 mandeep.dh 50
 
4256 mandeep.dh 51
import net.htmlparser.jericho.Source;
52
 
3405 mandeep.dh 53
import org.apache.shiro.SecurityUtils;
3137 mandeep.dh 54
import org.apache.thrift.TException;
3546 mandeep.dh 55
import org.apache.thrift.transport.TTransportException;
3137 mandeep.dh 56
 
57
/**
58
 * @author mandeep
59
 * 
60
 */
61
public class TicketsController extends BaseController {
62
 
63
    /**
64
     * 
65
     */
66
    private static final long serialVersionUID = 1L;
67
 
68
    List<Ticket>              tickets          = new ArrayList<Ticket>();
3405 mandeep.dh 69
    private String            customerEmailId;
3339 mandeep.dh 70
    private String            description;
71
    private String            assigneeEmailId;
72
    private String            priority;
73
    private String            category;
74
    private String            orderId;
3397 mandeep.dh 75
    private String[]          agentIds;
76
    private String            startTimestamp;
77
    private String            endTimestamp;
3137 mandeep.dh 78
 
3405 mandeep.dh 79
    private String            userId;
80
    private String            id;
81
    private String            activityDescription;
82
    private String            status;
83
    private String            activityType;
84
    private Ticket            ticket;
85
    private List<Activity>    activities;
86
    private String            subject;
87
    private String            body;
88
    private String            customerName;
89
    private String            customerMobileNumber;
3546 mandeep.dh 90
    private User              user;
91
    private String            orderStatus;
92
    private List<Order>       orders;
4267 anupam.sin 93
    private List<Payment>     payments;
4020 mandeep.dh 94
    private String            escalate;
4241 anupam.sin 95
    private String            cancelReason;
4267 anupam.sin 96
    private String            transactionId;
5791 anupam.sin 97
    private String            pickupExtension;
5909 amar.kumar 98
    private Map<String, Long> ticketCategorycountMap;
4438 anupam.sin 99
    private String paymentId;
100
 
4490 anupam.sin 101
    private HashMap<String, String> allAttachmentsForTicket;
102
 
3405 mandeep.dh 103
    public String index() {
104
        try {
105
            if (id != null && !id.isEmpty()) {
106
                SearchFilter searchFilter = new SearchFilter();
107
                crmServiceClient = new CRMClient().getClient();
108
                searchFilter.setTicketId(Long.parseLong(id));
109
                tickets = crmServiceClient.getTickets(searchFilter);
110
            }
111
        } catch (TException e) {
112
            log.error("Error while getting tickets", e);
113
            return EXCEPTION;
114
        }
115
 
116
        return INDEX;
117
    }
118
 
119
    public String edit() {
120
        try {
121
            long ticketId = Long.parseLong(id);
122
            SearchFilter searchFilter = new SearchFilter();
123
            searchFilter.setTicketId(ticketId);
124
            crmServiceClient = new CRMClient().getClient();
125
            ticket = crmServiceClient.getTickets(searchFilter).get(0);
126
            activities = crmServiceClient.getActivities(searchFilter);
4490 anupam.sin 127
            populateAttachments();
3405 mandeep.dh 128
            subject = createSubjectString(ticket);
129
 
130
            if (ticket.isSetCustomerId()) {
131
                userId = String.valueOf(ticket.getCustomerId());
3546 mandeep.dh 132
                userContextServiceClient = new UserClient().getClient();
133
 
134
                try {
135
                    user = userContextServiceClient.getUserById(ticket
136
                            .getCustomerId());
137
                } catch (UserContextException e) {
138
                    log.error("Could not fetch user details", e);
139
                }
3405 mandeep.dh 140
            }
141
 
142
            customerEmailId = ticket.getCustomerEmailId();
4689 anupam.sin 143
 
4277 anupam.sin 144
            if (TicketCategory.ORDER_CANCELLATION.equals(ticket.getCategory())) {
145
                log.info("Fetching orders for " + ticket.getCustomerId());
146
                orders = new ArrayList<Order>();
147
                transactionServiceClient = new TransactionClient().getClient();
4689 anupam.sin 148
                for (Order order : transactionServiceClient
149
                        .getOrdersForCustomer(ticket.getCustomerId(), 0, 0,
150
                                Collections.singletonList(OrderStatus.CANCEL_REQUEST_RECEIVED))) {
151
                    log.info("Processing Order: " + order.getId());
4287 anupam.sin 152
                    orders.add(order);
153
                }
4277 anupam.sin 154
            }
3546 mandeep.dh 155
 
156
            if (TicketCategory.COD_VERIFICATION.equals(ticket.getCategory())) {
157
                log.info("Fetching orders for " + ticket.getCustomerId());
158
                orders = new ArrayList<Order>();
159
                transactionServiceClient = new TransactionClient().getClient();
160
                for (Order order : transactionServiceClient
161
                        .getOrdersForCustomer(ticket.getCustomerId(), 0, 0,
4663 rajveer 162
                                Collections.singletonList(OrderStatus.COD_VERIFICATION_PENDING))) {
3546 mandeep.dh 163
                    log.info("Processing Order: " + order);
164
                    if (order.isCod()) {
165
                        orders.add(order);
166
                    }
167
                }
168
            }
4689 anupam.sin 169
 
4490 anupam.sin 170
            if (TicketCategory.DOA_RECEIVED.equals(ticket.getCategory())) {
171
                log.info("Fetching orders for " + ticket.getCustomerId());
172
                orders = new ArrayList<Order>();
173
                transactionServiceClient = new TransactionClient().getClient();
174
                for (Order order : transactionServiceClient
175
                        .getOrdersForCustomer(ticket.getCustomerId(), 0, 0,
176
                                Collections.singletonList(OrderStatus.DOA_REQUEST_RECEIVED))) {
177
                    log.info("Processing Order: " + order);
178
                    if (order != null) {
179
                        orders.add(order);
180
                    }
181
                }
182
            }
4689 anupam.sin 183
 
4490 anupam.sin 184
            if (TicketCategory.RETURN_FORM.equals(ticket.getCategory())) {
185
                log.info("Fetching orders for " + ticket.getCustomerId());
186
                orders = new ArrayList<Order>();
187
                transactionServiceClient = new TransactionClient().getClient();
188
                for (Order order : transactionServiceClient
189
                        .getOrdersForCustomer(ticket.getCustomerId(), 0, 0,
190
                                Collections.singletonList(OrderStatus.RET_REQUEST_RECEIVED))) {
191
                    log.info("Processing Order: " + order);
192
                    if (order != null) {
193
                        orders.add(order);
194
                    }
195
                }
196
            }
3578 mandeep.dh 197
 
4267 anupam.sin 198
            if (TicketCategory.PAYMENT_FLAGGED.equals(ticket.getCategory())) {
199
                log.info("Fetching orders for " + ticket.getCustomerId());
200
                payments = new ArrayList<Payment>();
201
                in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
202
                transactionServiceClient = new TransactionClient().getClient();
203
                for (Transaction transaction : transactionServiceClient
204
                        .getTransactionsForCustomer(ticket.getCustomerId(), 0, 0, TransactionStatus.FLAGGED)) {
205
                    log.info("Processing Transaction: " + transaction);
206
                    for ( Payment payment : paymentClient.getPaymentForTxnId(transaction.getId())) {
207
                        if( payment.getStatus().equals(PaymentStatus.AUTHORIZED)) {
208
                            payments.add(payment);
209
                        }
210
                    }
211
                }
212
            }
213
 
3578 mandeep.dh 214
            if (TicketCategory.FAILED_PAYMENTS.equals(ticket.getCategory())) {
215
                log.info("Fetching orders for " + ticket.getCustomerId());
216
                orders = new ArrayList<Order>();
217
                in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
218
 
219
                long failedTransactionId = -1;
220
                long lastfailedTransactionTimestamp = -1;
221
                for (Payment payment : paymentClient.getPaymentsForUser(ticket.getCustomerId(), 0, getTomorrow().getTime(), PaymentStatus.FAILED, 0)) {
222
                    if (payment.getErrorTimestamp() > lastfailedTransactionTimestamp) {
223
                        failedTransactionId = payment.getMerchantTxnId();
224
                    }
225
                }
226
 
227
                transactionServiceClient = new TransactionClient().getClient();
228
                orders.addAll(transactionServiceClient.getOrdersForTransaction(failedTransactionId, ticket.getCustomerId()));
229
            }
4008 mandeep.dh 230
 
231
            if (TicketCategory.DELAYED_DELIVERY.equals(ticket.getCategory())) {
232
                log.info("Fetching orders for " + ticket.getCustomerId());
233
                orders = new ArrayList<Order>();
234
                transactionServiceClient = new TransactionClient().getClient();
235
 
236
                for (Order order : transactionServiceClient.getOrdersForCustomer(ticket.getCustomerId(), 0, 0, null)) {
237
                    if (order.getExpected_delivery_time() > order.getPromised_delivery_time() && order.getExpected_delivery_time() > new Date().getTime()) {
238
                        orders.add(order);
239
                    }
240
                }
241
            }
5791 anupam.sin 242
 
243
            if (TicketCategory.STORE_PICKUP.equals(ticket.getCategory())) {
244
                log.info("Fetching orders for " + ticket.getCustomerId());
245
                orders = new ArrayList<Order>();
246
                transactionServiceClient = new TransactionClient().getClient();
247
                for (Order order : transactionServiceClient
248
                        .getOrdersForCustomer(ticket.getCustomerId(), 0, 0,
249
                                Collections.singletonList(OrderStatus.RECEIVED_AT_STORE))) {
250
                    log.info("Processing Order: " + order.getId());
251
                    orders.add(order);
252
                }
253
            }
254
 
3405 mandeep.dh 255
        } catch (TException e) {
3546 mandeep.dh 256
            log.error("Error loading edit page", e);
257
        } catch (NumberFormatException e) {
258
            log.error("Error parsing orderId", e);
259
        } catch (TransactionServiceException e) {
260
            log.error("Error fetching orders for " + ticket.getCustomerId(), e);
3578 mandeep.dh 261
        } catch (PaymentException e) {
262
            log.error("Error fetching payments for " + ticket.getCustomerId(), e);
3405 mandeep.dh 263
        }
264
 
265
        return EDIT;
266
    }
267
 
4490 anupam.sin 268
    private void populateAttachments() {
269
        allAttachmentsForTicket = new HashMap<String, String>();
270
        for (Activity activity : activities) {
271
            log.info("" + activity.getId());
272
            String attachment = activity.getAttachments();
273
            if (attachment != null && !attachment.isEmpty()) {
274
                log.info("" + attachment);
275
                String [] attachmentList = attachment.split(";");
276
                for (String attachmentName : attachmentList) {
277
                    if (attachmentName != null && !attachmentName.isEmpty()) {
278
                        log.info("" + attachmentName);
279
                        allAttachmentsForTicket.put(activity.getId() + "-" + attachmentName, attachmentName);
280
                    }
281
                }
282
            }
283
        }
284
    }
285
 
5791 anupam.sin 286
    public static void main(String[] args) throws TException {
287
        SearchFilter s = new SearchFilter();
288
//      s.setTicketId(200l);
289
//      s.setTicketAssigneeIds(null);
290
//      s.setTicketAssigneeIds(new ArrayList<Long>());
291
//      s.setActivityCreatorIds(Collections.singletonList(2l));
292
//      s.setEndTimestamp(new Date());
293
      //s.setTicketCategory(TicketCategory.STORE_PICKUP);
294
      s.setTicketId((long) 51345);
295
      in.shop2020.crm.CRMService.Client crmServiceClient = new CRMClient().getClient();
296
      Ticket ticket = crmServiceClient.getTickets(s).get(0);
297
      System.out.println(ticket.getCategory().name());
4490 anupam.sin 298
    }
299
 
3405 mandeep.dh 300
    private String createSubjectString(Ticket ticket) {
301
        return CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID + ticket.getId()
302
                + " " + ticket.getCategory().name();
303
    }
304
 
3578 mandeep.dh 305
    private Date getTomorrow() {
306
        Calendar calendar = Calendar.getInstance();
307
        calendar.setTime(new Date());
308
        calendar.add(Calendar.DAY_OF_MONTH, 1);
309
        return calendar.getTime();
310
    }
311
 
3339 mandeep.dh 312
    public String create() {
313
        try {
3546 mandeep.dh 314
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId)
315
                    .getId();
3339 mandeep.dh 316
            Ticket ticket = new Ticket();
317
            ticket.setDescription(description);
318
            ticket.setCreatorId(creatorId);
319
            ticket.setStatus(TicketStatus.OPEN);
320
            ticket.setPriority(TicketPriority.valueOf(priority));
321
            ticket.setCategory(TicketCategory.valueOf(category));
322
 
323
            Activity activity = new Activity();
324
            activity.setDescription("Creating Ticket");
325
            activity.setType(ActivityType.OTHER);
326
            activity.setTicketPriority(TicketPriority.valueOf(priority));
327
            activity.setTicketStatus(TicketStatus.OPEN);
328
            activity.setCreatorId(creatorId);
329
            activity.setTicketCategory(TicketCategory.valueOf(category));
330
            activity.setTicketDescription(description);
331
 
332
            if (orderId != null && !orderId.isEmpty()) {
333
                ticket.setOrderId(Long.parseLong(orderId));
334
            }
4490 anupam.sin 335
 
336
            if (TicketCategory.valueOf(category) == TicketCategory.DOA_RECEIVED) {
337
                transactionServiceClient = new TransactionClient().getClient();
338
                if (!transactionServiceClient.markOrderDoaRequestReceived(Long.parseLong(orderId))) {
339
                    throw new Exception("Order cannot be marked as DOA Received");
340
                }
341
                log.info("" + userId);
342
 
343
                Order order = transactionServiceClient.getOrder(Long.parseLong(orderId));
344
                log.info("In order table id is : " + order.getCustomer_id());
345
                if (userId == null || userId.isEmpty() || order.getCustomer_id() != Long.parseLong(userId)) {
346
                    throw new Exception("This order is not associated with this user Id");
347
                }
348
            }
349
 
350
            if (TicketCategory.valueOf(category) == TicketCategory.RETURN_FORM) {
351
                transactionServiceClient = new TransactionClient().getClient();
352
                if (!transactionServiceClient.markOrderReturnRequestReceived(Long.parseLong(orderId))) {
353
                    throw new Exception("Order cannot be marked as Return Requested");
354
                }
355
                log.info("" + userId);
356
 
357
                Order order = transactionServiceClient.getOrder(Long.parseLong(orderId));
358
                log.info("In order table id is : " + order.getCustomer_id());
359
                if (userId == null || userId.isEmpty() || order.getCustomer_id() != Long.parseLong(userId)) {
360
                    throw new Exception("This order is not associated with this user Id");
361
                }
362
            }
4681 amar.kumar 363
            if(body!=null && !body.isEmpty()){
364
            	String plainTextbody = new Source(body).getTextExtractor().toString();
365
            	ticket.setDescription(description + " : " + plainTextbody);
366
            }
4490 anupam.sin 367
 
3405 mandeep.dh 368
            if (userId != null && !userId.isEmpty()) {
369
                ticket.setCustomerId(Long.parseLong(userId));
370
                activity.setCustomerId(Long.parseLong(userId));
3546 mandeep.dh 371
            } else {
3390 mandeep.dh 372
                User user = null;
373
                userContextServiceClient = new UserClient().getClient();
374
                try {
3405 mandeep.dh 375
                    if (customerName != null && !customerName.isEmpty()) {
376
                        ticket.setCustomerName(customerName);
377
                        activity.setCustomerName(customerName);
378
                    }
379
 
380
                    if (customerEmailId != null && !customerEmailId.isEmpty()) {
381
                        ticket.setCustomerEmailId(customerEmailId);
382
                        activity.setCustomerEmailId(customerEmailId);
3546 mandeep.dh 383
                        user = userContextServiceClient
384
                                .getUserByEmail(customerEmailId);
3405 mandeep.dh 385
                    }
386
 
3546 mandeep.dh 387
                    if ((user == null || user.getUserId() == -1)
388
                            && customerMobileNumber != null
389
                            && !customerMobileNumber.isEmpty()) {
3405 mandeep.dh 390
                        ticket.setCustomerMobileNumber(customerMobileNumber);
391
                        activity.setCustomerMobileNumber(customerMobileNumber);
3546 mandeep.dh 392
                        user = userContextServiceClient
393
                                .getUserByMobileNumber(Long
394
                                        .parseLong(customerMobileNumber));
3405 mandeep.dh 395
                    }
3390 mandeep.dh 396
                } catch (UserContextException e) {
3546 mandeep.dh 397
                    log.error("Could not fetch user for: " + customerEmailId
398
                            + " " + customerMobileNumber + " " + customerName,
399
                            e);
3390 mandeep.dh 400
                }
3546 mandeep.dh 401
 
3390 mandeep.dh 402
                if (user != null && user.getUserId() != -1) {
403
                    ticket.setCustomerId(user.getUserId());
404
                    activity.setCustomerId(user.getUserId());
405
                }
3339 mandeep.dh 406
            }
407
 
408
            // handling null values appropriately
409
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
3546 mandeep.dh 410
                long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId)
411
                        .getId();
3339 mandeep.dh 412
                ticket.setAssigneeId(assigneeId);
413
                activity.setTicketAssigneeId(assigneeId);
414
            }
415
 
3546 mandeep.dh 416
            crmServiceClient = new CRMClient().getClient();
417
            id = String
418
                    .valueOf(crmServiceClient.insertTicket(ticket, activity));
3339 mandeep.dh 419
        } catch (TException e) {
420
            log.error("Error while creating ticket", e);
421
            return EXCEPTION;
4490 anupam.sin 422
        } catch (NumberFormatException e) {
423
            log.error("Error while parsing Order Id", e);
424
            return EXCEPTION;
425
        } catch (TransactionServiceException e) {
426
            log.error("Error while updating order status", e);
427
            return EXCEPTION;
428
        } catch (Exception e) {
429
            log.error(e.getMessage());
430
            return EXCEPTION;
3339 mandeep.dh 431
        }
432
 
3405 mandeep.dh 433
        return index();
3339 mandeep.dh 434
    }
435
 
3405 mandeep.dh 436
    public boolean isAssigneeEditable() {
437
        return SecurityUtils.getSubject().hasRole("TeamLead");
438
    }
439
 
3397 mandeep.dh 440
    public String searchTickets() throws ParseException {
3137 mandeep.dh 441
        try {
3397 mandeep.dh 442
            SearchFilter searchFilter = new SearchFilter();
3405 mandeep.dh 443
            if (userId != null && !userId.isEmpty()) {
444
                searchFilter.setCustomerId(Long.parseLong(userId));
445
            }
446
 
3397 mandeep.dh 447
            if (agentIds != null && agentIds.length != 0) {
448
                searchFilter.setTicketAssigneeIds(new ArrayList<Long>());
449
                for (String agentId : agentIds) {
3546 mandeep.dh 450
                    searchFilter.getTicketAssigneeIds().add(
451
                            CRMAuthorizingRealm.getAgent(agentId).getId());
3397 mandeep.dh 452
                }
453
            }
3137 mandeep.dh 454
 
3397 mandeep.dh 455
            if (startTimestamp != null && !startTimestamp.isEmpty()) {
3546 mandeep.dh 456
                searchFilter.setStartTimestamp(SDF.parse(startTimestamp)
457
                        .getTime());
3397 mandeep.dh 458
            }
459
 
460
            if (endTimestamp != null && !endTimestamp.isEmpty()) {
461
                searchFilter.setEndTimestamp(SDF.parse(endTimestamp).getTime());
462
            }
463
 
3422 mandeep.dh 464
            if (id != null && !id.isEmpty()) {
465
                searchFilter.setTicketId(Long.parseLong(id));
466
            }
467
 
3499 mandeep.dh 468
            if (status != null && !status.isEmpty()) {
3972 mandeep.dh 469
                searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
470
                searchFilter.getTicketStatuses().add(TicketStatus.valueOf(status));
471
                if (TicketStatus.OPEN.name().equals(status)) {
472
                    searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
473
                }
3499 mandeep.dh 474
            }
475
 
476
            if (category != null && !category.isEmpty()) {
3546 mandeep.dh 477
                searchFilter
478
                        .setTicketCategory(TicketCategory.valueOf(category));
3499 mandeep.dh 479
            }
480
 
3546 mandeep.dh 481
            crmServiceClient = new CRMClient().getClient();
3405 mandeep.dh 482
            tickets = crmServiceClient.getTickets(searchFilter);
3137 mandeep.dh 483
        } catch (TException e) {
484
            String errorString = "Error getting tickets for "
485
                    + currentAgentEmailId;
486
            log.error(errorString, e);
487
            addActionError(errorString);
488
        }
489
 
3405 mandeep.dh 490
        return index();
3137 mandeep.dh 491
    }
492
 
493
    public String getUnassignedTickets() {
494
        try {
3546 mandeep.dh 495
            crmServiceClient = new CRMClient().getClient();
3137 mandeep.dh 496
            tickets = crmServiceClient.getUnassignedTickets();
497
        } catch (TException e) {
3339 mandeep.dh 498
            String errorString = "Error getting tickets for "
499
                    + currentAgentEmailId;
3137 mandeep.dh 500
            log.error(errorString, e);
501
            addActionError(errorString);
502
        }
503
 
3405 mandeep.dh 504
        return index();
3137 mandeep.dh 505
    }
506
 
3405 mandeep.dh 507
    public String update() {
508
        try {
509
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId)
510
                    .getId();
511
 
3546 mandeep.dh 512
            SearchFilter searchFilter = new SearchFilter();
513
            searchFilter.setTicketId(Long.parseLong(id));
514
            crmServiceClient = new CRMClient().getClient();
515
            Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
3405 mandeep.dh 516
            ticket.setDescription(description);
517
            ticket.setPriority(TicketPriority.valueOf(priority));
518
 
519
            // Update when a ticket is closed!
3578 mandeep.dh 520
            if (TicketStatus.CLOSED.name().equals(status) || TicketCategory.COD_VERIFICATION.equals(ticket.getCategory())) {
3546 mandeep.dh 521
                if (TicketCategory.COD_VERIFICATION.equals(ticket.getCategory())
522
                        && pendingCODOrders(ticket.getCustomerId())) {
523
                    addActionMessage("Cod orders pending for verification. Ticket can not be closed and its category cannot be changed.");
524
                } else {
525
                    ticket.setCategory(TicketCategory.valueOf(category));
526
                    ticket.setStatus(TicketStatus.valueOf(status));
527
                    ticket.setCloseDate(new Date().getTime());
528
                }
3405 mandeep.dh 529
            }
3546 mandeep.dh 530
            else {
531
                ticket.setCategory(TicketCategory.valueOf(category));
3578 mandeep.dh 532
                ticket.setStatus(TicketStatus.valueOf(status));
3546 mandeep.dh 533
            }
3405 mandeep.dh 534
 
535
            if (activityDescription == null || activityDescription.isEmpty()) {
4241 anupam.sin 536
                activityDescription = "Ticket fields updated";
3405 mandeep.dh 537
            }
538
 
539
            Activity activity = new Activity();
540
            activity.setDescription(activityDescription);
541
            activity.setType(ActivityType.valueOf(activityType));
3546 mandeep.dh 542
            activity.setTicketPriority(ticket.getPriority());
543
            activity.setTicketStatus(ticket.getStatus());
3405 mandeep.dh 544
            activity.setCreatorId(creatorId);
3546 mandeep.dh 545
            activity.setTicketCategory(ticket.getCategory());
546
            activity.setTicketDescription(ticket.getDescription());
3405 mandeep.dh 547
 
548
            if (userId != null && !userId.isEmpty()) {
549
                activity.setCustomerId(Long.parseLong(userId));
550
            }
551
 
4035 mandeep.dh 552
            // Handling null values appropriately
553
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
554
                long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId)
555
                        .getId();
556
                ticket.setAssigneeId(assigneeId);
557
                activity.setTicketAssigneeId(assigneeId);
558
            }
559
 
3405 mandeep.dh 560
            if (ActivityType.SEND_EMAIL_TO_CUSTOMER.equals(activity.getType())) {
561
                log.info("Sending mail");
562
                Client helperClient = new HelperClient().getClient();
563
                activity.setUserEmailId(helperClient.saveUserEmailForSending(
5865 amar.kumar 564
                        Arrays.asList(customerEmailId), CRMConstants.CRM_EMAIL_SENDOR,
565
                        subject, body, null, CRMConstants.CRM_EMAIL_TYPE, null, null));
3405 mandeep.dh 566
 
4256 mandeep.dh 567
                String plainTextbody = new Source(body).getTextExtractor().toString();
568
 
3405 mandeep.dh 569
                // We change activityType to OTHER when pop up box for email
570
                // closes
571
                activity.setDescription("Subject: " + subject + "\n\n"
4256 mandeep.dh 572
                        + "Body: " + plainTextbody);
4206 mandeep.dh 573
 
574
                String activityDesc = activity.getDescription();
575
                if (activityDesc.length() > CRMConstants.DESCRIPTION_MAX_WIDTH) {
576
                    activityDesc = activityDesc.substring(0, CRMConstants.DESCRIPTION_MAX_WIDTH);
577
                    activityDesc += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
578
                }
579
 
580
                activity.setDescription(activityDesc);
3405 mandeep.dh 581
            }
582
 
4020 mandeep.dh 583
            if (ActivityType.ESCALATE_TICKET.equals(activity.getType())) {
584
                log.info("Sending escalation mail");
585
                EscalationMatrix escalation = CRMConstants.EscalationMatrix.valueOf(escalate);
586
                List<Integer> escalationReceipients = escalation.getAgentIds();
587
 
5865 amar.kumar 588
                List<String> toMailIds = new ArrayList<String>();
4020 mandeep.dh 589
                for (Integer agentId : escalationReceipients) {
5865 amar.kumar 590
                    toMailIds.add(CRMAuthorizingRealm.getAgent(agentId).getEmailId());
4034 mandeep.dh 591
 
592
                    // Setting last agent as assignee
593
                    ticket.setAssigneeId(agentId);
594
                    activity.setTicketAssigneeId(agentId);
4020 mandeep.dh 595
                }
596
 
597
                Client helperClient = new HelperClient().getClient();
598
                String escalationMailSubject = createEscalationMailSubject(ticket, escalation);
599
                String escalationMailBody = createEscalationMailBody(ticket, escalation);
600
 
601
                activity.setUserEmailId(helperClient.saveUserEmailForSending(
602
                        toMailIds, CRMConstants.CRM_EMAIL_SENDOR,
603
                        escalationMailSubject, escalationMailBody,
5865 amar.kumar 604
                        null, CRMConstants.CRM_EMAIL_TYPE, null, null));
4020 mandeep.dh 605
 
606
                // We change activityType to OTHER when pop up box for email
607
                // closes
608
                activity.setDescription("To: " + toMailIds + "\n\nSubject: " + escalationMailSubject + "\n\n"
609
                        + "Body: " + escalationMailBody);
610
            }
611
 
3405 mandeep.dh 612
            User user = null;
613
            userContextServiceClient = new UserClient().getClient();
614
            try {
615
                if (customerName != null && !customerName.isEmpty()) {
616
                    ticket.setCustomerName(customerName);
617
                    activity.setCustomerName(customerName);
618
                }
619
 
620
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
621
                    ticket.setCustomerEmailId(customerEmailId);
622
                    activity.setCustomerEmailId(customerEmailId);
3546 mandeep.dh 623
                    user = userContextServiceClient
624
                            .getUserByEmail(customerEmailId);
3405 mandeep.dh 625
                }
626
 
3546 mandeep.dh 627
                if ((user == null || user.getUserId() == -1)
628
                        && customerMobileNumber != null
629
                        && !customerMobileNumber.isEmpty()) {
3405 mandeep.dh 630
                    ticket.setCustomerMobileNumber(customerMobileNumber);
631
                    activity.setCustomerMobileNumber(customerMobileNumber);
3546 mandeep.dh 632
                    user = userContextServiceClient.getUserByMobileNumber(Long
633
                            .parseLong(customerMobileNumber));
3405 mandeep.dh 634
                }
635
            } catch (UserContextException e) {
636
                log.error("Could not fetch user for: " + customerEmailId + " "
637
                        + customerMobileNumber + " " + customerName, e);
638
            }
3546 mandeep.dh 639
 
3405 mandeep.dh 640
            if (user != null && user.getUserId() != -1) {
641
                ticket.setCustomerId(user.getUserId());
642
                activity.setCustomerId(user.getUserId());
643
            }
644
 
645
            crmServiceClient = new CRMClient().getClient();
646
            crmServiceClient.updateTicket(ticket, activity);
647
        } catch (TException e) {
648
            log.error("Error while updating ticket", e);
649
            return EXCEPTION;
650
        } catch (HelperServiceException hse) {
651
            log.error("Error while sending mail", hse);
652
            return EXCEPTION;
653
        }
654
 
655
        return index();
656
    }
657
 
4020 mandeep.dh 658
    private String createEscalationMailBody(Ticket ticket,
659
            EscalationMatrix escalation)
660
    {
661
        return body + "<br />Please visit following URL to check ticket details.<br />" + 
662
            "http://cs.shop2020.in:8080/crm?ticketId=" + ticket.getId();
663
    }
664
 
665
    private String createEscalationMailSubject(Ticket ticket,
666
            EscalationMatrix escalation)
667
    {
668
        return CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID + ticket.getId() + " - ESCALATION FROM CRM - Type: " + escalation.name();
669
    }
670
 
3546 mandeep.dh 671
    private boolean pendingCODOrders(long customerId) {
672
        try {
673
            log.info("Trying to fetch orders for " + customerId);
674
            transactionServiceClient = new TransactionClient().getClient();
675
            for (Order order : transactionServiceClient.getOrdersForCustomer(
676
                    customerId, 0, 0,
4663 rajveer 677
                    Collections.singletonList(OrderStatus.COD_VERIFICATION_PENDING))) {
3546 mandeep.dh 678
                log.info("Processing order: " + order.getId() + " with COD: " + order.isCod());
679
                if (order.isCod()) {
680
                    log.info("Returning true from pendingCODOrders() for " + customerId);
681
                    return true;
682
                }
683
            }
684
        } catch (TTransportException e) {
685
            log.error("Error while creating thrift client", e);
686
        } catch (TransactionServiceException e) {
687
            log.error("Error fetching orders", e);
688
        } catch (TException e) {
689
            log.error("Error fetching orders", e);
690
        }
691
 
692
        log.info("Returning false from pendingCODOrders() for " + customerId);
693
        return false;
694
    }
5791 anupam.sin 695
 
696
    public String extendExpiry() {
697
        try {
698
            log.info("order Id = " + orderId);
699
            log.info("pickup : " + pickupExtension);
700
            Attribute newAttribute = new Attribute();
701
            List<Attribute> attributes = new ArrayList<Attribute>();
702
            String extendedBy = "0";
703
            transactionServiceClient = new TransactionClient().getClient();
704
            attributes = transactionServiceClient.getAllAttributesForOrderId(Long.parseLong(orderId));
705
            for (Attribute attribute : attributes) {
706
                if (attribute.getName().equals("pickupExtension")) {
707
                    extendedBy = attribute.getValue();
708
                    break;
709
                }
710
            }
711
            log.info("ATTR = " + attributes);
712
            log.info("1");
713
            if (extendedBy.equals("0")) {
714
                newAttribute.setName("pickupExtension");
715
                newAttribute.setValue(pickupExtension);
716
                attributes.add(newAttribute);
717
                log.info("2");
718
            } else {
719
                Long totalExtension = Long.parseLong(extendedBy) + Long.parseLong(pickupExtension);
720
                for (Attribute attribute : attributes) {
721
                    if (attribute.getName().equals("pickupExtension")) {
722
                        attribute.setValue(totalExtension.toString());
723
                        log.info("3");
724
                    }
725
                }
726
            }
727
            transactionServiceClient = new TransactionClient().getClient();
728
            transactionServiceClient.setOrderAttributes(Long.parseLong(orderId), attributes);
5919 anupam.sin 729
 
730
            SearchFilter searchFilter = new SearchFilter();
731
            searchFilter.setTicketId(Long.parseLong(id));
732
            crmServiceClient = new CRMClient().getClient();
733
            Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
734
 
735
            // Inserting activity
736
            Activity activity = new Activity();
737
            activity.setDescription("Pickup extended by " + pickupExtension
738
                    + " Days.");
739
            activity.setType(ActivityType.OTHER);
740
            activity.setTicketPriority(ticket.getPriority());
741
            activity.setTicketStatus(ticket.getStatus());
742
            activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
743
            activity.setTicketCategory(ticket.getCategory());
744
            activity.setTicketDescription(ticket.getDescription());
745
            activity.setCustomerId(ticket.getCustomerId());
746
            activity.setTicketAssigneeId(ticket.getAssigneeId());
747
 
748
            crmServiceClient = new CRMClient().getClient();
749
            crmServiceClient.updateTicket(ticket, activity);
750
 
5917 anupam.sin 751
            //transactionServiceClient = new TransactionClient().getClient();
752
            //transactionServiceClient.enqueueExpiryExtensionEmail(orderId);
5791 anupam.sin 753
        } catch (NumberFormatException e) {
754
            e.printStackTrace();
755
        } catch (TException e) {
756
            e.printStackTrace();
757
        }
5917 anupam.sin 758
        return edit();
5791 anupam.sin 759
    }
760
 
761
    public String getPickupExpiryDate(long id, long deliveryTimestamp) {
762
        try {
763
            log.info("orderID" + id);
764
            log.info("Delivery Time : " + deliveryTimestamp);
765
            Date deliveryDate = new Date(deliveryTimestamp);
766
            Calendar cal = Calendar.getInstance();
767
            cal.setTime(deliveryDate);
768
 
769
            log.info("Orig Del Date :" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR));
770
 
771
            cal.add(Calendar.DATE, 5);
772
 
773
            log.info("After adding 5 days" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR));
774
 
775
            transactionServiceClient = new TransactionClient().getClient();
776
            List<Attribute> attributes = new ArrayList<Attribute>();
777
            String extendedBy = "0";
778
            attributes = transactionServiceClient.getAllAttributesForOrderId(id);
779
            for (Attribute attribute : attributes) {
780
                if (attribute.getName().equals("pickupExtension")) {
781
                    extendedBy = attribute.getValue();
782
                }
783
            }
784
            cal.add(Calendar.DATE, Integer.parseInt(extendedBy));
785
 
786
            log.info("Extended by" + extendedBy);
787
 
788
            log.info("Final : " + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR));
789
 
790
            SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy");
791
            return SDF.format(cal.getTime());
792
 
793
        } catch (NumberFormatException e) {
794
            e.printStackTrace();
795
        } catch (TException e) {
796
            e.printStackTrace();
797
        }
798
        return "N/A";
799
    }
3546 mandeep.dh 800
 
4277 anupam.sin 801
    public String updateOrderStatus() {
802
        long creatorId;
803
        OrderStatus orderStatusEnum = null;
804
        try {
805
            creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();
806
 
807
            if(orderStatus.equals("DENY_CANCEL_REQUEST")) {
808
                transactionServiceClient = new TransactionClient().getClient();
809
                transactionServiceClient.markOrderCancellationRequestDenied(Long.parseLong(orderId));
810
            } 
3546 mandeep.dh 811
 
4277 anupam.sin 812
            else {
3546 mandeep.dh 813
 
4277 anupam.sin 814
                orderStatusEnum = OrderStatus.valueOf(orderStatus);
815
                transactionServiceClient = new TransactionClient().getClient();
3578 mandeep.dh 816
 
4277 anupam.sin 817
                if (OrderStatus.SUBMITTED_FOR_PROCESSING.equals(orderStatusEnum)) {
818
                    transactionServiceClient.verifyOrder(Long.parseLong(orderId));
5348 anupam.sin 819
                    transactionServiceClient.updateCODAgent(currentAgentEmailId, Long.parseLong(orderId));
4277 anupam.sin 820
                }
4682 rajveer 821
                else if (OrderStatus.COD_VERIFICATION_FAILED.equals(orderStatusEnum)) {
4277 anupam.sin 822
                    transactionServiceClient.refundOrder(Long.parseLong(orderId), currentAgentEmailId, cancelReason);
5348 anupam.sin 823
                    transactionServiceClient.updateCODAgent(currentAgentEmailId, Long.parseLong(orderId));
4277 anupam.sin 824
                }
825
                else if (OrderStatus.CANCEL_REQUEST_CONFIRMED.equals(orderStatusEnum)) {
826
                    transactionServiceClient.markOrderCancellationRequestConfirmed(Long.parseLong(orderId));
827
                }
828
                else {
829
                    throw new UnsupportedOperationException("Cannot update status of orderId: " + orderId + " to: " + orderStatus);
830
                }
831
            }
832
 
833
            SearchFilter searchFilter = new SearchFilter();
834
            searchFilter.setTicketId(Long.parseLong(id));
835
            crmServiceClient = new CRMClient().getClient();
836
            Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
3546 mandeep.dh 837
 
4277 anupam.sin 838
            // Inserting activity
839
            Activity activity = new Activity();
840
            activity.setDescription("Status updated for Order Id: " + orderId
841
                    + " to " + orderStatus + ", Reason: " + cancelReason + body);
842
            activity.setType(ActivityType.OTHER);
843
            activity.setTicketPriority(ticket.getPriority());
844
            activity.setTicketStatus(ticket.getStatus());
845
            activity.setCreatorId(creatorId);
846
            activity.setTicketCategory(ticket.getCategory());
847
            activity.setTicketDescription(ticket.getDescription());
848
            activity.setCustomerId(ticket.getCustomerId());
849
            activity.setTicketAssigneeId(ticket.getAssigneeId());
3546 mandeep.dh 850
 
4277 anupam.sin 851
            crmServiceClient = new CRMClient().getClient();
852
            crmServiceClient.updateTicket(ticket, activity);
3546 mandeep.dh 853
 
4277 anupam.sin 854
        } catch (TException e) {
855
            log.info("Thrift exception", e);
856
        } catch (NumberFormatException e) {
857
            log.info("Could not convert to long", e);
858
        } catch (TransactionServiceException e) {
859
            log.info("Transaction client exception", e);
860
        }
861
 
3546 mandeep.dh 862
        return edit();
863
    }
4277 anupam.sin 864
 
3137 mandeep.dh 865
    public User getUser(Long userId) {
866
        User user = null;
867
 
868
        try {
3390 mandeep.dh 869
            userContextServiceClient = new UserClient().getClient();
3137 mandeep.dh 870
            user = userContextServiceClient.getUserById(userId);
871
        } catch (UserContextException e) {
872
            String errorString = "Could not fetch user for " + userId;
873
            log.error(errorString, e);
874
            addActionError(errorString);
875
        } catch (TException e) {
876
            String errorString = "Could not create client";
877
            log.error(errorString, e);
878
            addActionError(errorString);
879
        }
880
 
881
        return user;
882
    }
4267 anupam.sin 883
 
4438 anupam.sin 884
    public String allowPayment() {
885
        try {
886
            log.info("Allowing payment for paymentId = " + paymentId);
887
            transactionServiceClient = new TransactionClient().getClient();
888
            transactionServiceClient.markTransactionAsPaymentFlagRemoved(Long.parseLong(transactionId));
889
            if (id != null && !id.isEmpty()) {
890
                SearchFilter searchFilter = new SearchFilter();
891
                crmServiceClient = new CRMClient().getClient();
892
                searchFilter.setTicketId(Long.parseLong(id));
893
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
894
                // Inserting activity
895
                if(ticket != null) {
896
                    Activity activity = new Activity();
897
                    activity.setDescription("Flagged Payment allowed");
898
                    activity.setType(ActivityType.OTHER);
899
                    activity.setTicketPriority(ticket.getPriority());
900
                    activity.setTicketStatus(ticket.getStatus());
901
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
902
                    activity.setTicketCategory(ticket.getCategory());
903
                    activity.setTicketDescription(ticket.getDescription());
904
                    activity.setCustomerId(ticket.getCustomerId());
905
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
906
 
907
                    crmServiceClient = new CRMClient().getClient();
908
                    crmServiceClient.updateTicket(ticket, activity);
909
                }
910
            }
911
 
912
        } catch (TException e) {
913
            log.error("Error while getting tickets", e);
914
            return EXCEPTION;
915
        } catch (NumberFormatException e) {
916
            log.error("Invalid ticket Id", e);
917
            return EXCEPTION;
918
        } catch (TransactionServiceException e) {
919
            log.error("Error while marking transactions as Flag removed", e);
920
            return EXCEPTION;
921
        }
4267 anupam.sin 922
        return edit();
923
    }
924
 
4490 anupam.sin 925
    public String blockPayment() {
4438 anupam.sin 926
        try {
927
            log.info("Blocking payment for paymentId = " + paymentId);
928
            transactionServiceClient = new TransactionClient().getClient();
929
            transactionServiceClient.refundTransaction(Long.parseLong(transactionId), currentAgentEmailId, "FLAGGED_PAYMENT_CANCELLED");
930
            if (id != null && !id.isEmpty()) {
931
                SearchFilter searchFilter = new SearchFilter();
932
                crmServiceClient = new CRMClient().getClient();
933
                searchFilter.setTicketId(Long.parseLong(id));
934
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
935
                // Inserting activity
936
                if(ticket != null) {
937
                    Activity activity = new Activity();
938
                    activity.setDescription("Flagged Payment allowed");
939
                    activity.setType(ActivityType.OTHER);
940
                    activity.setTicketPriority(ticket.getPriority());
941
                    activity.setTicketStatus(ticket.getStatus());
942
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
943
                    activity.setTicketCategory(ticket.getCategory());
944
                    activity.setTicketDescription(ticket.getDescription());
945
                    activity.setCustomerId(ticket.getCustomerId());
946
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
947
 
948
                    crmServiceClient = new CRMClient().getClient();
949
                    crmServiceClient.updateTicket(ticket, activity);
950
                }
951
            }
952
 
953
        } catch (TException e) {
954
            log.error("Error while getting tickets", e);
955
            return EXCEPTION;
956
        } catch (NumberFormatException e) {
957
            log.error("Invalid ticket Id", e);
958
            return EXCEPTION;
959
        } catch (TransactionServiceException e) {
960
            log.error("Error while allowing flagged payment", e);
961
            return EXCEPTION;
962
        }
4267 anupam.sin 963
        return edit();
964
    }
4490 anupam.sin 965
 
966
    public String authorizeReturn() {
967
        try {
968
            log.info("Authorizing OrderId =" + orderId);
969
            transactionServiceClient = new TransactionClient().getClient();
970
            transactionServiceClient.markOrderReturnRequestAuthorized(Long.parseLong(orderId), true);
971
            if (id != null && !id.isEmpty()) {
972
                SearchFilter searchFilter = new SearchFilter();
973
                crmServiceClient = new CRMClient().getClient();
974
                searchFilter.setTicketId(Long.parseLong(id));
975
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
976
                // Inserting activity
977
                if(ticket != null) {
978
                    Activity activity = new Activity();
979
                    activity.setDescription("Return Authorized");
980
                    activity.setType(ActivityType.OTHER);
981
                    activity.setTicketPriority(ticket.getPriority());
982
                    activity.setTicketStatus(ticket.getStatus());
983
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
984
                    activity.setTicketCategory(ticket.getCategory());
985
                    activity.setTicketDescription(ticket.getDescription());
986
                    activity.setCustomerId(ticket.getCustomerId());
987
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
3137 mandeep.dh 988
 
4490 anupam.sin 989
                    crmServiceClient = new CRMClient().getClient();
990
                    crmServiceClient.updateTicket(ticket, activity);
991
                }
992
            }
993
 
994
        } catch (TException e) {
995
            log.error("Error while getting tickets", e);
996
            return EXCEPTION;
997
        } catch (NumberFormatException e) {
998
            log.error("Invalid ticket Id", e);
999
            return EXCEPTION;
1000
        } catch (TransactionServiceException e) {
1001
            log.error("Error while authorizing Return", e);
1002
            return EXCEPTION;
1003
        }
1004
        return edit();
1005
    }
1006
 
1007
    public String denyReturn() {
1008
        try {
1009
            log.info("Denying OrderId =" + orderId);
1010
            transactionServiceClient = new TransactionClient().getClient();
1011
            transactionServiceClient.markOrderReturnRequestAuthorized(Long.parseLong(orderId), false);
1012
            if (id != null && !id.isEmpty()) {
1013
                SearchFilter searchFilter = new SearchFilter();
1014
                crmServiceClient = new CRMClient().getClient();
1015
                searchFilter.setTicketId(Long.parseLong(id));
1016
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
1017
                // Inserting activity
1018
                if(ticket != null) {
1019
                    Activity activity = new Activity();
1020
                    activity.setDescription("Return Denied");
1021
                    activity.setType(ActivityType.OTHER);
1022
                    activity.setTicketPriority(ticket.getPriority());
1023
                    activity.setTicketStatus(ticket.getStatus());
1024
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
1025
                    activity.setTicketCategory(ticket.getCategory());
1026
                    activity.setTicketDescription(ticket.getDescription());
1027
                    activity.setCustomerId(ticket.getCustomerId());
1028
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
1029
 
1030
                    crmServiceClient = new CRMClient().getClient();
1031
                    crmServiceClient.updateTicket(ticket, activity);
1032
                }
1033
            }
1034
 
1035
        } catch (TException e) {
1036
            log.error("Error while getting tickets", e);
1037
            return EXCEPTION;
1038
        } catch (NumberFormatException e) {
1039
            log.error("Invalid ticket Id", e);
1040
            return EXCEPTION;
1041
        } catch (TransactionServiceException e) {
1042
            log.error("Error while denying Return", e);
1043
            return EXCEPTION;
1044
        }
1045
        return edit();
1046
    }
1047
 
1048
    public String authorizeDOA() {
1049
        try {
1050
            log.info("Authorizing OrderId =" + orderId);
1051
            transactionServiceClient = new TransactionClient().getClient();
1052
            transactionServiceClient.markOrderDoaRequestAuthorized(Long.parseLong(orderId), true);
1053
            if (id != null && !id.isEmpty()) {
1054
                SearchFilter searchFilter = new SearchFilter();
1055
                crmServiceClient = new CRMClient().getClient();
1056
                searchFilter.setTicketId(Long.parseLong(id));
1057
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
1058
                // Inserting activity
1059
                if(ticket != null) {
1060
                    Activity activity = new Activity();
1061
                    activity.setDescription("DOA Authorized");
1062
                    activity.setType(ActivityType.OTHER);
1063
                    activity.setTicketPriority(ticket.getPriority());
1064
                    activity.setTicketStatus(ticket.getStatus());
1065
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
1066
                    activity.setTicketCategory(ticket.getCategory());
1067
                    activity.setTicketDescription(ticket.getDescription());
1068
                    activity.setCustomerId(ticket.getCustomerId());
1069
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
1070
 
1071
                    crmServiceClient = new CRMClient().getClient();
1072
                    crmServiceClient.updateTicket(ticket, activity);
1073
                }
1074
            }
1075
 
1076
        } catch (TException e) {
1077
            log.error("Error while getting tickets", e);
1078
            return EXCEPTION;
1079
        } catch (NumberFormatException e) {
1080
            log.error("Invalid ticket Id", e);
1081
            return EXCEPTION;
1082
        } catch (TransactionServiceException e) {
1083
            log.error("Error while authorizing DOA", e);
1084
            return EXCEPTION;
1085
        }
1086
        return edit();
1087
    }
1088
 
1089
    public String denyDOA() {
1090
        try {
1091
            log.info("Denying OrderId =" + orderId);
1092
            transactionServiceClient = new TransactionClient().getClient();
1093
            transactionServiceClient.markOrderDoaRequestAuthorized(Long.parseLong(orderId), false);
1094
            if (id != null && !id.isEmpty()) {
1095
                SearchFilter searchFilter = new SearchFilter();
1096
                crmServiceClient = new CRMClient().getClient();
1097
                searchFilter.setTicketId(Long.parseLong(id));
1098
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
1099
                // Inserting activity
1100
                if(ticket != null) {
1101
                    Activity activity = new Activity();
1102
                    activity.setDescription("DOA Denied");
1103
                    activity.setType(ActivityType.OTHER);
1104
                    activity.setTicketPriority(ticket.getPriority());
1105
                    activity.setTicketStatus(ticket.getStatus());
1106
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
1107
                    activity.setTicketCategory(ticket.getCategory());
1108
                    activity.setTicketDescription(ticket.getDescription());
1109
                    activity.setCustomerId(ticket.getCustomerId());
1110
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
1111
 
1112
                    crmServiceClient = new CRMClient().getClient();
1113
                    crmServiceClient.updateTicket(ticket, activity);
1114
                }
1115
            }
1116
 
1117
        } catch (TException e) {
1118
            log.error("Error while getting tickets", e);
1119
            return EXCEPTION;
1120
        } catch (NumberFormatException e) {
1121
            log.error("Invalid ticket Id", e);
1122
            return EXCEPTION;
1123
        } catch (TransactionServiceException e) {
1124
            log.error("Error while denying DOA", e);
1125
            return EXCEPTION;
1126
        }
1127
        return edit();
1128
    }
5407 amar.kumar 1129
 
5909 amar.kumar 1130
    public String getAllOpenTickets() {
1131
    	try {
1132
    		ticketCategorycountMap= new HashMap<String, Long>();
1133
            crmServiceClient = new CRMClient().getClient();
1134
            ticketCategorycountMap = crmServiceClient.getOpenTicketsMap();
1135
        } catch (TException e) {
1136
            String errorString = "Error getting tickets for "
1137
                    + currentAgentEmailId;
1138
            log.error(errorString, e);
1139
            addActionError(errorString);
1140
        }
1141
        return OPEN;
1142
    }
1143
 
5407 amar.kumar 1144
    public double getCustomerTrustLevel(long userId) {
1145
    	try {
1146
			userContextServiceClient = new UserClient().getClient();
1147
			return userContextServiceClient.getTrustLevel(userId);
1148
		} catch (TException e) {
1149
			log.info("Exception while getting trustLevel for " +
1150
					"userId " + userId);
1151
		}
1152
		return 0;
1153
    }
4490 anupam.sin 1154
 
4020 mandeep.dh 1155
    public EscalationMatrix[] getEscalationMatrix () {
1156
        return CRMConstants.EscalationMatrix.values();
1157
    }
1158
 
3546 mandeep.dh 1159
    public String getAddress(Order order) {
1160
        return ModelUtils.extractAddressFromOrder(order);
1161
    }
1162
 
1163
    public String getProductName(LineItem lineItem) {
4008 mandeep.dh 1164
        return ModelUtils.extractProductNameFromLineItem(lineItem) + " " + lineItem.getColor();
3546 mandeep.dh 1165
    }
1166
 
3405 mandeep.dh 1167
    public ActivityType[] getActivityTypes() {
1168
        return ActivityType.values();
1169
    }
1170
 
1171
    public TicketStatus[] getTicketStatuses() {
1172
        return TicketStatus.values();
1173
    }
1174
 
3390 mandeep.dh 1175
    public Agent getAgent(long agentId) throws TException {
1176
        return CRMAuthorizingRealm.getAgent(agentId);
3137 mandeep.dh 1177
    }
1178
 
3339 mandeep.dh 1179
    public List<Agent> getAllAgents() {
3390 mandeep.dh 1180
        return CRMAuthorizingRealm.getAgents();
3339 mandeep.dh 1181
    }
1182
 
1183
    public TicketCategory[] getTicketCategories() {
1184
        return TicketCategory.values();
1185
    }
1186
 
1187
    public TicketPriority[] getTicketPriorities() {
1188
        return TicketPriority.values();
1189
    }
1190
 
3137 mandeep.dh 1191
    public List<Ticket> getTickets() {
1192
        return tickets;
1193
    }
1194
 
1195
    public void setTickets(List<Ticket> tickets) {
1196
        this.tickets = tickets;
1197
    }
3339 mandeep.dh 1198
 
3405 mandeep.dh 1199
    public String getCustomerEmailId() {
1200
        return customerEmailId;
3339 mandeep.dh 1201
    }
1202
 
3405 mandeep.dh 1203
    public void setCustomerEmailId(String customerEmailId) {
1204
        this.customerEmailId = customerEmailId;
3339 mandeep.dh 1205
    }
1206
 
1207
    public String getDescription() {
1208
        return description;
1209
    }
1210
 
1211
    public void setDescription(String description) {
1212
        this.description = description;
1213
    }
1214
 
1215
    public String getAssigneeEmailId() {
1216
        return assigneeEmailId;
1217
    }
1218
 
1219
    public void setAssigneeEmailId(String assigneeEmailId) {
1220
        this.assigneeEmailId = assigneeEmailId;
1221
    }
1222
 
1223
    public String getPriority() {
1224
        return priority;
1225
    }
1226
 
1227
    public void setPriority(String priority) {
1228
        this.priority = priority;
1229
    }
1230
 
1231
    public String getCategory() {
1232
        return category;
1233
    }
1234
 
1235
    public void setCategory(String category) {
1236
        this.category = category;
1237
    }
1238
 
1239
    public String getOrderId() {
1240
        return orderId;
1241
    }
1242
 
1243
    public void setOrderId(String orderId) {
1244
        this.orderId = orderId;
1245
    }
3397 mandeep.dh 1246
 
1247
    public String[] getAgentIds() {
1248
        return agentIds;
1249
    }
1250
 
1251
    public void setAgentIds(String[] agentIds) {
1252
        this.agentIds = agentIds;
1253
    }
1254
 
1255
    public String getStartTimestamp() {
1256
        return startTimestamp;
1257
    }
1258
 
1259
    public void setStartTimestamp(String startTimestamp) {
1260
        this.startTimestamp = startTimestamp;
1261
    }
1262
 
1263
    public String getEndTimestamp() {
1264
        return endTimestamp;
1265
    }
1266
 
1267
    public void setEndTimestamp(String endTimestamp) {
1268
        this.endTimestamp = endTimestamp;
1269
    }
3405 mandeep.dh 1270
 
1271
    public String getUserId() {
1272
        return userId;
1273
    }
1274
 
1275
    public void setUserId(String userId) {
1276
        this.userId = userId;
1277
    }
1278
 
1279
    public String getId() {
1280
        return id;
1281
    }
1282
 
1283
    public void setId(String id) {
1284
        this.id = id;
1285
    }
1286
 
1287
    public String getActivityDescription() {
1288
        return activityDescription;
1289
    }
1290
 
1291
    public void setActivityDescription(String activityDescription) {
1292
        this.activityDescription = activityDescription;
1293
    }
1294
 
1295
    public String getStatus() {
1296
        return status;
1297
    }
1298
 
1299
    public void setStatus(String status) {
1300
        this.status = status;
1301
    }
1302
 
1303
    public String getActivityType() {
1304
        return activityType;
1305
    }
1306
 
1307
    public void setActivityType(String activityType) {
1308
        this.activityType = activityType;
1309
    }
1310
 
1311
    public Ticket getTicket() {
1312
        return ticket;
1313
    }
1314
 
1315
    public void setTicket(Ticket ticket) {
1316
        this.ticket = ticket;
1317
    }
1318
 
1319
    public List<Activity> getActivities() {
1320
        return activities;
1321
    }
1322
 
1323
    public void setActivities(List<Activity> activities) {
1324
        this.activities = activities;
1325
    }
1326
 
1327
    public String getSubject() {
1328
        return subject;
1329
    }
1330
 
1331
    public void setSubject(String subject) {
1332
        this.subject = subject;
1333
    }
1334
 
1335
    public String getBody() {
1336
        return body;
1337
    }
1338
 
1339
    public void setBody(String body) {
1340
        this.body = body;
1341
    }
1342
 
1343
    public String getCustomerName() {
1344
        return customerName;
1345
    }
1346
 
1347
    public void setCustomerName(String customerName) {
1348
        this.customerName = customerName;
1349
    }
1350
 
1351
    public String getCustomerMobileNumber() {
1352
        return customerMobileNumber;
1353
    }
1354
 
1355
    public void setCustomerMobileNumber(String customerMobileNumber) {
1356
        this.customerMobileNumber = customerMobileNumber;
1357
    }
3546 mandeep.dh 1358
 
1359
    public User getUser() {
1360
        return user;
1361
    }
1362
 
1363
    public void setUser(User user) {
1364
        this.user = user;
1365
    }
1366
 
1367
    public String getOrderStatus() {
1368
        return orderStatus;
1369
    }
1370
 
1371
    public void setOrderStatus(String orderStatus) {
1372
        this.orderStatus = orderStatus;
1373
    }
1374
 
1375
    public List<Order> getOrders() {
1376
        return orders;
1377
    }
1378
 
1379
    public void setOrders(List<Order> orders) {
1380
        this.orders = orders;
1381
    }
4020 mandeep.dh 1382
 
1383
    public String getEscalate() {
1384
        return escalate;
1385
    }
1386
 
1387
    public void setEscalate(String escalate) {
1388
        this.escalate = escalate;
1389
    }
4241 anupam.sin 1390
 
1391
    public String getCancelReason() {
1392
        return cancelReason;
1393
    }
1394
 
1395
    public void setCancelReason(String cancelReason) {
1396
        this.cancelReason = cancelReason;
1397
    }
4267 anupam.sin 1398
 
1399
    public List<Payment> getPayments() {
1400
        return payments;
1401
    }
1402
 
1403
    public void setPayments(List<Payment> payments) {
1404
        this.payments = payments;
1405
    }
1406
 
1407
    public String getTransactionId() {
1408
        return transactionId;
1409
    }
1410
 
1411
    public void setTransactionId(String transactionId) {
1412
        this.transactionId = transactionId;
1413
    }
4438 anupam.sin 1414
 
1415
    public String getPaymentId() {
1416
        return paymentId;
1417
    }
1418
 
1419
    public void setPaymentId(String paymentId) {
1420
        this.paymentId = paymentId;
1421
    }
4490 anupam.sin 1422
 
1423
    public HashMap<String, String> getAllAttachmentsForTicket() {
1424
        return allAttachmentsForTicket;
1425
    }
1426
 
1427
    public void setAllAttachmentsForTicket(HashMap<String, String> allAttachmentsForTicket) {
1428
        this.allAttachmentsForTicket = allAttachmentsForTicket;
1429
    }
5791 anupam.sin 1430
 
1431
    public String getPickupExtension() {
1432
        return pickupExtension;
1433
    }
1434
 
1435
    public void setPickupExtension(String pickupExtension) {
1436
        this.pickupExtension = pickupExtension;
1437
    }
5909 amar.kumar 1438
 
1439
	public Map<String, Long> getTicketCategorycountMap() {
1440
		return ticketCategorycountMap;
1441
	}
1442
 
1443
	public void setTicketCategorycountMap(
1444
			Map<String, Long> ticketCategorycountMap) {
1445
		this.ticketCategorycountMap = ticketCategorycountMap;
1446
	}
3137 mandeep.dh 1447
}