Private
Public Access
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package factura
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
dto_directors "repuestera_back/dto/Builders/Directors"
|
|
r "repuestera_back/repositorios"
|
|
)
|
|
|
|
func ObtenerFacturas(c *gin.Context) {
|
|
filtros := r.FiltrosFactura{
|
|
Numero: c.Query("numero"),
|
|
Cliente: c.Query("cliente"),
|
|
Estado: c.Query("estado"),
|
|
}
|
|
|
|
if raw := c.Query("clienteId"); raw != "" {
|
|
id, err := strconv.ParseUint(raw, 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "clienteId inválido"})
|
|
return
|
|
}
|
|
filtros.ClienteID = uint(id)
|
|
}
|
|
|
|
desde, err := fechaQuery(c.Query("desde"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "desde inválido, se espera YYYY-MM-DD"})
|
|
return
|
|
}
|
|
hasta, err := fechaQuery(c.Query("hasta"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "hasta inválido, se espera YYYY-MM-DD"})
|
|
return
|
|
}
|
|
filtros.Desde = desde
|
|
filtros.Hasta = hasta
|
|
|
|
facturas, err := r.ObtenerRepositorios(c).Factura.Listar(filtros)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
respuesta, err := dto_directors.NewFacturaDirector().Listar(facturas)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, respuesta)
|
|
}
|
|
|
|
func fechaQuery(valor string) (*time.Time, error) {
|
|
if valor == "" {
|
|
return nil, nil
|
|
}
|
|
fecha, err := time.Parse("2006-01-02", valor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &fecha, nil
|
|
}
|