Function ReadSQL(W: String; R: Boolean = False): String;
Var
  D, IT, DF: String;
  I: Integer;
Begin
  if R Then
	W:= FFilter;
	
  I:= Pos('####', W);
  if I > 0 THEN
  Begin
    DF:= Copy(W, 1, I - 1);
	Delete(W, 1, I + 4);
	I:= Pos('&&', DF);
	if I > 0 THEN
	Begin
	  IT:= Copy(DF, 1, I - 1);
	  Delete(DF, 1, I + 2);
	 End;
  End;
  
  D:= 'SELECT CAST(' + QuotedStr(IT) + ' AS DATETIME) AS TARIH, '#13#10 +
    ' ''DEVİR'' AS KASAADI, ''DEVİR'' AS KASAACIKLAMA, ''DEVİR'' AS ACIKLAMA, ''DEVİR'' AS ISLEMADI, '#13#10 + 
	' ISNULL(SUM(GIREN), 0) AS GIREN, ISNULL(SUM(CIKAN), 0) AS CIKAN, '#13#10 + 
	' CAST(0 AS DECIMAL(18, 4)) AS DURUM, ''DEVİR'' AS EVRAKTIPI '#13#10 + 
	' FROM KASAHAREKET H '#13#10 +
	' LEFT OUTER JOIN TBLKASALAR K ON K.ID = H.KASA '#13#10 + 
	' WHERE ISNULL(K.DELETED, 0) = 0 '#13#10;
  if DF <> '' Then
    D:= D + ' AND ' + DF;
	
  Result:= 'Set DateFormat DMY SELECT * FROM(' + #13#10 +
    D + #13#10 +
	' UNION ALL '#13#10 + 
    ' SELECT TARIH,  K.KASAADI, K.ACIKLAMA AS KASAACIKLAMA, H.ACIKLAMA, ISLEMADI, GIREN, CIKAN, '#13#10 + 
    ' CAST(0 AS DECIMAL(18, 4)) AS DURUM, '#13#10 +
    ' CASE COALESCE(EVRAKTIPI, 0) '#13#10 +
	' WHEN 0 THEN ''NAKİT'' WHEN 1 THEN ''ÇEK'' WHEN 2 THEN ''SENET'' WHEN 3 THEN ''VİSA'' END AS EVRAKTIPI '#13#10 +
	' from KASAHAREKET H '#13#10 + 
	' LEFT OUTER JOIN TBLKASALAR K ON K.ID = H.KASA '#13#10 + 
	' WHERE ISNULL(K.DELETED, 0) = 0 ';
	
  if W <> '' Then
    Result:= Result + ' AND ' + W;
  Result:= Result + ') AS A ORDER BY TARIH';
End;

Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL('', True);
End;

procedure RaporLoad;
Begin
  SetViewCaption('BASLIK', 'PARADOX KASA HAREKET RAPORU');
  SetQuerySQL('kullaniciQ', ReadSQL('', True));
End;

Function RaporFileName: String;
Begin
  Result:= ParsDir + 'Kasa Raporları\Kasa Hareket.fr3';
End;

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

procedure OnCreateReport;
Begin
  CalcLineTotal('CIKAN;GIREN;DURUM');
End;

procedure cxButton1Click(Sender: TObject);
Var
  E: String;
  
  procedure Ekle(S: String);
  Begin
    if E = '' Then
	  E:= S
	else
	  E:= E + ', ' + S;
  End;

Var
  I: Integer;
  F, FD: String;
begin
  I:= Self.evraklar.Checked;
  
  F:= '';
  FD:= '';
  
  if I > 0 Then
  Begin
    E:= '';

	if I And  2 <>  2 Then 
	Begin
	  if I And  4 =  4 Then Ekle('0');
  	  if I And  8 =  8 Then Ekle('1');
	  if I And 16 = 16 Then Ekle('2');	  
	End;
	
	if E <> '' Then
	  F:= 'COALESCE(EVRAKTIPI, 0) IN(' + E + ') AND';
	F:= F +
        ' CAST(CONVERT(VARCHAR, TARIH, 104) AS DATETIME) >= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', Self.IlkTarih.Date)) + ' AS DATETIME) ' + 
        ' AND CAST(CONVERT(VARCHAR, TARIH, 104) AS DATETIME) <= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', Self.SonTarih.Date)) + ' AS DATETIME)';	
	
	
	FD:= FormatDateTime('dd.mm.yyy', Self.IlkTarih.Date - 1) + '&&';
	
	if E <> '' Then
	  FD:= FD + 'COALESCE(EVRAKTIPI, 0) IN(' + E + ') AND';
	  
	FD:= FD +
        ' CAST(CONVERT(VARCHAR, TARIH, 104) AS DATETIME) < CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', Self.IlkTarih.Date - 1)) + ' AS DATETIME) ';
	
	if (not Self.tkCheck.Checked) and (self.kasaCombo.Text <> '') Then
	Begin
	  F :=  F + ' AND K.ID = (SELECT ID FROM TBLKASALAR WHERE KASAADI = ' + QuotedStr(self.kasaCombo.Text) + ')';
	  FD:= FD + ' AND K.ID = (SELECT ID FROM TBLKASALAR WHERE KASAADI = ' + QuotedStr(self.kasaCombo.Text) + ')';
	 End;
	  
	if not ReadFilterOnly Then
	Begin
      CreateReport(FD + '####' + F, ReadSQL(FD + '####' + F), 
       'TARIH;KASAADI;KASAACIKLAMA:0;ISLEMADI;ACIKLAMA;GIREN;CIKAN;DURUM;EVRAKTIPI', 
	   'Tarih;Kasa;Kasa Açıklama;İşlem;Açıklama;Giren;Çıkan;Durum;Belge Tipi', 
	   0);  	    
	   
	  CreateSummary('GIREN', skSum); 
	  CreateSummary('CIKAN', skSum); 
	  CreateSummary('TARIH', skCount);	
	End
	Else
	Begin
	  SetReportData(FD + '####' + F, ReadSQL(FD + '####' + F));
	End;
	CalcLineTotal('CIKAN;GIREN;DURUM');
  End;
  Self.Close;
End;
  
procedure editFormShow(Sender: TObject);
begin
  AddCheckItems(TComponent(Self.evraklar), 'Tümü;Nakit;Çek;Senet;VİSA');
  Self.IlkTarih.Date:= Date;
  Self.SonTarih.Date:= Date;
  FillStringList('SELECT KASAADI FROM TBLKASALAR WHERE ISNULL(DELETED, 0) = 0', 'KASAADI', Self.kasaCOmbo.Properties.Items, True);
end;

// İptal
procedure cxButton2Click(Sender: TObject);
begin
  Self.Close;
end;

Begin
End.