Ejemplo n.º 1
0
        public ActionResult Create(HRD hrd)
        {
            var year     = hrd.Year;
            var userID   = _needAssessmentService.GetUserProfileId(HttpContext.User.Identity.Name);
            var seasonID = hrd.SeasonID.HasValue ? hrd.SeasonID.Value:1;
            var rationID = hrd.RationID;


            var planName           = hrd.Plan.PlanName;
            var startDate          = hrd.Plan.StartDate;
            var firstDayOfTheMonth = startDate.AddDays(1 - startDate.Day);
            var endDate            = firstDayOfTheMonth.AddMonths(hrd.Plan.Duration).AddDays(-1);

            if (ModelState.IsValid)
            {
                var existingPlan = _planService.FindBy(m => m.PlanName == planName && m.ProgramID == 1).FirstOrDefault();
                if (existingPlan != null)
                {
                    ModelState.AddModelError("Errors", @"HRD Name already Exists Please Change HRD Name");
                }
                else
                {
                    try
                    {
                        _planService.AddHRDPlan(planName, firstDayOfTheMonth, endDate);
                        var plan   = _planService.FindBy(m => m.PlanName == planName).FirstOrDefault();
                        var planID = plan.PlanID;
                        _hrdService.AddHRD(year, userID, seasonID, rationID, planID);
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception exception)
                    {
                        var log = new Logger();
                        log.LogAllErrorsMesseges(exception, _log);
                        ModelState.AddModelError("Errors", @"Unable To Create New HRD");
                        //ViewBag.Error = "HRD for this Season and Year already Exists";
                    }
                }
            }

            ViewBag.Year     = hrd.Year;
            ViewBag.RationID = new SelectList(_rationService.GetAllRation(), "RationID", "RefrenceNumber", hrd.RationID = 1);
            ViewBag.SeasonID = new SelectList(_seasonService.GetAllSeason(), "SeasonID", "Name");
            return(View(hrd));
        }