Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
0
        protected bool MigrateUserData(string userName)
        {
            var userNameIdDic = MembershipUserExtended.GetUserIdUserNameList();

            try
            {
                var ravenUserHistory =
                    RavenSession.Query <UserTimeStampsHistory>().Where(c => c.UserName == userName).ToList();
                var efUHistoryList = UserTimeTrackHistory.GetUserTimeStampHistory(userName);
                if (ravenUserHistory.Count > 0)
                {
                    if (efUHistoryList.Count > 0)
                    {
                        foreach (var efUserTimeStampHistory
                                 in from history in ravenUserHistory
                                 from timeStamp in history.History
                                 where !efUHistoryList.Any(
                                     c =>
                                     c.ClockInTime.Equals(timeStamp.Start_time) &&
                                     c.ClockOutTime.Equals(timeStamp.End_time))
                                 select new UserTimeTrackHistory()
                        {
                            UserId = userNameIdDic[userName],
                            UserName = userName,
                            ClockInTime = DateTime.Parse(timeStamp.Start_time).TimeOfDay.ToString(),
                            ClockOutTime = DateTime.Parse(timeStamp.End_time).TimeOfDay.ToString(),
                            StampDate = DateTime.Parse(timeStamp.Stampdate),
                            CreatedBy = HttpContext.User.Identity.Name,
                            CreatedDate = DateTime.Now,
                            UserIP = WebHelpers.GetIpAddress() + "~" + Request.UserHostName
                        })
                        {
                            efUserTimeStampHistory.Save();
                        }
                    }
                    else
                    {
                        foreach (var efUserTimeStampHistory
                                 in from history in ravenUserHistory
                                 from timeStamp in history.History
                                 select new UserTimeTrackHistory()
                        {
                            UserId = userNameIdDic[userName],
                            UserName = userName,
                            ClockInTime = DateTime.Parse(timeStamp.Start_time).TimeOfDay.ToString(),
                            ClockOutTime = DateTime.Parse(timeStamp.End_time).TimeOfDay.ToString(),
                            StampDate = DateTime.Parse(timeStamp.Stampdate),
                            CreatedBy = HttpContext.User.Identity.Name,
                            CreatedDate = DateTime.Now,
                            UserIP =
                                WebHelpers.GetIpAddress() + "~" + Request.UserHostName
                        })
                        {
                            efUserTimeStampHistory.Save();
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }