$1   PROCEDURE [dbo].[MAGAZA_VERGI_TOPLAM]
@aktif_kullanici varchar(25)='',
@magaza_no_liste StringList READONLY,
--@kasa_no_liste StringList READONLY,
@baslangic_tarih dateTime,
@bitis_tarih datetime
AS
DECLARE @sorgu AS NVARCHAR(MAX)='',
@VERGILER AS NVARCHAR(MAX)='',
@kosul as NVARCHAR(MAX)='',
@magazlar  AS NVARCHAR(MAX)='',
@kasalar as NVARCHAR(MAX)='',
@baslangicTarihVarchar as varchar(25),
@bitisTarihVarchar as varchar(25)

SET @VERGILER=STUFF((SELECT (',' + QuoteName(c.vergi_oran))
            FROM VERGI_TANIMLARI c WITH(NOLOCK)  
            group by c.vergi_oran
            ORDER BY c.vergi_oran 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

SET @magazlar=  STUFF((
            SELECT ',''' + c.Item+''''
            FROM @magaza_no_liste c
            FOR XML PATH('')
            ), 1, 1, '')

--SET @kasalar=  STUFF((
--            SELECT ',''' + c.Item+''''
--            FROM @kasa_no_liste c
--            FOR XML PATH('')
--            ), 1, 1, '')

set @baslangicTarihVarchar= CONVERT(VARCHAR(10),@baslangic_tarih,120)
set @bitisTarihVarchar= CONVERT(VARCHAR(10),@bitis_tarih,120)



IF(@magazlar!='')
	SET @kosul+=CASE WHEN @kosul!='' THEN ' AND ' ELSE '' END + 'evr_magaza_kod in ('+@magazlar+')'
--IF(@kasalar!='')
--	SET @kosul+=CASE WHEN @kosul!='' THEN ' AND ' ELSE '' END + 'evr_kasa_no in ('+@kasalar+')'


IF(@kosul='')
	SET @kosul+=CASE WHEN @kosul='' THEN ' 1=1 ' END 


SET @sorgu=N'
SELECT *
FROM(
SELECT
ISNULL(har_kdv_oran,0) as oran,
evr_magaza_kod as [Magaza Kod],
(SELECT TOP 1 mgz_isim FROM MAGAZA_TANIMLARI WITH(NOLOCK) WHERE mgz_kod=evr_magaza_kod) as [Magaza İsim],
ISNULL(har_kdv_tutar,0) as har_kdv_tutar
FROM EVRAK_HAREKET_BILGI WITH(NOLOCK)
INNER JOIN EVRAK_UST_BILGI WITH(NOLOCK) ON evr_RECno=har_evrak_rec_id
WHERE  '+@kosul+' AND  CONVERT(VARCHAR(10),evr_evrak_tarih,120) between '''+@baslangicTarihVarchar+''' and '''+@bitisTarihVarchar+'''
AND (evr_magaza_kod=dbo.fn_AktifKullaniciMagazaGetir('''+@aktif_kullanici+''') OR '''+@aktif_kullanici+'''=0)
)as stokDepo
PIVOT
(sum(har_kdv_tutar)
FOR oran IN ('+@VERGILER+')
)AS pvt
'
execute (@sorgu) 
