All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 3m24s
343 lines
12 KiB
C#
343 lines
12 KiB
C#
using Domain.Entities;
|
|
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));
|
|
}
|
|
|
|
#region Obtener Presupuestos
|
|
|
|
[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}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Crear / Actualizar / Eliminar
|
|
|
|
[HttpPost("create")]
|
|
public async Task<IActionResult> Create([FromBody] EQuoteHeader quote, [FromQuery] int formSeriesId)
|
|
{
|
|
try
|
|
{
|
|
if (quote == null)
|
|
return BadRequest("El presupuesto no puede ser nulo.");
|
|
|
|
var result = await _quoteService.CreateQuoteAsync(quote, formSeriesId);
|
|
return Ok(result);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return BadRequest($"Validación fallida: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return BadRequest($"Error de negocio: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[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}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Impuestos (QuoteTaxes)
|
|
|
|
[HttpGet("{quoteId:int}/taxes")]
|
|
public async Task<IActionResult> GetTaxes(int quoteId)
|
|
{
|
|
try
|
|
{
|
|
var taxes = await _quoteService.GetTaxesByQuoteIdAsync(quoteId);
|
|
return Ok(taxes);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPost("{quoteId:int}/taxes")]
|
|
public async Task<IActionResult> AddTax(int quoteId, [FromBody] EQuoteTax tax)
|
|
{
|
|
try
|
|
{
|
|
if (tax == null || quoteId != tax.QuoteheaderId)
|
|
return BadRequest("Datos inválidos para el impuesto.");
|
|
|
|
var result = await _quoteService.AddTaxAsync(tax);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPut("taxes")]
|
|
public async Task<IActionResult> UpdateTax([FromBody] EQuoteTax tax)
|
|
{
|
|
try
|
|
{
|
|
if (tax == null || tax.Id <= 0)
|
|
return BadRequest("Datos inválidos para actualizar el impuesto.");
|
|
|
|
await _quoteService.UpdateTaxAsync(tax);
|
|
return Ok("Impuesto actualizado correctamente.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpDelete("taxes/{taxId:int}")]
|
|
public async Task<IActionResult> DeleteTax(int taxId)
|
|
{
|
|
try
|
|
{
|
|
await _quoteService.DeleteTaxAsync(taxId);
|
|
return Ok("Impuesto eliminado correctamente.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Ajustes Comerciales (QuoteAdjustments)
|
|
|
|
[HttpGet("{quoteId:int}/adjustments")]
|
|
public async Task<IActionResult> GetAdjustments(int quoteId)
|
|
{
|
|
try
|
|
{
|
|
var adjustments = await _quoteService.GetAdjustmentsByQuoteIdAsync(quoteId);
|
|
return Ok(adjustments);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPost("{quoteId:int}/adjustments")]
|
|
public async Task<IActionResult> AddAdjustment(int quoteId, [FromBody] EQuoteAdjustment adjustment)
|
|
{
|
|
try
|
|
{
|
|
if (adjustment == null || quoteId != adjustment.QuoteheaderId)
|
|
return BadRequest("Datos inválidos para el ajuste.");
|
|
|
|
var result = await _quoteService.AddAdjustmentAsync(adjustment);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPut("adjustments")]
|
|
public async Task<IActionResult> UpdateAdjustment([FromBody] EQuoteAdjustment adjustment)
|
|
{
|
|
try
|
|
{
|
|
if (adjustment == null || adjustment.Id <= 0)
|
|
return BadRequest("Datos inválidos para actualizar el ajuste.");
|
|
|
|
await _quoteService.UpdateAdjustmentAsync(adjustment);
|
|
return Ok("Ajuste actualizado correctamente.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpDelete("adjustments/{adjustmentId:int}")]
|
|
public async Task<IActionResult> DeleteAdjustment(int adjustmentId)
|
|
{
|
|
try
|
|
{
|
|
await _quoteService.DeleteAdjustmentAsync(adjustmentId);
|
|
return Ok("Ajuste eliminado correctamente.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#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
|
|
|
|
#region Endpoint de emision de presupuesto (encabezado + detalles + roles + ajustes + impuestos)
|
|
[HttpPost("createfull")]
|
|
public async Task<IActionResult> CreateFullQuote([FromBody] EQuoteHeader quote, [FromQuery] int formSeriesId)
|
|
{
|
|
try
|
|
{
|
|
if (quote == null)
|
|
return BadRequest("El presupuesto no puede ser nulo.");
|
|
|
|
var quoteNumber = await _quoteService.CreateFullQuoteAsync(quote, formSeriesId);
|
|
return Ok(new { QuoteNumber = quoteNumber });
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return BadRequest($"Validación fallida: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return BadRequest($"Error de negocio: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |