104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
|
|
using Core.Interfaces;
|
||
|
|
using Domain.Dtos.Sales;
|
||
|
|
using Domain.Generics;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using System.Reflection;
|
||
|
|
|
||
|
|
namespace phronCare.API.Controllers.Sales
|
||
|
|
{
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
[ApiController]
|
||
|
|
public class SalesDocumentController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ISalesDocumentDom _salesDocumentService;
|
||
|
|
|
||
|
|
public SalesDocumentController(ISalesDocumentDom salesDocumentService)
|
||
|
|
{
|
||
|
|
_salesDocumentService = salesDocumentService ?? throw new ArgumentNullException(nameof(salesDocumentService));
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("search")]
|
||
|
|
public async Task<ActionResult<PagedResult<SalesDocumentSummaryDto>>> Search(
|
||
|
|
[FromQuery] int? customerId,
|
||
|
|
[FromQuery] string? customerText,
|
||
|
|
[FromQuery] int? quoteId,
|
||
|
|
[FromQuery] int? documentType,
|
||
|
|
[FromQuery] int? status,
|
||
|
|
[FromQuery] DateTime? issueDateFrom,
|
||
|
|
[FromQuery] DateTime? issueDateTo,
|
||
|
|
[FromQuery] int page = 1,
|
||
|
|
[FromQuery] int pageSize = 50)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = await _salesDocumentService.SearchAsync(
|
||
|
|
customerId,
|
||
|
|
customerText,
|
||
|
|
quoteId,
|
||
|
|
documentType,
|
||
|
|
status,
|
||
|
|
issueDateFrom,
|
||
|
|
issueDateTo,
|
||
|
|
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<SalesDocumentDto>> GetById(int id)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var salesDocument = await _salesDocumentService.GetDtoByIdAsync(id);
|
||
|
|
if (salesDocument == null)
|
||
|
|
return NotFound($"Sales Document con ID {id} no encontrado.");
|
||
|
|
|
||
|
|
return Ok(salesDocument);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
||
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<ActionResult<SalesDocumentDto>> Create([FromBody] SalesDocumentCreateRequest request)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (request == null)
|
||
|
|
return BadRequest("El payload no puede ser nulo.");
|
||
|
|
|
||
|
|
var created = await _salesDocumentService.CreateAsync(request);
|
||
|
|
var salesDocument = await _salesDocumentService.GetDtoByIdAsync(created.Id);
|
||
|
|
|
||
|
|
if (salesDocument == null)
|
||
|
|
return StatusCode(500, $"No se pudo recuperar el Sales Document creado con ID {created.Id}.");
|
||
|
|
|
||
|
|
return CreatedAtAction(nameof(GetById), new { id = salesDocument.Id }, salesDocument);
|
||
|
|
}
|
||
|
|
catch (ArgumentException ex)
|
||
|
|
{
|
||
|
|
return BadRequest(ex.Message);
|
||
|
|
}
|
||
|
|
catch (InvalidOperationException ex)
|
||
|
|
{
|
||
|
|
return BadRequest(ex.Message);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
||
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|