CREATE VIEW [dbo].[vw_BankaHareketler]
AS
SELECT   TOP (100) PERCENT Id, FisId, Tur, Tarih, Saat, EvrakNo, HesapId, Tipi, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Yatan, Cekilen, MmzId, ViewTip, Aciklama, Magaza
FROM         (SELECT   BnHId AS Id, BnHFisId AS FisId, BnHTur AS Tur, BnHTarih AS Tarih, BnHSaat AS Saat, BnHEvrakNo AS EvrakNo, BnHHesapId AS HesapId, BnHTipi AS Tipi, 
                                                    BnHKarsiHsId AS KarsiHsId, BnHTutar AS Tutar, BnHFaiz AS Faiz, BnHDovizTip AS DovizTip, BnHDoviz AS Doviz, BnHDovizTutar AS DovizTutar, BnHKur AS Kur, 
                                                    CASE BnHTipi WHEN 0 THEN BnHTutar WHEN 1 THEN 0 END AS Yatan, CASE BnHTipi WHEN 0 THEN 0 WHEN 1 THEN BnHTutar END AS Cekilen, BnHMsMerkId AS MmzId, 
                                                    BnHTur AS ViewTip, BnHAciklama AS Aciklama, BnHMagazaKodu AS Magaza
                           FROM         dbo.Tb_BankaHareket
                           WHERE     (BnHTur <> 1) AND (BnHTur <> 8)
                           UNION ALL
                           SELECT   BnHId AS Id, BnHFisId AS FısId, BnHTur AS Tur, BnHTarih AS Tarih, BnHSaat AS Saat, BnHEvrakNo AS EvrakNo, BnHKarsiHsId AS HesapId, 
                                                    CASE BnHTipi WHEN 1 THEN 0 WHEN 0 THEN 1 END AS Tipi, BnHHesapId AS KarsiHsId, BnHTutar AS Tutar, BnHFaiz AS Faiz, BnHDovizTip AS DovizTip, BnHDoviz AS Doviz, 
                                                    BnHDovizTutar AS DovizTutar, BnHKur AS Kur, CASE BnHTipi WHEN 1 THEN BnHTutar WHEN 0 THEN 0 END AS Yatan, 
                                                    CASE BnHTipi WHEN 1 THEN 0 WHEN 0 THEN BnHTutar END AS Cekilen, BnHMsMerkId AS MmzId, BnHTur AS ViewTip, BnHAciklama, BnHMagazaKodu
                           FROM         dbo.Tb_BankaHareket AS Tb_BankaHareket_1
                           WHERE     (BnHTur = 3)
                           UNION ALL
                           SELECT   KsHId, KsHFisId, KsHTur, KsHTarih, KsHSaat, KsHEvrakNo, KsHKarsiHsId, CASE KsHTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS Expr3, KsHKasaId, KsHTutar, KsHFaiz, 
                                                    KsHDovizTip, KsHDoviz, KsHDovizTutar, KsHKur, CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHTutar END AS Expr1, 
                                                    CASE KsHTipi WHEN 0 THEN KsHTutar WHEN 1 THEN 0 END AS Expr2, KsHMsMerkId, 10 AS ViewTip, KsHAciklama, KsHMagazaKodu
                           FROM         dbo.Tb_KasaHareket
                           WHERE     (KsHTur = 5)
                           UNION ALL
                           SELECT   CsHId, CsHFisId, CsHFisTuru, CsHTarih, CsHSaat, CsHEvrakNo, CsHKarsiHsId, CASE CsHTur WHEN 0 THEN 1 WHEN 1 THEN 0 WHEN 9 THEN 1 ELSE 0 END AS Expr1, 
                                                    CsHEvrakId, CsHTutar, 0 AS Expr2, CsHDovizTip, CsHDoviz, CsHDovizTutar, CsHKur, 
                                                    CASE CsHTur WHEN 0 THEN 0 WHEN 1 THEN CsHTutar WHEN 9 THEN 0 WHEN 11 THEN CsHTutar WHEN 13 THEN CsHTutar END AS Expr3, 
                                                    CASE CsHTur WHEN 0 THEN CsHTutar WHEN 1 THEN 0 WHEN 9 THEN CsHTutar WHEN 11 THEN 0 WHEN 13 THEN 0 END AS Expr4, CsHMsMerkId, 
                                                    CASE CsHFisTuru WHEN 0 THEN CsHTur + 30 WHEN 1 THEN CsHTur + 50 END AS vwTur, CsHAciklama, CsHMagazaKodu
                           FROM         dbo.Tb_CkSnHareket
                           WHERE     (CsHTipi = 0) AND (CsHTur = 0 OR
                                                    CsHTur = 1 OR
                                                    CsHTur = 9 OR
                                                    CsHTur = 11 OR
                                                    CsHTur = 13)
                           UNION ALL
                           SELECT   CrHId, CrHFisId, CrHTur, CrHTarih, CrHSaat, CrhEvrakNo, CrHKarsiHsId, CrHTur - 1 AS Expr4, CrHCariId, CrHTutar, CrHFaiz, CrHDovizTip, CrHDoviz, CrHDovizTutar, CrHKur, 
                                                    CASE CrHTur WHEN 1 THEN CrHTutar ELSE 0 END AS Expr1, CASE CrHTur WHEN 2 THEN CrHTutar ELSE 0 END AS Expr2, CrHMsMerkId, CrHTur + 20 AS Expr3, CrHAciklama, 
                                                    CrHMagazaKodu
                           FROM         dbo.Tb_CariHareket
                           WHERE     (CrHTipi = 2) OR
                                                    (CrHTipi = 3)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_CariHareketler]
AS
SELECT     TOP (100) PERCENT Id, FisId, Tur, Tarih, Saat, EvrakNo, CariId, Tipi, BrAl, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Borc, Alacak, MmzId, ViewTip, PersId, Vade, Aciklama, MalToplam, 
                      TopIsk, SatTah, Sira, Magaza, KdvTipi, KdvTut, OzelKod
FROM         (SELECT     dbo.Tb_CariHareket.CrHId AS Id, dbo.Tb_CariHareket.CrHFisId AS FisId, dbo.Tb_CariHareket.CrHTur AS Tur, dbo.Tb_CariHareket.CrHTarih AS Tarih, dbo.Tb_CariHareket.CrHSaat AS Saat, 
                                              dbo.Tb_CariHareket.CrhEvrakNo AS EvrakNo, dbo.Tb_CariHareket.CrHCariId AS CariId, dbo.Tb_CariHareket.CrHTipi AS Tipi, 
                                              CASE CrHTur WHEN 0 THEN CASE CrHBrAl WHEN 0 THEN 0 ELSE 1 END WHEN 1 THEN 1 WHEN 2 THEN 0 END AS BrAl, dbo.Tb_CariHareket.CrHKarsiHsId AS KarsiHsId, 
                                              dbo.Tb_CariHareket.CrHTutar AS Tutar, dbo.Tb_CariHareket.CrHFaiz AS Faiz, dbo.Tb_CariHareket.CrHDovizTip AS DovizTip, dbo.Tb_CariHareket.CrHDoviz AS Doviz, 
                                              dbo.Tb_CariHareket.CrHDovizTutar AS DovizTutar, dbo.Tb_CariHareket.CrHKur AS Kur, 
                                              CASE CrHTur WHEN 0 THEN CASE CrHBrAl WHEN 0 THEN CrHTutar ELSE 0 END WHEN 1 THEN 0 WHEN 2 THEN CrHTutar END AS Borc, 
                                              CASE CrHTur WHEN 0 THEN CASE CrHBrAl WHEN 0 THEN 0 ELSE CrHTutar END WHEN 1 THEN CrHTutar WHEN 2 THEN 0 END AS Alacak, dbo.Tb_CariHareket.CrHMsMerkId AS MmzId, 
                                              CONVERT(INT, CONVERT(nvarchar(1), dbo.Tb_CariHareket.CrHTur) + '' + CONVERT(Nvarchar(1), dbo.Tb_CariHareket.CrHTipi)) AS ViewTip, dbo.Tb_CariHareket.CrHPersId AS PersId, 
                                              CASE CrHTipi WHEN 4 THEN Tb_CkSnKart.CksVadeTarih WHEN 5 THEN Tb_CkSnKart.CksVadeTarih ELSE '' END AS Vade, dbo.Tb_CariHareket.CrHAciklama AS Aciklama, 0 AS MalToplam,
                                               0 AS TopIsk, 0 AS SatTah, 1 AS Sira, dbo.Tb_CariHareket.CrHMagazaKodu AS Magaza, 0 AS KdvTipi, 0 AS KdvTut, dbo.Tb_CariHareket.CrHOzelKod1 AS OzelKod
                       FROM          dbo.Tb_CariHareket LEFT OUTER JOIN
                                              dbo.Tb_CkSnKart ON dbo.Tb_CariHareket.CrHKarsiHsId = dbo.Tb_CkSnKart.CksId
                       UNION ALL
                       SELECT     FatId, 0 AS Expr1, FatTur, FatTarih, FatSaat, FatNo, FatCariId, FatTip, 
                                             CASE FatTur WHEN 0 THEN 1 WHEN 1 THEN 0 WHEN 2 THEN 1 WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN 1 WHEN 6 THEN 1 END AS Expr2, 0 AS Expr3, FatGenTop, 0 AS Expr4, 
                                             '' AS Expr5, 'TL' AS Expr6, 0 AS Expr7, 0 AS Expr8, 
                                             CASE FatTur WHEN 0 THEN 0 WHEN 1 THEN FatGenTop WHEN 2 THEN 0 WHEN 3 THEN FatGenTop WHEN 4 THEN FatGenTop WHEN 5 THEN 0 WHEN 6 THEN 0 END AS Expr9, 
                                             CASE FatTur WHEN 0 THEN FatGenTop WHEN 1 THEN 0 WHEN 2 THEN FatGenTop WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FatGenTop WHEN 6 THEN FatGenTop END AS Expr10,
                                              FatMsMerkId, 30 + FatTur AS ViewTip, FatPersId AS PersId, FatVade AS Vade, FatAciklama AS Aciklama, FatMalToplam AS MalToplam, FatTopIsk AS TopIsk, 1 AS SatTah, 0 AS Expr11, 
                                             FatMagazaKodu, FatKdvTip, FatKdvTut, FatOzelKod1
                       FROM         dbo.Tb_Fatura
                       WHERE     (FatKaynak = 0) OR
                                             (FatKaynak = 1) OR
                                             (FatKaynak = 3) OR
                                             (FatKaynak = 4)
                       UNION ALL
                       SELECT     SatId, 0 AS Expr1, SatTur, SatTarih, SatSaat, SatNo, SatCariId, 0 AS Expr2, 0 AS Expr3, 0 AS Expr4, SatGenTop, 0 AS Expr5, '' AS Expr6, 'TL' AS Expr7, 0 AS Expr8, 0 AS Expr9, 
                                             SatGenTop AS Expr10, 0 AS Expr11, SatMsMerkId, 40 AS ViewTip, SatPersId AS PersId, SatVade AS Vade, SatAciklama AS Aciklama, SatMalToplam AS MalToplam, SatTopIsk AS TopIsk, 
                                             1 AS SatTah, 0 AS Expr12, SatMagazaKodu, SatKdvTip, SatKdvTut, SatOzelKod1
                       FROM         dbo.Tb_Satis
                       UNION ALL
                       SELECT     YzKId, 0 AS Expr1, 0 AS Expr2, YzKTarih, YzKSaat, YzKNo, YzKCariId, 0 AS Expr3, 0 AS Expr4, 0 AS Expr5, YzKGenTop, 0 AS Expr6, '' AS Expr7, 'TL' AS Expr8, 0 AS Expr9, 0 AS Expr10, 
                                             YzKGenTop AS Expr11, 0 AS Expr12, YzKMsMerkId, 41 AS Expr13, YzKPersId, '' AS Expr14, YzKAciklama, YzKMalToplam, 0 AS Expr15, 1 AS Expr16, 0 AS Expr17, YzKMagazaKodu, 
                                             1 AS Expr18, YzKKdvTut, YzKOzelKod1
                       FROM         dbo.Tb_YzKsSatis
                       UNION ALL
                       SELECT     dbo.Tb_CkSnHareket.CsHId, dbo.Tb_CkSnHareket.CsHFisId, dbo.Tb_CkSnHareket.CsHFisTuru, dbo.Tb_CkSnHareket.CsHTarih, dbo.Tb_CkSnHareket.CsHSaat, 
                                             dbo.Tb_CkSnHareket.CsHEvrakNo, CASE CsHTur WHEN 11 THEN CksCariId ELSE dbo.Tb_CkSnHareket.CsHKarsiHsId END AS Expr1, dbo.Tb_CkSnHareket.CsHFisTuru + 4 AS Tipi, 
                                             CASE CsHTur WHEN 2 THEN 1 WHEN 3 THEN 1 WHEN 4 THEN 0 WHEN 7 THEN 0 WHEN 9 THEN 0 WHEN 11 THEN 1 END AS BrAl, dbo.Tb_CkSnHareket.CsHEvrakId, 
                                             dbo.Tb_CkSnHareket.CsHTutar, 0 AS Expr3, dbo.Tb_CkSnHareket.CsHDovizTip, dbo.Tb_CkSnHareket.CsHDoviz, dbo.Tb_CkSnHareket.CsHDovizTutar, dbo.Tb_CkSnHareket.CsHKur, 
                                             CASE CsHTur WHEN 2 THEN 0 WHEN 3 THEN 0 WHEN 4 THEN CsHTutar WHEN 7 THEN CsHTutar WHEN 9 THEN CsHTutar WHEN 11 THEN 0 END AS Expr4, 
                                             CASE CsHTur WHEN 2 THEN CsHTutar WHEN 3 THEN CsHTutar WHEN 4 THEN 0 WHEN 7 THEN 0 WHEN 9 THEN 0 WHEN 11 THEN CsHTutar END AS Expr5, 
                                             dbo.Tb_CkSnHareket.CsHMsMerkId, CASE CshFisTuru WHEN 0 THEN 50 + dbo.Tb_CkSnHareket.CsHTur WHEN 1 THEN 70 + dbo.Tb_CkSnHareket.CsHTur END AS ViewTip, 
                                             dbo.Tb_CkSnHareket.CsHPersId AS PersId, Tb_CkSnKart_1.CksVadeTarih AS Vade, dbo.Tb_CkSnHareket.CsHAciklama AS Aciklama, 0 AS MalToplam, 0 AS TopIsk, 2 AS SatTah, 2 AS Expr6,
                                              dbo.Tb_CkSnHareket.CsHMagazaKodu, 0 AS Expr7, 0 AS Expr8, dbo.Tb_CkSnHareket.CsHOzelKod1
                       FROM         dbo.Tb_CkSnHareket LEFT OUTER JOIN
                                             dbo.Tb_CkSnKart AS Tb_CkSnKart_1 ON dbo.Tb_CkSnHareket.CsHEvrakId = Tb_CkSnKart_1.CksId
                       WHERE     (dbo.Tb_CkSnHareket.CsHTur = 2) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 3) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 4) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 7) AND (dbo.Tb_CkSnHareket.CsHTipi = 0) AND (Tb_CkSnKart_1.CksCiroTp = 0) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 9) AND (dbo.Tb_CkSnHareket.CsHTipi < 2) AND (Tb_CkSnKart_1.CksCiroTp = 0) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 11) AND (dbo.Tb_CkSnHareket.CsHTipi < 2) AND (Tb_CkSnKart_1.CksCiroTp = 0)
                       UNION ALL
                       SELECT DISTINCT 
                                             dbo.Tb_StkHarBaslik.ShbId, 0 AS Expr1, 1 AS Expr2, dbo.Tb_StkHarBaslik.ShbTarih, dbo.Tb_StkHarBaslik.ShbSaat, dbo.Tb_StkHarBaslik.ShbEvrakNo, dbo.Tb_StkHarBaslik.ShbCariId, 
                                             0 AS Expr3, CASE ShkTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS Expr4, 0 AS Expr5, dbo.Tb_StkHarBaslik.ShbTopTutar, 0 AS Expr6, '' AS Expr7, 'TL' AS Expr8, 0 AS Expr9, 
                                             0 AS Expr10, CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShbTopTutar END AS Expr11, CASE ShkTipi WHEN 0 THEN ShbTopTutar WHEN 1 THEN 0 END AS Expr12, 0 AS Expr13, 
                                             90 + dbo.Tb_StkHarKalem.ShkTipi AS Expr14, dbo.Tb_StkHarBaslik.ShbPersId, dbo.Tb_StkHarBaslik.ShbTarih AS Expr15, dbo.Tb_StkHarBaslik.ShbAciklama, 
                                             dbo.Tb_StkHarBaslik.ShbTopTutar AS Expr16, 0 AS Expr17, 0 AS Expr18, 0 AS Expr19, dbo.Tb_StkHarBaslik.ShbMagazaKodu, dbo.Tb_StkHarBaslik.ShbKdvTipi, 0 AS Expr20, 
                                             dbo.Tb_StkHarBaslik.ShbOzKod1
                       FROM         dbo.Tb_StkHarBaslik LEFT OUTER JOIN
                                             dbo.Tb_StkHarKalem ON dbo.Tb_StkHarBaslik.ShbId = dbo.Tb_StkHarKalem.ShkBaslikId
                       WHERE     (dbo.Tb_StkHarBaslik.ShbFisTur = 1) AND (ISNULL(dbo.Tb_StkHarBaslik.ShbCariId, 0) > 0)) AS A
ORDER BY Tarih, Saat, Sira
GO
CREATE VIEW [dbo].[vw_CariStokluHareketler]
AS
SELECT     TOP (100) PERCENT Id, FisId, Tur, Tarih, Saat, EvrakNo, CariId, Tipi, BrAl, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Borc, Alacak, MmzId, ViewTip, PersId, StokId, Birim, Miktar, Fiyat, 
                      SatTut, SatTip, Vade, Aciklama, FarkGun, SatTah, MalyFiyat, MalyTutar, BrimMiktar, BrimFiyat, TopIsk, Magaza, Sira, Iade, SatirIskonto, Islem, KalemId, DepoId, TsTarih1, TsTarih2, Taksit, SatAcik, 
                      Ozelkod
