public async Task <PayRunInsightsDomain> GetPayRunInsightsAsync(Guid payRunId)
        {
            var invoices = await _dbContext.PayrunInvoices.Where(p => p.PayrunId == payRunId).Include(pi => pi.Invoice)
                           .ToListAsync();

            var isCedarDownloaded = await _dbContext.PayrunHistories.Where(ph => ph.PayRunId.Equals(payRunId))
                                    .AnyAsync(ph => ph.Type == PayRunHistoryType.CedarFileDownload);

            var paidLog = await _dbContext.PayrunHistories.Include(ph => ph.Creator).FirstOrDefaultAsync(ph => ph.PayRunId.Equals(payRunId) && ph.Type == PayRunHistoryType.PaidPayrun);

            var heldInvoiceStatuses =
                new[] { InvoiceStatus.Rejected, InvoiceStatus.Held, InvoiceStatus.Released, InvoiceStatus.ReleaseAccepted };

            var result = new PayRunInsightsDomain
            {
                TotalInvoiceAmount = invoices.Where(x => x.InvoiceStatus == InvoiceStatus.Accepted).Sum(i => i.Invoice.GrossTotal),
                SupplierCount      = invoices.Where(x => x.InvoiceStatus == InvoiceStatus.Accepted).Select(i => i.Invoice.SupplierId).Distinct().Count(),
                ServiceUserCount   = invoices.Where(x => x.InvoiceStatus == InvoiceStatus.Accepted).Select(i => i.Invoice.ServiceUserId).Distinct().Count(),
                HoldsCount         = invoices.Count(i => heldInvoiceStatuses.Contains(i.InvoiceStatus)),
                TotalHeldAmount    = invoices.Where(i => heldInvoiceStatuses.Contains(i.InvoiceStatus))
                                     .Sum(i => i.Invoice.NetTotal),
                IsCedarFileDownloaded = isCedarDownloaded,
                PaidBy = paidLog?.Creator.Name,
                PaidOn = paidLog?.DateCreated
            };

            return(result);
        }
Ejemplo n.º 2
0
        public MakePayRunPaymentUseCaseTests()
        {
            _payrun         = TestDataHelper.CreatePayRun(type: PayrunType.ResidentialRecurring, status: PayrunStatus.Approved);
            _payRunInsights = new PayRunInsightsDomain
            {
                TotalInvoiceAmount = 1000M,
                HoldsCount         = 5,
                TotalHeldAmount    = 100M
            };

            var payRunInvoiceGateway = new Mock <IPayRunInvoiceGateway>();

            _payRunGateway = new Mock <IPayRunGateway>();
            _dbManager     = new Mock <IDatabaseManager>();

            _payRunGateway.Setup(g => g.GetPayRunAsync(It.IsAny <Guid>(), It.IsAny <PayRunFields>(), It.IsAny <bool>()))
            .ReturnsAsync(_payrun);

            payRunInvoiceGateway.Setup(g => g.GetPayRunInsightsAsync(It.IsAny <Guid>())).ReturnsAsync(_payRunInsights);

            _useCase = new MakePayRunPaymentUseCase(_payRunGateway.Object, _dbManager.Object,
                                                    payRunInvoiceGateway.Object);
        }