public BillDTO(DataModel.Bill bill) { this.TenantId = bill.TenantId; this.Resource = bill.Resource; this.PeriodStart = bill.Period.PeriodStart; this.PeriodEnd = bill.Period.PeriodEnd; this.BillingPeriodId = bill.Period.BillingPeriodId; this.Rate = bill.Rate; this.Usage = bill.Usage; this.Paid = bill.Paid; this.Cost = bill.Cost(); this.Owed = bill.Owed(); }
public async Task <DataModel.Bill> GetBill(int tenantId, ResourceType resource, BillingPeriod period) { if (period == null) { return(null); } var billingRate = await _context.ResourceUsageRates .Where(r => r.ResourceType == resource) .Where(r => r.PeriodStart <= period.PeriodStart && r.PeriodEnd >= period.PeriodEnd) .Select(r => r) .FirstOrDefaultAsync(); var totalUsage = await _context.TenantResourceUsages .Where(u => u.TenantId == tenantId) .Where(u => u.ResourceType == resource) .Where(u => u.SampleTime >= period.PeriodStart && u.SampleTime <= period.PeriodEnd) .SumAsync(u => u.UsageAmount); var totalPaid = await _context.Payments .Where(p => p.TenantId == tenantId) .Where(p => p.ResourceType == resource) .Where(p => p.BillingPeriodId == period.BillingPeriodId) .SumAsync(p => p.Amount); var bill = new DataModel.Bill { Resource = resource, Period = period, Usage = totalUsage, Rate = billingRate.Rate, Paid = totalPaid, TenantId = tenantId, }; return(bill); }
public async Task <IEnumerable <DataModel.Bill> > GetBills(BillingPeriod period) { if (period == null) { return(new List <DataModel.Bill>()); } var billingRates = await _context.ResourceUsageRates .Where(r => r.PeriodStart <= period.PeriodStart && r.PeriodEnd >= period.PeriodEnd) .Select(r => r) .ToListAsync(); var totalUsages = await _context.TenantResourceUsages .Where(u => u.SampleTime >= period.PeriodStart && u.SampleTime <= period.PeriodEnd) .GroupBy(u => new { u.TenantId, u.ResourceType }) .Select(gr => new { Resource = gr.Key.ResourceType, Usage = gr.Sum(u => u.UsageAmount), TenantId = gr.Key.TenantId }) .ToListAsync(); var totalPayments = await _context.Payments .Where(p => p.BillingPeriodId == period.BillingPeriodId) .GroupBy(p => new { p.TenantId, p.ResourceType }) .Select(gr => new { Resource = gr.Key.ResourceType, Payment = gr.Sum(p => p.Amount), TenantId = gr.Key.TenantId }) .ToListAsync(); var bills = new List <DataModel.Bill>(); foreach (var resourceUsage in totalUsages) { var rate = billingRates .Where(r => r.ResourceType == resourceUsage.Resource) .Select(r => r.Rate) .FirstOrDefault(); var payment = totalPayments .Where(p => p.Resource == resourceUsage.Resource) .Where(p => p.TenantId == resourceUsage.TenantId) .Sum(p => p.Payment); var bill = new DataModel.Bill { Resource = resourceUsage.Resource, Period = period, Usage = resourceUsage.Usage, Rate = rate, Paid = payment, TenantId = resourceUsage.TenantId }; bills.Add(bill); } return(bills); }