 ALTER VIEW [dbo].[QETS_STOKISLON_FATISL] AS 
   SELECT  'FATURA' AS FISTIPI, F.DEPOID,F.TUR AS TUR, F.TURADI AS ISLEM, FI.STOKID,STOK.ADI AS STOKADI, FI.ACIKLAMA, FI.ADET * F.TUR AS GCADET,   
            FI.BFIYAT,FI.SATIRIND, FI.SATIRIND2, F.FATNO, F.UNVANI AS FATUNVANI, CARI.KODU AS CARIKODU, CARI.UNVANI AS CARIUNVANI, ISNULL(F.ULKEKODU,CARI.ULKEKODU) AS ULKEKODU,
           ISNULL(F.ADRES1,CARI.ADRES1) AS ADRES1,ISNULL(F.ADRES2,CARI.ADRES2) AS ADRES2,F.VDNO,F.VD,CARI.TEL1,CARI.TEL2,CARI.FAX,CARI.GSM,
           (CASE    
   			   WHEN F.TURADI = 'PERAKENDE' THEN (ISNULL(FI.ADET,0)*ISNULL(FI.BFIYAT,0))-(ISNULL(FI.ADET,0) * ISNULL(FI.BFIYAT,0)) / (100.0+ISNULL(FI.KDV,0) * ISNULL(FI.KDV,0))   ELSE (ISNULL(FI.BFIYAT,0)*ISNULL(FI.ADET,0)) END)  AS SATIRBTUTARI,     
            ISNULL(FI.ADET,0)* (ISNULL(FI.BFIYAT,0) - (ISNULL(FI.BFIYAT,0) * (100 - ISNULL(FI.SATIRIND,0)) / 100 *  (100 - ISNULL(FI.SATIRIND2,0)) / 100 )) - ISNULL(FI.SATIRINDT, 0) AS SATIRINDT,   
            ISNULL(FI.BFIYAT,0) - (ISNULL(FI.BFIYAT,0) * (100 - ISNULL(F.INDORAN1,0)) / 100 *  (100 - ISNULL(F.INDORAN2,0)) / 100 *  (100 - ISNULL(F.INDORAN3,0)) / 100)  AS FALTIINDT,   
            ISNULL(FI.BFIYAT,0) * (100 - ISNULL(FI.SATIRIND,0)) / 100 * (100 - ISNULL(FI.SATIRIND2,0)) / 100 * 
            (1 - ISNULL(FI.SATIRINDT, 0) / (CASE WHEN ISNULL(FI.BFIYAT,0) = 0 THEN 1 ELSE ISNULL(FI.BFIYAT,0) END * CASE WHEN ISNULL(FI.ADET,0) = 0 THEN 1 ELSE ISNULL(FI.ADET,0) END)) * 
            (100 - ISNULL(F.INDORAN1,0)) / 100 * (100 + ISNULL(F.EKORAN1,0)) / 100 * (100 - ISNULL(F.INDORAN2,0)) / 100 * (100 + ISNULL(F.EKORAN2,0)) / 100 * (100 - ISNULL(F.INDORAN3,0)) / 100 * (100 + ISNULL(F.EKORAN3,0)) / 100 AS NFIYAT, 
            STOK.ADI, ISNULL(FI.BFIYAT,0) * (100 - ISNULL(FI.SATIRIND,0)) / 100 * (100 - ISNULL(FI.SATIRIND2,0)) / 100 * 
            (1 - ISNULL(FI.SATIRINDT, 0) / (CASE WHEN ISNULL(FI.BFIYAT,0) = 0 THEN 1 ELSE ISNULL(FI.BFIYAT,0) END * CASE WHEN ISNULL(FI.ADET,0) = 0 THEN 1 ELSE ISNULL(FI.ADET,0) END)) * 
            (100 - ISNULL(F.INDORAN1,0)) / 100 * (100 + ISNULL(F.EKORAN1,0)) / 100 * (100 - ISNULL(F.INDORAN2,0)) / 100 * (100 + ISNULL(F.EKORAN2,0)) / 100 * (100 - ISNULL(F.INDORAN3,0)) / 100 * 
            (100 + ISNULL(F.EKORAN3,0)) / 100 * ISNULL(FI.ADET,0) * F.TUR AS TOPLAM, ISNULL(F.INDORAN1,0) AS INDORAN1, ISNULL(F.EKORAN1,0) AS EKORAN1, ISNULL(F.INDORAN2,0) AS INDORAN2, ISNULL(F.EKORAN2,0) AS EKORAN2, 
            ISNULL(F.INDORAN3,0) AS INDORAN3, F.EKORAN3, F.BAYIID, F.SATISTID, STOK.SALISFIYAT, STOK.KODU AS STOKKODU, F.TARIH,FI.ARATOPLAM,   
            ISNULL(FI.VADE,F.VADE) AS VADE, F.FATID, STOK.SKOMORAN AS STKOMORAN, STOK.BKOMORAN, F.IRSALIYE,ISNULL(FI.SRMMRKID,F.MASMERID) AS MASMERID,   
            F.FATGRUP, NULL AS KDEPOID, STOK.GRUP AS SGRUPKODU, F.SUBEID AS SUBEID,CAST(TUR AS NVARCHAR(15)) AS ISLEMDETAY, FI.FATISLID SATIRKNO,FI.SATINALMAISLID,   
            STOK.BIRIM AS SBIRIMI,BIRIM2,KATSAYI2,BIRIM3,KATSAYI3,BIRIM4,KATSAYI4,FI.KDV AS KDV,   
            F.TOPLAM AS FTTOPLAM, F.KDV AS FTKDV, F.GTOPLAM AS FTGTOPLAM,F.[USER] AS YETKILI, SG.ADI AS SGRUPADI, ISNULL(SG.ALKOLLU,0) AS ALKOLLU,FI.ADET AS  GCADETM,   
            SUBSTRING(SG.KODU, 1,ISNULL(SG.ANAGRUPUZUNLUGU, 2)) AS ANAGRUPKODU, SUBSTRING(SG.KODU, 1,ISNULL(SG.ALTGRUPUZUNLUGU, 4)) AS USTGRUPKODU,    
            SUBSTRING(SG.KODU, 1,ISNULL(SG.ALT2GRUPUZUNLUGU, 6)) AS ALTGRUPKODU, SG.MUHNHESAPID, HSP150.KODU AS NMUHKODU, HSP150.ACIKLAMA AS NMUHADI,    
            SG.MUHGHESAPID, HSP600.KODU AS GMUHKODU, HSP600.ACIKLAMA AS GMUHADI, FI.HESAPID, NULL AS KDEPOKODU,NULL AS KDEPOADI,CARI.CARIID ,STOK.TURU AS TURU,SAYIMBIRIMI,    
            STOK.DEMTIPI,STOK.DEMOMRU,STOK.AMORTISMANORANI,STOK.SFIYAT1 HURDADEGERI,STOK.KIST,FI.TEVORAN, (F.IND1+F.IND2+F.IND3) AS FAINDTOPLAM,(F.SIND+F.IND1+F.IND2+F.IND3) AS INDTOPLAM, (FI.BFIYAT*FI.ADET) AS BTUTAR,   
            SG.MUHAISKHESAPID,HSPAISK.KODU AS AISKMUHKODU, HSPAISK.ACIKLAMA AS AISKMUHADI, SG.MUHSISKHESAPID,HSPAISK.KODU AS SISKMUHKODU, HSPSISK.ACIKLAMA AS SISKMUHADI,   
            F.INDHESAPID, HSPFIND.KODU AS FINDHESAPKODU, HSPFIND.ACIKLAMA AS FINDHESAPADI,CARI.ALISHESAPID AS ALISHESAPID, CARI.HESAPID AS CHESAPID,   
           CASE  
          WHEN ISNULL(TEVORAN,'')<>'' THEN     
			 CASE 
				  WHEN FATTUR.ISLEMID=1  THEN KDVTANIM.TEVKALISHESAPID  
				  WHEN FATTUR.ISLEMID=2  THEN KDVTANIM.TEVKALISIADEHESAPID  
				  WHEN FATTUR.ISLEMID=3  THEN KDVTANIM.TEVKSATISHESAPID 
				  WHEN FATTUR.ISLEMID=4  THEN KDVTANIM.TEVKSATISIADEHESAPID 
				  ELSE NULL 
			  END
         ELSE NULL END  TEVKDVHESAPID, 
          
         CASE 
            WHEN FATTUR.ISLEMID =1 THEN KDVTANIM.ALISHESAPID 
            WHEN FATTUR.ISLEMID =2 THEN KDVTANIM.ALISIADEHESAPID
            WHEN FATTUR.ISLEMID =3 THEN KDVTANIM.SATISHESAPID
			WHEN FATTUR.ISLEMID =4 THEN KDVTANIM.SATISIADEHESAPID
			WHEN FATTUR.ISLEMID =5 THEN KDVTANIM.SATISHESAPID
			WHEN FATTUR.ISLEMID =6 THEN KDVTANIM.SATISHESAPID
			WHEN FATTUR.ISLEMID =7 THEN KDVTANIM.SATISHESAPID END KDVHESAPID,       			  			 
   			 
   			 CONVERT(NVARCHAR(5),F.KTARIH,108) SAAT,F.DOVIZKODU,F.DOVIZKURU,SUBELER.SUBEKODU,SUBELER.SUBEADI,F.ACIKLAMA AS FATACIKLAMA  
            FROM FATISL FI WITH (NOLOCK) INNER JOIN    
                 FATURA F  WITH (NOLOCK) ON FI.FATID = F.FATID LEFT JOIN   
                 ETS_CARI CARI  WITH (NOLOCK) ON F.CARIID = CARI.CARIID INNER JOIN   
                 STOK WITH (NOLOCK)  ON FI.STOKID = STOK.STOKID LEFT JOIN    
                 STOKGRUP SG  WITH (NOLOCK) ON STOK.GRUP = SG.KODU LEFT JOIN 
                 STOKGRUPSUBE SGS WITH (NOLOCK) ON SG.KODU=SGS.STOKGRUPKODU AND F.SUBEID=SGS.SUBEID LEFT JOIN        
                 HESAP AS HSP150    WITH (NOLOCK) ON SGS.MUHNHESAPID = HSP150.HESAPID LEFT JOIN    
                 HESAP AS HSP600    WITH (NOLOCK) ON SGS.MUHGHESAPID = HSP600.HESAPID LEFT JOIN    
                 HESAP AS HSPAISK   WITH (NOLOCK) ON SGS.MUHAISKHESAPID = HSPAISK.HESAPID LEFT JOIN    
                 HESAP AS HSPSISK   WITH (NOLOCK) ON SGS.MUHSISKHESAPID = HSPSISK.HESAPID LEFT JOIN   
                 HESAP AS HSPFIND   WITH (NOLOCK) ON  F.INDHESAPID = HSPFIND.HESAPID LEFT OUTER JOIN    
                 KDVTANIM  WITH (NOLOCK) ON KDVTANIM.ORAN=FI.KDV AND ISNULL(SG.ALKOLLU,0) = ISNULL(KDVTANIM.ALKOLLU,0) LEFT OUTER JOIN
                 FATTUR  WITH (NOLOCK) ON F.TURADI = FATTUR.TURADI   LEFT OUTER JOIN
                 SUBELER WITH (NOLOCK) ON F.SUBEID = SUBELER.SUBEID 
            WHERE  ISNULL(F.FIPTAL,0)=0 AND ( ISNULL(IRSALIYE,0)=0 OR  ( (ISNULL(IRSALIYE,0)=1) AND ( ISNULL(FATCOUNTER,0) =0) ) )   

GO