Subversion Repositories SmartDukaan

Rev

Rev 5865 | Rev 5917 | 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);
729
        } catch (NumberFormatException e) {
730
            e.printStackTrace();
731
        } catch (TException e) {
732
            e.printStackTrace();
733
        }
734
        return EDIT;
735
    }
736
 
737
    public String getPickupExpiryDate(long id, long deliveryTimestamp) {
738
        try {
739
            log.info("orderID" + id);
740
            log.info("Delivery Time : " + deliveryTimestamp);
741
            Date deliveryDate = new Date(deliveryTimestamp);
742
            Calendar cal = Calendar.getInstance();
743
            cal.setTime(deliveryDate);
744
 
745
            log.info("Orig Del Date :" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR));
746
 
747
            cal.add(Calendar.DATE, 5);
748
 
749
            log.info("After adding 5 days" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR));
750
 
751
            transactionServiceClient = new TransactionClient().getClient();
752
            List<Attribute> attributes = new ArrayList<Attribute>();
753
            String extendedBy = "0";
754
            attributes = transactionServiceClient.getAllAttributesForOrderId(id);
755
            for (Attribute attribute : attributes) {
756
                if (attribute.getName().equals("pickupExtension")) {
757
                    extendedBy = attribute.getValue();
758
                }
759
            }
760
            cal.add(Calendar.DATE, Integer.parseInt(extendedBy));
761
 
762
            log.info("Extended by" + extendedBy);
763
 
764
            log.info("Final : " + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR));
765
 
766
            SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy");
767
            return SDF.format(cal.getTime());
768
 
769
        } catch (NumberFormatException e) {
770
            e.printStackTrace();
771
        } catch (TException e) {
772
            e.printStackTrace();
773
        }
774
        return "N/A";
775
    }
3546 mandeep.dh 776
 
4277 anupam.sin 777
    public String updateOrderStatus() {
778
        long creatorId;
779
        OrderStatus orderStatusEnum = null;
780
        try {
781
            creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();
782
 
783
            if(orderStatus.equals("DENY_CANCEL_REQUEST")) {
784
                transactionServiceClient = new TransactionClient().getClient();
785
                transactionServiceClient.markOrderCancellationRequestDenied(Long.parseLong(orderId));
786
            } 
3546 mandeep.dh 787
 
4277 anupam.sin 788
            else {
3546 mandeep.dh 789
 
4277 anupam.sin 790
                orderStatusEnum = OrderStatus.valueOf(orderStatus);
791
                transactionServiceClient = new TransactionClient().getClient();
3578 mandeep.dh 792
 
4277 anupam.sin 793
                if (OrderStatus.SUBMITTED_FOR_PROCESSING.equals(orderStatusEnum)) {
794
                    transactionServiceClient.verifyOrder(Long.parseLong(orderId));
5348 anupam.sin 795
                    transactionServiceClient.updateCODAgent(currentAgentEmailId, Long.parseLong(orderId));
4277 anupam.sin 796
                }
4682 rajveer 797
                else if (OrderStatus.COD_VERIFICATION_FAILED.equals(orderStatusEnum)) {
4277 anupam.sin 798
                    transactionServiceClient.refundOrder(Long.parseLong(orderId), currentAgentEmailId, cancelReason);
5348 anupam.sin 799
                    transactionServiceClient.updateCODAgent(currentAgentEmailId, Long.parseLong(orderId));
4277 anupam.sin 800
                }
801
                else if (OrderStatus.CANCEL_REQUEST_CONFIRMED.equals(orderStatusEnum)) {
802
                    transactionServiceClient.markOrderCancellationRequestConfirmed(Long.parseLong(orderId));
803
                }
804
                else {
805
                    throw new UnsupportedOperationException("Cannot update status of orderId: " + orderId + " to: " + orderStatus);
806
                }
807
            }
808
 
809
            SearchFilter searchFilter = new SearchFilter();
810
            searchFilter.setTicketId(Long.parseLong(id));
811
            crmServiceClient = new CRMClient().getClient();
812
            Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
3546 mandeep.dh 813
 
4277 anupam.sin 814
            // Inserting activity
815
            Activity activity = new Activity();
816
            activity.setDescription("Status updated for Order Id: " + orderId
817
                    + " to " + orderStatus + ", Reason: " + cancelReason + body);
818
            activity.setType(ActivityType.OTHER);
819
            activity.setTicketPriority(ticket.getPriority());
820
            activity.setTicketStatus(ticket.getStatus());
821
            activity.setCreatorId(creatorId);
822
            activity.setTicketCategory(ticket.getCategory());
823
            activity.setTicketDescription(ticket.getDescription());
824
            activity.setCustomerId(ticket.getCustomerId());
825
            activity.setTicketAssigneeId(ticket.getAssigneeId());
3546 mandeep.dh 826
 
4277 anupam.sin 827
            crmServiceClient = new CRMClient().getClient();
828
            crmServiceClient.updateTicket(ticket, activity);
3546 mandeep.dh 829
 
4277 anupam.sin 830
        } catch (TException e) {
831
            log.info("Thrift exception", e);
832
        } catch (NumberFormatException e) {
833
            log.info("Could not convert to long", e);
834
        } catch (TransactionServiceException e) {
835
            log.info("Transaction client exception", e);
836
        }
837
 
3546 mandeep.dh 838
        return edit();
839
    }
