Subversion Repositories SmartDukaan

Rev

Rev 4277 | Rev 4438 | 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;
3546 mandeep.dh 14
import in.shop2020.model.v1.order.LineItem;
15
import in.shop2020.model.v1.order.Order;
16
import in.shop2020.model.v1.order.OrderStatus;
4267 anupam.sin 17
import in.shop2020.model.v1.order.Transaction;
3546 mandeep.dh 18
import in.shop2020.model.v1.order.TransactionServiceException;
4267 anupam.sin 19
import in.shop2020.model.v1.order.TransactionStatus;
3137 mandeep.dh 20
import in.shop2020.model.v1.user.User;
21
import in.shop2020.model.v1.user.UserContextException;
4267 anupam.sin 22
import in.shop2020.payments.Attribute;
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;
4241 anupam.sin 34
import in.shop2020.util.CRMConstants.CODCancelMatrix;
3405 mandeep.dh 35
import in.shop2020.utils.HelperService.Client;
36
import in.shop2020.utils.HelperServiceException;
3546 mandeep.dh 37
import in.shop2020.utils.ModelUtils;
3137 mandeep.dh 38
 
3397 mandeep.dh 39
import java.text.ParseException;
3137 mandeep.dh 40
import java.util.ArrayList;
3578 mandeep.dh 41
import java.util.Calendar;
3390 mandeep.dh 42
import java.util.Collections;
3405 mandeep.dh 43
import java.util.Date;
3137 mandeep.dh 44
import java.util.List;
45
 
4256 mandeep.dh 46
import net.htmlparser.jericho.Source;
47
 
3405 mandeep.dh 48
import org.apache.shiro.SecurityUtils;
3137 mandeep.dh 49
import org.apache.thrift.TException;
3546 mandeep.dh 50
import org.apache.thrift.transport.TTransportException;
3137 mandeep.dh 51
 
52
/**
53
 * @author mandeep
54
 * 
55
 */
