/// <summary>
 /// Sets the project list in presenter.
 /// </summary>
 /// <param name="presenter">The presenter.</param>
 private void SetProjectListInPresenter(TeamAllocationPresenter presenter)
 {
     var projects = this.lookupService.RetrieveProjects(null, null, false, false, SessionData.Instance.UserInfo.Developer.DeveloperID);
     var masterProjectList = projects.CopyObject<List<ProjectFillItem>>();
     var projectList = projects.CopyObject<List<ProjectFillItem>>();
     presenter.AssignMasterProjects(masterProjectList);
     presenter.AssignProjects(projectList);
     presenter.Projects.ForEach(
         p =>
         {
             p.ProjectName = p.ProjectFullName;
         });
 }
        /// <summary>
        /// Assigns the team engagement list.
        /// </summary>
        /// <param name="presenter">The presenter.</param>
        private void AssignTeamEngagementList(TeamAllocationPresenter presenter)
        {
            var loanResourceList = this.loanResourceService.RetrieveList(presenter.DeveloperId, presenter.SelectedDate.RetrieveCurrentMonthStartDate(), presenter.SelectedDate.RetrieveCurrentMonthEndDate());
            loanResourceList = loanResourceList.Where(l => l.LoanResourceID != null && l.AvailableLoanHours > 0).Select(AssignManagerShortName).ToList();
            foreach (var loanResource in loanResourceList)
            {
                var teamAllocationItem = new TeamEngagementDateListItem
                {
                    Date = loanResource.Date,
                    IsHoliday = loanResource.IsHoliday,
                    ProjectName = loanResource.ManagerShortName,
                    ProjectID = loanResource.LoanResourceID,
                    ProjectAllocatedHours = loanResource.AvailableLoanHours,
                    TotalAvailableHours = loanResource.AvailableLoanHours,
                    IsLoanResource = true,
                    LoanResourceTooltip = loanResource.ManagerName
                };
                presenter.TeamAllocationList.Add(teamAllocationItem);
            }

            var teamAllocationList = presenter.TeamAllocationList.GroupBy(p => new { date = p.Date.Value, isHoliday = p.IsHoliday }).Select(grp => new TeamEngagementDateListItem { Date = grp.Key.date, TeamEngagementItems = grp.OrderBy(g => g.Priority).ToList(), IsHoliday = grp.Key.isHoliday }).ToList();
            teamAllocationList.ForEach(t => { t.TotalAllocatedHours = t.TeamEngagementItems.Where(i => i.IsLoanResource == false).Select(s => s.TotalAllocatedHours).FirstOrDefault(); t.TotalAvailableHours = t.TeamEngagementItems.Where(i => i.IsLoanResource == false).Select(s => s.TotalAvailableHours).FirstOrDefault(); });
            presenter.AssignTeamAllocationList(teamAllocationList);
        }
        /// <summary>
        /// Assigns the values.
        /// </summary>
        /// <param name="presenter">The presenter.</param>
        private void AssignTeamAllocationValues(TeamAllocationPresenter presenter)
        {
            IList<int> projectids = presenter.SelectedProjects.Count > 0 ? presenter.SelectedProjects.Select(s => s.ProjectID).ToList() : null;
            presenter.DeveloperInfo = this.developerService.RetrieveWithManagerInfo(new List<int>() { presenter.DeveloperId }, SessionData.Instance.UserInfo.Developer.DeveloperID);
            if (presenter.DeveloperInfo != null)
            {
                presenter.DeveloperInfo.DeveloperImage = presenter.DeveloperInfo.LoginID.RetrieveDeveloperImageName();
            }

            presenter.PreviousMonthSummary = this.teamEngagementService.RetrieveCount(new List<int>() { presenter.DeveloperId }, presenter.SelectedDate.RetrievePreviousMonthStartDate(), presenter.SelectedDate.RetrievePreviousMonthEndDate(), projectids, false, false, SessionData.Instance.UserInfo.Developer.DeveloperID);
            presenter.NextMonthSummary = this.teamEngagementService.RetrieveCount(new List<int>() { presenter.DeveloperId }, presenter.SelectedDate.RetrieveNextMonthStartDate(), presenter.SelectedDate.RetrieveNextMonthEndDate(), projectids, false, false, SessionData.Instance.UserInfo.Developer.DeveloperID);
            presenter.CurrentMonthSummary = this.teamEngagementService.RetrieveCount(new List<int>() { presenter.DeveloperId }, presenter.SelectedDate.RetrieveCurrentMonthStartDate(), presenter.SelectedDate.RetrieveCurrentMonthEndDate(), projectids, false, false, SessionData.Instance.UserInfo.Developer.DeveloperID);
            presenter.AssignTeamAllocationList(this.teamEngagementService.RetrieveList(presenter.DeveloperId, presenter.SelectedDate.RetrieveCurrentMonthStartDate(), presenter.SelectedDate.RetrieveCurrentMonthEndDate(), projectids, SessionData.Instance.UserInfo.Developer.DeveloperID));
            this.AssignTeamEngagementList(presenter);
        }
        private static void ValidateTeamEngagementDetails(TeamAllocationPresenter presenter)
        {
            var pastAllocationAllowedDays = ApplicationSettings.Instance.PastAllocationAllowedDays;
            var allowedDays = DateTimeHelper.RetrieveCurrentDate().AddDays(-pastAllocationAllowedDays);
            if (presenter.ProjectId == default(int))
            {
                presenter.ValidationErrors.Add(Id, @Resources.ProjectErrorMessage);
            }

            if (!presenter.StartDate.IsValidDatabaseDate())
            {
                presenter.ValidationErrors.Add(StartDate, @Resources.AssignmnetStartDate);
            }
            else if (presenter.StartDate < allowedDays)
            {
                presenter.ValidationErrors.Add(StartDate, string.Format("{0} {1:MM/dd/yyyy}", Resources.StartDateAllowedDaysErrorMessage, allowedDays));
            }

            if (!presenter.EndDate.IsValidDatabaseDate())
            {
                presenter.ValidationErrors.Add(EndDate, @Resources.AssignmentEndDate);
            }
            else if (presenter.StartDate.IsValidDatabaseDate() && presenter.EndDate.IsValidDatabaseDate() && presenter.StartDate > presenter.EndDate)
            {
                presenter.ValidationErrors.Add(EndDate, @Resources.EndDateInvalidErrorMessage);
            }
            else if (presenter.EndDate < allowedDays)
            {
                presenter.ValidationErrors.Add(EndDate, string.Format("{0} {1:MM/dd/yyyy}", Resources.EndDateAllowedDaysErrorMessage, allowedDays));
            }

            if (presenter.PerDayHours > presenter.MaximumAvailableHours)
            {
                if (presenter.MaximumAvailableHours == 0)
                {
                    presenter.ValidationErrors.Add(DayHours, Resources.NoHoursToAllocate);
                }
                else
                {
                    presenter.ValidationErrors.Add(DayHours, string.Format(CultureInfo.CurrentCulture, "{0} {1}", Resources.DayHoursErrorMessage, presenter.MaximumAvailableHours));
                }
            }
        }
 /// <summary>
 /// Retrieves the team allocation list.
 /// </summary>
 /// <param name="presenter">The presenter.</param>
 /// <param name="projectIds">The project ids.</param>
 /// <param name="startDate">The start date.</param>
 /// <param name="endDate">The end date.</param>
 /// <returns>
 /// List of Team Engagement Date List Item
 /// </returns>
 private IList<TeamEngagementDateListItem> RetrieveTeamAllocationList(TeamAllocationPresenter presenter, List<int> projectIds, DateTime startDate, DateTime? endDate)
 {
     var teamAllocationList = this.teamEngagementService.RetrieveList(presenter.DeveloperId, startDate, endDate, projectIds, SessionData.Instance.UserInfo.Developer.DeveloperID);
     teamAllocationList = teamAllocationList.Where(i => i.ProjectID > 0).ToList();
     teamAllocationList.ForEach(s => s.EndDate = s.Date);
     return teamAllocationList;
 }
        /// <summary>
        /// Adds the new allocation row.
        /// </summary>
        /// <param name="presenter">list of team allocation presenter</param>
        /// <returns>
        /// Add New Allocation Row
        /// </returns>
        public PartialViewResult AddNewAllocationRow(TeamAllocationPresenter presenter)
        {
            this.SetProjectListInPresenter(presenter);
            var developerList = this.developerService.RetrieveList(null, null, SessionData.Instance.UserInfo.Developer.DeveloperID);
            presenter.AssignDeveloperList(developerList.Where(x => x.DeveloperID != presenter.DeveloperId).ToList());
            int idCount = 1;
            for (int i = 0; i < presenter.TeamAllocationList.Count; i++)
            {
                if (presenter.TeamAllocationList[i].AllocationID == 0)
                {
                    var lastPartialID = presenter.TeamAllocationList.Last();
                    var id = lastPartialID.PartialID;
                    idCount = id + 1;
                }
            }

            presenter.TeamAllocationList.Add(new TeamEngagementDateListItem() { Date = DateTimeHelper.RetrieveCurrentDateTime(), EndDate = DateTimeHelper.RetrieveCurrentDateTime(), ProjectAllocatedHours = 0, AllocationID = 0, PartialID = idCount });
            presenter.IdSuffixValue = presenter.TeamAllocationList.Count;
            return this.PartialView(AllocationListConstant, presenter);
        }
        public ActionResult ValidateTeamDetails(TeamAllocationPresenter presenter)
        {
            ModelState.Clear();
            if (presenter != null)
            {
                ValidateTeamEngagementDetails(presenter);
            }

            return new JsonResult { Data = presenter, MaxJsonLength = int.MaxValue };
        }
        public PartialViewResult DeleteAllocationListRecord(TeamAllocationPresenter presenter, string allocationId, string partialId, string developerId, string startDate, string endDate, string projectId, string pageFlag, string date)
        {
            this.SetProjectListInPresenter(presenter);
            var developerList = this.developerService.RetrieveList(null, null, SessionData.Instance.UserInfo.Developer.DeveloperID);
            presenter.AssignDeveloperList(developerList.Where(x => x.DeveloperID != presenter.DeveloperId).ToList());
            var teamAllocationList = presenter.TeamAllocationList.ToList();
            var removableListItem = partialId == "0" ? presenter.TeamAllocationList.Single(r => r.AllocationID == Convert.ToInt32(allocationId)) : presenter.TeamAllocationList.Single(r => r.PartialID == Convert.ToInt32(partialId));
            if (removableListItem != null)
            {
                if (partialId == "0")
                {
                    ModelState.Clear();
                    this.teamEngagementService.Delete(Convert.ToInt32(developerId, CultureInfo.CurrentCulture), Convert.ToInt32(projectId, CultureInfo.CurrentCulture), Convert.ToDateTime(startDate, CultureInfo.InvariantCulture), Convert.ToDateTime(endDate, CultureInfo.InvariantCulture));
                    var projectids = presenter.SelectedProjects.Count > 0 ? presenter.SelectedProjects.Select(s => s.ProjectID).ToList() : null;
                    presenter.SelectedDate = (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(DateCurrentPage, StringComparison.OrdinalIgnoreCase)) || !string.IsNullOrEmpty(date) ? Convert.ToDateTime(date) : DateTimeHelper.RetrieveCurrentDateTime();
                    var startDateTime = (string.IsNullOrEmpty(pageFlag) && string.IsNullOrEmpty(date)) || (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(DateCurrentPage, StringComparison.OrdinalIgnoreCase)) ? presenter.SelectedDate : presenter.SelectedDate.RetrieveCurrentMonthStartDate();
                    var endDateTime = !string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(FromCalendarPage, StringComparison.OrdinalIgnoreCase) ? presenter.SelectedDate.RetrieveCurrentMonthEndDate() : (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(DateCurrentPage, StringComparison.OrdinalIgnoreCase) ? presenter.SelectedDate : (DateTime?)null);
                    presenter.AssignTeamAllocationList(this.RetrieveTeamAllocationList(presenter, projectids, startDateTime, endDateTime));
                    foreach (var allocationList in teamAllocationList.Where(item => item.PartialID > 0).ToList())
                    {
                        presenter.TeamAllocationList.Add(allocationList);
                    }

                    presenter.IdSuffixValue = presenter.TeamAllocationList.Count;
                }
                else
                {
                    ModelState.Clear();
                    teamAllocationList.Remove(removableListItem);
                    presenter.AssignTeamAllocationList(teamAllocationList);
                }
            }

            presenter.IdSuffixValue = presenter.TeamAllocationList.Count;
            return this.PartialView(AllocationListConstant, presenter);
        }
        public JsonResult SaveAllocationList(TeamAllocationPresenter presenter)
        {
            var result = 0;
            if (presenter != null)
            {
                int previousDaysCount = ApplicationSettings.Instance.PastAllocationAllowedDays;
                var allowedDays = DateTimeHelper.RetrieveCurrentDate().AddDays(-previousDaysCount);
                var teamEngagementDetail = new AllocationDetails();
                foreach (var allocation in presenter.TeamAllocationList)
                {
                    if ((allocation.Date <= allocation.EndDate && allocation.Date >= allowedDays) && (allocation.ProjectID != null && allocation.ProjectID > 0))
                    {
                        if (!string.IsNullOrEmpty(allocation.SelectedDeveloperIds))
                        {
                            allocation.SelectedDeveloperIds += "," + presenter.DeveloperId;
                        }
                        else
                        {
                            allocation.SelectedDeveloperIds = presenter.DeveloperId.ToString();
                        }

                        teamEngagementDetail.Add(new Allocation()
                        {
                            AllocationID = allocation.AllocationID,
                            ProjectID = allocation.ProjectID,
                            DeveloperIDs = allocation.SelectedDeveloperIds,
                            FromDate = allocation.Date,
                            ToDate = allocation.EndDate,
                            Hours = allocation.ProjectAllocatedHours
                        });
                    }
                }

                if (teamEngagementDetail.Count > 0)
                {
                    presenter.ValidationErrors.Append(this.teamEngagementService.SaveTeamEngagementList(teamEngagementDetail, SessionData.Instance.UserInfo.Developer.DeveloperID, presenter.IsHoliday, true, false));
                }

                result = presenter.ValidationErrors.Count;
                if (result > 0)
                {
                    return this.Json(presenter);
                }
            }

            return this.Json("success");
        }
        /// <summary>
        /// Manages the allocation.
        /// </summary>
        /// <param name="developerId">The developer identifier.</param>
        /// <param name="date">The date.</param>
        /// <param name="pageFlag">The page flag.</param>
        /// <returns>
        /// Manage Allocation Page
        /// </returns>
        public ActionResult ManageAllocation(int developerId, string date, string pageFlag)
        {
            this.AddBreadcrumbItem(Resources.Assignments, Url.AssignmentsAction());
            this.AddBreadcrumbItem(Resources.ViewAllocations, Url.ViewAllocationAction());
            var presenter = new TeamAllocationPresenter();
            presenter.LeaveDays = this.RetireveLeavesDetails(developerId.ToString());
            this.SetProjectListInPresenter(presenter);
            presenter.DeveloperId = developerId;
            var developerList = this.developerService.RetrieveList(null, null, SessionData.Instance.UserInfo.Developer.DeveloperID);
            presenter.AssignDeveloperList(developerList.Where(x => x.DeveloperID != developerId).ToList());
            var projectids = presenter.SelectedProjects.Count > 0 ? presenter.SelectedProjects.Select(s => s.ProjectID).ToList() : null;
            presenter.DeveloperInfo = this.developerService.RetrieveWithManagerInfo(new List<int>() { presenter.DeveloperId }, SessionData.Instance.UserInfo.Developer.DeveloperID);
            if (presenter.DeveloperInfo != null)
            {
                presenter.DeveloperInfo.DeveloperImage = presenter.DeveloperInfo.LoginID.RetrieveDeveloperImageName();
            }

            presenter.SelectedDate = (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(DateCurrentPage, StringComparison.OrdinalIgnoreCase)) || !string.IsNullOrEmpty(date) ? Convert.ToDateTime(date) : DateTimeHelper.RetrieveCurrentDateTime();
            var startDate = (string.IsNullOrEmpty(pageFlag) && string.IsNullOrEmpty(date)) || (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(DateCurrentPage, StringComparison.OrdinalIgnoreCase)) ? presenter.SelectedDate : presenter.SelectedDate.RetrieveCurrentMonthStartDate();
            var endDate = !string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(FromCalendarPage, StringComparison.OrdinalIgnoreCase) ? presenter.SelectedDate.RetrieveCurrentMonthEndDate() : (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(DateCurrentPage, StringComparison.OrdinalIgnoreCase) ? presenter.SelectedDate : (DateTime?)null);
            presenter.AssignTeamAllocationList(this.RetrieveTeamAllocationList(presenter, projectids, startDate, endDate));
            if (presenter.TeamAllocationList.Count >= 0)
            {
                if (!string.IsNullOrEmpty(pageFlag) && pageFlag.Equals(FromCalendarPage, StringComparison.OrdinalIgnoreCase))
                {
                    presenter.SelectedDate = DateTimeHelper.RetrieveCurrentDateTime();
                }

                presenter.TeamAllocationList.Add(new TeamEngagementDateListItem() { Date = presenter.SelectedDate, EndDate = presenter.SelectedDate, ProjectAllocatedHours = 0, AllocationID = 0, PartialID = 1 });
            }

            presenter.IdSuffixValue = presenter.TeamAllocationList.Count;
            return this.View(ManageAllocationConstant, presenter);
        }
        public ActionResult ViewAllocation(TeamAllocationPresenter presenter)
        {
            this.AddBreadcrumbItem(Resources.Assignments, Url.AssignmentsAction());
            this.AddBreadcrumbItem(Resources.ManageAllocation, Url.ViewAllocationAction());
            if (presenter != null)
            {
                presenter.LeaveDays = this.RetireveLeavesDetails(presenter.DeveloperId.ToString());
                this.SetProjectListInPresenter(presenter);
                if (!string.IsNullOrEmpty(presenter.SelectedProjectIds))
                {
                    List<string> ids = presenter.SelectedProjectIds.Split(CommaSeparatorCharConst).ToList();
                    presenter.AssignSelectedProjects(presenter.MasterProjects.Where(project => ids.Contains(project.ProjectID.ToString(CultureInfo.CurrentCulture))).ToList());
                    IList<DeveloperListItem> developerList = this.developerService.RetrieveList(null, null, SessionData.Instance.UserInfo.Developer.DeveloperID);
                    presenter.AssignDeveloperList(developerList.Where(x => x.DeveloperID != presenter.DeveloperId).ToList());
                }

                this.AssignTeamAllocationValues(presenter);
            }

            return this.View(ViewTeamAllocationConst, presenter);
        }
 /// <summary>
 /// Views the allocation.
 /// </summary>
 /// <param name="developerId">The developer identifier.</param>
 /// <param name="date">The date.</param>
 /// <returns>
 /// View Allocation Page
 /// </returns>
 public ActionResult ViewAllocation(int developerId, string date)
 {
     this.AddBreadcrumbItem(Resources.Assignments, Url.AssignmentsAction());
     this.AddBreadcrumbItem(Resources.ManageAllocation, Url.ViewAllocationAction());
     TeamAllocationPresenter presenter = new TeamAllocationPresenter();
     this.SetProjectListInPresenter(presenter);
     presenter.DeveloperId = developerId;
     IList<DeveloperListItem> developerList = this.developerService.RetrieveList(null, null, SessionData.Instance.UserInfo.Developer.DeveloperID);
     presenter.AssignDeveloperList(developerList.Where(x => x.DeveloperID != developerId).ToList());
     presenter.SelectedDate = date.ConvertToSelectedDateTime(DateTimeHelper.RetrieveCurrentDateTime());
     this.AssignTeamAllocationValues(presenter);
     presenter.AssignSelectedProjects(presenter.MasterProjects);
     presenter.LeaveDays = this.RetireveLeavesDetails(developerId.ToString());
     return this.View(ViewTeamAllocationConst, presenter);
 }
        public ActionResult RetrieveAvailabilityStatus(TeamAllocationPresenter presenter, string dateForAvailablity)
        {
            var result = new HoursAvailabilityListItem();
            if (!string.IsNullOrEmpty(dateForAvailablity) && dateForAvailablity != "undefined")
            {                
                presenter.StartDate = Convert.ToDateTime(dateForAvailablity);
                presenter.EndDate = Convert.ToDateTime(dateForAvailablity);
            }

            if (presenter != null)
            {
                var selectedDevIds = presenter.TeamAllocationList.FirstOrDefault(m => m.AllocationID.HasValue && m.AllocationID.Value == presenter.CopyToAllocationId);
                IList<int> selectedDeveloperListItem = null;
                if (selectedDevIds != null && !string.IsNullOrEmpty(selectedDevIds.SelectedDeveloperIds))
                {
                    selectedDeveloperListItem = selectedDevIds.SelectedDeveloperIds.Split(CommaSeparatorCharConst).Select(s => Convert.ToInt32(s, CultureInfo.CurrentCulture)).ToList();
                    selectedDeveloperListItem.Add(presenter.DeveloperId);
                    result = this.teamEngagementService.RetrieveHoursAvailability(selectedDeveloperListItem, selectedDevIds.Date, selectedDevIds.EndDate, selectedDevIds.ProjectID.Value, presenter.IsHoliday, SessionData.Instance.UserInfo.Developer.DeveloperID);
                }
                else
                {
                    selectedDeveloperListItem = new List<int>() { presenter.DeveloperId };
                    result = this.teamEngagementService.RetrieveHoursAvailability(selectedDeveloperListItem, presenter.StartDate, presenter.EndDate, presenter.ProjectId, presenter.IsHoliday, SessionData.Instance.UserInfo.Developer.DeveloperID);
                }
            }

            return new JsonResult { Data = result };
        }
        public ActionResult SaveAllocation(TeamAllocationPresenter presenter)
        {
            var result = 0;
            if (presenter != null)
            {
                IList<int> selectedDeveloperList = null;
                if (!string.IsNullOrEmpty(presenter.SelectedDeveloperIds))
                {
                    selectedDeveloperList = presenter.SelectedDeveloperIds.Split(CommaSeparatorCharConst).Select(s => Convert.ToInt32(s, CultureInfo.CurrentCulture)).ToList();
                    selectedDeveloperList.Add(presenter.DeveloperId);
                }
                else
                {
                    selectedDeveloperList = new List<int>() { presenter.DeveloperId };
                }

                presenter.ValidationErrors.Append(this.teamEngagementService.Save(selectedDeveloperList, presenter.ProjectId, presenter.StartDate, presenter.EndDate, presenter.PerDayHours, SessionData.Instance.UserInfo.Developer.DeveloperID, presenter.IsHoliday, true, false));
                result = presenter.ValidationErrors.Count;
            }

            return this.Json(result);
        }