// GET: ListEmployeeAccounts public IActionResult ListEmployeeAccounts() { var employeeAccountViewModel = new EmployeeAccountsViewModel() { EmployeeAccounts = _context.EmployeeAccount .Include(e => e.AccountStatus) .Include(e => e.Employee) .ToList() }; return(View(employeeAccountViewModel)); }
public ActionResult AddEmployee([Bind(Include = "EmployeeID,Name,Surname,IDNumber,DateHired,ContactNum,KinContactNum,Email,FarmID,PositionID,GroupID,UserName,Password,ConfirmPassword,Type")] EmployeeAccountsViewModel employeeAccounts) { // employeeAccounts.Type = "Employee"; if (ModelState.IsValid) { try { // add employee account to account table var varBefore = db.Accounts.Select(x => x.AccountID).Count(); int before = Convert.ToInt16(varBefore); Account account = new Account(); account.UserName = employeeAccounts.UserName; account.Password = employeeAccounts.Password; account.ConfirmPassword = employeeAccounts.ConfirmPassword; db.Accounts.Add(account); db.SaveChanges(); //Acc ID var accFind = db.Accounts.Where(x => x.UserName == account.UserName).Select(y => y.AccountID).Single(); int accId = Convert.ToInt16(accFind); // add employee details to employee table var varAfter = db.Accounts.Select(x => x.AccountID).Count(); int after = Convert.ToInt16(varAfter); var beforeEmployeeIsAdded = db.Employees.Select(x => x.EmployeeID).Count(); if (before < after) { int positionId = employeeAccounts.PositionID; int farmId = employeeAccounts.FarmID; int groupId = employeeAccounts.GroupID; Employee employees = new Employee(); //employees.Account = new Account(); //employees.Position = new Position(); employees.EmployeeID = employeeAccounts.EmployeeID; employees.Email = employeeAccounts.Email; employees.Name = employeeAccounts.Name; employees.Surname = employeeAccounts.Surname; employees.AccountID = accId; employees.KinContactNum = employeeAccounts.KinContactNum; employees.IDNumber = employeeAccounts.IDNumber; employees.ContactNum = employeeAccounts.ContactNum; employees.DateHired = employeeAccounts.DateHired; DateTime hired = employeeAccounts.DateHired; employees.PositionID = positionId; employees.FarmID = farmId; employees.GroupID = groupId; // employees.Account.UserName = employeeAccounts.UserName; db.Employees.Add(employees); db.SaveChanges(); var checkEmp = db.Employees.Select(x => x.EmployeeID).Count(); if (beforeEmployeeIsAdded < checkEmp) { EmpPos employeePosition = new EmpPos(); employeePosition.EmployeeID = employees.EmployeeID; employeePosition.Started = hired; employeePosition.PositionID = positionId; employeePosition.Ended = null; db.EmpPos.Add(employeePosition); db.SaveChanges(); return(RedirectToAction("Index")); } } } catch (Exception ex) { // undo account commit if employee transaction fails var acc = db.Accounts.Where(x => x.UserName == employeeAccounts.UserName).Select(y => y.AccountID).Single(); int ac = Convert.ToInt16(acc); AccountsController emp = new AccountsController(); emp.DeleteConfirmed(ac); ViewBag.FarmID = new SelectList(db.Farms, "FarmID", "FarmName", employeeAccounts.FarmID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", employeeAccounts.PositionID); ViewBag.GroupID = new SelectList(db.Groups, "GroupID", "GName", employeeAccounts.GroupID); ViewBag.fatalError = ex.ToString(); return(View(employeeAccounts)); } } ViewBag.FarmID = new SelectList(db.Farms, "FarmID", "FarmName", employeeAccounts.FarmID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", employeeAccounts.PositionID); ViewBag.GroupID = new SelectList(db.Groups, "GroupID", "GName", employeeAccounts.GroupID); return(View(employeeAccounts)); }