<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.SessionState;
using Purchasing.Core;
using System.Web.Mvc;
using Purchasing.Core.Domain;
using Purchasing.Core.Queries;
using Purchasing.Web.Models;
using Purchasing.Web.Services;
using UCDArch.Core.PersistanceSupport;
using UCDArch.Web.ActionResults;

namespace Purchasing.Web.Controllers
{
    /// <summary>
    /// Disabling session state #1 because we don't need it, #2 so that we can call partial views and ajax calls in parallel (aka no lock on session)
    /// </summary>
    [SessionState(SessionStateBehavior.Disabled)]
    public class HistoryController : ApplicationController
    {
        private readonly IRepositoryFactory _repositoryFactory;
        private readonly IQueryRepositoryFactory _queryRepositoryFactory;
        private readonly IOrderService _orderService;

        public HistoryController(IRepositoryFactory repositoryFactory, IQueryRepositoryFactory queryRepositoryFactory, IOrderService OrderService)
        {
            _repositoryFactory = repositoryFactory;
            _queryRepositoryFactory = queryRepositoryFactory;
            _orderService = OrderService;
        }

        /// <summary>
        /// List of Orders
        /// </summary>
        /// <param name="selectedOrderStatus">Includes 3 that are not in our database</param>
        /// <param name="startDate">For when Order was created</param>
        /// <param name="endDate">For when Order was created</param>
        /// <param name="startLastActionDate">For when Order was last acted on</param>
        /// <param name="endLastActionDate">For when Order was last acted on</param>
        /// <param name="showPending">Show orders pending your action</param>
        /// <param name="showCreated">Only show orders you created</param>
        /// <returns></returns>
        public ActionResult Index(string selectedOrderStatus,
            DateTime? startDate,
            DateTime? endDate,
            DateTime? startLastActionDate,
            DateTime? endLastActionDate,
            bool showPending = false,
            bool showCreated = false)
        {
            //TODO: Review even/odd display of table once Trish has look at it. (This page is a single, and the background color is the same as the even background color.
            var saveSelectedOrderStatus = selectedOrderStatus;
            if (selectedOrderStatus == "All")
            {
                selectedOrderStatus = null;
            }

            var isComplete = (selectedOrderStatus == OrderStatusCode.Codes.Complete);

            if (selectedOrderStatus == "Received" || selectedOrderStatus == "UnReceived")
            {
                selectedOrderStatus = OrderStatusCode.Codes.Complete;
                isComplete = true;
            }

            var ordersIndexed = _orderService.GetIndexedListofOrders(isComplete, showPending, selectedOrderStatus, startDate, endDate, showCreated, startLastActionDate, endLastActionDate);
            ViewBag.IndexLastModified = ordersIndexed.LastModified;

            var orders = ordersIndexed.Results.AsQueryable();

            if (saveSelectedOrderStatus == "Received")
            {
                orders = orders.Where(a => a.Received == "Yes");
            }
            else if (saveSelectedOrderStatus == "UnReceived")
            {
                orders = orders.Where(a => a.Received == "No");
            }

            var model = new FilteredOrderListModelDto
            {
                SelectedOrderStatus = selectedOrderStatus,
                StartDate = startDate,
                EndDate = endDate,
                StartLastActionDate = startLastActionDate,
                EndLastActionDate = endLastActionDate,
                ShowPending = showPending,
                ShowCreated = showCreated,
                ColumnPreferences =
                    _repositoryFactory.ColumnPreferencesRepository.GetNullableById(
                        CurrentUser.Identity.Name) ??
                    new ColumnPreferences(CurrentUser.Identity.Name)
            };
            ViewBag.DataTablesPageSize = model.ColumnPreferences.DisplayRows;

            PopulateModel(orders.OrderByDescending(a => a.LastActionDate).ToList(), model);

            return View("Index", model);
        }

        /// <summary>
        /// Page to view Administrative Workgroup Orders
        /// </summary>
        /// <returns></returns>
        /// <summary>
        /// Page to view Administrative Workgroup Orders
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminOrders(string selectedOrderStatus,
            DateTime? startDate,
            DateTime? endDate,
            DateTime? startLastActionDate,
            DateTime? endLastActionDate,
            bool showPending = false)
        {
            //TODO: Review even/odd display of table once Trish has look at it. (This page is a single, and the background color is the same as the even background color.
            var saveSelectedOrderStatus = selectedOrderStatus;
            if (selectedOrderStatus == "All")
            {
                selectedOrderStatus = null;
            }
            var isComplete = selectedOrderStatus == OrderStatusCode.Codes.Complete;

            if (selectedOrderStatus == "Received" || selectedOrderStatus == "UnReceived")
            {
                selectedOrderStatus = OrderStatusCode.Codes.Complete;
                isComplete = true;
            }

            var ordersIndexed = _orderService.GetAdministrativeIndexedListofOrders(isComplete, showPending, selectedOrderStatus, startDate, endDate, startLastActionDate, endLastActionDate);
            ViewBag.IndexLastModified = ordersIndexed.LastModified;
            
            var orders = ordersIndexed.Results.AsQueryable();

            if (saveSelectedOrderStatus == "Received")
            {
                orders = orders.Where(a => a.Received == "Yes");
            }
            else if (saveSelectedOrderStatus == "UnReceived")
            {
                orders = orders.Where(a => a.Received == "No");
            }

            var model = new FilteredOrderListModelDto
            {
                SelectedOrderStatus = selectedOrderStatus,
                StartDate = startDate,
                EndDate = endDate,
                StartLastActionDate = startLastActionDate,
                EndLastActionDate = endLastActionDate,
                ShowPending = showPending,
                ColumnPreferences =
                    _repositoryFactory.ColumnPreferencesRepository.GetNullableById(
                        CurrentUser.Identity.Name) ??
                    new ColumnPreferences(CurrentUser.Identity.Name)
            };
            ViewBag.DataTablesPageSize = model.ColumnPreferences.DisplayRows;
            PopulateModel(orders.OrderByDescending(a => a.LastActionDate).ToList(), model);

            return View("AdminOrders", model);
        }

        #region PartialViews

        public ActionResult RecentActivity()
        {
            var lastOrderEvent = _queryRepositoryFactory.OrderTrackingHistoryRepository.Queryable.Where(
                d => d.AccessUserId == CurrentUser.Identity.Name).OrderByDescending(e => e.DateCreated).FirstOrDefault();

            return PartialView(lastOrderEvent);
        }

        public ActionResult RecentComments()
        {
            var recentComments = _queryRepositoryFactory.CommentHistoryRepository
                .Queryable.Where(a => a.AccessUserId == CurrentUser.Identity.Name)
                .OrderByDescending(o => o.DateCreated)
                .Take(5).ToList();

            return PartialView(recentComments);
        }


        #endregion PartialViews

        #region AJAX Calls
        public JsonNetResult RecentlyCompleted()
        {

            var deniedThisMonth =
                _orderService.GetListofOrders(false, false, OrderStatusCode.Codes.Denied, null, null, true,
                                              new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), null).Count();
            var completedThisMonth =
                _orderService.GetListofOrders(true, false, OrderStatusCode.Codes.Complete, null, null, true,
                                  new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), null).Count();

            return new JsonNetResult(new {deniedThisMonth, completedThisMonth});
        }
         

        #endregion AJAX Calls

        #region Private Methods

        private void PopulateModel(List<OrderHistory> orders, FilteredOrderListModelDto model)
        {
            model.OrderHistory = orders;

            if (model.RequresOrderTracking() || model.RequiresApprovals())
            {
                var orderIds = model.OrderHistory.Select(a => a.OrderId).ToList();
                if (model.RequresOrderTracking())
                {
                    model.OrderTracking =
                        (from o in
                             _repositoryFactory.OrderTrackingRepository.Queryable.Fetch(x => x.User).Fetch(
                                 x => x.StatusCode)
                         where orderIds.Contains(o.Order.Id)
                         select o).ToList();
                }

                if (model.RequiresApprovals())
                {
                    model.Approvals =
                        (from a in
                             _repositoryFactory.ApprovalRepository.Queryable.Fetch(x => x.User).Fetch(
                                 x => x.SecondaryUser)
                         where orderIds.Contains(a.Order.Id)
                         select a).ToList();
                }
            }

            model.PopulateStatusCodes(_repositoryFactory.OrderStatusCodeRepository);
        } 

        #endregion Private Methods
    }
}<commit_msg>use the history index to count completed orders<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.SessionState;
using Purchasing.Core;
using System.Web.Mvc;
using Purchasing.Core.Domain;
using Purchasing.Core.Queries;
using Purchasing.Web.Models;
using Purchasing.Web.Services;
using UCDArch.Core.PersistanceSupport;
using UCDArch.Web.ActionResults;

