Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void SplitSoldHoursByTimesheets(Job job)
- {
- var techIdToDuration = TenantHub.GetTimesheetService().GetJobTimesheets(job.Id)
- .Where(x => x.ArrivedOnUtc.HasValue)
- .Select(x => new {
- TechnicianId = x.TechnicianId,
- Duration = (x.DoneOnUtc ?? x.CanceledOnUtc ?? x.ArrivedOnUtc.Value) - x.ArrivedOnUtc.Value
- })
- .Where(x => x.Duration > TimeSpan.Zero)
- .GroupBy(x => x.TechnicianId)
- .ToDictionary(x => x.Key, y => y.Sum(z => (int)z.Duration.TotalSeconds));
- if (!techIdToDuration.Any()) {
- return;
- }
- var techsInfo = job.Assignments.Select(x => new {
- AssignmentId = x.Id,
- TechnicianId = x.Technician.Id,
- IsManagedTech = x.Technician.IsManagedTech,
- }).ToArray();
- var managedTechs = techsInfo.Where(x => x.IsManagedTech).Select(x => x.TechnicianId).ToHashSet();
- var managedTechAndDuration = techIdToDuration.Keys.Where(managedTechs.Contains)
- .Select(x => new {
- TechnicianId = x,
- Duration = techIdToDuration[x],
- }).ToArray();
- var managedTechsDurationTotal = managedTechAndDuration.Sum(x => x.Duration);
- if (managedTechsDurationTotal <= 0) {
- return;
- }
- TenantHub.GetDispatchService().UpdateSplits(job.Id, managedTechAndDuration.Select(tech => new SplitModel
- {
- AssignmentId = techsInfo.FirstOrDefault(a => a.TechnicianId == tech.TechnicianId)?.AssignmentId ?? 0,
- TechnicianId = tech.TechnicianId,
- Split = Math.Round((decimal)tech.Duration * 100 / managedTechsDurationTotal, 2)
- }).ToArray(), false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement