49 lines
2.2 KiB
C#
49 lines
2.2 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.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, int page, int pageSize) =>
|
|
await context.Invoices
|
|
.Include(i => i.ShopPO).ThenInclude(p => p.Store)
|
|
.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))
|
|
.OrderByDescending(i => i.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
public async Task<int> GetCountAsync(Guid? agentId, Guid? routeId, Guid? shopId, InvoiceStatus? status) =>
|
|
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));
|
|
|
|
public async Task AddAsync(Models.Invoice invoice) =>
|
|
await context.Invoices.AddAsync(invoice);
|
|
|
|
public void Update(Models.Invoice invoice) =>
|
|
context.Invoices.Update(invoice);
|
|
}
|