namespace Purchasing.Web.Controllers
{
    /// <summary>
    /// Disabling session state #1 because we don't need it, #2 so that we can call partial views and ajax calls in parallel (aka no lock on session)
    /// </summary>
    [SessionState(SessionStateBehavior.Disabled)]
    public class HistoryController : ApplicationController
    {
        private readonly IRepositoryFactory _repositoryFactory;
        private readonly IQueryRepositoryFactory _queryRepositoryFactory;
        private readonly IOrderService _orderService;

        public HistoryController(IRepositoryFactory repositoryFactory, IQueryRepositoryFactory queryRepositoryFactory, IOrderService OrderService)
        {
            _repositoryFactory = repositoryFactory;
            _queryRepositoryFactory = queryRepositoryFactory;
            _orderService = OrderService;
        }

        /// <summary>
        /// List of Orders
        /// </summary>
        /// <param name="selectedOrderStatus">Includes 3 that are not in our database</param>
        /// <param name="startDate">For when Order was created</param>
        /// <param name="endDate">For when Order was created</param>
        /// <param name="startLastActionDate">For when Order was last acted on</param>
        /// <param name="endLastActionDate">For when Order was last acted on</param>
        /// <param name="showPending">Show orders pending your action</param>
        /// <param name="showCreated">Only show orders you created</param>
        /// <returns></returns>
        public ActionResult Index(string selectedOrderStatus,
            DateTime? startDate,
            DateTime? endDate,
            DateTime? startLastActionDate,
            DateTime? endLastActionDate,
            bool showPending = false,
            bool showCreated = false)
        {
            //TODO: Review even/odd display of table once Trish has look at it. (This page is a single, and the background color is the same as the even background color.
            var saveSelectedOrderStatus = selectedOrderStatus;
            if (selectedOrderStatus == "All")
            {
                selectedOrderStatus = null;
            }

            var isComplete = (selectedOrderStatus == OrderStatusCode.Codes.Complete);

            if (selectedOrderStatus == "Received" || selectedOrderStatus == "UnReceived")
            {
                selectedOrderStatus = OrderStatusCode.Codes.Complete;
                isComplete = true;
            }

            var ordersIndexed = _orderService.GetIndexedListofOrders(isComplete, showPending, selectedOrderStatus, startDate, endDate, showCreated, startLastActionDate, endLastActionDate);
            ViewBag.IndexLastModified = ordersIndexed.LastModified;

            var orders = ordersIndexed.Results.AsQueryable();

            if (saveSelectedOrderStatus == "Received")
            {
                orders = orders.Where(a => a.Received == "Yes");
            }
            else if (saveSelectedOrderStatus == "UnReceived")
            {
                orders = orders.Where(a => a.Received == "No");
            }

            var model = new FilteredOrderListModelDto
            {
                SelectedOrderStatus = selectedOrderStatus,
                StartDate = startDate,
                EndDate = endDate,
                StartLastActionDate = startLastActionDate,
                EndLastActionDate = endLastActionDate,
                ShowPending = showPending,
                ShowCreated = showCreated,
                ColumnPreferences =
                    _repositoryFactory.ColumnPreferencesRepository.GetNullableById(
                        CurrentUser.Identity.Name) ??
                    new ColumnPreferences(CurrentUser.Identity.Name)
            };
            ViewBag.DataTablesPageSize = model.ColumnPreferences.DisplayRows;

            PopulateModel(orders.OrderByDescending(a => a.LastActionDate).ToList(), model);

            return View("Index", model);
        }

