Var
  CariID: Integer;
  Tarih1, Tarih2: TDateTime;

procedure FindParam;
Var 
  S: TStringList;
  I: Integer;
  C: String;
Begin
  S:= TStringList.Create;
  S.Text:= FFilter;
  
  For I:= 0 to S.Count - 1 Do
  Begin
    C:= S[I];
	
	if Length(C) > 9 Then
	  Delete(C, 1, 9);
	
    if Pos('CARIID = ', S[I]) = 1 Then
	  CariID:= StrToInt(C)
    else if Pos('TARIH1 = ', S[I]) = 1 Then
	  Tarih1:= StrToDate(C)
    else if Pos('TARIH2 = ', S[I]) = 1 Then
	  Tarih2:= StrToDate(C)
  End;
  S.Free;
End;

Function ReadSQL(W: String): String;
Begin
  FFilter:= W;
  FindParam;
  
  Result:= '  SET DATEFORMAT DMY  '#13 +
        '    '#13 +
        '  DECLARE @CARIID INT, @T1 DATETIME, @T2 DATETIME, @T DECIMAL(18, 4) '#13 +
        '    '#13 +
        ' SET @CARIID = ' + IntToStr(CariID) + #13 +
        ' SET @T1 = ' + QuotedStr(DateToStr(Tarih1)) + #13 +
        ' SET @T2 = ' + QuotedStr(DateToStr(Tarih2)) + #13 +
		' SET @T = ISNULL((SELECT SUM((ALACAK - BORC) * KUR) FROM CARIDOKUM WHERE CARIID =  @CARIID AND TARIH < @T1), 0)'#13 +
        '    '#13 +
        ' SELECT * FROM( '#13 +
        '  SELECT   '#13 +
        '    ISLEMTIPI, ISLEM, TARIH, BELGENO, KUR, STOKKODU, CINSI, MIKTARI, BIRIMADI, NETFIYAT * ((KDV + 100.0000) / 100.0000) AS FIYATI,  '#13 +
        '    CASE ISLEMTIPI WHEN 0 THEN NETTUTAR * ((KDV + 100.0000) / 100.0000) ELSE NULL END AS BORC,  '#13 +
        '    CASE ISLEMTIPI WHEN 1 THEN NETTUTAR * ((KDV + 100.0000) / 100.0000) ELSE NULL END AS ALACAK  '#13 +
        '  FROM(  '#13 +
        '  SELECT   '#13 +
        '    0 AS ISLEMTIPI, ''SATIŞ'' AS ISLEM, B.TARIH, B.BELGENO, B.KUR, S.STOKKODU, S.CINSI,   '#13 +
        '    H.MIKTARI, BR.BIRIMADI, H.KDV,   '#13 +
        '    H.FIYATI,   '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y) / ' +
		'        CASE when ISNULL(H.MIKTARI, 0) = 0 THEN 1 ELSE H.MIKTARI END, 4) AS NETFIYAT, '#13 +
        '    H.FIYATI * H.MIKTARI AS TUTARI,  '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y), 4) AS NETTUTAR  '#13 +
        '  FROM TBLSATFATHAREKET H  '#13 +
        '  LEFT OUTER JOIN TBLSATFATBASLIK B WITH(INDEX=TBLSATFATBASLIK_KARTNO) ON B.KARTNO = H.FATURAID  '#13 +
        '  LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON S.KARTNO = H.STOKID  '#13 +
        '  LEFT OUTER JOIN TBLBIRIMLEREX BR WITH(INDEX=TBLBIRIMLEREX_KARTNO) ON BR.KARTNO = H.BIRIMID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        ' UNION ALL '#13 +
        ' SELECT   '#13 +
        '    1, ''ALIŞ'' AS ISLEM, B.TARIH, B.BELGENO, B.KUR, S.STOKKODU, S.CINSI,   '#13 +
        '    H.MIKTARI, BR.BIRIMADI, H.KDV,   '#13 +
        '    H.FIYATI,   '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y) / ' + 
		'        CASE when ISNULL(H.MIKTARI, 0) = 0 THEN 1 ELSE H.MIKTARI END, 4) AS NETFIYAT, '#13 +
        '    H.FIYATI * H.MIKTARI AS TUTARI,  '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y), 4) AS NETTUTAR  '#13 +
        '  FROM TBLALFATHAREKET H  '#13 +
        '  LEFT OUTER JOIN TBLALFATBASLIK B WITH(INDEX=TBLALFATBASLIK_KARTNO) ON B.KARTNO = H.FATURAID  '#13 +
        '  LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON S.KARTNO = H.STOKID  '#13 +
        '  LEFT OUTER JOIN TBLBIRIMLEREX BR WITH(INDEX=TBLBIRIMLEREX_KARTNO) ON BR.KARTNO = H.BIRIMID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        ' UNION ALL '#13 +
        ' SELECT   '#13 +
        '    1, ''SATIŞ İADE'' AS ISLEM, B.TARIH, B.BELGENO, B.KUR, S.STOKKODU, S.CINSI,   '#13 +
        '    H.MIKTARI, BR.BIRIMADI, H.KDV,   '#13 +
        '    H.FIYATI,   '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y) / ' + 
		'        CASE when ISNULL(H.MIKTARI, 0) = 0 THEN 1 ELSE H.MIKTARI END, 4) AS NETFIYAT, '#13 +
        '    H.FIYATI * H.MIKTARI AS TUTARI,  '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y), 4) AS NETTUTAR  '#13 +
        '  FROM TBLSATFATIADEHAREKET H  '#13 +
        '  LEFT OUTER JOIN TBLSATFATIADEBASLIK B WITH(INDEX=TBLSATFATIADEBASLIK_KARTNO) ON B.KARTNO = H.FATURAID  '#13 +
        '  LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON S.KARTNO = H.STOKID  '#13 +
        '  LEFT OUTER JOIN TBLBIRIMLEREX BR WITH(INDEX=TBLBIRIMLEREX_KARTNO) ON BR.KARTNO = H.BIRIMID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        ' UNION ALL '#13 +
        ' SELECT   '#13 +
        '    0 AS ISLEMTIPI, ''ALIŞ İADE'' AS ISLEM, B.TARIH, B.BELGENO, B.KUR, S.STOKKODU, S.CINSI,   '#13 +
        '    H.MIKTARI, BR.BIRIMADI, H.KDV,   '#13 +
        '    H.FIYATI,   '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y) / ' + 
		'        CASE when ISNULL(H.MIKTARI, 0) = 0 THEN 1 ELSE H.MIKTARI END, 4) AS NETFIYAT, '#13 +
        '    H.FIYATI * H.MIKTARI AS TUTARI,  '#13 +
        '    ROUND(dbo.ISKONTODUS(dbo.ISKONTODUS(H.MIKTARI * H.FIYATI - H.ISKONTOTOPLAMI, B.ALTISKONTO1Y), B.ALTISKONTO2Y), 4) AS NETTUTAR  '#13 +
        '  FROM TBLALFATIADEHAREKET H  '#13 +
        '  LEFT OUTER JOIN TBLALFATIADEBASLIK B WITH(INDEX=TBLALFATIADEBASLIK_KARTNO) ON B.KARTNO = H.FATURAID  '#13 +
        '  LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON S.KARTNO = H.STOKID  '#13 +
        '  LEFT OUTER JOIN TBLBIRIMLEREX BR WITH(INDEX=TBLBIRIMLEREX_KARTNO) ON BR.KARTNO = H.BIRIMID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        '  ) AS A  '#13 +
        '  UNION ALL  '#13 +
        '  SELECT   '#13 +
        '    2, ''TAHSİLAT'', TARIH, BELGENO, B.KUR, ''TAHSİLAT'',   '#13 +
        '    CASE H.ISLEM   '#13 +
        '      WHEN 0 THEN ''NAKİT''  '#13 +
        '      WHEN 1 THEN ''ÇEK''  '#13 +
        '      WHEN 2 THEN ''SENET''  '#13 +
        '      WHEN 3 THEN ''VİSA''  '#13 +
        '      WHEN 7 THEN ''DEKONT''  '#13 +
        '    END, NULL, NULL, NULL,  '#13 +
        '    NULL, SUM(H.TUTAR) '#13 +
        '  FROM TBLCARIGIRISDETAY H  '#13 +
        '  LEFT OUTER JOIN TBLCARIGIRIS B WITH(INDEX=TBLCARIGIRIS_KARTNO) ON B.KARTNO = H.BASLIKID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        '  GROUP BY B.TARIH, B.BELGENO, H.ISLEM, B.KUR  '#13 +
        ' UNION ALL '#13 +
        '  SELECT   '#13 +
        '    2, ''ÖDEME'', TARIH, BELGENO, B.KUR, ''ÖDEME'',   '#13 +
        '    CASE H.ISLEM   '#13 +
        '      WHEN 0 THEN ''NAKİT''  '#13 +
        '      WHEN 1 THEN ''ÇEK''  '#13 +
        '      WHEN 2 THEN ''SENET''  '#13 +
        '      WHEN 3 THEN ''VİSA''  '#13 +
        '      WHEN 4 THEN ''ŞAHSİ ÇEK''  '#13 +
        '      WHEN 5 THEN ''ŞAHSİ SENET''  '#13 +
        '      WHEN 6 THEN ''ŞAHSİ VİSA''  '#13 +
        '      WHEN 7 THEN ''DEKONT''  '#13 +
        '      WHEN 9 THEN ''ÇEK''  '#13 +
        '      WHEN 4 THEN ''SENET''  '#13 +
        '    END, NULL, NULL, NULL,  '#13 +
        '    SUM(H.TUTAR), NULL '#13 +
        '  FROM TBLCARICIKISDETAY H  '#13 +
        '  LEFT OUTER JOIN TBLCARICIKIS B WITH(INDEX=TBLCARICIKIS_KARTNO) ON B.KARTNO = H.BASLIKID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        '  GROUP BY B.TARIH, B.BELGENO, H.ISLEM, B.KUR  '#13 +
        ' UNION ALL '#13 +
        ' SELECT   '#13 +
        '    2, ''ÖDEME İADE'', TARIH, BELGENO, B.KUR, ''ÖDEME İADE'',   '#13 +
        '    CASE H.ISLEM   '#13 +
        '      WHEN 0 THEN ''NAKİT''  '#13 +
        '      WHEN 1 THEN ''ÇEK''  '#13 +
        '      WHEN 2 THEN ''SENET''  '#13 +
        '      WHEN 3 THEN ''VİSA''  '#13 +
        '      WHEN 4 THEN ''ŞAHSİ ÇEK''  '#13 +
        '      WHEN 5 THEN ''ŞAHSİ SENET''  '#13 +
        '      WHEN 6 THEN ''ŞAHSİ VİSA''  '#13 +
        '      WHEN 7 THEN ''DEKONT''  '#13 +
        '      WHEN 9 THEN ''ÇEK''  '#13 +
        '      WHEN 4 THEN ''SENET''  '#13 +
        '    END, NULL, NULL, NULL,  '#13 +
        '    NULL, SUM(H.TUTAR) '#13 +
        '  FROM TBLCARICIKISIADEDETAY H  '#13 +
        '  LEFT OUTER JOIN TBLCARICIKISIADE B WITH(INDEX=TBLCARICIKISIADE_KARTNO) ON B.KARTNO = H.BASLIKID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        '  GROUP BY B.TARIH, B.BELGENO, H.ISLEM, B.KUR  '#13 +
        ' UNION ALL '#13 +
        ' SELECT   '#13 +
        '    2, ''TAHSİLAT İADE'', TARIH, BELGENO, B.KUR, ''TAHSİLAT İADE'',   '#13 +
        '    CASE H.ISLEM   '#13 +
        '      WHEN 0 THEN ''NAKİT''  '#13 +
        '      WHEN 1 THEN ''ÇEK''  '#13 +
        '      WHEN 2 THEN ''SENET''  '#13 +
        '      WHEN 3 THEN ''VİSA''  '#13 +
        '      WHEN 4 THEN ''ŞAHSİ ÇEK''  '#13 +
        '      WHEN 5 THEN ''ŞAHSİ SENET''  '#13 +
        '      WHEN 6 THEN ''ŞAHSİ VİSA''  '#13 +
        '      WHEN 7 THEN ''DEKONT''  '#13 +
        '      WHEN 9 THEN ''ÇEK''  '#13 +
        '      WHEN 4 THEN ''SENET''  '#13 +
        '    END, NULL, NULL, NULL,  '#13 +
        '    SUM(H.TUTAR), NULL '#13 +
        '  FROM TBLCARIGIRISIADEDETAY H  '#13 +
        '  LEFT OUTER JOIN TBLCARIGIRISIADE B WITH(INDEX=TBLCARIGIRISIADE_KARTNO) ON B.KARTNO = H.BASLIKID  '#13 +
        '  WHERE ISNULL(B.DELETED, 0) = 0 AND B.CARIID = @CARIID AND B.TARIH BETWEEN @T1 AND @T2  '#13 +
        '  GROUP BY B.TARIH, B.BELGENO, H.ISLEM, B.KUR  '#13 +
        '  UNION ALL '#13 +
        '  SELECT 4, ISLEM, TARIH, BELGENO, KUR, ''NAKİT'', NULL, NULL, NULL, NULL, BORC, ALACAK '#13 +
        '  FROM CARIDOKUM '#13 +
        '  WHERE ISLEMTIPI IN(20, 21, 184) AND CARIID = @CARIID AND TARIH BETWEEN @T1 AND @T2  '#13 +
        '  UNION ALL'#13 +
        '  SELECT '#13 +
        '    0, ''DEVİR'', @T1 - 1, ''DEVİR'', 1, ''DEVİR'', NULL, NULL, NULL, NULL,'#13 +
        '    CASE WHEN @T < 0 THEN -1 * @T ELSE @T END, '#13 +
        '    CASE WHEN @T < 0 THEN 0 ELSE @T END '#13 +
		' '#13 +
        ' ) AS B ORDER BY TARIH ';  
