Function ReadSQL(W: String): String;
Begin
  Result:= 'Set DateFormat DMY '#13#10 + 
    ' SELECT  '#13#10 + 
    '   CAST(CONVERT(VARCHAR, TARIH, 104) AS DATE) AS TARIH, ISLEM, BELGENO, S.STOKKODU, S.CINSI, C.FIRMAKODU, C.FIRMAADI, PARCASAYISI, '#13#10 + 
    '   FIYATI, FIYATI * PARCASAYISI AS TUTARI '#13#10 + 
    ' FROM '#13#10 + 
    ' ( '#13#10 + 
    ' Select  '#13#10 + 
    '   H.TARIH,  '#13#10 + 
    '   CASE ISLEM  '#13#10 + 
    '     WHEN 237 THEN ''KESİMDEN GİRİŞ''  '#13#10 + 
    ' 	WHEN 238 THEN ''FASONA GİDİŞ''  '#13#10 + 
    ' 	WHEN 239 THEN ''FASONDAN GELİŞ''  '#13#10 + 
    ' 	WHEN 240 THEN ''ÜRETİMDEN GİRİŞ'' '#13#10 + 
    '   END AS ISLEM, '#13#10 + 
    '   H.BELGENO, UA.CIKISSTOK AS STOKNO, H.FASONCU, PARCASAYISI, PARCAUCRETI AS FIYATI '#13#10 + 
    ' From TBLSTOKURETIMISEMRIHAREKET H '#13#10 + 
    ' LEFT OUTER JOIN TBLSTOKURETIMISEMRI B ON B.ID = H.ISEMRINO '#13#10 + 
    ' LEFT OUTER JOIN TBLSTOKURUNAGACI UA ON UA.ID = B.URUNAGACIID '#13#10 + 
    ' UNION ALL '#13#10 + 
    ' Select  '#13#10 + 
    '   H.TARIH, ''ARIZALI GELEN'', B.BELGENO, UA.CIKISSTOK AS STOKNO, FASONCU,  PARCASAYISI, FIYATI '#13#10 + 
    ' from TBLSTOKURETIMISEMRIHAREKETARIZALIGELEN A '#13#10 + 
    ' LEFT OUTER JOIN TBLSTOKURETIMISEMRIHAREKET H ON A.ID = A.BASLIKID '#13#10 + 
    ' LEFT OUTER JOIN TBLSTOKURETIMISEMRI B ON B.ID = H.ISEMRINO '#13#10 + 
    ' LEFT OUTER JOIN TBLSTOKURUNAGACI UA ON UA.ID = B.URUNAGACIID '#13#10 + 
    ' UNION ALL '#13#10 + 
    ' SELECT  '#13#10 + 
    '   TARIH, ISLEM, BELGENO, STOKID, CARIID, CASE WHEN GIREN > 0 THEN GIREN ELSE CIKAN END, FIYATI '#13#10 + 
    ' from STOKHAREKET '#13#10 + 
    ' WHERE ISLEMTIPI NOT IN (145, 237, 237, 239, 240) '#13#10 + 
    ' ) AS A '#13#10 + 
    ' LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON S.KARTNO = A.STOKNO '#13#10 + 
    ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = A.FASONCU ';
	
  if W <> '' Then
    Result:= Result + ' WHERE STOKNO = ' + W;
  Result:= Result + ' ORDER BY TARIH';
End;

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

Function RaporFileName: String;
Begin
  Result:= ParsDir + 'Fason\UrunAgaciHareket.Fr3';
End;

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

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadFilter(Var F: String): Boolean;
Var
  IDS: String;
Begin
  IDS:= SelectRecord('SELECT KOD, ADI, CIKISSTOK FROM TBLSTOKURUNAGACI', 
     'KOD;ADI', 
	 'Ürün Kodu;Ürün Adı', 
	 'CIKISSTOK', 
	 'Kod / Adı', 
	 'KOD;ADI', 
	 False);
	 
  Result:= IDS <> '';
  if Result Then
    F:= IDS;    
End;

procedure Execute;
Var
  IDS: String;
Begin
  if ReadFilter(IDS) Then
  Begin
    CreateReport(IDS, ReadSQL(IDS), 
     'ISLEM;TARIH;BELGENO;STOKKODU:0;CINSI;FIRMAKODU:0;FIRMAADI;PARCASAYISI;FIYATI;TUTARI', 
	 'İşlem;Tarih;Belge No;Stok Kodu;Cinsi;Firma Kod;Firma Adı;Miktarı;Fiyatı;Tutarı', 
	  0);
	  
    CreateSummary('TUTARI', skSum); 
	CreateSummary('PARCASAYISI', skSum); 
  End;
End;

Begin
End.