FROM         (SELECT     dbo.Tb_CariHareket.CrHId AS Id, dbo.Tb_CariHareket.CrHFisId AS FisId, dbo.Tb_CariHareket.CrHTur AS Tur, dbo.Tb_CariHareket.CrHTarih AS Tarih, dbo.Tb_CariHareket.CrHSaat AS Saat, 
                                              dbo.Tb_CariHareket.CrhEvrakNo AS EvrakNo, dbo.Tb_CariHareket.CrHCariId AS CariId, dbo.Tb_CariHareket.CrHTipi AS Tipi, dbo.Tb_CariHareket.CrHBrAl AS BrAl, 
                                              dbo.Tb_CariHareket.CrHKarsiHsId AS KarsiHsId, dbo.Tb_CariHareket.CrHTutar AS Tutar, dbo.Tb_CariHareket.CrHFaiz AS Faiz, dbo.Tb_CariHareket.CrHDovizTip AS DovizTip, 
                                              dbo.Tb_CariHareket.CrHDoviz AS Doviz, dbo.Tb_CariHareket.CrHDovizTutar AS DovizTutar, dbo.Tb_CariHareket.CrHKur AS Kur, 
                                              CASE CrHTur WHEN 0 THEN CASE CrHBrAl WHEN 0 THEN CrHTutar ELSE 0 END WHEN 1 THEN 0 WHEN 2 THEN CrHTutar END AS Borc, 
                                              CASE CrHTur WHEN 0 THEN CASE CrHBrAl WHEN 0 THEN 0 ELSE CrHTutar END WHEN 1 THEN CrHTutar WHEN 2 THEN 0 END AS Alacak, dbo.Tb_CariHareket.CrHMsMerkId AS MmzId, 
                                              CONVERT(INT, CONVERT(nvarchar(1), dbo.Tb_CariHareket.CrHTur) + '' + CONVERT(Nvarchar(1), dbo.Tb_CariHareket.CrHTipi)) AS ViewTip, dbo.Tb_CariHareket.CrHPersId AS PersId, 
                                              0 AS StokId, '' AS Birim, 0 AS Miktar, 0 AS Fiyat, 0 AS SatTut, 3 AS SatTip, 
                                              CASE CrHTipi WHEN 4 THEN Tb_CkSnKart.CksVadeTarih WHEN 5 THEN Tb_CkSnKart.CksVadeTarih ELSE '' END AS Vade, dbo.Tb_CariHareket.CrHAciklama AS Aciklama, 0 AS FarkGun, 
                                              0 AS SatTah, 0 AS MalyFiyat, 0 AS MalyTutar, 0 AS BrimMiktar, 0 AS BrimFiyat, 0 AS TopIsk, dbo.Tb_CariHareket.CrHMagazaKodu AS Magaza, 1 AS Sira, 0 AS Iade, 0 AS SatirIskonto, 
                                              0 AS Islem, 0 AS KalemId, 0 AS DepoId, '' AS TsTarih1, '' AS TsTarih2, dbo.Tb_CariHareket.CrHTaksit AS Taksit, dbo.Tb_CariHareket.CrHAciklama AS SatAcik, 
                                              dbo.Tb_CariHareket.CrHOzelKod1 AS Ozelkod
                       FROM          dbo.Tb_CariHareket LEFT OUTER JOIN
                                              dbo.Tb_CkSnKart ON dbo.Tb_CariHareket.CrHKarsiHsId = dbo.Tb_CkSnKart.CksId
                       UNION ALL
                       SELECT     dbo.Tb_Fatura.FatId, 0 AS Expr1, dbo.Tb_Fatura.FatTur, dbo.Tb_Fatura.FatTarih, dbo.Tb_Fatura.FatSaat, dbo.Tb_Fatura.FatNo, dbo.Tb_Fatura.FatCariId, dbo.Tb_Fatura.FatTip, 0 AS Expr2, 
                                             0 AS Expr3, dbo.Tb_Fatura.FatGenTop, 0 AS Expr4, '' AS Expr5, 'TL' AS Expr6, 0 AS Expr7, 0 AS Expr8, 
                                             CASE FatTur WHEN 0 THEN 0 WHEN 1 THEN FatGenTop WHEN 2 THEN 0 WHEN 3 THEN FatGenTop WHEN 4 THEN FatGenTop WHEN 5 THEN 0 WHEN 6 THEN 0 END AS Expr9, 
                                             CASE FatTur WHEN 0 THEN FatGenTop WHEN 1 THEN 0 WHEN 2 THEN FatGenTop WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FatGenTop WHEN 6 THEN FatGenTop END AS Expr10,
                                              dbo.Tb_Fatura.FatMsMerkId, 30 + dbo.Tb_Fatura.FatTur AS ViewTip, dbo.Tb_Fatura.FatPersId AS PersId, dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_FatKalem.FtKBirim AS Birim, 
                                             dbo.Tb_FatKalem.FtKMiktar AS Miktar, dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS SatTut, dbo.Tb_FatKalem.FtKSatirTipi AS SatTip, dbo.Tb_Fatura.FatVade AS Vade, 
                                             dbo.Tb_Fatura.FatAciklama AS Aciklama, DATEDIFF(d, dbo.Tb_Fatura.FatTarih, dbo.Tb_Fatura.FatVade) AS FarkGun, 1 AS SatTah, dbo.Tb_FatKalem.FtKMalyFiyat AS MalyFiyat, 
                                             dbo.Tb_FatKalem.FtKMalyTutar AS MalyTutar, dbo.Tb_FatKalem.FtKBirimMiktar AS BrimMiktar, dbo.Tb_FatKalem.FtKBirimFiyat AS BrimFiyat, dbo.Tb_Fatura.FatTopIsk AS TopIsk, 
                                             dbo.Tb_Fatura.FatMagazaKodu, 0 AS Expr11, CASE Tb_Fatura.FatTur WHEN 5 THEN 1 WHEN 6 THEN 1 ELSE 0 END AS Iade, 
                                             dbo.Tb_FatKalem.FtKTutar - dbo.Tb_FatKalem.FtKOtvMatrah AS SatirIskonto, 1 AS Expr12, dbo.Tb_FatKalem.FtKId, dbo.Tb_FatKalem.FtKDepoId, '' AS TsTarih1, '' AS TsTarih2, 0 AS Expr13,
                                              dbo.Tb_FatKalem.FtKAciklama, dbo.Tb_Fatura.FatOzelKod1
                       FROM         dbo.Tb_Fatura LEFT OUTER JOIN
                                             dbo.Tb_FatKalem ON dbo.Tb_FatKalem.FtKFisId = dbo.Tb_Fatura.FatId
                       WHERE     (dbo.Tb_Fatura.FatKaynak = 0) OR
                                             (dbo.Tb_Fatura.FatKaynak = 1) OR
                                             (dbo.Tb_Fatura.FatKaynak = 3) OR
                                             (dbo.Tb_Fatura.FatKaynak = 4)
                       UNION ALL
                       SELECT     dbo.Tb_Satis.SatId, 0 AS Expr1, dbo.Tb_Satis.SatTur, dbo.Tb_Satis.SatTarih, dbo.Tb_Satis.SatSaat, dbo.Tb_Satis.SatNo, dbo.Tb_Satis.SatCariId, 0 AS Expr2, 0 AS Expr3, 0 AS Expr4, 
                                             dbo.Tb_Satis.SatGenTop, 0 AS Expr5, '' AS Expr6, 'TL' AS Expr7, 0 AS Expr8, 0 AS Expr9, dbo.Tb_Satis.SatGenTop AS Expr10, 0 AS Expr11, dbo.Tb_Satis.SatMsMerkId, 40 AS ViewTip, 
                                             dbo.Tb_Satis.SatPersId AS PersId, dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlBirim AS Birim, dbo.Tb_SatKalem.SKlMiktar AS Miktar, dbo.Tb_SatKalem.SKlFiyat AS Fiyat, 
                                             dbo.Tb_SatKalem.SKlTutar AS SatTut, dbo.Tb_SatKalem.SKlSatirTipi AS SatTip, dbo.Tb_Satis.SatVade AS Vade, dbo.Tb_Satis.SatAciklama AS Aciklama, DATEDIFF(d, 
                                             dbo.Tb_Satis.SatTarih, dbo.Tb_Satis.SatVade) AS FarkGun, 1 AS SatTah, dbo.Tb_SatKalem.SKlMalyFiyat AS MalyFiyat, dbo.Tb_SatKalem.SKlMalyTutar AS MalyTutar, 
                                             dbo.Tb_SatKalem.SKlBirimMiktar AS BrimMiktar, dbo.Tb_SatKalem.SKlBirimFiyat AS BrimFiyat, dbo.Tb_Satis.SatTopIsk AS TopIsk, dbo.Tb_Satis.SatMagazaKodu, 0 AS Expr12, 
                                             0 AS Expr13, dbo.Tb_SatKalem.SKlTutar - dbo.Tb_SatKalem.SKlOtvMatrah AS SatirIskonto, 
                                             CASE SKlMiktar - SKlTeslimMiktar WHEN 0 THEN 2 ELSE CASE Tb_SatKalem.SKlTeslim WHEN 0 THEN 2 WHEN 1 THEN 3 END END AS Expr14, dbo.Tb_SatKalem.SKlId, 
                                             dbo.Tb_SatKalem.SKlDepoId, dbo.Tb_SatKalem.SKlTesTar1, dbo.Tb_SatKalem.SKlTesTar2, 0 AS Expr15, dbo.Tb_SatKalem.SKlAciklama, dbo.Tb_Satis.SatOzelKod1
                       FROM         dbo.Tb_Satis LEFT OUTER JOIN
                                             dbo.Tb_SatKalem ON dbo.Tb_SatKalem.SKlFisId = dbo.Tb_Satis.SatId
                       UNION ALL
                       SELECT     dbo.Tb_YzKsSatis.YzKId, 0 AS Expr1, 0 AS Expr2, dbo.Tb_YzKsSatis.YzKTarih, dbo.Tb_YzKsSatis.YzKSaat, dbo.Tb_YzKsSatis.YzKNo, dbo.Tb_YzKsSatis.YzKCariId, 0 AS Expr3, 
                                             0 AS Expr4, 0 AS Expr5, dbo.Tb_YzKsSatis.YzKGenTop, 0 AS Expr6, '' AS Expr7, 'TL' AS Expr8, 0 AS Expr9, 0 AS Expr10, dbo.Tb_YzKsSatis.YzKGenTop AS Expr11, 0 AS Expr12, 
                                             dbo.Tb_YzKsSatis.YzKMsMerkId, 41 AS Expr13, dbo.Tb_YzKsSatis.YzKPersId, dbo.Tb_YzKKalem.YkKStokId, dbo.Tb_YzKKalem.YkKBirim, dbo.Tb_YzKKalem.YkKMiktar, 
                                             dbo.Tb_YzKKalem.YkKFiyat, dbo.Tb_YzKKalem.YkKTutar, 0 AS Expr14, '' AS Expr15, dbo.Tb_YzKKalem.YkKAciklama, 0 AS Expr16, 1 AS Expr17, dbo.Tb_YzKKalem.YkKMalyFiyat, 
                                             dbo.Tb_YzKKalem.YkKMalyTutar, dbo.Tb_YzKKalem.YkKBirimMiktar, dbo.Tb_YzKKalem.YkKBirimFiyat, 0 AS Expr18, dbo.Tb_YzKsSatis.YzKMagazaKodu, 0 AS Expr19, 0 AS Expr20, 
                                             0 AS Expr21, 4 AS Expr22, dbo.Tb_YzKKalem.YkKId, dbo.Tb_YzKKalem.YkKDepoId, '' AS Expr23, '' AS Expr24, 0 AS Expr25, dbo.Tb_YzKKalem.YkKAciklama AS Expr26, 
                                             dbo.Tb_YzKsSatis.YzKOzelKod1
                       FROM         dbo.Tb_YzKsSatis LEFT OUTER JOIN
                                             dbo.Tb_YzKKalem ON dbo.Tb_YzKsSatis.YzKId = dbo.Tb_YzKKalem.YkKFisId
                       UNION ALL
                       SELECT     dbo.Tb_CkSnHareket.CsHId, dbo.Tb_CkSnHareket.CsHFisId, dbo.Tb_CkSnHareket.CsHFisTuru, dbo.Tb_CkSnHareket.CsHTarih, dbo.Tb_CkSnHareket.CsHSaat, 
                                             dbo.Tb_CkSnHareket.CsHEvrakNo, CASE CsHTur WHEN 11 THEN CksCariId ELSE dbo.Tb_CkSnHareket.CsHKarsiHsId END AS Expr15, 0 AS Expr1, 0 AS Expr2, 
                                             dbo.Tb_CkSnHareket.CsHEvrakId, dbo.Tb_CkSnHareket.CsHTutar, 0 AS Expr3, dbo.Tb_CkSnHareket.CsHDovizTip, dbo.Tb_CkSnHareket.CsHDoviz, dbo.Tb_CkSnHareket.CsHDovizTutar, 
                                             dbo.Tb_CkSnHareket.CsHKur, CASE CsHTur WHEN 2 THEN 0 WHEN 3 THEN 0 WHEN 4 THEN CsHTutar WHEN 7 THEN CsHTutar WHEN 9 THEN CsHTutar WHEN 11 THEN 0 END AS Expr4,
                                              CASE CsHTur WHEN 2 THEN CsHTutar WHEN 3 THEN CsHTutar WHEN 4 THEN 0 WHEN 7 THEN 0 WHEN 9 THEN 0 WHEN 11 THEN CsHTutar END AS Expr5, 
                                             dbo.Tb_CkSnHareket.CsHMsMerkId, CASE CshFisTuru WHEN 0 THEN 50 + dbo.Tb_CkSnHareket.CsHTur WHEN 1 THEN 70 + dbo.Tb_CkSnHareket.CsHTur END AS ViewTip, 
                                             dbo.Tb_CkSnHareket.CsHPersId AS PersId, 0 AS StokId, '' AS Birim, 0 AS Miktar, 0 AS Fiyat, 0 AS SatTut, 3 AS SatTip, Tb_CkSnKart_1.CksVadeTarih AS Vade, 
                                             dbo.Tb_CkSnHareket.CsHAciklama AS Aciklama, 0 AS FarkGun, 2 AS SatTah, 0 AS MalyFiyat, 0 AS MalyTutar, 0 AS BrimMiktar, 0 AS BrimFiyat, 0 AS TopIsk, 
                                             dbo.Tb_CkSnHareket.CsHMagazaKodu, 2 AS Expr6, 0 AS Expr7, 0 AS Expr8, 5 AS Expr9, 0 AS Expr10, 0 AS Expr11, '' AS Expr12, '' AS Expr13, 0 AS Expr14, 
                                             dbo.Tb_CkSnHareket.CsHAciklama, dbo.Tb_CkSnHareket.CsHOzelKod1
                       FROM         dbo.Tb_CkSnHareket LEFT OUTER JOIN
                                             dbo.Tb_CkSnKart AS Tb_CkSnKart_1 ON dbo.Tb_CkSnHareket.CsHEvrakId = Tb_CkSnKart_1.CksId
                       WHERE     (dbo.Tb_CkSnHareket.CsHTur = 2) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 3) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 4) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 7) AND (dbo.Tb_CkSnHareket.CsHTipi = 0) AND (Tb_CkSnKart_1.CksCiroTp = 0) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 9) AND (dbo.Tb_CkSnHareket.CsHTipi < 2) AND (Tb_CkSnKart_1.CksCiroTp = 0) OR
                                             (dbo.Tb_CkSnHareket.CsHTur = 11) AND (dbo.Tb_CkSnHareket.CsHTipi < 2) AND (Tb_CkSnKart_1.CksCiroTp = 0)
                       UNION ALL
                       SELECT     dbo.Tb_StkHarBaslik.ShbId, ShkBaslikId AS Expr1, 1 AS Expr2, dbo.Tb_StkHarBaslik.ShbTarih, dbo.Tb_StkHarBaslik.ShbSaat, dbo.Tb_StkHarBaslik.ShbEvrakNo, dbo.Tb_StkHarBaslik.ShbCariId, 
                                             0 AS Expr3, CASE ShkTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS Expr4, 0 AS Expr5, dbo.Tb_StkHarBaslik.ShbTopTutar, 0 AS Expr6, '' AS Expr7, 'TL' AS Expr8, 0 AS Expr9, 
                                             0 AS Expr10, CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShbTopTutar END AS Expr11, CASE ShkTipi WHEN 0 THEN ShbTopTutar WHEN 1 THEN 0 END AS Expr12, 0 AS Expr13, 
                                             90 + dbo.Tb_StkHarKalem.ShkTipi AS Expr14, dbo.Tb_StkHarBaslik.ShbPersId, dbo.Tb_StkHarKalem.ShkStokId, dbo.Tb_StkHarKalem.ShkBirim, dbo.Tb_StkHarKalem.ShkMiktar, 
                                             dbo.Tb_StkHarKalem.ShkFiyat, dbo.Tb_StkHarKalem.ShkTutar, 0 AS Expr15, dbo.Tb_StkHarBaslik.ShbTarih AS Expr16, dbo.Tb_StkHarBaslik.ShbAciklama, 0 AS Expr17, 0 AS Expr18, 
                                             dbo.Tb_StkHarKalem.ShkMalyFiyat, dbo.Tb_StkHarKalem.ShkMalyTutar, dbo.Tb_StkHarKalem.ShkBirimMiktar, dbo.Tb_StkHarKalem.ShkBirimFiyat, 0 AS Expr19, 
                                             dbo.Tb_StkHarBaslik.ShbMagazaKodu, 0 AS Expr20, 0 AS Expr21, 0 AS Expr22, 6 AS Expr23, dbo.Tb_StkHarKalem.ShkId, dbo.Tb_StkHarKalem.ShkDepoId, 
                                             dbo.Tb_StkHarBaslik.ShbTarih AS Expr24, dbo.Tb_StkHarBaslik.ShbTarih AS Expr25, 0 AS Expr26, '' AS Expr27, dbo.Tb_StkHarBaslik.ShbOzKod1
                       FROM         dbo.Tb_StkHarBaslik LEFT OUTER JOIN
                                             dbo.Tb_StkHarKalem ON dbo.Tb_StkHarBaslik.ShbId = dbo.Tb_StkHarKalem.ShkBaslikId
                       WHERE     (dbo.Tb_StkHarBaslik.ShbFisTur = 1) AND (ISNULL(dbo.Tb_StkHarBaslik.ShbCariId, 0) > 0)) AS A
GO
CREATE VIEW [dbo].[vw_CkSnHareketler]
AS
SELECT     TOP (100) PERCENT Tarih, Saat, EvrakNo, Islem, Tipi, KarsiHs, EvrakId, Tutar, HrkId, FisId, FisTuru, DovizTip, Doviz, DovizTutar, Kur, PersId, Aciklama, OKod1, 
                      OKod2, OKod3, MmzId, AsilIslem, Magaza
FROM         (SELECT     CsHTarih AS Tarih, CsHSaat AS Saat, CsHEvrakNo AS EvrakNo, CsHTur + 2 AS Islem, CsHTipi AS Tipi, CsHKarsiHsId AS KarsiHs, CsHEvrakId AS EvrakId, 
                                              CsHTutar AS Tutar, CsHId AS HrkId, CsHFisId AS FisId, CsHFisTuru AS FisTuru, CsHDovizTip AS DovizTip, CsHDoviz AS Doviz, 
                                              CsHDovizTutar AS DovizTutar, CsHKur AS Kur, CsHPersId AS PersId, CsHAciklama AS Aciklama, CsHOzelKod1 AS OKod1, CsHOzelKod2 AS OKod2, 
                                              CsHOzelKod3 AS OKod3, CsHMsMerkId AS MmzId, CONVERT(nvarchar, CsHTur + 2) + CONVERT(nvarchar, CsHTipi) AS AsilIslem, 
                                              CsHMagazaKodu AS Magaza
                       FROM          dbo.Tb_CkSnHareket
                       UNION ALL
                       SELECT     CrHTarih, CrHSaat AS Expr5, CrhEvrakNo, CrHTur - 1 AS Expr2, CrHTipi, CrHCariId, CrHKarsiHsId, CrHTutar, CrHId, CrHFisId, CrHTipi - 4 AS Expr1, 
                                             CrHDovizTip, CrHDoviz, CrHDovizTutar, CrHKur, CrHPersId, CrHAciklama, CrHOzelKod1, CrHOzelKod2, CrHOzelKod3, CrHMsMerkId AS Expr3, 
                                             CrHTur - 1 AS Expr4, CrHMagazaKodu
                       FROM         dbo.Tb_CariHareket
                       WHERE     (CrHTipi = 4) OR
                                             (CrHTipi = 5)) AS A