56
public class TicketsController extends BaseController {
57
 
58
    /**
59
     * 
60
     */
61
    private static final long serialVersionUID = 1L;
62
 
63
    List<Ticket>              tickets          = new ArrayList<Ticket>();
3405 mandeep.dh 64
    private String            customerEmailId;
3339 mandeep.dh 65
    private String            description;
66
    private String            assigneeEmailId;
67
    private String            priority;
68
    private String            category;
69
    private String            orderId;
3397 mandeep.dh 70
    private String[]          agentIds;
71
    private String            startTimestamp;
72
    private String            endTimestamp;
3137 mandeep.dh 73
 
3405 mandeep.dh 74
    private String            userId;
75
    private String            id;
76
    private String            activityDescription;
77
    private String            status;
78
    private String            activityType;
79
    private Ticket            ticket;
80
    private List<Activity>    activities;
81
    private String            subject;
82
    private String            body;
83
    private String            customerName;
84
    private String            customerMobileNumber;
3546 mandeep.dh 85
    private User              user;
86
    private String            orderStatus;
87
    private List<Order>       orders;
4267 anupam.sin 88
    private List<Payment>     payments;
4020 mandeep.dh 89
    private String            escalate;
4241 anupam.sin 90
    private String            cancelReason;
4267 anupam.sin 91
    private String            transactionId;
3405 mandeep.dh 92
 
93
    public String index() {
94
        try {
95
            if (id != null && !id.isEmpty()) {
96
                SearchFilter searchFilter = new SearchFilter();
97
                crmServiceClient = new CRMClient().getClient();
98
                searchFilter.setTicketId(Long.parseLong(id));
99
                tickets = crmServiceClient.getTickets(searchFilter);
100
            }
101
        } catch (TException e) {
102
            log.error("Error while getting tickets", e);
103
            return EXCEPTION;
104
        }
105
 
106
        return INDEX;
107
    }
108
 
109
    public String edit() {
110
        try {
111
            long ticketId = Long.parseLong(id);
112
            SearchFilter searchFilter = new SearchFilter();
113
            searchFilter.setTicketId(ticketId);
114
            crmServiceClient = new CRMClient().getClient();
115
            ticket = crmServiceClient.getTickets(searchFilter).get(0);
116
            activities = crmServiceClient.getActivities(searchFilter);
117
            subject = createSubjectString(ticket);
118
 
119
            if (ticket.isSetCustomerId()) {
120
                userId = String.valueOf(ticket.getCustomerId());
3546 mandeep.dh 121
                userContextServiceClient = new UserClient().getClient();
122
 
123
                try {
124
                    user = userContextServiceClient.getUserById(ticket
125
                            .getCustomerId());
126
                } catch (UserContextException e) {
127
                    log.error("Could not fetch user details", e);
128
                }
3405 mandeep.dh 129
            }
130
 
131
            customerEmailId = ticket.getCustomerEmailId();
4277 anupam.sin 132
 
133
            if (TicketCategory.ORDER_CANCELLATION.equals(ticket.getCategory())) {
134
                log.info("Fetching orders for " + ticket.getCustomerId());
135
                orders = new ArrayList<Order>();
136
                transactionServiceClient = new TransactionClient().getClient();
137
                Order order = transactionServiceClient.getOrder(ticket.getOrderId());
138
                log.info("Processing Order: " + order);
4287 anupam.sin 139
                if(OrderStatus.CANCEL_REQUEST_RECEIVED.equals(order.getStatus())) {
140
                    orders.add(order);
141
                }
4277 anupam.sin 142
            }
3546 mandeep.dh 143
 
144
            if (TicketCategory.COD_VERIFICATION.equals(ticket.getCategory())) {
145
                log.info("Fetching orders for " + ticket.getCustomerId());
146
                orders = new ArrayList<Order>();
147
                transactionServiceClient = new TransactionClient().getClient();
148
                for (Order order : transactionServiceClient
149
                        .getOrdersForCustomer(ticket.getCustomerId(), 0, 0,
150
                                Collections.singletonList(OrderStatus.INIT))) {
151
                    log.info("Processing Order: " + order);
152
                    if (order.isCod()) {
153
                        orders.add(order);
154
                    }
155
                }
156
            }
3578 mandeep.dh 157
 
4267 anupam.sin 158
            if (TicketCategory.PAYMENT_FLAGGED.equals(ticket.getCategory())) {
159
                log.info("Fetching orders for " + ticket.getCustomerId());
160
                payments = new ArrayList<Payment>();
161
                in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
162
                transactionServiceClient = new TransactionClient().getClient();
163
                for (Transaction transaction : transactionServiceClient
164
                        .getTransactionsForCustomer(ticket.getCustomerId(), 0, 0, TransactionStatus.FLAGGED)) {
165
                    log.info("Processing Transaction: " + transaction);
166
                    for ( Payment payment : paymentClient.getPaymentForTxnId(transaction.getId())) {
167
                        if( payment.getStatus().equals(PaymentStatus.AUTHORIZED)) {
168
                            payments.add(payment);
169
                        }
170
                    }
171
                }
172
            }
173
 
3578 mandeep.dh 174
            if (TicketCategory.FAILED_PAYMENTS.equals(ticket.getCategory())) {
175
                log.info("Fetching orders for " + ticket.getCustomerId());
176
                orders = new ArrayList<Order>();
177
                in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
178
 
179
                long failedTransactionId = -1;
180
                long lastfailedTransactionTimestamp = -1;
181
                for (Payment payment : paymentClient.getPaymentsForUser(ticket.getCustomerId(), 0, getTomorrow().getTime(), PaymentStatus.FAILED, 0)) {
182
                    if (payment.getErrorTimestamp() > lastfailedTransactionTimestamp) {
183
                        failedTransactionId = payment.getMerchantTxnId();
184
                    }
185
                }
186
 
187
                transactionServiceClient = new TransactionClient().getClient();
188
                orders.addAll(transactionServiceClient.getOrdersForTransaction(failedTransactionId, ticket.getCustomerId()));
189
            }
4008 mandeep.dh 190
 
191
            if (TicketCategory.DELAYED_DELIVERY.equals(ticket.getCategory())) {
192
                log.info("Fetching orders for " + ticket.getCustomerId());
193
                orders = new ArrayList<Order>();
194
                transactionServiceClient = new TransactionClient().getClient();
195
 
196
                for (Order order : transactionServiceClient.getOrdersForCustomer(ticket.getCustomerId(), 0, 0, null)) {
197
                    if (order.getExpected_delivery_time() > order.getPromised_delivery_time() && order.getExpected_delivery_time() > new Date().getTime()) {
198
                        orders.add(order);
199
                    }
200
                }
201
            }
3405 mandeep.dh 202
        } catch (TException e) {
3546 mandeep.dh 203
            log.error("Error loading edit page", e);
204
        } catch (NumberFormatException e) {
205
            log.error("Error parsing orderId", e);
206
        } catch (TransactionServiceException e) {
207
            log.error("Error fetching orders for " + ticket.getCustomerId(), e);
3578 mandeep.dh 208
        } catch (PaymentException e) {
209
            log.error("Error fetching payments for " + ticket.getCustomerId(), e);
3405 mandeep.dh 210
        }
211
 
212
        return EDIT;
213
    }
214
 
215
    private String createSubjectString(Ticket ticket) {
216
        return CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID + ticket.getId()
217
                + " " + ticket.getCategory().name();
218
    }
219
 
3578 mandeep.dh 220
    private Date getTomorrow() {
221
        Calendar calendar = Calendar.getInstance();
222
        calendar.setTime(new Date());
223
        calendar.add(Calendar.DAY_OF_MONTH, 1);
224
        return calendar.getTime();
225
    }
226
 
3339 mandeep.dh 227
    public String create() {
228
        try {
3546 mandeep.dh 229
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId)
230
                    .getId();
3339 mandeep.dh 231
            Ticket ticket = new Ticket();
232
            ticket.setDescription(description);
233
            ticket.setCreatorId(creatorId);
234
            ticket.setStatus(TicketStatus.OPEN);
235
            ticket.setPriority(TicketPriority.valueOf(priority));
236
            ticket.setCategory(TicketCategory.valueOf(category));
237
 
238
            Activity activity = new Activity();
239
            activity.setDescription("Creating Ticket");
240
            activity.setType(ActivityType.OTHER);
241
            activity.setTicketPriority(TicketPriority.valueOf(priority));
242
            activity.setTicketStatus(TicketStatus.OPEN);
243
            activity.setCreatorId(creatorId);
244
            activity.setTicketCategory(TicketCategory.valueOf(category));
245
            activity.setTicketDescription(description);
246
 
247
            if (orderId != null && !orderId.isEmpty()) {
248
                ticket.setOrderId(Long.parseLong(orderId));
249
            }
250
 
3405 mandeep.dh 251
            if (userId != null && !userId.isEmpty()) {
252
                ticket.setCustomerId(Long.parseLong(userId));
253
                activity.setCustomerId(Long.parseLong(userId));
3546 mandeep.dh 254
            } else {
3390 mandeep.dh 255
                User user = null;
256
                userContextServiceClient = new UserClient().getClient();
257
                try {
3405 mandeep.dh 258
                    if (customerName != null && !customerName.isEmpty()) {
259
                        ticket.setCustomerName(customerName);
260
                        activity.setCustomerName(customerName);
261
                    }
262
 
263
                    if (customerEmailId != null && !customerEmailId.isEmpty()) {
264
                        ticket.setCustomerEmailId(customerEmailId);
265
                        activity.setCustomerEmailId(customerEmailId);
3546 mandeep.dh 266
                        user = userContextServiceClient
267
                                .getUserByEmail(customerEmailId);
3405 mandeep.dh 268
                    }
269
 
3546 mandeep.dh 270
                    if ((user == null || user.getUserId() == -1)
271
                            && customerMobileNumber != null
272
                            && !customerMobileNumber.isEmpty()) {
3405 mandeep.dh 273
                        ticket.setCustomerMobileNumber(customerMobileNumber);
274
                        activity.setCustomerMobileNumber(customerMobileNumber);
3546 mandeep.dh 275
                        user = userContextServiceClient
276
                                .getUserByMobileNumber(Long
277
                                        .parseLong(customerMobileNumber));
3405 mandeep.dh 278
                    }
3390 mandeep.dh 279
                } catch (UserContextException e) {
3546 mandeep.dh 280
                    log.error("Could not fetch user for: " + customerEmailId
281
                            + " " + customerMobileNumber + " " + customerName,
282
                            e);
3390 mandeep.dh 283
                }
3546 mandeep.dh 284
 
3390 mandeep.dh 285
                if (user != null && user.getUserId() != -1) {
286
                    ticket.setCustomerId(user.getUserId());
287
                    activity.setCustomerId(user.getUserId());
288
                }
3339 mandeep.dh 289
            }
290
 
291
            // handling null values appropriately
292
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
3546 mandeep.dh 293
                long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId)
294
                        .getId();
3339 mandeep.dh 295
                ticket.setAssigneeId(assigneeId);
296
                activity.setTicketAssigneeId(assigneeId);
297
            }
