Function ReadSQL(W: String): String;
Begin
  Result:= 
        ' Declare @SN VarChar(60) '#13 +
        '  '#13 +
        ' Set @SN = ' + QuotedStr(W) + #13 +
        '  '#13 +
        ' SELECT  '#13 +
        '   A.SERINO, S.STOKKODU, S.CINSI, A.ISLEM, A.FIRMAKODU, A.FIRMAADI, A.BELGENO, A.TARIH '#13 +
        ' FROM ( '#13 +
        ' Select  '#13 +
        '   ''SATIŞ FATURASI'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLSATFATBASLIK B WITH(INDEX=TBLSATFATBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 12 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''ALIŞ FATURASI'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLALFATBASLIK B WITH(INDEX=TBLALFATBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 13 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''SATIŞ İADE FATURASI'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLSATFATIADEBASLIK B WITH(INDEX=TBLSATFATIADEBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 14 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''ALIŞ İADE FATURASI'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLALFATIADEBASLIK B WITH(INDEX=TBLALFATIADEBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 15 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''SATIŞ İRSALİYESİ'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLSATIRSBASLIK B WITH(INDEX=TBLSATIRSBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 72 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''ALIŞ İRSALİYESİ'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLALIRSBASLIK B WITH(INDEX=TBLALIRSBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 73 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''SATIŞ İADE İRSALİYESİ'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLSATIRSIADEBASLIK B WITH(INDEX=TBLSATIRSIADEBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 112 AND SN.SERINO = @SN '#13 +
        ' UNION ALL '#13 +
        ' Select  '#13 +
        '   ''ALIŞ İADE İRSALİYESİ'' AS ISLEM, STOKID, SERINO, C.FIRMAKODU, C.FIRMAADI, B.BELGENO,B.TARIH, B.KAYITTARIHI '#13 +
        ' from TBLSERINO SN WITH(INDEX=TBLSERINO_SERINO) '#13 +
        ' LEFT OUTER JOIN TBLALIRSIADEBASLIK B WITH(INDEX=TBLALIRSIADEBASLIK_KARTNO) ON B.KARTNO = SN.KARTNO '#13 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID '#13 +
        ' WHERE SN.MODULID = 111 AND SN.SERINO = @SN '#13 +
        ' ) AS A '#13 +
        ' LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON S.KARTNO = A.STOKID '#13 +
        ' ORDER BY TARIH, A.KAYITTARIHI ';
		
End;

procedure RaporLoad;
Begin
End;

Function RaporFileName: String;
Begin
  Result:= '';
End;

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

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadFilter(Var F: String): Boolean;
Var
  SN: String;
Begin
  Result:= InputText('Seri Nosu', 'Seri No Girin', 'Sorgulamak İçin Seri No Girin', 'Seri No', SN);
  if Result Then
    F:= SN;
End;

procedure Execute;
Var
  SN: String;
Begin
  if ReadFilter(SN) Then
  Begin
    CreateReport(SN, ReadSQL(SN), 
     'SERINO;STOKKODU;CINSI;ISLEM;FIRMAKODU;FIRMAADI;BELGENO;TARIH', 
	 'Seri No;Stok Kodu;Cinsi;İşlem;Firma Kodu;Firma Adı;Belge No;Tarih', 
	  0);
	  
	CreateSummary('ISLEM', skCount); 
  End;
End;

Begin
End.