Esempio n. 1
0
        public ActionResult SearchForm(OrdersSearchValuesModel model, bool isExpanding, bool isCollapsed, int? userId = null, int? statusId = null, int? supplierId = null, bool hideUserField = false, bool hideStatusField = false, bool hideSupplierField = false)
        {
            if (model == null) model = new OrdersSearchValuesModel();

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId))
            using (SuppliersRepository suppliersRep = new SuppliersRepository(CurrentUser.CompanyId))
            using (OrderStatusesRepository statusesRep = new OrderStatusesRepository())
            using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
            {
                List<SelectListItemDB> usersAsSelectItems = new List<SelectListItemDB>() { new SelectListItemDB() { Id = -1, Name = Loc.Dic.AllUsersOption } };
                usersAsSelectItems.AddRange(usersRep.GetList().Select(x => new SelectListItemDB() { Id = x.Id, Name = x.FirstName + " " + x.LastName }));
                model.UsersList = new SelectList(usersAsSelectItems, "Id", "Name");

                List<SelectListItemDB> budgetsAsSelectItems = new List<SelectListItemDB>() { new SelectListItemDB() { Id = -1, Name = Loc.Dic.AllBudgetsOption } };
                budgetsAsSelectItems.AddRange(budgetsRep.GetList().AsEnumerable().Select(x => new SelectListItemDB() { Id = x.Id, Name = "(" + x.Year + ") " + x.Name }));
                model.BudgetsList = new SelectList(budgetsAsSelectItems, "Id", "Name");

                List<Supplier> suppliersSelectList = new List<Supplier>() { new Supplier() { Id = -1, Name = Loc.Dic.AllSuppliersOption } };
                suppliersSelectList.AddRange(suppliersRep.GetList().OrderByDescending(x => x.Name).ToList());
                model.SuppliersList = new SelectList(suppliersSelectList, "Id", "Name");

                List<Orders_Statuses> statusesSelectList = new List<Orders_Statuses>() { new Orders_Statuses() { Id = -1, Name = Loc.Dic.AllStatusesOption } };
                statusesSelectList.AddRange(statusesRep.GetList().ToList());
                model.StatusesList = new SelectList(statusesSelectList, "Id", "Name");

                List<SelectListStringItem> allocationsSelectList = new List<SelectListStringItem>() { new SelectListStringItem() { Id = "-1", Name = Loc.Dic.AllAllocationsOption } };
                allocationsSelectList.AddRange(allocationsRep.GetList().GroupBy(x => x.ExternalId).AsEnumerable().Select(x => new SelectListStringItem() { Id = x.First().ExternalId, Name = x.First().DisplayName }).ToList());
                model.AllocationsList = new SelectList(allocationsSelectList, "Id", "Name");
            }

            ViewBag.IsExpanding = isExpanding;
            ViewBag.IsCollapsed = isCollapsed;
            ViewBag.UserId = userId;
            ViewBag.StatusId = statusId;
            ViewBag.SupplierId = supplierId;
            ViewBag.HideUserField = hideUserField;
            ViewBag.HideStatusField = hideStatusField;
            ViewBag.HideSupplierField = hideSupplierField;
            return PartialView(model);
        }
Esempio n. 2
0
        public ActionResult Search(OrdersSearchValuesModel model)
        {
            if (!Authorized(RoleType.OrdersWriter) && !Authorized(RoleType.OrdersViewer)) return Error(Loc.Dic.error_no_permission);

            List<Order> matchingOrders;
            List<Order> TextMatchOrders = new List<Order>();

            ViewBag.UserId = model.UserId;
            ViewBag.StatusId = model.StatusId;
            ViewBag.SupplierId = model.SupplierId;
            ViewBag.HideUserField = model.HideUserField;
            ViewBag.HideStatusField = model.HideStatusField;
            ViewBag.HideSupplierField = model.HideSupplierField;

            using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            using (SuppliersRepository suppliersRep = new SuppliersRepository(CurrentUser.CompanyId))
            {
                IQueryable<Order> ordersQuery;

                ordersQuery = ordersRep.GetList("Company", "Orders_Statuses", "Supplier", "User").Where(x => x.CompanyId == CurrentUser.CompanyId);

                if (Authorized(RoleType.OrdersViewer))
                {
                    if (model.UserId.HasValue && model.UserId.Value != -1)
                        ordersQuery = ordersQuery.Where(x => x.UserId == model.UserId.Value);
                }
                else
                {
                    ordersQuery = ordersQuery.Where(x => x.UserId == CurrentUser.UserId);
                    ViewBag.UserId = CurrentUser.UserId;
                }

                if (model.BudgetId.HasValue && model.BudgetId.Value != -1)
                    ordersQuery = ordersQuery.Where(x => x.BudgetId == model.BudgetId.Value);

                if (model.OrderNumber.HasValue && model.OrderNumber.Value != -1)
                    ordersQuery = ordersQuery.Where(x => x.OrderNumber == model.OrderNumber.Value);

                if (model.SupplierId.HasValue && model.SupplierId.Value != -1)
                    ordersQuery = ordersQuery.Where(x => x.SupplierId == model.SupplierId.Value);

                if (model.StatusId.HasValue && model.StatusId.Value != -1)
                    ordersQuery = ordersQuery.Where(x => x.StatusId == model.StatusId.Value);

                if (model.AllocationId != null && model.AllocationId != "-1")
                    ordersQuery = ordersQuery.Where(x => x.Orders_OrderToAllocation.Any(oa => oa.Budgets_Allocations.ExternalId == model.AllocationId));

                if (model.PriceMin.HasValue && model.PriceMax.HasValue && model.PriceMax.Value < model.PriceMin.Value)
                    model.PriceMax = null;

                if (model.PriceMin.HasValue)
                    ordersQuery = ordersQuery.Where(x => x.Price >= model.PriceMin.Value);

                if (model.PriceMax.HasValue)
                    ordersQuery = ordersQuery.Where(x => x.Price <= model.PriceMax.Value);

                if (model.CreationMin.HasValue && model.CreationMax.HasValue && model.CreationMax.Value < model.CreationMin.Value)
                    model.CreationMax = null;

                if (model.CreationMin.HasValue)
                    ordersQuery = ordersQuery.Where(x => x.CreationDate >= model.CreationMin.Value);

                if (model.CreationMax.HasValue)
                    ordersQuery = ordersQuery.Where(x => x.CreationDate <= model.CreationMax.Value);

                matchingOrders = ordersQuery.ToList();
            }

            if (!String.IsNullOrEmpty(model.NoteText))
            {
                List<string> searchWords = model.NoteText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(str => str.Trim()).ToList();
                foreach (var order in matchingOrders)
                {
                    foreach (var word in searchWords)
                    {
                        if (!String.IsNullOrEmpty(order.NotesForSupplier) && order.NotesForSupplier.Contains(word))
                        {
                            TextMatchOrders.Add(order);
                            break;
                        }
                    }
                }

                model.Matches = TextMatchOrders;
            }
            else
            {
                model.Matches = matchingOrders;
            }

            return View(model);
        }