ORDER BY Tarih, Saat, Islem
GO
CREATE VIEW [dbo].[vw_EnCokSatilanMiktar]
AS
SELECT        TOP (100) StokId, SUM(Miktar) AS Miktar, SUM(Tutar) AS Tutar, COUNT(StokId) AS SatAd
FROM            (SELECT        FtKStokId AS StokId, FtKMiktar AS Miktar, FtKMalyTutar AS Tutar, FtKStokId AS SatAd
                          FROM            dbo.Tb_FatKalem
                          WHERE        (FtKSatirTipi = 0) AND (FtKKaynak = 0 OR
                                                    FtKKaynak = 1 OR
                                                    FtKKaynak = 3 OR
                                                    FtKKaynak = 4) AND (FtKTipi = 1 OR
                                                    FtKTipi = 3 OR
                                                    FtKTipi > 4)
                          UNION ALL
                          SELECT        SKlStokId, SKlMiktar, SKlMalyTutar, SKlId
                          FROM            dbo.Tb_SatKalem
                          WHERE        (SKlSatirTipi = 0)
                          UNION ALL
                          SELECT        YkKStokId, YkKMiktar, YkKMalyTutar, YkKId
                          FROM            dbo.Tb_YzKKalem) AS A
GROUP BY StokId
ORDER BY Miktar DESC
GO
CREATE VIEW [dbo].[vw_EnCokSatilanTutar]
AS
SELECT        TOP (100) StokId, SUM(Miktar) AS Miktar, SUM(Tutar) AS Tutar, COUNT(StokId) AS SatAd
FROM            (SELECT        FtKStokId AS StokId, FtKMiktar AS Miktar, FtKMalyTutar AS Tutar, FtKStokId AS SatAd
                          FROM            dbo.Tb_FatKalem
                          WHERE        (FtKSatirTipi = 0) AND (FtKKaynak = 0 OR
                                                    FtKKaynak = 1 OR
                                                    FtKKaynak = 3 OR
                                                    FtKKaynak = 4) AND (FtKTipi = 1 OR
                                                    FtKTipi = 3 OR
                                                    FtKTipi > 4)
                          UNION ALL
                          SELECT        SKlStokId, SKlMiktar, SKlMalyTutar, SKlId
                          FROM            dbo.Tb_SatKalem
                          WHERE        (SKlSatirTipi = 0)
                          UNION ALL
                          SELECT        YkKStokId, YkKMiktar, YkKMalyTutar, YkKId
                          FROM            dbo.Tb_YzKKalem) AS A
GROUP BY StokId
ORDER BY Tutar DESC
GO
CREATE VIEW [dbo].[vw_FrKrKrHareketler]
AS
SELECT   TOP (100) PERCENT Id, FisId, Tur, Tarih, Saat, EvrakNo, KrKartId, Tipi, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Harcanan, Odenen, MmzId, Taksit, ViewTip, Aciklama, HrOd, 
                         Magaza
FROM         (SELECT   BnHId AS Id, BnHFisId AS FisId, BnHTur AS Tur, BnHTarih AS Tarih, BnHSaat AS Saat, BnHEvrakNo AS EvrakNo, 
                                                    CASE BnHTur WHEN 1 THEN BnHHesapId ELSE BnHKarsiHsId END AS KrKartId, BnHTipi AS Tipi, 
                                                    CASE BnHTur WHEN 1 THEN BnHKarsiHsId ELSE BnHHesapId END AS KarsiHsId, BnHTutar AS Tutar, BnHFaiz AS Faiz, BnHDovizTip AS DovizTip, BnHDoviz AS Doviz, 
                                                    BnHDovizTutar AS DovizTutar, BnHKur AS Kur, 
                                                    CASE BnHTur WHEN 1 THEN CASE BnHTipi WHEN 0 THEN BnHTutar WHEN 1 THEN 0 END ELSE CASE BnHTur WHEN 5 THEN 0 ELSE BnHTutar END END AS Harcanan, 
                                                    CASE BnHTur WHEN 1 THEN CASE BnHTipi WHEN 0 THEN 0 WHEN 1 THEN BnHTutar END ELSE CASE BnHTur WHEN 5 THEN BnHTutar ELSE 0 END END AS Odenen, 
                                                    BnHMsMerkId AS MmzId, 0 AS Taksit, BnHTur AS ViewTip, BnHAciklama AS Aciklama, 
                                                    CASE BnHTur WHEN 1 THEN CASE BnHTipi WHEN 0 THEN 0 WHEN 1 THEN 1 END ELSE CASE BnHTur WHEN 5 THEN 1 ELSE 0 END END AS HrOd, 
                                                    BnHMagazaKodu AS Magaza
                           FROM         dbo.Tb_BankaHareket
                           WHERE     (BnHTur = 1) OR
                                                    (BnHTur = 5) OR
                                                    (BnHTur = 8) AND (BnHTipi = 1)
                           UNION ALL
                           SELECT   KsHId, KsHFisId, KsHTur, KsHTarih, KsHSaat, KsHEvrakNo, KsHKarsiHsId, KsHTipi, KsHKasaId, KsHTutar, KsHFaiz, KsHDovizTip, KsHDoviz, KsHDovizTutar, KsHKur, 
                                                    CASE KsHTur WHEN 7 THEN 0 ELSE KsHTutar END AS Expr1, CASE KsHTur WHEN 7 THEN KsHTutar ELSE 0 END AS Expr2, KsHMsMerkId, KsHFaiz AS Expr3, 
                                                    KsHTur AS ViewTip, KsHAciklama, CASE KsHTur WHEN 7 THEN 1 ELSE 0 END AS Expr4, KsHMagazaKodu
                           FROM         dbo.Tb_KasaHareket
                           WHERE     (KsHTur = 7) OR
                                                    (KsHTur = 9) OR
                                                    (KsHTur = 11)
                           UNION ALL
                           SELECT   CrHId, CrHFisId, CrHTur, CrHTarih, CrHSaat, CrhEvrakNo, CrHKarsiHsId, CrHBrAl, CrHCariId, CrHTutar, CrHFaiz, CrHDovizTip, CrHDoviz, CrHDovizTutar, CrHKur, 
                                                    CrHTutar AS Expr1, 0 AS Expr2, CrHMsMerkId, CrHTaksit, 2 AS ViewTip, CrHAciklama, 0 AS Expr3, CrHMagazaKodu
                           FROM         dbo.Tb_CariHareket
                           WHERE     (CrHTipi = 1) AND (CrHTur = 2)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_HizmetHareketler]
AS
SELECT     TOP (100) PERCENT Id, Tipi, BaslikTipi, Tarih, Saat, KdvTipi, StokId, Birim, Miktar, Fiyat, Tutar, Doviz, DovizTip, DovizFiyat, Kur, DepoId, GirenDepoId, PartiId, 
                      BirimMiktar, BirimFiyat, MalyFiyat, MalyTutar, KdvOran, GirenTut, CikanTut, ViewTip, GirCik, BaslikId, Aciklama, Magaza
FROM         (SELECT     dbo.Tb_StkHarKalem.ShkId AS Id, dbo.Tb_StkHarKalem.ShkTipi AS Tipi, dbo.Tb_StkHarKalem.ShkBaslikTipi AS BaslikTipi, 
                                              dbo.Tb_StkHarKalem.ShkTarih AS Tarih, dbo.Tb_StkHarKalem.ShkSaat AS Saat, dbo.Tb_StkHarKalem.ShkKdvTipi AS KdvTipi, 
                                              dbo.Tb_StkHarKalem.ShkStokId AS StokId, dbo.Tb_StkHarKalem.ShkBirim AS Birim, dbo.Tb_StkHarKalem.ShkMiktar AS Miktar, 
                                              dbo.Tb_StkHarKalem.ShkFiyat AS Fiyat, dbo.Tb_StkHarKalem.ShkTutar AS Tutar, dbo.Tb_StkHarKalem.ShkDoviz AS Doviz, 
                                              dbo.Tb_StkHarKalem.ShkDovizTip AS DovizTip, dbo.Tb_StkHarKalem.ShkDovizFiyat AS DovizFiyat, dbo.Tb_StkHarKalem.ShkKur AS Kur, 
                                              dbo.Tb_StkHarKalem.ShkDepoId AS DepoId, dbo.Tb_StkHarKalem.ShkGirenDepoId AS GirenDepoId, dbo.Tb_StkHarKalem.ShkPartiId AS PartiId, 
                                              dbo.Tb_StkHarKalem.ShkBirimMiktar AS BirimMiktar, dbo.Tb_StkHarKalem.ShkBirimFiyat AS BirimFiyat, dbo.Tb_StkHarKalem.ShkMalyFiyat AS MalyFiyat, 
                                              dbo.Tb_StkHarKalem.ShkMalyTutar AS MalyTutar, dbo.Tb_StkHarKalem.ShkKdvOran AS KdvOran, 
                                              CASE ShkTipi WHEN 0 THEN ShkTutar WHEN 1 THEN 0 END AS GirenTut, CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShkTutar END AS CikanTut, 
                                              0 AS ViewTip, dbo.Tb_StkHarKalem.ShkTipi AS GirCik, dbo.Tb_StkHarKalem.ShkBaslikId AS BaslikId, '' AS Aciklama, 
                                              dbo.Tb_StkHarBaslik.ShbMagazaKodu AS Magaza
                       FROM          dbo.Tb_StkHarKalem LEFT OUTER JOIN
                                              dbo.Tb_StkHarBaslik ON dbo.Tb_StkHarBaslik.ShbId = dbo.Tb_StkHarKalem.ShkBaslikId
                       WHERE      (dbo.Tb_StkHarKalem.ShkBaslikTipi = 3)
                       UNION ALL
                       SELECT     dbo.Tb_FatKalem.FtKId AS Id, dbo.Tb_FatKalem.FtKTipi AS Tipi, dbo.Tb_FatKalem.FtKTipi AS BaslikTipi, dbo.Tb_FatKalem.FtKTarih AS Tarih, 
                                             dbo.Tb_FatKalem.FtKSaat AS Saat, dbo.Tb_FatKalem.FtKKdvTipi AS KdvTipi, dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_FatKalem.FtKBirim AS Birim, 
                                             dbo.Tb_FatKalem.FtKMiktar AS Miktar, dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS Tutar, dbo.Tb_FatKalem.FtKDoviz AS Doviz, 
                                             dbo.Tb_FatKalem.FtKDovizTipi AS DovizTip, dbo.Tb_FatKalem.FtKDovizFiyat AS DovizFiyat, dbo.Tb_FatKalem.FtKKur AS Kur, 
                                             dbo.Tb_FatKalem.FtKDepoId AS DepoId, 0 AS GirenDepoId, dbo.Tb_FatKalem.FtKPartiId AS PartiId, dbo.Tb_FatKalem.FtKBirimMiktar AS BirimMiktar, 
                                             dbo.Tb_FatKalem.FtKBirimFiyat AS BirimFiyat, dbo.Tb_FatKalem.FtKMalyFiyat AS MalyFiyat, dbo.Tb_FatKalem.FtKMalyTutar AS MalyTutar, 
                                             dbo.Tb_FatKalem.FtKKdvOran AS KdvOran, 
                                             CASE FtkTipi WHEN 0 THEN FtKTutar WHEN 1 THEN 0 WHEN 2 THEN FtKTutar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FtKTutar END AS GirenTut, 
                                             CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN FtKTutar WHEN 2 THEN 0 WHEN 3 THEN FtKTutar WHEN 4 THEN FtKTutar WHEN 5 THEN 0 END AS CikanTut, 
                                             dbo.Tb_FatKalem.FtKTipi + 1 AS ViewTipi, 
                                             CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 END AS GirCik, 
                                             dbo.Tb_FatKalem.FtKFisId, dbo.Tb_FatKalem.FtKAciklama, dbo.Tb_Fatura.FatMagazaKodu
                       FROM         dbo.Tb_FatKalem LEFT OUTER JOIN
                                             dbo.Tb_Fatura ON dbo.Tb_Fatura.FatId = dbo.Tb_FatKalem.FtKFisId
                       WHERE     (dbo.Tb_FatKalem.FtKSatirTipi = 1) AND (dbo.Tb_FatKalem.FtKKaynak = 0 OR
                                             dbo.Tb_FatKalem.FtKKaynak = 1 OR
                                             dbo.Tb_FatKalem.FtKKaynak = 3 OR
                                             dbo.Tb_FatKalem.FtKKaynak = 4)
                       UNION ALL
                       SELECT     dbo.Tb_SatKalem.SKlId AS Id, dbo.Tb_SatKalem.SKlTipi AS Tipi, dbo.Tb_SatKalem.SKlTipi AS BaslikTipi, dbo.Tb_SatKalem.SKlTarih AS Tarih, 
                                             dbo.Tb_SatKalem.SKlSaat, dbo.Tb_SatKalem.SKlKdvTipi AS KdvTipi, dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlBirim AS Birim, 
                                             dbo.Tb_SatKalem.SKlMiktar AS Miktar, dbo.Tb_SatKalem.SKlFiyat AS Fiyat, dbo.Tb_SatKalem.SKlTutar AS Tutar, dbo.Tb_SatKalem.SKlDoviz AS Doviz, 
                                             dbo.Tb_SatKalem.SKlDovizTipi AS DovizTip, dbo.Tb_SatKalem.SKlDovizFiyat AS DovizFiyat, dbo.Tb_SatKalem.SKlKur AS Kur, 
                                             dbo.Tb_SatKalem.SKlDepoId AS DepoId, 0 AS GirenDepoId, dbo.Tb_SatKalem.SKlPartiId AS PartiId, dbo.Tb_SatKalem.SKlBirimMiktar AS BirimMiktar, 
                                             dbo.Tb_SatKalem.SKlBirimFiyat AS BirimFiyat, dbo.Tb_SatKalem.SKlMalyFiyat AS MalyFiyat, dbo.Tb_SatKalem.SKlMalyTutar AS MalyTutar, 
                                             dbo.Tb_SatKalem.SKlKdvOran AS KdvOran, 0 AS GirenTut, dbo.Tb_SatKalem.SKlTutar AS CikanTut, 20 AS ViewTipi, 1 AS GirCik, dbo.Tb_SatKalem.SKlFisId, 
                                             dbo.Tb_SatKalem.SKlAciklama, dbo.Tb_Satis.SatMagazaKodu
                       FROM         dbo.Tb_SatKalem LEFT OUTER JOIN
                                             dbo.Tb_Satis ON dbo.Tb_Satis.SatId = dbo.Tb_SatKalem.SKlFisId
                       WHERE     (dbo.Tb_SatKalem.SKlSatirTipi = 1)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_KasaHareketler]
AS
SELECT TOP (100) PERCENT Id, FısId, Tur, Tarih, Saat, EvrakNo, KasaId, GlGd, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Gelir, Gider, MmzId, vwTur, Aciklama, Magaza, DovGelir, DovGider
FROM     (SELECT KsHId AS Id, KsHFisId AS FısId, KsHTur AS Tur, KsHTarih AS Tarih, KsHSaat AS Saat, KsHEvrakNo AS EvrakNo, KsHKasaId AS KasaId, KsHTipi AS GlGd, KsHKarsiHsId AS KarsiHsId, KsHTutar AS Tutar, KsHFaiz AS Faiz, 
                                    KsHDovizTip AS DovizTip, KsHDoviz AS Doviz, KsHDovizTutar AS DovizTutar, KsHKur AS Kur, CASE KsHTipi WHEN 0 THEN KsHTutar WHEN 1 THEN 0 END AS Gelir, 
                                    CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHTutar END AS Gider, KsHMsMerkId AS MmzId, KsHTur AS vwTur, KsHAciklama AS Aciklama, KsHMagazaKodu AS Magaza, 
                                    CASE KsHTipi WHEN 0 THEN KsHDovizTutar WHEN 1 THEN 0 END AS DovGelir, CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHDovizTutar END AS DovGider
                  FROM      dbo.Tb_KasaHareket
                  WHERE   (KsHTur > 3) AND (KsHTur < 10) OR
                                    (KsHTur = 0)
                  UNION ALL
                  SELECT KsHId AS Id, KsHFisId AS FısId, KsHTur AS Tur, KsHTarih AS Tarih, KsHSaat AS Saat, KsHEvrakNo AS EvrakNo, KsHKarsiHsId AS KasaId, CASE KsHTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS GlGd, KsHKasaId AS KarsiHsId, 
                                    KsHTutar AS Tutar, KsHFaiz AS Faiz, KsHDovizTip AS DovizTip, KsHDoviz AS Doviz, KsHDovizTutar AS DovizTutar, KsHKur AS Kur, CASE KsHTipi WHEN 1 THEN KsHTutar WHEN 0 THEN 0 END AS Gelir, 
                                    CASE KsHTipi WHEN 1 THEN 0 WHEN 0 THEN KsHTutar END AS Gider, KsHMsMerkId AS MmzId, KsHTur AS vwTur, KsHAciklama, KsHMagazaKodu, CASE KsHTipi WHEN 1 THEN KsHDovizTutar WHEN 0 THEN 0 END AS DovGelir, 
                                    CASE KsHTipi WHEN 1 THEN 0 WHEN 0 THEN KsHDovizTutar END AS DovGider
                  FROM     dbo.Tb_KasaHareket AS Tb_KasaHareket_1
                  WHERE  (KsHTur = 6)
                  UNION ALL
                  SELECT CsHId, CsHFisId, CsHFisTuru, CsHTarih, CsHSaat, CsHEvrakNo, CsHKarsiHsId, CASE CsHTur WHEN 0 THEN 1 WHEN 1 THEN 0 WHEN 9 THEN 1 WHEN 11 THEN 0 END AS Expr1, CsHEvrakId, CsHTutar, 0 AS Expr2, CsHDovizTip, 
                                    CsHDoviz, CsHDovizTutar, CsHKur, CASE CsHTur WHEN 0 THEN 0 WHEN 1 THEN CsHTutar WHEN 9 THEN 0 WHEN 11 THEN CsHTutar END AS Expr3, 
                                    CASE CsHTur WHEN 0 THEN CsHTutar WHEN 1 THEN 0 WHEN 9 THEN CsHTutar WHEN 11 THEN 0 END AS Expr4, CsHMsMerkId, CASE CsHFisTuru WHEN 0 THEN CsHTur + 10 WHEN 1 THEN CsHTur + 30 END AS vwTur, 
                                    CsHAciklama, CsHMagazaKodu, CASE CsHTur WHEN 0 THEN 0 WHEN 1 THEN CsHDovizTutar WHEN 9 THEN 0 WHEN 11 THEN CsHDovizTutar END AS Expr5, 
                                    CASE CsHTur WHEN 0 THEN CsHDovizTutar WHEN 1 THEN 0 WHEN 9 THEN CsHDovizTutar WHEN 11 THEN 0 END AS Expr6
                  FROM     dbo.Tb_CkSnHareket
                  WHERE  (CsHTipi = 1) AND (CsHTur = 0 OR
                                    CsHTur = 1 OR
                                    CsHTur = 9 OR
                                    CsHTur = 11)
                  UNION ALL
                  SELECT CrHId, CrHFisId, CrHTur, CrHTarih, CrHSaat, CrhEvrakNo, CrHKarsiHsId, CrHTur - 1 AS GlGd, CrHCariId, CrHTutar, CrHFaiz, CrHDovizTip, CrHDoviz, CrHDovizTutar, CrHKur, 
                                    CASE CrHTur WHEN 1 THEN CrHTutar WHEN 2 THEN 0 END AS Expr1, CASE CrHTur WHEN 1 THEN 0 WHEN 2 THEN CrHTutar END AS Expr2, CrHMsMerkId, CrHTur + 50 AS vwTur, CrHAciklama, CrHMagazaKodu, 
                                    CASE CrHTur WHEN 1 THEN CrHDovizTutar WHEN 2 THEN 0 END AS Expr3, CASE CrHTur WHEN 1 THEN 0 WHEN 2 THEN CrHDovizTutar END AS Expr4
                  FROM     dbo.Tb_CariHareket
                  WHERE  (CrHTipi = 0) AND (CrHTur = 1 OR
                                    CrHTur = 2)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_KdvOtv]