298
 
3546 mandeep.dh 299
            crmServiceClient = new CRMClient().getClient();
300
            id = String
301
                    .valueOf(crmServiceClient.insertTicket(ticket, activity));
3339 mandeep.dh 302
        } catch (TException e) {
303
            log.error("Error while creating ticket", e);
304
            return EXCEPTION;
305
        }
306
 
3405 mandeep.dh 307
        return index();
3339 mandeep.dh 308
    }
309
 
3405 mandeep.dh 310
    public boolean isAssigneeEditable() {
311
        return SecurityUtils.getSubject().hasRole("TeamLead");
312
    }
313
 
3397 mandeep.dh 314
    public String searchTickets() throws ParseException {
3137 mandeep.dh 315
        try {
3397 mandeep.dh 316
            SearchFilter searchFilter = new SearchFilter();
3405 mandeep.dh 317
            if (userId != null && !userId.isEmpty()) {
318
                searchFilter.setCustomerId(Long.parseLong(userId));
319
            }
320
 
3397 mandeep.dh 321
            if (agentIds != null && agentIds.length != 0) {
322
                searchFilter.setTicketAssigneeIds(new ArrayList<Long>());
323
                for (String agentId : agentIds) {
3546 mandeep.dh 324
                    searchFilter.getTicketAssigneeIds().add(
325
                            CRMAuthorizingRealm.getAgent(agentId).getId());
3397 mandeep.dh 326
                }
327
            }
3137 mandeep.dh 328
 
3397 mandeep.dh 329
            if (startTimestamp != null && !startTimestamp.isEmpty()) {
3546 mandeep.dh 330
                searchFilter.setStartTimestamp(SDF.parse(startTimestamp)
331
                        .getTime());
3397 mandeep.dh 332
            }
333
 
334
            if (endTimestamp != null && !endTimestamp.isEmpty()) {
335
                searchFilter.setEndTimestamp(SDF.parse(endTimestamp).getTime());
336
            }
337
 
3422 mandeep.dh 338
            if (id != null && !id.isEmpty()) {
339
                searchFilter.setTicketId(Long.parseLong(id));
340
            }
341
 
3499 mandeep.dh 342
            if (status != null && !status.isEmpty()) {
3972 mandeep.dh 343
                searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
344
                searchFilter.getTicketStatuses().add(TicketStatus.valueOf(status));
345
                if (TicketStatus.OPEN.name().equals(status)) {
346
                    searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
347
                }
3499 mandeep.dh 348
            }
349
 
350
            if (category != null && !category.isEmpty()) {
3546 mandeep.dh 351
                searchFilter
352
                        .setTicketCategory(TicketCategory.valueOf(category));
3499 mandeep.dh 353
            }
354
 
3546 mandeep.dh 355
            crmServiceClient = new CRMClient().getClient();
3405 mandeep.dh 356
            tickets = crmServiceClient.getTickets(searchFilter);
3137 mandeep.dh 357
        } catch (TException e) {
358
            String errorString = "Error getting tickets for "
359
                    + currentAgentEmailId;
360
            log.error(errorString, e);
361
            addActionError(errorString);
362
        }
363
 
3405 mandeep.dh 364
        return index();
3137 mandeep.dh 365
    }