End;

procedure RaporLoad;
Begin
  FindParam;
  
// Filtre Kullanılırsa
  if FFilter <> '' Then
  Begin
    SetQueryParam('ekstreQ', 'TARIH1', Tarih1);
	SetQueryParam('ekstreQ', 'TARIH2', Tarih2);
	SetQueryParam('ekstreQ', 'CARIID', CariID);

	SetQueryParam('cariQ', 'KARTNO', CariID);
  End;
End;

Function RaporFileName: String;
Begin
  Result:= ParsDir + 'Cari Kart Raporları\Hesap Ekstresi\Hesap Ekstresi Detay.fr3';
End;

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

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadFilter(Var F: String): Boolean;
Begin
  F:= '';
  Result:= ReadMusteriTarih(CariID, Tarih1, Tarih2);
  
  if Result Then
    F:= 'CARIID = ' + IntToStr(CariID) + #13#10 + 
	    'TARIH1 = ' + DateToStr(Tarih1) + #13#10 + 
		'TARIH2 = ' + DateToStr(Tarih2);
End;

procedure OnCreateReport;
Begin
//  CalcEkstreFromActivePage;

  CreateSummary('BORC', skSum);
  CreateSummary('ALACAK', skSum);
End;

procedure Execute;
Var
  F: String;
Begin
  if ReadFilter(F) Then
  Begin
    CreateReport(F, ReadSQL(F), 
      'ISLEM;TARIH;BELGENO;STOKKODU;CINSI;MIKTARI;BIRIMADI;FIYATI;BORC;ALACAK', 
	  'İşlem;Tarih;Belge No;Stok Kodu;Cinsi;Miktari;Birim;Fiyati;Borç;Alacak', 
	  0);
  End;
End;

Begin
End.