using Microsoft.EntityFrameworkCore; using PowderCoating.Core.Entities; using PowderCoating.Core.Enums; using PowderCoating.Core.Interfaces.Repositories; using PowderCoating.Infrastructure.Data; namespace PowderCoating.Infrastructure.Repositories; /// /// Typed repository for that provides domain-specific multi-level /// include queries previously expressed inline in BillsController. /// public class BillRepository : Repository, IBillRepository { public BillRepository(ApplicationDbContext context) : base(context) { } /// public async Task LoadForViewAsync(int id) { return await _context.Bills .Where(b => b.Id == id && !b.IsDeleted) .Include(b => b.Vendor) .Include(b => b.APAccount) .Include(b => b.LineItems.Where(li => !li.IsDeleted)) .ThenInclude(li => li.Account) .Include(b => b.LineItems.Where(li => !li.IsDeleted)) .ThenInclude(li => li.Job) .Include(b => b.Payments.Where(p => !p.IsDeleted)) .ThenInclude(p => p.BankAccount) .FirstOrDefaultAsync(); } /// public async Task LoadForEditAsync(int id) { return await _context.Bills .Where(b => b.Id == id && !b.IsDeleted) .Include(b => b.LineItems.Where(li => !li.IsDeleted)) .FirstOrDefaultAsync(); } /// public async Task> GetForIndexAsync(string? statusFilter, string? searchTerm, decimal? searchAmount) { var query = _context.Bills .Include(b => b.Vendor) .Include(b => b.LineItems.Where(li => !li.IsDeleted)) .Where(b => !b.IsDeleted); if (statusFilter == "Unpaid") query = query.Where(b => b.Status == BillStatus.Open || b.Status == BillStatus.PartiallyPaid); else if (statusFilter == "Overdue") query = query.Where(b => b.Status != BillStatus.Paid && b.Status != BillStatus.Voided && b.DueDate.HasValue && b.DueDate.Value.Date < DateTime.Today); if (!string.IsNullOrEmpty(searchTerm)) { var term = searchTerm; query = query.Where(b => b.BillNumber.Contains(term) || b.Vendor.CompanyName.Contains(term) || (b.VendorInvoiceNumber != null && b.VendorInvoiceNumber.Contains(term)) || (b.Memo != null && b.Memo.Contains(term)) || b.LineItems.Any(li => li.Description.Contains(term)) || (searchAmount.HasValue && (b.Total == searchAmount.Value || b.AmountPaid == searchAmount.Value))); } return await query.OrderByDescending(b => b.BillDate).ToListAsync(); } /// public async Task GetLastBillNumberAsync(string prefix) { return await _context.Bills .IgnoreQueryFilters() .Where(b => b.BillNumber.StartsWith(prefix)) .OrderByDescending(b => b.BillNumber) .Select(b => b.BillNumber) .FirstOrDefaultAsync(); } /// public async Task GetLastPaymentNumberAsync(string prefix) { return await _context.BillPayments .IgnoreQueryFilters() .Where(p => p.PaymentNumber.StartsWith(prefix)) .OrderByDescending(p => p.PaymentNumber) .Select(p => p.PaymentNumber) .FirstOrDefaultAsync(); } }