366
 
367
    public String getUnassignedTickets() {
368
        try {
3546 mandeep.dh 369
            crmServiceClient = new CRMClient().getClient();
3137 mandeep.dh 370
            tickets = crmServiceClient.getUnassignedTickets();
371
        } catch (TException e) {
3339 mandeep.dh 372
            String errorString = "Error getting tickets for "
373
                    + currentAgentEmailId;
3137 mandeep.dh 374
            log.error(errorString, e);
375
            addActionError(errorString);
376
        }
377
 
3405 mandeep.dh 378
        return index();
3137 mandeep.dh 379
    }
380
 
3405 mandeep.dh 381
    public String update() {
382
        try {
383
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId)
384
                    .getId();
385
 
3546 mandeep.dh 386
            SearchFilter searchFilter = new SearchFilter();
387
            searchFilter.setTicketId(Long.parseLong(id));
388
            crmServiceClient = new CRMClient().getClient();
389
            Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
3405 mandeep.dh 390
            ticket.setDescription(description);
391
            ticket.setPriority(TicketPriority.valueOf(priority));
392
 
393
            // Update when a ticket is closed!
3578 mandeep.dh 394
            if (TicketStatus.CLOSED.name().equals(status) || TicketCategory.COD_VERIFICATION.equals(ticket.getCategory())) {
3546 mandeep.dh 395
                if (TicketCategory.COD_VERIFICATION.equals(ticket.getCategory())
396
                        && pendingCODOrders(ticket.getCustomerId())) {
397
                    addActionMessage("Cod orders pending for verification. Ticket can not be closed and its category cannot be changed.");
398
                } else {
399
                    ticket.setCategory(TicketCategory.valueOf(category));
400
                    ticket.setStatus(TicketStatus.valueOf(status));
401
                    ticket.setCloseDate(new Date().getTime());
402
                }
3405 mandeep.dh 403
            }
3546 mandeep.dh 404
            else {
405
                ticket.setCategory(TicketCategory.valueOf(category));
3578 mandeep.dh 406
                ticket.setStatus(TicketStatus.valueOf(status));
3546 mandeep.dh 407
            }
3405 mandeep.dh 408
 
409
            if (activityDescription == null || activityDescription.isEmpty()) {
4241 anupam.sin 410
                activityDescription = "Ticket fields updated";
3405 mandeep.dh 411
            }
412
 
413
            Activity activity = new Activity();
414
            activity.setDescription(activityDescription);
415
            activity.setType(ActivityType.valueOf(activityType));
3546 mandeep.dh 416
            activity.setTicketPriority(ticket.getPriority());
417
            activity.setTicketStatus(ticket.getStatus());
3405 mandeep.dh 418
            activity.setCreatorId(creatorId);
3546 mandeep.dh 419
            activity.setTicketCategory(ticket.getCategory());
420
            activity.setTicketDescription(ticket.getDescription());
3405 mandeep.dh 421
 
422
            if (userId != null && !userId.isEmpty()) {
423
                activity.setCustomerId(Long.parseLong(userId));
424
            }
425
 
4035 mandeep.dh 426
            // Handling null values appropriately
427
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
428
                long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId)
