All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m24s
154 lines
6.5 KiB
C#
154 lines
6.5 KiB
C#
using Domain.Entities;
|
|
using Domain.Generics;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Models.Helpers;
|
|
using Models.Interfaces;
|
|
using Models.Models;
|
|
|
|
namespace PhronCare.Core.Data.Repositories.Sales
|
|
{
|
|
public class PhSQuoteHeaderRepository(PhronCareOperationsHubContext context) : IPhSQuoteHeaderRepository
|
|
{
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|
public async Task<PagedResult<EQuoteHeader>> GetAllAsync(int page = 1, int pageSize = 50)
|
|
{
|
|
var query = _context.PhSQuoteHeaders
|
|
.Include(q => q.PhSQuoteDetails)
|
|
.Include(q => q.PhSQuoteRoles)
|
|
.Include(q => q.PhSQuoteAdjustments)
|
|
.AsNoTracking();
|
|
|
|
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
|
|
|
|
return new PagedResult<EQuoteHeader>
|
|
{
|
|
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>),
|
|
TotalItems = pagedEntities.TotalItems,
|
|
Page = pagedEntities.Page,
|
|
PageSize = pagedEntities.PageSize
|
|
};
|
|
}
|
|
public async Task<EQuoteHeader?> GetByIdAsync(int id)
|
|
{
|
|
var entity = await _context.PhSQuoteHeaders
|
|
.Include(q => q.PhSQuoteDetails)
|
|
.Include(q => q.PhSQuoteRoles)
|
|
.Include(q => q.PhSQuoteAdjustments)
|
|
.FirstOrDefaultAsync(q => q.Id == id);
|
|
|
|
return entity != null ? EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>(entity) : null;
|
|
}
|
|
public async Task<IEnumerable<EQuoteHeader>> GetByCustomerIdAsync(int customerId)
|
|
{
|
|
var entities = await _context.PhSQuoteHeaders
|
|
.Where(q => q.CustomerId == customerId)
|
|
.Include(q => q.PhSQuoteDetails)
|
|
.Include(q => q.PhSQuoteRoles)
|
|
.Include(q => q.PhSQuoteAdjustments)
|
|
.ToListAsync();
|
|
|
|
return entities.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>);
|
|
}
|
|
public async Task<PagedResult<EQuoteHeader>> SearchAsync(int? customerId,
|
|
string? quoteNumber, int? professionalId, int? institutionId, int? patientId,
|
|
DateTime? issueDateFrom, DateTime? issueDateTo, string? status,
|
|
int page = 1, int pageSize = 50)
|
|
{
|
|
var query = _context.PhSQuoteHeaders
|
|
.Include(q => q.PhSQuoteDetails)
|
|
.Include(q => q.PhSQuoteRoles)
|
|
.Include(q => q.PhSQuoteAdjustments)
|
|
.AsQueryable();
|
|
|
|
if (customerId.HasValue)
|
|
query = query.Where(q => q.CustomerId == customerId);
|
|
|
|
if (!string.IsNullOrEmpty(quoteNumber))
|
|
query = query.Where(q => q.Quotenumber.Contains(quoteNumber));
|
|
|
|
if (professionalId.HasValue)
|
|
query = query.Where(q => q.PhSQuoteRoles.Any(r => r.Entitytype == PhSEntityTypes.Professional && r.EntityId == professionalId));
|
|
|
|
if (institutionId.HasValue)
|
|
query = query.Where(q => q.PhSQuoteRoles.Any(r => r.Entitytype == PhSEntityTypes.Institution && r.EntityId == institutionId));
|
|
|
|
if (patientId.HasValue)
|
|
query = query.Where(q => q.PhSQuoteRoles.Any(r => r.Entitytype == PhSEntityTypes.Patient && r.EntityId == patientId));
|
|
|
|
if (issueDateFrom.HasValue)
|
|
query = query.Where(q => q.Issuedate >= issueDateFrom.Value);
|
|
|
|
if (issueDateTo.HasValue)
|
|
query = query.Where(q => q.Issuedate <= issueDateTo.Value);
|
|
|
|
if (!string.IsNullOrEmpty(status))
|
|
query = query.Where(q => q.Status == status);
|
|
|
|
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
|
|
|
|
return new PagedResult<EQuoteHeader>
|
|
{
|
|
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>),
|
|
TotalItems = pagedEntities.TotalItems,
|
|
Page = pagedEntities.Page,
|
|
PageSize = pagedEntities.PageSize
|
|
};
|
|
}
|
|
public async Task<EQuoteHeader> AddAsync(EQuoteHeader quoteHeader)
|
|
{
|
|
var dbEntity = EntityMapper.MapEntity<EQuoteHeader, PhSQuoteHeader>(quoteHeader);
|
|
_context.PhSQuoteHeaders.Add(dbEntity);
|
|
await _context.SaveChangesAsync();
|
|
return EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>(dbEntity);
|
|
}
|
|
public async Task UpdateAsync(EQuoteHeader quoteHeader)
|
|
{
|
|
var dbEntity = EntityMapper.MapEntity<EQuoteHeader, PhSQuoteHeader>(quoteHeader);
|
|
_context.PhSQuoteHeaders.Update(dbEntity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var entity = await _context.PhSQuoteHeaders.FindAsync(id);
|
|
if (entity != null)
|
|
{
|
|
_context.PhSQuoteHeaders.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
// ----------------------------
|
|
// Métodos para Ajustes
|
|
// ----------------------------
|
|
public async Task<IEnumerable<EQuoteAdjustment>> GetAdjustmentsByQuoteIdAsync(int quoteId)
|
|
{
|
|
var adjustments = await _context.PhSQuoteAdjustments
|
|
.Where(a => a.QuoteheaderId == quoteId)
|
|
.ToListAsync();
|
|
|
|
return adjustments.Select(EntityMapper.MapEntity<PhSQuoteAdjustment, EQuoteAdjustment>);
|
|
}
|
|
public async Task<EQuoteAdjustment> AddAdjustmentAsync(EQuoteAdjustment adjustment)
|
|
{
|
|
var dbEntity = EntityMapper.MapEntity<EQuoteAdjustment, PhSQuoteAdjustment>(adjustment);
|
|
_context.PhSQuoteAdjustments.Add(dbEntity);
|
|
await _context.SaveChangesAsync();
|
|
return EntityMapper.MapEntity<PhSQuoteAdjustment, EQuoteAdjustment>(dbEntity);
|
|
}
|
|
public async Task UpdateAdjustmentAsync(EQuoteAdjustment adjustment)
|
|
{
|
|
var dbEntity = EntityMapper.MapEntity<EQuoteAdjustment, PhSQuoteAdjustment>(adjustment);
|
|
_context.PhSQuoteAdjustments.Update(dbEntity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
public async Task DeleteAdjustmentAsync(int adjustmentId)
|
|
{
|
|
var entity = await _context.PhSQuoteAdjustments.FindAsync(adjustmentId);
|
|
if (entity != null)
|
|
{
|
|
_context.PhSQuoteAdjustments.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
} |