        /// <summary>
        /// Page to view Administrative Workgroup Orders
        /// </summary>
        /// <returns></returns>
        /// <summary>
        /// Page to view Administrative Workgroup Orders
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminOrders(string selectedOrderStatus,
            DateTime? startDate,
            DateTime? endDate,
            DateTime? startLastActionDate,
            DateTime? endLastActionDate,
            bool showPending = false)
        {
            //TODO: Review even/odd display of table once Trish has look at it. (This page is a single, and the background color is the same as the even background color.
            var saveSelectedOrderStatus = selectedOrderStatus;
            if (selectedOrderStatus == "All")
            {
                selectedOrderStatus = null;
            }
            var isComplete = selectedOrderStatus == OrderStatusCode.Codes.Complete;

            if (selectedOrderStatus == "Received" || selectedOrderStatus == "UnReceived")
            {
                selectedOrderStatus = OrderStatusCode.Codes.Complete;
                isComplete = true;
            }

            var ordersIndexed = _orderService.GetAdministrativeIndexedListofOrders(isComplete, showPending, selectedOrderStatus, startDate, endDate, startLastActionDate, endLastActionDate);
            ViewBag.IndexLastModified = ordersIndexed.LastModified;
            
            var orders = ordersIndexed.Results.AsQueryable();

            if (saveSelectedOrderStatus == "Received")
            {
                orders = orders.Where(a => a.Received == "Yes");
            }
            else if (saveSelectedOrderStatus == "UnReceived")
            {
                orders = orders.Where(a => a.Received == "No");
            }

            var model = new FilteredOrderListModelDto
            {
                SelectedOrderStatus = selectedOrderStatus,
                StartDate = startDate,
                EndDate = endDate,
                StartLastActionDate = startLastActionDate,
                EndLastActionDate = endLastActionDate,
                ShowPending = showPending,
                ColumnPreferences =
                    _repositoryFactory.ColumnPreferencesRepository.GetNullableById(
                        CurrentUser.Identity.Name) ??
                    new ColumnPreferences(CurrentUser.Identity.Name)
            };
            ViewBag.DataTablesPageSize = model.ColumnPreferences.DisplayRows;
            PopulateModel(orders.OrderByDescending(a => a.LastActionDate).ToList(), model);

            return View("AdminOrders", model);
        }

        #region PartialViews

        public ActionResult RecentActivity()
        {
            var lastOrderEvent = _queryRepositoryFactory.OrderTrackingHistoryRepository.Queryable.Where(
                d => d.AccessUserId == CurrentUser.Identity.Name).OrderByDescending(e => e.DateCreated).FirstOrDefault();

            return PartialView(lastOrderEvent);
        }

        public ActionResult RecentComments()
        {
            var recentComments = _queryRepositoryFactory.CommentHistoryRepository
                .Queryable.Where(a => a.AccessUserId == CurrentUser.Identity.Name)
                .OrderByDescending(o => o.DateCreated)
                .Take(5).ToList();

            return PartialView(recentComments);
        }


        #endregion PartialViews

        #region AJAX Calls
        public JsonNetResult RecentlyCompleted()
        {
            var deniedThisMonth =
                _orderService.GetIndexedListofOrders(false, false, OrderStatusCode.Codes.Denied, null, null, true,
                                              new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), null).Results.Count();
            var completedThisMonth =
                _orderService.GetIndexedListofOrders(true, false, OrderStatusCode.Codes.Complete, null, null, true,
                                  new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), null).Results.Count();

            return new JsonNetResult(new {deniedThisMonth, completedThisMonth});
        }
         

        #endregion AJAX Calls

        #region Private Methods

        private void PopulateModel(List<OrderHistory> orders, FilteredOrderListModelDto model)
        {
            model.OrderHistory = orders;

            if (model.RequresOrderTracking() || model.RequiresApprovals())
            {
                var orderIds = model.OrderHistory.Select(a => a.OrderId).ToList();
                if (model.RequresOrderTracking())
                {
                    model.OrderTracking =
                        (from o in
                             _repositoryFactory.OrderTrackingRepository.Queryable.Fetch(x => x.User).Fetch(
                                 x => x.StatusCode)
                         where orderIds.Contains(o.Order.Id)
                         select o).ToList();
                }

                if (model.RequiresApprovals())
                {
                    model.Approvals =
                        (from a in
                             _repositoryFactory.ApprovalRepository.Queryable.Fetch(x => x.User).Fetch(
                                 x => x.SecondaryUser)
                         where orderIds.Contains(a.Order.Id)
                         select a).ToList();
                }
            }

            model.PopulateStatusCodes(_repositoryFactory.OrderStatusCodeRepository);
        } 

        #endregion Private Methods
    }
}
{'fraction_non_alphanumeric': 0.08243890148652054, 'fraction_numerical': 0.0006046863189720333, 'mean_word_length': 1.9044343626518367, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2550953', 'n_tokens_mistral': 4695, 'n_tokens_neox': 4542, 'n_words': 1067}