SELECT STHAR.SUBE_KODU, STHAR.STOK_KODU, STSABIT.STOK_ADI, STSABIT.GRUP_KODU AS SGRUP_KODU, STGRUP.GRUP_ISIM AS SGRUP_ISIM,
STSABIT.KOD_1 AS SKOD_1, STOKKOD1.GRUP_ISIM AS SKOD1_ISIM, STSABIT.KOD_2 AS SKOD_2, STOKKOD2.GRUP_ISIM AS SKOD2_ISIM,
STSABIT.SATICI_KODU, STHAR.STHAR_ACIKLAMA, CASABIT.CARI_ISIM, CASABIT.CARI_TIP, CASABIT.GRUP_KODU AS CGRUP_KODU,
CASABIT.RAPOR_KODU1 AS CKOD_1, CARIKOD1.GRUP_ISIM AS CKOD1_ISIM, CASABIT.RAPOR_KODU2 AS CKOD_2, CASABIT.CARI_IL,
CASABIT.CARI_ILCE, STHAR.STHAR_HTUR, STHAR.STHAR_KOD1, STHAR.STHAR_KOD2, STHAR.STHAR_TARIH,
STHAR.FISNO AS STHAR_FISNO, STHAR.AMBAR_KABULNO, STHAR.IRSALIYE_NO, STHAR.STHAR_ODEGUN, STHAR.STHAR_BF,
STHAR.STHAR_NF, STHAR.STHAR_IAF,
(STHAR.STHAR_BF - STHAR.STHAR_NF * (CASE WHEN FATUIRS.KDV_DAHILMI = 'E' THEN (1 + STHAR.STHAR_KDV / 100) ELSE 1 END))
* STHAR.STHAR_GCMIK AS ISKONTOTUT,
CASE WHEN STHAR.STHAR_SATISK < 0.00100 THEN STHAR.STHAR_SATISK * 100000 ELSE STHAR.STHAR_SATISK END AS STHAR_SATISK,
STHAR.STHAR_MALFISK, STSABIT.OLCU_BR1, STSABIT.OLCU_BR2, STSABIT.OLCU_BR3,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END) AS GIRISM,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* (CASE WHEN STSABIT.PAYDA_1 = 0 THEN 0 ELSE (STSABIT.PAY_1 / STSABIT.PAYDA_1) END) AS GRISIM2,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* (CASE WHEN STSABIT.PAYDA2 = 0 THEN 0 ELSE (STSABIT.PAY2 / STSABIT.PAYDA2) END) AS GIRISM3,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END) * STHAR.STHAR_NF AS NETGIRISTUT,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END) * STHAR.STHAR_BF AS BRUTGIRISTUT,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END) * STHAR.STHAR_IAF AS IADEGIRISTUT,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END) AS CIKISM,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* (CASE WHEN STSABIT.PAYDA_1 = 0 THEN 0 ELSE (STSABIT.PAY_1 / STSABIT.PAYDA_1) END) AS CIKISM2,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* (CASE WHEN STSABIT.PAYDA2 = 0 THEN 0 ELSE (STSABIT.PAY2 / STSABIT.PAYDA2) END) AS CIKISM3,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END) * STHAR.STHAR_NF AS NETCIKISTUT,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END) * STHAR.STHAR_BF AS BRUTCIKISTUT, STHAR.STHAR_KDV,
STHAR.STHAR_GCMIK * STHAR.STHAR_NF * STHAR.STHAR_KDV / 100 AS KDV_TUT, STHAR.STHAR_SIPNUM, STHAR.STHAR_DOVTIP,
STHAR.STHAR_DOVFIAT, STHAR.STHAR_GCMIK * STHAR.STHAR_DOVFIAT AS DOVIZ_TUTARI,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK2 ELSE 0 END) AS IKINCI_GIRIS_MIKTAR,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK2 ELSE 0 END) AS IKINCI_CIKIS_MIKTAR,
(CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* STHAR.STHAR_NF + (CASE WHEN STHAR.STHAR_GCKOD = 'G' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* STHAR.STHAR_NF * STHAR.STHAR_KDV / 100 AS ALIS_TOPLAM, (CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* STHAR.STHAR_NF + (CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN STHAR.STHAR_GCMIK ELSE 0 END)
* STHAR.STHAR_NF * STHAR.STHAR_KDV / 100 AS SATIS_TOPLAM, STHAR.CEVRIM,
(CASE WHEN STHAR.STHAR_GCKOD = 'C' THEN (STHAR.CEVRIM * STHAR.STHAR_GCMIK * STHAR.STHAR_NF) ELSE 0 END) AS BEKLEME_ADATI,
STSABITEK.TUR, STSABITEK.MGRUP AS MRUP, STSABITEK.INGISIM, STSABITEK.KULL1N, STSABITEK.KULL2N, STSABITEK.KULL1S,
STSABITEK.KULL2S, STHAR.DEPO_KODU, STHAR.PROMASYON_KODU, STHAR.BAGLANTI_NO, STHAR.VADE_TARIHI, STHAR.LISTE_NO,
STHAR.LISTE_FIAT, CARIKOD2.GRUP_ISIM AS CKOD2_ISIM, STHAR.PLASIYER_KODU AS PLAS_KOD, STHAR.PROJE_KODU,
STHAR.EKALAN_NEDEN, STHAR.EKALAN, STHAR.EKALAN1 AS EKALAN2, STHAR.MUH_KODU AS MUHKODU, PROJE.PROJE_ACIKLAMA,
ISKONTIP1.ACIKLAMA AS ISKONTO1_ACIKLAMA, ISKONTIP2.ACIKLAMA AS ISKONTO2_ACIKLAMA, ISKONTIP3.ACIKLAMA AS ISKONTO3_ACIKLAMA,
(CASE WHEN (STSABIT.B_YEDEK7 = 1) OR
(STSABIT.B_YEDEK7 = 2) THEN 'E' ELSE 'H' END) AS KITSTOGU,
(CASE WHEN STHAR.L_YEDEK9 = - 1 THEN 'K' ELSE (CASE WHEN STHAR.L_YEDEK9 > 0 THEN 'B' ELSE 'N' END) END) AS KITHAREKETI,
STSABIT.KOD_3 AS KOD3, STSABIT.KOD_4 AS KOD4, STSABIT.KOD_5 AS KOD5, CASABIT.RAPOR_KODU3 AS CKOD_3,
CASABIT.RAPOR_KODU4 AS CKOD_4, CASABIT.RAPOR_KODU5 AS CKOD_5, STHAR.YAPKOD
, CASE WHEN STSABIT.YAPILANDIR = 'E' THEN dbo.NSF_YAPACIK(STHAR.YAPKOD, STOKPARAM.C_YEDEK8, ESNYAPMAS.YAPACIK) ELSE NULL END AS YAPACIK
FROM dbo.TBLSTHAR STHAR INNER JOIN
dbo.TBLSTSABIT STSABIT ON STHAR.STOK_KODU = STSABIT.STOK_KODU INNER JOIN
--dbo.NETCONTEXT ON 1 = 1 INNER JOIN
dbo.TBLSTOKPARAM AS STOKPARAM WITH (NOLOCK) ON STOKPARAM.ISLETME_KODU = STSABIT.ISLETME_KODU LEFT OUTER JOIN
dbo.TBLSTGRUP STGRUP ON STSABIT.GRUP_KODU = STGRUP.GRUP_KOD LEFT OUTER JOIN
dbo.TBLCASABIT CASABIT ON STHAR.STHAR_ACIKLAMA = CASABIT.CARI_KOD INNER JOIN
dbo.TBLSTSABITEK STSABITEK ON STSABIT.STOK_KODU = STSABITEK.STOK_KODU LEFT OUTER JOIN
dbo.TBLSTOKKOD1 STOKKOD1 ON STSABIT.KOD_1 = STOKKOD1.GRUP_KOD LEFT OUTER JOIN
dbo.TBLCARIKOD1 CARIKOD1 ON CASABIT.RAPOR_KODU1 = CARIKOD1.GRUP_KOD LEFT OUTER JOIN
dbo.TBLCARIKOD2 CARIKOD2 ON CASABIT.RAPOR_KODU2 = CARIKOD2.GRUP_KOD LEFT OUTER JOIN
dbo.TBLSTOKKOD2 STOKKOD2 ON STSABIT.KOD_2 = STOKKOD2.GRUP_KOD LEFT OUTER JOIN
dbo.TBLPROJE PROJE ON STHAR.PROJE_KODU = PROJE.PROJE_KODU LEFT OUTER JOIN
dbo.TBLISKONTIP ISKONTIP1 ON STHAR.SATISK1TIP = ISKONTIP1.ISKONTOTIP LEFT OUTER JOIN
dbo.TBLISKONTIP ISKONTIP2 ON STHAR.SATISK2TIP = ISKONTIP2.ISKONTOTIP LEFT OUTER JOIN
dbo.TBLISKONTIP ISKONTIP3 ON STHAR.SATISK3TIP = ISKONTIP3.ISKONTOTIP LEFT OUTER JOIN
dbo.TBLESNYAPMAS AS ESNYAPMAS WITH (NOLOCK) ON ESNYAPMAS.YAPKOD = STHAR.YAPKOD LEFT OUTER JOIN
dbo.TBLFATUIRS FATUIRS ON FATUIRS.CARI_KODU = STHAR.STHAR_ACIKLAMA AND STHAR.STHAR_FTIRSIP = FATUIRS.FTIRSIP AND
STHAR.FISNO = FATUIRS.FATIRS_NO