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 +
   '   C.FIRMAKODU, C.FIRMAADI, S.SUBEADI, ISNULL(P.AD, '''') + '' '' + ISNULL(P.SOYAD, '''') AS PERSONEL, BELGENO, B.TARIH, ' + #13#10 +
   '   TUTARI, B.ACIKLAMA, B.OZELKOD1, B.OZELKOD2, B.OZELKOD3, B.OZELKOD4, B.OZELKOD5 ' + #13#10 +
   ' FROM TBLSATFATBASLIK B ' + #13#10 +
   ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID ' + #13#10 +
   ' LEFT OUTER JOIN TBLPERSONEL P WITH(INDEX=TBLPERSONEL_KARTNO) ON P.KARTNO = B.PERSONELID ' + #13#10 +
   ' LEFT OUTER JOIN TBLCARISUBELER S WITH(INDEX=TBLCARISUBELER_CARIID) ON S.CARIID = B.CARIID AND S.ID = B.SUBEID '#13#10 +
   ' WHERE B.CARIID = ' + IntToStr(CariID) + ' AND B.TARIH >= ' + QuotedStr(DateToStr(Tarih1)) + ' AND B.TARIH <= ' + QuotedStr(DateToStr(Tarih2)) + #13#10 +
   ' ORDER BY B.TARIH';
End;
	
Function RaporFileName: String;
Begin
  Result:= ''; // ParsDir + 'Fatura Raporları\Satış Faturaları\SatisFaturalari.fr3';
End;
   
Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL('');
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 Execute;
Var
  F: String;
Begin
  if ReadFilter(F) Then
  Begin
    CreateReport(F, ReadSQL(F), 
      'FIRMAKODU:0;FIRMAADI;TARIH;BELGENO;SUBEADI;TUTARI;OZELKOD1;OZELKOD2:0;OZELKOD3:0;OZELKOD4:0;OZELKOD5:0;ACIKLAMA', 
	  'Fİrma Kodu;Firma Adı;Tarih;Belge No;Şube;Tutarı;Ö.K.1;Ö.K.2;Ö.K.3;Ö.K.4;Ö.K.5;Açıklama', 
	  0);
	  
	CReateSummary('TUTARI', skSum);
  End;
End;

Begin
End.

