public IHttpActionResult InsertAccountData(VMAccount model) { if (!IsRep && (model.RepId == null || model.RepId <= 0)) { List <dynamic> errorList = new List <dynamic>(); if (!HasRight(new string[] { "RDREPGRPALL" })) { errorList.Add(new { Message = "You do not have right to read repgroup" }); } if (!HasRight(new string[] { "WRREP" })) { errorList.Add(new { Message = "You do not have right to create rep" }); } if (errorList != null && errorList.Count <= 0) { errorList.Add(new { Message = "RepGroup and Rep are required" }); } return(Ok <dynamic>(new { Status = HttpStatusCode.PreconditionFailed, ResponseMessage = errorList })); } var repository = new RepositoryAccount(); var response = new DataResponse(); if (ModelState.IsValid) { var entityAccount = new EntityAccount { EnrolledDate = model.EnrolledDate, IsActive = model.IsActive, LeadSourceId = model.LeadSourceId, LeadServiceIntrest = model.ServiceInterest, RepGroupId = model.RepGroupId, RepId = model.RepId, IsConverted = true, ServiceIds = model.EnrolledServices }; entityAccount.Practice = new EntityPractice { //Fax = model.Fax, ReportDeliveryEmail = model.ReportDeliveryEmail, ReportDeliveryFax = model.ReportDeliveryFax, Name = model.PracticeName, SpecialityTypeId = model.SpecialityTypeId, ContactPreferenceId = model.MethodOfContact, Address = new List <EntityPracticeAddress> { new EntityPracticeAddress { Id = model.AddressId ?? 0, 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 } }, AddressId = model.AddressId ?? 0, ManagerName = model.ManagerName, ManagerEmail = model.ManagerEmail, ManagerPhone = model.ManagerPhone, BillingContact = model.BillingContact, BillingContactEmail = model.BillingContactEmail, BillingContactPhone = model.BillingContactPhone, officedayshrs = model.WorkingHours, } }, Contact = new List <EntityPracticeContact> { new EntityPracticeContact { BillingContact = model.BillingContact, BillingContactEmail = model.BillingContactEmail, BillingContactPhone = model.BillingContactPhone, ManagerName = model.ManagerName, ManagerPhone = model.ManagerPhone, ManagerEmail = model.ManagerEmail, officedayshrs = model.WorkingHours, AddressId = model.AddressId ?? 0 } } }; var specialities = new List <EntityPracticeSpeciality>(); entityAccount.Practice.Specialities = new List <EntityPracticeSpeciality>(); entityAccount.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)) { entityAccount.Practice.SpecialityType = model.NewSpectialityName; } if (specialities.Count > 0) { entityAccount.Practice.Specialities = specialities; } if (model.Locations != null) { var addressList = new List <EntityPracticeAddress>(); var contactList = new List <EntityPracticeContact>(); model.Locations.ToList().ForEach(a => addressList.Add(new EntityPracticeAddress { Id = a.AddressId ?? 0, AddressIndex = a.AddressIndex.Value, Line1 = a.AddressLine1, Line2 = a.AddressLine2, City = a.City, AddressTypeId = 2, StateId = a.StateId ?? 0, Zip = a.Zip, Fax = a.Fax, Phone = new List <EntityPracticePhone> { new EntityPracticePhone { PhoneNumber = a.PhoneNumber, Extension = a.Extension } }, AddressId = a.AddressId ?? 0, ManagerName = a.ManagerName, ManagerEmail = a.ManagerEmail, ManagerPhone = a.ManagerPhone, BillingContact = a.BillingContact, BillingContactEmail = a.BillingContactEmail, BillingContactPhone = a.BillingContactPhone, officedayshrs = a.WorkingHours, })); if (addressList.Count > 0) { entityAccount.Practice.Address = entityAccount.Practice.Address.Concat(addressList); } entityAccount.Practice.Address.ToList().ForEach(a => contactList.Add(new EntityPracticeContact { AddressId = a.AddressId, ManagerName = a.ManagerName, ManagerEmail = a.ManagerEmail, ManagerPhone = a.ManagerPhone, BillingContact = a.BillingContact, BillingContactEmail = a.BillingContactEmail, BillingContactPhone = a.BillingContactPhone, officedayshrs = a.officedayshrs, })); if (contactList.Count > 0) { entityAccount.Practice.Contact = contactList; } } if (model.Providers != null) { 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 ?? 0, 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, Fax = a.Location.Fax, 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) { entityAccount.Practice.Providers = providerList; } } entityAccount.UpdatedBy = entityAccount.CreatedBy = entityAccount.CurrentUserId = CurrentUser.Id; entityAccount.BusinessId = CurrentUser.BusinessId; entityAccount.CreatedByName = string.Format("{0} {1}", CurrentUser.FirstName, CurrentUser.LastName); if (model.Id > 0) //Update { entityAccount.Id = model.Id ?? 0; response = repository.Update(entityAccount); } else { if (model.LeadId > 0) //Converted from Lead { entityAccount.LeadId = model.LeadId; response = repository.ConvertToAccount(entityAccount); } else //New account { response = repository.Insert(entityAccount); } if (response.Id != null) { string services = string.Empty, providers = string.Empty, practiceAddress = string.Empty; if (entityAccount.ServiceIds != null) { var ids = ((IEnumerable <string>)entityAccount.ServiceIds).Select(a => Convert.ToInt32(a)).ToArray(); services = string.Join(", ", new RepositoryLookups().GetServicesById(ids)); } foreach (var item in entityAccount.Practice.Providers) { providers = providers + item.FirstName + " " + item.LastName + " (" + item.NPI + ") <br />"; } var primaryAddress = entityAccount.Practice.Address.First(); 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); EntityGroupManagerDetails RepModel = repository.RepDetailsByAccountId(response.Id).Model; EmailNotification emailNotify = new EmailNotification { PracticeName = model.PracticeName, PracticeAddress = practiceAddress, Providers = providers, Services = services, RepFirstName = RepModel.RepFirstName, RepMiddleName = RepModel.RepMiddleName, RepLastName = RepModel.RepLastName, RepEmail = RepModel.RepEmail, Managers = RepModel.Managers, CurrentUserFirstName = CurrentUser.FirstName, CurrentUserLastName = CurrentUser.LastName, RootPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, ReturnUrl = ConfigurationManager.AppSettings["BaseUrl"], }; NewAccountEmailNotification(emailNotify); } } } 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)); }