Var
  personelID: Integer;

Function DefaultSQL(W: String): String;
Begin
  Result:= 'Select '#13 +
		'   P.AD + '' '' + P.SOYAD AS PERSONEL, C.FIRMAKODU, C.FIRMAADI,'#13 +
		'   BELGENO, ISLEM, TARIH, BORC, ALACAK'#13 +
		' from CARIDOKUM D'#13 +
		' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = D.CARIID'#13 +
		' LEFT OUTER JOIN TBLPERSONEL P WITH(INDEX=TBLPERSONEL_KARTNO) ON P.KARTNO = D.PERSONEL'#13 +
		' WHERE ISLEMTIPI > 0';
  if W <> '' Then
    Result:= Result + ' AND ' + W + #13;
  Result:= Result + ' ORDER BY TARIH'#13;
End;

procedure RaporLoad;
Begin
  if FFilter <> '' Then
    AddQueryToSQL('KullaniciQ', ' AND ' + FFilter);
  AddQueryToSQL('KullaniciQ', 'ORDER BY TARIH');
End;

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

Function RaporDefaultSQL: String;
Begin
  Result:= DefaultSQL('');
End;

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

// Tamam
procedure OnReport(Sender: TObject);
Var
  F: String;
begin
  F:= '';
  if personelID > 0 Then  
    F:= ' D.PERSONEL = ' + IntToStr(personelID) + ' AND ';
  
  F:= F + 
        ' CAST(CONVERT(VARCHAR, D.TARIH, 104) AS DATETIME) >= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', Self.IlkTarih.Date)) + ' AS DATETIME) ' + 
        ' AND CAST(CONVERT(VARCHAR, D.TARIH, 104) AS DATETIME) <= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy', Self.SonTarih.Date)) + ' AS DATETIME)';	
  
  if not ReadFilterOnly Then
  Begin
      CreateReport(F, DefaultSQL(F), 
        'PERSONEL;FIRMAKODU:0;FIRMAADI;BELGENO;ISLEM;TARIH;BORC;ALACAK',
	    'Personel;Firma Kodu;Firma Adı;Belge No;İşlem;Tarih;Borç;Alacak',
	     0);
	  
	  CreateSummary('BORC', skSum); 
	  CreateSummary('ALACAK', skSum);  
	  CreateSummary('PERSONEL', skCount);   	
  End
  Else
  Begin
	SetReportData(F, DefaultSQL(F));
  End;
  
  Self.Close;
end;

procedure selectPersonel(Sender: TObject);
Var
  IDS: String;
begin
  IDS:= SelectRecord(
     'SELECT KARTNO, AD + '' '' + SOYAD AS PERSONEL FROM TBLPERSONEL WHERE DELETED = 0 ORDER BY AD, SOYAD', 
     'PERSONEL', 
	 'Personel', 
	 'KARTNO', 
	 'Personel', 
	 'PERSONEL', 
	 False);
  if IDS <> '' Then
  Begin
    personelID:= strToInt(IDS);
    Self.personelEdit.Text:= GetDBValue('SELECT AD + '' '' + SOYAD FROM TBLPERSONEL WHERE KARTNO = ' + IDS);
  End;
End;

procedure editFormShow(Sender: TObject);
begin
  Self.IlkTarih.Date:= Date;
  Self.SonTarih.Date:= Date;
  
  personelID:= -1;
  self.personelEdit.Properties.OnButtonClick:= @selectPersonel;
end;

Begin
End.