CREATE PROCEDURE dbo.ApiAfterBarcode
@Kod nvarchar(130),
@ObjectId int
as
BEGIN

--declare @Kod nvarchar(130);
--set @Kod = '4716850001826'



INSERT INTO [dbo].[KodyKreskowe]
           ([Zapis]
           ,[ZapisType]
           ,[Kod]
         --  ,[Jednostka]
           ,[TypKodu]
           ,[PodTypKodu]
           ,[Obcy])

select objectId [Zapis], 'Towary' [ZapisType],Barcode [Kod], 0 TypKodu, 0   [PodTypKodu], 0 Obcy  from dbo.ApiBarcodes
where  Barcode = @Kod and objectId = @ObjectId and  State = 0 and NOT EXISTS( select * from dbo.KodyKreskowe where Kod = @Kod)

IF (( select count(*) from dbo.Towary where ID = @ObjectId and ISNULL(EAN,'')='') >0)
BEGIN
  update dbo.Towary set EAN = @Kod where ID = @ObjectId;
END
update dbo.ApiBarcodes set State =1 where Barcode = @Kod;

END
