public bool Approve(int leaveId, int approverId, string approverComments, bool autoApproved) { var log = new StringBuilder(); var leave = _leaveRepository.GetBy(l => l.Id == leaveId, "RequestedForUser.User.Person"); if (leave != null) { // Calculate the number of days between these days float leaveCount = 0; var counter = leave.Start; while (counter <= leave.End) { var result = ShouldDeduct(counter); if (result.Key) { leaveCount = leaveCount + 1; log.AppendLine($"{counter.ToShortDateString()} - {result.Key} - {result.Value}"); } else { log.AppendLine($"{counter.ToShortDateString()} - {result.Key} - {result.Value}"); } counter = counter.AddDays(1); } // Short Circuit - Deduct only half for half days if (leave.Duration == LeaveDuration.FirstHalf || leave.Duration == LeaveDuration.SecondHalf) { leaveCount = 0.5f; } // Check for Leave Balance var leavePeriod = _leaveTimePeriodRepository.GetBy(i => i.Start <= DateTime.UtcNow && i.End >= DateTime.UtcNow); var leaveEntitlement = _leaveEntitlementRepository.GetBy(l => l.LeaveTypeId == leave.LeaveTypeId && l.EmployeeId == leave.RequestedForUserId && l.LeaveTimePeriodId == leavePeriod.Id, "LeaveType"); //var leaveEntitlement = _leaveEntitlementRepository.GetBy(l => l.LeaveTypeId == leave.LeaveTypeId && l.EmployeeId == leave.RequestedForUserId, "LeaveType"); if (leaveEntitlement != null) { log.AppendLine($"Going to deduct {leaveCount} from {leaveEntitlement.Allocation} leaves of Type {leaveEntitlement.LeaveType.Title}"); if (leaveCount <= leaveEntitlement.Allocation) { //Get current var currentLeaveCount = leaveEntitlement.Allocation; // Deduct only half for half days leaveEntitlement.Allocation = leaveEntitlement.Allocation - leaveCount; _leaveEntitlementRepository.Update(leaveEntitlement); _unitOfWork.Commit(); // New Balance log.AppendLine($"New Leave Balance is {leaveEntitlement.Allocation}"); // Approve the Leave leave.Status = LeaveStatus.Approved; leave.Count = leaveCount; leave.CalculationLog = log.ToString(); leave.ApproverId = approverId; leave.ApproverComments = approverComments; leave.ActedOn = DateTime.UtcNow; _leaveRepository.Update(leave); _unitOfWork.Commit(); var leaveApproverId = _employeeRepository.GetBy(i => i.Id == leave.ApproverId); var getUser = leaveApproverId.UserId; var comments = "Deducting leave as leave got approved"; // If auto approved, mention that. if (autoApproved) { comments = "Deducting leave as leave got auto approved"; } // Create a log var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate { EmployeeId = leave.RequestedForUserId, LeaveTimePeriodId = leaveEntitlement.LeaveTimePeriodId, LeaveTypeId = leave.LeaveTypeId, LeaveId = leave.Id, Operation = LeaveOperation.Deduct, LeaveCount = leave.Count, PreviousBalance = currentLeaveCount, NewBalance = leaveEntitlement.Allocation, Comments = comments, CreatedByUserId = getUser }; _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate); _unitOfWork.Commit(); return(true); } } } return(false); }
public JsonResult UpdateLeaveEntitlement(UpdateLeaveEntitlementViewModel vm) { var currentEntitlements = _leaveEntitlementRepository.GetBy(e => e.EmployeeId == vm.EmployeeId && e.LeaveTimePeriodId == vm.LeaveTimePeriodId && e.LeaveTypeId == vm.LeaveTypeId); if (vm.Operation == LeaveOperation.Deduct) { if (currentEntitlements != null) { if (currentEntitlements.Allocation - vm.Allocation >= 0) { // Create a log var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate { EmployeeId = vm.EmployeeId, LeaveTimePeriodId = vm.LeaveTimePeriodId, LeaveTypeId = vm.LeaveTypeId, Operation = vm.Operation, LeaveCount = vm.Allocation, PreviousBalance = currentEntitlements.Allocation, NewBalance = currentEntitlements.Allocation - vm.Allocation, Comments = vm.Comments, CreatedByUserId = WebUser.Id }; _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate); _unitOfWork.Commit(); // Update Entitlements currentEntitlements.Allocation = currentEntitlements.Allocation - vm.Allocation; currentEntitlements.UpdatedByUserId = WebUser.Id; _leaveEntitlementRepository.Update(currentEntitlements); _unitOfWork.Commit(); return(Json(true)); } return(Json(false)); } return(Json(false)); } else { if (currentEntitlements != null) { // Create a log var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate { EmployeeId = vm.EmployeeId, LeaveTimePeriodId = vm.LeaveTimePeriodId, LeaveTypeId = vm.LeaveTypeId, Operation = vm.Operation, LeaveCount = vm.Allocation, PreviousBalance = currentEntitlements.Allocation, NewBalance = currentEntitlements.Allocation + vm.Allocation, Comments = vm.Comments, CreatedByUserId = WebUser.Id }; _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate); _unitOfWork.Commit(); // Update Entitlements currentEntitlements.Allocation = currentEntitlements.Allocation + vm.Allocation; currentEntitlements.UpdatedByUserId = WebUser.Id; _leaveEntitlementRepository.Update(currentEntitlements); _unitOfWork.Commit(); return(Json(true)); } else { // Create a log var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate { EmployeeId = vm.EmployeeId, LeaveTimePeriodId = vm.LeaveTimePeriodId, LeaveTypeId = vm.LeaveTypeId, Operation = vm.Operation, LeaveCount = vm.Allocation, PreviousBalance = 0, NewBalance = vm.Allocation, Comments = vm.Comments, CreatedByUserId = WebUser.Id }; _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate); _unitOfWork.Commit(); // Create Entitlements var newEntitlement = new LeaveEntitlement { EmployeeId = vm.EmployeeId, LeaveTimePeriodId = vm.LeaveTimePeriodId, LeaveTypeId = vm.LeaveTypeId, Allocation = vm.Allocation, Comments = vm.Comments, CreatedByUserId = WebUser.Id }; _leaveEntitlementRepository.Create(newEntitlement); _unitOfWork.Commit(); return(Json(true)); } } }