Esempio n. 1
0
        public void CorporateUploadDataRow(CorporateCustomerEditModel customerEditModel, IEnumerable <Language> languages, IEnumerable <Lab> labs,
                                           List <string> customTags, CorporateUploadEditModel corporateModel, IEnumerable <AccountAdditionalFieldsEditModel> accountAdditionalFields,
                                           List <EventCusomerAdjustOrderViewModel> adjustOrderForCustomerEditModel, OrganizationRoleUser createdByOrgRoleUser, long organizationRoleUserId,
                                           List <long> customerList, long?source, StringBuilder sb, long corporateUploadId, out CustomerWithDuplicateAcesModel customerIdWithSameAcesId)
        {
            var    customerWithSameAcesId = new CustomerWithDuplicateAcesModel();
            string errorMessage           = _corporateUploadHelper.CheckAddtionalField(customerEditModel, accountAdditionalFields);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                throw new InvalidOperationException("System will not parse the customer data as " + errorMessage + " is not added for this corporate account.");
            }

            if (!string.IsNullOrEmpty(customerEditModel.PreApprovedPackage))
            {
                var preApprovedPackage = _packageRepository.GetByName(customerEditModel.PreApprovedPackage);
                if (preApprovedPackage == null)
                {
                    throw new InvalidOperationException("Package does not exist");
                }
                customerEditModel.PreApprovedPackageId = preApprovedPackage.Id;
            }

            //Customer customerWithAcesId = null;
            List <Customer> customersWithAcesId = null;

            var isMemberUploadByAces = false;

            isMemberUploadByAces = (source.HasValue && source.Value == (long)MemberUploadSource.Aces);

            if (!string.IsNullOrWhiteSpace(customerEditModel.AcesId))
            {
                customersWithAcesId = _customerRegistrationService.GetCustomersByAcesId(customerEditModel.AcesId);
                //if (!customersWithAcesId.IsNullOrEmpty() && customersWithAcesId.Count() > 1)
                //{
                //    throw new InvalidOperationException("Aces Id can not be duplicate, Aces Id : " + customerEditModel.AcesId + " already assigned.");
                //}

                //customerWithAcesId = customersWithAcesId.IsNullOrEmpty() ? null : customersWithAcesId.First();
            }

            if (customerEditModel.PreApprovedTest != null && customerEditModel.PreApprovedTest.Any())
            {
                var pairs = TestType.A1C.GetNameValuePairs();
                var preApprovedTestExists = pairs.Where(x => customerEditModel.PreApprovedTest.Contains(x.SecondValue.ToLower())).Select(x => x.SecondValue.ToLower());

                var testNotExist = string.Empty;

                if (preApprovedTestExists != null && preApprovedTestExists.Any() && (preApprovedTestExists.Count() != customerEditModel.PreApprovedTest.Count()))
                {
                    var testNotExistInSystem = customerEditModel.PreApprovedTest.Where(x => !preApprovedTestExists.Contains(x.ToLower())).Select(x => x).ToArray();

                    testNotExist = string.Join(",", testNotExistInSystem);
                }
                else if (preApprovedTestExists == null || !preApprovedTestExists.Any())
                {
                    testNotExist = string.Join(",", customerEditModel.PreApprovedTest);
                }

                if (!string.IsNullOrEmpty(testNotExist))
                {
                    throw new InvalidOperationException(testNotExist + " test alias name does not exist in HIP");
                }
            }

            if (string.IsNullOrEmpty(customerEditModel.Activity))
            {
                throw new InvalidOperationException("Activity cannot be blank.");
            }

            if (!isMemberUploadByAces && !(customerEditModel.Activity.ToLower() == UploadActivityType.MailOnly.GetDescription().ToLower() || customerEditModel.Activity.ToLower() == UploadActivityType.OnlyCall.GetDescription().ToLower() ||
                                           customerEditModel.Activity.ToLower() == UploadActivityType.BothMailAndCall.GetDescription().ToLower()))
            {
                throw new InvalidOperationException("Activity value invalid.");
            }

            if (isMemberUploadByAces && !(customerEditModel.Activity.ToLower() == UploadActivityType.MailOnly.GetDescription().ToLower() ||
                                          customerEditModel.Activity.ToLower() == UploadActivityType.None.GetDescription().ToLower() || customerEditModel.Activity.ToLower() == UploadActivityType.BothMailAndCall.GetDescription().ToLower() ||
                                          customerEditModel.Activity.ToLower() == UploadActivityType.DoNotCallDoNotMail.GetDescription().ToLower()))
            {
                throw new InvalidOperationException("Activity value invalid.");
            }

            if (!string.IsNullOrEmpty(customerEditModel.PredictedZip))
            {
                var invalidZipMessage = _customerRegistrationService.ValidateZipCodes(customerEditModel.PredictedZip);
                if (!string.IsNullOrEmpty(invalidZipMessage))
                {
                    throw new InvalidOperationException(invalidZipMessage);
                }
            }

            //if (string.IsNullOrEmpty(customerEditModel.TargetYear))
            //{
            //    throw new InvalidOperationException("Target Year is required");
            //}

            if (!string.IsNullOrEmpty(customerEditModel.TargetYear))
            {
                int targetYear;
                var validTargetYear = int.TryParse(customerEditModel.TargetYear, out targetYear);
                if (!validTargetYear || targetYear < DateTime.Now.Year || targetYear > (DateTime.Now.Year + 2))
                {
                    throw new InvalidOperationException("Invalid Target Year");
                }
            }

            if (!isMemberUploadByAces && !string.IsNullOrEmpty(customerEditModel.Email) && !IsValidEmailAddress(customerEditModel.Email))
            {
                throw new InvalidOperationException("Invalid Customer Email Address.");
            }

            if (!isMemberUploadByAces && !string.IsNullOrEmpty(customerEditModel.AlternateEmail) && !IsValidEmailAddress(customerEditModel.AlternateEmail))
            {
                throw new InvalidOperationException("Invalid Customer Alternate Email Address.");
            }

            if (!isMemberUploadByAces && !string.IsNullOrEmpty(customerEditModel.PcpEmail) && !IsValidEmailAddress(customerEditModel.PcpEmail))
            {
                throw new InvalidOperationException("Invalid PCP Email Address.");
            }

            if (!isMemberUploadByAces)
            {
                if ((!string.IsNullOrEmpty(customerEditModel.BillingMemberId) || !string.IsNullOrEmpty(customerEditModel.BillingMemberPlan)) && (customerEditModel.BillingMemberPlanYear == null || customerEditModel.BillingMemberPlanYear == 0))
                {
                    throw new InvalidOperationException("Billing Year is required");
                }

                if (customerEditModel.BillingMemberPlanYear.HasValue && customerEditModel.BillingMemberPlanYear.Value > 0 && string.IsNullOrEmpty(customerEditModel.BillingMemberId) && string.IsNullOrEmpty(customerEditModel.BillingMemberPlan))
                {
                    throw new InvalidOperationException("Billing MemberID or Billing Plan Name or both is required");
                }

                if (!string.IsNullOrEmpty(customerEditModel.WarmTransferAllowed) && (customerEditModel.WarmTransferAllowed.ToLower() != "yes" && customerEditModel.WarmTransferAllowed.ToLower() != "no"))
                {
                    throw new InvalidOperationException("Invalid WarmTransfer Allowed Value");
                }

                if (!string.IsNullOrEmpty(customerEditModel.WarmTransferAllowed) && string.IsNullOrEmpty(customerEditModel.WarmTransferYear))
                {
                    throw new InvalidOperationException("WarmTransfer Year is required");
                }
                if (!string.IsNullOrEmpty(customerEditModel.WarmTransferYear))
                {
                    int warmTransferYear;
                    var validWarmTransferYear = int.TryParse(customerEditModel.WarmTransferYear, out warmTransferYear);
                    if (!validWarmTransferYear || warmTransferYear < DateTime.Now.Year || warmTransferYear > (DateTime.Now.Year + 2))
                    {
                        throw new InvalidOperationException("Invalid WarmTransfer Year");
                    }
                }

                //if (customerEditModel.RequiredTest != null && customerEditModel.RequiredTest.Any())
                //{
                //    var pairs = TestType.A1C.GetNameValuePairs();
                //    var requiredTestExists = pairs.Where(x => customerEditModel.RequiredTest.Contains(x.SecondValue.ToLower())).Select(x => x.SecondValue.ToLower());

                //    var testNotExist = string.Empty;

                //    if (requiredTestExists != null && requiredTestExists.Any() && (requiredTestExists.Count() != customerEditModel.RequiredTest.Count()))
                //    {
                //        var testNotExistInSystem = customerEditModel.RequiredTest.Where(x => !requiredTestExists.Contains(x.ToLower())).Select(x => x).ToArray();

                //        testNotExist = string.Join(",", testNotExistInSystem);
                //    }
                //    else if (requiredTestExists == null || !requiredTestExists.Any())
                //    {
                //        testNotExist = string.Join(",", customerEditModel.RequiredTest);
                //    }

                //    if (!string.IsNullOrEmpty(testNotExist))
                //        throw new InvalidOperationException(testNotExist + " test alias name does not exist in HIP (Required Test)");

                //    if (string.IsNullOrEmpty(customerEditModel.ForYear))
                //    {
                //        throw new InvalidOperationException("For Year is required");
                //    }
                //    int forYear;
                //    var validforYear = int.TryParse(customerEditModel.ForYear, out forYear);
                //    if (!validforYear || forYear < DateTime.Now.Year || forYear > (DateTime.Now.Year + 2))
                //    {
                //        throw new InvalidOperationException("Invalid For Year");
                //    }
                //}
            }

            if (string.IsNullOrEmpty(customerEditModel.EligibilityYear))
            {
                throw new InvalidOperationException("Eligibility Year is required");
            }

            int eligibilityYear;
            var validEligibilityYear = int.TryParse(customerEditModel.EligibilityYear, out eligibilityYear);

            if (!validEligibilityYear || eligibilityYear < DateTime.Now.Year || eligibilityYear > (DateTime.Now.Year + 2))
            {
                throw new InvalidOperationException("Invalid Eligibility Year");
            }

            var activityPairs = UploadActivityType.MailOnly.GetNameValuePairs();
            var activityId    = activityPairs.First(x => customerEditModel.Activity.ToLower() == x.SecondValue.ToLower()).FirstValue;

            bool isNewCustomer = false;

            DateTime?dob = null;

            if (!string.IsNullOrEmpty(customerEditModel.Dob))
            {
                try
                {
                    dob = Convert.ToDateTime(customerEditModel.Dob);
                }
                catch (Exception ex)
                {
                    throw new Exception("DOB is not in correct format. Please Provide in MM/DD/YYYY", ex);
                }
            }

            if (isMemberUploadByAces)
            {
                if (string.IsNullOrEmpty(customerEditModel.Product))
                {
                    throw new InvalidOperationException("Product Type is required");
                }

                if (!string.IsNullOrEmpty(customerEditModel.Product))
                {
                    var productTypePairs = ProductType.CHA.GetNameValuePairs();

                    if (!productTypePairs.Any(x => x.SecondValue.ToLower() == customerEditModel.Product.ToLower()))
                    {
                        throw new InvalidOperationException("Invalid Product Type");
                    }
                }

                if (string.IsNullOrEmpty(customerEditModel.DNCFlag))
                {
                    throw new InvalidOperationException("DNC is required");
                }
                if (!string.IsNullOrEmpty(customerEditModel.DNCFlag))
                {
                    if (customerEditModel.DNCFlag.Trim() == "1")
                    {
                        customerEditModel.DNCFlag = "yes";
                    }
                    else if (customerEditModel.DNCFlag.Trim() == "0")
                    {
                        customerEditModel.DNCFlag = "no";
                    }
                    else
                    {
                        throw new InvalidOperationException("Invalid DNC, Only 0/1 is accepted.");
                    }
                }
            }
            var customer = _customerRepository.GetCustomerForCorporate(customerEditModel.FirstName, customerEditModel.MiddleName, customerEditModel.LastName,
                                                                       customerEditModel.Email, PhoneNumber.ToNumber(customerEditModel.PhoneHome), PhoneNumber.ToNumber(customerEditModel.PhoneCell),
                                                                       dob, corporateModel == null ? string.Empty : corporateModel.Tag, sb);

            if (customer == null && (sb != null && sb.ToString() != ""))
            {
                throw new Exception("");
            }
            else
            {
                sb = new StringBuilder();
            }

            //IEnumerable<EventCustomer> eventCustomers = null;

            //if (customer != null && customer.CustomerId > 0)
            //{
            //    eventCustomers = _eventCustomerPreApprovedTestService.GetFutureEventCustomers(customer.CustomerId);
            //}

            if (string.IsNullOrWhiteSpace(customerEditModel.State) || string.IsNullOrWhiteSpace(customerEditModel.City) || string.IsNullOrWhiteSpace(customerEditModel.Zip))
            {
                var addressErrorMessage = "";
                if (string.IsNullOrWhiteSpace(customerEditModel.State))
                {
                    addressErrorMessage = addressErrorMessage + "State";
                }

                if (string.IsNullOrWhiteSpace(customerEditModel.City))
                {
                    addressErrorMessage = string.IsNullOrWhiteSpace(addressErrorMessage) ? "City" : addressErrorMessage + ", City";
                }

                if (string.IsNullOrWhiteSpace(customerEditModel.Zip))
                {
                    addressErrorMessage = string.IsNullOrWhiteSpace(addressErrorMessage) ? "Zip" : addressErrorMessage + ", Zip";
                }

                addressErrorMessage = addressErrorMessage + " is required.";

                throw new Exception(addressErrorMessage);
            }

            AddressSanitizingInvalidZip(customerEditModel);

            if (customer != null && !customersWithAcesId.IsNullOrEmpty())
            {
                customersWithAcesId = customersWithAcesId.Where(x => x.CustomerId != customer.CustomerId).ToList();
            }

            if (!customersWithAcesId.IsNullOrEmpty())
            {
                customersWithAcesId.ForEach(x => x.AcesId = "");
            }

            using (var scope = new TransactionScope())
            {
                if (!customersWithAcesId.IsNullOrEmpty())
                {
                    foreach (var member in customersWithAcesId)
                    {
                        _customerService.SaveCustomerOnly(member, organizationRoleUserId);
                    }

                    customerWithSameAcesId.AcesId     = customerEditModel.AcesId;
                    customerWithSameAcesId.CustomerId = customer != null ? customer.CustomerId : 0;
                    customerWithSameAcesId.CustomersWithSameAcesId = string.Join(",", customersWithAcesId.Select(x => x.CustomerId));
                }

                var updatedCustomer = _customerRegistrationService.RegisterCorporateCustomer(customer, customerEditModel, corporateModel == null ? string.Empty : corporateModel.Tag,
                                                                                             createdByOrgRoleUser, languages, labs, sb, activityId, source, out isNewCustomer);

                if (customTags != null && customTags.Any() && updatedCustomer != null)
                {
                    foreach (var customTag in customTags)
                    {
                        _corporateCustomerCustomTagService.Save(new CorporateCustomerCustomTag
                        {
                            CustomerId           = updatedCustomer.CustomerId,
                            IsActive             = true,
                            Tag                  = customTag,
                            DataRecorderMetaData = new DataRecorderMetaData(new OrganizationRoleUser(organizationRoleUserId), DateTime.Now, null)
                        });
                    }
                }

                customerEditModel.CustomerId = updatedCustomer.CustomerId;

                //if (!eventCustomers.IsNullOrEmpty())
                //{
                //    var list = _eventCustomerPreApprovedTestService.MarkcustomerForAdjustOrder(customerEditModel, eventCustomers, createdByOrgRoleUser, updatedCustomer, corporateModel.UploadCorporateId);

                //    if (!list.IsNullOrEmpty())
                //        adjustOrderForCustomerEditModel = adjustOrderForCustomerEditModel.Concat(list).ToList();
                //}

                scope.Complete();
            }

            UpdateNeedVerificationAddress(customerEditModel);

            if (isNewCustomer && customerList != null)
            {
                customerList.Add(customerEditModel.CustomerId);
            }

            UpdateCorporateMemberUpload(corporateUploadId, customerEditModel);

            customerIdWithSameAcesId = customerWithSameAcesId;
        }