package dto_builders import ( "errors" "time" dto_stock "repuestera_back/dto/Stock" "repuestera_back/models" ) // MovimientoStockBuilder construye y valida el registro auditable de una // variación de stock. type MovimientoStockBuilder struct { datos *dto_stock.MovimientoStockDto err error } var _ IBuilder[models.MovimientoStock] = (*MovimientoStockBuilder)(nil) func NewMovimientoStockBuilder() *MovimientoStockBuilder { return &MovimientoStockBuilder{} } func (b *MovimientoStockBuilder) ConDatos(datos dto_stock.MovimientoStockDto) *MovimientoStockBuilder { b.datos = &datos return b } func (b *MovimientoStockBuilder) Build() (*models.MovimientoStock, error) { if b.datos == nil { return nil, errors.New("faltan los datos del movimiento de stock") } datos := b.datos if datos.StockID == 0 { AcumularError(&b.err, "el stock es obligatorio") } if datos.RepuestoID == 0 { AcumularError(&b.err, "el repuesto es obligatorio") } if datos.AlmacenID == 0 { AcumularError(&b.err, "el almacén es obligatorio") } if datos.Tipo == "" { AcumularError(&b.err, "el tipo de movimiento es obligatorio") } if datos.Cantidad <= 0 { AcumularError(&b.err, "la cantidad del movimiento debe ser mayor a cero") } if datos.SaldoAnterior < 0 { AcumularError(&b.err, "el saldo anterior no puede ser negativo") } if datos.SaldoPosterior < 0 { AcumularError(&b.err, "el saldo posterior no puede ser negativo") } if datos.UsuarioID == 0 { AcumularError(&b.err, "el usuario es obligatorio") } if b.err != nil { return nil, b.err } referenciaTipo, _ := ValidarTexto(datos.ReferenciaTipo, "el tipo de referencia", &b.err) if b.err != nil { return nil, b.err } fecha := datos.Fecha if fecha.IsZero() { fecha = time.Now() } return &models.MovimientoStock{ StockID: datos.StockID, RepuestoID: datos.RepuestoID, AlmacenID: datos.AlmacenID, Tipo: datos.Tipo, Cantidad: datos.Cantidad, SaldoAnterior: datos.SaldoAnterior, SaldoPosterior: datos.SaldoPosterior, ReferenciaTipo: referenciaTipo, ReferenciaID: datos.ReferenciaID, UsuarioID: datos.UsuarioID, Observacion: datos.Observacion, Fecha: fecha, }, nil }