Var
  cariID,
  depoID,
  personelID: Integer;

Function ReadSQL(W: String): String;
Begin
  Result:= 'Select '#13#10 +   
    '   ISLEM, FIRMAKODU, FIRMAADI, ISNULL(AD, '''') + '' '' + ISNULL(SOYAD, '''') AS PERSONEL, D.STOKKODU, D.CINSI,  ETKENMADDE AS GRUPADI, MIKTARI,'#13#10 +   
    '   D.KOD1, D.KOD2, D.KOD3, D.KOD4, D.KOD5, dbo.TRMONTHNAME(DATEPART(month, TARIH)) AS AYADI, '#13#10 +   
    '   DATEPART(year, TARIH) AS YIL, DATEPART(month, TARIH) AS AY,'#13#10 +   
    '   CASE WHEN ISNULL(BORC, 0) > 0 THEN BORC ELSE ALACAK END AS TUTAR'#13#10 +   
    ' from DETAYCARIDOKUM D'#13#10 +   
    ' LEFT OUTER JOIN TBLPERSONEL P ON P.KARTNO = D.PERSONELID'#13#10 + 
	' LEFT OUTER JOIN TBLSTOKLAR S ON S.STOKKODU = D.STOKKODU'#13#10 +   	
    ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = D.CARIID' + #13#10;

  if W <> '' Then
    Result:= Result + ' WHERE ' + W;
End;

procedure RaporLoad;
Begin
  SetViewCaption('BASLIK', 'PARADOX PERSONEL FAALİYET ANALİZİ');
// Filtre Kullanılırsa
  if FFilter <> '' Then
    AddQueryToSQL('KullaniciQ', ' WHERE ' + FFilter);
End;

Function RaporFileName: String;
Begin
  Result:= ''; // ParsDir + 'Fatura Raporları\Satış Faturaları\SatisFaturalariAnaliz.fr3';
End;

Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL('');
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.PERSONELID = ' + IntToStr(personelID) + ' AND ';
  
  F:= F + 
        ' D.KAYITTARIHI >= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy hh:nn', Self.IlkTarih.Date)) + ' AS DATETIME) ' + 
        ' AND  D.KAYITTARIHI <= CAST(' + QuotedStr(FormatDateTime('dd.mm.yyy hh:nn', Self.SonTarih.Date)) + ' AS DATETIME)';	
  
  if not ReadFilterOnly Then
  Begin
      CreateReport(F, ReadSQL(F), 
        'ISLEM;PERSONEL;FIRMAKODU:0;FIRMAADI;MIKTARI;TUTAR;KOD1;KOD2;KOD3;KOD4;KOD5;STOKKODU;CINSI;GRUPADI',
	    'İşlem;Personel;Firma Kodu;Firma Adı;Miktarı;Tutari;Kod1;Kod2;KOd3;KOd4;Kod5;Stok Kodu;Cinsi;Grup',
	     2);
	  
	AddDataField('TUTAR'); 
	AddRowField('PERSONEL');
	AddColumnField('ISLEM');  	
  End
  Else
  Begin
	SetReportData(F, ReadSQL(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:= Now;
  Self.SonTarih.Date:= Now;
  
  personelID:= -1;
  self.personelEdit.Properties.OnButtonClick:= @selectPersonel;
end;

Begin
End.
