Пример #1
0
        public static void SetDefaultClockoutTimeStampToForgottenStamps(string userName)
        {
            var startDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]).Date;

            using (var dbContext = new TimeTrackingEntities())
            {
                var userForgottenTimeOutStamps = (from utsh in dbContext.UserTimeTrackHistories
                                                  where utsh.IsDeleted == false &&
                                                  utsh.UserName.ToLower().Equals(userName.ToLower()) &&
                                                  utsh.StampDate < startDate &&
                                                  (utsh.ClockInTime.Length > 0 && (utsh.ClockOutTime == null || utsh.ClockOutTime.Length == 0))
                                                  select utsh).ToList();
                if (userForgottenTimeOutStamps.Any())
                {
                    var sDate = new DateTime();
                    foreach (var userForgottenTimeOutStamp in userForgottenTimeOutStamps)
                    {
                        var timeSpan = TimeSpan.Parse(WebConfigurationManager.AppSettings["AutomaticTimeOutLimit"]);
                        userForgottenTimeOutStamp.ClockOutTime = String.Format("{0:t}", sDate.Add(timeSpan));
                        userForgottenTimeOutStamp.UpdatedBy    = "sys";
                        userForgottenTimeOutStamp.UpdatedDate  = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]);
                    }
                }
                dbContext.SaveChanges();
            }
        }
Пример #2
0
        public static WeeklyTimeTrackWeekListViewModel GetClockInOutTime(string userName, DateTime startDate, DateTime endDate)
        {
            var weeklyTimeTrack = GetWeeklyClockInOutTimeByDate(userName, startDate, endDate);
            var selectedValue   = weeklyTimeTrack.WeekStartDate.HasValue
                                    ? weeklyTimeTrack.WeekStartDate.Value.ToString()
                                    : string.Empty;

            return(new WeeklyTimeTrackWeekListViewModel
            {
                WeeklyTimeTrack = weeklyTimeTrack,
                SelectedValue = selectedValue,
                UserName = userName,
                WeekList = new SelectList(WeekManager.GetWeekList(WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]), Convert.ToInt32(WebConfigurationManager.AppSettings["NumberOfPriorWeeks"]), true), "WeekStartDate", "WeekStartEndDateDisplay", selectedValue)
            });
        }
Пример #3
0
        public static void TrackClockInOutTime(string userName)
        {
            var startDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]).Date;
            var endDate   = startDate.AddDays(1);

            SetDefaultClockoutTimeStampToForgottenStamps(userName);
            using (var dbContext = new TimeTrackingEntities())
            {
                var usersLatestRecord = (from utsh in dbContext.UserTimeTrackHistories
                                         where utsh.IsDeleted == false &&
                                         utsh.UserName.ToLower().Equals(userName.ToLower()) &&
                                         (utsh.StampDate >= startDate && utsh.StampDate < endDate) &&
                                         utsh.ClockInTime.Length > 0 && (utsh.ClockOutTime == null || utsh.ClockOutTime.Length == 0)
                                         select utsh).OrderByDescending(c => c.StampDate).ThenByDescending(c => c.CreatedDate).FirstOrDefault();

                if (usersLatestRecord != null) // User has clocked in, Clock user out
                {
                    usersLatestRecord.ClockOutTime = string.Format("{0:t}", WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]));
                    usersLatestRecord.UpdatedBy    = userName;
                    usersLatestRecord.UpdatedDate  = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]);
                    usersLatestRecord.UserIP       = WebHelpers.GetIpAddress();
                }
                else // User hasn't clocked in yet, Clock user in
                {
                    var userTimeStampHistory = new UserTimeTrackHistory
                    {
                        UserId      = MembershipUserExtended.GetUserIdByUserName(userName),
                        UserName    = userName,
                        ClockInTime = string.Format("{0:t}", WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"])),
                        StampDate   = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]),
                        CreatedBy   = userName,
                        CreatedDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]),
                        UserIP      = WebHelpers.GetIpAddress(),
                        IsDeleted   = false
                    };
                    dbContext.UserTimeTrackHistories.Add(userTimeStampHistory);
                }
                dbContext.SaveChanges();
            }
        }
Пример #4
0
        public static DailyTimeTrack GetCurrentDayClockInOutTime(string userName)
        {
            var startDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]).Date;
            var endDate   = startDate.AddDays(1);

            using (var dbContext = new TimeTrackingEntities())
            {
                var currentUser =
                    dbContext.ExtendedUserProfiles.FirstOrDefault(c => c.UserName.ToLower().Equals(userName));

                var userClockInOutTimeList = (from utsh in dbContext.UserTimeTrackHistories
                                              where utsh.IsDeleted == false &&
                                              utsh.UserName.ToLower().Equals(userName.ToLower()) &&
                                              (utsh.StampDate >= startDate && utsh.StampDate < endDate) &&
                                              utsh.ClockInTime.Length > 0
                                              select utsh).ToList();

                if (userClockInOutTimeList.Any())
                {
                    var userTimeStampHistory = userClockInOutTimeList.FirstOrDefault();
                    if (userTimeStampHistory != null)
                    {
                        var dailyTimeTrack = new DailyTimeTrack(userTimeStampHistory.StampDate, currentUser.HourlyRate.HasValue ? currentUser.HourlyRate.Value : 0)
                        {
                            TimeTrackList =
                                GetTimeTrackList(userClockInOutTimeList, userTimeStampHistory.StampDate).
                                OrderByDescending(c => c.ClockInTime).ThenByDescending(
                                    d => d.ClockOutTime).ToList()
                        };

                        dailyTimeTrack.SubmitButtonText = dailyTimeTrack.TimeTrackList.Any(c => c.ClockOutTime == null) ? WebConfigurationManager.AppSettings["ClockOutText"] : WebConfigurationManager.AppSettings["ClockInText"];
                        return(dailyTimeTrack);
                    }
                }
                return(new DailyTimeTrack());
            }
        }