4277 anupam.sin 840
 
3137 mandeep.dh 841
    public User getUser(Long userId) {
842
        User user = null;
843
 
844
        try {
3390 mandeep.dh 845
            userContextServiceClient = new UserClient().getClient();
3137 mandeep.dh 846
            user = userContextServiceClient.getUserById(userId);
847
        } catch (UserContextException e) {
848
            String errorString = "Could not fetch user for " + userId;
849
            log.error(errorString, e);
850
            addActionError(errorString);
851
        } catch (TException e) {
852
            String errorString = "Could not create client";
853
            log.error(errorString, e);
854
            addActionError(errorString);
855
        }
856
 
857
        return user;
858
    }
4267 anupam.sin 859
 
4438 anupam.sin 860
    public String allowPayment() {
861
        try {
862
            log.info("Allowing payment for paymentId = " + paymentId);
863
            transactionServiceClient = new TransactionClient().getClient();
864
            transactionServiceClient.markTransactionAsPaymentFlagRemoved(Long.parseLong(transactionId));
865
            if (id != null && !id.isEmpty()) {
866
                SearchFilter searchFilter = new SearchFilter();
867
                crmServiceClient = new CRMClient().getClient();
868
                searchFilter.setTicketId(Long.parseLong(id));
869
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
870
                // Inserting activity
871
                if(ticket != null) {
872
                    Activity activity = new Activity();
873
                    activity.setDescription("Flagged Payment allowed");
874
                    activity.setType(ActivityType.OTHER);
875
                    activity.setTicketPriority(ticket.getPriority());
876
                    activity.setTicketStatus(ticket.getStatus());
877
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
878
                    activity.setTicketCategory(ticket.getCategory());
879
                    activity.setTicketDescription(ticket.getDescription());
880
                    activity.setCustomerId(ticket.getCustomerId());
881
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
882
 
883
                    crmServiceClient = new CRMClient().getClient();
884
                    crmServiceClient.updateTicket(ticket, activity);
885
                }
886
            }
887
 
888
        } catch (TException e) {
889
            log.error("Error while getting tickets", e);
890
            return EXCEPTION;
891
        } catch (NumberFormatException e) {
892
            log.error("Invalid ticket Id", e);
893
            return EXCEPTION;
894
        } catch (TransactionServiceException e) {
895
            log.error("Error while marking transactions as Flag removed", e);
896
            return EXCEPTION;
897
        }
4267 anupam.sin 898
        return edit();
899
    }
900
 
4490 anupam.sin 901
    public String blockPayment() {
4438 anupam.sin 902
        try {
903
            log.info("Blocking payment for paymentId = " + paymentId);
904
            transactionServiceClient = new TransactionClient().getClient();
905
            transactionServiceClient.refundTransaction(Long.parseLong(transactionId), currentAgentEmailId, "FLAGGED_PAYMENT_CANCELLED");
906
            if (id != null && !id.isEmpty()) {
907
                SearchFilter searchFilter = new SearchFilter();
908
                crmServiceClient = new CRMClient().getClient();
909
                searchFilter.setTicketId(Long.parseLong(id));
910
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
911
                // Inserting activity
912
                if(ticket != null) {
913
                    Activity activity = new Activity();
914
                    activity.setDescription("Flagged Payment allowed");
915
                    activity.setType(ActivityType.OTHER);
916
                    activity.setTicketPriority(ticket.getPriority());
917
                    activity.setTicketStatus(ticket.getStatus());
918
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
919
                    activity.setTicketCategory(ticket.getCategory());
920
                    activity.setTicketDescription(ticket.getDescription());
921
                    activity.setCustomerId(ticket.getCustomerId());
922
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
923
 
924
                    crmServiceClient = new CRMClient().getClient();
925
                    crmServiceClient.updateTicket(ticket, activity);
926
                }
927
            }
928
 
929
        } catch (TException e) {
930
            log.error("Error while getting tickets", e);
931
            return EXCEPTION;
932
        } catch (NumberFormatException e) {
933
            log.error("Invalid ticket Id", e);
934
            return EXCEPTION;
935
        } catch (TransactionServiceException e) {
936
            log.error("Error while allowing flagged payment", e);
937
            return EXCEPTION;
938
        }
4267 anupam.sin 939
        return edit();
940
    }
