using Domain.Entities; using Domain.Generics; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using Models.Helpers; using Models.Interfaces; using Models.Models; namespace PhronCare.Core.Data.Repositories.Sales { public class PhSQuoteHeaderRepository(PhronCareOperationsHubContext context, IPhSFormSeriesRepository formSeriesRepository) : IPhSQuoteHeaderRepository { private readonly PhronCareOperationsHubContext _context = context; private readonly IPhSFormSeriesRepository _formSeriesRepository = formSeriesRepository; public async Task> 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 { Items = pagedEntities.Items.Select(EntityMapper.MapEntity), TotalItems = pagedEntities.TotalItems, Page = pagedEntities.Page, PageSize = pagedEntities.PageSize }; } public async Task 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(entity) : null; } public async Task> 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); } public async Task> 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 { Items = pagedEntities.Items.Select(EntityMapper.MapEntity), TotalItems = pagedEntities.TotalItems, Page = pagedEntities.Page, PageSize = pagedEntities.PageSize }; } public async Task AddAsync(EQuoteHeader quoteHeader) { var dbEntity = EntityMapper.MapEntity(quoteHeader); _context.PhSQuoteHeaders.Add(dbEntity); await _context.SaveChangesAsync(); return EntityMapper.MapEntity(dbEntity); } public async Task UpdateAsync(EQuoteHeader quoteHeader) { var dbEntity = EntityMapper.MapEntity(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> GetAdjustmentsByQuoteIdAsync(int quoteId) { var adjustments = await _context.PhSQuoteAdjustments .Where(a => a.QuoteheaderId == quoteId) .ToListAsync(); return adjustments.Select(EntityMapper.MapEntity); } public async Task AddAdjustmentAsync(EQuoteAdjustment adjustment) { var dbEntity = EntityMapper.MapEntity(adjustment); _context.PhSQuoteAdjustments.Add(dbEntity); await _context.SaveChangesAsync(); return EntityMapper.MapEntity(dbEntity); } public async Task UpdateAdjustmentAsync(EQuoteAdjustment adjustment) { var dbEntity = EntityMapper.MapEntity(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 // ---------------------------- /// /// Obtiene todos los impuestos asociados a un presupuesto dado por su ID. /// public async Task> GetTaxesByQuoteIdAsync(int quoteId) { var taxes = await _context.PhSQuoteTaxes .Where(t => t.QuoteheaderId == quoteId) .ToListAsync(); return taxes.Select(EntityMapper.MapEntity); } /// /// Agrega un nuevo impuesto al presupuesto correspondiente. /// public async Task AddTaxAsync(EQuoteTax tax) { var dbEntity = EntityMapper.MapEntity(tax); _context.PhSQuoteTaxes.Add(dbEntity); await _context.SaveChangesAsync(); return EntityMapper.MapEntity(dbEntity); } /// /// Actualiza los datos de un impuesto existente en un presupuesto. /// public async Task UpdateTaxAsync(EQuoteTax tax) { var dbEntity = EntityMapper.MapEntity(tax); _context.PhSQuoteTaxes.Update(dbEntity); await _context.SaveChangesAsync(); } /// /// Elimina un impuesto asociado a un presupuesto a partir de su ID. /// 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) /// /// 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. /// /// Presupuesto a registrar, incluyendo entidades relacionadas. /// Identificador de la serie de numeración a utilizar. /// Cadena con el número generado del presupuesto. public async Task 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(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(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(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(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(tax); _context.PhSQuoteTaxes.Add(dbTax); } } await _context.SaveChangesAsync(); await transaction.CommitAsync(); return headerEntity.Quotenumber; } catch { await transaction.RollbackAsync(); throw; } } public async Task BeginTransactionAsync() { return await _context.Database.BeginTransactionAsync(); } #endregion } }