Private
Public Access
100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package stock
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
dto "repuestera_back/dto/Stock"
|
|
"repuestera_back/models"
|
|
models_logs "repuestera_back/models/logs"
|
|
"repuestera_back/repositorios"
|
|
"repuestera_back/utils"
|
|
)
|
|
|
|
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 := ajustarStock(
|
|
repositorios.BaseDeDatos(),
|
|
repositorios.ObtenerRepositorios(c),
|
|
utils.EmpresaID(c),
|
|
c.GetUint("userID"),
|
|
data,
|
|
)
|
|
if err != nil {
|
|
responderError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, resultado)
|
|
}
|
|
|
|
// ajustarStock deja el stock en la cantidad real indicada y registra la diferencia.
|
|
func ajustarStock(db *gorm.DB, repo *repositorios.Repositorios, empresaID, usuarioID uint, entrada dto.AjusteDto) (*models.Stock, error) {
|
|
if entrada.CantidadReal < 0 || math.IsNaN(entrada.CantidadReal) || math.IsInf(entrada.CantidadReal, 0) {
|
|
return nil, ErrCantidadInvalida
|
|
}
|
|
if strings.TrimSpace(entrada.Motivo) == "" {
|
|
return nil, ErrMotivoRequerido
|
|
}
|
|
if err := validarObservacion(entrada.Observacion); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cantidadReal := redondearCantidad(entrada.CantidadReal)
|
|
var resultado *models.Stock
|
|
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
stock, err := repo.Stock.BloquearPorID(tx, entrada.StockID)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return ErrStockNoEncontrado
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if cantidadReal < stock.CantidadReservada {
|
|
return fmt.Errorf("%w: reservado %.3f", ErrAjusteBajoReserva, stock.CantidadReservada)
|
|
}
|
|
|
|
saldoAnterior := stock.Cantidad
|
|
diferencia := redondearCantidad(cantidadReal - saldoAnterior)
|
|
stock.Cantidad = cantidadReal
|
|
if err := repo.Stock.ActualizarCantidades(tx, stock); err != nil {
|
|
return err
|
|
}
|
|
|
|
observacion := strings.TrimSpace(strings.Join([]string{entrada.Motivo, entrada.Observacion}, " - "))
|
|
if err := registrarMovimiento(tx, repo, usuarioID, stock, models.MovimientoAjuste, diferencia, saldoAnterior, stock.Cantidad, "AJUSTE", stock.ID, observacion); err != nil {
|
|
return err
|
|
}
|
|
|
|
if diferencia != 0 {
|
|
if err := asientoAjuste(tx, repo, stock, diferencia); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
resultado = stock
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
registrarLog(repo, empresaID, usuarioID, "AJUSTE_STOCK", []models_logs.LogDetalle{
|
|
{NombreTabla: "stock", Columna: "stockId", ValorNuevo: fmt.Sprintf("%d", entrada.StockID)},
|
|
{NombreTabla: "stock", Columna: "cantidadReal", ValorNuevo: fmt.Sprintf("%.3f", cantidadReal)},
|
|
{NombreTabla: "stock", Columna: "motivo", ValorNuevo: entrada.Motivo},
|
|
})
|
|
return resultado, nil
|
|
}
|