All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m39s
with Validations
95 lines
4.2 KiB
C#
95 lines
4.2 KiB
C#
using Domain.Entities;
|
|
using Models.Helpers;
|
|
using Models.Interfaces;
|
|
using Models.Models;
|
|
|
|
namespace Models.Repositories
|
|
{
|
|
public class PhSQuoteRepository(PhronCareOperationsHubContext context,
|
|
IPhSFormSeriesRepository formSeriesRepository) : IPhSQuoteRepository
|
|
{
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|
private readonly IPhSFormSeriesRepository _formSeriesRepository = formSeriesRepository;
|
|
|
|
#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.
|
|
/// <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>
|
|
/// </summary>
|
|
public async Task<string> CreateFullQuoteAsync(EQuoteHeader quote, int formSeriesId)
|
|
{
|
|
using var transaction = await _context.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
var nextNumber = await _formSeriesRepository.GetNextInternalNumberAsync(formSeriesId);
|
|
var series = await _formSeriesRepository.GetByIdAsync(formSeriesId)
|
|
?? throw new InvalidOperationException("Serie no encontrada");
|
|
int padding = 8; /* format "00000000" */
|
|
|
|
quote.Quotenumber = $"{series.Letter}-{nextNumber.ToString($"D{padding}")}";
|
|
|
|
var headerEntity = EntityMapper.MapEntity<EQuoteHeader, PhSQuoteHeader>(quote);
|
|
_context.PhSQuoteHeaders.Add(headerEntity);
|
|
|
|
#region Nota: Esta seccion queda para futura modificacion en caso de cambiar CASCADE INSERT de las entidades relacionadas
|
|
//// 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);
|
|
// }
|
|
//}
|
|
#endregion
|
|
|
|
await _context.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
return headerEntity.Quotenumber;
|
|
}
|
|
catch
|
|
{
|
|
await transaction.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |