//public string GetEmailBody()
        //{
        //    var emailBody = RazorHelper.RenderRazorViewToString("Email/AddNewBusiness", this);
        //    return emailBody;
        //}


        //internal string GetEmailBody(VMLead model)
        //{
        //    ViewData.Model = new EBP.Api.Models.EmailModel.LeadEmailModel
        //    {
        //        ManagerName=model.ManagerName,
        //        RepId=model.RepId,
        //        RepName=model.RepName,
        //    };
        //    var emailBody = RazorHelper.RenderRazorViewToString("~/Views/Shared/Email/NewLeadToManager.cshtml", this );
        //    return emailBody;
        //}


        internal string GetEmailBody(VMLead model)
        {
            ViewData.Model = new EBP.Api.Models.EmailModel.LeadEmailModel
            {
                ManagerName = model.ManagerName,
                RepId       = model.RepId,
                RepName     = model.RepName,
            };
            var emailBody = RazorHelper.RenderRazorViewToString("~/Views/Shared/Email/NewLeadToManager.cshtml", this);

            return(emailBody);
        }
Example #2
0
        public IHttpActionResult InsertLeadData(VMLead model)
        {
            List <string> adddress = new List <string>()
            {
                "model.PracticeAddressLine1",
                "model.City",
                "model.StateId",
                "model.Zip",
                "model.PhoneNumber"
            };

            List <string> reporrepgroup = null;

            bool hasAnyAddressFields = adddress.Any(a => ModelState.IsValidField(a)),
                 hasAllAddressFields = adddress.All(a => ModelState.IsValidField(a));

            if (!HasRight(new string[] { "WRREP" }) && IsRep)
            {
                ModelState["model.RepId"].Errors.Clear();
                ModelState["model.RepGroupId"].Errors.Clear();
            }
            else
            {
                reporrepgroup = new List <string>()
                {
                    "model.RepId",
                    "model.RepGroupId"
                };
            }

            if (hasAnyAddressFields && !hasAllAddressFields)
            {
                var errorList = ModelState.Where(a => a.Value.Errors.Any()).Select(s => new
                {
                    Key     = s.Key.Split('.').Last(),
                    Message = s.Value.Errors[0].ErrorMessage
                });
                return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = errorList }));
            }

            var isRepandRepgroupSelected = reporrepgroup != null && reporrepgroup.Any(a => ModelState.IsValidField(a));

            if (!isRepandRepgroupSelected)
            {
                var errorList = ModelState.Where(a => a.Value.Errors.Any() && reporrepgroup.Contains(a.Key)).Select(s => new
                {
                    Key     = s.Key.Split('.').Last(),
                    Message = s.Value.Errors[0].ErrorMessage
                });
                return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = errorList }));
            }

            var repository = new RepositoryLead();
            var response   = new DataResponse <EntityLead>();

            if (!string.IsNullOrEmpty(model.PracticeName))
            {
                #region Fully Valid

                var entityLead = new EntityLead
                {
                    LeadSourceId       = model.LeadSourceId,
                    LeadServiceIntrest = model.ServiceInterest,
                    IsActive           = true,
                    RepGroupId         = model.RepGroupId,
                    RepId = model.RepId,
                    ContactPreferenceId = 1,
                    OtherLeadSource     = model.OtherLeadSource,
                    IsConverted         = false,
                };

                entityLead.Practice = new EntityPractice
                {
                    Name             = model.PracticeName,
                    SpecialityTypeId = model.SpecialityTypeId,
                    //Fax = model.Fax
                };

                if (hasAllAddressFields)
                {
                    //entityLead.Practice.Fax = model.Fax;
                    entityLead.Practice.Address = new List <EntityPracticeAddress> {
                        new EntityPracticeAddress {
                            Id            = model.AddressId == null ? 0 : model.AddressId.Value,
                            AddressIndex  = -1,
                            City          = model.City,
                            Line1         = model.PracticeAddressLine1,
                            Line2         = model.PracticeAddressLine2,
                            AddressTypeId = 1,
                            StateId       = model.StateId ?? 0,
                            Zip           = model.Zip,
                            Fax           = model.Fax,
                            Phone         = new List <EntityPracticePhone> {
                                new EntityPracticePhone {
                                    PhoneNumber = model.PhoneNumber,
                                    Extension   = model.PhoneExtension
                                }
                            }
                        }
                    };
                }

                entityLead.Practice.Specialities = new List <EntityPracticeSpeciality>();
                var specialities = new List <EntityPracticeSpeciality>();

                entityLead.PracticeTypeId = model.SpecialityTypeId == 0 ? null : model.SpecialityTypeId;
                if (model.SpecialityTypeId == 1)
                {
                    specialities.Add(new EntityPracticeSpeciality {
                        PracticeSpecialityId = model.SpecialityId ?? 0
                    });
                }
                else if (model.SpecialityTypeId == 2 && model.SpecialityIds != null && model.SpecialityIds.Count() > 0)
                {
                    foreach (string sId in model.SpecialityIds)
                    {
                        specialities.Add(new EntityPracticeSpeciality {
                            PracticeSpecialityId = int.Parse(sId)
                        });
                    }
                }
                else if (model.SpecialityTypeId == 3 && !string.IsNullOrEmpty(model.NewSpectialityName))
                {
                    entityLead.Practice.SpecialityType = model.NewSpectialityName;
                }
                if (specialities.Count > 0)
                {
                    entityLead.Practice.Specialities = specialities;
                }

                #region Save Location
                if (model.Locations != null && model.Locations.Count() > 0)
                {
                    var addressList = new List <EntityPracticeAddress>();

                    model.Locations.ForEach(a => addressList.Add(new EntityPracticeAddress
                    {
                        Id            = a.AddressId == null ? 0 : a.AddressId.Value,
                        AddressIndex  = a.AddressIndex.Value,
                        City          = a.City,
                        Line1         = a.AddressLine1,
                        Line2         = a.AddressLine2,
                        AddressTypeId = 2,
                        StateId       = a.StateId ?? 0,
                        Zip           = a.Zip,
                        ManagerName   = a.ManagerName,
                        Phone         = new List <EntityPracticePhone> {
                            new EntityPracticePhone {
                                PhoneNumber = a.PhoneNumber,
                                Extension   = a.Extension
                            }
                        }
                    }));

                    if (addressList.Count > 0)
                    {
                        entityLead.Practice.Address = entityLead.Practice.Address.Concat(addressList);
                    }
                }
                #endregion

                #region Save Providers

                if (hasAnyAddressFields && hasAllAddressFields)
                {
                    if (model.Providers != null && model.Providers.Count() > 0)
                    {
                        var isMultipleProviders = model.Providers.GroupBy(a => a.NPI).Any(a => a.Count() > 1);
                        if (isMultipleProviders)
                        {
                            return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = new { Key = "NPI", Message = "NPI is Repeating" } }));
                        }

                        var providerList = new List <EntityProvider>();

                        model.Providers.ForEach(a =>
                                                providerList.Add(
                                                    new EntityProvider
                        {
                            Id           = a.Id == null ? 0 : a.Id.Value,
                            DegreeId     = a.DegreeId ?? 0,
                            FirstName    = a.FirstName,
                            LastName     = a.LastName,
                            MiddleName   = a.MiddleName,
                            NPI          = a.NPI,
                            AddressIndex = a.Location != null ? (a.Location.AddressIndex == null ? -1 : a.Location.AddressIndex.Value) : -1,
                            Address      = a.Location != null ? new EntityProviderAddress
                            {
                                Id            = a.Location.AddressId == null ? 0 : a.Location.AddressId.Value,
                                City          = a.Location.City,
                                Line1         = a.Location.AddressLine1,
                                Line2         = a.Location.AddressLine2,
                                AddressTypeId = 2,
                                StateId       = a.Location.StateId.Value,
                                Zip           = a.Location.Zip,
                                ManagerName   = a.Location.ManagerName,
                                Phone         = a.Location == null ? null : new List <EntityPracticePhone> {
                                    new EntityPracticePhone {
                                        PhoneNumber = a.Location.PhoneNumber,
                                        Extension   = a.Location.Extension
                                    }
                                }
                            } : null
                        }));

                        if (providerList.Count > 0)
                        {
                            entityLead.Practice.Providers = providerList;
                        }
                    }
                }
                #endregion

                entityLead.UpdatedBy     = entityLead.CreatedBy = entityLead.CurrentUserId = CurrentUser.Id;
                entityLead.BusinessId    = CurrentUser.BusinessId;
                entityLead.CreatedByName = string.Format("{0} {1}", CurrentUser.FirstName, CurrentUser.LastName);

                if (model.Id > 0)
                {
                    entityLead.LeadId = model.Id ?? 0;
                    response          = repository.Update(entityLead);
                }
                else
                {
                    string services = string.Empty, providers = string.Empty, practiceAddress = string.Empty;

                    response = repository.Insert(entityLead);
                    var responseModel = response.Model;

                    if (responseModel.Practice.Providers != null && responseModel.Practice.Providers.Count() > 0)
                    {
                        foreach (var item in responseModel.Practice.Providers)
                        {
                            providers = providers + item.FirstName + " " + item.LastName + " (" + item.NPI + ") <br />";
                        }
                    }

                    var primaryAddress = responseModel.Practice.Address.FirstOrDefault();
                    if (primaryAddress != null)
                    {
                        var stateName = new RepositoryLookups().GetAllStates().Model.List.Where(a => a.Id == primaryAddress.StateId).First().Value;
                        practiceAddress = string.Format("{0}, {1}, {2}, {3}, {4}", primaryAddress.Line1, primaryAddress.Line2, primaryAddress.City, stateName, primaryAddress.Zip);
                    }

                    if (responseModel.LeadId > 0)
                    {
                        EmailNotification emailNotify = new EmailNotification
                        {
                            PracticeName         = model.PracticeName,
                            PracticeAddress      = practiceAddress,
                            Providers            = providers,
                            Services             = services,
                            RepFirstName         = responseModel.Rep.FirstName,
                            RepMiddleName        = responseModel.Rep.MiddleName,
                            RepLastName          = responseModel.Rep.LastName,
                            RepEmail             = responseModel.Rep.RepEmail,
                            Managers             = responseModel.Rep.Managers,
                            CurrentUserFirstName = CurrentUser.FirstName,
                            CurrentUserLastName  = CurrentUser.LastName,
                            RootPath             = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath,
                            ReturnUrl            = ConfigurationManager.AppSettings["BaseUrl"],
                        };
                        NewLeadEmailNotification(emailNotify);
                    }
                }

                #endregion
            }
            else
            {
                var errorList = ModelState.Where(a => a.Value.Errors.Any()).Select(s => new
                {
                    Key     = s.Key.Split('.').Last(),
                    Message = s.Value.Errors[0].ErrorMessage
                });
                return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = errorList }));
            }
            return(Ok <DataResponse>(response));
        }