ALTER PROCEDURE [dbo].[sp_invCierreTomaTemp_Ins]
    @codbus VARCHAR(50),
    @nomart VARCHAR(100),
    @codAlt VARCHAR(50),
    @ubiinv1 VARCHAR(50),
    @stock_logico DECIMAL(18,4),
    @stock_fisico DECIMAL(18,4),
    @stock_dif DECIMAL(18,4),
    @contado VARCHAR(50),
    @fecha_toma DATETIME,
    @bodega INT,
    @contabilizado VARCHAR(50),
    @numlin INT,
    @idPretoma INT
AS
BEGIN
	IF EXISTS (SELECT * FROM invCierreTomaTemp WHERE codbus = @codbus)
		BEGIN
			DECLARE @stock_F DECIMAL
			DECLARE @stock_D DECIMAL

			SET @stock_F = (SELECT stock_fisico FROM invCierreTomaTemp WHERE codbus = @codbus) + @stock_fisico
			SET @stock_D = @stock_F - @stock_logico

			UPDATE invCierreTomaTemp 
			SET stock_fisico = @stock_F,
			stock_dif = @stock_D
			WHERE codbus = @codbus
		END
	ELSE
		BEGIN
			INSERT INTO invCierreTomaTemp (
				codbus, nomart, codAlt, ubiinv1, stock_logico, stock_fisico, stock_dif, 
				contado, fecha_toma, bodega, contabilizado, numlin, idPretoma
			) 
			VALUES (
				@codbus, @nomart, @codAlt, @ubiinv1, @stock_logico, @stock_fisico, @stock_dif, 
				@contado, @fecha_toma, @bodega, @contabilizado, @numlin, @idPretoma
			);
		END
END;