AS
SELECT        TOP (100) PERCENT Tarih, Birim, Miktar, Fiyat, Tutar, KdvMatrah, KdvTutar, KdvOran, ViewTipi, GirCik, OtvMatrah, OtvTutar, SatirTip, IslemTip, FisNo, CariId, TevkTutar, BaslikId, GenelToplam, StokId, Magaza, Iskonto
FROM            (SELECT        dbo.Tb_FatKalem.FtKTarih AS Tarih, dbo.Tb_FatKalem.FtKBirim AS Birim, dbo.Tb_FatKalem.FtKMiktar AS Miktar, dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS Tutar, 
                                                    dbo.Tb_FatKalem.FtKKdvMatrah AS KdvMatrah, dbo.Tb_FatKalem.FtKKdvTutar AS KdvTutar, dbo.Tb_FatKalem.FtKKdvOran AS KdvOran, dbo.Tb_FatKalem.FtKTipi + 20 AS ViewTipi, 
                                                    CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 2 WHEN 5 THEN 3 WHEN 6 THEN 3 END AS GirCik, dbo.Tb_FatKalem.FtKOtvMatrah AS OtvMatrah, 
                                                    dbo.Tb_FatKalem.FtKOtvTutar AS OtvTutar, dbo.Tb_FatKalem.FtKSatirTipi AS SatirTip, 0 AS IslemTip, dbo.Tb_Fatura.FatNo AS FisNo, dbo.Tb_Fatura.FatCariId AS CariId, dbo.Tb_Fatura.FatTevTut AS TevkTutar, 
                                                    dbo.Tb_FatKalem.FtKFisId AS BaslikId, dbo.Tb_Fatura.FatGenTop AS GenelToplam, dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_Fatura.FatMagazaKodu AS Magaza, 
                                                    dbo.Tb_FatKalem.FtKTutar - dbo.Tb_FatKalem.FtKOtvMatrah AS Iskonto
                          FROM            dbo.Tb_FatKalem LEFT OUTER JOIN
                                                    dbo.Tb_Fatura ON dbo.Tb_Fatura.FatId = dbo.Tb_FatKalem.FtKFisId
                          WHERE        (dbo.Tb_FatKalem.FtKKaynak <> 2) AND (dbo.Tb_FatKalem.FtKKaynak <> 5) AND (dbo.Tb_FatKalem.FtKTipi < 6)
                          UNION ALL
                          SELECT        dbo.Tb_SatKalem.SKlTarih AS Tarih, dbo.Tb_SatKalem.SKlBirim AS Birim, dbo.Tb_SatKalem.SKlMiktar AS Miktar, dbo.Tb_SatKalem.SKlFiyat AS Fiyat, dbo.Tb_SatKalem.SKlTutar AS Tutar, 
                                                   dbo.Tb_SatKalem.SKlKdvMatrah AS KdvMatrah, dbo.Tb_SatKalem.SKlKdvTutar AS KdvTutar, dbo.Tb_SatKalem.SKlKdvOran AS KdvOran, 30 AS ViewTipi, 1 AS GirCik, dbo.Tb_SatKalem.SKlOtvMatrah AS OtvMatrah, 
                                                   dbo.Tb_SatKalem.SKlOtvTutar AS OtvTutar, dbo.Tb_SatKalem.SKlSatirTipi AS SatirTip, 1 AS IslemTip, dbo.Tb_Satis.SatNo AS FisNo, dbo.Tb_Satis.SatCariId AS CariId, dbo.Tb_Satis.SatTevTut AS TevkTutar, 
                                                   dbo.Tb_SatKalem.SKlFisId AS BaslikId, dbo.Tb_Satis.SatGenTop AS GenelToplam, dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_Satis.SatMagazaKodu, 
                                                   dbo.Tb_SatKalem.SKlTutar - dbo.Tb_SatKalem.SKlOtvMatrah AS Expr1
                          FROM            dbo.Tb_SatKalem LEFT OUTER JOIN
                                                   dbo.Tb_Satis ON dbo.Tb_Satis.SatId = dbo.Tb_SatKalem.SKlFisId
                          UNION ALL
                          SELECT        dbo.Tb_YzKKalem.YkKTarih, dbo.Tb_YzKKalem.YkKBirim, dbo.Tb_YzKKalem.YkKMiktar, dbo.Tb_YzKKalem.YkKFiyat, dbo.Tb_YzKKalem.YkKTutar, dbo.Tb_YzKKalem.YkKKdvMatrah, 
                                                   dbo.Tb_YzKKalem.YkKKdvTutar, dbo.Tb_YzKKalem.YkKKdvOran, 40 AS Expr11, 1 AS Expr12, 0 AS Expr1, 0 AS Expr2, 0 AS Expr3, 2 AS Expr4, dbo.Tb_YzKsSatis.YzKNo, dbo.Tb_YzKsSatis.YzKCariId, 0 AS Expr5, 
                                                   dbo.Tb_YzKKalem.YkKFisId, dbo.Tb_YzKsSatis.YzKGenTop, dbo.Tb_YzKKalem.YkKStokId, dbo.Tb_YzKsSatis.YzKMagazaKodu, 0 AS Expr6
                          FROM            dbo.Tb_YzKKalem LEFT OUTER JOIN
                                                   dbo.Tb_YzKsSatis ON dbo.Tb_YzKsSatis.YzKId = dbo.Tb_YzKKalem.YkKFisId
                          UNION ALL
                          SELECT        KsHTarih, '' AS Expr1, 1 AS Expr2, KsHTutar, KsHTutar AS Expr3, KsHTutar / (1 + KsHKdvOrn / 100) AS Expr4, KsHTutar - KsHTutar / (1 + KsHKdvOrn / 100) AS Expr5, KsHKdvOrn, 50 AS Expr6, 0 AS Expr7, 0 AS Expr8,
                                                    0 AS Expr9, 0 AS Expr10, 0 AS Expr16, KsHEvrakNo, 0 AS Expr11, 0 AS Expr12, 0 AS Expr13, KsHTutar AS Expr14, 0 AS Expr15, KsHMagazaKodu, 0 AS Expr17
                          FROM            dbo.Tb_KasaHareket
                          WHERE        (KsHTur = 4 OR
                                                   KsHTur = 11) AND (KsHKdvOrn > 0) AND (KsHTipi = 1)) AS A
ORDER BY Tarih, ViewTipi, BaslikId
GO
CREATE VIEW [dbo].[vw_KrediHareketler]
AS
SELECT        TOP (100) PERCENT Id, FısId, Tur, Tarih, Saat, EvrakNo, KrediId, Tipi, KarsiHsId, Tutar, DovizTip, Doviz, DovizTutar, Kur, Yatan, Cekilen, MmzId, ViewTip, Aciklama, Magaza
FROM            (SELECT        BnHId AS Id, BnHFisId AS FısId, BnHTur AS Tur, BnHTarih AS Tarih, BnHSaat AS Saat, BnHEvrakNo AS EvrakNo, BnHKarsiHsId AS KrediId, BnHTipi AS Tipi, BnHHesapId AS KarsiHsId, BnHTutar AS Tutar, 
                                                    BnHDovizTip AS DovizTip, BnHDoviz AS Doviz, BnHDovizTutar AS DovizTutar, BnHKur AS Kur, CASE BnHTipi WHEN 0 THEN BnHTutar WHEN 1 THEN 0 END AS Yatan, 
                                                    CASE BnHTipi WHEN 0 THEN 0 WHEN 1 THEN BnHTutar END AS Cekilen, BnHMsMerkId AS MmzId, BnHTur AS ViewTip, BnHAciklama AS Aciklama, BnHMagazaKodu AS Magaza
                          FROM            dbo.Tb_BankaHareket
                          WHERE        (BnHTur = 6) OR
                                                    (BnHTur = 8) AND (BnHTipi = 0)
                          UNION ALL
                          SELECT        KsHId, KsHFisId, KsHTur, KsHTarih, KsHSaat, KsHEvrakNo, KsHKarsiHsId, KsHTipi AS Expr3, KsHKasaId, KsHTutar, KsHDovizTip, KsHDoviz, KsHDovizTutar, KsHKur, 
                                                   CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHTutar END AS Expr1, CASE KsHTipi WHEN 0 THEN KsHTutar WHEN 1 THEN 0 END AS Expr2, KsHMsMerkId, 10 AS ViewTip, KsHAciklama, KsHMagazaKodu
                          FROM            dbo.Tb_KasaHareket
                          WHERE        (KsHTur = 8)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_MasMerHareketler]
AS
SELECT     TOP (100) PERCENT TurAd, Tarih, Saat, EvrakNo, Gelir, Gider, MmzId, Magaza, Kaynak, ViewTip, GlGd
FROM         (SELECT     CASE BnHTur WHEN 2 THEN 'Banka Masrafı' ELSE 'Bankadan Firma Kredi İşlemleri' END AS TurAd, dbo.Tb_BankaHareket.BnHTarih AS Tarih, 
                                              dbo.Tb_BankaHareket.BnHSaat AS Saat, dbo.Tb_BankaHareket.BnHEvrakNo AS EvrakNo, 
                                              CASE BnHTipi WHEN 0 THEN BnHTutar WHEN 1 THEN 0 END AS Gelir, CASE BnHTipi WHEN 0 THEN 0 WHEN 1 THEN BnHTutar END AS Gider, 
                                              dbo.Tb_BankaHareket.BnHMsMerkId AS MmzId, dbo.Tb_BankaHareket.BnHMagazaKodu AS Magaza, dbo.Tb_HesapKart.HesAd AS Kaynak, 
                                              dbo.Tb_BankaHareket.BnHTur AS ViewTip, dbo.Tb_BankaHareket.BnHTipi AS GlGd
                       FROM          dbo.Tb_BankaHareket LEFT OUTER JOIN
                                              dbo.Tb_HesapKart ON dbo.Tb_BankaHareket.BnHHesapId = dbo.Tb_HesapKart.HesId
                       WHERE      (dbo.Tb_BankaHareket.BnHTur = 2) OR
                                              (dbo.Tb_BankaHareket.BnHTur = 6)
                       UNION ALL
                       SELECT     CASE CrHTipi WHEN 0 THEN 'Nakit' WHEN 1 THEN 'Kredi Kartı' WHEN 4 THEN 'Çek' WHEN 5 THEN 'Senet' WHEN 2 THEN 'Havale' WHEN 3 THEN 'EFT' WHEN
                                              6 THEN 'Ödeme Kartı' END + ' ' + CASE CrHTur WHEN 0 THEN 'Devir' WHEN 1 THEN 'Tahsilat' WHEN 2 THEN 'Ödeme' END AS Expr1, 
                                             dbo.Tb_CariHareket.CrHTarih, dbo.Tb_CariHareket.CrHSaat, dbo.Tb_CariHareket.CrhEvrakNo, CASE CrHTur WHEN 1 THEN CrHTutar ELSE 0 END AS Gelir, 
                                             CASE CrHTur WHEN 1 THEN 0 ELSE CrHTutar END AS Gider, dbo.Tb_CariHareket.CrHMsMerkId, dbo.Tb_CariHareket.CrHMagazaKodu, 
                                             dbo.Tb_CariKart.CarUnvan, dbo.Tb_CariHareket.CrHTur + 10 AS Expr2, dbo.Tb_CariHareket.CrHTur
                       FROM         dbo.Tb_CariHareket LEFT OUTER JOIN
                                             dbo.Tb_CariKart ON dbo.Tb_CariHareket.CrHCariId = dbo.Tb_CariKart.CarId
                       WHERE     (dbo.Tb_CariHareket.CrHTur > 0)
                       UNION ALL
                       SELECT     CASE CsHTur WHEN 2 THEN 'Verilen Firma Evrağı Portföye İade' WHEN 3 THEN 'Verilen Müşteri Evrağı Portföye İade' WHEN 4 THEN 'Müşteri Evrağı Sahibine İade'
                                              WHEN 5 THEN 'Alınan Müşteri Evrağı Karşılıksız' WHEN 7 THEN 'Teminat Müşteri Evrağının Tahsili' WHEN 9 THEN 'Teminat Firma Evrağı Ödemesi' WHEN
                                              11 THEN 'Teminat İçin Alınan Evrak Tahsili/İadesi' END AS Expr1, dbo.Tb_CkSnHareket.CsHTarih, dbo.Tb_CkSnHareket.CsHSaat, 
                                             dbo.Tb_CkSnHareket.CsHEvrakNo, 
                                             CASE CsHTur WHEN 2 THEN CsHTutar WHEN 3 THEN CsHTutar WHEN 11 THEN CASE CsHTipi WHEN 2 THEN 0 ELSE CsHTutar END ELSE 0 END AS Expr2,
                                              CASE CsHTur WHEN 4 THEN CsHTutar WHEN 5 THEN CsHTutar WHEN 9 THEN CASE CsHTipi WHEN 2 THEN 0 ELSE CsHTutar END ELSE 0 END AS Expr3,
                                              dbo.Tb_CkSnHareket.CsHMsMerkId, dbo.Tb_CkSnHareket.CsHMagazaKodu, CONVERT(Nvarchar, dbo.Tb_CkSnKart.CksVadeTarih) 
                                             + ' Vadeli Evrak' AS Expr4, dbo.Tb_CkSnHareket.CsHTur + 20 AS Expr5, 
                                             CASE CsHTur WHEN 2 THEN 0 WHEN 3 THEN 0 WHEN 11 THEN CASE CsHTipi WHEN 2 THEN 1 ELSE 0 END ELSE 1 END AS Expr6
                       FROM         dbo.Tb_CkSnHareket LEFT OUTER JOIN
                                             dbo.Tb_CkSnKart ON dbo.Tb_CkSnHareket.CsHEvrakId = dbo.Tb_CkSnKart.CksId
                       WHERE     (dbo.Tb_CkSnHareket.CsHTur = 2) AND (dbo.Tb_CkSnHareket.CsHTur = 3) AND (dbo.Tb_CkSnHareket.CsHTur = 4) AND (dbo.Tb_CkSnHareket.CsHTur = 5) 
                                             AND (dbo.Tb_CkSnHareket.CsHTur = 9) AND (dbo.Tb_CkSnHareket.CsHTipi <> 2) AND (dbo.Tb_CkSnHareket.CsHTur = 11) AND 
                                             (dbo.Tb_CkSnHareket.CsHTipi <> 2)
                       UNION ALL
                       SELECT     CASE KsHTur WHEN 4 THEN 'Kasadan Masraf Ödeme' WHEN 8 THEN 'Kasadan Kredi İşlemi' WHEN 11 THEN 'Firma Kredi Kartıyla Masraf Ödeme' END AS Expr1,
                                              dbo.Tb_KasaHareket.KsHTarih, dbo.Tb_KasaHareket.KsHSaat, dbo.Tb_KasaHareket.KsHEvrakNo, 
                                             CASE KsHTipi WHEN 0 THEN KsHTutar WHEN 1 THEN 0 END AS Gelir, CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHTutar END AS Gider, 
                                             dbo.Tb_KasaHareket.KsHMsMerkId, dbo.Tb_KasaHareket.KsHMagazaKodu, 
                                             CASE KshTur WHEN 11 THEN FkkAd ELSE dbo.Tb_KasaKart.KasAd END AS Expr2, 40 + dbo.Tb_KasaHareket.KsHTur AS Expr3, 
                                             dbo.Tb_KasaHareket.KsHTipi
                       FROM         dbo.Tb_KasaHareket LEFT OUTER JOIN
                                             dbo.Tb_KasaKart ON dbo.Tb_KasaHareket.KsHKasaId = dbo.Tb_KasaKart.KasId LEFT OUTER JOIN
                                             dbo.Tb_FirmaKrKart ON dbo.Tb_KasaHareket.KsHKasaId = dbo.Tb_FirmaKrKart.FkkId
                       WHERE     (dbo.Tb_KasaHareket.KsHTur = 4) OR
                                             (dbo.Tb_KasaHareket.KsHTur = 8) OR
                                             (dbo.Tb_KasaHareket.KsHTur = 11)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_MasrafHareketler]
AS
SELECT        TOP (100) PERCENT Id, FisId, Tur, Tarih, Saat, EvrakNo, MasrafId, GlGd, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Gelir, Gider, MmzId, ViewTip, Aciklama, Magaza
FROM            (SELECT        KsHId AS Id, KsHFisId AS FisId, KsHTur AS Tur, KsHTarih AS Tarih, KsHSaat AS Saat, KsHEvrakNo AS EvrakNo, CASE KsHTur WHEN 4 THEN KsHKarsiHsId ELSE KsHKasaId END AS MasrafId, KsHTipi AS GlGd, 
                                                    CASE KsHTur WHEN 4 THEN KsHKasaId ELSE KsHKarsiHsId END AS KarsiHsId, KsHTutar AS Tutar, KsHFaiz AS Faiz, KsHDovizTip AS DovizTip, KsHDoviz AS Doviz, KsHDovizTutar AS DovizTutar, KsHKur AS Kur, 
                                                    CASE KsHTipi WHEN 0 THEN KsHTutar WHEN 1 THEN 0 END AS Gelir, CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHTutar END AS Gider, KsHMsMerkId AS MmzId, KsHTur AS ViewTip, 
                                                    KsHAciklama AS Aciklama, KsHMagazaKodu AS Magaza
                          FROM            dbo.Tb_KasaHareket
                          WHERE        (KsHTur = 1) OR
                                                    (KsHTur = 4) OR
                                                    (KsHTur = 10) OR
                                                    (KsHTur = 11)
                          UNION ALL
                          SELECT        BnHId, BnHFisId, BnHTur, BnHTarih, BnHSaat, BnHEvrakNo, BnHKarsiHsId, BnHTipi, BnHHesapId, BnHTutar, BnHFaiz, BnHDovizTip, BnHDoviz, BnHDovizTutar, BnHKur, 
                                                   CASE BnHTipi WHEN 0 THEN BnHTutar WHEN 1 THEN 0 END AS Gelir, CASE BnHTipi WHEN 0 THEN 0 WHEN 1 THEN BnHTutar END AS Gider, BnHMsMerkId, 20 AS ViewTip, BnHAciklama, BnHMagazaKodu
                          FROM            dbo.Tb_BankaHareket
                          WHERE        (BnHTur = 2)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_OdKartHareketler]
