Leandro Hernan Rojas f1bc764bbf
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 6m18s
Add Adjustment, Products & Tags.
Quote Demo v1
2025-05-05 22:50:02 -03:00

246 lines
8.0 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 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 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
}
}