public void Edit(DTOEmployee model)
        {
            Address  address  = _mapper.Map <Address>(model.Address);
            Employee employee = _mapper.Map <Employee>(model);

            _context.Update(address);
            _context.SaveChanges();

            EmployeeInventory employeeInventory = _context.EmployeeInventories
                                                  .Where(x => x.EmployeeId == employee.Id && x.EndDate == null)
                                                  .FirstOrDefault();

            if (employeeInventory.InventoryId == null)
            {
                employeeInventory.InventoryId = model.InventoryId;
                if (employeeInventory.InventoryId != null)
                {
                    employeeInventory.HireDate = DateTime.Now;
                }
            }
            else
            {
                if (employeeInventory.InventoryId != model.InventoryId)
                {
                    employeeInventory.EndDate = DateTime.Now;
                    EmployeeInventory ei = new EmployeeInventory
                    {
                        EmployeeId  = employee.Id,
                        InventoryId = model.InventoryId,
                        HireDate    = null
                    };
                    if (ei.InventoryId != null)
                    {
                        ei.HireDate = DateTime.Now;
                    }
                    _context.Add(ei);
                    _context.SaveChanges();
                }
            }

            EmployeeSalaries employeeSalaries = _context.EmployeeSalaries
                                                .Where(x => x.EmployeeId == employee.Id && x.EndDate == null)
                                                .FirstOrDefault();

            if (employeeSalaries.Value != model.Salary)
            {
                employeeSalaries.EndDate = DateTime.Now;
                EmployeeSalaries es = new EmployeeSalaries
                {
                    EmployeeId = employee.Id,
                    Value      = model.Salary,
                    StartDate  = DateTime.Now
                };
                _context.Add(es);
                _context.SaveChanges();
            }

            base.Edit(employee);
        }
        public Inventory GetInventory(int employeeId)
        {
            EmployeeInventory employeeInventory = _context.EmployeeInventories
                                                  .Where(x => x.EmployeeId == employeeId && x.EndDate == null)
                                                  .Include(x => x.Inventory)
                                                  .FirstOrDefault();

            if (employeeInventory == null)
            {
                return(null);
            }
            else
            {
                return(employeeInventory.Inventory);
            }
        }
        public void Add(DTOEmployee model)
        {
            Address  address  = _mapper.Map <Address>(model.Address);
            Employee employee = _mapper.Map <Employee>(model);

            _context.Addresses.Add(address);
            _context.SaveChanges();

            employee.AddressId = address.Id;
            employee.Username  = model.FirstName.ToLower() + model.LastName.Substring(0, 1).ToLower();
            int b = 0;

            foreach (var x in _context.Users.ToList())
            {
                if (x.UserName == employee.Username)
                {
                    b++;
                }
            }
            if (b > 0)
            {
                employee.Username += b.ToString();
            }
            employee.PasswordHash = "AQAAAAEAACcQAAAAEA27GXFgUsI5e3+EHt0MSqROepea6LqlbFSugDckdIrVK+MyaYbiqZABm4qUmjep+A==";
            _context.Add(employee);
            _context.SaveChanges();

            EmployeeInventory employeeInventory = new EmployeeInventory
            {
                EmployeeId  = employee.Id,
                InventoryId = model.InventoryId,
                HireDate    = null,
                EndDate     = null
            };

            if (employeeInventory.InventoryId != null)
            {
                employeeInventory.HireDate = DateTime.Now;
            }
            _context.Add(employeeInventory);
            _context.SaveChanges();

            EmployeeSalaries employeeSalary = new EmployeeSalaries
            {
                EmployeeId = employee.Id,
                Value      = model.Salary,
                StartDate  = DateTime.Now,
                EndDate    = null
            };

            _context.Add(employeeSalary);
            _context.SaveChanges();

            ApplicationUser user = new ApplicationUser
            {
                UserName           = employee.Username,
                NormalizedUserName = employee.Username.ToUpper(),
                Email                = employee.Username + "@ims.ba",
                NormalizedEmail      = (employee.Username + "@ims.ba").ToUpper(),
                EmailConfirmed       = true,
                PhoneNumber          = employee.PhoneNumber,
                PhoneNumberConfirmed = false,
                PasswordHash         = employee.PasswordHash,
                SecurityStamp        = Guid.NewGuid().ToString(),
                ConcurrencyStamp     = Guid.NewGuid().ToString()
            };

            _context.Users.Add(user);
            _context.SaveChanges();

            ApplicationUserRole userRole = new ApplicationUserRole
            {
                UserId = user.Id,
                RoleId = 2
            };

            _context.UserRoles.Add(userRole);
            _context.SaveChanges();

            Person person = new Person
            {
                Active         = true,
                CreatedDate    = DateTime.Now,
                FirstName      = employee.FirstName,
                LastName       = employee.LastName,
                GenderId       = employee.GenderId,
                PhoneNumber    = employee.PhoneNumber,
                IdentityUserId = user.Id
            };

            _context.Persons.Add(person);
            _context.SaveChanges();
        }