Subversion Repositories SmartDukaan

Rev

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