ALTER PROCEDURE  [dbo].[sp_InvCierreTomaInsertarLinea]
 (
 @codToma      INT,
 @codBus       VARCHAR(30),
 @bodega       INT,
 @fechaToma    DATETIME,
 @stock_Fisico DECIMAL(18,4), 
 @stock_Logico DECIMAL(18,4),
 @contado      BIT,
 @numlin	   INT
 )

AS

IF NOT EXISTS (SELECT * FROM invCierreToma WHERE codToma=@codToma AND codbus=@codbus) 
BEGIN  
  DECLARE @codfam    VARCHAR(10)
  DECLARE @codSub    VARCHAR(10)
  DECLARE @codBar    VARCHAR(50)
  DECLARE @nomart    VARCHAR(100)
  DECLARE @codAlt    VARCHAR(50)
  DECLARE @ubiinv1   VARCHAR(300)    
    
  SELECT @codfam=codfam, @codsub= codsub, @codbar=codbar, @nomart=nomart, @codAlt=codalt, @ubiinv1=ubiinv1  FROM invArticulos WHERE codbus=@codbus
     
  INSERT INTO invCierreToma (codToma,fecha_toma, codbus, bodega, nomart, codfam, codsub, codbar, stock_logico, stock_fisico, contado, codAlt, ubiinv1, numlin)
  VALUES(@codToma,@fechatoma, @codbus, @bodega,@nomart,@codfam, @codsub, @codbar, @stock_Logico, @stock_Fisico, @contado,@codAlt, @ubiinv1, @numlin) 
END
ELSE BEGIN
  UPDATE invCierreToma SET stock_Fisico= @stock_Fisico WHERE codToma= @codToma AND codBus=@codBus
END