CREATE OR ALTER PROC [dbo].[sp_GELGIDH]   
(@tarih1 varchar(20),  
@tarih2 varchar(20),   
@dep varchar(500),   
@orderby int,   
@arama varchar(100),  
@GosterilecekKayit int,  
@IslemTuru varchar(5),  
@KarsiHesap varchar(50),  
@IslemNoLike bit = 0,
@KarsiHesapAdi varchar(150)
)  
as        
begin     
declare @l_KarsiHesapAdi nvarchar(150)
SET @l_KarsiHesapAdi = UPPER(@KarsiHesapAdi)
declare @str varchar(max)              
        
set @str='select * from (select TOP ' + CAST(@GosterilecekKayit as nvarchar(10)) + ' * from GELGID where TARIH >='''+ @Tarih1+''' and TARIH <= '''+@Tarih2+'''';        
        
if @dep <> ''        
 set @str=@str+' and Departman in ('+@Dep+')'        
if @IslemTuru <> 'Tümü'        
 set @str=@str+' and BAACIKLA = '''+@IslemTuru+''''        
if @KarsiHesap <> 'Tümü'        
 set @str=@str+' and KarsiHesp = '''+@KarsiHesap+''''      
   
if @KarsiHesapAdi <> ''        
 set @str=@str+' and (UPPER(HesapAdi) like ''%'+@l_KarsiHesapAdi+'%'' or UPPER(KASAADI) like ''%'+@l_KarsiHesapAdi+'%'' or UPPER(MUSADI) like ''%'+@l_KarsiHesapAdi+'%'')'
 
if @arama <> ''   
begin        
if @orderby=0   
begin  
  if @IslemNoLike = 0  
  set @str=@str+'and CAST(ISLEMNO as nvarchar(20)) = '''+@arama+''' '   
  else  
  set @str=@str+'and ISLEMNO like ''%'+@arama+'%'' '   
 end;  
  
 if @orderby=1         
  set @str=@str+'and upper(GELGIDKODU) like upper('''+@arama+'%'') '         
 if @orderby=2         
  set @str=@str+'and upper(GELGIDADI) like upper(''%'+@arama+'%'') '        
 if @orderby=3         
  set @str=@str+'and upper(ACIKLAMA) like upper(''%'+@arama+'%'') '         
 
end;  
  
set @str=@str+' ORDER BY ISLEMNO DESC) y order by ISLEMNO';            
EXEC    (@str)       
PRINT (@str)        
end;  
go 

insert into VergiMuafiyetleri (Kod,Aciklama) select  '0','YOK'

delete from StokBarkodlari where StokKodu in (select Kodu from STOKKARTI where AktifPasif='P')