public static IEnumerable <RealTimeCommission> GetCustomerRealTimeCommissions(GetCustomerRealTimeCommissionsRequest request) { var results = new List <RealTimeCommission>(); // Get the commission record var realtimeresponse = Exigo.WebService().GetRealTimeCommissions(new Common.Api.ExigoWebService.GetRealTimeCommissionsRequest { CustomerID = request.CustomerID }); if (realtimeresponse.Commissions.Length == 0) { return(results); } // Get the unique periods for each of the commission results if (request.GetPeriodVolumes) { var periods = new List <Period>(); var periodRequests = new List <GetPeriodsRequest>(); foreach (var commissionResponse in realtimeresponse.Commissions) { var periodID = commissionResponse.PeriodID; var periodTypeID = commissionResponse.PeriodType; var req = periodRequests.Where(c => c.PeriodTypeID == periodTypeID).FirstOrDefault(); if (req == null) { periodRequests.Add(new GetPeriodsRequest() { PeriodTypeID = periodTypeID, PeriodIDs = new int[] { periodID } }); } else { var ids = req.PeriodIDs.ToList(); ids.Add(periodID); req.PeriodIDs = ids.Distinct().ToArray(); } } foreach (var req in periodRequests) { var responses = GetPeriods(req); foreach (var response in responses) { periods.Add(response); } } // Get the volumes for each unique period var volumeCollections = new List <VolumeCollection>(); foreach (var period in periods) { volumeCollections.Add(GetCustomerVolumes(new GetCustomerVolumesRequest { CustomerID = request.CustomerID, PeriodID = period.PeriodID, PeriodTypeID = period.PeriodTypeID, VolumesToFetch = request.VolumesToFetch })); } // Process each commission response try { foreach (var commission in realtimeresponse.Commissions) { var typedCommission = (RealTimeCommission)commission; typedCommission.Period = periods .Where(c => c.PeriodTypeID == commission.PeriodType) .Where(c => c.PeriodID == commission.PeriodID) .FirstOrDefault(); typedCommission.Volumes = volumeCollections .Where(c => c.Period.PeriodTypeID == typedCommission.Period.PeriodTypeID) .Where(c => c.Period.PeriodID == typedCommission.Period.PeriodID) .FirstOrDefault(); typedCommission.PaidRank = typedCommission.Volumes.PayableAsRank; results.Add(typedCommission); } return(results.OrderByDescending(c => c.Period.StartDate)); } catch { return(results); } } else { var periodInfo = Exigo.GetPeriods(new GetPeriodsRequest { PeriodIDs = realtimeresponse.Commissions.Select(p => p.PeriodID).ToArray(), PeriodTypeID = realtimeresponse.Commissions.FirstOrDefault().PeriodType }); foreach (var commission in realtimeresponse.Commissions) { var typedCommission = (RealTimeCommission)commission; var period = periodInfo.FirstOrDefault(p => p.PeriodID == commission.PeriodID && p.PeriodTypeID == commission.PeriodType); typedCommission.Period = new Period(); //typedCommission.Period.PeriodID = commission.PeriodID; //typedCommission.Period.PeriodTypeID =commission.PeriodType; //typedCommission.Period.PeriodDescription = commission.PeriodDescription; if (period != null) { typedCommission.Period = period; } results.Add(typedCommission); } return(results.OrderByDescending(c => c.Period.StartDate)); } }