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 +
      '   SUBE, SUBEADI, TARIH, SUM(SATIS) - SUM(IADE) - SUM(HARCAMA) NETCIRO, SUM(HARCAMA) AS HARCAMA, '#13#10 +
      '   SUM(SATIS) AS SATIS, SUM(IADE) AS IADE, SUM(NAKIT) AS NAKIT, SUM(VISA) AS VISA, SUM(DIGER) AS UYGULAMA,  '#13#10 +
      '   SUM(KAMPANYAINDIRIM) AS KAMPANYAINDIRIM, SUM(PUANADET) AS PUANADET, SUM(PUANTUTAR) AS PUANTUTAR,  '#13#10 +
      '   SUM(INDIRIMCEKIADET) AS INDIRIMCEKIADET, SUM(INDIRIMCEKITUTAR) AS INDIRIMCEKITUTAR,   '#13#10 +
      '   SUM(HEDIYECEKIADET) AS HEDIYECEKIADET  '#13#10 +
      ' FROM(  '#13#10 +
      ' SELECT   '#13#10 +
      '   B.TARIH, S.ID AS SUBE, '''' AS OZELKOD1,  '#13#10 +
      '   SUM(CIKAN) AS HARCAMA, 0 AS SATIS, 0 AS IADE, 0 AS NAKIT, 0 AS VISA, 0 AS DIGER,  '#13#10 +
      '   0 AS KAMPANYAINDIRIM, 0 AS PUANADET,   '#13#10 +
      '   0 AS PUANTUTAR, 0 AS INDIRIMCEKIADET, 0 AS INDIRIMCEKITUTAR, 0 AS HEDIYECEKIADET  '#13#10 +
      ' FROM KASAHAREKET B  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON B.KASA = S.KASA  '#13#10 +
      ' WHERE B.ISLEMTIPI = 15 '#13#10 +
      ' GROUP BY B.TARIH, S.ID '#13#10 +
      ' UNION ALL '#13#10 +
      ' SELECT   '#13#10 +
      '   B.TARIH, S.ID AS SUBE, B.OZELKOD1,  '#13#10 +
      '   0 AS HARCAMA, SUM(B.TUTARI) AS SATIS, 0 AS IADE, 0 AS NAKIT, 0 AS VISA, 0 AS DIGER,  '#13#10 +
      '   0 AS KAMPANYAINDIRIM, 0 AS PUANADET,   '#13#10 +
      '   0 AS PUANTUTAR, 0 AS INDIRIMCEKIADET, 0 AS INDIRIMCEKITUTAR, 0 AS HEDIYECEKIADET  '#13#10 +
      ' FROM TBLSATFATBASLIK B  '#13#10 +
      ' LEFT OUTER JOIN TBLPERSONEL P ON B.PERSONELID = P.KARTNO  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON P.SUBEID = S.ID  '#13#10 +
      ' WHERE ISNULL(B.DELETED, 0) = 0  '#13#10 +
      ' GROUP BY B.TARIH, S.ID, B.OZELKOD1 '#13#10 +
      ' UNION ALL  '#13#10 +
      ' SELECT   '#13#10 +
      '   B.TARIH, S.ID AS SUBE, B.OZELKOD1, 0, 0, SUM(B.TUTARI), 0 AS NAKIT, 0 AS VISA, 0 AS DIGER,  '#13#10 +
      '   0 AS KAMPANYAINDIRIM, 0 AS PUANADET,   '#13#10 +
      '   0 AS PUANTUTAR, 0 AS INDIRIMCEKIADET, 0 AS INDIRIMCEKITUTAR, 0 AS HEDIYECEKIADET  '#13#10 +
      ' FROM TBLSATFATIADEBASLIK B  '#13#10 +
      ' LEFT OUTER JOIN TBLPERSONEL P ON B.PERSONELID = P.KARTNO  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON P.SUBEID = S.ID  '#13#10 +
      ' WHERE ISNULL(B.DELETED, 0) = 0  '#13#10 +
      ' GROUP BY B.TARIH, S.ID, B.OZELKOD1 '#13#10 +
      ' UNION ALL  '#13#10 +
      ' SELECT   '#13#10 +
      '   B.TARIH, S.ID, B.OZELKOD1, 0, 0, 0, SUM(D.TUTAR), 0, 0, 0, 0, 0, 0, 0, 0  '#13#10 +
      ' FROM TBLCARIGIRIS B  '#13#10 +
      ' LEFT OUTER JOIN TBLCARIGIRISDETAY D ON B.KARTNO = D.BASLIKID  '#13#10 +
      ' LEFT OUTER JOIN TBLPERSONEL P ON B.PERSONELID = P.KARTNO  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON P.SUBEID = S.ID  '#13#10 +
      ' WHERE ISNULL(B.DELETED, 0) = 0 AND D.ISLEM = 0  '#13#10 +
      ' GROUP BY B.TARIH, S.ID, B.OZELKOD1  '#13#10 +
      ' UNION ALL  '#13#10 +
      ' SELECT   '#13#10 +
      '   B.TARIH, S.ID, B.OZELKOD1, 0, 0, 0, 0, SUM(D.TUTAR), 0, 0, 0, 0, 0, 0, 0    '#13#10 +
      ' FROM TBLCARIGIRIS B  '#13#10 +
      ' LEFT OUTER JOIN TBLCARIGIRISDETAY D ON B.KARTNO = D.BASLIKID  '#13#10 +
      ' LEFT OUTER JOIN TBLPERSONEL P ON B.PERSONELID = P.KARTNO  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON P.SUBEID = S.ID  '#13#10 +
      ' WHERE ISNULL(B.DELETED, 0) = 0 AND D.ISLEM = 3  '#13#10 +
      ' GROUP BY B.TARIH, S.ID, B.OZELKOD1  '#13#10 +
      ' UNION ALL  '#13#10 +
      ' SELECT   '#13#10 +
      '   B.TARIH, S.ID, B.OZELKOD1, 0, 0, 0, 0, 0, SUM(D.TUTAR), 0, 0, 0, 0, 0, 0    '#13#10 +
      ' FROM TBLCARIGIRIS B  '#13#10 +
      ' LEFT OUTER JOIN TBLCARIGIRISDETAY D ON B.KARTNO = D.BASLIKID  '#13#10 +
      ' LEFT OUTER JOIN TBLPERSONEL P ON B.PERSONELID = P.KARTNO  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON P.SUBEID = S.ID  '#13#10 +
      ' WHERE ISNULL(B.DELETED, 0) = 0 AND NOT D.ISLEM IN(0, 3)  '#13#10 +
      ' GROUP BY B.TARIH, S.ID, B.OZELKOD1 '#13#10 +
      ' UNION ALL  '#13#10 +
      ' SELECT   '#13#10 +
      '   CONVERT(VARCHAR, B.TARIH, 104), SUBE, '''', 0, 0, 0, 0, 0, 0,   '#13#10 +
      '   SUM(KAMPANYAINDIRIM), SUM(PUANADET), SUM(PUANTUTAR), SUM(INDIRIMCEKIADET), SUM(INDIRIMCEKITUTAR), SUM(HEDIYECEKIADET)  '#13#10 +
      ' FROM TBLMAGAZAUYGULAMALINK B  '#13#10 +
      ' GROUP BY TARIH, SUBE  '#13#10 +
      ' ) AS A  '#13#10 +
      ' LEFT OUTER JOIN TBLSUBELER S ON S.ID = A.SUBE  '#13#10 +
      ' WHERE TARIH BETWEEN @T1 AND @T2  '#13#10 +
      ' GROUP BY TARIH, SUBE, SUBEADI '#13#10 +
      ' '#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ı\CiroRapor.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), 
     'SUBEADI;TARIH;SATIS;IADE;HARCAMA;NETCIRO;NAKIT;VISA;UYGULAMA;KAMPANYAINDIRIM;PUANADET;PUANTUTAR;INDIRIMCEKIADET;INDIRIMCEKITUTAR;HEDIYECEKIADET', 
	 'Şube;Tarih;Satış;Değişim;Gider;Net Ciro;Nakit;VİSA;Uygulama;Kampanya İndirim;Puan Adet;Puan Tutar;İndirim Çeki Ad.;İndirim Çeki Tutar;Hediye Çeki Adet', 
	  0);
	 
	CreateSummary('SATIS', skSum); 
	CreateSummary('IADE', skSum); 
	CreateSummary('HARCAMA', skSum); 
	CreateSummary('NETCIRO', skSum); 
	CreateSummary('NAKIT', skSum); 
	CreateSummary('VISA', skSum); 
	CreateSummary('UYGULAMA', skSum); 
	CreateSummary('KAMPANYAINDIRIM', skSum); 
	CreateSummary('PUANADET', skSum); 
	CreateSummary('PUANTUTAR', skSum); 
	CreateSummary('INDIRIMCEKIADET', skSum); 
	CreateSummary('INDIRIMCEKITUTAR', skSum); 
	CreateSummary('HEDIYECEKIADET', skSum); 
	
	CreateSummary('SUBEADI', skCount); 
	
  End;
End;

Begin
End.