public async Task <ActionResult> CreateByLeaveType(int leaveType) { var currentEmployee = await GetCurrentEmployeeAsync(); if (!await _UserManager.IsCompanyPrivelegedUser(currentEmployee)) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation"]); Forbid(); } var leaveTypeObj = await _UnitOfWork.LeaveTypes.FindAsync(x => x.Id == leaveType); if (currentEmployee.CompanyId != leaveTypeObj.CompanyId) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation because it belongs to other company"]); return(Forbid()); } LeaveAllocationEditionViewModel allocationEditionViewModel = await InitializeLeaveAllocationEditionViewModel( leaveType : leaveTypeObj, includeEmployeesDictionary : true, includeLeaveTypesDictionary : true, allowEditPeriod : true); SetEditViewProperties(allowEditPeriod: true, allowEditEmployee: true); ViewBag.Action = nameof(CreateNewByLeaveType); return(View(CreateLeaveAllocationView, allocationEditionViewModel)); }
public async Task <ActionResult> CreateNewByEmployee(LeaveAllocationEditionViewModel leaveAllocationViewModel) { #region Verification of access var currentEmployee = await _UnitOfWork.Employees.GetEmployeeAsync(User); if (currentEmployee == null || !await _UserManager.IsCompanyPrivelegedUser(currentEmployee)) { ModelState.AddModelError("", _Localizer["You not authorized to allocate leave to employees"]); return(Forbid()); } #endregion #region Verification of integrity if (!ModelState.IsValid) { return(View(CreateLeaveAllocationView, leaveAllocationViewModel)); } var concernedEmployee = await _UnitOfWork.Employees.FindAsync(x => x.Id.Equals(leaveAllocationViewModel.AllocationEmployeeId)); var concernedLeaveType = await _UnitOfWork.LeaveTypes.FindAsync(x => x.Id.Equals(leaveAllocationViewModel.AllocationLeaveTypeId)); bool companyIsIntegral = concernedEmployee.CompanyId == concernedLeaveType.CompanyId && concernedLeaveType.CompanyId == currentEmployee.CompanyId; if (!companyIsIntegral) { ModelState.AddModelError("", _Localizer["Company not matches for all the actors"]); _Logger.LogError("There is mismatch in data: currentUser.companyId must match Company Ids of concerned user and leave type", new object[] { $"CurrentUser.CompanyId: {currentEmployee.CompanyId}", $"ConcernedEmployee.CompanyId: {concernedEmployee.CompanyId}", $"LeaveType.CompanyId: {concernedLeaveType.CompanyId}", }); return(BadRequest()); } int period = leaveAllocationViewModel.Period; #endregion var newLeaveAllocation = new LeaveAllocation() { AllocationEmployeeId = leaveAllocationViewModel.AllocationEmployeeId, AllocationLeaveTypeId = leaveAllocationViewModel.AllocationLeaveTypeId, DateCreated = leaveAllocationViewModel.DateCreated, NumberOfDays = leaveAllocationViewModel.NumberOfDays, Period = period, }; bool succeed = await _UnitOfWork.LeaveAllocations.CreateAsync(newLeaveAllocation); succeed &= await _UnitOfWork.Save(); if (succeed) { return(await UserLeaveAllocationsForPeriod(userId : leaveAllocationViewModel.AllocationEmployeeId, period : leaveAllocationViewModel.Period.ToString())); } else { var donnor = await InitializeLeaveAllocationEditionViewModel(includeEmployeesDictionary : true, includeLeaveTypesDictionary : true); leaveAllocationViewModel.Employees = donnor.Employees; leaveAllocationViewModel.AllocationLeaveTypes = donnor.AllocationLeaveTypes; return(View(leaveAllocationViewModel)); } }
public async Task <ActionResult> Edit(int id, LeaveAllocationEditionViewModel editionViewModel) { if (!await _UserManager.IsCompanyPrivelegedUser(User)) { return(Forbid()); } LeaveAllocation originalLeaveAllocation = await _UnitOfWork.LeaveAllocations.FindAsync(x => x.Id == id, includes : new System.Linq.Expressions.Expression <Func <LeaveAllocation, object> >[] { x => x.AllocationLeaveType, x => x.AllocationEmployee }); try { if (originalLeaveAllocation == null) { return(NotFound()); } #region Validation operation var newLeaveType = await _UnitOfWork.LeaveTypes.FindAsync(x => x.Id == editionViewModel.AllocationLeaveTypeId, includes : new System.Linq.Expressions.Expression <Func <LeaveType, object> >[] { x => x.Company }); #endregion LeaveAllocation newLeaveAllocation = _Mapper.Map <LeaveAllocation>(editionViewModel); await WriteHistory(newLeaveAllocation, originalLeaveAllocation); originalLeaveAllocation.AllocationEmployeeId = newLeaveAllocation.AllocationEmployeeId; originalLeaveAllocation.AllocationLeaveTypeId = newLeaveAllocation.AllocationLeaveTypeId; originalLeaveAllocation.Period = newLeaveAllocation.Period; originalLeaveAllocation.NumberOfDays = newLeaveAllocation.NumberOfDays; bool succeed = await _UnitOfWork.LeaveAllocations.UpdateAsync(originalLeaveAllocation); succeed &= await _UnitOfWork.Save(); if (succeed) { return(await UserLeaveAllocationsForPeriod(editionViewModel.AllocationEmployeeId, period : editionViewModel.Period.ToString())); } else { if (newLeaveType?.CompanyId == null) { return(BadRequest()); } int companyId = newLeaveType.CompanyId; var lists = await GetListLeaveTypesAndEmployees(companyId, newLeaveAllocation.AllocationEmployeeId, newLeaveAllocation.AllocationLeaveTypeId); editionViewModel.AllocationLeaveTypes = lists.Item1; editionViewModel.Employees = lists.Item2; ViewBag.Action = nameof(Edit); return(View(CreateLeaveAllocationView, editionViewModel)); } } catch (AggregateException e) { var messages = new StringBuilder(); e.Flatten().InnerExceptions.Select(x => { messages.Append(x.Message); return(0); }); HomeController.DisplayProblem(_Logger, this, _Localizer["Problem while getting leave allocation"], messages.ToString()); return(Redirect(Request.Headers["Referer"].ToString())); } finally { ; } }
public async Task <ActionResult> CreateNewByLeaveType(LeaveAllocationEditionViewModel leave) { var currentEmployee = await _UnitOfWork.Employees.GetEmployeeAsync(User); if (!await _UserManager.IsCompanyPrivelegedUser(User)) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation"]); return(Forbid()); } if (!ModelState.IsValid) { return(View(leave)); } var leaveTypeObj = await _UnitOfWork.LeaveTypes.FindAsync(x => x.Id == leave.AllocationLeaveTypeId); var consernedEmployee = await _UnitOfWork.Employees.FindAsync(x => x.Id.Equals(leave.AllocationEmployeeId)); if (currentEmployee.CompanyId != consernedEmployee.CompanyId) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation because you allocate leave to employee from other company"]); return(Forbid()); } if (leaveTypeObj.CompanyId != consernedEmployee.CompanyId) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation because it belongs to other company"]); return(Unauthorized()); } int period = leave.Period; var newLeaveAllocation = new LeaveAllocation() { AllocationEmployeeId = leave.AllocationEmployeeId, AllocationLeaveTypeId = leave.AllocationLeaveTypeId, DateCreated = leave.DateCreated, NumberOfDays = leave.NumberOfDays, Period = period, }; bool succeed = await _UnitOfWork.LeaveAllocations.CreateAsync(newLeaveAllocation); succeed &= await _UnitOfWork.Save(); if (succeed) { return(await UserLeaveAllocationsForPeriod(userId : leave.AllocationEmployeeId, period : leave.Period.ToString()));; } else { leave = await InitializeLeaveAllocationEditionViewModel( employee : consernedEmployee, leaveType : leaveTypeObj, period : leave.Period, includeEmployeesDictionary : true ); SetEditViewProperties(allowEditPeriod: true, allowEditEmployee: true, allowEditLeaveType: false); return(View(leave)); } }
/// <summary> /// Returns edition view model, defined by arguments /// </summary> /// <param name="id">Id of leave allocation. Null means that it is creation</param> /// <param name="employee">Employee. If null, it means current employee</param> /// <param name="leaveType">Leave type. Null means you must include the dictionary of leaveTypes</param> /// <param name="period">Period of leave. Ignored, if Id specified. Otherwise, in the case of creation if new leave allocation, specified period. /// In case if it is unspecified, period is current year</param> /// <param name="duration">Duration of leave. Ignored if id specified. If not, takes all avalable leave for this employee and leave type</param> /// <param name="includeEmployeesDictionary">Force include of employees dictionary to viewModel (Admin, HR specialist)</param> /// <param name="includeLeaveTypesDictionary">Force include leaveTypes dictionary to viewModel </param> /// <returns></returns> public async Task <LeaveAllocationEditionViewModel> InitializeLeaveAllocationEditionViewModel(long?id = null, Employee employee = null, LeaveType leaveType = null, int?period = null, int?duration = null, bool includeEmployeesDictionary = false, bool includeLeaveTypesDictionary = false, bool allowEditPeriod = false, bool allowEditLeaveType = false, bool allowEditEmployee = false) { int effectivePeriod = DateTime.Now.Year; if (period != null) { effectivePeriod = (int)period; } return(await Task.Factory.StartNew(() => { Employee currentEmployee = GetCurrentEmployeeAsync().Result; LeaveAllocationEditionViewModel leaveAllocationVM = null; if (id == null) { leaveAllocationVM = new LeaveAllocationEditionViewModel() { DateCreated = DateTime.Now, NumberOfDays = 0 }; if (leaveType == null) { includeLeaveTypesDictionary = true; } else { leaveAllocationVM.AllocationLeaveTypeId = leaveType.Id; leaveAllocationVM.AllocationLeaveType = _Mapper.Map <LeaveTypeNavigationViewModel>(leaveType); } if (employee == null) { employee = GetCurrentEmployeeAsync().Result; } leaveAllocationVM.AllocationEmployee = _Mapper.Map <EmployeePresentationDefaultViewModel>(employee); leaveAllocationVM.AllocationEmployeeId = leaveAllocationVM.AllocationEmployee.Id; if (duration == null && leaveType != null) { leaveAllocationVM.NumberOfDays = leaveType.DefaultDays; } leaveAllocationVM.Period = effectivePeriod; } else { leaveAllocationVM = _Mapper.Map <LeaveAllocationEditionViewModel>(_UnitOfWork.LeaveAllocations.FindAsync(x => x.Id == (long)id, includes: new Expression <Func <LeaveAllocation, object> >[] { x => x.AllocationEmployee, x => x.AllocationLeaveType }).Result); } includeEmployeesDictionary &= _UserManager.IsCompanyPrivelegedUser(User).Result; if (includeEmployeesDictionary) { leaveAllocationVM.Employees = _UnitOfWork.Employees.WhereAsync(filter: x => currentEmployee != null && currentEmployee.CompanyId != null && x.CompanyId == currentEmployee.CompanyId, includes: new Expression <Func <Employee, object> >[] { e => e.Company, e => e.Manager }) .Result .Select(x => new SelectListItem(x.FormatEmployeeSNT(), x.Id, leaveAllocationVM.AllocationEmployeeId?.Equals(x.Id) ?? false)); } if (includeLeaveTypesDictionary) { leaveAllocationVM.AllocationLeaveTypes = _UnitOfWork.LeaveTypes.WhereAsync( filter: lt => currentEmployee != null && currentEmployee.CompanyId != null && lt.CompanyId == currentEmployee.CompanyId, includes: new Expression <Func <LeaveType, object> >[] { e => e.Company }) .Result .Select(x => new SelectListItem(x.LeaveTypeName, x.Id.ToString(), leaveAllocationVM.AllocationLeaveTypeId.Equals(x.Id))); } SetEditViewProperties(allowEditPeriod, allowEditLeaveType, allowEditEmployee); return leaveAllocationVM; })); }