429
                        .getId();
430
                ticket.setAssigneeId(assigneeId);
431
                activity.setTicketAssigneeId(assigneeId);
432
            }
433
 
3405 mandeep.dh 434
            if (ActivityType.SEND_EMAIL_TO_CUSTOMER.equals(activity.getType())) {
435
                log.info("Sending mail");
436
                Client helperClient = new HelperClient().getClient();
437
                activity.setUserEmailId(helperClient.saveUserEmailForSending(
3546 mandeep.dh 438
                        customerEmailId, CRMConstants.CRM_EMAIL_SENDOR,
439
                        subject, body, null, CRMConstants.CRM_EMAIL_TYPE));
3405 mandeep.dh 440
 
4256 mandeep.dh 441
                String plainTextbody = new Source(body).getTextExtractor().toString();
442
 
3405 mandeep.dh 443
                // We change activityType to OTHER when pop up box for email
444
                // closes
445
                activity.setDescription("Subject: " + subject + "\n\n"
4256 mandeep.dh 446
                        + "Body: " + plainTextbody);
4206 mandeep.dh 447
 
448
                String activityDesc = activity.getDescription();
449
                if (activityDesc.length() > CRMConstants.DESCRIPTION_MAX_WIDTH) {
450
                    activityDesc = activityDesc.substring(0, CRMConstants.DESCRIPTION_MAX_WIDTH);
451
                    activityDesc += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
452
                }
453
 
454
                activity.setDescription(activityDesc);
3405 mandeep.dh 455
            }
456
 
4020 mandeep.dh 457
            if (ActivityType.ESCALATE_TICKET.equals(activity.getType())) {
458
                log.info("Sending escalation mail");
459
                EscalationMatrix escalation = CRMConstants.EscalationMatrix.valueOf(escalate);
460
                List<Integer> escalationReceipients = escalation.getAgentIds();
461
 
462
                String toMailIds = "";
463
                for (Integer agentId : escalationReceipients) {
464
                    toMailIds += CRMAuthorizingRealm.getAgent(agentId).getEmailId() + ";";
4034 mandeep.dh 465
 
466
                    // Setting last agent as assignee
467
                    ticket.setAssigneeId(agentId);
468
                    activity.setTicketAssigneeId(agentId);
4020 mandeep.dh 469
                }
470
 
471
                Client helperClient = new HelperClient().getClient();
472
                String escalationMailSubject = createEscalationMailSubject(ticket, escalation);
473
                String escalationMailBody = createEscalationMailBody(ticket, escalation);
474
 
475
                activity.setUserEmailId(helperClient.saveUserEmailForSending(
476
                        toMailIds, CRMConstants.CRM_EMAIL_SENDOR,
477
                        escalationMailSubject, escalationMailBody,
478
                        null, CRMConstants.CRM_EMAIL_TYPE));
479
 
480
                // We change activityType to OTHER when pop up box for email
481
                // closes
482
                activity.setDescription("To: " + toMailIds + "\n\nSubject: " + escalationMailSubject + "\n\n"
483
                        + "Body: " + escalationMailBody);
484
            }
485
 
3405 mandeep.dh 486
            User user = null;
487
            userContextServiceClient = new UserClient().getClient();
488
            try {
489
                if (customerName != null && !customerName.isEmpty()) {
490
                    ticket.setCustomerName(customerName);
491
                    activity.setCustomerName(customerName);
492
                }
493
 
494
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
495
                    ticket.setCustomerEmailId(customerEmailId);
496
                    activity.setCustomerEmailId(customerEmailId);
3546 mandeep.dh 497
                    user = userContextServiceClient
498
                            .getUserByEmail(customerEmailId);
3405 mandeep.dh 499
                }
500
 
3546 mandeep.dh 501
                if ((user == null || user.getUserId() == -1)
502
                        && customerMobileNumber != null
503
                        && !customerMobileNumber.isEmpty()) {
3405 mandeep.dh 504
                    ticket.setCustomerMobileNumber(customerMobileNumber);
505
                    activity.setCustomerMobileNumber(customerMobileNumber);
3546 mandeep.dh 506
                    user = userContextServiceClient.getUserByMobileNumber(Long
507
                            .parseLong(customerMobileNumber));
3405 mandeep.dh 508
                }
509
            } catch (UserContextException e) {
510
                log.error("Could not fetch user for: " + customerEmailId + " "
511
                        + customerMobileNumber + " " + customerName, e);
512
            }
