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
  FindParam;
  
  Result:='SET DATEFORMAT DMY ' + #13#10 +
		' SELECT  ' + #13#10 +
		QuotedStr(DateToStr(Tarih1)) + ' AS TARIH, ''NAKİL'' AS BELGENO,  ' + #13#10 +
		' ''NAKİL'' AS ISLEM, CARIDOKUM.DOVIZ, DATEPART(MONTH, GetDate()) AS AY, DATEPART(year, GetDate()) AS YIL,  ' + #13#10 +
		' CARIDOKUM.TLDURUM, CARIDOKUM.CARIDURUM, SUM(TUTARI) AS TUTARI, -1 AS ISLEMTIPI, 1 AS KUR,  ' + #13#10 +
		' SUM(ALACAK) AS ALACAK, SUM(BORC) AS BORC, SUM(TLALACAK) AS TLALACAK, SUM(TLBORC) AS TLBORC,  ' + #13#10 +
		' SUM(CARIBORC) AS CARIBORC, SUM(CARIALACAK) AS CARIALACAK ' + #13#10 +
		' FROM CARIDOKUM  LEFT OUTER JOIN TBLCARI ON TBLCARI.KARTNO = CARIDOKUM.CARIID ' + #13#10 +
		' Where CARIID = ' + IntToStr(cariID) + ' AND TARIH < ' + QuotedStr(DateToStr(Tarih1))  +  #13#10 +
		' GROUP BY ' + #13#10 +
  		'   CARIDOKUM.DOVIZ, CARIDOKUM.TLDURUM, CARIDOKUM.CARIDURUM ' + #13#10 +
		' UNION ALL ' + #13#10 +
		' SELECT  ' + #13#10 +
  		'   CARIDOKUM.TARIH, CARIDOKUM.BELGENO, CARIDOKUM.ISLEM, CARIDOKUM.DOVIZ,  ' + #13#10 +
  		'   CARIDOKUM.AY, CARIDOKUM.YIL,  CARIDOKUM.TLDURUM, CARIDOKUM.CARIDURUM, TUTARI, ISLEMTIPI, KUR, ALACAK, BORC, TLALACAK, TLBORC, CARIBORC, CARIALACAK ' + #13#10 +
		' FROM CARIDOKUM  LEFT OUTER JOIN TBLCARI ON TBLCARI.KARTNO = CARIDOKUM.CARIID ' + #13#10 +
		' Where CARIID = ' + IntToStr(cariID) + ' AND TARIH >= ' + QuotedStr(DateToStr(Tarih1)) + ' AND TARIH <= ' + QuotedStr(DateToStr(Tarih2)) +  #13#10 +
		' 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 Extresi Tarihli.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('CARIBORC', skSum);
  CreateSummary('CARIALACAK', skSum);
End;

procedure Execute;
Var
  F: String;
Begin
  if ReadFilter(F) Then
  Begin
    CreateReport(F, ReadSQL(F), 
      'TARIH;BELGENO;ISLEM;CARIBORC;CARIALACAK;CARIDURUM;TLBORC:0;TLALACAK:0;TLDURUM:0', 
	  'Tarih;Belge No;İşlem;Borç;Alacak;Durum;TL Borç;TL Alacak;TL Durum', 
	  0);
  End;
End;

Begin
End.