Subversion Repositories SmartDukaan

Rev

Rev 8847 | Rev 16244 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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