public async Task <IActionResult> GetProcessingPercentageJson() { try { decimal result = 100; var unCompletedRequest = await _processingsRequestsManager.GetSingleItemAsync(p => p.Completed == false); if (unCompletedRequest != null) { int daysCount = (int)unCompletedRequest.ToDate.Subtract(unCompletedRequest.FromDate).TotalDays + 1; int employeesCount = unCompletedRequest.Employees?.TryParseToNumbers()?.Count() ?? 0; if (employeesCount == 0) { var allEmployees = await _erpManager.GetShortEmployeesInfo(); employeesCount = allEmployees.Count; } int totalRecords = daysCount * employeesCount; var completedDaysReports = await _employeesDaysReportsManager .GetAll(d => d.ProcessingDate >= unCompletedRequest.RequestDate).AsNoTracking().CountAsync(); if (completedDaysReports > 0 && totalRecords > 0) { result = ((decimal)completedDaysReports / (decimal)totalRecords) * 100m; } } return(Json(new { Percentage = Math.Round(result, 2) })); } catch (Exception) { return(Json(new { Percentage = 0 })); } }
public async Task <bool> DoWork() { try { //Check if there is any open proceesing request var openProcessingRequest = await _processingsRequestsManager.GetSingleItemAsync(pr => !pr.Completed); if (openProcessingRequest != null) { //Get requset employees ids IEnumerable <decimal> listOfEmployeesIdsNumbers = openProcessingRequest.Employees.TryParseToNumbers(); List <int> employeesIds = listOfEmployeesIdsNumbers?.Select(d => (int)d).ToList() ?? new List <int>(); //Get last processed day report for required employees var lastDayReport = await _employeesDaysReportsManager .GetAll(d => (employeesIds.Count < 1 || employeesIds.Contains(d.EmployeeId)) && d.ProcessingDate > openProcessingRequest.RequestDate) .OrderByDescending(d => d.EmployeeId).ThenByDescending(d => d.DayDate).FirstOrDefaultAsync(); int lastEmployeeId = lastDayReport?.EmployeeId ?? 0; DateTime fromDate = lastDayReport?.DayDate.AddDays(1) ?? openProcessingRequest.FromDate; DateTime toDate = lastDayReport?.DayDate.AddDays(10) ?? openProcessingRequest.FromDate.AddDays(10); toDate = toDate <= openProcessingRequest.ToDate ? toDate : openProcessingRequest.ToDate; var employees = await _erpManager.GetEmployeesInfo(employeesIds); var lastRequestEmployeeId = employees.Max(e => e.EmployeeId); if (fromDate > openProcessingRequest.ToDate) { if (lastEmployeeId >= lastRequestEmployeeId) { openProcessingRequest.Completed = true; openProcessingRequest.CompletedDate = DateTime.Now; await _processingsRequestsManager.UpdateItemAsync(openProcessingRequest); return(true); } fromDate = openProcessingRequest.FromDate; toDate = openProcessingRequest.FromDate.AddDays(10); toDate = toDate <= openProcessingRequest.ToDate ? toDate : openProcessingRequest.ToDate; employees = employees .Where(e => e.EmployeeId > lastEmployeeId).OrderBy(e => e.EmployeeId) .Take(100).ToList(); } else if (lastEmployeeId > 0) { employees = employees .Where(e => e.EmployeeId <= lastEmployeeId).OrderByDescending(e => e.EmployeeId) .Take(100).ToList(); } else { employees = employees .Where(e => e.EmployeeId > lastEmployeeId).OrderBy(e => e.EmployeeId) .Take(100).ToList(); } return(await processDaysReports(employees, fromDate, toDate)); } } catch (Exception ex) { } return(true); }