Function ReadSQL(W: String): String;
Begin
  Result:= 
        ' SELECT A.*, ' +
        '   CASE A.ISLEM WHEN -1 THEN ''DEVİR'' WHEN -2 THEN ''KASA HAREKET'' WHEN -3 THEN ''CARİ GİRİŞ'' ' +
        '   WHEN -4 THEN ''CARİ ÇIKIŞ'' WHEN -5 THEN ''BANKAYA ÇIKIŞ'' WHEN -6 THEN ''BANKADAN GİRİŞ'' ' +
        '   WHEN -7 THEN ''PERSONEL AVANS'' WHEN -8 THEN ''PERSONEL MAAŞ'' END AS ISLEMACIKLAMA, '  +
        '   K.KASAADI AS KASA,  ' +
        '   CASE WHEN A.ISLEM IN(-3, -4) THEN C.FIRMAKODU WHEN A.ISLEM IN(-5, -6) THEN B.BANKAKODU WHEN A.ISLEM IN(-7, -8) THEN P.AD + '' '' + P.SOYAD ELSE '''' END AS ISLEMKODU, ' +
        '   CASE WHEN A.ISLEM IN(-3, -4) THEN C.FIRMAADI WHEN A.ISLEM IN(-5, -6) THEN B.BANKAADI WHEN A.ISLEM IN(-7, -8) THEN P.AD + '' '' + P.SOYAD ELSE '''' END AS ISLEMADI, ' +
        '   CASE WHEN A.ISLEM IN(-3, -4) THEN C.KOD3 ELSE '''' END AS KOD3, ' +
        '   CASE WHEN A.ISLEM IN(-3, -4) THEN C.KOD4 ELSE '''' END AS KOD4, ' +
        '   CASE WHEN A.ISLEM = -2 THEN I.ISLEMADI ELSE '''' END AS ISLEMADI ' +
        ' FROM( ' +
        ' SELECT ' +
        '   -1 AS ISLEM, ''DEVİR'' AS ISLEMACIKLAMASI, ' +
        '   CAST(''01.01.'' + ISNULL(CAST((SELECT KARTNO FROM TBLDEVIR WHERE MODULID = -99999) AS VARCHAR), ' +
        '   (CAST(DATEPART(yyyy, GETDATE()) AS VARCHAR) + '' 00:00:01'')) AS DATETIME) AS TARIH, ''DEVİR'' AS BELGENO,   ' +
        '   CASE WHEN DEVIR < 0.000 THEN -1 * DEVIR ELSE 0 END AS CIKAN, ' +
        '   CASE WHEN DEVIR > 0.001 THEN DEVIR ELSE 0 END AS GIREN, 0 AS KARTID, 0 AS KASAID, ''DEVİR'' AS GRUPADI, ''DEVİR'' AS ALTGRUPADI, ''DEVİR'' AS ARAGRUPADI ' +
        ' FROM TBLDEVIR D WITH(INDEX=TBLDEVIR_INDEX) ' +
        ' WHERE MODULID = 35 ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -2, I.ISLEMADI, H.TARIH, '''' AS BELGENO,   ' +
        '   CASE I.TIP WHEN 0 THEN 0 ELSE TUTAR END AS CIKAN, CASE I.TIP WHEN 1 THEN 0 ELSE H.TUTAR END AS GIREN, ' +
        '   H.ISLEM, H.KASA, I.GRUPADI, I.ALTGRUPADI, I.ARAGRUPADI ' +
        ' FROM TBLKASAHAREKET H ' +
        ' LEFT JOIN TBLKASAISLEMLER I ON H.ISLEM = I.ID ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -3, B.ACIKLAMA, B.TARIH, BELGENO, 0, D.TUTAR,B.CARIID, B.KASA, ''CARİ İŞLEMLER'' AS GRUPADI, ''TAHSİLAT'' AS ALTGRUPADI, ''CARİ GİRİŞ'' AS ARAGRUPADI ' +
        ' FROM TBLCARIGIRISDETAY D ' +
        ' LEFT JOIN TBLCARIGIRIS B WITH (INDEX = TBLCARIGIRIS_KARTNO) ON D.BASLIKID = B.KARTNO ' +
        ' WHERE B.DELETED = 0 AND D.ISLEM IN (0, 1, 2, 3) ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -4, B.ACIKLAMA,  B.TARIH, BELGENO, D.TUTAR, 0, B.CARIID, B.KASA, ''CARİ İŞLEMLER'' AS GRUPADI, ''ÖDEME'' AS ALTGRUPADI, ''CARİ ÇIKIŞ'' AS ARAGRUPADI ' +
        ' FROM TBLCARICIKISDETAY D ' +
        ' LEFT JOIN TBLCARICIKIS B WITH (INDEX = TBLCARICIKIS_KARTNO) ON D.BASLIKID = B.KARTNO ' +
        ' WHERE B.DELETED = 0 AND D.ISLEM IN (0, 1, 2, 3) ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -5, B.ACIKLAMA,  B.TARIH, BELGENO, D.TUTAR, 0, BANKAID, KASAID, ''BANKA'' AS GRUPADI, ''TRANSFER'' AS ALTGRUPADI, ''NAKİT ÇIKIŞ'' AS ARAGRUPADI ' +
        ' FROM TBLBANKANAKITGIRISDETAY D ' +
        ' LEFT JOIN TBLBANKANAKITGIRIS B WITH (INDEX = TBLBANKANAKITGIRIS_KARTNO) ON D.BASLIKID = B.KARTNO ' +
        ' WHERE B.DELETED = 0 ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -6, B.ACIKLAMA,  B.TARIH, BELGENO, 0, D.TUTAR, B.BANKAID, D.KASAID, ''BANKA'' AS GRUPADI, ''TRANSFER'' AS ALTGRUPADI, ''NAKİT GİRİŞ'' AS ARAGRUPADI ' +
        ' FROM TBLBANKANAKITCIKISDETAY D ' +
        ' LEFT JOIN TBLBANKANAKITCIKIS B WITH (INDEX = TBLBANKANAKITCIKIS_KARTNO) ON D.BASLIKID = B.KARTNO ' +
        ' LEFT JOIN TBLBANKALAR BK WITH (INDEX = TBLBANKALAR_KARTNO) ON B.BANKAID = BK.KARTNO ' +
        ' WHERE B.DELETED = 0 ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -7, ''PERSONEL AVANS'', CAST(CONVERT(VARCHAR, TARIH, 104) AS DATETIME), '''', H.BORC, 0, H.PERSONELID, P.KASAKARTNO, ''PERSONEL'' AS GRUPADI, ''PERSONEL'' AS ALTGRUPADI, ''AVANS'' AS ARAGRUPADI ' +
        ' FROM TBLPERSONELHAREKET H ' +
        ' LEFT JOIN TBLPERSONEL P WITH (INDEX = TBLPERSONEL_KARTNO) ON H.PERSONELID = P.KARTNO ' +
        ' WHERE H.TIP = 4 ' +
        ' UNION ALL ' +
        ' SELECT ' +
        '   -8, ''PERSONEL MAAŞ'', CAST(CONVERT(VARCHAR, TARIH, 104) AS DATETIME), '''', H.BORC, 0, H.PERSONELID, P.KASAKARTNO, ''PERSONEL'' AS GRUPADI, ''PERSONEL'' AS ALTGRUPADI, ''MAAŞ'' AS ARAGRUPADI ' +
        ' FROM TBLPERSONELHAREKET H ' +
        ' LEFT JOIN TBLPERSONEL P WITH (INDEX = TBLPERSONEL_KARTNO) ON H.PERSONELID = P.KARTNO ' +
        ' WHERE H.TIP = 0 ' +
        ' ) AS A ' +
        ' LEFT OUTER JOIN TBLKASALAR K ON K.ID = A.KASAID ' +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = A.KARTID ' +
        ' LEFT OUTER JOIN TBLKASAISLEMLER I ON I.ID = A.KARTID ' +
        ' LEFT OUTER JOIN TBLPERSONEL P ON P.KARTNO = A.KARTID ' + 
		' LEFT OUTER JOIN TBLBANKALAR B ON B.KARTNO = A.KARTID ';
  if W <> '' Then
    Result:= Result + ' WHERE ' + W;
  Result:= Result + ' ORDER BY TARIH';		
