KAR ZARAR RAPORU
==========================================
,ALACAK2018=ROUND(ISNULL((select sum (CH.ALACAK) FROM CARIHAREKETLER CH WHERE CH.MUSTERIKODU=M.MUSTERIKODU AND YEAR(CH.TARIH)=2018 AND CH.HAREKETTURU=’A’ AND CH.IPTAL=0),0),2)
,BORC2018=ROUND(ISNULL((select sum (CH.BORC) FROM CARIHAREKETLER CH WHERE CH.MUSTERIKODU=M.MUSTERIKODU AND YEAR(CH.TARIH)=2018 AND CH.HAREKETTURU=’B’ AND CH.IPTAL=0),0),2)
,BAKIYE2018=ROUND(ISNULL( (SELECT SUM(CH.BORC-CH.ALACAK) FROM CARIHAREKETLER CH WHERE M.MUSTERIKODU=CH.MUSTERIKODU AND YEAR(CH.TARIH)=2018),0),2)
,ALACAK2019=ROUND(ISNULL((select sum (CH.ALACAK) FROM CARIHAREKETLER CH WHERE CH.MUSTERIKODU=M.MUSTERIKODU AND YEAR(CH.TARIH)=2019 AND CH.HAREKETTURU=’A’ AND CH.IPTAL=0),0),2)
,BORC2019=ROUND(ISNULL((select sum (CH.BORC) FROM CARIHAREKETLER CH WHERE CH.MUSTERIKODU=M.MUSTERIKODU AND YEAR(CH.TARIH)=2019 AND CH.HAREKETTURU=’B’ AND CH.IPTAL=0),0),2)
,BAKIYE2019=ROUND(ISNULL( (SELECT SUM(CH.BORC-CH.ALACAK) FROM CARIHAREKETLER CH WHERE M.MUSTERIKODU=CH.MUSTERIKODU AND YEAR(CH.TARIH)=2019),0),2)
,ALACAK2020=ROUND(ISNULL((select sum (CH.ALACAK) FROM CARIHAREKETLER CH WHERE CH.MUSTERIKODU=M.MUSTERIKODU AND YEAR(CH.TARIH)=2020 AND CH.HAREKETTURU=’A’ AND CH.IPTAL=0),0),2)
,BORC2020=ROUND(ISNULL((select sum (CH.BORC) FROM CARIHAREKETLER CH WHERE CH.MUSTERIKODU=M.MUSTERIKODU AND YEAR(CH.TARIH)=2020 AND CH.HAREKETTURU=’B’ AND CH.IPTAL=0),0),2)
,BAKIYE2020=ROUND(ISNULL( (SELECT SUM(CH.BORC-CH.ALACAK) FROM CARIHAREKETLER CH WHERE M.MUSTERIKODU=CH.MUSTERIKODU AND YEAR(CH.TARIH)=2020),0),2)
,SONBAKIYE=ROUND(ISNULL( (SELECT SUM(CH.BORC-CH.ALACAK) FROM CARIHAREKETLER CH WHERE M.MUSTERIKODU=CH.MUSTERIKODU),0),2)
FROM MUSTERI M Order By UNVAN ASC
,ALISMIKTAR=(CASE WHEN SH.GIRISCIKISTIPI=’GIRIS’ THEN ISNULL(SH.MIKTAR,0) ELSE 0 END)
,SATISMIKTAR=(CASE WHEN SH.GIRISCIKISTIPI=’CIKIS’ THEN ISNULL(SH.MIKTAR,0) ELSE 0 END)
,S.STOKSEVIYESIBIRIMI
,NETFIYATALIS=(CASE WHEN SH.GIRISCIKISTIPI=’GIRIS’ THEN ISNULL(SH.ARATOPLAM/SH.MIKTAR,0) ELSE 0 END)
,NETFIYATSATIS=(CASE WHEN SH.GIRISCIKISTIPI=’CIKIS’ THEN ISNULL(SH.ARATOPLAM/SH.MIKTAR,0) ELSE 0 END)
,D.DEPOADI
,DEPODAKIMIKTAR =dbo.DepodakiMiktar(S.STOKKODU,D.DEPOKODU,’2021-12-31 23:59:00.000′)
FROM STOKHAR SH,STOK S,DEPOLAR D,MUSTERI M WHERE SH.MUSTERIKODU=M.MUSTERIKODU AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.STOKKODU=S.STOKKODU AND D.DEPOKODU=SH.DEPOKODU
AND SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000’ AND D.DEPOKODU=0
GROUP BY S.STOKKODU,S.STOKADI,SH.GIRISCIKISTIPI,SH.MIKTAR,SH.TARIH,S.STOKSEVIYESIBIRIMI,D.DEPOADI,D.DEPOKODU,SH.ARATOPLAM,SH.EVRAKSERI,SH.EVRAKSIRA,SH.MUSTERIKODU,M.UNVAN,SH.NORMALIADE
ORDER BY SH.TARIH ASC
,ALISMIKTAR=(SELECT SUM(ISNULL(SH.MIKTAR,0)) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’GIRIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND D.DEPOKODU=SH.DEPOKODU )
,SATISMIKTAR=(SELECT SUM(ISNULL(SH.MIKTAR,0)) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’CIKIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND D.DEPOKODU=SH.DEPOKODU )
,S.STOKSEVIYESIBIRIMI
,NETFIYATALIS=(SELECT SUM(ISNULL(SH.ARATOPLAM/SH.MIKTAR,0)) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’GIRIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND D.DEPOKODU=SH.DEPOKODU )
,NETFIYATSATIS=(SELECT SUM(ISNULL(SH.ARATOPLAM/SH.MIKTAR,0)) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’CIKIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND D.DEPOKODU=SH.DEPOKODU )
,D.DEPOADI
,DEPODAKIMIKTAR =dbo.DepodakiMiktar(S.STOKKODU,D.DEPOKODU,’2021-12-31 23:59:00.000′)
FROM STOK S,DEPOLAR D WHERE
D.DEPOKODU=0
GROUP BY S.STOKKODU,S.STOKADI,S.STOKSEVIYESIBIRIMI,D.DEPOADI,D.DEPOKODU
SELECT S.STOKKODU,S.STOKADI
,ALISMIKTAR=(SELECT ISNULL(SUM(SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’GIRIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU=0 )
,SATISMIKTAR=(SELECT ISNULL(SUM(SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’CIKIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU IN (0,1,2,3,4,5,6,7,8) )
,S.STOKSEVIYESIBIRIMI
,NETFIYATALIS=(SELECT ISNULL(SUM(SH.ARATOPLAM/SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’GIRIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU=0 )
,NETFIYATSATIS=(SELECT ISNULL(SUM(SH.ARATOPLAM/SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’CIKIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU IN (0,1,2,3,4,5,6,7,8) )
,DEPODAKIMIKTAR =dbo.DepodakiMiktar(S.STOKKODU,0,’2021-12-31 23:59:00.000′)
FROM STOK S WHERE 1=1
GROUP BY S.STOKKODU,S.STOKADI,S.STOKSEVIYESIBIRIMI
—kar oranı ıle bırlıkte
SELECT *,KARORANI=CASE WHEN T1.NETFIYATSATIS=0 THEN 0 ELSE ROUND( (1-(T1.NETFIYATALIS/T1.NETFIYATSATIS))*100,2) END
FROM (
SELECT S.STOKKODU,S.STOKADI
,ALISMIKTAR=(SELECT ISNULL(SUM(SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’GIRIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU=0 )
,SATISMIKTAR=(SELECT ISNULL(SUM(SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’CIKIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU IN (0,1,2,3,4,5,6,7,8) )
,S.STOKSEVIYESIBIRIMI
,NETFIYATALIS=(SELECT ISNULL(SUM(SH.ARATOPLAM)/SUM(SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’GIRIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU=0 )
,NETFIYATSATIS=(SELECT ISNULL(SUM(SH.ARATOPLAM)/SUM(SH.MIKTAR),0) FROM STOKHAR SH WHERE SH.TARIH<‘2021-12-31 23:59:00.00′ AND SH.TARIH>=’2021-01-01 00:00:00.000′ AND SH.STOKKODU=S.STOKKODU AND SH.GIRISCIKISTIPI=’CIKIS’ AND SH.NORMALIADE=’NORMAL’ AND SH.EVRAKTIPI IN (‘FATURA’,’IRSALIYE’) AND SH.DEPOKODU IN (0,1,2,3,4,5,6,7,8) )
,DEPODAKIMIKTAR =dbo.DepodakiMiktar(S.STOKKODU,0,’2021-12-31 23:59:00.000′)
FROM STOK S WHERE 1=1
–AND S.STOKKODU=’110401′
GROUP BY S.STOKKODU,S.STOKADI,S.STOKSEVIYESIBIRIMI
) T1