Subversion Repositories SmartDukaan

Rev

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