4490 anupam.sin 941
 
942
    public String authorizeReturn() {
943
        try {
944
            log.info("Authorizing OrderId =" + orderId);
945
            transactionServiceClient = new TransactionClient().getClient();
946
            transactionServiceClient.markOrderReturnRequestAuthorized(Long.parseLong(orderId), true);
947
            if (id != null && !id.isEmpty()) {
948
                SearchFilter searchFilter = new SearchFilter();
949
                crmServiceClient = new CRMClient().getClient();
950
                searchFilter.setTicketId(Long.parseLong(id));
951
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
952
                // Inserting activity
953
                if(ticket != null) {
954
                    Activity activity = new Activity();
955
                    activity.setDescription("Return Authorized");
956
                    activity.setType(ActivityType.OTHER);
957
                    activity.setTicketPriority(ticket.getPriority());
958
                    activity.setTicketStatus(ticket.getStatus());
959
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
960
                    activity.setTicketCategory(ticket.getCategory());
961
                    activity.setTicketDescription(ticket.getDescription());
962
                    activity.setCustomerId(ticket.getCustomerId());
963
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
3137 mandeep.dh 964
 
4490 anupam.sin 965
                    crmServiceClient = new CRMClient().getClient();
966
                    crmServiceClient.updateTicket(ticket, activity);
967
                }
968
            }
969
 
970
        } catch (TException e) {
971
            log.error("Error while getting tickets", e);
972
            return EXCEPTION;
973
        } catch (NumberFormatException e) {
974
            log.error("Invalid ticket Id", e);
975
            return EXCEPTION;
976
        } catch (TransactionServiceException e) {
977
            log.error("Error while authorizing Return", e);
978
            return EXCEPTION;
979
        }
980
        return edit();
981
    }
982
 
983
    public String denyReturn() {
984
        try {
985
            log.info("Denying OrderId =" + orderId);
986
            transactionServiceClient = new TransactionClient().getClient();
987
            transactionServiceClient.markOrderReturnRequestAuthorized(Long.parseLong(orderId), false);
988
            if (id != null && !id.isEmpty()) {
989
                SearchFilter searchFilter = new SearchFilter();
990
                crmServiceClient = new CRMClient().getClient();
991
                searchFilter.setTicketId(Long.parseLong(id));
992
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
993
                // Inserting activity
994
                if(ticket != null) {
995
                    Activity activity = new Activity();
996
                    activity.setDescription("Return Denied");
997
                    activity.setType(ActivityType.OTHER);
998
                    activity.setTicketPriority(ticket.getPriority());
999
                    activity.setTicketStatus(ticket.getStatus());
1000
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
1001
                    activity.setTicketCategory(ticket.getCategory());
1002
                    activity.setTicketDescription(ticket.getDescription());
1003
                    activity.setCustomerId(ticket.getCustomerId());
1004
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
1005
 
1006
                    crmServiceClient = new CRMClient().getClient();
1007
                    crmServiceClient.updateTicket(ticket, activity);
1008
                }
1009
            }
1010
 
1011
        } catch (TException e) {
1012
            log.error("Error while getting tickets", e);
1013
            return EXCEPTION;
1014
        } catch (NumberFormatException e) {
1015
            log.error("Invalid ticket Id", e);
1016
            return EXCEPTION;
1017
        } catch (TransactionServiceException e) {
1018
            log.error("Error while denying Return", e);
1019
            return EXCEPTION;
1020
        }
1021
        return edit();
1022
    }
1023
 
