Exemple #1
0
        public JsonNetResult GetChildWorkgroupIds(int id)
        {
            var success = true;
            var message = "Updated";

            try
            {
                var workgroup = _repositoryFactory.WorkgroupRepository.Queryable.Single(a => a.Id == id);
                Check.Require(workgroup.Administrative);
                Check.Require(workgroup.IsActive);

                var sb  = new StringBuilder();
                var ids = _workgroupService.GetChildWorkgroups(id);
                if (ids != null && ids.Any())
                {
                    ids.Sort();
                    foreach (var childIds in ids)
                    {
                        sb.Append(" " + childIds);
                    }
                    message = sb.ToString();
                }
                else
                {
                    message = "None";
                }
            }
            catch (Exception ex)
            {
                success = false;
                message = ex.Message;
            }

            return(new JsonNetResult(new { success, message }));
        }
        public ActionResult TotalByWorkgroup(DateTime?startDate, DateTime?endDate, bool showAdmin)
        {
            const int defaultResultSize = 1000;
            var       viewModel         = TotalByWorkgroupViewModel.Create(startDate, endDate, showAdmin);

            if (startDate == null || endDate == null)
            {
                Message = "Select a start date and an end date then click apply to view report.";


                return(View(viewModel));
            }

            //Grab all workgroups for this user as well as related primary orgs
            var allWorkgroups             = _workgroupService.LoadAdminWorkgroups(true).ToList();
            var workgroupIds              = allWorkgroups.Select(x => x.Id).ToArray();
            var workgroupsWithPrimaryOrgs =
                _repositoryFactory.WorkgroupRepository.Queryable.Where(x => workgroupIds.Contains(x.Id))
                .Select(x => new { Workgroup = x, Primary = x.PrimaryOrganization }).ToList();

            //Get every order that matches these workgroups
            var matchingOrders = _searchService.GetOrdersByWorkgroups(allWorkgroups, startDate.Value, endDate.Value, defaultResultSize);

            if (matchingOrders.Count == defaultResultSize)
            {
                Message = "Max size of 1000 reached. Please adjust filters.";
            }
            var workgroupCounts = new List <OrderTotals>();

            foreach (var workgroup in allWorkgroups)
            {
                if (workgroup.IsActive && !workgroup.Administrative)
                {
                    var orderTotal = new OrderTotals();
                    orderTotal.WorkgroupName  = workgroup.Name;
                    orderTotal.WorkgroupId    = workgroup.Id;
                    orderTotal.Administrative = false;
                    orderTotal.PrimaryOrg     =
                        workgroupsWithPrimaryOrgs.Single(x => x.Workgroup.Id == workgroup.Id).Primary.Name;
                    orderTotal.InitiatedOrders = matchingOrders.AsQueryable().Count(a => a.WorkgroupId == workgroup.Id);
                    orderTotal.DeniedOrders    =
                        matchingOrders.AsQueryable()
                        .Count(a => a.WorkgroupId == workgroup.Id && a.StatusId == OrderStatusCode.Codes.Denied);
                    orderTotal.CanceledOrders =
                        matchingOrders.AsQueryable()
                        .Count(a => a.WorkgroupId == workgroup.Id && a.StatusId == OrderStatusCode.Codes.Cancelled);
                    orderTotal.CompletedOrders =
                        matchingOrders.AsQueryable()
                        .Count(
                            a =>
                            a.WorkgroupId == workgroup.Id &&
                            (a.StatusId == OrderStatusCode.Codes.Complete ||
                             a.StatusId == OrderStatusCode.Codes.CompleteNotUploadedKfs));
                    orderTotal.PendingOrders =
                        matchingOrders.AsQueryable()
                        .Count(
                            a =>
                            a.WorkgroupId == workgroup.Id &&
                            (a.StatusId == OrderStatusCode.Codes.Approver ||
                             a.StatusId == OrderStatusCode.Codes.AccountManager ||
                             a.StatusId == OrderStatusCode.Codes.Purchaser ||
                             a.StatusId == OrderStatusCode.Codes.Requester ||
                             a.StatusId == OrderStatusCode.Codes.ConditionalApprover));

                    workgroupCounts.Add(orderTotal);
                }
            }
            if (showAdmin)
            {
                foreach (var workgroup in allWorkgroups)
                {
                    if (workgroup.IsActive && workgroup.Administrative)
                    {
                        var orderTotal = new OrderTotals();
                        orderTotal.WorkgroupName  = workgroup.Name;
                        orderTotal.WorkgroupId    = workgroup.Id;
                        orderTotal.Administrative = true;
                        orderTotal.PrimaryOrg     = workgroup.PrimaryOrganization.Name;

                        orderTotal.InitiatedOrders = 0;
                        orderTotal.DeniedOrders    = 0;
                        orderTotal.CanceledOrders  = 0;
                        orderTotal.CompletedOrders = 0;
                        orderTotal.PendingOrders   = 0;
                        var childWorkgroupIds = _workgroupService.GetChildWorkgroups(workgroup.Id);

                        foreach (var childWorkgroupId in childWorkgroupIds)
                        //NOTE these child workgroup Id's don't include ones that "do not inherit permissions"
                        {
                            int id = childWorkgroupId;
                            var childOrderTotal = workgroupCounts.FirstOrDefault(a => a.WorkgroupId == id);
                            if (childOrderTotal != null)
                            {
                                orderTotal.InitiatedOrders += childOrderTotal.InitiatedOrders;
                                orderTotal.DeniedOrders    += childOrderTotal.DeniedOrders;
                                orderTotal.CanceledOrders  += childOrderTotal.CanceledOrders;
                                orderTotal.CompletedOrders += childOrderTotal.CompletedOrders;
                                orderTotal.PendingOrders   += childOrderTotal.PendingOrders;
                            }
                        }
                        workgroupCounts.Add(orderTotal);
                    }
                }
            }
            viewModel.WorkgroupCounts = workgroupCounts.OrderBy(a => a.WorkgroupName);


            var columnPreferences =
                _repositoryFactory.ColumnPreferencesRepository.GetNullableById(CurrentUser.Identity.Name) ??
                new ColumnPreferences(CurrentUser.Identity.Name);

            ViewBag.DataTablesPageSize = columnPreferences.DisplayRows;

            return(View(viewModel));
        }
        public void GenerateDisplayTable(ISearchService indexSearchService, IRepositoryFactory repositoryFactory,IWorkgroupService workgroupService, int workgroupId, DateTime month)
        {
            Check.Require(month.Day == 1);
            var endMonth = month.AddMonths(1);

            var workgroup = repositoryFactory.WorkgroupRepository.Queryable.Single(a => a.Id == workgroupId);
            var allChildWorkgroups = new List<int>();
            allChildWorkgroups.Add(workgroupId);
            if (workgroup.Administrative)
            {
                allChildWorkgroups = workgroupService.GetChildWorkgroups(workgroupId);
            }

            Columns = new List<ReportProcessingColumns>();
            var workgroups = repositoryFactory.WorkgroupRepository.Queryable.Where(a => allChildWorkgroups.Contains(a.Id)).ToList();
            var matchingOrders = indexSearchService.GetOrdersByWorkgroups(workgroups, month, endMonth);
            foreach (var matchingOrder in matchingOrders.Where(a => a.IsComplete && a.StatusId != OrderStatusCode.Codes.Denied && a.StatusId != OrderStatusCode.Codes.Cancelled))
            {

                var column = new ReportProcessingColumns();
                var order = repositoryFactory.OrderRepository.Queryable.Single(a => a.Id == matchingOrder.OrderId);

                column.Organization = order.Workgroup.PrimaryOrganization.Id;
                column.Workgroup = order.Workgroup.Name;
                column.OrderRequestNumber = order.RequestNumber;
                column.OrderId = order.Id;
                var orderHistory = order.OrderTrackings.LastOrDefault(a => a.StatusCode.Id == OrderStatusCode.Codes.AccountManager && a.Description.StartsWith("approved"));
                if (orderHistory != null)
                {
                    column.ArrivedAtPurchaser = orderHistory.DateCreated;
                }

                orderHistory = order.OrderTrackings.LastOrDefault(a => a.Description.StartsWith("rerouted to purchaser"));
                if (orderHistory != null)
                {
                    column.ReroutedToPurchaserDate = orderHistory.DateCreated;
                    column.ReRoutedToPurchaserBy = orderHistory.User.FullName;
                    column.ReroutedToPurchaserName = orderHistory.Description.Substring(22);
                    if (column.ArrivedAtPurchaser != null)
                    {
                        column.ReRoutTime = column.ReroutedToPurchaserDate.Value - column.ArrivedAtPurchaser.Value;
                    }
                }
                else if(OnlyShowReRouted)
                {
                    continue;
                }
                orderHistory = order.OrderTrackings.FirstOrDefault(a => a.StatusCode.IsComplete);
                if (orderHistory != null)
                {
                    column.CompletedByPurchaserDate = orderHistory.DateCreated;
                    column.CompletedByPurchaserName = orderHistory.User.FullName;
                    if (column.ArrivedAtPurchaser != null && column.ReroutedToPurchaserDate != null)
                    {
                        column.CompletionTime = column.CompletedByPurchaserDate.Value - column.ReroutedToPurchaserDate.Value;
                    }
                    else if (column.ArrivedAtPurchaser != null && column.ReroutedToPurchaserDate == null)
                    {
                        column.CompletionTime = column.CompletedByPurchaserDate.Value - column.ArrivedAtPurchaser.Value;
                    }
                }
                Columns.Add(column);
            }
        }
        public void GenerateDisplayTable(ISearchService indexSearchService, IRepositoryFactory repositoryFactory, IWorkgroupService workgroupService, int workgroupId, DateTime month)
        {
            const int defaultResultSize = 1000;

            Check.Require(month.Day == 1);
            var endMonth = month.AddMonths(1);

            var workgroup          = repositoryFactory.WorkgroupRepository.Queryable.Single(a => a.Id == workgroupId);
            var allChildWorkgroups = new List <int>();

            allChildWorkgroups.Add(workgroupId);
            if (workgroup.Administrative)
            {
                allChildWorkgroups = workgroupService.GetChildWorkgroups(workgroupId);
            }

            Columns = new List <ReportProcessingColumns>();
            var workgroups     = repositoryFactory.WorkgroupRepository.Queryable.Where(a => allChildWorkgroups.Contains(a.Id)).ToList();
            var matchingOrders = indexSearchService.GetOrdersByWorkgroups(workgroups, month, endMonth, defaultResultSize);

            SearchResultsCount = matchingOrders.Count;

            foreach (var matchingOrder in matchingOrders.Where(a => a.IsComplete && a.StatusId != OrderStatusCode.Codes.Denied && a.StatusId != OrderStatusCode.Codes.Cancelled))
            {
                var column = new ReportProcessingColumns();
                var order  = repositoryFactory.OrderRepository.Queryable.Single(a => a.Id == matchingOrder.OrderId);

                column.Organization       = order.Workgroup.PrimaryOrganization.Id;
                column.Workgroup          = order.Workgroup.Name;
                column.OrderRequestNumber = order.RequestNumber;
                column.OrderId            = order.Id;
                var orderHistory = order.OrderTrackings.LastOrDefault(a => a.StatusCode.Id == OrderStatusCode.Codes.AccountManager && a.Description.StartsWith("approved"));
                if (orderHistory != null)
                {
                    column.ArrivedAtPurchaser = orderHistory.DateCreated;
                }

                orderHistory = order.OrderTrackings.LastOrDefault(a => a.Description.StartsWith("rerouted to purchaser"));
                if (orderHistory != null)
                {
                    column.ReroutedToPurchaserDate = orderHistory.DateCreated;
                    column.ReRoutedToPurchaserBy   = orderHistory.User.FullName;
                    column.ReroutedToPurchaserName = orderHistory.Description.Substring(22);
                    if (column.ArrivedAtPurchaser != null)
                    {
                        column.ReRoutTime = column.ReroutedToPurchaserDate.Value - column.ArrivedAtPurchaser.Value;
                    }
                }
                else if (OnlyShowReRouted)
                {
                    continue;
                }
                orderHistory = order.OrderTrackings.FirstOrDefault(a => a.StatusCode.IsComplete);
                if (orderHistory != null)
                {
                    column.CompletedByPurchaserDate = orderHistory.DateCreated;
                    column.CompletedByPurchaserName = orderHistory.User.FullName;
                    if (column.ArrivedAtPurchaser != null && column.ReroutedToPurchaserDate != null)
                    {
                        column.CompletionTime = column.CompletedByPurchaserDate.Value - column.ReroutedToPurchaserDate.Value;
                    }
                    else if (column.ArrivedAtPurchaser != null && column.ReroutedToPurchaserDate == null)
                    {
                        column.CompletionTime = column.CompletedByPurchaserDate.Value - column.ArrivedAtPurchaser.Value;
                    }
                }
                Columns.Add(column);
            }
        }