コード例 #1
0
        public FunctionalityScore MakeScore(FunctionalityScore score, ccEntities db)
        {
            db.ContextOptions.LazyLoadingEnabled   = false;
            db.ContextOptions.ProxyCreationEnabled = false;

            ViewBag.Success = false;

            ModelState.Clear();

            var functiolnalityLevel = FunctionalityLevel.GetLevelByScore(db.FunctionalityLevels, score);

            if (functiolnalityLevel != null)
            {
                score.FunctionalityLevelId = functiolnalityLevel.Id;
            }

            var client = db.Clients.SingleOrDefault(f => f.Id == score.ClientId);

            if (score.StartDate < client.JoinDate)
            {
                ModelState.AddModelError(string.Empty, "Start Date cannot be earlier than client's join date.");
            }

            var existing = db.FunctionalityScores.Any(f => f.StartDate == score.StartDate && f.ClientId == score.ClientId);

            if (existing)
            {
                ModelState.AddModelError(string.Empty,
                                         "Duplicate functionality scores are not allowed"
                                         );
            }



            var lastReport = ccEntitiesExtensions.LastSubmittedHcRepDate(score.ClientId);

            if (this.Permissions.User.RoleId != (int)FixedRoles.Admin && score.StartDate < lastReport)
            {
                ModelState.AddModelError(string.Empty, "Start date cannot be within an already submitted Financial Report period.");
            }

            TryValidateModel(score);

            if (ModelState.IsValid)
            {
                try
                {
                    score.UpdatedAt = DateTime.Now;
                    score.UpdatedBy = this.Permissions.User.Id;


                    db.FunctionalityScores.AddObject(score);

                    var rowsUpdated = db.SaveChanges();
                    if (rowsUpdated > 0)
                    {
                        score = new FunctionalityScore();
                    }
                    ViewBag.Success = true;
                }
                catch (ValidationException ex)
                {
                    ModelState.AddModelError(ex.GetType().ToString(), ex);
                }
                catch (UpdateException ex)
                {
                    ModelState.AddModelError(ex.GetType().ToString(), ex);
                    ModelState.AddModelError(string.Empty, ex.InnerException.Message);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            return(score);
        }