1024
    public String authorizeDOA() {
1025
        try {
1026
            log.info("Authorizing OrderId =" + orderId);
1027
            transactionServiceClient = new TransactionClient().getClient();
1028
            transactionServiceClient.markOrderDoaRequestAuthorized(Long.parseLong(orderId), true);
1029
            if (id != null && !id.isEmpty()) {
1030
                SearchFilter searchFilter = new SearchFilter();
1031
                crmServiceClient = new CRMClient().getClient();
1032
                searchFilter.setTicketId(Long.parseLong(id));
1033
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
1034
                // Inserting activity
1035
                if(ticket != null) {
1036
                    Activity activity = new Activity();
1037
                    activity.setDescription("DOA Authorized");
1038
                    activity.setType(ActivityType.OTHER);
1039
                    activity.setTicketPriority(ticket.getPriority());
1040
                    activity.setTicketStatus(ticket.getStatus());
1041
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
1042
                    activity.setTicketCategory(ticket.getCategory());
1043
                    activity.setTicketDescription(ticket.getDescription());
1044
                    activity.setCustomerId(ticket.getCustomerId());
1045
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
1046
 
1047
                    crmServiceClient = new CRMClient().getClient();
1048
                    crmServiceClient.updateTicket(ticket, activity);
1049
                }
1050
            }
1051
 
1052
        } catch (TException e) {
1053
            log.error("Error while getting tickets", e);
1054
            return EXCEPTION;
1055
        } catch (NumberFormatException e) {
1056
            log.error("Invalid ticket Id", e);
1057
            return EXCEPTION;
1058
        } catch (TransactionServiceException e) {
1059
            log.error("Error while authorizing DOA", e);
1060
            return EXCEPTION;
1061
        }
1062
        return edit();
1063
    }
1064
 
1065
    public String denyDOA() {
1066
        try {
1067
            log.info("Denying OrderId =" + orderId);
1068
            transactionServiceClient = new TransactionClient().getClient();
1069
            transactionServiceClient.markOrderDoaRequestAuthorized(Long.parseLong(orderId), false);
1070
            if (id != null && !id.isEmpty()) {
1071
                SearchFilter searchFilter = new SearchFilter();
1072
                crmServiceClient = new CRMClient().getClient();
1073
                searchFilter.setTicketId(Long.parseLong(id));
1074
                Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
1075
                // Inserting activity
1076
                if(ticket != null) {
1077
                    Activity activity = new Activity();
1078
                    activity.setDescription("DOA Denied");
1079
                    activity.setType(ActivityType.OTHER);
1080
                    activity.setTicketPriority(ticket.getPriority());
1081
                    activity.setTicketStatus(ticket.getStatus());
1082
                    activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
1083
                    activity.setTicketCategory(ticket.getCategory());
1084
                    activity.setTicketDescription(ticket.getDescription());
1085
                    activity.setCustomerId(ticket.getCustomerId());
1086
                    activity.setTicketAssigneeId(ticket.getAssigneeId());
1087
 
1088
                    crmServiceClient = new CRMClient().getClient();
1089
                    crmServiceClient.updateTicket(ticket, activity);
1090
                }
1091
            }
1092
 
1093
        } catch (TException e) {
1094
            log.error("Error while getting tickets", e);
1095
            return EXCEPTION;
1096
        } catch (NumberFormatException e) {
1097
            log.error("Invalid ticket Id", e);
1098
            return EXCEPTION;
1099
        } catch (TransactionServiceException e) {
1100
            log.error("Error while denying DOA", e);
1101
            return EXCEPTION;
1102
        }
1103
        return edit();
1104
    }
5407 amar.kumar 1105
 
5909 amar.kumar 1106
    public String getAllOpenTickets() {
1107
    	try {
1108
    		ticketCategorycountMap= new HashMap<String, Long>();
1109
            crmServiceClient = new CRMClient().getClient();
1110
            ticketCategorycountMap = crmServiceClient.getOpenTicketsMap();
1111
        } catch (TException e) {
1112
            String errorString = "Error getting tickets for "
1113
                    + currentAgentEmailId;
1114
            log.error(errorString, e);
1115
            addActionError(errorString);
1116
        }
1117
        return OPEN;
1118
    }
1119
 
5407 amar.kumar 1120
    public double getCustomerTrustLevel(long userId) {
1121
    	try {
1122
			userContextServiceClient = new UserClient().getClient();
1123
			return userContextServiceClient.getTrustLevel(userId);
1124
		} catch (TException e) {
1125
			log.info("Exception while getting trustLevel for " +
1126
					"userId " + userId);
1127
		}
1128
		return 0;
1129
    }
4490 anupam.sin 1130
 
4020 mandeep.dh 1131
    public EscalationMatrix[] getEscalationMatrix () {
1132
        return CRMConstants.EscalationMatrix.values();
1133
    }