AS
SELECT   TOP (100) PERCENT Id, FisId, Tur, Tarih, Saat, EvrakNo, OdKartId, Tipi, BrAl, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Odeme, Satis, MmzId, ViewTip, Aciklama, Magaza
FROM         (SELECT   CrHId AS Id, CrHFisId AS FisId, CrHTur AS Tur, CrHTarih AS Tarih, CrHSaat AS Saat, CrhEvrakNo AS EvrakNo, CrHKarsiHsId AS OdKartId, CrHTipi AS Tipi, 
                                                    CASE CrhBrAl WHEN 0 THEN 1 WHEN 1 THEN 0 END AS BrAl, CrHCariId AS KarsiHsId, CrHTutar AS Tutar, CrHFaiz AS Faiz, CrHDovizTip AS DovizTip, CrHDoviz AS Doviz, 
                                                    CrHDovizTutar AS DovizTutar, CrHKur AS Kur, CASE CrHBrAl WHEN 0 THEN CrHTutar WHEN 1 THEN 0 END AS Odeme, 
                                                    CASE CrhBrAl WHEN 0 THEN 0 WHEN 1 THEN CrHTutar END AS Satis, CrHMsMerkId AS MmzId, 0 AS ViewTip, CrHAciklama AS Aciklama, CrHMagazaKodu AS Magaza
                           FROM         dbo.Tb_CariHareket
                           WHERE     (CrHTipi = 6)
                           UNION ALL
                           SELECT   Tb_CariHareket_1.CrHId, Tb_CariHareket_1.CrHFisId, Tb_CariHareket_1.CrHTur, Tb_CariHareket_1.CrHTarih, Tb_CariHareket_1.CrHSaat, Tb_CariHareket_1.CrhEvrakNo, 
                                                    dbo.Tb_OdemeKart.OdKId, Tb_CariHareket_1.CrHTipi, 1 AS Expr1, dbo.Tb_CariKart.CarId, Tb_CariHareket_1.CrHTutar, Tb_CariHareket_1.CrHFaiz, 
                                                    Tb_CariHareket_1.CrHDovizTip, Tb_CariHareket_1.CrHDoviz, Tb_CariHareket_1.CrHDovizTutar, Tb_CariHareket_1.CrHKur, Tb_CariHareket_1.CrHTutar AS Expr2, 0 AS Expr3, 
                                                    Tb_CariHareket_1.CrHMsMerkId, 1 AS Expr4, Tb_CariHareket_1.CrHAciklama, Tb_CariHareket_1.CrHMagazaKodu
                           FROM         dbo.Tb_OdemeKart LEFT OUTER JOIN
                                                    dbo.Tb_CariKart ON dbo.Tb_OdemeKart.OdKCariId = dbo.Tb_CariKart.CarId LEFT OUTER JOIN
                                                    dbo.Tb_CariHareket AS Tb_CariHareket_1 ON dbo.Tb_CariKart.CarId = Tb_CariHareket_1.CrHCariId
                           WHERE     (Tb_CariHareket_1.CrHTur = 1)
                           UNION ALL
                           SELECT   KsHId, KsHFisId, KsHTur, KsHTarih, KsHSaat, KsHEvrakNo, KsHKasaId, KsHTipi, KsHTipi AS Expr1, KsHKarsiHsId, KsHTutar, KsHFaiz, KsHDovizTip, KsHDoviz, KsHDovizTutar, 
                                                    KsHKur, CASE KsHTipi WHEN 0 THEN 0 WHEN 1 THEN KsHTutar END AS Expr2, CASE KsHTipi WHEN 0 THEN KsHTutar WHEN 1 THEN 0 END AS Expr3, KsHMsMerkId, 2 AS Expr4, 
                                                    KsHAciklama, KsHMagazaKodu
                           FROM         dbo.Tb_KasaHareket
                           WHERE     (KsHTur = 3)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_PosHesaplari]
AS
SELECT     TOP (100) PERCENT PosId, Kod, Ad, PosAd, PosSira
FROM         (SELECT     HesId AS PosId, HesKod AS Kod, HesAd AS Ad, HesPosAd AS PosAd, CONVERT(nvarchar, HesId) + '-0000' AS PosSira
                       FROM          dbo.Tb_HesapKart
                       UNION ALL
                       SELECT     EkpId, CONVERT(nvarchar, EkpId) AS Expr1, '' AS Expr2, EkpAd, CONVERT(nvarchar, EkpHesapId) + '-' + CONVERT(nvarchar, EkpId) AS Expr3
                       FROM         dbo.Tb_EkPosKart) AS A

GO
CREATE VIEW [dbo].[vw_SatisKalemleri]
AS
SELECT        TOP (100) PERCENT Id, FısId, Tur, Tarih, Saat, EvrakNo, CariId, Tipi, BrAl, KarsiHsId, Tutar, Faiz, DovizTip, Doviz, DovizTutar, Kur, Borc, Alacak, MmzId, ViewTip, PersId, StokId, Birim, Miktar, Fiyat, SatTut, SatTip, Vade, 
                         Aciklama, FarkGun, SatTah, MalyFiyat, MalyTutar, BrimMiktar, BrimFiyat, TopIsk, Magaza, Sira, KdvTip, KdvOran, KdvTutar, KdvMatrah, OzelKod
FROM            (SELECT        dbo.Tb_Fatura.FatId AS Id, 0 AS FısId, dbo.Tb_Fatura.FatTur AS Tur, dbo.Tb_Fatura.FatTarih AS Tarih, dbo.Tb_Fatura.FatSaat AS Saat, dbo.Tb_Fatura.FatNo AS EvrakNo, dbo.Tb_Fatura.FatCariId AS CariId, 
                                                    dbo.Tb_Fatura.FatTip AS Tipi, 0 AS BrAl, 0 AS KarsiHsId, dbo.Tb_Fatura.FatGenTop AS Tutar, 0 AS Faiz, '' AS DovizTip, 'TL' AS Doviz, 0 AS DovizTutar, 0 AS Kur, 
                                                    CASE FatTur WHEN 0 THEN 0 WHEN 1 THEN FatGenTop WHEN 2 THEN 0 WHEN 3 THEN FatGenTop WHEN 4 THEN FatGenTop WHEN 5 THEN 0 WHEN 6 THEN 0 END AS Borc, 
                                                    CASE FatTur WHEN 0 THEN FatGenTop WHEN 1 THEN 0 WHEN 2 THEN FatGenTop WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FatGenTop WHEN 6 THEN FatGenTop END AS Alacak, 
                                                    dbo.Tb_Fatura.FatMsMerkId AS MmzId, 30 + dbo.Tb_Fatura.FatTur AS ViewTip, dbo.Tb_Fatura.FatPersId AS PersId, dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_FatKalem.FtKBirim AS Birim, 
                                                    dbo.Tb_FatKalem.FtKMiktar AS Miktar, dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS SatTut, dbo.Tb_FatKalem.FtKSatirTipi AS SatTip, dbo.Tb_Fatura.FatVade AS Vade, 
                                                    dbo.Tb_Fatura.FatAciklama AS Aciklama, DATEDIFF(d, dbo.Tb_Fatura.FatTarih, dbo.Tb_Fatura.FatVade) AS FarkGun, 1 AS SatTah, dbo.Tb_FatKalem.FtKMalyFiyat AS MalyFiyat, 
                                                    dbo.Tb_FatKalem.FtKMalyTutar AS MalyTutar, dbo.Tb_FatKalem.FtKBirimMiktar AS BrimMiktar, dbo.Tb_FatKalem.FtKBirimFiyat AS BrimFiyat, dbo.Tb_Fatura.FatTopIsk AS TopIsk, 
                                                    dbo.Tb_Fatura.FatMagazaKodu AS Magaza, 0 AS Sira, dbo.Tb_FatKalem.FtKKdvTipi AS KdvTip, dbo.Tb_FatKalem.FtKKdvOran AS KdvOran, dbo.Tb_FatKalem.FtKKdvTutar AS KdvTutar, 
                                                    dbo.Tb_FatKalem.FtKKdvMatrah AS KdvMatrah, dbo.Tb_Fatura.FatOzelKod1 AS OzelKod
                          FROM            dbo.Tb_Fatura LEFT OUTER JOIN
                                                    dbo.Tb_FatKalem ON dbo.Tb_FatKalem.FtKFisId = dbo.Tb_Fatura.FatId
                          WHERE        (dbo.Tb_Fatura.FatKaynak = 0 OR
                                                    dbo.Tb_Fatura.FatKaynak = 1 OR
                                                    dbo.Tb_Fatura.FatKaynak = 3 OR
                                                    dbo.Tb_Fatura.FatKaynak = 4) AND (dbo.Tb_Fatura.FatTur = 1 OR
                                                    dbo.Tb_Fatura.FatTur = 3)
                          UNION ALL
                          SELECT        dbo.Tb_Satis.SatId, 0 AS Expr1, dbo.Tb_Satis.SatTur, dbo.Tb_Satis.SatTarih, dbo.Tb_Satis.SatSaat, dbo.Tb_Satis.SatNo, dbo.Tb_Satis.SatCariId, 0 AS Expr2, 0 AS Expr3, 0 AS Expr4, dbo.Tb_Satis.SatGenTop, 
                                                   0 AS Expr5, '' AS Expr6, 'TL' AS Expr7, 0 AS Expr8, 0 AS Expr9, dbo.Tb_Satis.SatGenTop AS Expr10, 0 AS Expr11, dbo.Tb_Satis.SatMsMerkId, 40 AS ViewTip, dbo.Tb_Satis.SatPersId AS PersId, 
                                                   dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlBirim AS Birim, dbo.Tb_SatKalem.SKlMiktar AS Miktar, dbo.Tb_SatKalem.SKlFiyat AS Fiyat, dbo.Tb_SatKalem.SKlTutar AS SatTut, 
                                                   dbo.Tb_SatKalem.SKlSatirTipi AS SatTip, dbo.Tb_Satis.SatVade AS Vade, dbo.Tb_Satis.SatAciklama AS Aciklama, DATEDIFF(d, dbo.Tb_Satis.SatTarih, dbo.Tb_Satis.SatVade) AS FarkGun, 1 AS SatTah, 
                                                   dbo.Tb_SatKalem.SKlMalyFiyat AS MalyFiyat, dbo.Tb_SatKalem.SKlMalyTutar AS MalyTutar, dbo.Tb_SatKalem.SKlBirimMiktar AS BrimMiktar, dbo.Tb_SatKalem.SKlBirimFiyat AS BrimFiyat, 
                                                   dbo.Tb_Satis.SatTopIsk AS TopIsk, dbo.Tb_Satis.SatMagazaKodu, 0 AS Expr12, dbo.Tb_SatKalem.SKlKdvTipi, dbo.Tb_SatKalem.SKlKdvOran, dbo.Tb_SatKalem.SKlKdvTutar AS KdvTutar, 
                                                   dbo.Tb_SatKalem.SKlKdvMatrah AS KdvMatrah, dbo.Tb_Satis.SatOzelKod1 AS OzelKod
                          FROM            dbo.Tb_Satis LEFT OUTER JOIN
                                                   dbo.Tb_SatKalem ON dbo.Tb_SatKalem.SKlFisId = dbo.Tb_Satis.SatId
                          UNION ALL
                          SELECT        dbo.Tb_YzKsSatis.YzKId, 0 AS Expr1, 0 AS Expr2, dbo.Tb_YzKsSatis.YzKTarih, dbo.Tb_YzKsSatis.YzKSaat, dbo.Tb_YzKsSatis.YzKNo, dbo.Tb_YzKsSatis.YzKCariId, 0 AS Expr3, 0 AS Expr4, 0 AS Expr5, 
                                                   dbo.Tb_YzKsSatis.YzKGenTop, 0 AS Expr6, '' AS Expr7, 'TL' AS Expr8, 0 AS Expr9, 0 AS Expr10, dbo.Tb_YzKsSatis.YzKGenTop AS Expr11, 0 AS Expr12, dbo.Tb_YzKsSatis.YzKMsMerkId, 41 AS Expr13, 
                                                   dbo.Tb_YzKsSatis.YzKPersId, dbo.Tb_YzKKalem.YkKStokId, dbo.Tb_YzKKalem.YkKBirim, dbo.Tb_YzKKalem.YkKMiktar, dbo.Tb_YzKKalem.YkKFiyat, dbo.Tb_YzKKalem.YkKTutar, 0 AS Expr14, '' AS Expr15, 
                                                   dbo.Tb_YzKKalem.YkKAciklama, 0 AS Expr16, 1 AS Expr17, dbo.Tb_YzKKalem.YkKMalyFiyat, dbo.Tb_YzKKalem.YkKMalyTutar, dbo.Tb_YzKKalem.YkKBirimMiktar, dbo.Tb_YzKKalem.YkKBirimFiyat, 0 AS Expr18, 
                                                   dbo.Tb_YzKsSatis.YzKMagazaKodu, 0 AS Expr19, 1 AS Expr20, dbo.Tb_YzKKalem.YkKKdvOran, dbo.Tb_YzKKalem.YkKKdvTutar AS KdvTutar, dbo.Tb_YzKKalem.YkKKdvMatrah AS KdvMatrah, 
                                                   dbo.Tb_YzKsSatis.YzKOzelKod1
                          FROM            dbo.Tb_YzKsSatis LEFT OUTER JOIN
                                                   dbo.Tb_YzKKalem ON dbo.Tb_YzKsSatis.YzKId = dbo.Tb_YzKKalem.YkKFisId) AS A
GO
CREATE VIEW [dbo].[vw_SatisKalemGrup]
AS
SELECT        Tarih, StokId, SUM(Miktar) AS Miktar, SUM(MalyTutar) AS MlyTutar, SUM(SatTut) AS SatTut, SUM(SatTut) / SUM(Miktar) AS OrtTut, SatTip, OzelKod
FROM            dbo.vw_SatisKalemleri
GROUP BY Tarih, StokId, SatTip, OzelKod
GO
CREATE VIEW [dbo].[vw_SiprHareketler]
AS
SELECT        TOP (100) PERCENT Tarih, Saat, Tur, BaslikId, ViewTipi, GirenMik, CikanMik, Fiyat, Tutar, GirCik, StokId, TTarih1, TTarih2, DepoId, Magaza
FROM            (SELECT        dbo.Tb_SatKalem.SKlTarih AS Tarih, dbo.Tb_SatKalem.SKlSaat AS Saat, 'İleri Teslim Satış' AS Tur, dbo.Tb_SatKalem.SKlFisId AS BaslikId, 1 AS ViewTipi, 0 AS GirenMik, 
                                                    dbo.Tb_SatKalem.SKlBirimMiktar - dbo.Tb_SatKalem.SKlTeslimMiktar AS CikanMik, dbo.Tb_SatKalem.SKlBirimFiyat AS Fiyat, (dbo.Tb_SatKalem.SKlBirimMiktar - dbo.Tb_SatKalem.SKlTeslimMiktar) 
                                                    * dbo.Tb_SatKalem.SKlBirimFiyat AS Tutar, 0 AS GirCik, dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlTesTar1 AS TTarih1, dbo.Tb_SatKalem.SKlTesTar2 AS TTarih2, 
                                                    dbo.Tb_SatKalem.SKlDepoId AS DepoId, dbo.Tb_Satis.SatMagazaKodu AS Magaza
                          FROM            dbo.Tb_SatKalem LEFT OUTER JOIN
                                                    dbo.Tb_Satis ON dbo.Tb_SatKalem.SKlFisId = dbo.Tb_Satis.SatId
                          WHERE        (dbo.Tb_SatKalem.SKlSatirTipi = 0) AND (dbo.Tb_SatKalem.SKlTeslim = 1)
                          UNION ALL
                          SELECT        dbo.Tb_SipKalem.SpKTarih, dbo.Tb_SipKalem.SpKSaat, CASE SpKTipi WHEN 1 THEN 'Gelecek Sipariş' ELSE 'Teslim Edilecek Sipariş' END AS Expr1, dbo.Tb_SipKalem.SpKFisId, 2 AS Expr2, 
                                                   CASE SpKTipi WHEN 1 THEN SpKBirimMiktar - SpKTeslimMiktar ELSE 0 END AS Expr3, CASE SpKTipi WHEN 0 THEN SpKBirimMiktar - SpKTeslimMiktar ELSE 0 END AS Expr4, dbo.Tb_SipKalem.SpKBirimFiyat, 
                                                   (dbo.Tb_SipKalem.SpKBirimMiktar - dbo.Tb_SipKalem.SpKTeslimMiktar) * dbo.Tb_SipKalem.SpKBirimFiyat AS Expr5, dbo.Tb_SipKalem.SpKTipi AS GirCik, dbo.Tb_SipKalem.SpKStokId, 
                                                   dbo.Tb_SipKalem.SpKTesTar1, dbo.Tb_SipKalem.SpKTesTar2, dbo.Tb_SipKalem.SpKDepoId, dbo.Tb_Siparis.SipMagazaKodu
                          FROM            dbo.Tb_SipKalem LEFT OUTER JOIN
                                                   dbo.Tb_Siparis ON dbo.Tb_SipKalem.SpKFisId = dbo.Tb_Siparis.SipId
                          WHERE        ((SELECT        ISNULL(SUM(IrKMiktar), 0) AS Expr1
                                                      FROM            dbo.Tb_IrsKalem
                                                      WHERE        (IrKKaynak = 3) AND (IrKKaynakId = dbo.Tb_SipKalem.SpKId)) +
                                                       (SELECT        ISNULL(SUM(FtKMiktar), 0) AS Expr1
                                                         FROM            dbo.Tb_FatKalem
                                                         WHERE        (FtKKaynak = 3) AND (FtKKaynakId = dbo.Tb_SipKalem.SpKId)) < dbo.Tb_SipKalem.SpKMiktar)) AS A
ORDER BY Tarih
GO
CREATE VIEW [dbo].[vw_StokHareketler]
AS
SELECT   TOP (100) PERCENT Id, Tipi, BaslikTipi, Tarih, KdvTipi, StokId, Birim, Miktar, Fiyat, Tutar, Doviz, DovizTip, DovizFiyat, Kur, DepoId, GirenDepoId, PartiId, BirimMiktar, BirimFiyat, MalyFiyat, 
                         MalyTutar, KdvOran, GirenMik, CikanMik, ViewTipi, GirCik, Satis, Saat, BaslikId, Magaza
