private void SaveExecute() { try { if (!ValidationClass.JMBGisValid(employee.JMBG)) { MessageBox.Show("JMBG is not valid."); return; } if (!ValidationClass.JMBGIsUnique(employee.JMBG)) { MessageBox.Show("JMBG already exists in database"); return; } if (!ValidationClass.RegisterNumberIsValid(employee.IDNumber)) { MessageBox.Show("Registration number is not valid. It must have 9 numbers"); return; } if (!ValidationClass.RegNumberIsUnique(employee.IDNumber)) { MessageBox.Show("Registration number already exists in database"); return; } if (!ValidationClass.TelfonNumberValid(employee.PhoneNumber)) { MessageBox.Show("Telephone number is not valid. It must have 9 numbers"); return; } employee.LocationID = SelctedLocation.ID; employee.DateOfBirth = StartDate; employee.ManagerId = SelectedMenager.ID; if (Gender.Equals("male")) { employee.Gender = "M"; } else if (Gender.Equals("female")) { employee.Gender = "F"; } else { employee.Gender = "O"; } tblSector sectorDB = sectorService.GetSectorByName(Sector); string textForFile = String.Format("Added user {0} {1} JMBG {2}", employee.FirstName, employee.LastName, employee.JMBG); eventObject.OnActionPerformed(textForFile); tblEmployee emplInDb = employeeService.AddEmployee(employee); if (sectorDB == null) { sectorDB = new tblSector(); sectorDB.SectorName = Sector; sectorDB = sectorService.AddSector(sectorDB); //employee.SectorID = sectorDB.SectorID; employeeService.EditSector(emplInDb.EmployeeID, sectorDB); } else { employeeService.EditSector(emplInDb.EmployeeID, sectorDB); } employeeService.EditManager(emplInDb.EmployeeID, SelectedMenager.ID); addEmployee.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void SaveExecute() { try { if (!ValidationClass.JMBGisValid(employee.JMBG)) { MessageBox.Show("JMBG is not valid."); return; } if (!ValidationClass.RegisterNumberIsValid(employee.IDNumber)) { MessageBox.Show("Registration number is not valid"); return; } if (!ValidationClass.TelfonNumberValid(employee.PhoneNumber)) { MessageBox.Show("Telefon number is not valid. It must have 9 numbers"); return; } employee.DateOfBirth = StartDate; Employee.LocationID = SelctedLocation.ID; if (SelectedMenager != null) { employee.ManagerId = SelectedMenager.ID; } if (Gender.Equals("male")) { employee.Gender = "M"; } else if (Gender.Equals("female")) { employee.Gender = "F"; } else { employee.Gender = "O"; } tblSector sectorDB = sectorService.GetSectorByName(Sector); employeeService.EditEmployee(Employee); if (sectorDB == null) { sectorDB = new tblSector(); sectorDB.SectorName = Sector; sectorDB = sectorService.AddSector(sectorDB); employeeService.EditSector(Employee.EmployeeID, sectorDB); } else { //some comment employeeService.EditSector(Employee.EmployeeID, sectorDB); } if (Employee.ManagerId != null) { if (SelectedMenager != null) { employeeService.EditManager((int)Employee.EmployeeID, SelectedMenager.ID); } else { employeeService.EditManager((int)Employee.EmployeeID, 0); } } string textForFile = String.Format("Updated user {0} {1} JMBG {2} to {3} {4} JMBG {5}", oldEmployee.FirstName, oldEmployee.LastName, oldEmployee.JMBG, employee.FirstName, employee.LastName, employee.JMBG); eventObject.OnActionPerformed(textForFile); editEmployee.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void SaveExecute() { try { if (!ValidationClass.JMBGisValid(employee.JMBG)) { MessageBox.Show("JMBG is not valid."); return; } if (!ValidationClass.JMBGIsUniqueForEditWindow(employee.JMBG, employee)) { MessageBox.Show("JMBG already exists in database"); return; } if (!ValidationClass.RegisterNumberIsValid(employee.RegistrationNumber)) { MessageBox.Show("Registration number is not valid"); return; } if (!ValidationClass.RegNumberIsUniqueForEdit(employee.RegistrationNumber, employee)) { MessageBox.Show("Registration number already exists in database"); return; } if (!ValidationClass.TelfonNumberValid(employee.TelefonNumber)) { MessageBox.Show("Telefon number is not valid. It must have 9 numbers"); return; } employee.LocationID = SelctedLocation.LocationID; employee.DateOfBirth = StartDate; if (String.IsNullOrEmpty(selectedMenager.Menager)) { vwMenager menagerDB = employeeService.GetMenagerByName(" "); employee.MenagerID = menagerDB.EmployeeID; } else { employee.MenagerID = selectedMenager.EmployeeID; } string genderForDB; if (Gender.Equals("male")) { genderForDB = "M"; } else if (Gender.Equals("female")) { genderForDB = "F"; } else { genderForDB = "O"; } tblGender gender = genderService.GetGenderByName(genderForDB); tblSector sectorDB = sectorService.GetSectorByName(Sector); if (sectorDB == null) { sectorDB = new tblSector(); sectorDB.SectorName = Sector; sectorDB = sectorService.AddSector(sectorDB); employee.SectorID = sectorDB.SectorID; } else { employee.SectorID = sectorDB.SectorID; } employee.GenderID = gender.GenderID; isUpdateUser = true; string textForFile = String.Format("Updated user {0} {1} JMBG {2} to {3} {4} JMBG {5}", oldEmployee.FirstName, oldEmployee.LastName, oldEmployee.JMBG, employee.FirstName, employee.LastName, employee.JMBG); eventObject.OnActionPerformed(textForFile); employeeService.EditEmployee(employee); editEmployee.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// Saves created Worker to the database /// </summary> private void SaveExecute() { tblLocation location = new tblLocation(); string[] fullLocation = Location.Split(','); string address = fullLocation[0]; try { using (WorkersDbEntities db = new WorkersDbEntities()) { location = db.tblLocations.Where(x => x.Address == address).FirstOrDefault(); Worker.FKLocation = location.Id; Worker.Gender = Gender; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } tblSector sector = new tblSector(); sector.Sector = Sector; List <tblSector> sectors = new List <tblSector>(); try { using (WorkersDbEntities db = new WorkersDbEntities()) { foreach (tblSector dbSector in db.tblSectors) { sectors.Add(dbSector); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } if (sectors.Count == 0) { try { using (WorkersDbEntities db = new WorkersDbEntities()) { db.tblSectors.Add(sector); db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } try { using (WorkersDbEntities db = new WorkersDbEntities()) { tblSector sector1 = db.tblSectors.Where(x => x.Sector == sector.Sector).FirstOrDefault(); Worker.FKSector = sector1.Id; db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } } else { for (int i = 0; i < sectors.Count; i++) { if (sectors[i].Sector == sector.Sector) { Worker.FKSector = sectors[i].Id; } else if (i == sectors.Count - 1 && sectors[i].Sector != sector.Sector) { try { using (WorkersDbEntities db = new WorkersDbEntities()) { db.tblSectors.Add(sector); db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } try { using (WorkersDbEntities db = new WorkersDbEntities()) { tblSector sector1 = db.tblSectors.Where(x => x.Sector == sector.Sector).FirstOrDefault(); Worker.FKSector = sector1.Id; db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } } else { continue; } } } SaveWorker(); }
/// <summary> /// Creates or edits an employee /// </summary> /// <param name="employee">the employee that is being added</param> /// <returns>a new or edited employee</returns> public vwEmployee AddEmployee(vwEmployee employee) { try { using (CompanyDBEntities context = new CompanyDBEntities()) { if (employee.UserID == 0) { tblUser newUser = new tblUser { FirstName = employee.FirstName, LastName = employee.LastName, JMBG = employee.JMBG, Gender = employee.Gender, Residence = employee.Residence, MarriageStatus = employee.MarriageStatus, Username = employee.Username, UserPassword = PasswordHasher.Hash(employee.UserPassword) }; context.tblUsers.Add(newUser); context.SaveChanges(); employee.UserID = newUser.UserID; //TODO remove when method to create sectors is made if (employee.SectorID == 0) { if (GetAllSectors().Count == 0) { tblSector sec = new tblSector { SectorName = "Default", SectorDescription = "Default" }; AddSector(sec); employee.SectorID = sec.SectorID; } else { employee.SectorID = GetAllSectors()[0].SectorID; } } tblEmployee newEmployee = new tblEmployee { YearsOfService = employee.YearsOfService, Salary = employee.Salary, EducationDegree = employee.EducationDegree, SectorID = employee.SectorID, ManagerID = employee.ManagerID, PositionID = employee.PositionID, UserID = employee.UserID }; context.tblEmployees.Add(newEmployee); context.SaveChanges(); employee.EmployeeID = newEmployee.EmployeeID; return(employee); } else { tblUser userToEdit = (from ss in context.tblUsers where ss.UserID == employee.UserID select ss).First(); userToEdit.FirstName = employee.FirstName; userToEdit.LastName = employee.LastName; userToEdit.JMBG = employee.JMBG; userToEdit.Gender = employee.Gender; userToEdit.Residence = employee.Residence; userToEdit.MarriageStatus = employee.MarriageStatus; userToEdit.Username = employee.Username; // Save only if password changed if (userToEdit.UserPassword != employee.UserPassword) { userToEdit.UserPassword = PasswordHasher.Hash(employee.UserPassword); } userToEdit.UserID = employee.UserID; tblUser userEdit = (from ss in context.tblUsers where ss.UserID == employee.UserID select ss).First(); context.SaveChanges(); tblEmployee employeeToEdit = (from ss in context.tblEmployees where ss.UserID == employee.UserID select ss).First(); employeeToEdit.YearsOfService = employee.YearsOfService; employeeToEdit.Salary = employee.Salary; employeeToEdit.EducationDegree = employee.EducationDegree; employeeToEdit.SectorID = employee.SectorID; employeeToEdit.ManagerID = employee.ManagerID; employeeToEdit.PositionID = employee.PositionID; employeeToEdit.UserID = employee.UserID; employeeToEdit.EmployeeID = employee.EmployeeID; tblEmployee employeeEdit = (from ss in context.tblEmployees where ss.EmployeeID == employee.EmployeeID select ss).First(); context.SaveChanges(); return(employee); } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
public AddSectorViewModel(AddSector open) { addSector = open; sector = new tblSector(); }
private void SaveExecute() { try { tblEmploye newEmploye = new tblEmploye(); newEmploye.UserName = Employe.UserName; newEmploye.Surname = Employe.Surname; newEmploye.IdNumber = Employe.IdNumber; newEmploye.Number = Employe.Number; newEmploye.JMBG = Employe.JMBG; newEmploye.DateOfBirth = CalculateBirth(Employe.JMBG); //for sector string newSector = Sector.SectorName; tblSector newSectorObject = new tblSector(); newSectorObject.SectorName = newSector; List <tblSector> sectorList = context.tblSectors.ToList(); List <string> sectorNames = new List <string>(); for (int i = 0; i < sectorList.Count; i++) { sectorNames.Add(sectorList[i].SectorName); } if (sectorNames.Contains(newSectorObject.SectorName)) { tblSector sectorToFind = new tblSector(); sectorToFind = (from r in context.tblSectors where r.SectorName == newSectorObject.SectorName select r).First(); newEmploye.SectorID = sectorToFind.SectorID; context.SaveChanges(); } else { context.tblSectors.Add(newSectorObject); context.SaveChanges(); newEmploye.SectorID = newSectorObject.SectorID; context.SaveChanges(); } newEmploye.GenderID = Gender.GenderID; string adress = Location.Adress; if (Location.Adress == "Adresa1") { Location.LocationID = 1; } if (Location.Adress == "Adresa2") { Location.LocationID = 1; } if (Location.Adress == "Adresa3") { Location.LocationID = 3; } if (Location.Adress == "Adresa4") { Location.LocationID = 4; } if (Location.Adress == "Adresa5") { Location.LocationID = 5; } newEmploye.LocationID = Location.LocationID; context.tblEmployes.Add(newEmploye); context.SaveChanges(); addEmploye.Close(); } catch (Exception ex) { MessageBox.Show("Error occured. Make sure that you have provided valid JMBG. Please fix the problems and try again." + ex.ToString()); } }