public ActionResult PCDelete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PlanCreditF2 planCredit = db.PlanCreditsF2.Find(id);

            if (planCredit == null)
            {
                return(HttpNotFound());
            }

            //Проверяем наличие у пользователя прав для работы с данной сущностью
            if (!UsrWksMethods.isAllowed(db, planCredit.ProjectId))
            {
                return(View("~/Views/Shared/AccessDenied.cshtml"));
            }

            string _prgName = db.Projects.Where(x => x.id == planCredit.ProjectId).Select(x => x.ShortName).FirstOrDefault().ToString();

            planCredit.ProjectName = _prgName;

            string _orgName = db.Organizations.Where(x => x.id == planCredit.OrganizationId).Select(x => x.Title).FirstOrDefault().ToString();

            planCredit.OrganizationName = _orgName;

            string _incName = db.Incomes.Where(x => x.Id == planCredit.IncomeId).Select(x => x.Name).FirstOrDefault().ToString();

            planCredit.IncomeName = _incName;

            return(View(planCredit));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                PlanCreditF2 planCredit = db.PlanCreditsF2.FirstOrDefault(x => x.PlanCreditF2Id == id);
                if (planCredit == null)
                {
                    TempData["MessageError"] = "Удаляемый объект отсутствует в базе данных";
                    return(RedirectToAction("PCShow"));
                }

                //Проверяем наличие у пользователя прав для работы с данной сущностью
                if (!UsrWksMethods.isAllowed(db, planCredit.ProjectId))
                {
                    return(View("~/Views/Shared/AccessDenied.cshtml"));
                }

                db.PlanCreditsF2.Remove(planCredit);
                db.SaveChanges();
                TempData["MessageOK"] = "Информация удалена";
                return(RedirectToAction("PCShow"));
            }
            catch (Exception ex)
            {
                ViewBag.ErMes   = ex.Message;
                ViewBag.ErStack = ex.StackTrace;
                ViewBag.ErInner = ex.InnerException.InnerException.Message;
                return(View("Error"));
            }
        }
        public ActionResult PCCreate()
        {
            PlanCreditF2 _model = new PlanCreditF2();

            _model.Date             = DateTime.Today;
            ViewData["periodItems"] = new SelectList(db.PlanningPeriods, "PlanningPeriodId", "PeriodName");
            return(View(_model));
        }
        public ActionResult PCCreate([Bind(Include = "PlanCreditId,Date,Sum,ProjectId,OrganizationId,Appointment,UserId,PeriodId,IncomeId")] PlanCreditF2 planCredit)
        {
            if (ModelState.IsValid)
            {
                //Запрещаем руководителям проектов добавление/редактирование планов текущего и предыдущих месяцев
                int NextMonth = DateTime.Today.Month + 1;
                int WYear     = DateTime.Today.Year;
                if (NextMonth > 12)
                {
                    NextMonth = 1;
                    WYear    += 1;
                }
                int  PlanMonth    = planCredit.Date.Month;
                int  PlanYear     = planCredit.Date.Year;
                bool isPrgManager = System.Web.HttpContext.Current.User.IsInRole("ProjectManager");
                if (isPrgManager && ((PlanMonth < NextMonth && PlanYear <= DateTime.Today.Year)))
                {
                    TempData["MessageError"] = "Руководителям проектов запрещено добавление/редактирование планов текущего и предыдущих месяцев";
                    ViewData["periodItems"]  = new SelectList(db.PlanningPeriods, "PlanningPeriodId", "PeriodName");
                    ViewData["prgSelect"]    = db.Projects.FirstOrDefault(x => x.id == planCredit.ProjectId).ShortName;
                    ViewData["orgSelect"]    = db.Organizations.FirstOrDefault(x => x.id == planCredit.OrganizationId).Title;
                    ViewData["incSelect"]    = db.Incomes.FirstOrDefault(x => x.Id == planCredit.IncomeId).Name;
                    return(View(planCredit));
                }

                try
                {
                    //Получаем идентификатор текущего пользователя
                    using (ApplicationDbContext aspdb = new ApplicationDbContext())
                    {
                        var user = System.Web.HttpContext.Current.User.Identity.GetUserId();
                        planCredit.UserId = user;
                    }
                    db.PlanCreditsF2.Add(planCredit);
                    db.SaveChanges();
                    TempData["MessageOK"] = "Информация добавлена";
                    return(RedirectToAction("PCShow"));
                }
                catch (Exception ex)
                {
                    ViewBag.ErMes   = ex.Message;
                    ViewBag.ErStack = ex.StackTrace;
                    ViewBag.ErInner = ex.InnerException.InnerException.Message;
                    return(View("Error"));
                }
            }

            TempData["MessageError"] = "Ошибка валидации модели";
            ViewData["periodItems"]  = new SelectList(db.PlanningPeriods, "PlanningPeriodId", "PeriodName");
            return(View(planCredit));
        }
        public ActionResult PCDetails(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PlanCreditF2 planCredit = (from _pc in db.PlanCreditsF2
                                       join _pname in db.Projects on _pc.ProjectId equals _pname.id
                                       join _org in db.Organizations on _pc.OrganizationId equals _org.id
                                       join _inc in db.Incomes on _pc.IncomeId equals _inc.Id
                                       join _period in db.PlanningPeriods on _pc.PeriodId equals _period.PlanningPeriodId
                                       where (_pc.PlanCreditF2Id == id)
                                       select new
            {
                PlanCreditF2Id = _pc.PlanCreditF2Id,
                Sum = _pc.Sum,
                Date = _pc.Date,
                ProjectId = _pc.ProjectId,
                ProjectName = _pname.ShortName,
                OrganizationName = _org.Title,
                IncomesName = _inc.Name,
                PeriodName = _period.PeriodName,
                Appointment = _pc.Appointment
            }).AsEnumerable().Select(x => new PlanCreditF2
            {
                PlanCreditF2Id   = x.PlanCreditF2Id,
                Sum              = x.Sum,
                Date             = x.Date,
                ProjectId        = x.ProjectId,
                ProjectName      = x.ProjectName,
                OrganizationName = x.OrganizationName,
                IncomeName       = x.IncomesName,
                PeriodName       = x.PeriodName,
                Appointment      = x.Appointment
            }).FirstOrDefault();

            if (planCredit == null)
            {
                return(HttpNotFound());
            }

            //Проверяем наличие у пользователя прав для работы с данной сущностью
            if (!UsrWksMethods.isAllowed(db, planCredit.ProjectId))
            {
                return(View("~/Views/Shared/AccessDenied.cshtml"));
            }

            return(View(planCredit));
        }
        public ActionResult PCEdit(int?id, string isClone)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PlanCreditF2 planCredit = db.PlanCreditsF2.Find(id);

            if (planCredit == null)
            {
                return(HttpNotFound());
            }

            //Проверяем наличие у пользователя прав для работы с данной сущностью
            if (!UsrWksMethods.isAllowed(db, planCredit.ProjectId))
            {
                return(View("~/Views/Shared/AccessDenied.cshtml"));
            }

            string _prgName = db.Projects.Where(x => x.id == planCredit.ProjectId).Select(x => x.ShortName).FirstOrDefault().ToString();

            ViewData["ProjectName"] = _prgName;

            string _orgName = db.Organizations.Where(x => x.id == planCredit.OrganizationId).Select(x => x.Title).FirstOrDefault().ToString();

            ViewData["OrganizationName"] = _orgName;

            string _incName = db.Incomes.Where(x => x.Id == planCredit.IncomeId).Select(x => x.Name).FirstOrDefault().ToString();

            ViewData["IncomeName"] = _incName;

            ViewData["periodItems"] = new SelectList(db.PlanningPeriods, "PlanningPeriodId", "PeriodName");

            if (Convert.ToBoolean(isClone))
            {
                planCredit.Date = DateTime.Today;
                return(View("PCClone", planCredit));
            }
            else
            {
                return(View(planCredit));
            }
        }
        public ActionResult PCEdit([Bind(Include = "PlanCreditF2Id,Date,Sum,ProjectId,OrganizationId,Appointment,PeriodId, IncomeId")] PlanCreditF2 planCredit)
        {
            //Проверяем наличие у пользователя прав для работы с данной сущностью
            if (!UsrWksMethods.isAllowed(db, planCredit.ProjectId))
            {
                return(View("~/Views/Shared/AccessDenied.cshtml"));
            }

            if (ModelState.IsValid)
            {
                //Запрещаем руководителям проектов добавление/редактирование планов текущего и предыдущих месяцев
                int NextMonth = DateTime.Today.Month + 1;
                int WYear     = DateTime.Today.Year;
                if (NextMonth > 12)
                {
                    NextMonth = 1;
                    WYear    += 1;
                }
                int  PlanMonth    = planCredit.Date.Month;
                int  PlanYear     = planCredit.Date.Year;
                bool isPrgManager = System.Web.HttpContext.Current.User.IsInRole("ProjectManager");
                if (isPrgManager && ((PlanMonth < NextMonth && PlanYear <= DateTime.Today.Year)))
                {
                    TempData["MessageError"] = "Руководителям проектов запрещено добавление/редактирование планов текущего и предыдущих месяцев";
                    ViewData["periodItems"]  = new SelectList(db.PlanningPeriods, "PlanningPeriodId", "PeriodName");
                    ViewData["prgSelect"]    = db.Projects.FirstOrDefault(x => x.id == planCredit.ProjectId).ShortName;
                    ViewData["orgSelect"]    = db.Organizations.FirstOrDefault(x => x.id == planCredit.OrganizationId).Title;
                    ViewData["incSelect"]    = db.Incomes.FirstOrDefault(x => x.Id == planCredit.IncomeId).Name;

                    return(View(planCredit));
                }

                try
                {
                    //Получаем идентификатор текущего пользователя
                    using (ApplicationDbContext aspdb = new ApplicationDbContext())
                    {
                        var user = System.Web.HttpContext.Current.User.Identity.GetUserId();
                        planCredit.UserId = user;
                    }

                    db.Database.Log            = (s => System.Diagnostics.Debug.WriteLine(s));
                    db.Entry(planCredit).State = EntityState.Modified;
                    db.SaveChanges();
                    TempData["MessageOK"] = "Информация обновлена";
                    return(RedirectToAction("PCShow"));
                }
                catch (Exception ex)
                {
                    ViewBag.ErMes   = ex.Message;
                    ViewBag.ErStack = ex.StackTrace;
                    ViewBag.ErInner = ex.InnerException.InnerException.Message;
                    return(View("Error"));
                }
            }

            TempData["MessageError"] = "Ошибка валидации модели";
            string _prgName = db.Projects.Where(x => x.id == planCredit.ProjectId).Select(x => x.ShortName).FirstOrDefault().ToString();

            ViewData["ProjectName"] = _prgName;
            return(View(planCredit));
        }