public void Ticker_Tick(object sender, EventArgs e)
 {
     _context = new RfidContext();
     foreach (var user in _context.C_Users)
     {
         if (user.isInside)
         {
             if (user.P_UserTime.Count != 0)
             {
                 user.P_UserTime.Last().TimeOut = DateTime.Now;
                 TimeSpan?lengthOfInside        = user.P_UserTime.Last().TimeOut - user.P_UserTime.Last().TimeIn;
                 if (lengthOfInside > Singelton.WatcherSetting.MaxTimeInside)
                 {
                     lengthOfInside = Singelton.WatcherSetting.MaxTimeInside;
                     user.P_UserTime.Last().TimeOut = user.P_UserTime.Last().TimeIn + MaxTimeInside;
                     DateTime?dtInsige = DateTime.Today.Add(lengthOfInside.Value);
                     user.P_UserTime.Last().LengthOfInside = dtInsige;
                     user.isInside = false;
                     _context.SaveChanges();
                 }
                 else
                 {
                 }
             }
         }
     }
     _context.Dispose();
 }
        public M_Users GetUserInfo(int selectedId, TextBlock nameUser, ImageBrush imageReport)
        {
            var db           = new RfidContext();
            var selectedUser = (from d in db.C_Users
                                where d.ID == selectedId
                                select d).Single();
            var dir = new DirectoryInfo(Singelton.PathToPhoto);

            var queryPhoto = from cust in db.C_Users
                             where cust.ID == selectedId
                             select new { cust.Photo };

            var names = db.Database.SqlQuery <M_Names>(new SqlGetUserName()
                                                       .SQl_GetUserFirstName, selectedId).SingleOrDefault();
            var firstName = names.NameFirst;
            var lastName  = names.NameLast;
            var z         = queryPhoto.ToList();

            nameUser.Text = firstName + " " + lastName;
            var str = dir.FullName + Path.AltDirectorySeparatorChar + z[0].Photo;

            try
            {
                var nameImage = z[0].Photo;
                var words     = nameImage.Split('_');
                var indexdot  = words[2].IndexOf('.');
                nameUser.Text           = words[1] + " " + words[2].Remove(indexdot);
                imageReport.ImageSource = ImageLoaderHelper.GetImageFromFolder(str);
            }
            catch (Exception ex)
            {
                MessageBox.Show((string)Application.Current.Resources[StringsMapper.ErrorUserPhoto] + ex);
            }

            db.Dispose();
            return(selectedUser);
        }
        public void FactoryDirectors()
        {
            RfidContext db = new RfidContext();

            Directors = (from der in db.C_Users
                         where (bool)der.IsDirector
                         select new Directors
            {
                NameDepart = der.P_Departments.Name,
                CodeDepart = der.P_Departments.CodeFull,

                First = der.P_Departments.DepartmentDirectorName.FirstOrDefault().NameFirst,
                Last = der.P_Departments.DepartmentDirectorName.FirstOrDefault().NameLast,
                Third = der.P_Departments.DepartmentDirectorName.FirstOrDefault().NameThird,
                PhoneNumber = der.P_Departments.DepartmentDirectorPhone.FirstOrDefault().PhoneNumber
            }).ToList();

            foreach (var dir in Directors)
            {
                dir.CreateFullSearchString();
            }

            db.Dispose();
        }
        public M_Users GetCurrentUser(int?selectedId, DataGrid monthlyReportGrid, DataGrid basicInformationGrid)
        {
            var db           = new RfidContext();
            var selectedUser = (from d in db.C_Users
                                where d.ID == selectedId
                                select d).Single();
            var culture = App.Language;

            if (selectedUser.P_UserTime.ToList().Count != 0)
            {
                var queryAllCustomers = (from cust in db.C_UserTime
                                         where cust.P_Users.ID == selectedId
                                         select cust)
                                        .AsEnumerable()
                                        .Select(cust => new
                {
                    cust.ID,
                    cust.SingleDate,
                    Day = culture.DateTimeFormat.GetDayName(((DayOfWeek)Enum.Parse(typeof(DayOfWeek), cust.Day))),
                    cust.TimeIn,
                    cust.TimeOut,
                    cust.LengthOfInside,
                    cust.LengthOfOutside
                });

                monthlyReportGrid.ItemsSource = queryAllCustomers.ToList();
                monthlyReportGrid.Items.Refresh();
            }
            else
            {
                MessageBox.Show((string)Application.Current.Resources[StringsMapper.ErrorUserTimeNFound],
                                (string)Application.Current.Resources[StringsMapper.ErrorHeader],
                                MessageBoxButton.OK, MessageBoxImage.Error);
                monthlyReportGrid.ItemsSource = null;
            }

            // get your 'Uploaded' folder
            var dir        = new DirectoryInfo(Singelton.PathToPhoto);
            var queryPhoto = from cust in db.C_Users
                             where cust.ID == selectedId
                             select new { cust.Photo };
            var names = db.Database
                        .SqlQuery <M_Names>(new SqlGetUserName().SQl_GetUserFirstName, (object)selectedId)
                        .SingleOrDefault();
            var firstName = names.NameFirst;
            var lastName  = names.NameLast;
            var z         = queryPhoto.ToList();

            ((TextBlock)basicInformationGrid.FindName("NameUser")).Text = firstName + " " + lastName;
            var str = dir.FullName + Path.AltDirectorySeparatorChar + z[0].Photo;


            try
            {
                var NameImage = z[0].Photo;
                var words     = NameImage.Split('_');
                var indexdot  = words[2].IndexOf('.');
                ((TextBlock)basicInformationGrid.FindName("NameUser")).Text            = words[1] + " " + words[2].Remove(indexdot);
                ((ImageBrush)basicInformationGrid.FindName("imageReport")).ImageSource =
                    ImageLoaderHelper.GetImageFromFolder(str);
            }
            catch (Exception)
            {
                ((ImageBrush)basicInformationGrid.FindName("imageReport")).ImageSource =
                    new BitmapImage(new Uri("pack://application:,,,/Image/ProfileIcon.png"));
                var message = (string)Application.Current.Resources[StringsMapper.ErrorUserPhoto];
                MessageBox.Show(message, (string)Application.Current.Resources[StringsMapper.ErrorHeader],
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }


            db.Dispose();
            return(selectedUser);
        }