first commit
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using Fitness_core_BE.Interfaces;
|
||||
using Fitness_core_BE.Models;
|
||||
using Fitness_core_BE;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Fitness_core_BE.Repos
|
||||
{
|
||||
public class ClientInvoiceRepository : IClientInvoiceRepository
|
||||
{
|
||||
private readonly CentralDbContext _context;
|
||||
|
||||
public ClientInvoiceRepository(CentralDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<ClientInvoice> CreateClientInvoiceFromSubscription(Guid subscriptionId)
|
||||
{
|
||||
var subscription = await _context.ClientSubscriptions
|
||||
.Include(cs => cs.ClientPackage)
|
||||
.Include(cs => cs.Client)
|
||||
.ThenInclude(c => c.User)
|
||||
.FirstOrDefaultAsync(cs => cs.SubscriptionId == subscriptionId);
|
||||
|
||||
if (subscription == null)
|
||||
{
|
||||
throw new Exception("Subscription not found");
|
||||
}
|
||||
|
||||
var subItems = await _context.ClientSubItems
|
||||
.Where(si => si.FKClientSubscriptionId == subscriptionId && si.IsActive)
|
||||
.ToListAsync();
|
||||
|
||||
decimal totalAmount = subscription.ClientPackage.PackagePrice;
|
||||
totalAmount += subItems.Sum(si => si.SubItemPrice);
|
||||
|
||||
var clientCard = await _context.ClientCard
|
||||
.FirstOrDefaultAsync(cc => cc.FK_ClientId == subscription.Client.ClientId);
|
||||
|
||||
if (clientCard == null)
|
||||
{
|
||||
throw new Exception("Client card not found");
|
||||
}
|
||||
|
||||
var clientInvoice = new ClientInvoice
|
||||
{
|
||||
ClientInvoiceId = Guid.NewGuid(),
|
||||
FKClientSubscriptionId = subscriptionId,
|
||||
FKUserId = subscription.Client.FKUserId,
|
||||
FKClientCardId = clientCard.CardId,
|
||||
Amount = totalAmount,
|
||||
Status = "Pending",
|
||||
ClientInvoiceDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.ClientInvoices.Add(clientInvoice);
|
||||
|
||||
var packageItem = new ClientInvoiceItem
|
||||
{
|
||||
ClientInvoiceItemId = Guid.NewGuid(),
|
||||
FKClientInvoiceId = clientInvoice.ClientInvoiceId,
|
||||
ItemName = subscription.ClientPackage.PackageName,
|
||||
ItemDescription = subscription.ClientPackage.PackageDescription,
|
||||
ItemPrice = subscription.ClientPackage.PackagePrice,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.ClientInvoiceItems.Add(packageItem);
|
||||
|
||||
foreach (var subItem in subItems)
|
||||
{
|
||||
var clientInvoiceItem = new ClientInvoiceItem
|
||||
{
|
||||
ClientInvoiceItemId = Guid.NewGuid(),
|
||||
FKClientInvoiceId = clientInvoice.ClientInvoiceId,
|
||||
ItemName = subItem.SubItemName,
|
||||
ItemDescription = subItem.SubItemDescription,
|
||||
ItemPrice = subItem.SubItemPrice,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.ClientInvoiceItems.Add(clientInvoiceItem);
|
||||
}
|
||||
|
||||
return clientInvoice;
|
||||
}
|
||||
|
||||
public async Task<ClientInvoice?> GetClientInvoiceById(Guid clientInvoiceId)
|
||||
{
|
||||
return await _context.ClientInvoices
|
||||
.Include(i => i.ClientSubscription)
|
||||
.ThenInclude(cs => cs.ClientPackage)
|
||||
.Include(i => i.ClientSubscription)
|
||||
.ThenInclude(cs => cs.Client)
|
||||
.Include(i => i.User)
|
||||
.Include(i => i.ClientCard)
|
||||
.Include(i => i.Items)
|
||||
.FirstOrDefaultAsync(i => i.ClientInvoiceId == clientInvoiceId);
|
||||
}
|
||||
|
||||
public async Task<List<ClientInvoice>> GetClientInvoicesByClientId(Guid clientId)
|
||||
{
|
||||
return await _context.ClientInvoices
|
||||
.Include(i => i.ClientSubscription)
|
||||
.ThenInclude(cs => cs.ClientPackage)
|
||||
.Include(i => i.User)
|
||||
.Include(i => i.ClientCard)
|
||||
.Include(i => i.Items)
|
||||
.Where(i => i.ClientSubscription.FKClientId == clientId)
|
||||
.OrderByDescending(i => i.ClientInvoiceDate)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<ClientInvoice>> GetClientInvoicesByUserId(Guid userId)
|
||||
{
|
||||
return await _context.ClientInvoices
|
||||
.Include(i => i.ClientSubscription)
|
||||
.ThenInclude(cs => cs.ClientPackage)
|
||||
.Include(i => i.ClientSubscription)
|
||||
.ThenInclude(cs => cs.Client)
|
||||
.Include(i => i.ClientCard)
|
||||
.Include(i => i.Items)
|
||||
.Where(i => i.FKUserId == userId)
|
||||
.OrderByDescending(i => i.ClientInvoiceDate)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ClientInvoice> UpdateClientInvoiceStatus(Guid clientInvoiceId, string status)
|
||||
{
|
||||
var clientInvoice = await _context.ClientInvoices.FindAsync(clientInvoiceId);
|
||||
|
||||
if (clientInvoice == null)
|
||||
{
|
||||
throw new Exception("ClientInvoice not found");
|
||||
}
|
||||
|
||||
clientInvoice.Status = status;
|
||||
|
||||
return clientInvoice;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user