FROM         (SELECT   dbo.Tb_StkHarKalem.ShkId AS Id, dbo.Tb_StkHarKalem.ShkTipi AS Tipi, dbo.Tb_StkHarKalem.ShkBaslikTipi AS BaslikTipi, dbo.Tb_StkHarKalem.ShkTarih AS Tarih, 
                                                    dbo.Tb_StkHarKalem.ShkKdvTipi AS KdvTipi, dbo.Tb_StkHarKalem.ShkStokId AS StokId, dbo.Tb_StkHarKalem.ShkBirim AS Birim, dbo.Tb_StkHarKalem.ShkMiktar AS Miktar, 
                                                    dbo.Tb_StkHarKalem.ShkFiyat AS Fiyat, dbo.Tb_StkHarKalem.ShkTutar AS Tutar, dbo.Tb_StkHarKalem.ShkDoviz AS Doviz, dbo.Tb_StkHarKalem.ShkDovizTip AS DovizTip, 
                                                    dbo.Tb_StkHarKalem.ShkDovizFiyat AS DovizFiyat, dbo.Tb_StkHarKalem.ShkKur AS Kur, dbo.Tb_StkHarKalem.ShkDepoId AS DepoId, 
                                                    dbo.Tb_StkHarKalem.ShkGirenDepoId AS GirenDepoId, dbo.Tb_StkHarKalem.ShkPartiId AS PartiId, dbo.Tb_StkHarKalem.ShkBirimMiktar AS BirimMiktar, 
                                                    dbo.Tb_StkHarKalem.ShkBirimFiyat AS BirimFiyat, dbo.Tb_StkHarKalem.ShkMalyFiyat AS MalyFiyat, dbo.Tb_StkHarKalem.ShkMalyTutar AS MalyTutar, 
                                                    dbo.Tb_StkHarKalem.ShkKdvOran AS KdvOran, 
                                                    CASE ShkBaslikTipi WHEN 2 THEN 0 ELSE CASE ShkTipi WHEN 0 THEN ShkBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN ShkBirimMiktar WHEN 3 THEN 0 END END AS GirenMik, 
                                                    CASE ShkBaslikTipi WHEN 2 THEN ShkBirimMiktar ELSE CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShkBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN ShkBirimMiktar END END AS
                                                     CikanMik, dbo.Tb_StkHarKalem.ShkBaslikTipi AS ViewTipi, 
                                                    CASE ShkBaslikTipi WHEN 2 THEN 1 ELSE CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 END END AS GirCik, 0 AS Satis, 
                                                    dbo.Tb_StkHarKalem.ShkSaat AS Saat, dbo.Tb_StkHarKalem.ShkBaslikId AS BaslikId, dbo.Tb_StkHarBaslik.ShbMagazaKodu AS Magaza
                           FROM         dbo.Tb_StkHarKalem LEFT OUTER JOIN
                                                    dbo.Tb_StkHarBaslik ON dbo.Tb_StkHarBaslik.ShbId = dbo.Tb_StkHarKalem.ShkBaslikId
                           WHERE     (dbo.Tb_StkHarKalem.ShkBaslikTipi <> 3)
                           UNION ALL
                           SELECT   Tb_StkHarKalem_1.ShkId AS Id, Tb_StkHarKalem_1.ShkTipi AS Tipi, Tb_StkHarKalem_1.ShkBaslikTipi AS BaslikTipi, Tb_StkHarKalem_1.ShkTarih AS Tarih, 
                                                    Tb_StkHarKalem_1.ShkKdvTipi AS KdvTipi, Tb_StkHarKalem_1.ShkStokId AS StokId, Tb_StkHarKalem_1.ShkBirim AS Birim, Tb_StkHarKalem_1.ShkMiktar AS Miktar, 
                                                    Tb_StkHarKalem_1.ShkFiyat AS Fiyat, Tb_StkHarKalem_1.ShkTutar AS Tutar, Tb_StkHarKalem_1.ShkDoviz AS Doviz, Tb_StkHarKalem_1.ShkDovizTip AS DovizTip, 
                                                    Tb_StkHarKalem_1.ShkDovizFiyat AS DovizFiyat, Tb_StkHarKalem_1.ShkKur AS Kur, Tb_StkHarKalem_1.ShkGirenDepoId AS DepoId, 
                                                    Tb_StkHarKalem_1.ShkDepoId AS GirenDepoId, Tb_StkHarKalem_1.ShkPartiId AS PartiId, Tb_StkHarKalem_1.ShkBirimMiktar AS BirimMiktar, 
                                                    Tb_StkHarKalem_1.ShkBirimFiyat AS BirimFiyat, Tb_StkHarKalem_1.ShkMalyFiyat AS MalyFiyat, Tb_StkHarKalem_1.ShkMalyTutar AS MalyTutar, 
                                                    Tb_StkHarKalem_1.ShkKdvOran AS KdvOran, Tb_StkHarKalem_1.ShkBirimMiktar, 0 AS Expr1, Tb_StkHarKalem_1.ShkBaslikTipi AS ViewTipi, 0 AS Expr2, 0 AS Satis, 
                                                    Tb_StkHarKalem_1.ShkSaat, Tb_StkHarKalem_1.ShkBaslikId AS BaslikId, Tb_StkHarBaslik_1.ShbMagazaKodu
                           FROM         dbo.Tb_StkHarKalem AS Tb_StkHarKalem_1 LEFT OUTER JOIN
                                                    dbo.Tb_StkHarBaslik AS Tb_StkHarBaslik_1 ON Tb_StkHarBaslik_1.ShbId = Tb_StkHarKalem_1.ShkBaslikId
                           WHERE     (Tb_StkHarKalem_1.ShkBaslikTipi = 2)
                           UNION ALL
                           SELECT   dbo.Tb_IrsKalem.IrKId AS Id, dbo.Tb_IrsKalem.IrKTipi AS Tipi, dbo.Tb_IrsKalem.IrKTipi AS BaslikTipi, dbo.Tb_IrsKalem.IrKTarih AS Tarih, dbo.Tb_IrsKalem.IrKKdvTipi AS KdvTipi, 
                                                    dbo.Tb_IrsKalem.IrKStokId AS StokId, dbo.Tb_IrsKalem.IrKBirim AS Birim, dbo.Tb_IrsKalem.IrKMiktar AS Miktar, dbo.Tb_IrsKalem.IrKFiyat AS Fiyat, 
                                                    dbo.Tb_IrsKalem.IrKTutar AS Tutar, dbo.Tb_IrsKalem.IrKDoviz AS Doviz, dbo.Tb_IrsKalem.IrKDovizTipi AS DovizTip, dbo.Tb_IrsKalem.IrKDovizFiyat AS DovizFiyat, 
                                                    dbo.Tb_IrsKalem.IrKKur AS Kur, dbo.Tb_IrsKalem.IrKDepoId AS DepoId, 0 AS GirenDepoId, dbo.Tb_IrsKalem.IrKPartiId AS PartiId, 
                                                    dbo.Tb_IrsKalem.IrKBirimMiktar AS BirimMiktar, dbo.Tb_IrsKalem.IrKBirimFiyat AS BirimFiyat, dbo.Tb_IrsKalem.IrKMalyFiyat AS MalyFiyat, 
                                                    dbo.Tb_IrsKalem.IrKMalyTutar AS MalyTutar, dbo.Tb_IrsKalem.IrKKdvOran AS KdvOran, 
                                                    CASE IrkTipi WHEN 0 THEN IrKBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN IrKBirimMiktar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN IrKBirimMiktar WHEN 6 THEN IrKBirimMiktar
                                                     WHEN 7 THEN 0 WHEN 8 THEN 0 WHEN 9 THEN IrKBirimMiktar END AS GirenMik, 
                                                    CASE IrkTipi WHEN 0 THEN 0 WHEN 1 THEN IrKBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN IrKBirimMiktar WHEN 4 THEN IrKBirimMiktar WHEN 5 THEN 0 WHEN 6 THEN 0 WHEN 7 THEN
                                                     IrKBirimMiktar WHEN 8 THEN IrKBirimMiktar WHEN 9 THEN 0 END AS CikanMik, dbo.Tb_IrsKalem.IrKTipi + 10 AS ViewTipi, 
                                                    CASE IrkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 WHEN 6 THEN 0 WHEN 7 THEN 1 WHEN 8 THEN 1 WHEN 9 THEN 0 END
                                                     AS GirCik, 0 AS Satis, dbo.Tb_IrsKalem.IrKSaat, dbo.Tb_IrsKalem.IrKFisId, dbo.Tb_Irsaliye.IrsMagazaKodu
                           FROM         dbo.Tb_IrsKalem LEFT OUTER JOIN
                                                    dbo.Tb_Irsaliye ON dbo.Tb_Irsaliye.IrsId = dbo.Tb_IrsKalem.IrKFisId
                           WHERE     (dbo.Tb_IrsKalem.IrKKaynak = 0 OR
                                                    dbo.Tb_IrsKalem.IrKKaynak = 3 OR
                                                    dbo.Tb_IrsKalem.IrKKaynak = 4) AND (dbo.Tb_IrsKalem.IrKMiktar - dbo.Tb_IrsKalem.IrKFtMiktar > 0)
                           UNION ALL
                           SELECT   dbo.Tb_FatKalem.FtKId AS Id, dbo.Tb_FatKalem.FtKTipi AS Tipi, dbo.Tb_FatKalem.FtKTipi AS BaslikTipi, dbo.Tb_FatKalem.FtKTarih AS Tarih, 
                                                    dbo.Tb_FatKalem.FtKKdvTipi AS KdvTipi, dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_FatKalem.FtKBirim AS Birim, dbo.Tb_FatKalem.FtKMiktar AS Miktar, 
                                                    dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS Tutar, dbo.Tb_FatKalem.FtKDoviz AS Doviz, dbo.Tb_FatKalem.FtKDovizTipi AS DovizTip, 
                                                    dbo.Tb_FatKalem.FtKDovizFiyat AS DovizFiyat, dbo.Tb_FatKalem.FtKKur AS Kur, dbo.Tb_FatKalem.FtKDepoId AS DepoId, 0 AS GirenDepoId, 
                                                    dbo.Tb_FatKalem.FtKPartiId AS PartiId, dbo.Tb_FatKalem.FtKBirimMiktar AS BirimMiktar, dbo.Tb_FatKalem.FtKBirimFiyat AS BirimFiyat, 
                                                    dbo.Tb_FatKalem.FtKMalyFiyat AS MalyFiyat, dbo.Tb_FatKalem.FtKMalyTutar AS MalyTutar, dbo.Tb_FatKalem.FtKKdvOran AS KdvOran, 
                                                    CASE FtkTipi WHEN 0 THEN FtKBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN FtKBirimMiktar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FtKBirimMiktar WHEN 6 THEN FtKBirimMiktar
                                                     END AS GirenMik, 
                                                    CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN FtKBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN FtKBirimMiktar WHEN 4 THEN FtKBirimMiktar WHEN 5 THEN 0 WHEN 6 THEN 0 END AS
                                                     CikanMik, dbo.Tb_FatKalem.FtKTipi + 20 AS ViewTipi, 
                                                    CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 WHEN 6 THEN 0 END AS GirCik, 
                                                    CASE FtKTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 ELSE 0 END AS Satis, dbo.Tb_FatKalem.FtKSaat, dbo.Tb_FatKalem.FtKFisId, 
                                                    dbo.Tb_Fatura.FatMagazaKodu
                           FROM         dbo.Tb_FatKalem LEFT OUTER JOIN
                                                    dbo.Tb_Fatura ON dbo.Tb_Fatura.FatId = dbo.Tb_FatKalem.FtKFisId
                           WHERE     (dbo.Tb_FatKalem.FtKSatirTipi = 0) AND (dbo.Tb_FatKalem.FtKKaynak = 0 OR
                                                    dbo.Tb_FatKalem.FtKKaynak = 1 OR
                                                    dbo.Tb_FatKalem.FtKKaynak = 3 OR
                                                    dbo.Tb_FatKalem.FtKKaynak = 4)
                           UNION ALL
                           SELECT   dbo.Tb_SatKalem.SKlId AS Id, dbo.Tb_SatKalem.SKlTipi AS Tipi, dbo.Tb_SatKalem.SKlTipi AS BaslikTipi, dbo.Tb_SatKalem.SKlTarih AS Tarih, 
                                                    dbo.Tb_SatKalem.SKlKdvTipi AS KdvTipi, dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlBirim AS Birim, dbo.Tb_SatKalem.SKlMiktar AS Miktar, 
                                                    dbo.Tb_SatKalem.SKlFiyat AS Fiyat, dbo.Tb_SatKalem.SKlTutar AS Tutar, dbo.Tb_SatKalem.SKlDoviz AS Doviz, dbo.Tb_SatKalem.SKlDovizTipi AS DovizTip, 
                                                    dbo.Tb_SatKalem.SKlDovizFiyat AS DovizFiyat, dbo.Tb_SatKalem.SKlKur AS Kur, dbo.Tb_SatKalem.SKlDepoId AS DepoId, 0 AS GirenDepoId, 
                                                    dbo.Tb_SatKalem.SKlPartiId AS PartiId, 
                                                    CASE WHEN SKlBirimMiktar < SKlTeslimMiktar THEN SKlBirimMiktar WHEN SKlBirimMiktar = SKlTeslimMiktar THEN SKlBirimMiktar ELSE SKlBirimMiktar - SKlTeslimMiktar END AS
                                                     BirimMiktar, dbo.Tb_SatKalem.SKlBirimFiyat AS BirimFiyat, dbo.Tb_SatKalem.SKlMalyFiyat AS MalyFiyat, dbo.Tb_SatKalem.SKlMalyTutar AS MalyTutar, 
                                                    dbo.Tb_SatKalem.SKlKdvOran AS KdvOran, 0 AS GirenMik, 
                                                    CASE WHEN SKlBirimMiktar < SKlTeslimMiktar THEN SKlBirimMiktar WHEN SKlBirimMiktar = SKlTeslimMiktar THEN SKlBirimMiktar ELSE SKlBirimMiktar - SKlTeslimMiktar END AS
                                                     CikanMik, 30 AS ViewTipi, 1 AS GirCik, 1 AS Satis, dbo.Tb_SatKalem.SKlSaat, dbo.Tb_SatKalem.SKlFisId, dbo.Tb_Satis.SatMagazaKodu
                           FROM         dbo.Tb_SatKalem LEFT OUTER JOIN
                                                    dbo.Tb_Satis ON dbo.Tb_Satis.SatId = dbo.Tb_SatKalem.SKlFisId
                           WHERE     (dbo.Tb_SatKalem.SKlSatirTipi = 0) AND (dbo.Tb_SatKalem.SKlTeslim = 0) OR
                                                    (dbo.Tb_SatKalem.SKlSatirTipi = 0) AND (dbo.Tb_SatKalem.SKlTeslim = 1) AND (dbo.Tb_SatKalem.SKlTeslimMiktar > 0)
                           UNION ALL
                           SELECT   dbo.Tb_YzKKalem.YkKId, 0 AS Expr1, 0 AS Expr2, dbo.Tb_YzKKalem.YkKTarih, 1 AS Expr3, dbo.Tb_YzKKalem.YkKStokId, dbo.Tb_YzKKalem.YkKBirim, 
                                                    dbo.Tb_YzKKalem.YkKMiktar, dbo.Tb_YzKKalem.YkKFiyat, dbo.Tb_YzKKalem.YkKTutar, 'TL' AS Expr4, '' AS Expr5, 0 AS Expr6, 1 AS Expr7, dbo.Tb_YzKKalem.YkKDepoId, 
                                                    0 AS Expr8, dbo.Tb_YzKKalem.YkKPartiId, dbo.Tb_YzKKalem.YkKBirimMiktar, dbo.Tb_YzKKalem.YkKBirimFiyat, dbo.Tb_YzKKalem.YkKMalyFiyat, 
                                                    dbo.Tb_YzKKalem.YkKMalyTutar, dbo.Tb_YzKKalem.YkKKdvOran, 0 AS Expr9, dbo.Tb_YzKKalem.YkKBirimMiktar AS Expr10, 40 AS Expr11, 1 AS Expr12, 1 AS Expr13, 
                                                    dbo.Tb_YzKKalem.YkKSaat, dbo.Tb_YzKKalem.YkKFisId, dbo.Tb_YzKsSatis.YzKMagazaKodu
                           FROM         dbo.Tb_YzKKalem LEFT OUTER JOIN
                                                    dbo.Tb_YzKsSatis ON dbo.Tb_YzKsSatis.YzKId = dbo.Tb_YzKKalem.YkKFisId) AS A
ORDER BY Tarih, Saat
GO
CREATE VIEW [dbo].[vw_StokHareketlerFatura]
AS
SELECT        TOP (100) PERCENT Id, Tipi, BaslikTipi, Tarih, KdvTipi, StokId, Birim, Miktar, Fiyat, Tutar, Doviz, DovizTip, DovizFiyat, Kur, DepoId, GirenDepoId, PartiId, BirimMiktar, BirimFiyat, MalyFiyat, MalyTutar, KdvOran, GirenMik, 
                         CikanMik, ViewTipi, GirCik, Satis, Saat, BaslikId, Magaza