3546 mandeep.dh 513
 
3405 mandeep.dh 514
            if (user != null && user.getUserId() != -1) {
515
                ticket.setCustomerId(user.getUserId());
516
                activity.setCustomerId(user.getUserId());
517
            }
518
 
519
            crmServiceClient = new CRMClient().getClient();
520
            crmServiceClient.updateTicket(ticket, activity);
521
        } catch (TException e) {
522
            log.error("Error while updating ticket", e);
523
            return EXCEPTION;
524
        } catch (HelperServiceException hse) {
525
            log.error("Error while sending mail", hse);
526
            return EXCEPTION;
527
        }
528
 
529
        return index();
530
    }
531
 
4020 mandeep.dh 532
    private String createEscalationMailBody(Ticket ticket,
533
            EscalationMatrix escalation)
534
    {
535
        return body + "<br />Please visit following URL to check ticket details.<br />" + 
536
            "http://cs.shop2020.in:8080/crm?ticketId=" + ticket.getId();
537
    }
538
 
539
    private String createEscalationMailSubject(Ticket ticket,
540
            EscalationMatrix escalation)
541
    {
542
        return CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID + ticket.getId() + " - ESCALATION FROM CRM - Type: " + escalation.name();
543
    }
544
 
3546 mandeep.dh 545
    private boolean pendingCODOrders(long customerId) {
546
        try {
547
            log.info("Trying to fetch orders for " + customerId);
548
            transactionServiceClient = new TransactionClient().getClient();
549
            for (Order order : transactionServiceClient.getOrdersForCustomer(
550
                    customerId, 0, 0,
551
                    Collections.singletonList(OrderStatus.INIT))) {
552
                log.info("Processing order: " + order.getId() + " with COD: " + order.isCod());
553
                if (order.isCod()) {
554
                    log.info("Returning true from pendingCODOrders() for " + customerId);
555
                    return true;
556
                }
557
            }
558
        } catch (TTransportException e) {
559
            log.error("Error while creating thrift client", e);
560
        } catch (TransactionServiceException e) {
561
            log.error("Error fetching orders", e);
562
        } catch (TException e) {
563
            log.error("Error fetching orders", e);
564
        }
565
 
566
        log.info("Returning false from pendingCODOrders() for " + customerId);
567
        return false;
568
    }
569
 
4277 anupam.sin 570
    public String updateOrderStatus() {
571
        long creatorId;
572
        OrderStatus orderStatusEnum = null;
573
        try {
574
            creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();
575
 
576
            if(orderStatus.equals("DENY_CANCEL_REQUEST")) {
577
                transactionServiceClient = new TransactionClient().getClient();
578
                transactionServiceClient.markOrderCancellationRequestDenied(Long.parseLong(orderId));
579
            } 
3546 mandeep.dh 580
 
4277 anupam.sin 581
            else {
3546 mandeep.dh 582
 
4277 anupam.sin 583
                orderStatusEnum = OrderStatus.valueOf(orderStatus);
584
                transactionServiceClient = new TransactionClient().getClient();
3578 mandeep.dh 585
 
4277 anupam.sin 586
                if (OrderStatus.SUBMITTED_FOR_PROCESSING.equals(orderStatusEnum)) {
587
                    transactionServiceClient.verifyOrder(Long.parseLong(orderId));
588
                }
589
                else if (OrderStatus.CANCELED.equals(orderStatusEnum)) {
590
                    transactionServiceClient.refundOrder(Long.parseLong(orderId), currentAgentEmailId, cancelReason);
591
                }
592
                else if (OrderStatus.CANCEL_REQUEST_CONFIRMED.equals(orderStatusEnum)) {
593
                    transactionServiceClient.markOrderCancellationRequestConfirmed(Long.parseLong(orderId));
594
                }
595
                else {
596
                    throw new UnsupportedOperationException("Cannot update status of orderId: " + orderId + " to: " + orderStatus);
597
                }
598
            }
599
 
600
            SearchFilter searchFilter = new SearchFilter();
601
            searchFilter.setTicketId(Long.parseLong(id));
602
            crmServiceClient = new CRMClient().getClient();
603
            Ticket ticket = crmServiceClient.getTickets(searchFilter).get(0);
3546 mandeep.dh 604
 
4277 anupam.sin 605
            // Inserting activity
606
            Activity activity = new Activity();
607
            activity.setDescription("Status updated for Order Id: " + orderId
608
                    + " to " + orderStatus + ", Reason: " + cancelReason + body);
609
            activity.setType(ActivityType.OTHER);
610
            activity.setTicketPriority(ticket.getPriority());
611
            activity.setTicketStatus(ticket.getStatus());
612
            activity.setCreatorId(creatorId);
613
            activity.setTicketCategory(ticket.getCategory());
614
            activity.setTicketDescription(ticket.getDescription());
615
            activity.setCustomerId(ticket.getCustomerId());
616
            activity.setTicketAssigneeId(ticket.getAssigneeId());
3546 mandeep.dh 617
 
4277 anupam.sin 618
            crmServiceClient = new CRMClient().getClient();
619
            crmServiceClient.updateTicket(ticket, activity);
3546 mandeep.dh 620
 
4277 anupam.sin 621
        } catch (TException e) {
622
            log.info("Thrift exception", e);
623
        } catch (NumberFormatException e) {
624
            log.info("Could not convert to long", e);
625
        } catch (TransactionServiceException e) {
626
            log.info("Transaction client exception", e);
627
        }
628
 
3546 mandeep.dh 629
        return edit();
630
    }
