Exemple #1
0
        public ListModelBase <HourlyAppointmentBookedModel, HourlyAppointmentsBookedListModelFilter> GetHourlyAppointmentsBooked(int pageNumber, int pageSize, ModelFilterBase filter, out int totalRecords)
        {
            var customFilter = filter as HourlyAppointmentsBookedListModelFilter;

            customFilter = customFilter ?? new HourlyAppointmentsBookedListModelFilter();

            var appFilter = new AppointmentsBookedListModelFilter
            {
                AccountId = customFilter.AccountId,
                FromDate  = customFilter.FromDate,
                ToDate    = customFilter.ToDate,
                ShowCustomersWithAppointment = customFilter.ShowCustomersWithAppointment,
                IsHealthPlanEvent            = true
            };

            var eventCustomers = _eventCustomerRepository.GetEventCustomersbyRegisterationDate(pageNumber, pageSize, appFilter, out totalRecords);

            if (eventCustomers.IsNullOrEmpty())
            {
                return(null);
            }

            var appointments = _appointmentRepository.GetByIds(eventCustomers.Where(ec => ec.AppointmentId.HasValue).Select(ec => ec.AppointmentId.Value).ToList());
            var eventIds     = eventCustomers.Select(ec => ec.EventId).Distinct().ToArray();
            var model        = _eventReportingService.GetEventVolumeModel(eventCustomers.Select(ec => ec.EventId).Distinct().ToArray());

            var customers             = _customerRepository.GetCustomers(eventCustomers.Select(ec => ec.CustomerId).Distinct().ToArray());
            var customerIds           = customers.Select(x => x.CustomerId);
            var orders                = _orderRepository.GetAllOrdersByEventCustomerIds(eventCustomers.Select(ec => ec.Id).ToArray(), true);
            var customerEligibilities = _customerEligibilityRepository.GetCustomerEligibilityByCustomerIdsAndYear(customerIds, DateTime.Today.Year);

            if (customerEligibilities.IsNullOrEmpty())
            {
                customerEligibilities = new List <CustomerEligibility>();
            }

            var eventPackages = _eventPackageRepository.GetbyEventIds(eventIds);
            var eventTests    = _eventTestRepository.GetByEventIds(eventIds);

            var sourceCodes = _sourceCodeRepository.GetSourceCodeByIds(orders.SelectMany(o => o.OrderDetails.Where(od => od.SourceCodeOrderDetail != null && od.SourceCodeOrderDetail.IsActive)
                                                                                         .Select(od => od.SourceCodeOrderDetail.SourceCodeId)).Distinct().ToArray());

            var orgRoleUserIds = eventCustomers.Where(ec => ec.DataRecorderMetaData != null && ec.DataRecorderMetaData.DataRecorderCreator != null).Select(ec => ec.DataRecorderMetaData.DataRecorderCreator.Id).ToList();

            var eventAppointmentChangeLogs = _eventAppointmentChangeLogRepository.GetByEventCustomerIds(eventCustomers.Select(ec => ec.Id).ToArray()).ToArray();

            orgRoleUserIds.AddRange(eventAppointmentChangeLogs.Select(eacl => eacl.CreatedByOrgRoleUserId));

            var registeredbyAgent = _organizationRoleUserRepository.GetOrganizationRoleUsers(orgRoleUserIds);

            var roles = _roleRepository.GetAll();

            var registeredbyAgentNameIdPair = _organizationRoleUserRepository.GetNameIdPairofUsers(orgRoleUserIds.ToArray()).ToArray();

            var callDetails = _callCenterCallRepository.GetCallDetails(customers);

            var shippingDetailIds = orders.SelectMany(o => o.OrderDetails.SelectMany(od => od.ShippingDetailOrderDetails.Select(sdod => sdod.ShippingDetailId))).ToArray();
            var shippingDetails   = _shippingDetailRepository.GetByIds(shippingDetailIds);
            var cdShippingOption  = _shippingOptionRepository.GetShippingOptionByProductId((long)Product.UltraSoundImages);
            var shippingOptions   = _shippingOptionRepository.GetAllShippingOptions();

            var customTags = _customTagRepository.GetByCustomerIds(customerIds);

            var tagNames               = customers.Where(x => !string.IsNullOrEmpty(x.Tag)).Select(x => x.Tag).ToArray();
            var corporateAccount       = _corporateAccountRepository.GetByTags(tagNames);
            var corporateAccountIds    = corporateAccount.Select(x => x.Id).ToArray();
            var accountAdditionalField = _accountAdditionalFieldRepository.GetAccountAdditionalFieldsByAccountIds(corporateAccountIds);

            var primaryCarePhysicians = _primaryCarePhysicianRepository.GetByCustomerIds(eventCustomers.Select(ec => ec.CustomerId).Distinct().ToArray());
            var languages             = _languageRepository.GetAll();
            var pcpAppointments       = _pcpAppointmentRepository.GetByEventCustomerIds(eventCustomers.Select(ec => ec.Id).ToArray());

            return(_customAppointmentsBookedModelFactory.CreateHourlyModel(eventCustomers, appointments, orders, model, customers, registeredbyAgent, roles, registeredbyAgentNameIdPair, sourceCodes, callDetails,
                                                                           shippingDetails, cdShippingOption, shippingOptions, eventAppointmentChangeLogs, primaryCarePhysicians, eventPackages, eventTests, languages, customTags, corporateAccount,
                                                                           accountAdditionalField, pcpAppointments, customerEligibilities));
        }
        private void ParseCallUploadedCalls(IEnumerable <DataRow> rows, List <CallUploadLog> failedCustomerList, List <CallUploadLog> successCustomerList, long callUploadId)
        {
            var callUploadLogs = rows.Select(row => _callUploadHelper.GetUploadLog(row, callUploadId));

            if (callUploadLogs.IsNullOrEmpty())
            {
                _logger.Info("No Record Found For Parsing in file");
                return;
            }

            SaveFailedCustomers(failedCustomerList, callUploadLogs);

            callUploadLogs = callUploadLogs.Where(x => x.IsSuccessfull);

            if (callUploadLogs.IsNullOrEmpty())
            {
                _logger.Info("No Successfull Customer Found For Parsing in file");
                return;
            }
            var customerIds = callUploadLogs.Select(x => x.CustomerId).Distinct();

            var outboundCustomerIds = callUploadLogs.Where(x => !x.IsDirectMail).Select(x => x.CustomerId).ToArray();

            IEnumerable <Customer> customers = null;

            try
            {
                customers = _customerRepository.GetCustomers(customerIds.ToArray());
            }
            catch (Exception ex)
            {
                _logger.Error(" Exception : " + ex.Message + " \n Stack Trace: " + ex.StackTrace);
            }

            if (customers != null && customers.Any())
            {
                var healthPlans = _corporateAccountRepository.GetAllHealthPlan();

                var events = ((IUniqueItemRepository <Event>)_eventRepository).GetByIds(callUploadLogs.Where(x => x.EventId > 0).Select(x => x.EventId).ToArray());

                var callQueue = _callQueueRepository.GetCallQueueByCategory(HealthPlanCallQueueCategory.CallRound);

                var callQueueCustomers = _callQueueCustomerRepository.GetCallQueueCustomersBuCustomerIds(outboundCustomerIds, callQueue.Id).ToList();

                var prospectCustomers = _prospectCustomerRepository.GetProspectCustomersByCustomerIds(outboundCustomerIds);

                var calls = _callCenterCallRepository.GetCallDetails(outboundCustomerIds);

                var emailIds = callUploadLogs.Select(x => x.Email).Distinct().ToList();

                var userNames = callUploadLogs.Where(x => !string.IsNullOrEmpty(x.UserName)).Select(x => x.UserName).Distinct().ToList();

                var organizationRoleUserEmail = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByEmail(emailIds, (long)Roles.CallCenterRep);

                var organizationRoleUserUserName = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByUserNames(userNames, (long)Roles.CallCenterRep);

                var directMailTypes = _directMailTypeRepository.GetAll();


                foreach (var callUploadLog in callUploadLogs)
                {
                    try
                    {
                        var customer = customers.SingleOrDefault(x => x.CustomerId == callUploadLog.CustomerId);
                        if (customer == null)
                        {
                            callUploadLog.IsSuccessfull = false;
                            callUploadLog.ErrorMessage  = "Customer ID is not valid.";
                            _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                            failedCustomerList.Add(callUploadLog);
                            continue;
                        }

                        if (string.IsNullOrEmpty(customer.Tag))
                        {
                            callUploadLog.IsSuccessfull = false;
                            callUploadLog.ErrorMessage  = "Customer does not belong to any HealthPlan.";
                            _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                            failedCustomerList.Add(callUploadLog);
                            continue;
                        }

                        var healthPlan = healthPlans.SingleOrDefault(x => x.Tag == customer.Tag);

                        if (healthPlan == null)
                        {
                            callUploadLog.IsSuccessfull = false;
                            callUploadLog.ErrorMessage  = "HealthPlan " + customer.Tag + " not found.";
                            _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                            failedCustomerList.Add(callUploadLog);
                            continue;
                        }

                        var orgRoleId = _callUploadHelper.GetOrganizationRoleId(callUploadLog, organizationRoleUserEmail, organizationRoleUserUserName);

                        if (orgRoleId <= 0)
                        {
                            callUploadLog.IsSuccessfull = false;
                            callUploadLog.ErrorMessage  = "Created By Information does not found.";
                            _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                            failedCustomerList.Add(callUploadLog);
                            continue;
                        }

                        long?directMailTypeId = null;
                        if (callUploadLog.IsDirectMail)
                        {
                            var campaign = _campaignRepository.GetCampaignByName(callUploadLog.CampaignName);

                            if (campaign == null)
                            {
                                callUploadLog.IsSuccessfull = false;
                                callUploadLog.ErrorMessage  = " Please provide a valid campaign name.";
                                _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                                failedCustomerList.Add(callUploadLog);
                                continue;
                            }

                            if (!string.IsNullOrEmpty(callUploadLog.DirectMailType))
                            {
                                var directMailType = directMailTypes.SingleOrDefault(s => s.Name.ToLower() == callUploadLog.DirectMailType.ToLower());

                                if (directMailType == null)
                                {
                                    callUploadLog.IsSuccessfull = false;
                                    callUploadLog.ErrorMessage  = " Please provide a valid Direct Mail Type.";
                                    _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                                    failedCustomerList.Add(callUploadLog);
                                    continue;
                                }
                                directMailTypeId = directMailType.Id;
                            }
                            var isInvalidAddress = false;
                            if (callUploadLog.IsInvalidAddress != null)
                            {
                                isInvalidAddress = callUploadLog.IsInvalidAddress.ToLower().Trim() == "yes" ? true : false;
                            }

                            var directMail = new DirectMail
                            {
                                CustomerId       = callUploadLog.CustomerId,
                                MailDate         = callUploadLog.OutreachDateTime.Value.Date,
                                CallUploadId     = callUploadLog.CallUploadId,
                                CampaignId       = campaign.Id,
                                DirectMailTypeId = directMailTypeId,
                                Mailedby         = orgRoleId,
                                IsInvalidAddress = isInvalidAddress,
                                Notes            = isInvalidAddress ? "Direct Mail returned because of invalid address." : string.Empty,
                            };

                            _directMailRepository.Save(directMail);
                        }
                        else
                        {
                            Event theEventData = null;
                            if (callUploadLog.EventId > 0)
                            {
                                theEventData = events.SingleOrDefault(x => x.Id == callUploadLog.EventId);
                                if (theEventData == null)
                                {
                                    callUploadLog.IsSuccessfull = false;
                                    callUploadLog.ErrorMessage  = "Event Id provide is not valid.";
                                    _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                                    failedCustomerList.Add(callUploadLog);
                                    continue;
                                }
                            }

                            if (callUploadLog.EventId > 0)
                            {
                                var isValidEventForAccount = _eventRepository.ValidateEventForAccount(callUploadLog.EventId, healthPlan.Id);
                                if (!isValidEventForAccount)
                                {
                                    callUploadLog.IsSuccessfull = false;
                                    callUploadLog.ErrorMessage  = "Event Id provide is not valid for customer HealthPlan.";
                                    _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                                    failedCustomerList.Add(callUploadLog);
                                    continue;
                                }
                            }

                            var prospectCustomer = prospectCustomers.FirstOrDefault(x => x.CustomerId == customer.CustomerId);

                            if (prospectCustomer == null && customer != null)
                            {
                                prospectCustomer = _prospectCustomerFactory.CreateProspectCustomerFromCustomer(customer, false);
                                prospectCustomer = ((IUniqueItemRepository <ProspectCustomer>)_prospectCustomerRepository).Save(prospectCustomer);
                            }

                            var callQueueCustomer = SaveCallQueueCustomer(callQueue.Id, callUploadLog, healthPlan.Id,
                                                                          callQueueCustomers);

                            var call = SaveCall(callUploadLog, theEventData != null ? theEventData.Id : 0, orgRoleId, healthPlan.Id);

                            if (prospectCustomer != null)
                            {
                                UpdateContactedInfo(prospectCustomer.Id, call.Id, orgRoleId);
                            }

                            SaveCallQueueCustomerCall(callQueueCustomer, call);
                            SaveCallNotes(callUploadLog, call.Id, orgRoleId);

                            var  customerCalls = calls.Where(x => x.CalledCustomerId == callUploadLog.CustomerId);
                            bool isRemovedFromCallQueue;
                            var  isRuleApplied = _callUploadRuleEngine.ApplyRuleEngine(callUploadLog, customerCalls, prospectCustomer, orgRoleId, callQueueCustomer.Id, callQueueCustomer.CallQueueId, out isRemovedFromCallQueue, _logger);

                            if (isRemovedFromCallQueue)
                            {
                                callQueueCustomer.Status = CallQueueStatus.Removed;
                                _callQueueCustomerRepository.Save(callQueueCustomer);
                            }

                            callUploadLog.IsRuleApplied = isRuleApplied;
                        }

                        successCustomerList.Add(callUploadLog);

                        _callUploadLogRepository.SaveCallUploadLog(callUploadLog);
                    }
                    catch (Exception ex)
                    {
                        callUploadLog.IsSuccessfull = false;
                        callUploadLog.ErrorMessage  = "Message: " + ex.Message + "\n stack Trace: " + ex.StackTrace;
                        failedCustomerList.Add(callUploadLog);
                        _logger.Error("Message: " + ex.Message + "\n stack Trace: " + ex.StackTrace);
                    }
                }
            }
            else
            {
                foreach (var callUploadLog in callUploadLogs)
                {
                    callUploadLog.IsSuccessfull = false;
                    callUploadLog.ErrorMessage  = "Please Provide a valid CustomerId";
                    failedCustomerList.Add(callUploadLog);
                }
            }


            SaveFailedCustomers(failedCustomerList, callUploadLogs);
        }
        public void PollForApplyRule()
        {
            try
            {
                var timeOfDay = DateTime.Now;
                _logger.Info("Entering In Apply Rules On Locked Customers PollingAgent");
                if (_isDevEnvironment || timeOfDay.TimeOfDay > new TimeSpan(4, 0, 0))
                {
                    var callUploadLogs = _callUploadLogRepository.GetCustomerForApplyRule(OutReachType);


                    if (callUploadLogs.IsNullOrEmpty())
                    {
                        _logger.Info("No Record Found");
                        return;
                    }
                    var customerIds       = callUploadLogs.Select(x => x.CustomerId);
                    var callcenterCalls   = _callCenterCallRepository.GetCallDetails(customerIds);
                    var prospectCustomers = _prospectCustomerRepository.GetProspectCustomersByCustomerIds(customerIds.ToArray());

                    var emailIds  = callUploadLogs.Select(x => x.Email).Distinct().ToList();
                    var userNames = callUploadLogs.Select(x => x.UserName).Distinct().ToList();

                    var organizationRoleUserEmail    = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByEmail(emailIds, (long)Roles.CallCenterRep);
                    var organizationRoleUserUserName = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByUserNames(userNames, (long)Roles.CallCenterRep);

                    var callQueue = _callQueueRepository.GetCallQueueByCategory(HealthPlanCallQueueCategory.CallRound);

                    var callQueueCustomers = _callQueueCustomerRepository.GetCallQueueCustomersBuCustomerIds(customerIds, callQueue.Id).ToList();

                    foreach (var uploadLog in callUploadLogs)
                    {
                        try
                        {
                            var customerCalls    = callcenterCalls.Where(x => x.CalledCustomerId == uploadLog.CustomerId);
                            var prospectCustomer = prospectCustomers.Single(x => x.CustomerId == uploadLog.CustomerId);
                            var orgRoleId        = _callUploadHelper.GetOrganizationRoleId(uploadLog, organizationRoleUserEmail, organizationRoleUserUserName);

                            var  callQueueCustomer = callQueueCustomers.FirstOrDefault(x => x.CustomerId.HasValue && x.CustomerId.Value == uploadLog.CustomerId);
                            bool isRemovedFromCallQueue;

                            var isRuleApplied = _callUploadRuleEngine.ApplyRuleEngine(uploadLog, customerCalls, prospectCustomer, orgRoleId, callQueueCustomer.Id, callQueueCustomer.CallQueueId, out isRemovedFromCallQueue, _logger);
                            if (isRuleApplied)
                            {
                                if (isRemovedFromCallQueue)
                                {
                                    callQueueCustomer.Status = CallQueueStatus.Removed;
                                    _callQueueCustomerRepository.Save(callQueueCustomer);
                                }

                                uploadLog.IsRuleApplied = isRuleApplied;
                                _callUploadLogRepository.SaveCallUploadLog(uploadLog);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logger.Error("Error on Apply Rule on Locked Customer For CallUploadLogId: " + uploadLog.Id + " Exception: " + exception + "\n Stack Trace:" + exception.StackTrace);
                        }
                    }
                }
                else
                {
                    _logger.Info(string.Format("Apply Rule on Locked Customer can not be called as time of day is {0}", timeOfDay.TimeOfDay));
                }
            }
            catch (Exception exception)
            {
                _logger.Error("Error on Apply Rule on Locked Customer Exception: " + exception + "\n Stack Trace:" + exception.StackTrace);
            }
        }