138 lines
5.1 KiB
C#
138 lines
5.1 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
|
|||
|
|
{
|
|||
|
|
#region Declaraciones
|
|||
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|||
|
|
#endregion
|
|||
|
|
#region Métodos
|
|||
|
|
public async Task<PagedResult<EQuoteHeader>> GetAllAsync(int page = 1, int pageSize = 50)
|
|||
|
|
{
|
|||
|
|
var query = _context.PhSQuoteHeaders
|
|||
|
|
.Include(q => q.PhSQuoteDetails)
|
|||
|
|
.Include(q => q.PhSQuoteRoles)
|
|||
|
|
.AsQueryable();
|
|||
|
|
|
|||
|
|
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)
|
|||
|
|
.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)
|
|||
|
|
.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)
|
|||
|
|
.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 == "PhS_Professionals" && r.EntityId == professionalId));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (institutionId.HasValue)
|
|||
|
|
{
|
|||
|
|
query = query.Where(q => q.PhSQuoteRoles.Any(r => r.Entitytype == "PhS_Institutions" && r.EntityId == institutionId));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (patientId.HasValue)
|
|||
|
|
{
|
|||
|
|
query = query.Where(q => q.PhSQuoteRoles.Any(r => r.Entitytype == "PhS_Patients" && 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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Paginación final
|
|||
|
|
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();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|