End;		

 procedure RaporLoad;
Begin
End;

Function RaporFileName: String;
Begin
  Result:= '';
End;

Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL('');
End;

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadFilter(Var F: String): Boolean;
Var
  t1, t2: TDateTime;
Begin
  Result:= Input2Date('Tarihleri Girin', t1, t2);
  if Result Then
  Begin
    F:= ' CAST(CONVERT(VARCHAR, A.TARIH, 104) AS DATETIME) >= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', t1)) + ' AS DATETIME) ' + 
        ' AND CAST(CONVERT(VARCHAR, A.TARIH, 104) AS DATETIME) <= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', t2)) + ' AS DATETIME)';  
  End;
End;

procedure Execute;
Var
  IDS: String;
Begin
  if ReadFilter(IDS) Then
  Begin
    CreateReport(IDS, ReadSQL(IDS), 
     'ISLEMACIKLAMA;ISLEMACIKLAMASI;GRUPADI:0;ALTGRUPADI:0;ARAGRUPADI:0;TARIH;BELGENO;ISLEMKODU;ISLEMADI;KASA;PERSONEL;GIREN;CIKAN;KOD3;KOD4', 
	 'İşlem;Açıklama;Grup;Alt Grup;Ara Grup;Tarih;Belge No;İşlem Kodu;İşlem Adı;Kasa;Personel;Giren;Çıkan;Ö.Kod 3;Ö.Kod 4', 
	  0);
	  
	CreateSummary('GIREN', skSum); 
	CreateSummary('CIKAN', skSum); 
	CreateSummary('ISLEMACIKLAMA', skCount);   
  End;
End;

Begin
End.