Example #1
0
 public UserStatus(Security.UserSettings userSettings, List <WorkRecord> records, List <Leave.ILeave> leaves, List <UserLeaveStatus> leavesStatus)
 {
     this.userSettings = userSettings;
     this.records      = records;
     this.leaves       = leaves;
     this.leavesStatus = leavesStatus;
 }
        // 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));
        }