Subversion Repositories SmartDukaan

Rev

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