Subversion Repositories SmartDukaan

Rev

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