1134
 
3546 mandeep.dh 1135
    public String getAddress(Order order) {
1136
        return ModelUtils.extractAddressFromOrder(order);
1137
    }
1138
 
1139
    public String getProductName(LineItem lineItem) {
4008 mandeep.dh 1140
        return ModelUtils.extractProductNameFromLineItem(lineItem) + " " + lineItem.getColor();
3546 mandeep.dh 1141
    }
1142
 
3405 mandeep.dh 1143
    public ActivityType[] getActivityTypes() {
1144
        return ActivityType.values();
1145
    }
1146
 
1147
    public TicketStatus[] getTicketStatuses() {
1148
        return TicketStatus.values();
1149
    }
1150
 
3390 mandeep.dh 1151
    public Agent getAgent(long agentId) throws TException {
1152
        return CRMAuthorizingRealm.getAgent(agentId);
3137 mandeep.dh 1153
    }
1154
 
3339 mandeep.dh 1155
    public List<Agent> getAllAgents() {
3390 mandeep.dh 1156
        return CRMAuthorizingRealm.getAgents();
3339 mandeep.dh 1157
    }
1158
 
1159
    public TicketCategory[] getTicketCategories() {
1160
        return TicketCategory.values();
1161
    }
1162
 
1163
    public TicketPriority[] getTicketPriorities() {
1164
        return TicketPriority.values();
1165
    }
1166
 
3137 mandeep.dh 1167
    public List<Ticket> getTickets() {
1168
        return tickets;
1169
    }
1170
 
1171
    public void setTickets(List<Ticket> tickets) {
1172
        this.tickets = tickets;
1173
    }
3339 mandeep.dh 1174
 
3405 mandeep.dh 1175
    public String getCustomerEmailId() {
1176
        return customerEmailId;
3339 mandeep.dh 1177
    }
1178
 
3405 mandeep.dh 1179
    public void setCustomerEmailId(String customerEmailId) {
1180
        this.customerEmailId = customerEmailId;
3339 mandeep.dh 1181
    }
1182
 
1183
    public String getDescription() {
1184
        return description;
1185
    }
1186
 
1187
    public void setDescription(String description) {
1188
        this.description = description;
1189
    }
1190
 
1191
    public String getAssigneeEmailId() {
1192
        return assigneeEmailId;
1193
    }
1194
 
1195
    public void setAssigneeEmailId(String assigneeEmailId) {
1196
        this.assigneeEmailId = assigneeEmailId;
1197
    }
1198
 
1199
    public String getPriority() {
1200
        return priority;
1201
    }
1202
 
1203
    public void setPriority(String priority) {
1204
        this.priority = priority;
1205
    }
1206
 
1207
    public String getCategory() {
1208
        return category;
1209
    }
1210
 
1211
    public void setCategory(String category) {
1212
        this.category = category;
1213
    }
1214
 
1215
    public String getOrderId() {
1216
        return orderId;
1217
    }
1218
 
1219
    public void setOrderId(String orderId) {
1220
        this.orderId = orderId;
1221
    }
3397 mandeep.dh 1222
 
1223
    public String[] getAgentIds() {
1224
        return agentIds;
1225
    }
1226
 
1227
    public void setAgentIds(String[] agentIds) {
1228
        this.agentIds = agentIds;
1229
    }
1230
 
1231
    public String getStartTimestamp() {
1232
        return startTimestamp;
1233
    }
1234
 
1235
    public void setStartTimestamp(String startTimestamp) {
1236
        this.startTimestamp = startTimestamp;
1237
    }
1238
 
1239
    public String getEndTimestamp() {
1240
        return endTimestamp;
1241
    }
1242
 
1243
    public void setEndTimestamp(String endTimestamp) {
1244
        this.endTimestamp = endTimestamp;
1245
    }
3405 mandeep.dh 1246
 
1247
    public String getUserId() {
1248
        return userId;
1249
    }
1250
 
1251
    public void setUserId(String userId) {
1252
        this.userId = userId;
1253
    }
1254
 
1255
    public String getId() {
1256
        return id;
1257
    }
1258
 
1259
    public void setId(String id) {
1260
        this.id = id;
1261
    }
1262
 
1263
    public String getActivityDescription() {
1264
        return activityDescription;
1265
    }
1266
 
1267
    public void setActivityDescription(String activityDescription) {
1268
        this.activityDescription = activityDescription;
1269
    }
1270
 
