5707bd4c31
Introduced StoreCreditMovement entity and migration to track store credit usage and settlements. Updated payment flow to enforce credit limits and record credit movements. Modified outstanding balance calculation to use credit movements. Enhanced frontend to display real-time credit info and prevent over-credit payments. Updated API types, seed data, and DI registrations accordingly.
58 lines
2.7 KiB
C#
58 lines
2.7 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, 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 void Update(Models.Invoice invoice) =>
|
|
context.Invoices.Update(invoice);
|
|
}
|