Exemple #1
0
        public MainViewModel()
        {
            EmployeeHandler = new EmployeeHandler(this);
            if (OpenDbConnection)
            {
                PersistencyService.OpenApiConnection();
            }

            EmployeesInDb     = new ObservableCollection <Employee>();
            LoggedInEmployees = new List <Employee>();
            DatabaseTable     = new ObservableCollection <Employee>();

            if (OpenDbConnection)
            {
                PersistencyService.GetData(EmployeesInDb);
                PersistencyService.GetDataLoggedIn(LoggedInEmployees);
            }

            LoginOrLogoutCommand           = new RelayCommand(EmployeeHandler.LoginOrLogout);
            CompleteEmployeeListCommand    = new RelayCommand(EmployeeHandler.CompleteEmployeeList);
            PersonalEntryListCommand       = new RelayCommand(EmployeeHandler.PersonalEntryList);
            OwnDepartmentListCommand       = new RelayCommand(EmployeeHandler.OwnDepartmentList);
            AddSickOrVacationdaysCommand   = new RelayCommand(EmployeeHandler.ChangeVacationOrSickdays);
            AdminChangePersonalInfoCommand = new RelayCommand(EmployeeHandler.AdminChangePersonalInfo);
            AdminChangeSalaryInfoCommand   = new RelayCommand(EmployeeHandler.AdminChangeSalaryInfo);
            SortByNameCommand       = new RelayCommand(EmployeeHandler.SortByName);
            SortByEmployeeIdCommand = new RelayCommand(EmployeeHandler.SortByEmployeeId);
            ExportAsCsvCommand      = new RelayCommand(() => ExportEmployeesToCsvFile());
        }
        /// <summary>
        /// The admin metohd of changing other employee's personal data (Name and Manager attribtues).
        /// </summary>
        public void AdminChangePersonalInfo()
        {
            if (SelectedEmployee == null)
            {
                if (MainViewModel.OpenDbConnection == false)
                {
                    throw new NullReferenceException("Please select an employee to edit.");
                }
                return;
            }

            //Admin level 1 and 2 privileges check
            var admin = (AdminEmp)LastLoggedIn;

            if (admin.AdminLvl != 2) //If admin is lvl 1
            {
                if (SelectedEmployee.Manager != LastLoggedIn.Name)
                {
                    if (MainViewModel.OpenDbConnection == false)
                    {
                        throw new ArgumentException("Error! You can only change data for your own employees.");
                    }
                    var errorMsg = new MessageDialog("You can only change data for your own employees.", "Error");
                    errorMsg.ShowAsync();
                    return;
                }
            }

            if (!String.IsNullOrWhiteSpace(_viewModel.AdminChangeNameBox))
            {
                SelectedEmployee.Name = _viewModel.AdminChangeNameBox;
            }
            if (!String.IsNullOrWhiteSpace(_viewModel.AdminChangeManagerBox))
            {
                SelectedEmployee.Manager = _viewModel.AdminChangeManagerBox;
            }

            if (MainViewModel.OpenDbConnection == false)
            {
                return;
            }

            PersistencyService.PutData(SelectedEmployee);
            PersistencyService.GetData(_viewModel.EmployeesInDb);
            if (admin.AdminLvl != 2)
            {
                OwnDepartmentList();
            }
            else
            {
                PersistencyService.GetData(_viewModel.DatabaseTable);
            }
        }
        /// <summary>
        /// Returns the complete visual list of employees in the admin's own department.
        /// </summary>
        public void OwnDepartmentList()
        {
            PersistencyService.GetData(_viewModel.EmployeesInDb);
            var allEmployees  = _viewModel.EmployeesInDb.ToList();
            var ownDepartment = from e in allEmployees where e.Manager == LastLoggedIn.Name select e;

            _viewModel.DatabaseTable.Clear();
            foreach (var e in ownDepartment)
            {
                _viewModel.DatabaseTable.Add(e);
            }
        }
        private void UpdateLogoutTime()
        {
            if (MainViewModel.OpenDbConnection)
            {
                PersistencyService.GetData(_viewModel.EmployeesInDb);                               //Need updated LastLogin before updating LastLogout
            }
            var employeeList     = _viewModel.EmployeesInDb.ToList();
            var employeeToLogout = employeeList.Find(e => e.EmployeeId == _employeeToLogout.EmployeeId);

            _employeeToLogout            = employeeToLogout;
            _employeeToLogout.LastLogout = DateTime.Now;
        }
        /// <summary>
        /// The standard- and admin employee's method of adding own vacation or sickdays.
        /// </summary>
        public void ChangeVacationOrSickdays()
        {
            if (DanxMainPage.SickDayRButton.IsChecked == true)
            {
                LastLoggedIn.SickDays += 1;
            }
            else if (DanxMainPage.VacationDayRButton.IsChecked == true)
            {
                LastLoggedIn.VacationDays += 1;
            }

            if (MainViewModel.OpenDbConnection == false)
            {
                return;
            }
            PersistencyService.PutData(LastLoggedIn);

            PersistencyService.GetData(_viewModel.EmployeesInDb);
            var updatedEmpList = _viewModel.EmployeesInDb.ToList();
            var updatedEmp     = updatedEmpList.Find(e => e.EmployeeId == LastLoggedIn.EmployeeId);

            _viewModel.DatabaseTable.Clear();
            _viewModel.DatabaseTable.Add(updatedEmp);
        }
        /// <summary>
        ///  The admin metohd of changing other employee's salary data (SalaryNumber, VacationDays, SickDays and WorkedDays attribtues).
        /// </summary>
        public void AdminChangeSalaryInfo()
        {
            if (SelectedEmployee == null)
            {
                return;
            }

            var admin = (AdminEmp)LastLoggedIn;

            if (admin.AdminLvl != 2) //If admin is lvl 1
            {
                if (SelectedEmployee.Manager != LastLoggedIn.Name)
                {
                    var errorMsg = new MessageDialog("You can only change data for your own employees.", "Error");
                    errorMsg.ShowAsync();
                    return;
                }
            }

            try
            {
                //The following needs parsing due to the fact that the TextBox controls are bound to string values.

                if (!String.IsNullOrWhiteSpace(_viewModel.AdminChangeSalaryNumberBox))
                {
                    SelectedEmployee.SalaryNumber = int.Parse(_viewModel.AdminChangeSalaryNumberBox);
                }

                if (!String.IsNullOrWhiteSpace(_viewModel.AdminChangeVacationDaysBox))
                {
                    SelectedEmployee.VacationDays = int.Parse(_viewModel.AdminChangeVacationDaysBox);
                }

                if (!String.IsNullOrWhiteSpace(_viewModel.AdminChangeSickDaysBox))
                {
                    SelectedEmployee.SickDays = int.Parse(_viewModel.AdminChangeSickDaysBox);
                }

                if (!String.IsNullOrWhiteSpace(_viewModel.AdminChangeWorkedDaysBox))
                {
                    SelectedEmployee.WorkedDays = int.Parse(_viewModel.AdminChangeWorkedDaysBox);
                }

                if (MainViewModel.OpenDbConnection == false)
                {
                    return;
                }

                PersistencyService.PutData(SelectedEmployee);
                PersistencyService.GetData(_viewModel.EmployeesInDb);
                if (admin.AdminLvl != 2)
                {
                    OwnDepartmentList();
                }
                else
                {
                    PersistencyService.GetData(_viewModel.DatabaseTable);
                }
            }
            catch (FormatException)
            {
                var errorMsg = new MessageDialog("Only integer values are allowed.", "Error");
                if (MainViewModel.OpenDbConnection)
                {
                    errorMsg.ShowAsync();
                }
            }
        }
 /// <summary>
 /// Returns the complete visual employeelist for the admin.
 /// </summary>
 public void CompleteEmployeeList()
 {
     PersistencyService.GetData(_viewModel.DatabaseTable);
 }