Ejemplo n.º 1
0
        public ActionResult Finances_getItems()
        {
            var parameters = AjaxModel.GetParameters(HttpContext);
            var mng        = new FinanceManager();
            var items      = mng.GetFinFinances().AsQueryable();

            if (parameters.filter != null && parameters.filter.Count > 0)
            {
                var fromID = parameters.filter.ContainsKey("fromName") ? RDL.Convert.StrToInt(parameters.filter["fromName"].ToString(), 0) : 0;
                items = items.Where(x =>
                                    (fromID == 0 || x.fromID == fromID)
                                    );

                var toID = parameters.filter.ContainsKey("toName") ? RDL.Convert.StrToInt(parameters.filter["toName"].ToString(), 0) : 0;
                items = items.Where(x =>
                                    (toID == 0 || x.toID == toID)
                                    );

                var projectID = parameters.filter.ContainsKey("projectName") ? RDL.Convert.StrToInt(parameters.filter["projectName"].ToString(), 0) : 0;
                items = items.Where(x =>
                                    (projectID == 0 || x.projectID == projectID)
                                    );

                var typeID = parameters.filter.ContainsKey("typeName") ? RDL.Convert.StrToInt(parameters.filter["typeName"].ToString(), 0) : 0;
                items = items.Where(x =>
                                    (typeID == 0 || x.typeID == typeID)
                                    );

                List <int?> statusIDs = new List <int?>();
                if (parameters.filter.ContainsKey("statusName"))
                {
                    statusIDs = (parameters.filter["statusName"] as ArrayList).ToArray().Select(x => (int?)RDL.Convert.StrToInt(x.ToString(), 0)).ToList();
                }
                items = items.Where(x =>
                                    (statusIDs.Count == 0 || statusIDs.Contains(x.statusID))
                                    );

                DateTime createdMin = DateTime.MinValue, createdMax = DateTime.MaxValue;
                if (parameters.filter.ContainsKey("created") && parameters.filter["created"] != null)
                {
                    var dates = parameters.filter["created"].ToString().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (dates.Length > 0)
                    {
                        createdMin = RDL.Convert.StrToDateTime(dates[0].Trim(), DateTime.MinValue);
                    }
                    if (dates.Length > 1)
                    {
                        createdMax = RDL.Convert.StrToDateTime(dates[1].Trim(), DateTime.MaxValue);
                    }
                }
                items = items.Where(x =>
                                    (createdMin <= x.created && x.created <= createdMax)
                                    );
            }

            var sorts      = parameters.sort.Split(",".ToArray(), StringSplitOptions.RemoveEmptyEntries);
            var directions = parameters.direction.Split(",".ToArray(), StringSplitOptions.RemoveEmptyEntries);
            var sort1      = sorts.Length > 0 ? sorts[0] : "";
            var direction1 = directions.Length > 0 ? directions[0] : "";

            var sort2      = sorts.Length > 1 ? sorts[1] : "";
            var direction2 = directions.Length > 1 ? directions[1] : "";

            IOrderedQueryable <fin_finances> orderedItems = items.OrderByDescending(x => x.created);

            switch (sort1)
            {
            case "fromName":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.fin_contragents.name);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.fin_contragents.name);
                }
                break;

            case "toName":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.fin_contragents1.name);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.fin_contragents1.name);
                }
                break;

            case "sum":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.sum);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.sum);
                }
                break;

            case "desc":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.desc);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.desc);
                }
                break;

            case "projectName":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.tt_projects.name);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.tt_projects.name);
                }
                break;

            case "typeName":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.fin_types.name);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.fin_types.name);
                }
                break;

            case "statusName":
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.fin_statuses.name);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.fin_statuses.name);
                }
                break;

            default:
                if (direction1 == "up")
                {
                    orderedItems = items.OrderBy(x => x.created);
                }
                else
                {
                    orderedItems = items.OrderByDescending(x => x.created);
                }
                break;
            }

            if (sort2 != "")
            {
                switch (sort2)
                {
                case "fromName":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.fin_contragents.name);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.fin_contragents.name);
                    }
                    break;

                case "toName":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.fin_contragents1.name);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.fin_contragents1.name);
                    }
                    break;

                case "sum":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.sum);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.sum);
                    }
                    break;

                case "desc":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.desc);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.desc);
                    }
                    break;

                case "projectName":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.tt_projects.name);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.tt_projects.name);
                    }
                    break;

                case "typeName":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.fin_types.name);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.fin_types.name);
                    }
                    break;

                case "statusName":
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.fin_statuses.name);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.fin_statuses.name);
                    }
                    break;

                default:
                    if (direction1 == "up")
                    {
                        orderedItems = orderedItems.ThenBy(x => x.created);
                    }
                    else
                    {
                        orderedItems = orderedItems.ThenByDescending(x => x.created);
                    }
                    break;
                }
            }

            var total = orderedItems.Count();
            var res2  = orderedItems.Skip(parameters.pageSize * (parameters.page - 1)).Take(parameters.pageSize).ToList();


            return(Json(new
            {
                items = res2.Select(x => new
                {
                    x.id,
                    created = x.created.GetValueOrDefault().ToString("dd.MM.yyyy"),
                    x.fromID,
                    fromName = x.fin_contragents != null ? x.fin_contragents.name :"",
                    x.toID,
                    toName = x.fin_contragents1 != null ? x.fin_contragents1.name : "",
                    sum = x.sum ?? 0,
                    desc = x.desc ?? "",
                    x.typeID,
                    typeName = x.fin_types != null ? x.fin_types.name : "",
                    x.projectID,
                    projectName = x.tt_projects != null ? x.tt_projects.name : "",
                    x.statusID,
                    statusName = x.fin_statuses != null ? x.fin_statuses.name : ""
                }),
                total = total
            }, JsonRequestBehavior.AllowGet));
        }