Private
Public Access
falta testeo
This commit is contained in:
1 parent
9205fea8d8
commit
61ca620c8e
50 files changed
+3061
-1112
No files matched your search
@@ -1,12 +1,19 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
dto "repuestera_back/dto/Stock"
|
||||
stocksvc "repuestera_back/services/stock"
|
||||
"repuestera_back/models"
|
||||
models_logs "repuestera_back/models/logs"
|
||||
"repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
)
|
||||
|
||||
func ReservarStock(c *gin.Context) {
|
||||
@@ -15,14 +22,14 @@ func ReservarStock(c *gin.Context) {
|
||||
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,
|
||||
})
|
||||
|
||||
resultado, err := reservarStock(
|
||||
repositorios.BaseDeDatos(),
|
||||
repositorios.ObtenerRepositorios(c),
|
||||
utils.EmpresaID(c),
|
||||
c.GetUint("userID"),
|
||||
data,
|
||||
)
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
@@ -30,3 +37,68 @@ func ReservarStock(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, resultado)
|
||||
}
|
||||
|
||||
// reservarStock bloquea stock, incrementa lo reservado y crea la reserva activa.
|
||||
func reservarStock(db *gorm.DB, repo *repositorios.Repositorios, empresaID, usuarioID uint, entrada dto.ReservaDto) (*models.Reserva, error) {
|
||||
if err := validarCantidad(entrada.Cantidad); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validarObservacion(entrada.Observacion); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if entrada.FechaVencimiento != nil && !entrada.FechaVencimiento.After(time.Now()) {
|
||||
return nil, ErrFechaVencimiento
|
||||
}
|
||||
if _, err := repo.Cliente.ObtenerPorID(entrada.ClienteID); err != nil {
|
||||
return nil, fmt.Errorf("%w: cliente %d", ErrClienteNoEncontrado, entrada.ClienteID)
|
||||
}
|
||||
|
||||
cantidad := redondearCantidad(entrada.Cantidad)
|
||||
var reserva *models.Reserva
|
||||
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
stock, err := repo.Stock.BloquearPorRepuestoAlmacen(tx, entrada.RepuestoID, entrada.AlmacenID)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrStockNoEncontrado
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if disponible(stock) < cantidad {
|
||||
return fmt.Errorf("%w: disponible %.3f, solicitado %.3f", ErrStockInsuficiente, disponible(stock), cantidad)
|
||||
}
|
||||
|
||||
saldoReservado := stock.CantidadReservada
|
||||
stock.CantidadReservada = redondearCantidad(stock.CantidadReservada + cantidad)
|
||||
if err := repo.Stock.ActualizarCantidades(tx, stock); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clienteID := entrada.ClienteID
|
||||
reserva = &models.Reserva{
|
||||
RepuestoID: uint(entrada.RepuestoID),
|
||||
AlmacenID: entrada.AlmacenID,
|
||||
Cantidad: cantidad,
|
||||
Estado: models.ReservaActiva,
|
||||
ClienteID: &clienteID,
|
||||
FechaVencimiento: entrada.FechaVencimiento,
|
||||
CreadoPorID: usuarioID,
|
||||
Observacion: entrada.Observacion,
|
||||
}
|
||||
if err := repo.Reserva.CrearTx(tx, reserva); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return registrarMovimiento(tx, repo, usuarioID, stock, models.MovimientoReserva, cantidad, saldoReservado, stock.CantidadReservada, "RESERVA", uint64(reserva.ID), entrada.Observacion)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
registrarLog(repo, empresaID, usuarioID, "RESERVAR_STOCK", []models_logs.LogDetalle{
|
||||
{Columna: "reservaId", ValorNuevo: fmt.Sprintf("%d", reserva.ID)},
|
||||
{Columna: "repuestoId", ValorNuevo: fmt.Sprintf("%d", entrada.RepuestoID)},
|
||||
{Columna: "almacenId", ValorNuevo: fmt.Sprintf("%d", entrada.AlmacenID)},
|
||||
{Columna: "cantidad", ValorNuevo: fmt.Sprintf("%.3f", cantidad)},
|
||||
})
|
||||
return reserva, nil
|
||||
}
|
||||
Reference in new issue
Block a user