    
  CREATE proc [dbo].[sp_AlisStokKarti](@Kodu nvarchar(30),@UreticiKodu nvarchar(30),@Adi nvarchar(40),        
@Adi2 nvarchar(40),@Birim nvarchar(5),@Kdv decimal,@Deger nvarchar(max),@VergiNo nvarchar(50),@Barkod nvarchar(16))        
as        
begin        
declare @No int =0,@error nvarchar(50),@message nvarchar(500)      
select @No= isnull(StokNo,0),@Kodu=Kodu,@Adi= StokAdi from STOKKARTI where Kodu=@Kodu    
if(@No<=0)    
begin       
 begin try        
 set @Birim=(select case when isnull(@Birim,'') ='' then 'ADET' else @Birim end)    
 insert into STOKKARTI(Kodu,UreticiFirmaKodu,StokAdi,StokAdi2,AnaBirim,Barkodu,AlisKdv,KULNO,TP, AktifPasif)        
 VALUES(@Kodu,@UreticiKodu,@Adi,@Adi2,@Birim,@Barkod,@Kdv,1,'TS','A')        
 set @No=@@IDENTITY        
 update STOKKARTI set StokNo=@No where NoId=@No        
 end try        
 begin catch        
   set @No=-1        
    select @error = ERROR_NUMBER(),        
      @message = ERROR_MESSAGE()        
 end catch        
 if(@No>0)        
 begin        
 update InvoiceItemMap set OurCode=@Kodu  where SenderIndentifier= @VergiNo and        
    ( SenderItemValue =case SenderItemKey        
   when 'B' then @Deger        
   when 'S' then @Deger        
   when 'M' then @Deger        
   when 'D' then @Deger        
   when 'N' then @Deger        
   end )        
        
 select StokNo,Kodu,StokAdi from STOKKARTI where StokNo=@No        
 end        
 else         
   select @No StokNo,@error Kodu ,@message StokAdi     
 end    
    
else    
 update InvoiceItemMap set OurCode=@Kodu  where SenderIndentifier= @VergiNo and        
    ( SenderItemValue =case SenderItemKey        
   when 'B' then @Deger        
   when 'S' then @Deger        
   when 'M' then @Deger        
   when 'D' then @Deger        
   when 'N' then @Deger        
   end )      
  select @No StokNo,@Kodu Kodu ,@Adi  StokAdi    
end 
 
 