All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m39s
with Validations
288 lines
12 KiB
C#
288 lines
12 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Models.Interfaces;
|
|
using Models.Helpers;
|
|
using Models.Models;
|
|
using Domain.Entities;
|
|
using Domain.Generics;
|
|
|
|
namespace PhronCare.Core.Data.Repositories.Sales
|
|
{
|
|
public class PhSQuoteHeaderRepository(PhronCareOperationsHubContext context) : IPhSQuoteHeaderRepository
|
|
{
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|
//private readonly IPhSFormSeriesRepository _formSeriesRepository = formSeriesRepository;
|
|
#region Metodos
|
|
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)
|
|
.Include(q => q.PhSQuoteTaxes)
|
|
.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)
|
|
.Include(q => q.PhSQuoteTaxes)
|
|
.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)
|
|
.Include(q => q.PhSQuoteTaxes)
|
|
.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)
|
|
.Include(q => q.PhSQuoteTaxes)
|
|
.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 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
|
|
#region <DECRECATED>
|
|
// ----------------------------
|
|
// 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();
|
|
// }
|
|
//}
|
|
|
|
//// ----------------------------
|
|
//// Métodos para Impuestos
|
|
//// ----------------------------
|
|
|
|
///// <summary>
|
|
///// Obtiene todos los impuestos asociados a un presupuesto dado por su ID.
|
|
///// </summary>
|
|
//public async Task<IEnumerable<EQuoteTax>> GetTaxesByQuoteIdAsync(int quoteId)
|
|
//{
|
|
// var taxes = await _context.PhSQuoteTaxes
|
|
// .Where(t => t.QuoteheaderId == quoteId)
|
|
// .ToListAsync();
|
|
|
|
// return taxes.Select(EntityMapper.MapEntity<PhSQuoteTaxis, EQuoteTax>);
|
|
//}
|
|
|
|
///// <summary>
|
|
///// Agrega un nuevo impuesto al presupuesto correspondiente.
|
|
///// </summary>
|
|
//public async Task<EQuoteTax> AddTaxAsync(EQuoteTax tax)
|
|
//{
|
|
// var dbEntity = EntityMapper.MapEntity<EQuoteTax, PhSQuoteTaxis>(tax);
|
|
// _context.PhSQuoteTaxes.Add(dbEntity);
|
|
// await _context.SaveChangesAsync();
|
|
// return EntityMapper.MapEntity<PhSQuoteTaxis, EQuoteTax>(dbEntity);
|
|
//}
|
|
|
|
///// <summary>
|
|
///// Actualiza los datos de un impuesto existente en un presupuesto.
|
|
///// </summary>
|
|
//public async Task UpdateTaxAsync(EQuoteTax tax)
|
|
//{
|
|
// var dbEntity = EntityMapper.MapEntity<EQuoteTax, PhSQuoteTaxis>(tax);
|
|
// _context.PhSQuoteTaxes.Update(dbEntity);
|
|
// await _context.SaveChangesAsync();
|
|
//}
|
|
|
|
///// <summary>
|
|
///// Elimina un impuesto asociado a un presupuesto a partir de su ID.
|
|
///// </summary>
|
|
//public async Task DeleteTaxAsync(int taxId)
|
|
//{
|
|
// var entity = await _context.PhSQuoteTaxes.FindAsync(taxId);
|
|
// if (entity != null)
|
|
// {
|
|
// _context.PhSQuoteTaxes.Remove(entity);
|
|
// await _context.SaveChangesAsync();
|
|
// }
|
|
//}
|
|
|
|
//#region Guardado completo de presupuesto (encabezado + detalles + roles + ajustes + impuestos)
|
|
///// <summary>
|
|
///// Crea un nuevo presupuesto, incluyendo encabezado, detalles, roles, ajustes e impuestos asociados.
|
|
///// Genera automáticamente el número de presupuesto en base a la serie indicada.
|
|
///// </summary>
|
|
///// <param name="quote">Presupuesto a registrar, incluyendo entidades relacionadas.</param>
|
|
///// <param name="formSeriesId">Identificador de la serie de numeración a utilizar.</param>
|
|
///// <returns>Cadena con el número generado del presupuesto.</returns>
|
|
//public async Task<string> CreateFullQuoteAsync(EQuoteHeader quote, int formSeriesId)
|
|
//{
|
|
// using var transaction = await _context.Database.BeginTransactionAsync();
|
|
// try
|
|
// {
|
|
// // Obtener el próximo número de presupuesto desde SP
|
|
// var nextNumber = await _formSeriesRepository.GetNextInternalNumberAsync(formSeriesId);
|
|
// quote.Quotenumber = nextNumber.ToString();
|
|
|
|
// // Map y guardado de Header
|
|
// var headerEntity = EntityMapper.MapEntity<EQuoteHeader, PhSQuoteHeader>(quote);
|
|
// _context.PhSQuoteHeaders.Add(headerEntity);
|
|
// await _context.SaveChangesAsync();
|
|
|
|
// // Guardado de Detalles
|
|
// if (quote.PhSQuoteDetails?.Any() == true)
|
|
// {
|
|
// foreach (var detail in quote.PhSQuoteDetails)
|
|
// {
|
|
// detail.QuoteheaderId = headerEntity.Id;
|
|
// var dbDetail = EntityMapper.MapEntity<EQuoteDetail, PhSQuoteDetail>(detail);
|
|
// _context.PhSQuoteDetails.Add(dbDetail);
|
|
// }
|
|
// }
|
|
|
|
// // Guardado de Roles
|
|
// if (quote.PhSQuoteRoles?.Any() == true)
|
|
// {
|
|
// foreach (var role in quote.PhSQuoteRoles)
|
|
// {
|
|
// role.QuoteheaderId = headerEntity.Id;
|
|
// var dbRole = EntityMapper.MapEntity<EQuoteRole, PhSQuoteRole>(role);
|
|
// _context.PhSQuoteRoles.Add(dbRole);
|
|
// }
|
|
// }
|
|
|
|
// // Guardado de Ajustes
|
|
// if (quote.PhSQuoteAdjustments?.Any() == true)
|
|
// {
|
|
// foreach (var adj in quote.PhSQuoteAdjustments)
|
|
// {
|
|
// adj.QuoteheaderId = headerEntity.Id;
|
|
// var dbAdj = EntityMapper.MapEntity<EQuoteAdjustment, PhSQuoteAdjustment>(adj);
|
|
// _context.PhSQuoteAdjustments.Add(dbAdj);
|
|
// }
|
|
// }
|
|
|
|
// // Guardado de Impuestos
|
|
// if (quote.PhSQuoteTaxes?.Any() == true)
|
|
// {
|
|
// foreach (var tax in quote.PhSQuoteTaxes)
|
|
// {
|
|
// tax.QuoteheaderId = headerEntity.Id;
|
|
// var dbTax = EntityMapper.MapEntity<EQuoteTax, PhSQuoteTaxis>(tax);
|
|
// _context.PhSQuoteTaxes.Add(dbTax);
|
|
// }
|
|
// }
|
|
|
|
// await _context.SaveChangesAsync();
|
|
// await transaction.CommitAsync();
|
|
|
|
// return headerEntity.Quotenumber;
|
|
// }
|
|
// catch
|
|
// {
|
|
// await transaction.RollbackAsync();
|
|
// throw;
|
|
// }
|
|
//}
|
|
//public async Task<IDbContextTransaction> BeginTransactionAsync()
|
|
//{
|
|
// return await _context.Database.BeginTransactionAsync();
|
|
//}
|
|
#endregion
|
|
}
|
|
} |