Ejemplo n.º 1
0
        public ActionResult Index(PaymentRequestSearchModel model)
        {
            const int RecordsPerPage = 20;
            var       pageIndex      = model.Page ?? 1;
            var       currentUser    = manager.FindById(User.Identity.GetUserId());

            var  results      = (from x in db.PaymentRequests select x);
            bool isDirOrAdmin = manager.IsInRole(currentUser.Id, SalaryRoles.Directors) || manager.IsInRole(currentUser.Id, SalaryRoles.Administrators);

            if (manager.IsInRole(currentUser.Id, SalaryRoles.Managers) && !isDirOrAdmin)
            {
                results = results.Where(x => currentUser.Id == (x.Project.Department.Boss != null ? x.Project.Department.Boss.Id : "nouser"));
            }
            else if (manager.IsInRole(currentUser.Id, SalaryRoles.Assistant) && !isDirOrAdmin)
            {
                results = results.Where(x => currentUser.Id == (x.Project.Department.Assistant != null ? x.Project.Department.Assistant.Id : "nouser"));
            }
            else if (manager.IsInRole(currentUser.Id, SalaryRoles.Managers) && manager.IsInRole(currentUser.Id, SalaryRoles.Assistant) && !isDirOrAdmin)
            {
                var r1 = results.Where(x => currentUser.Id == (x.Project.Department.Boss != null ? x.Project.Department.Boss.Id : "nouser"));
                var r2 = results.Where(x => currentUser.Id == (x.Project.Department.Assistant != null ? x.Project.Department.Assistant.Id : "nouser"));
                results = r1.Concat(r2);
            }

            if (model.SelectedStates == null)
            {
                if (Request.Cookies["SelectedStates"] == null)
                {
                    if (manager.IsInRole(currentUser.Id, "Administrators"))
                    {
                        model.SelectedStates = new PaymentRequestState[1] {
                            PaymentRequestState.Confirmed
                        }
                    }
                    ;
                    if (manager.IsInRole(currentUser.Id, "Directors"))
                    {
                        model.SelectedStates = new PaymentRequestState[1] {
                            PaymentRequestState.WaitConfirm
                        }
                    }
                    ;
                }
                else
                {
                    Object obj = JsonConvert.DeserializeObject(Request.Cookies["SelectedStates"].Value);
                    model.SelectedStates = ((IEnumerable)obj).Cast <object>().Select(x => PaymentRequest.FromString(x.ToString())).ToArray();
                }
            }
            if (model.SelectedStates != null)
            {
                Response.Cookies.Add(new HttpCookie("SelectedStates", JsonConvert.SerializeObject(model.SelectedStates)));
            }

            if (model.SelectedStates != null)
            {
                results = results.Where(x => model.SelectedStates.Contains(x.RequestState));
            }

            if (model.Project != null)
            {
                results = results.Where(x => x.Project.ProjectId == model.Project);
            }

            if (model.DateTo != null)
            {
                results = results.Where(x => x.WhenCreated <= model.DateTo);
            }
            if (model.DateFrom != null)
            {
                results = results.Where(x => x.WhenCreated >= model.DateFrom);
            }

            ViewBag.Projects    = results.Select(x => x.Project).Distinct().ToList();
            model.SearchResults = results.ToList().ToPagedList(pageIndex, RecordsPerPage);
            model.Comments.Clear();

            foreach (var it in model.SearchResults)
            {
                int cnt = db.RequestHistories.Where(x => x.Request.PaymentRequestId == it.PaymentRequestId).ToList().Count;
                if (cnt > 0)
                {
                    model.Comments.Add(it.PaymentRequestId, null);
                }
            }
            return(View(model));
        }