// GET: Portal public ActionResult Overview() { //Get the user from the session Models.Security.User user = (Models.Security.User)Session["User"]; Models.Security.AuthState userState = (Models.Security.AuthState)Session["AuthState"]; if (user == null || userState == null) { return(RedirectToAction("Index", "Home")); } const int defaultDays = -7; DateTime todayDate = DateTime.UtcNow; DateTime sevenDaysBackT = todayDate.AddDays(defaultDays); DateTime svn = new DateTime(sevenDaysBackT.Year, sevenDaysBackT.Month, sevenDaysBackT.Day, 0, 0, 0); DateTime yesterdayDate = new DateTime(todayDate.AddDays(-1).Year, todayDate.AddDays(-1).Month, todayDate.AddDays(-1).Day, 23, 59, 59); //Get the list of records for the past default Days. List <DBContext.Main_Data> records = db.Main_Data.Where(x => x.Status_Start_Time >= svn && x.Status_End_Time <= yesterdayDate && x.User_ID == user.ID).ToList(); List <Models.Portal.WorkRecord> workRecords = new List <Models.Portal.WorkRecord>(); foreach (var record in records) { var newWorkRecord = new Models.Portal.WorkRecord(record); workRecords.Add(newWorkRecord); } var dailyTotalHours = Models.Portal.TimeSheet.CalculateTotalHoursForPeriod(workRecords, defaultDays, todayDate); //Get the stats for today; var todayRecords = db.Main_Data.Where(x => x.CurrentDate.Day == todayDate.Day && x.CurrentDate.Month == todayDate.Month && x.CurrentDate.Year == todayDate.Year && x.User_ID == user.ID).ToList(); List <Models.Portal.WorkRecord> todayRecordsModel = new List <Models.Portal.WorkRecord>(); foreach (var record in todayRecords) { var newWorkRecord = new Models.Portal.WorkRecord(record); todayRecordsModel.Add(newWorkRecord); } var userSettings = new Models.Security.UserSettings(); //get top five public holidays var publicHolidaysDb = db.Public_Holidays.Where(x => x.Holiday_country == user.Country && x.Holiday_date >= todayDate.Date).Take(5).ToList(); List <Models.Portal.PublicHoliday> publicHolidays = new List <Models.Portal.PublicHoliday>(); DateTime[] publicHolidaysAsArray = new DateTime[publicHolidaysDb.Count]; int counter = 0; foreach (var holiday in publicHolidaysDb) { publicHolidaysAsArray[counter] = holiday.Holiday_date.Value.Date; publicHolidays.Add(new Models.Portal.PublicHoliday(holiday)); counter++; } //calculate work time for the current month; int currentMonth = todayDate.Month; var monthlyRecords = db.Main_Data.Where(x => x.CurrentDate.Day >= 1 && x.CurrentDate.Month == currentMonth && x.CurrentDate.Year == todayDate.Year && x.CurrentDate.Day <= 31 && x.User_ID == user.ID).ToList(); var recordsModel = new List <Models.Portal.WorkRecord>(); foreach (var record in monthlyRecords) { var newWorkRecord = new Models.Portal.WorkRecord(record); recordsModel.Add(newWorkRecord); } //Get the leaves status List <DBContext.Leave> thisYearleaves = db.Leaves.Where(x => x.StartDate.Value >= todayDate.Date && x.UserId == user.ID).ToList(); List <Models.Portal.Leave.ILeave> leaves = new List <Models.Portal.Leave.ILeave>(); foreach (var leave in thisYearleaves) { var newleave = new Models.Portal.Leave.Leave(leave, publicHolidaysAsArray); leaves.Add(newleave); } //get user leaves status(entitled, accrued, carried over) List <DBContext.User_Leaves_Status> userLeavesStatusDb = db.User_Leaves_Status.Where(x => x.UserId == user.ID).ToList(); List <Models.Portal.UserLeaveStatus> userLeaveStatus = new List <Models.Portal.UserLeaveStatus>(); foreach (var item in userLeavesStatusDb) { var newLeave = new Models.Portal.UserLeaveStatus(item); userLeaveStatus.Add(newLeave); } //Set User Stats and calculate the data var userStats = new Models.Portal.UserStatus(userSettings, todayRecordsModel, leaves, userLeaveStatus); userStats.totalWorkedHoursPerMonth = userStats.CalculateWorkTimeForGivenList(recordsModel); userStats.CalculateStats(); //Get user Alerts! List <DBContext.Alert> alerts = db.Alerts.Where(x => x.UserID == user.ID || (x.ManagerId == user.ManagerID && x.IsForEveryone == true)).ToList(); //Filter the list; alerts = alerts.Where(x => (x.StartDate == null || x.EndDate == null) || x.EndDate.Value.Date > todayDate.Date).ToList(); List <Models.Portal.Alert> alertList = new List <Models.Portal.Alert>(); foreach (var alert in alerts) { var newAlert = new Models.Portal.Alert(alert); alertList.Add(newAlert); } //Generate AutoAlerts and add them to the list of the already existing one var autoAlerts = Models.Portal.AutoGeneratedAlerts.GenerateAlerts(user); alertList.AddRange(autoAlerts); //Get the requested leaves List <DBContext.Leave_Requests> requestedLeavesDb = db.Leave_Requests.Where(x => x.Requestor == user.ID && x.RequestStartDate.Value >= todayDate.Date).ToList(); foreach (var leave in requestedLeavesDb) { var newLeave = new Models.Portal.Leave.LeaveRequest(leave, publicHolidaysAsArray); leaves.Add(newLeave); } //Get the requested leaves List <DBContext.Leaves_Saved_Not_Submited> savedLeavesDb = db.Leaves_Saved_Not_Submited.Where(x => x.UserID == user.ID && x.StartDate.Value >= todayDate.Date).ToList(); foreach (var leave in savedLeavesDb) { var newLeave = new Models.Portal.Leave.SavedLeave(leave, publicHolidaysAsArray); leaves.Add(newLeave); } leaves = leaves.OrderBy(x => x.start).Take(5).ToList(); var dashboard = new Models.Portal.Dashboard(user, dailyTotalHours, userStats, leaves, alertList, publicHolidays); return(View(dashboard)); }
public ActionResult MyTimeSheet() { Models.Security.User user = (Models.Security.User)Session["User"]; Models.Security.AuthState userState = (Models.Security.AuthState)Session["AuthState"]; if (user == null || userState == null) { return(RedirectToAction("Index", "Home")); } var worklogsDb = db.Main_Data.Where(x => x.User_ID == user.ID).OrderByDescending(x => x.Status_Start_Time).Take(100); List <Models.Portal.WorkRecord> workLogs = new List <Models.Portal.WorkRecord>(); foreach (var worklog in worklogsDb) { var newWorkLog = new Models.Portal.WorkRecord(worklog); workLogs.Add(newWorkLog); } //Get today date in UTC Format DateTime todayDate = DateTime.UtcNow.Date; //Get the public holidays var publicHolidaysDb = db.Public_Holidays.Where(x => x.Holiday_country == user.Country && x.Holiday_date >= todayDate.Date).ToList(); List <Models.Portal.PublicHoliday> publicHolidays = new List <Models.Portal.PublicHoliday>(); DateTime[] publicHolidaysAsArray = new DateTime[publicHolidaysDb.Count]; int counter = 0; foreach (var holiday in publicHolidaysDb) { publicHolidaysAsArray[counter] = holiday.Holiday_date.Value.Date; publicHolidays.Add(new Models.Portal.PublicHoliday(holiday)); counter++; } List <DBContext.Leave> thisYearleaves = db.Leaves.Where(x => x.StartDate.Value.Year == todayDate.Year && x.UserId == user.ID && x.EndDate.Value.Year == todayDate.Year).ToList(); List <Models.Portal.Leave.ILeave> leaves = new List <Models.Portal.Leave.ILeave>(); foreach (var leave in thisYearleaves) { var newleave = new Models.Portal.Leave.Leave(leave, publicHolidaysAsArray); leaves.Add(newleave); } //Get the requested leaves List <DBContext.Leave_Requests> requestedLeavesDb = db.Leave_Requests.Where(x => x.Requestor == user.ID && x.RequestStartDate.Value >= todayDate.Date).ToList(); foreach (var leave in requestedLeavesDb) { var newLeave = new Models.Portal.Leave.LeaveRequest(leave, publicHolidaysAsArray); leaves.Add(newLeave); } //Get the requested leaves List <DBContext.Leaves_Saved_Not_Submited> savedLeavesDb = db.Leaves_Saved_Not_Submited.Where(x => x.UserID == user.ID && x.StartDate.Value >= todayDate.Date).ToList(); foreach (var leave in savedLeavesDb) { var newLeave = new Models.Portal.Leave.SavedLeave(leave, publicHolidaysAsArray); leaves.Add(newLeave); } //get a list of projects that the user is assign to var projectsDb = db.User_Assigned_Projects.Where(x => x.UserID == user.ID).ToList(); List <Models.Portal.Project> projectsAssignedToUser = new List <Models.Portal.Project>(); //add one item as default All projects with the value 0 projectsAssignedToUser.Add(new Models.Portal.Project(0, "All projects", user.ID)); foreach (var project in projectsDb) { projectsAssignedToUser.Add( new Models.Portal.Project(project) ); } //Implement Dependency injection //Mostlikly is not needed var cb = new ContainerBuilder(); //cb.RegisterType<List<Models.Portal.WorkRecord>>(); //cb.RegisterType<List<Models.Portal.Leave.ILeave>>(); cb.RegisterInstance(workLogs).As <List <Models.Portal.WorkRecord> >(); cb.RegisterInstance(leaves).As <List <Models.Portal.Leave.ILeave> >(); cb.RegisterInstance(projectsAssignedToUser).As <List <Models.Portal.Project> >(); cb.RegisterType <Models.Activity.MyActivity>(); var builder = cb.Build(); var myActivity = builder.Resolve <Models.Activity.MyActivity>(); //Models.Activity.MyActivity myActivity = new Models.Activity.MyActivity(); //myActivity.workLogs = workLogs; //myActivity.leaves = leaves; //Add values to session for further use when the user change the number of records to show SetMyStatSession(myActivity); Session.Timeout = 120; return(View(myActivity)); }