using Distribution.Data; using Distribution.Models; using Microsoft.EntityFrameworkCore; namespace Distribution.Features.SupplierPayment; public class SupplierPaymentRepository(AppDBContext context) : ISupplierPaymentRepository { public async Task GetByIdAsync(Guid id) => await context.SupplierPayments .Include(p => p.ChequePage) .Include(p => p.Allocations) .FirstOrDefaultAsync(p => p.Id == id); public async Task> ListBySupplierAsync(Guid supplierId, int page, int pageSize) => await context.SupplierPayments .Include(p => p.ChequePage) .Include(p => p.Allocations) .Where(p => p.FK_SupplierId == supplierId && p.IsActive) .OrderByDescending(p => p.PaidAt) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); public async Task CountBySupplierAsync(Guid supplierId) => await context.SupplierPayments .CountAsync(p => p.FK_SupplierId == supplierId && p.IsActive); public async Task AddAsync(Models.SupplierPayment payment) => await context.SupplierPayments.AddAsync(payment); public async Task GetAllocatedTotalForGrnAsync(Guid goodsReceiptId) => await context.SupplierPaymentGRNs .Where(a => a.FK_GoodsReceiptId == goodsReceiptId && a.IsActive && a.SupplierPayment!.IsActive) .SumAsync(a => (decimal?)a.AllocatedAmount) ?? 0m; public async Task GetGrnWithItemsAsync(Guid goodsReceiptId) => await context.GoodsReceipts .Include(g => g.Items) .FirstOrDefaultAsync(g => g.Id == goodsReceiptId && g.IsActive); }