4277 anupam.sin 631
 
3137 mandeep.dh 632
    public User getUser(Long userId) {
633
        User user = null;
634
 
635
        try {
3390 mandeep.dh 636
            userContextServiceClient = new UserClient().getClient();
3137 mandeep.dh 637
            user = userContextServiceClient.getUserById(userId);
638
        } catch (UserContextException e) {
639
            String errorString = "Could not fetch user for " + userId;
640
            log.error(errorString, e);
641
            addActionError(errorString);
642
        } catch (TException e) {
643
            String errorString = "Could not create client";
644
            log.error(errorString, e);
645
            addActionError(errorString);
646
        }
647
 
648
        return user;
649
    }
4267 anupam.sin 650
 
651
    public String allowPayment() throws NumberFormatException, TransactionServiceException, TException {
652
        transactionServiceClient = new TransactionClient().getClient();
653
        transactionServiceClient.markTransactionAsPaymentFlagRemoved(Long.parseLong(transactionId));
654
        return edit();
655
    }
656
 
657
    public String blockPayment() throws NumberFormatException, TransactionServiceException, TException {
658
        transactionServiceClient = new TransactionClient().getClient();
659
        log.info("*" + transactionId + "*");
660
        transactionServiceClient.refundTransaction(Long.parseLong(transactionId), currentAgentEmailId, "FLAGGED_PAYMENT_CANCELLED");
661
        return edit();
662
    }
3137 mandeep.dh 663
 
4020 mandeep.dh 664
    public EscalationMatrix[] getEscalationMatrix () {
665
        return CRMConstants.EscalationMatrix.values();
666
    }
667
 
4241 anupam.sin 668
    public CODCancelMatrix[] getCODCancelMatrix () {
669
        return CRMConstants.CODCancelMatrix.values();
670
    }
671
 
3546 mandeep.dh 672
    public String getAddress(Order order) {
673
        return ModelUtils.extractAddressFromOrder(order);
674
    }
675
 
676
    public String getProductName(LineItem lineItem) {
4008 mandeep.dh 677
        return ModelUtils.extractProductNameFromLineItem(lineItem) + " " + lineItem.getColor();
3546 mandeep.dh 678
    }
679
 
3405 mandeep.dh 680
    public ActivityType[] getActivityTypes() {
681
        return ActivityType.values();
682
    }
683
 
684
    public TicketStatus[] getTicketStatuses() {
685
        return TicketStatus.values();
686
    }
687
 
3390 mandeep.dh 688
    public Agent getAgent(long agentId) throws TException {
689
        return CRMAuthorizingRealm.getAgent(agentId);
3137 mandeep.dh 690
    }
691
 
3339 mandeep.dh 692
    public List<Agent> getAllAgents() {
3390 mandeep.dh 693
        return CRMAuthorizingRealm.getAgents();
3339 mandeep.dh 694
    }
695
 
696
    public TicketCategory[] getTicketCategories() {
697
        return TicketCategory.values();
698
    }
699
 
700
    public TicketPriority[] getTicketPriorities() {
701
        return TicketPriority.values();
702
    }
703
 
3137 mandeep.dh 704
    public List<Ticket> getTickets() {
705
        return tickets;
706
    }
707
 
708
    public void setTickets(List<Ticket> tickets) {
709
        this.tickets = tickets;
710
    }
3339 mandeep.dh 711
 
3405 mandeep.dh 712
    public String getCustomerEmailId() {
713
        return customerEmailId;
3339 mandeep.dh 714
    }
715
 
3405 mandeep.dh 716
    public void setCustomerEmailId(String customerEmailId) {
717
        this.customerEmailId = customerEmailId;
3339 mandeep.dh 718
    }
719
 
720
    public String getDescription() {
721
        return description;
722
    }
723
 
724
    public void setDescription(String description) {
725
        this.description = description;
726
    }
727
 
728
    public String getAssigneeEmailId() {
729
        return assigneeEmailId;
730
    }
731
 
