Var
  Tarih1, Tarih2: TDateTime;

procedure FindParam(FF: String);
Var 
  S: TStringList;
  I: Integer;
  C: String;
Begin
  S:= TStringList.Create;
  S.Text:= FF;
  
  For I:= 0 to S.Count - 1 Do
  Begin
    C:= S[I];
	
	if Length(C) > 9 Then
	  Delete(C, 1, 9);
	
    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
  if W <> '' Then
    FindParam(W)
  else 
    FindParam(FFilter);
	
  Result:=       ' Set DateFormat DMY  ' + #13#10 +
      '  Declare @T1 DateTime, @T2 DateTime  ' + #13#10 +
      '    ' + #13#10 +
      ' Set @T1 = ' + QuotedStr(DateToStr(Tarih1)) +  #13#10 +
      ' Set @T2 = ' + QuotedStr(DateToStr(Tarih2)) +  #13#10 +
      '    ' + #13#10 +
      '  SELECT  ' + #13#10 +
      '    TARIH, P.AD + '' '' + P.SOYAD AS PERSONEL, SUM(SATIS) AS SATIS, SUM(IADE) AS IADE, SUM(NAKIT) AS NAKIT, SUM(VISA) AS VISA, SUM(DIGER) AS DIGER ' + #13#10 +
      '  FROM(  ' + #13#10 +
      '  SELECT   ' + #13#10 +
      '    B.TARIH, SUM(B.TUTARI) AS SATIS, 0 AS IADE, 0 AS NAKIT, 0 AS VISA, 0 AS DIGER, B.PERSONELID  ' + #13#10 +
      '  FROM TBLSATFATBASLIK B  ' + #13#10 +
      '  WHERE ISNULL(B.DELETED, 0) = 0  ' + #13#10 +
      '  GROUP BY B.TARIH, B.PERSONELID  ' + #13#10 +
      '  UNION ALL  ' + #13#10 +
      '  SELECT   ' + #13#10 +
      '    B.TARIH, 0, SUM(B.TUTARI) AS SATIS, 0 AS NAKIT, 0 AS VISA, 0 AS DIGER, B.PERSONELID  ' + #13#10 +
      '  FROM TBLSATFATIADEBASLIK B  ' + #13#10 +
      '  WHERE ISNULL(B.DELETED, 0) = 0  ' + #13#10 +
      '  GROUP BY B.TARIH, B.PERSONELID  ' + #13#10 +
      '  UNION ALL  ' + #13#10 +
      '  SELECT   ' + #13#10 +
      '    B.TARIH, 0, 0, SUM(D.TUTAR), 0, 0, B.PERSONELID  ' + #13#10 +
      '  FROM TBLCARIGIRIS B  ' + #13#10 +
      '  LEFT OUTER JOIN TBLCARIGIRISDETAY D ON B.KARTNO = D.BASLIKID  ' + #13#10 +
      '  WHERE ISNULL(B.DELETED, 0) = 0 AND D.ISLEM = 0  ' + #13#10 +
      '  GROUP BY B.TARIH, B.PERSONELID  ' + #13#10 +
      '  UNION ALL  ' + #13#10 +
      '  SELECT   ' + #13#10 +
      '    B.TARIH, 0, 0, 0, SUM(D.TUTAR), 0, B.PERSONELID  ' + #13#10 +
      '  FROM TBLCARIGIRIS B  ' + #13#10 +
      '  LEFT OUTER JOIN TBLCARIGIRISDETAY D ON B.KARTNO = D.BASLIKID  ' + #13#10 +
      '  WHERE ISNULL(B.DELETED, 0) = 0 AND D.ISLEM = 3  ' + #13#10 +
      '  GROUP BY B.TARIH, B.PERSONELID ' + #13#10 +
      '  UNION ALL  ' + #13#10 +
      '  SELECT   ' + #13#10 +
      '    B.TARIH, 0, 0, 0, 0, SUM(D.TUTAR), B.PERSONELID  ' + #13#10 +
      '  FROM TBLCARIGIRIS B  ' + #13#10 +
      '  LEFT OUTER JOIN TBLCARIGIRISDETAY D ON B.KARTNO = D.BASLIKID  ' + #13#10 +
      '  WHERE ISNULL(B.DELETED, 0) = 0 AND NOT D.ISLEM IN(0, 3)  ' + #13#10 +
      '  GROUP BY B.TARIH, B.PERSONELID  ' + #13#10 +
      '  ) AS A  ' + #13#10 +
      '  LEFT OUTER JOIN TBLPERSONEL P ON A.PERSONELID = P.KARTNO  ' + #13#10 +
      '  ' + #13#10 +
      '  WHERE TARIH BETWEEN @T1 AND @T2  ' + #13#10 +
      '  GROUP BY TARIH, P.AD, P.SOYAD ' + #13#10 +
      '  ';
End;

procedure RaporLoad;
Begin
  FindParam(FFilter);
  
  SetViewCaption('BASLIK', 'PARADOX ' + FormatDateTime('dd.mm.yyyy', Tarih1) + ' İLE ' + FormatDateTime('dd.mm.yyyy', Tarih2) + 
       ' ARASI CİRO RAPORU');
  
  SetQueryParam('kullaniciQ', 'TARIH1', Tarih1);
  SetQueryParam('kullaniciQ', 'TARIH2', Tarih2);
End;

Function RaporFileName: String;
Begin
  Result:= ParsDir + 'Karlılık Raporları\CiroRaporu.fr3';
End;

Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL('TARIH1 = ' + DateToStr(Date) + #13#10 + 'TARIH2 = ' + DateToStr(Date));
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:= 'TARIH1 = ' + DateToStr(t1) + #13#10 + 'TARIH2 = ' + DateToStr(t2);
  End;
End;

procedure Execute;
Var
  IDS: String;
Begin
  if ReadFilter(IDS) Then
  Begin
	CreateReport(IDS, ReadSQL(IDS), 
     'PERSONEL;TARIH;SATIS;NAKIT;VISA;DIGER', 
	 'Personel;Tarih;Satış;Nakit;VİSA;Diğer', 
	  0);
	 
	CreateSummary('SATIS', skSum); 
	CreateSummary('NAKIT', skSum); 
	CreateSummary('VISA', skSum); 
	CreateSummary('DIGER', skSum); 
	
	CreateSummary('PERSONEL', skCount); 
	
  End;
End;

Begin
End.