FROM            (SELECT        dbo.Tb_StkHarKalem.ShkId AS Id, dbo.Tb_StkHarKalem.ShkTipi AS Tipi, dbo.Tb_StkHarKalem.ShkBaslikTipi AS BaslikTipi, dbo.Tb_StkHarKalem.ShkTarih AS Tarih, dbo.Tb_StkHarKalem.ShkKdvTipi AS KdvTipi, 
                                                    dbo.Tb_StkHarKalem.ShkStokId AS StokId, dbo.Tb_StkHarKalem.ShkBirim AS Birim, dbo.Tb_StkHarKalem.ShkMiktar AS Miktar, dbo.Tb_StkHarKalem.ShkFiyat AS Fiyat, dbo.Tb_StkHarKalem.ShkTutar AS Tutar, 
                                                    dbo.Tb_StkHarKalem.ShkDoviz AS Doviz, dbo.Tb_StkHarKalem.ShkDovizTip AS DovizTip, dbo.Tb_StkHarKalem.ShkDovizFiyat AS DovizFiyat, dbo.Tb_StkHarKalem.ShkKur AS Kur, 
                                                    CASE ShkBaslikTipi WHEN 2 THEN ShkGirenDepoId ELSE dbo.Tb_StkHarKalem.ShkDepoId END AS DepoId, dbo.Tb_StkHarKalem.ShkGirenDepoId AS GirenDepoId, dbo.Tb_StkHarKalem.ShkPartiId AS PartiId, 
                                                    dbo.Tb_StkHarKalem.ShkBirimMiktar AS BirimMiktar, dbo.Tb_StkHarKalem.ShkBirimFiyat AS BirimFiyat, dbo.Tb_StkHarKalem.ShkMalyFiyat AS MalyFiyat, dbo.Tb_StkHarKalem.ShkMalyTutar AS MalyTutar, 
                                                    dbo.Tb_StkHarKalem.ShkKdvOran AS KdvOran, CASE ShkTipi WHEN 0 THEN ShkBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN ShkBirimMiktar WHEN 3 THEN 0 END AS GirenMik, 
                                                    CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShkBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN ShkBirimMiktar END AS CikanMik, dbo.Tb_StkHarKalem.ShkBaslikTipi AS ViewTipi, 
                                                    CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 END AS GirCik, 0 AS Satis, dbo.Tb_StkHarKalem.ShkSaat AS Saat, dbo.Tb_StkHarKalem.ShkBaslikId AS BaslikId, 
                                                    dbo.Tb_StkHarBaslik.ShbMagazaKodu AS Magaza
                          FROM            dbo.Tb_StkHarKalem LEFT OUTER JOIN
                                                    dbo.Tb_StkHarBaslik ON dbo.Tb_StkHarBaslik.ShbId = dbo.Tb_StkHarKalem.ShkBaslikId
                          WHERE        (dbo.Tb_StkHarKalem.ShkBaslikTipi <> 3)
                          UNION ALL
                          SELECT        Tb_StkHarKalem_1.ShkId AS Id, Tb_StkHarKalem_1.ShkTipi AS Tipi, Tb_StkHarKalem_1.ShkBaslikTipi AS BaslikTipi, Tb_StkHarKalem_1.ShkTarih AS Tarih, Tb_StkHarKalem_1.ShkKdvTipi AS KdvTipi, 
                                                   Tb_StkHarKalem_1.ShkStokId AS StokId, Tb_StkHarKalem_1.ShkBirim AS Birim, Tb_StkHarKalem_1.ShkMiktar AS Miktar, Tb_StkHarKalem_1.ShkFiyat AS Fiyat, Tb_StkHarKalem_1.ShkTutar AS Tutar, 
                                                   Tb_StkHarKalem_1.ShkDoviz AS Doviz, Tb_StkHarKalem_1.ShkDovizTip AS DovizTip, Tb_StkHarKalem_1.ShkDovizFiyat AS DovizFiyat, Tb_StkHarKalem_1.ShkKur AS Kur, Tb_StkHarKalem_1.ShkDepoId AS DepoId, 
                                                   Tb_StkHarKalem_1.ShkDepoId AS GirenDepoId, Tb_StkHarKalem_1.ShkPartiId AS PartiId, Tb_StkHarKalem_1.ShkBirimMiktar AS BirimMiktar, Tb_StkHarKalem_1.ShkBirimFiyat AS BirimFiyat, 
                                                   Tb_StkHarKalem_1.ShkMalyFiyat AS MalyFiyat, Tb_StkHarKalem_1.ShkMalyTutar AS MalyTutar, Tb_StkHarKalem_1.ShkKdvOran AS KdvOran, 
                                                   CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShkBirimMiktar END AS GirenMik, CASE ShkTipi WHEN 0 THEN ShkBirimMiktar WHEN 1 THEN 0 END AS CikanMik, Tb_StkHarKalem_1.ShkBaslikTipi AS ViewTipi, 
                                                   CASE ShkTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS GirCik, 0 AS Satis, Tb_StkHarKalem_1.ShkSaat, Tb_StkHarKalem_1.ShkBaslikId AS BaslikId, Tb_StkHarBaslik_1.ShbMagazaKodu
                          FROM            dbo.Tb_StkHarKalem AS Tb_StkHarKalem_1 LEFT OUTER JOIN
                                                   dbo.Tb_StkHarBaslik AS Tb_StkHarBaslik_1 ON Tb_StkHarBaslik_1.ShbId = Tb_StkHarKalem_1.ShkBaslikId
                          WHERE        (Tb_StkHarKalem_1.ShkBaslikTipi = 2)
                          UNION ALL
                          SELECT        dbo.Tb_IrsKalem.IrKId AS Id, dbo.Tb_IrsKalem.IrKTipi AS Tipi, dbo.Tb_IrsKalem.IrKTipi AS BaslikTipi, dbo.Tb_IrsKalem.IrKTarih AS Tarih, dbo.Tb_IrsKalem.IrKKdvTipi AS KdvTipi, dbo.Tb_IrsKalem.IrKStokId AS StokId, 
                                                   dbo.Tb_IrsKalem.IrKBirim AS Birim, dbo.Tb_IrsKalem.IrKMiktar AS Miktar, dbo.Tb_IrsKalem.IrKFiyat AS Fiyat, dbo.Tb_IrsKalem.IrKTutar AS Tutar, dbo.Tb_IrsKalem.IrKDoviz AS Doviz, 
                                                   dbo.Tb_IrsKalem.IrKDovizTipi AS DovizTip, dbo.Tb_IrsKalem.IrKDovizFiyat AS DovizFiyat, dbo.Tb_IrsKalem.IrKKur AS Kur, dbo.Tb_IrsKalem.IrKDepoId AS DepoId, 0 AS GirenDepoId, 
                                                   dbo.Tb_IrsKalem.IrKPartiId AS PartiId, dbo.Tb_IrsKalem.IrKBirimMiktar AS BirimMiktar, dbo.Tb_IrsKalem.IrKBirimFiyat AS BirimFiyat, dbo.Tb_IrsKalem.IrKMalyFiyat AS MalyFiyat, 
                                                   dbo.Tb_IrsKalem.IrKMalyTutar AS MalyTutar, dbo.Tb_IrsKalem.IrKKdvOran AS KdvOran, 
                                                   CASE IrkTipi WHEN 0 THEN IrKBirimMiktar - IrKFtMiktar WHEN 1 THEN 0 WHEN 2 THEN IrKBirimMiktar - IrKFtMiktar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN IrKBirimMiktar - IrKFtMiktar WHEN 6 THEN IrKBirimMiktar
                                                    - IrKFtMiktar WHEN 7 THEN 0 WHEN 8 THEN 0 WHEN 9 THEN IrKBirimMiktar - IrKFtMiktar END AS GirenMik, 
                                                   CASE IrkTipi WHEN 0 THEN 0 WHEN 1 THEN IrKBirimMiktar - IrKFtMiktar WHEN 2 THEN 0 WHEN 3 THEN IrKBirimMiktar - IrKFtMiktar WHEN 4 THEN IrKBirimMiktar - IrKFtMiktar WHEN 5 THEN 0 WHEN 6 THEN 0 WHEN
                                                    7 THEN IrKBirimMiktar - IrKFtMiktar WHEN 8 THEN IrKBirimMiktar - IrKFtMiktar WHEN 9 THEN 0 END AS CikanMik, dbo.Tb_IrsKalem.IrKTipi + 10 AS ViewTipi, 
                                                   CASE IrkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 WHEN 6 THEN 0 WHEN 7 THEN 1 WHEN 8 THEN 1 WHEN 9 THEN 0 END AS GirCik, 0 AS Satis, 
                                                   dbo.Tb_IrsKalem.IrKSaat, dbo.Tb_IrsKalem.IrKFisId, dbo.Tb_Irsaliye.IrsMagazaKodu
                          FROM            dbo.Tb_IrsKalem LEFT OUTER JOIN
                                                   dbo.Tb_Irsaliye ON dbo.Tb_Irsaliye.IrsId = dbo.Tb_IrsKalem.IrKFisId
                          WHERE        (dbo.Tb_IrsKalem.IrKKaynak = 0 OR
                                                   dbo.Tb_IrsKalem.IrKKaynak = 2 OR
                                                   dbo.Tb_IrsKalem.IrKKaynak = 3 OR
                                                   dbo.Tb_IrsKalem.IrKKaynak = 4) AND (dbo.Tb_IrsKalem.IrKMiktar - dbo.Tb_IrsKalem.IrKFtMiktar > 0)
                          UNION ALL
                          SELECT        dbo.Tb_FatKalem.FtKId AS Id, dbo.Tb_FatKalem.FtKTipi AS Tipi, dbo.Tb_FatKalem.FtKTipi AS BaslikTipi, dbo.Tb_FatKalem.FtKTarih AS Tarih, dbo.Tb_FatKalem.FtKKdvTipi AS KdvTipi, 
                                                   dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_FatKalem.FtKBirim AS Birim, dbo.Tb_FatKalem.FtKMiktar AS Miktar, dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS Tutar, 
                                                   dbo.Tb_FatKalem.FtKDoviz AS Doviz, dbo.Tb_FatKalem.FtKDovizTipi AS DovizTip, dbo.Tb_FatKalem.FtKDovizFiyat AS DovizFiyat, dbo.Tb_FatKalem.FtKKur AS Kur, dbo.Tb_FatKalem.FtKDepoId AS DepoId, 
                                                   0 AS GirenDepoId, dbo.Tb_FatKalem.FtKPartiId AS PartiId, dbo.Tb_FatKalem.FtKBirimMiktar AS BirimMiktar, dbo.Tb_FatKalem.FtKBirimFiyat AS BirimFiyat, dbo.Tb_FatKalem.FtKMalyFiyat AS MalyFiyat, 
                                                   dbo.Tb_FatKalem.FtKMalyTutar AS MalyTutar, dbo.Tb_FatKalem.FtKKdvOran AS KdvOran, 
                                                   CASE FtkTipi WHEN 0 THEN FtKBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN FtKBirimMiktar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FtKBirimMiktar WHEN 6 THEN FtKBirimMiktar END AS GirenMik, 
                                                   CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN FtKBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN FtKBirimMiktar WHEN 4 THEN FtKBirimMiktar WHEN 5 THEN 0 WHEN 6 THEN 0 END AS CikanMik, 
                                                   dbo.Tb_FatKalem.FtKTipi + 20 AS ViewTipi, CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 WHEN 6 THEN 0 END AS GirCik, 
                                                   CASE FtKTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 ELSE 0 END AS Satis, dbo.Tb_FatKalem.FtKSaat, dbo.Tb_FatKalem.FtKFisId, dbo.Tb_Fatura.FatMagazaKodu
                          FROM            dbo.Tb_FatKalem LEFT OUTER JOIN
                                                   dbo.Tb_Fatura ON dbo.Tb_Fatura.FatId = dbo.Tb_FatKalem.FtKFisId
                          WHERE        (dbo.Tb_FatKalem.FtKSatirTipi = 0)
                          UNION ALL
                          SELECT        dbo.Tb_SatKalem.SKlId AS Id, dbo.Tb_SatKalem.SKlTipi AS Tipi, dbo.Tb_SatKalem.SKlTipi AS BaslikTipi, dbo.Tb_SatKalem.SKlTarih AS Tarih, dbo.Tb_SatKalem.SKlKdvTipi AS KdvTipi, 
                                                   dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlBirim AS Birim, dbo.Tb_SatKalem.SKlMiktar AS Miktar, dbo.Tb_SatKalem.SKlFiyat AS Fiyat, dbo.Tb_SatKalem.SKlTutar AS Tutar, 
                                                   dbo.Tb_SatKalem.SKlDoviz AS Doviz, dbo.Tb_SatKalem.SKlDovizTipi AS DovizTip, dbo.Tb_SatKalem.SKlDovizFiyat AS DovizFiyat, dbo.Tb_SatKalem.SKlKur AS Kur, dbo.Tb_SatKalem.SKlDepoId AS DepoId, 
                                                   0 AS GirenDepoId, dbo.Tb_SatKalem.SKlPartiId AS PartiId, 
                                                   CASE WHEN SKlBirimMiktar < SKlTeslimMiktar THEN SKlBirimMiktar WHEN SKlBirimMiktar = SKlTeslimMiktar THEN SKlBirimMiktar ELSE SKlBirimMiktar - SKlTeslimMiktar END AS BirimMiktar, 
                                                   dbo.Tb_SatKalem.SKlBirimFiyat AS BirimFiyat, dbo.Tb_SatKalem.SKlMalyFiyat AS MalyFiyat, dbo.Tb_SatKalem.SKlMalyTutar AS MalyTutar, dbo.Tb_SatKalem.SKlKdvOran AS KdvOran, 0 AS GirenMik, 
                                                   CASE WHEN SKlBirimMiktar < SKlTeslimMiktar THEN SKlBirimMiktar WHEN SKlBirimMiktar = SKlTeslimMiktar THEN SKlBirimMiktar ELSE SKlBirimMiktar - SKlTeslimMiktar END AS CikanMik, 30 AS ViewTipi, 
                                                   1 AS GirCik, 1 AS Satis, dbo.Tb_SatKalem.SKlSaat, dbo.Tb_SatKalem.SKlFisId, dbo.Tb_Satis.SatMagazaKodu
                          FROM            dbo.Tb_SatKalem LEFT OUTER JOIN
                                                   dbo.Tb_Satis ON dbo.Tb_Satis.SatId = dbo.Tb_SatKalem.SKlFisId
                          WHERE        (dbo.Tb_SatKalem.SKlSatirTipi = 0) AND (dbo.Tb_SatKalem.SKlTeslim = 0) AND
                                                       ((SELECT        COUNT(FtKId) AS Expr1
                                                           FROM            dbo.Tb_FatKalem AS Tb_FatKalem_1
                                                           WHERE        (FtKKaynak = 2) AND (FtKKaynakId = dbo.Tb_SatKalem.SKlId)) = 0) AND
                                                       ((SELECT        COUNT(IrKId) AS Expr1
                                                           FROM            dbo.Tb_IrsKalem AS Tb_IrsKalem_1
                                                           WHERE        (IrKKaynak = 2) AND (IrKKaynakId = dbo.Tb_SatKalem.SKlId)) = 0) OR
                                                   (dbo.Tb_SatKalem.SKlSatirTipi = 0) AND (dbo.Tb_SatKalem.SKlTeslim = 1) AND (dbo.Tb_SatKalem.SKlTeslimMiktar > 0) AND (dbo.Tb_SatKalem.SKlTeslimMiktar > dbo.Tb_SatKalem.SKlBirimMiktar)
                          UNION ALL
                          SELECT        dbo.Tb_YzKKalem.YkKId, 0 AS Expr1, 0 AS Expr2, dbo.Tb_YzKKalem.YkKTarih, 1 AS Expr3, dbo.Tb_YzKKalem.YkKStokId, dbo.Tb_YzKKalem.YkKBirim, dbo.Tb_YzKKalem.YkKMiktar, dbo.Tb_YzKKalem.YkKFiyat, 
                                                   dbo.Tb_YzKKalem.YkKTutar, 'TL' AS Expr4, '' AS Expr5, 0 AS Expr6, 1 AS Expr7, dbo.Tb_YzKKalem.YkKDepoId, 0 AS Expr8, dbo.Tb_YzKKalem.YkKPartiId, dbo.Tb_YzKKalem.YkKBirimMiktar, 
                                                   dbo.Tb_YzKKalem.YkKBirimFiyat, dbo.Tb_YzKKalem.YkKMalyFiyat, dbo.Tb_YzKKalem.YkKMalyTutar, dbo.Tb_YzKKalem.YkKKdvOran, 0 AS Expr9, dbo.Tb_YzKKalem.YkKBirimMiktar AS Expr10, 40 AS Expr11, 
                                                   1 AS Expr12, 1 AS Expr13, dbo.Tb_YzKKalem.YkKSaat, dbo.Tb_YzKKalem.YkKFisId, dbo.Tb_YzKsSatis.YzKMagazaKodu
                          FROM            dbo.Tb_YzKKalem LEFT OUTER JOIN
                                                   dbo.Tb_YzKsSatis ON dbo.Tb_YzKsSatis.YzKId = dbo.Tb_YzKKalem.YkKFisId) AS A
ORDER BY Tarih, Saat
GO
CREATE VIEW [dbo].[vw_StokSiprHareket]
AS
SELECT        TOP (100) PERCENT Id, Tipi, BaslikTipi, Tarih, KdvTipi, StokId, Birim, Miktar, Fiyat, Tutar, Doviz, DovizTip, DovizFiyat, Kur, DepoId, GirenDepoId, PartiId, BirimMiktar, BirimFiyat, MalyFiyat, MalyTutar, KdvOran, GirenMik, 
                         CikanMik, ViewTipi, GirCik, Satis, SiprGiren, SiprCikan, Saat, Magaza
