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); }
public async Task ExecuteAsync(Guid payRunId) { var payRun = await _payRunGateway .GetPayRunAsync(payRunId, PayRunFields.None, true) .EnsureExistsAsync($"Pay Run with id {payRunId} not found"); var paidStatuses = new[] { PayrunStatus.Paid, PayrunStatus.PaidWithHold }; if (paidStatuses.Contains(payRun.Status)) { throw new ApiException($"Pay run with id {payRunId} already marked as paid", HttpStatusCode.BadRequest); } if (payRun.Status == PayrunStatus.Archived) { throw new ApiException($"Status change not allowed. Pay run with id {payRunId} is archived", HttpStatusCode.BadRequest); } if (payRun.Status != PayrunStatus.Approved) { throw new ApiException($"Pay run must be approved before marking as paid", HttpStatusCode.BadRequest); } // Change status of pay run var invoices = await _payRunInvoiceGateway.GetPayRunInsightsAsync(payRunId); payRun.Status = PayrunStatus.Paid; payRun.Paid = invoices.TotalInvoiceAmount; payRun.Held = invoices.TotalHeldAmount; if (invoices.HoldsCount > 0) { payRun.Status = PayrunStatus.PaidWithHold; } var history = new PayrunHistory { Status = payRun.Status, Notes = $"Pay run marked as paid", Type = PayRunHistoryType.PaidPayrun }; payRun.Histories.Add(history); await _dbManager.SaveAsync("Failed to mark pay run as paid"); }