Пример #5
0
        public ActionResult Create(UserTimeTrackHistoryMapped utth, FormCollection collection)
        {
            var    userList = MembershipUserExtended.GetFullNameUserNameList();
            string userName = collection["UserName"];

            if (userList.ContainsValue(userName))
            {
                utth.UserName = userName;

                DateTime clockInDt;
                DateTime clockOutDt;
                if (DateTime.TryParse(utth.ClockInTime, out clockInDt) && DateTime.TryParse(utth.ClockOutTime, out clockOutDt))
                {
                    if (clockOutDt.TimeOfDay.CompareTo(clockInDt.TimeOfDay) != -1) // if clock out time is earlier than clock in time than error
                    {
                        utth.ClockInTime  = string.Format("{0:t}", clockInDt);
                        utth.ClockOutTime = string.Format("{0:t}", clockOutDt);

                        utth.UserId      = MembershipUserExtended.GetUserIdByUserName(userName);
                        utth.CreatedBy   = LoggedInUserName;
                        utth.CreatedDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]);
                        utth.IsDeleted   = false;
                        var tth = utth.Get(utth.Save());
                        tth.UserName    = userList.FindKeyByValue(userName);
                        ViewBag.Message = "Record inserted successfully.";
                        return(View(tth));
                    }
                    ViewBag.Message = "Clock Out time can not be earlier than Clock In time.";
                    return(View(utth));
                }
                ViewBag.Message = "Not a valid Clock In/Out time, please make sure time is in correct format.";
                return(View(utth));
            }
            ViewBag.Message = "Error inserting record.";
            return(View(new UserTimeTrackHistoryMapped()));
        }
Пример #6
0
        public static WeeklyTimeTrackWeekListViewModel GetCurrentWeekClockInOutTime(string userName)
        {
            var weekManager = new WeekManager(WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]));

            return(GetClockInOutTime(userName, weekManager.WeekStartDate, weekManager.WeekEndDate));
        }
Пример #7
0
        public static TimeTrackErrorViewModel UpdateClockInOutTime(int timeTrackId, string stampDate, string selectedUser, string clockInTime, string clockOutTime, string updatedBy)
        {
            using (var dbContext = new TimeTrackingEntities())
            {
                var startDate = DateTime.Parse(stampDate);
                var endDate   = startDate.AddDays(1);
                var userTimeTrackRecordForStampedDate = dbContext.UserTimeTrackHistories
                                                        .Where(utsh => utsh.IsDeleted == false && utsh.UserName.ToLower().Equals(selectedUser.ToLower()) && (utsh.StampDate >= startDate && utsh.StampDate < endDate))
                                                        .ToList();

                var userTimeTrackRecord   = userTimeTrackRecordForStampedDate.FirstOrDefault(utsh => utsh.TimeTrackId == timeTrackId);
                var indexOfSelectedRecord = userTimeTrackRecordForStampedDate.IndexOf(userTimeTrackRecord);


                var errorMessage = new StringBuilder();
                var errorCount   = 0;
                var sDate        = new DateTime();

                if (userTimeTrackRecord != null)
                {
                    DateTime updatedClockInDateTime;
                    DateTime updatedClockOutDateTime;
                    if (DateTime.TryParse(clockInTime, out updatedClockInDateTime) && DateTime.TryParse(clockOutTime, out updatedClockOutDateTime))
                    {
                        var updatedClockInTime  = updatedClockInDateTime.TimeOfDay;
                        var updatedClockOutTime = updatedClockOutDateTime.TimeOfDay;

                        // CHECK IF userTimeTrackRecord object has CloclOutTime set. IF NOT SET IT WITH PROVIDED ClockOutTime
                        if (string.IsNullOrEmpty(userTimeTrackRecord.ClockOutTime))
                        {
                            userTimeTrackRecord.ClockOutTime = String.Format("{0:t}", sDate.Add(updatedClockOutTime));
                        }

                        var previousTimeTrackClockOutTimeRecord = string.Empty;
                        var nextTimeTrackClockInTimeRecord      = string.Empty;

                        if (indexOfSelectedRecord > 0 && (indexOfSelectedRecord - 1) >= 0)
                        {
                            previousTimeTrackClockOutTimeRecord =
                                userTimeTrackRecordForStampedDate[indexOfSelectedRecord - 1].ClockOutTime;
                        }

                        if ((indexOfSelectedRecord + 1) < userTimeTrackRecordForStampedDate.Count)
                        {
                            nextTimeTrackClockInTimeRecord =
                                userTimeTrackRecordForStampedDate[indexOfSelectedRecord + 1].ClockInTime;
                        }

                        if (updatedClockOutTime.CompareTo(updatedClockInTime) == -1)
                        {
                            errorMessage.Append("Updated Clock Out time can not be earlier than Updated Clock In time.");
                            errorCount += 1;
                        }
                        else if (!updatedClockInTime.Equals(DateTime.Parse(userTimeTrackRecord.ClockInTime).TimeOfDay))
                        {
                            if ((!string.IsNullOrEmpty(previousTimeTrackClockOutTimeRecord) &&
                                 updatedClockInTime.CompareTo(
                                     DateTime.Parse(previousTimeTrackClockOutTimeRecord).TimeOfDay) == -1) ||
                                (!string.IsNullOrEmpty(nextTimeTrackClockInTimeRecord) &&
                                 updatedClockInTime.CompareTo(DateTime.Parse(nextTimeTrackClockInTimeRecord).TimeOfDay) ==
                                 1))
                            {
                                // error clock in time cannot be earlier then previous checkout time and can not be later then next clockout time
                                errorMessage.Append(
                                    "Updated Clock In time can not be earlier than previous clock out time or later than earlier clock in time");
                                errorCount += 1;
                            }
                        }
                        else if (!updatedClockOutTime.Equals(DateTime.Parse(userTimeTrackRecord.ClockOutTime).TimeOfDay))
                        {
                            // Clock Out time has changed
                            if ((!string.IsNullOrEmpty(previousTimeTrackClockOutTimeRecord) &&
                                 updatedClockOutTime.CompareTo(
                                     DateTime.Parse(previousTimeTrackClockOutTimeRecord).TimeOfDay) == -1) ||
                                (!string.IsNullOrEmpty(nextTimeTrackClockInTimeRecord) &&
                                 updatedClockOutTime.CompareTo(
                                     DateTime.Parse(nextTimeTrackClockInTimeRecord).TimeOfDay) == 1))
                            {
                                errorMessage.Append(
                                    "Updated Clock Out time can not be earlier than previous clock out time or later than earlier clock in time");
                                errorCount += 1;
                            }
                        }
                        else if (
                            !updatedClockInTime.Equals(DateTime.Parse(userTimeTrackRecord.ClockInTime).TimeOfDay) &&
                            (!updatedClockOutTime.Equals(
                                 DateTime.Parse(userTimeTrackRecord.ClockOutTime).TimeOfDay)))
                        {
                            // Both clock in and out time has been changed
                            if (
                                updatedClockInTime.CompareTo(
                                    DateTime.Parse(previousTimeTrackClockOutTimeRecord).TimeOfDay) == -1 ||
                                updatedClockInTime.CompareTo(
                                    DateTime.Parse(nextTimeTrackClockInTimeRecord).TimeOfDay) == 1)
                            {
                                errorMessage.Append(
                                    "Updated Clock Out time can not be earlier than previous clock out time or later than earlier clock in time");
                                errorCount += 1;
                            }
                            if (
                                updatedClockOutTime.CompareTo(
                                    DateTime.Parse(previousTimeTrackClockOutTimeRecord).TimeOfDay) == -1 ||
                                updatedClockOutTime.CompareTo(
                                    DateTime.Parse(nextTimeTrackClockInTimeRecord).TimeOfDay) == 1)
                            {
                                errorMessage.Append(
                                    "Updated Clock Out time can not be earlier than previous clock out time or later than earlier clock in time");
                                errorCount += 1;
                            }
                        }

                        if (errorCount == 0)
                        {
                            userTimeTrackRecord.ClockInTime  = String.Format("{0:t}", sDate.Add(updatedClockInTime));
                            userTimeTrackRecord.ClockOutTime = String.Format("{0:t}", sDate.Add(updatedClockOutTime));
                            userTimeTrackRecord.UpdatedBy    = updatedBy;
                            userTimeTrackRecord.UpdatedDate  = WebHelpers.GetCurrentDateTimeByTimeZoneId(WebConfigurationManager.AppSettings["UserTimeZoneId"]);
                            dbContext.SaveChanges();
                        }
                        return(new TimeTrackErrorViewModel(GetTimeTrackFromUserTimeTrackHistory(dbContext.UserTimeTrackHistories.FirstOrDefault(utsh => utsh.TimeTrackId == timeTrackId)), errorMessage.ToString()));
                    }
                    return(new TimeTrackErrorViewModel(GetTimeTrackFromUserTimeTrackHistory(dbContext.UserTimeTrackHistories.FirstOrDefault(utsh => utsh.TimeTrackId == timeTrackId)), "Please provide a valid Clock In/Clock Out time."));
                }
                return(new TimeTrackErrorViewModel(GetTimeTrackFromUserTimeTrackHistory(dbContext.UserTimeTrackHistories.FirstOrDefault(utsh => utsh.TimeTrackId == timeTrackId)), string.Empty));
            }
        }