drop proc in_actualizatoma_CAT_proc
go
	    
create proc in_actualizatoma_CAT_proc
@lista varchar(8000)
as

declare @cadena_ins varchar(7950)
declare @cadena_del varchar(7950)
declare @fechainicio as smalldatetime		--fecha inicion de inventario
declare @siteorig  ud_idsite	--site que inicia inventario
declare @fi varchar(30)		--fecha inicio de inventario en texto
set @siteorig=(select sourcesite from setup)	--obtener el almacen original
set @fechainicio= (select startdateinven from site where siteid=@siteorig ) --obtener la fecha de inicio de inventario del site
set @fi=( select cast(@fechainicio as varchar(30) ) )

begin tran
--------------------------------------------------------
--agregar las subcategorias nuevas a in_cluster
set @cadena_ins='insert into in_cluster (classid,classubid,intrdatephisical) '+
" select classid,classubid,'"+@fi+"' from in_subcategory where classid in ("+@lista+") "+
' and (classid not in (select classid from in_cluster)      ) '

EXEC (@CADENA_INS)

--------------------------------------------------------
--eliminar las subcategorias que se han deseleccionados del in_cluster
set @cadena_del='delete in_cluster where classid in ( '+
" select classid from in_cluster  where classid not in ("+@lista+") )"

EXEC (@CADENA_DEL)
--------------------------------------------------------

--agregar los productos a in_inventphisical
insert into in_inventphisical (siteid,invtidsku, classid,classubid,stockphisical) 
		select @siteorig ,invtidsku,classid,classubid,0 from in_inventory 
		where classid in (select classid from in_cluster)
		and (classid not in (select classid from in_INVENTPHISICAL)      )
-------------------------------------------------------
--eliminar los productos deseleccionados de in_inventphisical
delete in_inventphisical 
      where classid in (select classid 
			from in_INVENTPHISICAL 
			where classid not in ( select classid 
			from in_cluster) )

 if (@@error!=0)
	rollback
else
	commit
