2025-05-05 22:50:02 -03:00
|
|
|
|
using Domain.Entities;
|
2025-04-27 02:19:29 -03:00
|
|
|
|
using Domain.Generics;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Models.Interfaces;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
|
|
namespace phronCare.API.Controllers.Sales
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
public class QuoteController : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IQuoteDom _quoteService;
|
|
|
|
|
|
public QuoteController(IQuoteDom quoteService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_quoteService = quoteService ?? throw new ArgumentNullException(nameof(quoteService));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-05 22:50:02 -03:00
|
|
|
|
#region Obtener Presupuestos
|
|
|
|
|
|
|
2025-04-27 02:19:29 -03:00
|
|
|
|
[HttpGet("all")]
|
|
|
|
|
|
public async Task<IActionResult> GetAll([FromQuery] int page = 1, [FromQuery] int pageSize = 50)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await _quoteService.GetAllQuotesAsync(page, pageSize);
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
|
|
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("search")]
|
|
|
|
|
|
public async Task<IActionResult> Search(
|
|
|
|
|
|
[FromQuery] int? customerId,
|
|
|
|
|
|
[FromQuery] string? quoteNumber,
|
|
|
|
|
|
[FromQuery] int? professionalId,
|
|
|
|
|
|
[FromQuery] int? institutionId,
|
|
|
|
|
|
[FromQuery] int? patientId,
|
|
|
|
|
|
[FromQuery] DateTime? issueDateFrom,
|
|
|
|
|
|
[FromQuery] DateTime? issueDateTo,
|
|
|
|
|
|
[FromQuery] string? status,
|
|
|
|
|
|
[FromQuery] int page = 1,
|
|
|
|
|
|
[FromQuery] int pageSize = 50)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await _quoteService.SearchQuotesAsync(
|
|
|
|
|
|
customerId,
|
|
|
|
|
|
quoteNumber,
|
|
|
|
|
|
professionalId,
|
|
|
|
|
|
institutionId,
|
|
|
|
|
|
patientId,
|
|
|
|
|
|
issueDateFrom,
|
|
|
|
|
|
issueDateTo,
|
|
|
|
|
|
status,
|
|
|
|
|
|
page,
|
|
|
|
|
|
pageSize);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
|
|
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id:int}")]
|
|
|
|
|
|
public async Task<ActionResult<EQuoteHeader>> GetById(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var quote = await _quoteService.GetQuoteByIdAsync(id);
|
|
|
|
|
|
if (quote == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(quote);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
|
|
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-05 22:50:02 -03:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Crear / Actualizar / Eliminar
|
|
|
|
|
|
|
2025-04-27 02:19:29 -03:00
|
|
|
|
[HttpPut("update")]
|
|
|
|
|
|
public async Task<IActionResult> Update([FromBody] EQuoteHeader quote)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (quote == null || quote.Id <= 0)
|
|
|
|
|
|
return BadRequest("El presupuesto es inválido o no tiene un ID válido.");
|
|
|
|
|
|
|
|
|
|
|
|
await _quoteService.UpdateQuoteAsync(quote);
|
|
|
|
|
|
return Ok("Presupuesto actualizado correctamente.");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
|
|
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("delete/{id:int}")]
|
|
|
|
|
|
public async Task<IActionResult> Delete(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _quoteService.DeleteQuoteAsync(id);
|
|
|
|
|
|
return Ok("Presupuesto eliminado correctamente.");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
|
|
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-05 22:50:02 -03:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-05-06 15:58:35 -03:00
|
|
|
|
|
|
|
|
|
|
#region Exportación
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("exportfiltered")]
|
|
|
|
|
|
public async Task<IActionResult> ExportFiltered([FromBody] QuoteSearchParams searchParams)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var file = await _quoteService.ExportFilteredQuotesToExcelAsync(searchParams);
|
|
|
|
|
|
return File(file,
|
|
|
|
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
|
|
|
|
"Presupuestos.xlsx");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-05-06 21:00:03 -03:00
|
|
|
|
#region Endpoint de emision de presupuesto (encabezado + detalles + roles + ajustes + impuestos)
|
|
|
|
|
|
[HttpPost("createfull")]
|
2025-05-07 18:35:49 -03:00
|
|
|
|
public async Task<IActionResult> CreateFullQuote([FromBody] CreateFullQuoteRequest request)
|
2025-05-06 21:00:03 -03:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-05-07 18:35:49 -03:00
|
|
|
|
// Validamos que el request y el objeto Quote no sean nulos
|
|
|
|
|
|
if (request == null || request.Quote == null)
|
|
|
|
|
|
return BadRequest("El payload no puede contener elementos nulos.");
|
2025-05-06 21:00:03 -03:00
|
|
|
|
|
2025-05-07 18:35:49 -03:00
|
|
|
|
// Desempaquetamos los datos
|
|
|
|
|
|
var quote = request.Quote;
|
|
|
|
|
|
var formSeriesId = request.FormSeriesId;
|
|
|
|
|
|
|
|
|
|
|
|
// Llamada al servicio de negocio
|
2025-05-06 21:00:03 -03:00
|
|
|
|
var quoteNumber = await _quoteService.CreateFullQuoteAsync(quote, formSeriesId);
|
2025-05-07 18:35:49 -03:00
|
|
|
|
|
|
|
|
|
|
// Devolvemos el número generado
|
|
|
|
|
|
return Ok(new { Success = true, QuoteNumber = quoteNumber });
|
2025-05-06 21:00:03 -03:00
|
|
|
|
}
|
|
|
|
|
|
catch (ArgumentNullException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest($"Validación fallida: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest($"Error de negocio: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-05-07 18:35:49 -03:00
|
|
|
|
return StatusCode(500, $"Ocurrió un error interno: {ex.Message}");
|
2025-05-06 21:00:03 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-07 18:35:49 -03:00
|
|
|
|
|
|
|
|
|
|
public class CreateFullQuoteRequest
|
|
|
|
|
|
{
|
|
|
|
|
|
public EQuoteHeader Quote { get; set; } = default!;
|
|
|
|
|
|
public int FormSeriesId { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-06 21:00:03 -03:00
|
|
|
|
#endregion
|
2025-04-27 02:19:29 -03:00
|
|
|
|
}
|
2025-05-06 15:58:35 -03:00
|
|
|
|
}
|