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,30 +1,144 @@
|
||||
package stock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type resultadoTraslado struct {
|
||||
Origen *models.Stock
|
||||
Destino *models.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,
|
||||
})
|
||||
|
||||
resultado, err := trasladarStock(
|
||||
repositorios.BaseDeDatos(),
|
||||
repositorios.ObtenerRepositorios(c),
|
||||
utils.EmpresaID(c),
|
||||
c.GetUint("userID"),
|
||||
data,
|
||||
)
|
||||
if err != nil {
|
||||
responderError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resultado)
|
||||
}
|
||||
|
||||
// trasladarStock descuenta el origen y suma el destino en una sola transacción.
|
||||
func trasladarStock(db *gorm.DB, repo *repositorios.Repositorios, empresaID, usuarioID uint, entrada dto.TrasladoDto) (*resultadoTraslado, error) {
|
||||
if err := validarCantidad(entrada.Cantidad); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if entrada.RepuestoID == 0 || entrada.AlmacenOrigenID == 0 || entrada.AlmacenDestinoID == 0 {
|
||||
return nil, ErrStockNoEncontrado
|
||||
}
|
||||
if entrada.AlmacenOrigenID == entrada.AlmacenDestinoID {
|
||||
return nil, ErrMismoAlmacen
|
||||
}
|
||||
if err := validarObservacion(entrada.Observacion); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cantidad := redondearCantidad(entrada.Cantidad)
|
||||
var resultado resultadoTraslado
|
||||
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
origen, err := repo.Stock.BuscarPorRepuestoAlmacen(tx, entrada.RepuestoID, entrada.AlmacenOrigenID)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrStockNoEncontrado
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destino, errDestino := repo.Stock.BuscarPorRepuestoAlmacen(tx, entrada.RepuestoID, entrada.AlmacenDestinoID)
|
||||
existeDestino := errDestino == nil
|
||||
if errDestino != nil && !errors.Is(errDestino, gorm.ErrRecordNotFound) {
|
||||
return errDestino
|
||||
}
|
||||
|
||||
// Bloquear siempre en orden ascendente de ID para evitar deadlocks
|
||||
// cuando dos traslados cruzan el mismo par de filas.
|
||||
if existeDestino && destino.ID < origen.ID {
|
||||
destino, err = repo.Stock.BloquearPorID(tx, destino.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
origen, err = repo.Stock.BloquearPorID(tx, origen.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
origen, err = repo.Stock.BloquearPorID(tx, origen.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if existeDestino {
|
||||
destino, err = repo.Stock.BloquearPorID(tx, destino.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
destino, err = repo.Stock.BloquearOCrearPorRepuestoAlmacen(tx, entrada.RepuestoID, entrada.AlmacenDestinoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if disponible(origen) < cantidad {
|
||||
return fmt.Errorf("%w: disponible %.3f, solicitado %.3f", ErrStockInsuficiente, disponible(origen), cantidad)
|
||||
}
|
||||
|
||||
saldoOrigen := origen.Cantidad
|
||||
saldoDestino := destino.Cantidad
|
||||
|
||||
origen.Cantidad = redondearCantidad(origen.Cantidad - cantidad)
|
||||
destino.Cantidad = redondearCantidad(destino.Cantidad + cantidad)
|
||||
if err := repo.Stock.ActualizarCantidades(tx, origen); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := repo.Stock.ActualizarCantidades(tx, destino); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
referencia := fmt.Sprintf("Traslado %d → %d", entrada.AlmacenOrigenID, entrada.AlmacenDestinoID)
|
||||
if err := registrarMovimiento(tx, repo, usuarioID, origen, models.MovimientoTrasladoSalida, -cantidad, saldoOrigen, origen.Cantidad, "TRASLADO", entrada.RepuestoID, referencia); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := registrarMovimiento(tx, repo, usuarioID, destino, models.MovimientoTrasladoEntrada, cantidad, saldoDestino, destino.Cantidad, "TRASLADO", entrada.RepuestoID, referencia); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resultado = resultadoTraslado{Origen: origen, Destino: destino}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
registrarLog(repo, empresaID, usuarioID, "TRASLADO_STOCK", []models_logs.LogDetalle{
|
||||
{Columna: "repuestoId", ValorNuevo: fmt.Sprintf("%d", entrada.RepuestoID)},
|
||||
{Columna: "origenId", ValorNuevo: fmt.Sprintf("%d", entrada.AlmacenOrigenID)},
|
||||
{Columna: "destinoId", ValorNuevo: fmt.Sprintf("%d", entrada.AlmacenDestinoID)},
|
||||
{Columna: "cantidad", ValorNuevo: fmt.Sprintf("%.3f", cantidad)},
|
||||
})
|
||||
return &resultado, nil
|
||||
}
|
||||
Reference in new issue
Block a user