FROM            (SELECT        dbo.Tb_StkHarKalem.ShkId AS Id, dbo.Tb_StkHarKalem.ShkTipi AS Tipi, dbo.Tb_StkHarKalem.ShkBaslikTipi AS BaslikTipi, dbo.Tb_StkHarKalem.ShkTarih AS Tarih, dbo.Tb_StkHarKalem.ShkKdvTipi AS KdvTipi, 
                                                    dbo.Tb_StkHarKalem.ShkStokId AS StokId, dbo.Tb_StkHarKalem.ShkBirim AS Birim, dbo.Tb_StkHarKalem.ShkMiktar AS Miktar, dbo.Tb_StkHarKalem.ShkFiyat AS Fiyat, dbo.Tb_StkHarKalem.ShkTutar AS Tutar, 
                                                    dbo.Tb_StkHarKalem.ShkDoviz AS Doviz, dbo.Tb_StkHarKalem.ShkDovizTip AS DovizTip, dbo.Tb_StkHarKalem.ShkDovizFiyat AS DovizFiyat, dbo.Tb_StkHarKalem.ShkKur AS Kur, 
                                                    dbo.Tb_StkHarKalem.ShkDepoId AS DepoId, dbo.Tb_StkHarKalem.ShkGirenDepoId AS GirenDepoId, dbo.Tb_StkHarKalem.ShkPartiId AS PartiId, dbo.Tb_StkHarKalem.ShkBirimMiktar AS BirimMiktar, 
                                                    dbo.Tb_StkHarKalem.ShkBirimFiyat AS BirimFiyat, dbo.Tb_StkHarKalem.ShkMalyFiyat AS MalyFiyat, dbo.Tb_StkHarKalem.ShkMalyTutar AS MalyTutar, dbo.Tb_StkHarKalem.ShkKdvOran AS KdvOran, 
                                                    CASE ShkTipi WHEN 0 THEN ShkBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN ShkBirimMiktar WHEN 3 THEN 0 END AS GirenMik, 
                                                    CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShkBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN ShkBirimMiktar END AS CikanMik, dbo.Tb_StkHarKalem.ShkBaslikTipi AS ViewTipi, 
                                                    CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 END AS GirCik, 0 AS Satis, 0 AS SiprGiren, 0 AS SiprCikan, dbo.Tb_StkHarKalem.ShkSaat AS Saat, 
                                                    dbo.Tb_StkHarBaslik.ShbMagazaKodu AS Magaza
                          FROM            dbo.Tb_StkHarKalem LEFT OUTER JOIN
                                                    dbo.Tb_StkHarBaslik ON dbo.Tb_StkHarBaslik.ShbId = dbo.Tb_StkHarKalem.ShkBaslikId
                          WHERE        (dbo.Tb_StkHarKalem.ShkBaslikTipi <> 3)
                          UNION ALL
                          SELECT        Tb_StkHarKalem_1.ShkId AS Id, Tb_StkHarKalem_1.ShkTipi AS Tipi, Tb_StkHarKalem_1.ShkBaslikTipi AS BaslikTipi, Tb_StkHarKalem_1.ShkTarih AS Tarih, Tb_StkHarKalem_1.ShkKdvTipi AS KdvTipi, 
                                                   Tb_StkHarKalem_1.ShkStokId AS StokId, Tb_StkHarKalem_1.ShkBirim AS Birim, Tb_StkHarKalem_1.ShkMiktar AS Miktar, Tb_StkHarKalem_1.ShkFiyat AS Fiyat, Tb_StkHarKalem_1.ShkTutar AS Tutar, 
                                                   Tb_StkHarKalem_1.ShkDoviz AS Doviz, Tb_StkHarKalem_1.ShkDovizTip AS DovizTip, Tb_StkHarKalem_1.ShkDovizFiyat AS DovizFiyat, Tb_StkHarKalem_1.ShkKur AS Kur, 
                                                   Tb_StkHarKalem_1.ShkGirenDepoId AS DepoId, Tb_StkHarKalem_1.ShkDepoId AS GirenDepoId, Tb_StkHarKalem_1.ShkPartiId AS PartiId, Tb_StkHarKalem_1.ShkBirimMiktar AS BirimMiktar, 
                                                   Tb_StkHarKalem_1.ShkBirimFiyat AS BirimFiyat, Tb_StkHarKalem_1.ShkMalyFiyat AS MalyFiyat, Tb_StkHarKalem_1.ShkMalyTutar AS MalyTutar, Tb_StkHarKalem_1.ShkKdvOran AS KdvOran, 
                                                   CASE ShkTipi WHEN 0 THEN 0 WHEN 1 THEN ShkBirimMiktar END AS GirenMik, CASE ShkTipi WHEN 0 THEN ShkBirimMiktar WHEN 1 THEN 0 END AS CikanMik, Tb_StkHarKalem_1.ShkBaslikTipi AS ViewTipi, 
                                                   CASE ShkTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS GirCik, 0 AS Satis, 0 AS SiprGiren, 0 AS SiprCikan, Tb_StkHarKalem_1.ShkSaat, Tb_StkHarBaslik_1.ShbMagazaKodu
                          FROM            dbo.Tb_StkHarKalem AS Tb_StkHarKalem_1 LEFT OUTER JOIN
                                                   dbo.Tb_StkHarBaslik AS Tb_StkHarBaslik_1 ON Tb_StkHarBaslik_1.ShbId = Tb_StkHarKalem_1.ShkBaslikId
                          WHERE        (Tb_StkHarKalem_1.ShkBaslikTipi = 2)
                          UNION ALL
                          SELECT        dbo.Tb_IrsKalem.IrKId AS Id, dbo.Tb_IrsKalem.IrKTipi AS Tipi, dbo.Tb_IrsKalem.IrKTipi AS BaslikTipi, dbo.Tb_IrsKalem.IrKTarih AS Tarih, dbo.Tb_IrsKalem.IrKKdvTipi AS KdvTipi, dbo.Tb_IrsKalem.IrKStokId AS StokId, 
                                                   dbo.Tb_IrsKalem.IrKBirim AS Birim, dbo.Tb_IrsKalem.IrKMiktar AS Miktar, dbo.Tb_IrsKalem.IrKFiyat AS Fiyat, dbo.Tb_IrsKalem.IrKTutar AS Tutar, dbo.Tb_IrsKalem.IrKDoviz AS Doviz, 
                                                   dbo.Tb_IrsKalem.IrKDovizTipi AS DovizTip, dbo.Tb_IrsKalem.IrKDovizFiyat AS DovizFiyat, dbo.Tb_IrsKalem.IrKKur AS Kur, dbo.Tb_IrsKalem.IrKDepoId AS DepoId, 0 AS GirenDepoId, 
                                                   dbo.Tb_IrsKalem.IrKPartiId AS PartiId, dbo.Tb_IrsKalem.IrKBirimMiktar AS BirimMiktar, dbo.Tb_IrsKalem.IrKBirimFiyat AS BirimFiyat, dbo.Tb_IrsKalem.IrKMalyFiyat AS MalyFiyat, 
                                                   dbo.Tb_IrsKalem.IrKMalyTutar AS MalyTutar, dbo.Tb_IrsKalem.IrKKdvOran AS KdvOran, 
                                                   CASE IrkTipi WHEN 0 THEN IrKBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN IrKBirimMiktar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN IrKBirimMiktar WHEN 6 THEN IrKBirimMiktar WHEN 7 THEN 0 WHEN 8 THEN
                                                    0 WHEN 9 THEN IrKBirimMiktar END AS GirenMik, 
                                                   CASE IrkTipi WHEN 0 THEN 0 WHEN 1 THEN IrKBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN IrKBirimMiktar WHEN 4 THEN IrKBirimMiktar WHEN 5 THEN 0 WHEN 6 THEN 0 WHEN 7 THEN IrKBirimMiktar WHEN 8 THEN
                                                    IrKBirimMiktar WHEN 9 THEN 0 END AS CikanMik, dbo.Tb_IrsKalem.IrKTipi + 10 AS ViewTipi, 
                                                   CASE IrkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 WHEN 6 THEN 0 WHEN 7 THEN 1 WHEN 8 THEN 1 WHEN 9 THEN 0 END AS GirCik, 0 AS Satis, 
                                                   0 AS SiprGiren, 0 AS SiprCikan, dbo.Tb_IrsKalem.IrKSaat, dbo.Tb_Irsaliye.IrsMagazaKodu
                          FROM            dbo.Tb_IrsKalem LEFT OUTER JOIN
                                                   dbo.Tb_Irsaliye ON dbo.Tb_Irsaliye.IrsId = dbo.Tb_IrsKalem.IrKFisId
                          WHERE        (dbo.Tb_IrsKalem.IrKKaynak = 0 OR
                                                   dbo.Tb_IrsKalem.IrKKaynak = 4 OR
                                                   dbo.Tb_IrsKalem.IrKKaynak = 5) AND (dbo.Tb_IrsKalem.IrKMiktar - dbo.Tb_IrsKalem.IrKFtMiktar > 0)
                          UNION ALL
                          SELECT        dbo.Tb_FatKalem.FtKId AS Id, dbo.Tb_FatKalem.FtKTipi AS Tipi, dbo.Tb_FatKalem.FtKTipi AS BaslikTipi, dbo.Tb_FatKalem.FtKTarih AS Tarih, dbo.Tb_FatKalem.FtKKdvTipi AS KdvTipi, 
                                                   dbo.Tb_FatKalem.FtKStokId AS StokId, dbo.Tb_FatKalem.FtKBirim AS Birim, dbo.Tb_FatKalem.FtKMiktar AS Miktar, dbo.Tb_FatKalem.FtKFiyat AS Fiyat, dbo.Tb_FatKalem.FtKTutar AS Tutar, 
                                                   dbo.Tb_FatKalem.FtKDoviz AS Doviz, dbo.Tb_FatKalem.FtKDovizTipi AS DovizTip, dbo.Tb_FatKalem.FtKDovizFiyat AS DovizFiyat, dbo.Tb_FatKalem.FtKKur AS Kur, dbo.Tb_FatKalem.FtKDepoId AS DepoId, 
                                                   0 AS GirenDepoId, dbo.Tb_FatKalem.FtKPartiId AS PartiId, dbo.Tb_FatKalem.FtKBirimMiktar AS BirimMiktar, dbo.Tb_FatKalem.FtKBirimFiyat AS BirimFiyat, dbo.Tb_FatKalem.FtKMalyFiyat AS MalyFiyat, 
                                                   dbo.Tb_FatKalem.FtKMalyTutar AS MalyTutar, dbo.Tb_FatKalem.FtKKdvOran AS KdvOran, 
                                                   CASE FtkTipi WHEN 0 THEN FtKBirimMiktar WHEN 1 THEN 0 WHEN 2 THEN FtKBirimMiktar WHEN 3 THEN 0 WHEN 4 THEN 0 WHEN 5 THEN FtKBirimMiktar WHEN 6 THEN FtKBirimMiktar END AS GirenMik, 
                                                   CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN FtKBirimMiktar WHEN 2 THEN 0 WHEN 3 THEN FtKBirimMiktar WHEN 4 THEN FtKBirimMiktar WHEN 5 THEN 0 WHEN 6 THEN 0 END AS CikanMik, 
                                                   dbo.Tb_FatKalem.FtKTipi + 20 AS ViewTipi, CASE FtkTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 WHEN 4 THEN 1 WHEN 5 THEN 0 END AS GirCik, 
                                                   CASE FtKTipi WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 0 WHEN 3 THEN 1 ELSE 0 END AS Satis, 0 AS SiprGiren, 0 AS SiprCikan, dbo.Tb_FatKalem.FtKSaat, dbo.Tb_Fatura.FatMagazaKodu
                          FROM            dbo.Tb_FatKalem LEFT OUTER JOIN
                                                   dbo.Tb_Fatura ON dbo.Tb_Fatura.FatId = dbo.Tb_FatKalem.FtKFisId
                          WHERE        (dbo.Tb_FatKalem.FtKSatirTipi = 0) AND (dbo.Tb_FatKalem.FtKKaynak = 0 OR
                                                   dbo.Tb_FatKalem.FtKKaynak = 1 OR
                                                   dbo.Tb_FatKalem.FtKKaynak = 3 OR
                                                   dbo.Tb_FatKalem.FtKKaynak = 4)
                          UNION ALL
                          SELECT        dbo.Tb_SatKalem.SKlId AS Id, dbo.Tb_SatKalem.SKlTipi AS Tipi, dbo.Tb_SatKalem.SKlTipi AS BaslikTipi, dbo.Tb_SatKalem.SKlTarih AS Tarih, dbo.Tb_SatKalem.SKlKdvTipi AS KdvTipi, 
                                                   dbo.Tb_SatKalem.SKlStokId AS StokId, dbo.Tb_SatKalem.SKlBirim AS Birim, dbo.Tb_SatKalem.SKlMiktar AS Miktar, dbo.Tb_SatKalem.SKlFiyat AS Fiyat, dbo.Tb_SatKalem.SKlTutar AS Tutar, 
                                                   dbo.Tb_SatKalem.SKlDoviz AS Doviz, dbo.Tb_SatKalem.SKlDovizTipi AS DovizTip, dbo.Tb_SatKalem.SKlDovizFiyat AS DovizFiyat, dbo.Tb_SatKalem.SKlKur AS Kur, dbo.Tb_SatKalem.SKlDepoId AS DepoId, 
                                                   0 AS GirenDepoId, dbo.Tb_SatKalem.SKlPartiId AS PartiId, dbo.Tb_SatKalem.SKlBirimMiktar AS BirimMiktar, dbo.Tb_SatKalem.SKlBirimFiyat AS BirimFiyat, dbo.Tb_SatKalem.SKlMalyFiyat AS MalyFiyat, 
                                                   dbo.Tb_SatKalem.SKlMalyTutar AS MalyTutar, dbo.Tb_SatKalem.SKlKdvOran AS KdvOran, 0 AS GirenMik, dbo.Tb_SatKalem.SKlTeslimMiktar AS CikanMik, 30 AS ViewTipi, 1 AS GirCik, 1 AS Satis, 0 AS SiprGiren, 
                                                   dbo.Tb_SatKalem.SKlBirimMiktar - dbo.Tb_SatKalem.SKlTeslimMiktar AS SiprCikan, dbo.Tb_SatKalem.SKlSaat, dbo.Tb_Satis.SatMagazaKodu
                          FROM            dbo.Tb_SatKalem LEFT OUTER JOIN
                                                   dbo.Tb_Satis ON dbo.Tb_Satis.SatId = dbo.Tb_SatKalem.SKlFisId
                          WHERE        (dbo.Tb_SatKalem.SKlSatirTipi = 0)
                          UNION ALL
                          SELECT        dbo.Tb_YzKKalem.YkKId, 0 AS Expr1, 0 AS Expr2, dbo.Tb_YzKKalem.YkKTarih, 1 AS Expr3, dbo.Tb_YzKKalem.YkKStokId, dbo.Tb_YzKKalem.YkKBirim, dbo.Tb_YzKKalem.YkKMiktar, dbo.Tb_YzKKalem.YkKFiyat, 
                                                   dbo.Tb_YzKKalem.YkKTutar, 'TL' AS Expr4, '' AS Expr5, 0 AS Expr6, 1 AS Expr7, dbo.Tb_YzKKalem.YkKDepoId, 0 AS Expr8, dbo.Tb_YzKKalem.YkKPartiId, dbo.Tb_YzKKalem.YkKBirimMiktar, 
                                                   dbo.Tb_YzKKalem.YkKBirimFiyat, dbo.Tb_YzKKalem.YkKMalyFiyat, dbo.Tb_YzKKalem.YkKMalyTutar, dbo.Tb_YzKKalem.YkKKdvOran, 0 AS Expr9, dbo.Tb_YzKKalem.YkKBirimMiktar AS Expr10, 40 AS Expr11, 
                                                   1 AS Expr12, 1 AS Expr13, 0 AS SiprGiren, 0 AS SiprCikan, dbo.Tb_YzKKalem.YkKSaat, dbo.Tb_YzKsSatis.YzKMagazaKodu
                          FROM            dbo.Tb_YzKKalem LEFT OUTER JOIN
                                                   dbo.Tb_YzKsSatis ON dbo.Tb_YzKsSatis.YzKId = dbo.Tb_YzKKalem.YkKFisId
                          UNION ALL
                          SELECT        dbo.Tb_SipKalem.SpKId, dbo.Tb_SipKalem.SpKTipi, dbo.Tb_SipKalem.SpKTipi AS Expr1, dbo.Tb_SipKalem.SpKTarih, dbo.Tb_SipKalem.SpKKdvTipi, dbo.Tb_SipKalem.SpKStokId, dbo.Tb_SipKalem.SpKBirim, 
                                                   dbo.Tb_SipKalem.SpKMiktar, dbo.Tb_SipKalem.SpKFiyat, dbo.Tb_SipKalem.SpKTutar, dbo.Tb_SipKalem.SpKDoviz, dbo.Tb_SipKalem.SpKDovizTipi, dbo.Tb_SipKalem.SpKDovizFiyat, dbo.Tb_SipKalem.SpKKur, 
                                                   dbo.Tb_SipKalem.SpKDepoId, 0 AS Expr2, 0 AS Expr3, dbo.Tb_SipKalem.SpKBirimMiktar AS Expr4, dbo.Tb_SipKalem.SpKBirimFiyat, dbo.Tb_SipKalem.SpKMalyFiyat, dbo.Tb_SipKalem.SpKMalyTutar, 
                                                   dbo.Tb_SipKalem.SpKKdvOran, 0 AS Expr5, 0 AS Expr6, 50 AS Expr7, CASE SpKTipi WHEN 0 THEN 1 WHEN 1 THEN 0 END AS Expr8, 0 AS Expr9, 
                                                   CASE SpKTipi WHEN 1 THEN CASE WHEN SpKBirimMiktar = SpKTeslimMiktar THEN SpKBirimMiktar ELSE SpKBirimMiktar - SpKTeslimMiktar END WHEN 0 THEN 0 END AS Expr10, 
                                                   CASE SpKTipi WHEN 0 THEN CASE WHEN SpKBirimMiktar = SpKTeslimMiktar THEN SpKBirimMiktar ELSE SpKBirimMiktar - SpKTeslimMiktar END WHEN 1 THEN 0 END AS Expr11, dbo.Tb_SipKalem.SpKSaat, 
                                                   dbo.Tb_Siparis.SipMagazaKodu
                          FROM            dbo.Tb_SipKalem LEFT OUTER JOIN
                                                   dbo.Tb_Siparis ON dbo.Tb_Siparis.SipId = dbo.Tb_SipKalem.SpKFisId
                          WHERE        (dbo.Tb_SipKalem.SpKTeslim = 1)) AS A
ORDER BY Tarih, Saat
GO
CREATE VIEW [dbo].[vw_TahsilatPrim]
AS
SELECT        Tarih, TahsTipi, Tutar, Aciklama, PersId, CariId, Tipi, Taksit, Magaza
FROM            (SELECT        dbo.Tb_CariHareket.CrHTarih AS Tarih, dbo.Tb_CariHareket.CrHTipi AS TahsTipi, dbo.Tb_CariHareket.CrHTutar AS Tutar, dbo.Tb_CariHareket.CrHAciklama AS Aciklama, dbo.Tb_Satis.SatPersId AS PersId, 
                                                    dbo.Tb_CariHareket.CrHCariId AS CariId, 1 AS Tipi, dbo.Tb_CariHareket.CrHTaksit AS Taksit, dbo.Tb_Satis.SatMagazaKodu AS Magaza
                          FROM            dbo.Tb_CariHareket LEFT OUTER JOIN
                                                    dbo.Tb_Satis ON dbo.Tb_Satis.SatId = dbo.Tb_CariHareket.CrHEntgId
                          WHERE        (dbo.Tb_CariHareket.CrHEntgTipi = 1) AND (ISNULL(dbo.Tb_Satis.SatId, 0) > 0) AND (dbo.Tb_Satis.SatPersId > 0) OR
                                                    (dbo.Tb_CariHareket.CrHEntgTipi = 2) AND (ISNULL(dbo.Tb_Satis.SatId, 0) > 0) AND (dbo.Tb_Satis.SatPersId > 0)
                          UNION ALL
                          SELECT        dbo.Tb_TaksitTahsil.TkOTarih, Tb_CariHareket_1.CrHTipi, dbo.Tb_TaksitTahsil.TkOTutar, dbo.Tb_Taksit.TakSiraNo + ' Numaralı Taksit Ödemesi' AS Expr1, Tb_Satis_1.SatPersId, dbo.Tb_TaksitTahsil.TkOCariId, 
                                                   2 AS Expr2, Tb_CariHareket_1.CrHTaksit, Tb_Satis_1.SatMagazaKodu AS Magaza
                          FROM            dbo.Tb_TaksitTahsil LEFT OUTER JOIN
                                                   dbo.Tb_CariHareket AS Tb_CariHareket_1 ON dbo.Tb_TaksitTahsil.TkOTahsilId = Tb_CariHareket_1.CrHId LEFT OUTER JOIN
                                                   dbo.Tb_Taksit ON dbo.Tb_TaksitTahsil.TkOTaksitId = dbo.Tb_Taksit.TakId LEFT OUTER JOIN
                                                   dbo.Tb_Satis AS Tb_Satis_1 ON dbo.Tb_Taksit.TakSatisId = Tb_Satis_1.SatId
                          WHERE        (dbo.Tb_Taksit.TakKaynak = 4) AND (Tb_Satis_1.SatPersId > 0)) AS A
GO
CREATE VIEW [dbo].[vw_VadeliIslemler]
AS
SELECT        TOP (100) PERCENT Vade, Tutar, Tur, Magaza, Ay, FarkGun, GelirGider, IlkGun, Sira
FROM            (SELECT        CksVadeTarih AS Vade, CksTutar AS Tutar, CksKartTuru AS Tur, CksMagazaKodu AS Magaza, SUBSTRING(CONVERT(NVARCHAR, CksVadeTarih), 6, 2) + '-' + CONVERT(NVARCHAR, YEAR(CksVadeTarih)) AS Ay, 
                                                    DAY(CksVadeTarih) AS FarkGun, CksKartTuru AS GelirGider, '01-' + SUBSTRING(CONVERT(NVARCHAR, CksVadeTarih), 6, 2) + '-' + CONVERT(NVARCHAR, YEAR(CksVadeTarih)) AS IlkGun, YEAR(CksVadeTarih) 
                                                    * 12 + MONTH(CksVadeTarih) AS Sira
                          FROM            dbo.Tb_CkSnKart
                          WHERE        (CksKartTuru = 0) AND (CksDurum = 0) OR
                                                    (CksKartTuru = 1) AND (CksDurum = 1)
                          UNION ALL
                          SELECT        HsgTarih AS Vade, CASE HsgGcTutar WHEN 0 THEN HsgTutar ELSE HsgGcTutar END AS Tutar, 3 AS Tur, HsgMagaza AS Magaza, SUBSTRING(CONVERT(NVARCHAR, HsgTarih), 6, 2) + '-' + CONVERT(NVARCHAR, 
                                                   YEAR(HsgTarih)) AS Ay, DAY(HsgTarih) AS FarkGun, 0 AS GelirGider, '01-' + SUBSTRING(CONVERT(NVARCHAR, HsgTarih), 6, 2) + '-' + CONVERT(NVARCHAR, YEAR(HsgTarih)) AS IlkGun, YEAR(HsgTarih) 
                                                   * 12 + MONTH(HsgTarih) AS Sira
                          FROM            dbo.Tb_HesGecme
                          WHERE        (HsgGecti = 0)
                          UNION ALL
                          SELECT        TakVade AS Vade, TakTutar -
                                                       (SELECT        ISNULL(SUM(TkOTutar), 0) AS Expr1
                                                         FROM            dbo.Tb_TaksitTahsil
                                                         WHERE        (TkOTaksitId = dbo.Tb_Taksit.TakId)) AS Tutar, CASE TakKaynak WHEN 4 THEN 4 ELSE 5 END AS Tur, TakMagazaKodu AS Magaza, SUBSTRING(CONVERT(NVARCHAR, TakVade), 6, 2) 
                                                   + '-' + CONVERT(NVARCHAR, YEAR(TakVade)) AS Ay, DAY(TakVade) AS FarkGun, CASE TakKaynak WHEN 4 THEN 0 ELSE 1 END AS GelirGider, '01-' + SUBSTRING(CONVERT(NVARCHAR, TakVade), 6, 2) 
                                                   + '-' + CONVERT(NVARCHAR, YEAR(TakVade)) AS IlkGun, YEAR(TakVade) * 12 + MONTH(TakVade) AS Sira
                          FROM            dbo.Tb_Taksit
                          WHERE        (TakKaraListe = 0)) AS A

GO
CREATE VIEW [dbo].[vw_StokGrupHareketler]
AS
SELECT        TOP (100) PERCENT StokId, DepoId, SUM(GirenMik) AS Giren, SUM(CikanMik) AS Cikan, SUM(GirenMik) - SUM(CikanMik) AS Bakiye
FROM            dbo.vw_StokHareketlerFatura
GROUP BY StokId, DepoId
ORDER BY StokId
GO