public async Task <IActionResult> Get(string groupReferenceKey, string taskReferenceKey, long userId) { if (string.IsNullOrWhiteSpace(taskReferenceKey)) { ModelState.AddModelError("taskReferenceKey", "Invalid Task Reference Key."); return(new BadRequestObjectResult(ModelState)); } if (userId <= 0 || !(await new UserLogic(DbContext, CurrentPartitionId).IsUserInPartitionId(userId))) { ModelState.AddModelError("userId", "Invalid User ID Reference Key."); return(new BadRequestObjectResult(ModelState)); } var hourPriceLogic = new HourPriceLogic(DbContext, CurrentPartitionId); return(Json(await hourPriceLogic.GetHourPrice(groupReferenceKey, taskReferenceKey, userId))); }
public async Task <IActionResult> Get(DateTime?fromDate, DateTime?toDate) { if (!fromDate.HasValue || !toDate.HasValue) { if (!fromDate.HasValue) { ModelState.AddModelError("fromDate", "Required Date is null or empty."); } if (!toDate.HasValue) { ModelState.AddModelError("toDate", "Required Date is null or empty."); } return(new BadRequestObjectResult(ModelState)); } var dateSpanDays = (toDate.Value - fromDate.Value).TotalDays + 1; if (dateSpanDays > 31) { ModelState.AddModelError("", "Date timespan is more than 31 days."); return(new BadRequestObjectResult(ModelState)); } var fromDateValue = fromDate.Value; var toDateValue = toDate.Value; var invoice = new InvoiceApi { FromDate = fromDateValue, ToDate = toDateValue, }; var partitionId = CurrentPartitionId; invoice.GroupTasks = DbContext.TaskItems.Where(ti => ti.Task.Group.PartitionId == partitionId && ti.Time > 0 && ti.Date >= fromDateValue && ti.Date <= toDateValue) .OrderBy(ti => ti.Task.Group.Name).ThenBy(ti => ti.Task.Name).GroupBy(ti => new { Group = ti.Task.Group.Name, Task = ti.Task.Name }) .Select(gw => new InvoiceGroupTaskApi { Group = gw.Key.Group, Task = gw.Key.Task, Time = gw.Sum(ti => ti.Time), Users = gw.OrderBy(ti => ti.User.FullName).GroupBy(ti => ti.UserId) .Select(ggw => new InvoiceUserApi { Id = ggw.Key, FullName = ggw.Select(ti => ti.User.FullName).FirstOrDefault(), Time = ggw.Sum(ti => ti.Time), }).ToList() }).AsNoTracking().ToList(); var hourPriceLogic = new HourPriceLogic(DbContext, CurrentPartitionId); invoice.TotalTime = invoice.GroupTasks.Sum(gt => gt.Time); invoice.SubTotalPrice = 0; foreach (var gt in invoice.GroupTasks) { foreach (var user in gt.Users) { user.HourPrice = hourPriceLogic.GetHourPrice(gt.Group, gt.Task, user.Id).Result?.HourPrice; if (user.HourPrice.HasValue) { user.Price = user.HourPrice.Value * user.Time / 60; invoice.SubTotalPrice += user.Price.Value; } } gt.Price = gt.Users.Sum(u => u.Price); } return(await Task.FromResult(Json(invoice))); }