Subversion Repositories SmartDukaan

Rev

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