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 +
        '    '#13 +
        ' SELECT '#13 + 
        '    S.KOD1, SUM(H.MIKTARI) AS MIKTAR, BR.BIRIMADI'#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 +
        '  GROUP BY S.KOD1, BIRIMADI ' +
        '  ORDER BY KOD1';
        
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), 
      'KOD1;MIKTAR;BIRIMADI', 
	  'Marka;Miktari;Birim', 
	  0);
    CreateSummary('MIKTAR', skSUm);
  End;
End;

Begin
End.