1271
    public String getStatus() {
1272
        return status;
1273
    }
1274
 
1275
    public void setStatus(String status) {
1276
        this.status = status;
1277
    }
1278
 
1279
    public String getActivityType() {
1280
        return activityType;
1281
    }
1282
 
1283
    public void setActivityType(String activityType) {
1284
        this.activityType = activityType;
1285
    }
1286
 
1287
    public Ticket getTicket() {
1288
        return ticket;
1289
    }
1290
 
1291
    public void setTicket(Ticket ticket) {
1292
        this.ticket = ticket;
1293
    }
1294
 
1295
    public List<Activity> getActivities() {
1296
        return activities;
1297
    }
1298
 
1299
    public void setActivities(List<Activity> activities) {
1300
        this.activities = activities;
1301
    }
1302
 
1303
    public String getSubject() {
1304
        return subject;
1305
    }
1306
 
1307
    public void setSubject(String subject) {
1308
        this.subject = subject;
1309
    }
1310
 
1311
    public String getBody() {
1312
        return body;
1313
    }
1314
 
1315
    public void setBody(String body) {
1316
        this.body = body;
1317
    }
1318
 
1319
    public String getCustomerName() {
1320
        return customerName;
1321
    }
1322
 
1323
    public void setCustomerName(String customerName) {
1324
        this.customerName = customerName;
1325
    }
1326
 
1327
    public String getCustomerMobileNumber() {
1328
        return customerMobileNumber;
1329
    }
1330
 
1331
    public void setCustomerMobileNumber(String customerMobileNumber) {
1332
        this.customerMobileNumber = customerMobileNumber;
1333
    }
3546 mandeep.dh 1334
 
1335
    public User getUser() {
1336
        return user;
1337
    }
1338
 
1339
    public void setUser(User user) {
1340
        this.user = user;
1341
    }
1342
 
1343
    public String getOrderStatus() {
1344
        return orderStatus;
1345
    }
1346
 
1347
    public void setOrderStatus(String orderStatus) {
1348
        this.orderStatus = orderStatus;
1349
    }
1350
 
1351
    public List<Order> getOrders() {
1352
        return orders;
1353
    }
1354
 
1355
    public void setOrders(List<Order> orders) {
1356
        this.orders = orders;
1357
    }
4020 mandeep.dh 1358
 
1359
    public String getEscalate() {
1360
        return escalate;
1361
    }
1362
 
1363
    public void setEscalate(String escalate) {
1364
        this.escalate = escalate;
1365
    }
4241 anupam.sin 1366
 
1367
    public String getCancelReason() {
1368
        return cancelReason;
1369
    }
1370
 
1371
    public void setCancelReason(String cancelReason) {
1372
        this.cancelReason = cancelReason;
1373
    }
4267 anupam.sin 1374
 
1375
    public List<Payment> getPayments() {
1376
        return payments;
1377
    }
1378
 
1379
    public void setPayments(List<Payment> payments) {
1380
        this.payments = payments;
1381
    }
1382
 
1383
    public String getTransactionId() {
1384
        return transactionId;
1385
    }
1386
 
1387
    public void setTransactionId(String transactionId) {
1388
        this.transactionId = transactionId;
1389
    }
4438 anupam.sin 1390
 
1391
    public String getPaymentId() {
1392
        return paymentId;
1393
    }
1394
 
1395
    public void setPaymentId(String paymentId) {
1396
        this.paymentId = paymentId;
1397
    }
4490 anupam.sin 1398
 
1399
    public HashMap<String, String> getAllAttachmentsForTicket() {
1400
        return allAttachmentsForTicket;
1401
    }
1402
 
1403
    public void setAllAttachmentsForTicket(HashMap<String, String> allAttachmentsForTicket) {
1404
        this.allAttachmentsForTicket = allAttachmentsForTicket;
1405
    }
5791 anupam.sin 1406
 
1407
    public String getPickupExtension() {
1408
        return pickupExtension;
1409
    }
1410
 
1411
    public void setPickupExtension(String pickupExtension) {
1412
        this.pickupExtension = pickupExtension;
1413
    }
5909 amar.kumar 1414
 
1415
	public Map<String, Long> getTicketCategorycountMap() {
1416
		return ticketCategorycountMap;
1417
	}
1418
 
1419
	public void setTicketCategorycountMap(
1420
			Map<String, Long> ticketCategorycountMap) {
1421
		this.ticketCategorycountMap = ticketCategorycountMap;
1422
	}
3137 mandeep.dh 1423
}