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:= StrToDateTime(C)
    else if Pos('TARIH2 = ', S[I]) = 1 Then
	  Tarih2:= StrToDateTime(C)
  End;
  S.Free;
End;
  
Function ReadSQL(W: String): String;
Begin
  FindParam(W);
  
  Result:= 'Set DateFormat DMY '#13 +
	' Declare @Tarih1 DateTime, @Tarih2 DateTime '#13 +
	' Declare @Rapor Table(SATIS DECIMAL(18, 4), IADE DECIMAL(18, 4), NAKIT DECIMAL(18, 4), VISA DECIMAL(18, 4), KAR DECIMAL(18, 4)) '#13 +
	'  '#13 +
	' Set @Tarih1 = ' + QuotedStr(FormatDateTime('dd.mm.yyyy hh:nn', Tarih1)) + #13 +
	' Set @Tarih2 = ' + QuotedStr(FormatDateTime('dd.mm.yyyy hh:nn', Tarih2)) + #13 +
	'  '#13 +
	' INSERT INTO @Rapor SELECT 0, 0, 0, 0, 0 '#13 +
	'  '#13 +
	' UPDATE @Rapor Set SATIS = (Select SUM(TUTARI) from TBLSATFATBASLIK B WHERE ISNULL(DELETED, 0) = 0 AND  KAYITTARIHI BETWEEN @Tarih1 AND @Tarih2)  '#13 +
	'  '#13 +
	' UPDATE @Rapor Set IADE = (Select SUM(TUTARI) from TBLSATFATIADEBASLIK B WHERE ISNULL(DELETED, 0) = 0 AND  KAYITTARIHI BETWEEN @Tarih1 AND @Tarih2)  '#13 +
	'  '#13 +
	' UPDATE @Rapor Set KAR = (SELECT SUM(KAR) FROM(Select  MIKTARI * (FIYATI - CASE WHEN ISNULL(H.MALIYET, 0) = 0 THEN   '#13 +
	'      ISNULL((SELECT TOP 1 FIYATI FROM TBLALFATHAREKET AFH LEFT OUTER JOIN TBLALFATBASLIK AFB WITH(INDEX=TBLALFATBASLIK_KARTNO) ON AFB.KARTNO = AFH.FATURAID  '#13 +
	' 	    WHERE ISNULL(AFB.DELETED, 0) = 0 AND AFH.STOKID = H.STOKID ORDER BY AFB.TARIH DESC), 0)  '#13 +
	'   ELSE H.MALIYET END) AS KAR  '#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 +
	' WHERE ISNULL(B.DELETED, 0) = 0 AND TARIH BETWEEN @Tarih1 AND @Tarih2  '#13 +
	' ) AS A) '#13 +
	'  '#13 +
	' UPDATE @Rapor Set NAKIT = (Select SUM(GIREN) from KASAHAREKET where ISLEMTIPI = 1 AND EVRAKTIPI = 0 AND KAYITTARIHI BETWEEN @Tarih1 AND @Tarih2)  '#13 +
	' UPDATE @Rapor Set VISA = (Select SUM(GIREN) from KASAHAREKET where ISLEMTIPI = 1 AND EVRAKTIPI = 3 AND KAYITTARIHI BETWEEN @Tarih1 AND @Tarih2)  '#13 +
	'  '#13 +	
	' Select * from @Rapor '#13#10;
End;

procedure RaporLoad;
Begin
  FindParam(FFilter);
  
  SetViewCaption('Tarih1', FormatDateTime('dd.mm.yyyy hh:nn', Tarih1));
  SetViewCaption('Tarih2', FormatDateTime('dd.mm.yyyy hh:nn', Tarih2));
  
  SetQueryParam('kullaniciQ', 'TARIH1', Tarih1);
  SetQueryParam('kullaniciQ', 'TARIH2', Tarih2);
End;

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

Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL('TARIH1 = ' + FormatDateTime('dd.mm.yyyy', Date) + #13#10 + 'TARIH2 = ' + FormatDateTime('dd.mm.yyyy', 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, true);
  if Result Then
  Begin
    F:= 'TARIH1 = ' + FormatDateTime('dd.mm.yyyy hh:nn', t1) + #13#10 + 'TARIH2 = ' + FormatDateTime('dd.mm.yyyy hh:nn', t2);
  End;
End;

procedure Execute;
Var
  IDS: String;
Begin
  if ReadFilter(IDS) Then
  Begin
	CreateReport(IDS, ReadSQL(IDS), 
     'SATIS;IADE;NAKIT;VISA;KAR', 
	 'Satış;İade;Nakit;VISA;Kar', 
	  0);
  End;
End;

Begin
End.