Subversion Repositories SmartDukaan

Rev

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