public async Task <PayRunInsightsResponse> GetAsync(Guid payRunId) { var payRun = await _payRunGateway.GetPayRunAsync(payRunId).EnsureExistsAsync($"Pay run with id {payRunId} not found"); var insights = await _payRunInvoiceGateway.GetPayRunInsightsAsync(payRunId); var previousPayRun = await _payRunGateway.GetPreviousPayRunAsync(payRun.Type); // Get pay run invoiced totals var previousPayRunTotal = 0M; if (previousPayRun != null) { previousPayRunTotal = await _payRunInvoiceGateway.GetPayRunInvoicedTotalAsync(previousPayRun.Id, new[] { InvoiceStatus.Accepted }); } var result = new PayRunInsightsResponse { PayRunId = payRun.Id, PayRunStatus = payRun.Status, TotalInvoiceAmount = insights.TotalInvoiceAmount, TotalDifferenceFromLastCycle = insights.TotalInvoiceAmount - previousPayRunTotal, SupplierCount = insights.SupplierCount, ServiceUserCount = insights.ServiceUserCount, HoldsCount = insights.HoldsCount, TotalHeldAmount = insights.TotalHeldAmount, IsCedarFileDownloaded = insights.IsCedarFileDownloaded, PaidBy = insights.PaidBy, PaidOn = insights.PaidOn }; return(result); }