Private
Public Access
lo que es back esta correjido
This commit is contained in:
1 parent
6ed8805e21
commit
9205fea8d8
23 files changed
+486
No files matched your search
@@ -0,0 +1,28 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
)
|
||||
|
||||
func ActualizarObservacion(c *gin.Context) {
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var data dto.ObservacionDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resultado, err := nuevoServicio(c).Comentar(id, data.Observacion)
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resultado)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func AjustarStock(c *gin.Context) {
|
||||
var data dto.AjusteDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
resultado, err := nuevoServicio(c).Ajustar(stocksvc.EntradaAjuste{
|
||||
StockID: data.StockID,
|
||||
CantidadReal: data.CantidadReal,
|
||||
Motivo: data.Motivo,
|
||||
Observacion: data.Observacion,
|
||||
})
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resultado)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func IngresarStock(c *gin.Context) {
|
||||
var data dto.IngresoDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]stocksvc.ItemIngreso, 0, len(data.Items))
|
||||
for _, it := range data.Items {
|
||||
items = append(items, stocksvc.ItemIngreso{
|
||||
RepuestoID: it.RepuestoID,
|
||||
CantidadEsperada: it.CantidadEsperada,
|
||||
CantidadRecibida: it.CantidadRecibida,
|
||||
UnidadMedida: it.UnidadMedida,
|
||||
Observacion: it.Observacion,
|
||||
})
|
||||
}
|
||||
resultado, err := nuevoServicio(c).Ingresar(stocksvc.EntradaIngreso{
|
||||
ProveedorID: data.ProveedorID,
|
||||
Numero: data.Numero,
|
||||
FechaEmision: data.FechaEmision,
|
||||
AlmacenDestinoID: data.AlmacenDestinoID,
|
||||
Observacion: data.Observacion,
|
||||
Items: items,
|
||||
})
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, resultado)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func LiberarReserva(c *gin.Context) {
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resultado, err := nuevoServicio(c).Liberar(uint(id))
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resultado)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
func ObtenerReservas(c *gin.Context) {
|
||||
filtros := r.FiltrosReserva{}
|
||||
_ = filtros
|
||||
|
||||
reservas, err := r.ObtenerRepositorios(c).Reserva.ListarActivas(filtros)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, reservas)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/models"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func ObtenerStock(c *gin.Context) {
|
||||
var q dto.FiltrosStockQuery
|
||||
if err := c.ShouldBindQuery(&q); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
filtros := r.FiltrosStock{
|
||||
RepuestoID: q.RepuestoID,
|
||||
AlmacenID: q.AlmacenID,
|
||||
Texto: q.Texto,
|
||||
SoloBajoMinimo: q.SoloBajoMinimo,
|
||||
Pagina: q.Pagina,
|
||||
PorPagina: q.PorPagina,
|
||||
}
|
||||
stocks, err := r.ObtenerRepositorios(c).Stock.Listar(filtros)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if q.Estado != "" {
|
||||
filtrado := make([]models.Stock, 0, len(stocks))
|
||||
for i := range stocks {
|
||||
estado := string(stocksvc.EstadoStock(&stocks[i], &stocks[i].Repuesto))
|
||||
if strings.EqualFold(estado, q.Estado) {
|
||||
filtrado = append(filtrado, stocks[i])
|
||||
}
|
||||
}
|
||||
stocks = filtrado
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, stocks)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
func ObtenerStockPorID(c *gin.Context) {
|
||||
uid, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || uid == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "id inválido"})
|
||||
return
|
||||
}
|
||||
stock, err := r.ObtenerRepositorios(c).Stock.ObtenerPorID(uint(uid))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "stock no encontrado"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, stock)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func RegistrarDevolucion(c *gin.Context) {
|
||||
var data dto.DevolucionDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]stocksvc.ItemDevolucion, 0, len(data.Items))
|
||||
for _, it := range data.Items {
|
||||
items = append(items, stocksvc.ItemDevolucion{
|
||||
RepuestoID: it.RepuestoID,
|
||||
Cantidad: it.Cantidad,
|
||||
PrecioUnitario: it.PrecioUnitario,
|
||||
})
|
||||
}
|
||||
resultado, err := nuevoServicio(c).Devolver(stocksvc.EntradaDevolucion{
|
||||
FacturaID: data.FacturaID,
|
||||
AlmacenID: data.AlmacenID,
|
||||
Motivo: data.Motivo,
|
||||
Items: items,
|
||||
})
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, resultado)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func ReservarStock(c *gin.Context) {
|
||||
var data dto.ReservaDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
resultado, err := nuevoServicio(c).Reservar(stocksvc.EntradaReserva{
|
||||
RepuestoID: data.RepuestoID,
|
||||
AlmacenID: data.AlmacenID,
|
||||
Cantidad: data.Cantidad,
|
||||
ClienteID: data.ClienteID,
|
||||
FechaVencimiento: data.FechaVencimiento,
|
||||
Observacion: data.Observacion,
|
||||
})
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, resultado)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func TrasladarStock(c *gin.Context) {
|
||||
var data dto.TrasladoDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
resultado, err := nuevoServicio(c).Trasladar(stocksvc.EntradaTraslado{
|
||||
RepuestoID: data.RepuestoID,
|
||||
AlmacenOrigenID: data.AlmacenOrigenID,
|
||||
AlmacenDestinoID: data.AlmacenDestinoID,
|
||||
Cantidad: data.Cantidad,
|
||||
Observacion: data.Observacion,
|
||||
})
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resultado)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"repuestera_back/services/stock"
|
||||
)
|
||||
|
||||
func codigoHTTP(err error) int {
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
return http.StatusNotFound
|
||||
case errors.Is(err, stock.ErrStockNoEncontrado),
|
||||
errors.Is(err, stock.ErrReservaInexistente),
|
||||
errors.Is(err, stock.ErrItemNoEncontrado),
|
||||
errors.Is(err, stock.ErrAlmacenInvalido),
|
||||
errors.Is(err, stock.ErrProveedorNoEncontrado),
|
||||
errors.Is(err, stock.ErrRepuestoNoEncontrado),
|
||||
errors.Is(err, stock.ErrSinAlmacen):
|
||||
return http.StatusNotFound
|
||||
case errors.Is(err, stock.ErrReservaYaLiberada),
|
||||
errors.Is(err, stock.ErrFacturaAnulada),
|
||||
errors.Is(err, stock.ErrItemDuplicado),
|
||||
errors.Is(err, stock.ErrRemitoDuplicado):
|
||||
return http.StatusConflict
|
||||
case errors.Is(err, stock.ErrComentarioLargo),
|
||||
errors.Is(err, stock.ErrComentarioVacio),
|
||||
errors.Is(err, stock.ErrMotivoRequerido),
|
||||
errors.Is(err, stock.ErrCantidadInvalida),
|
||||
errors.Is(err, stock.ErrFechaVencimiento),
|
||||
errors.Is(err, stock.ErrAjusteBajoReserva):
|
||||
return http.StatusBadRequest
|
||||
case errors.Is(err, stock.ErrStockInsuficiente),
|
||||
errors.Is(err, stock.ErrMismoAlmacen),
|
||||
errors.Is(err, stock.ErrDevolucionExcedeVendido):
|
||||
return http.StatusUnprocessableEntity
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
func responderError(c *gin.Context, err error) {
|
||||
c.JSON(codigoHTTP(err), gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
func parseUintParam(c *gin.Context, name string) (uint64, error) {
|
||||
uid, err := strconv.ParseUint(c.Param(name), 10, 64)
|
||||
if err != nil || uid == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "id inválido"})
|
||||
return 0, err
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func nuevoServicio(c *gin.Context) *stock.Servicio {
|
||||
return stock.Nuevo(c)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package dto
|
||||
|
||||
type AjusteDto struct {
|
||||
StockID uint64 `json:"stockId" binding:"required"`
|
||||
CantidadReal float64 `json:"cantidadReal" binding:"required"`
|
||||
Motivo string `json:"motivo" binding:"required"`
|
||||
Observacion string `json:"observacion"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package dto
|
||||
|
||||
type DevolucionDto struct {
|
||||
FacturaID uint `json:"facturaId" binding:"required"`
|
||||
AlmacenID uint `json:"almacenId"`
|
||||
Motivo string `json:"motivo" binding:"required"`
|
||||
Items []ItemDevolucionDto `json:"items" binding:"required,min=1"`
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dto
|
||||
|
||||
type FiltrosStockQuery struct {
|
||||
RepuestoID uint64 `form:"repuestoId"`
|
||||
AlmacenID uint `form:"almacenId"`
|
||||
Texto string `form:"q"`
|
||||
SoloBajoMinimo bool `form:"soloBajoMinimo"`
|
||||
Estado string `form:"estado"`
|
||||
Pagina int `form:"pagina"`
|
||||
PorPagina int `form:"porPagina"`
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dto
|
||||
|
||||
import "time"
|
||||
|
||||
type IngresoDto struct {
|
||||
ProveedorID uint `json:"proveedorId" binding:"required"`
|
||||
Numero string `json:"numero" binding:"required"`
|
||||
FechaEmision time.Time `json:"fechaEmision" binding:"required"`
|
||||
AlmacenDestinoID uint `json:"almacenDestinoId" binding:"required"`
|
||||
Observacion string `json:"observacion"`
|
||||
Items []ItemIngresoDto `json:"items" binding:"required,min=1"`
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package dto
|
||||
|
||||
type ItemDevolucionDto struct {
|
||||
RepuestoID uint64 `json:"repuestoId" binding:"required"`
|
||||
Cantidad float64 `json:"cantidad" binding:"required"`
|
||||
PrecioUnitario float64 `json:"precioUnitario"`
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package dto
|
||||
|
||||
type ItemIngresoDto struct {
|
||||
RepuestoID uint64 `json:"repuestoId" binding:"required"`
|
||||
CantidadEsperada float64 `json:"cantidadEsperada" binding:"required"`
|
||||
CantidadRecibida float64 `json:"cantidadRecibida" binding:"required"`
|
||||
UnidadMedida string `json:"unidadMedida"`
|
||||
Observacion string `json:"observacion"`
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type LiberarReservaDto struct {
|
||||
// sin body
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type ObservacionDto struct {
|
||||
Observacion string `json:"observacion" binding:"required"`
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dto
|
||||
|
||||
import "time"
|
||||
|
||||
type ReservaDto struct {
|
||||
RepuestoID uint64 `json:"repuestoId" binding:"required"`
|
||||
AlmacenID uint `json:"almacenId" binding:"required"`
|
||||
Cantidad float64 `json:"cantidad" binding:"required"`
|
||||
ClienteID *uint `json:"clienteId"`
|
||||
FechaVencimiento *time.Time `json:"fechaVencimiento"`
|
||||
Observacion string `json:"observacion"`
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package dto
|
||||
|
||||
type TrasladoDto struct {
|
||||
RepuestoID uint64 `json:"repuestoId" binding:"required"`
|
||||
AlmacenOrigenID uint `json:"almacenOrigenId" binding:"required"`
|
||||
AlmacenDestinoID uint `json:"almacenDestinoId" binding:"required"`
|
||||
Cantidad float64 `json:"cantidad" binding:"required"`
|
||||
Observacion string `json:"observacion"`
|
||||
}
|
||||
@@ -88,6 +88,7 @@ func main() {
|
||||
routes.ProveedorRoute(r)
|
||||
routes.ClienteRoute(r)
|
||||
routes.FacturaRoute(r)
|
||||
routes.StockRoute(r)
|
||||
|
||||
r.Run(":8080")
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
stock "repuestera_back/controllers/stock"
|
||||
"repuestera_back/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func StockRoute(r *gin.Engine) {
|
||||
grupo := r.Group("/stock")
|
||||
grupo.Use(middleware.AuthMiddleware(9))
|
||||
{
|
||||
grupo.GET("/", stock.ObtenerStock)
|
||||
grupo.GET("/:id", stock.ObtenerStockPorID)
|
||||
grupo.PATCH("/:id/observacion", stock.ActualizarObservacion)
|
||||
|
||||
grupo.POST("/traslados", stock.TrasladarStock)
|
||||
grupo.POST("/ajustes", stock.AjustarStock)
|
||||
grupo.POST("/ingresos", stock.IngresarStock)
|
||||
|
||||
grupo.GET("/reservas", stock.ObtenerReservas)
|
||||
grupo.POST("/reservas", stock.ReservarStock)
|
||||
grupo.PATCH("/reservas/:id/liberar", stock.LiberarReserva)
|
||||
|
||||
grupo.POST("/devoluciones", stock.RegistrarDevolucion)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user