public async Task<ActionResult> Add(ProjectAddViewModel viewModel)
        {
            Project model;
            UserProfile profile = await GetUserProfile();

            if (viewModel != null && viewModel.Project != null)
            {
                model = viewModel.Project;
            }
            else
            {
                model = new Project();
                PrepareModelForView(model);

                model.StatusCode = "T";
                model.ExpectedOrder = DateTime.Now.Date;

                viewModel = new ProjectAddViewModel();
                viewModel.Project = model;
            }

            model.CreationDate = DateTime.Now;
            model.SalesEngineer = profile.DisplayName;

            string bearerToken = cache.Get<string>("BearerToken");
            if (Request.IsAuthenticated && bearerToken == null)
            {
                var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
                var _authenticationContext = new AuthenticationContext(OfficeSettings.Authority, new ADALTokenCache(signInUserId));
                var token = await OfficeIntegration.GetTokenAsync(_authenticationContext, OfficeSettings.APIResourceId);
                if (token != null)
                {
                    bearerToken = cache.Set("BearerToken", token.AccessToken);
                }
            }
            ViewBag.Token = bearerToken;
            ViewBag.ProbabilitySelector = new SelectList(new[]
            {
                new { Name = "< 25", Value = 25 },
                new { Name = "50", Value = 50 },
                new { Name = "> 90", Value = 90 }
            }, "Value", "Name");

            PrepareModelForView(viewModel.Project);
            return View(viewModel);
        }
        public ActionResult Add(ProjectAddViewModel viewModel, bool ignoreSimilarProjects = false)
        {
            Project model = viewModel.Project;
            if (ModelState.IsValid)
            {
                if (!ignoreSimilarProjects)
                {
                    // Check for duplicate projects

                    // Manually load lazily loaded fields to create more accurate results
                    model.Sector = uow.SectorRepository.Get(s => s.Id.Equals(model.SectorId)).FirstOrDefault();
                    model.Type = uow.ProjectTypeRepository.Get(t => t.Id.Equals(model.TypeId)).FirstOrDefault();
                    model.Status = uow.ProjectStatusRepository.Get(s => s.Code.Equals(model.StatusCode)).FirstOrDefault();

                    var existingProjects = uow.ProjectRepository.Get(p => !p.StatusCode.Equals("D"));
                    var similarProjects = new Dictionary<Tuple<int, string>, double>();
                    foreach (var project in existingProjects)
                    {
                        var similarity = DistanceTo(model, project);
                        if (similarity > 0.7) //TODO: Need to find a good boundary, also make lazily loaded fields reliable in these results
                        {
                            similarProjects.Add(Tuple.Create(project.Id, project.Name), similarity);
                        }
                    }
                    if (similarProjects.Count > 0)
                    {
                        // Redirect to project comparison page if any are found
                        ModelState.AddModelError("PossibleDuplicate", "Similar projects found; review and then re-submit if appropriate.");
                        PrepareModelForView(model);
                        viewModel.SimilarProjects = similarProjects;
                        return View(viewModel);
                    }
                }

                var insertedProject = uow.ProjectRepository.Insert(model);
                try
                {
                    model.CreationDate = DateTime.Now;
                    if (insertedProject.LightingDesignRequired)
                    {
                        var lpd = new LightingDesign();
                        lpd.ProjectId = insertedProject.Id;
                        lpd.Submitted = DateTime.Now.Date;
                        lpd.RequiredBy = insertedProject.DesignRequiredBy ?? DateTime.Now.Date;
                        var insertedLpd = uow.LightingDesignRepository.Insert(lpd);
                    }
                    uow.Save();
                    return RedirectToAction("Detail", new { id = insertedProject.Id });
                }
                catch (DbEntityValidationException ex)
                {
                    foreach (var error in ex.EntityValidationErrors)
                    {
                        foreach (var innerError in error.ValidationErrors)
                        {
                            ModelState.AddModelError(innerError.PropertyName, innerError.ErrorMessage);
                        }
                    }
                }
            }
            if (model.Types == null || model.StatusCodes == null || model.Reasons == null || model.Sector == null)
            {
                PrepareModelForView(model);
            }
            return View(viewModel);
        }