732
    public void setAssigneeEmailId(String assigneeEmailId) {
733
        this.assigneeEmailId = assigneeEmailId;
734
    }
735
 
736
    public String getPriority() {
737
        return priority;
738
    }
739
 
740
    public void setPriority(String priority) {
741
        this.priority = priority;
742
    }
743
 
744
    public String getCategory() {
745
        return category;
746
    }
747
 
748
    public void setCategory(String category) {
749
        this.category = category;
750
    }
751
 
752
    public String getOrderId() {
753
        return orderId;
754
    }
755
 
756
    public void setOrderId(String orderId) {
757
        this.orderId = orderId;
758
    }
3397 mandeep.dh 759
 
760
    public String[] getAgentIds() {
761
        return agentIds;
762
    }
763
 
764
    public void setAgentIds(String[] agentIds) {
765
        this.agentIds = agentIds;
766
    }
767
 
768
    public String getStartTimestamp() {
769
        return startTimestamp;
770
    }
771
 
772
    public void setStartTimestamp(String startTimestamp) {
773
        this.startTimestamp = startTimestamp;
774
    }
775
 
776
    public String getEndTimestamp() {
777
        return endTimestamp;
778
    }
779
 
780
    public void setEndTimestamp(String endTimestamp) {
781
        this.endTimestamp = endTimestamp;
782
    }
3405 mandeep.dh 783
 
784
    public String getUserId() {
785
        return userId;
786
    }
787
 
788
    public void setUserId(String userId) {
789
        this.userId = userId;
790
    }
791
 
792
    public String getId() {
793
        return id;
794
    }
795
 
796
    public void setId(String id) {
797
        this.id = id;
798
    }
799
 
800
    public String getActivityDescription() {
801
        return activityDescription;
802
    }
803
 
804
    public void setActivityDescription(String activityDescription) {
805
        this.activityDescription = activityDescription;
806
    }
807
 
808
    public String getStatus() {
809
        return status;
810
    }
811
 
812
    public void setStatus(String status) {
813
        this.status = status;
814
    }
815
 
816
    public String getActivityType() {
817
        return activityType;
818
    }
819
 
820
    public void setActivityType(String activityType) {
821
        this.activityType = activityType;
822
    }
823
 
824
    public Ticket getTicket() {
825
        return ticket;
826
    }
827
 
828
    public void setTicket(Ticket ticket) {
829
        this.ticket = ticket;
830
    }
831
 
832
    public List<Activity> getActivities() {
833
        return activities;
834
    }
835
 
836
    public void setActivities(List<Activity> activities) {
837
        this.activities = activities;
838
    }
839
 
840
    public String getSubject() {
841
        return subject;
842
    }
843
 
844
    public void setSubject(String subject) {
845
        this.subject = subject;
846
    }
847
 
848
    public String getBody() {
849
        return body;
850
    }
851
 
852
    public void setBody(String body) {
853
        this.body = body;
854
    }
855
 
856
    public String getCustomerName() {
857
        return customerName;
858
    }
859
 
860
    public void setCustomerName(String customerName) {
861
        this.customerName = customerName;
862
    }
863
 
864
    public String getCustomerMobileNumber() {
865
        return customerMobileNumber;
866
    }
867
 
868
    public void setCustomerMobileNumber(String customerMobileNumber) {
869
        this.customerMobileNumber = customerMobileNumber;
870
    }
3546 mandeep.dh 871
 
872
    public User getUser() {
873
        return user;
874
    }
875
 
876
    public void setUser(User user) {
877
        this.user = user;
878
    }
879
 
880
    public String getOrderStatus() {
881
        return orderStatus;
882
    }
883
 
884
    public void setOrderStatus(String orderStatus) {
885
        this.orderStatus = orderStatus;
886
    }
887
 
888
    public List<Order> getOrders() {
889
        return orders;
890
    }
891
 
892
    public void setOrders(List<Order> orders) {
893
        this.orders = orders;
894
    }
4020 mandeep.dh 895
 
896
    public String getEscalate() {
897
        return escalate;
898
    }
899
 
900
    public void setEscalate(String escalate) {
901
        this.escalate = escalate;
902
    }
4241 anupam.sin 903
 
904
    public String getCancelReason() {
905
        return cancelReason;
906
    }
907
 
908
    public void setCancelReason(String cancelReason) {
909
        this.cancelReason = cancelReason;
910
    }
4267 anupam.sin 911
 
912
    public List<Payment> getPayments() {
913
        return payments;
914
    }
915
 
916
    public void setPayments(List<Payment> payments) {
917
        this.payments = payments;
918
    }
919
 
920
    public String getTransactionId() {
921
        return transactionId;
922
    }
923
 
924
    public void setTransactionId(String transactionId) {
925
        this.transactionId = transactionId;
926
    }
3137 mandeep.dh 927
}