Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1.         private void SplitSoldHoursByTimesheets(Job job)
  2.         {
  3.             var techIdToDuration = TenantHub.GetTimesheetService().GetJobTimesheets(job.Id)
  4.                 .Where(x => x.ArrivedOnUtc.HasValue)
  5.                 .Select(x => new {
  6.                     TechnicianId = x.TechnicianId,
  7.                     Duration = (x.DoneOnUtc ?? x.CanceledOnUtc ?? x.ArrivedOnUtc.Value) - x.ArrivedOnUtc.Value
  8.                 })
  9.                 .Where(x => x.Duration > TimeSpan.Zero)
  10.                 .GroupBy(x => x.TechnicianId)
  11.                 .ToDictionary(x => x.Key, y => y.Sum(z => (int)z.Duration.TotalSeconds));
  12.  
  13.             if (!techIdToDuration.Any()) {
  14.                 return;
  15.             }
  16.  
  17.             var techsInfo = job.Assignments.Select(x => new {
  18.                 AssignmentId = x.Id,
  19.                 TechnicianId = x.Technician.Id,
  20.                 IsManagedTech = x.Technician.IsManagedTech,
  21.             }).ToArray();
  22.            
  23.             var managedTechs = techsInfo.Where(x => x.IsManagedTech).Select(x => x.TechnicianId).ToHashSet();
  24.             var managedTechAndDuration = techIdToDuration.Keys.Where(managedTechs.Contains)
  25.                 .Select(x => new {
  26.                     TechnicianId = x,
  27.                     Duration = techIdToDuration[x],
  28.                 }).ToArray();
  29.  
  30.             var managedTechsDurationTotal = managedTechAndDuration.Sum(x => x.Duration);
  31.             if (managedTechsDurationTotal <= 0) {
  32.                 return;
  33.             }
  34.            
  35.             TenantHub.GetDispatchService().UpdateSplits(job.Id, managedTechAndDuration.Select(tech => new SplitModel
  36.             {
  37.                 AssignmentId = techsInfo.FirstOrDefault(a => a.TechnicianId == tech.TechnicianId)?.AssignmentId ?? 0,
  38.                 TechnicianId = tech.TechnicianId,
  39.                 Split = Math.Round((decimal)tech.Duration * 100 / managedTechsDurationTotal, 2)
  40.             }).ToArray(), false);
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement