public static void TrackClockInOutTime(int userId, string userName)
        {
            var startDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(ConfigurationManager.AppSettings["UserTimeZoneId"]).Date;
            var endDate = startDate.AddDays(1);
            SetDefaultClockoutTimeStampToForgottenStamps(userName);
            using (var dbContext = new SchTimeTrackingEntities())
            {
                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(ConfigurationManager.AppSettings["UserTimeZoneId"]));
                    usersLatestRecord.UpdatedBy = userName;
                    usersLatestRecord.UpdatedDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(ConfigurationManager.AppSettings["UserTimeZoneId"]);
                    usersLatestRecord.UserIP = WebHelpers.GetIpAddress();
                }
                else // User hasn't clocked in yet, Clock user in
                {
                    var userTimeStampHistory = new UserTimeTrackHistory
                    {
                        UserId = userId,
                        UserName = userName,
                        ClockInTime = string.Format("{0:t}", WebHelpers.GetCurrentDateTimeByTimeZoneId(ConfigurationManager.AppSettings["UserTimeZoneId"])),
                        StampDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(ConfigurationManager.AppSettings["UserTimeZoneId"]),
                        CreatedBy = userName,
                        CreatedDate = WebHelpers.GetCurrentDateTimeByTimeZoneId(ConfigurationManager.AppSettings["UserTimeZoneId"]),
                        UserIP = WebHelpers.GetIpAddress(),
                        IsDeleted = false
                    };
                    dbContext.UserTimeTrackHistories.Add(userTimeStampHistory);
                }
                dbContext.SaveChanges();
            }
        }
 public int Save()
 {
     using (var dbContext = new SchTimeTrackingEntities())
     {
         UserName = dbContext.UserProfiles.FirstOrDefault(c => c.UserId.Equals(UserId)).UserName;
         var utth = new UserTimeTrackHistory
             {
                 UserId = UserId,
                 UserName = UserName,
                 ClockInTime = ClockInTime,
                 ClockOutTime = ClockOutTime,
                 StampDate = StampDate,
                 UserIP = UserIp,
                 IsDeleted = IsDeleted,
                 CreatedBy = CreatedBy,
                 CreatedDate = CreatedDate
             };
         dbContext.UserTimeTrackHistories.Add(utth);
         dbContext.SaveChanges();
         return utth.TimeTrackId;
     }
 }
 public static TimeTrack GetTimeTrackFromUserTimeTrackHistory(UserTimeTrackHistory userTimeTrackHistory)
 {
     if (userTimeTrackHistory != null)
     {
         return new TimeTrack(userTimeTrackHistory.TimeTrackId, userTimeTrackHistory.StampDate,
                                userTimeTrackHistory.ClockInTime,
                                userTimeTrackHistory.ClockOutTime);
     }
     return new TimeTrack();
 }