81 lines
3.8 KiB
C#
81 lines
3.8 KiB
C#
using Distribution.Data;
|
|
using Distribution.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Distribution.Features.Invoice;
|
|
|
|
public class InvoiceRepository(AppDBContext context) : IInvoiceRepository
|
|
{
|
|
public async Task<Models.Invoice?> GetByIdAsync(Guid id) =>
|
|
await context.Invoices
|
|
.Include(i => i.ShopPO).ThenInclude(p => p.Store)
|
|
.Include(i => i.ShopPO).ThenInclude(p => p.Items.Where(x => x.IsActive))
|
|
.Include(i => i.Items.Where(x => x.IsActive)).ThenInclude(x => x.Item)
|
|
.Include(i => i.Items.Where(x => x.IsActive)).ThenInclude(x => x.ShopPOItem)
|
|
.Include(i => i.Payments.Where(p => p.IsActive))
|
|
.FirstOrDefaultAsync(i => i.Id == id && i.IsActive);
|
|
|
|
public async Task<Models.Invoice?> GetByShopPOIdAsync(Guid shopPOId) =>
|
|
await context.Invoices
|
|
.Include(i => i.ShopPO)
|
|
.FirstOrDefaultAsync(i => i.FK_ShopPOId == shopPOId && i.IsActive);
|
|
|
|
public async Task<List<Models.Invoice>> GetAllAsync(Guid? agentId, Guid? routeId, Guid? shopId, InvoiceStatus? status, PaymentStatus? paymentStatus, int page, int pageSize) =>
|
|
await context.Invoices
|
|
.Include(i => i.ShopPO).ThenInclude(p => p.Store)
|
|
.Include(i => i.Payments.Where(p => p.IsActive))
|
|
.Where(i => i.IsActive
|
|
&& (agentId == null || i.ShopPO.RaisedById == agentId)
|
|
&& (routeId == null || i.ShopPO.Store.FK_RouteId == routeId)
|
|
&& (shopId == null || i.ShopPO.FK_StoreId == shopId)
|
|
&& (status == null || i.Status == status)
|
|
&& (paymentStatus == null || i.PaymentStatus == paymentStatus))
|
|
.OrderByDescending(i => i.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
public async Task<int> GetCountAsync(Guid? agentId, Guid? routeId, Guid? shopId, InvoiceStatus? status, PaymentStatus? paymentStatus) =>
|
|
await context.Invoices
|
|
.Include(i => i.ShopPO).ThenInclude(p => p.Store)
|
|
.CountAsync(i => i.IsActive
|
|
&& (agentId == null || i.ShopPO.RaisedById == agentId)
|
|
&& (routeId == null || i.ShopPO.Store.FK_RouteId == routeId)
|
|
&& (shopId == null || i.ShopPO.FK_StoreId == shopId)
|
|
&& (status == null || i.Status == status)
|
|
&& (paymentStatus == null || i.PaymentStatus == paymentStatus));
|
|
|
|
public async Task<List<Models.Invoice>> GetOutstandingAsync() =>
|
|
await context.Invoices
|
|
.Include(i => i.Payments.Where(p => p.IsActive))
|
|
.Where(i => i.IsActive && i.PaymentStatus != PaymentStatus.Paid)
|
|
.ToListAsync();
|
|
|
|
public async Task AddAsync(Models.Invoice invoice) =>
|
|
await context.Invoices.AddAsync(invoice);
|
|
|
|
public async Task<Models.Item?> GetItemAsync(Guid itemId) =>
|
|
await context.Items.FirstOrDefaultAsync(i => i.Id == itemId && i.IsActive);
|
|
|
|
public async Task AddItemsAsync(IEnumerable<InvoiceItem> items) =>
|
|
await context.InvoiceItems.AddRangeAsync(items);
|
|
|
|
public async Task AddVersionAsync(InvoiceVersion version) =>
|
|
await context.InvoiceVersions.AddAsync(version);
|
|
|
|
public async Task<int> GetLatestVersionNumberAsync(Guid invoiceId) =>
|
|
await context.InvoiceVersions
|
|
.Where(v => v.FK_InvoiceId == invoiceId)
|
|
.MaxAsync(v => (int?)v.VersionNumber) ?? 0;
|
|
|
|
public async Task<List<InvoiceVersion>> GetVersionsAsync(Guid invoiceId) =>
|
|
await context.InvoiceVersions
|
|
.Include(v => v.Items).ThenInclude(i => i.Item)
|
|
.Where(v => v.FK_InvoiceId == invoiceId && v.IsActive)
|
|
.OrderBy(v => v.VersionNumber)
|
|
.ToListAsync();
|
|
|
|
public void Update(Models.Invoice invoice) =>
|
|
context.Invoices.Update(invoice);
|
|
}
|