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();
            }
        }
 public int Save()
 {
     using (var dbContext = new TimeTrackingEntities())
     {
         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();
 }