public static tblPosition AddPosition(tblPosition position)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             if (position.positionId == 0)
             {
                 //add
                 tblPosition newPosition = new tblPosition();
                 newPosition.name        = position.name;
                 newPosition.description = position.description;
                 context.tblPositions.Add(newPosition);
                 context.SaveChanges();
                 position.positionId = newPosition.positionId;
             }
             return(position);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }
 public static void DeleteSector(tblSector sector)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             tblSector sectorToDelete = (from u in context.tblSectors where u.sectorId == sector.sectorId select u).First();
             //check if some employee uses this sector. Get list of all employees with this sectorId
             List <tblEmployee> list = UsedSector(sector);
             //if list is not empty, update users with default sector
             if (list.Count > 0)
             {
                 tblEmployee employeeToEdit;
                 for (int i = 0; i < list.Count; i++)
                 {
                     employeeToEdit          = list[i];
                     employeeToEdit.sectorId = 1;
                     AddEmployee(employeeToEdit);
                 }
             }
             context.tblSectors.Remove(sectorToDelete);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }
 public static tblSector AddSector(tblSector sector)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             if (sector.sectorId == 0)
             {
                 //add
                 tblSector newSector = new tblSector();
                 newSector.name        = sector.name;
                 newSector.description = sector.description;
                 context.tblSectors.Add(newSector);
                 context.SaveChanges();
                 sector.sectorId = newSector.sectorId;
             }
             return(sector);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }
 public static tblEmployee AddEmployee(tblEmployee employee)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             if (employee.employeeId == 0)
             {
                 //add
                 tblEmployee newEmployee = new tblEmployee();
                 newEmployee.qualificationsId = employee.qualificationsId;
                 newEmployee.sectorId         = employee.sectorId;
                 if (employee.positionID != 0)
                 {
                     newEmployee.positionID = employee.positionID;
                 }
                 newEmployee.yearsOfService = employee.yearsOfService;
                 newEmployee.managerId      = employee.managerId;
                 newEmployee.userID         = employee.userID;
                 context.tblEmployees.Add(newEmployee);
                 context.SaveChanges();
                 employee.employeeId = newEmployee.employeeId;
                 return(employee);
             }
             else
             {
                 tblEmployee employeeToEdit = (from x in context.tblEmployees where x.employeeId == employee.employeeId select x).FirstOrDefault();
                 employeeToEdit.Salary           = employee.Salary;
                 employeeToEdit.positionID       = employee.positionID;
                 employeeToEdit.sectorId         = employee.sectorId;
                 employeeToEdit.managerId        = employee.managerId;
                 employeeToEdit.yearsOfService   = employee.yearsOfService;
                 employeeToEdit.qualificationsId = employee.qualificationsId;
                 employeeToEdit.userID           = employee.userID;
                 context.SaveChanges();
                 return(employee);
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }
 public static tblUser AddUser(tblUser user)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             if (user.userId == 0)
             {
                 //add
                 tblUser newUser = new tblUser();
                 newUser.username         = user.username;
                 newUser.password         = user.password;
                 newUser.jmbg             = user.jmbg;
                 newUser.genderId         = user.genderId;
                 newUser.residence        = user.residence;
                 newUser.marriageStatusId = user.marriageStatusId;
                 newUser.firstname        = user.firstname;
                 newUser.lastname         = user.lastname;
                 context.tblUsers.Add(newUser);
                 context.SaveChanges();
                 user.userId = newUser.userId;
                 return(user);
             }
             else
             {
                 tblUser userToEdit = (from x in context.tblUsers where x.userId == user.userId select x).FirstOrDefault();
                 userToEdit.username         = user.username;
                 userToEdit.lastname         = user.lastname;
                 userToEdit.firstname        = user.username;
                 userToEdit.residence        = user.residence;
                 userToEdit.password         = user.password;
                 userToEdit.marriageStatusId = user.marriageStatusId;
                 userToEdit.genderId         = user.genderId;
                 userToEdit.jmbg             = user.jmbg;
                 context.SaveChanges();
                 return(user);
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }
 public static tblManager AddManager(tblManager manager)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             if (manager.managerId == 0)
             {
                 //add
                 tblManager newManager = new tblManager();
                 newManager.officeNumber       = manager.officeNumber;
                 newManager.successfulProjects = manager.successfulProjects;
                 newManager.email          = manager.email;
                 newManager.backupPassword = manager.backupPassword;
                 newManager.userId         = manager.userId;
                 context.tblManagers.Add(newManager);
                 context.SaveChanges();
                 manager.managerId = newManager.managerId;
                 return(manager);
             }
             else
             {
                 tblManager managerToEdit = (from x in context.tblManagers where  x.managerId == manager.managerId select x).FirstOrDefault();
                 managerToEdit.officeNumber          = manager.officeNumber;
                 managerToEdit.levelOfResponsibility = manager.levelOfResponsibility;
                 managerToEdit.salary             = manager.salary;
                 managerToEdit.successfulProjects = manager.successfulProjects;
                 managerToEdit.backupPassword     = manager.backupPassword;
                 managerToEdit.email  = manager.email;
                 managerToEdit.userId = manager.userId;
                 context.SaveChanges();
                 return(manager);
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }
 public static tblAdministrator AddAdministrator(tblAdministrator admin)
 {
     try
     {
         using (dbFirmEntities context = new dbFirmEntities())
         {
             if (admin.administratorId == 0)
             {
                 //add
                 tblAdministrator newAdmin = new tblAdministrator();
                 newAdmin.adminTypeId = admin.adminTypeId;
                 //exipry date +7days
                 DateTime d = DateTime.Now;
                 d = d.AddDays(7);
                 newAdmin.expiryDate = d.Date;
                 newAdmin.userId     = admin.userId;
                 context.tblAdministrators.Add(newAdmin);
                 context.SaveChanges();
                 admin.administratorId = newAdmin.administratorId;
                 return(admin);
             }
             else
             {
                 tblAdministrator adminToEdit = (from x in context.tblAdministrators where x.administratorId == admin.administratorId select x).FirstOrDefault();
                 adminToEdit.expiryDate  = admin.expiryDate;
                 adminToEdit.adminTypeId = admin.adminTypeId;
                 adminToEdit.userId      = admin.userId;
                 context.SaveChanges();
                 return(admin);
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }