Var
  CariID, faturaID, sWhere: String;

procedure FindParam;
Var 
  I: Integer;
  W: String;
Begin
  CariID  := '';
  faturaID:= '';
  
  W:= FFilter;
  I:= Pos('#', W);
  if I > 0 Then
  Begin
    cariID:= Copy(W, 1, I - 1);
	Delete(W, 1, I);
	faturaID:= W;	
  End;
  
  if cariID <> '' Then
    sWhere:= sWhere + ' FB.CARIID = ' + cariID;
	
  if faturaID <> '' Then
  Begin
    if sWhere <> '' Then
	  sWhere:= sWhere + ' AND ';
    sWhere:= sWhere + ' H.FATURAID IN(' + faturaID + ')';
  End;
	
  if sWhere <> '' Then
    sWhere:= ' WHERE ' + sWhere;
End;

Function ReadSQL: String;
Begin
  findParam;
  
  Result:= 'Select '#13 +
		'   S.STOKKODU, S.CINSI, B.BIRIMADI, B.BARKOD, H.KDV, ISKONTO1, ISKONTO2, ISKONTO3, ISKONTO4, FIYATI,'#13 + 
		'   SUM(MIKTARI) AS MIKTARI, SUM(KDVTUTARI) AS KDVTUTARI'#13 +
		' From TBLSATFATHAREKET H WITH(INDEX=TBLSATFATHAREKET_FATURAID) '#13 +
		' LEFT OUTER JOIN TBLSATFATBASLIK FB WITH(INDEX=TBLSATFATBASLIK_KARTNO) ON FB.KARTNO = H.FATURAID '#13 +
		' LEFT OUTER JOIN TBLSTOKLAR S WITH(INDEX=TBLSTOKLAR_KARTNO) ON H.STOKID = S.KARTNO '#13 +
		' LEFT OUTER JOIN TBLBIRIMLEREX B WITH(INDEX=TBLBIRIMLEREX_KARTNO) ON H.BIRIMID = B.KARTNO'#13 +
		sWhere + #13 +
        ' GROUP BY S.STOKKODU, S.CINSI, B.BIRIMADI,B.BARKOD, H.KDV, ISKONTO1, ISKONTO2, ISKONTO3, ISKONTO4, FIYATI';
End;
 
Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL;
End;

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

procedure RaporLoad;
Begin
   setQuerySQL('detayQ', ReadSQL);
  
  setQuerySQL('cariQ', 'Select * from TBLCARI WHERE KARTNO = ' + cariID);
  setQuerySQL('baslikQ', 'Select '#13 + 
       '   GETDATE() AS TARIH, SUM(TUTARI) AS TUTARI, SUM(ALTISKONTO1) AS ALTISKONTO1, SUM(ALTISKONTO2) AS ALTISKONTO2, SUM(SATIRISKONTOSU) AS SATIRISKONTOSU, '#13 +
	   '   SUM(KDV) AS KDV'#13 +
	   ' from TBLSATFATBASLIK '#13 +
	   ' WHERE KARTNO IN (' + faturaID + ')');
	   
  setQuerySQL('KDVQ', 'SELECT '#13 +  
	   '   KDV, ISNULL(SUM(MIKTARI * FIYATI - ISKONTOTOPLAMI), 0) AS KDVMATRAHI, '#13 +  
       '   ISNULL(SUM(KDVTUTARI), 0) AS KDVTUTARI '#13 +   
	   ' FROM TBLSATFATHAREKET  '#13 + 
	   ' WHERE FATURAID IN(' + faturaID + ') '#13 + 
       ' GROUP BY KDV ');
End;

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadCariFilter(Var F: String): Boolean;
Var
  IDS: String;
Begin
  IDS:= SelectRecord('SELECT KARTNO, FIRMAKODU, FIRMAADI FROM TBLCARI WHERE ISNULL(DELETED, 0) = 0', 
     'FIRMAKODU;FIRMAADI', 
	 'Firma Kodu;Firma Adı', 
	 'KARTNO', 
	 'Firma Kodu / Adı', 
	 'FIRMAKODU;FIRMAADI', 
	 False);

  Result:= IDS <> '';
  if Result Then
	  F:= IDS;
End;

Function ReadFaturaFilter(cariID: String; Var F: String): Boolean;
Var
  IDS: String;
Begin
  IDS:= SelectRecord('Select KARTNO, TARIH, BELGENO, TUTARI from TBLSATFATBASLIK WHERE ISNULL(DELETED, 0) = 0 AND CARIID = ' + cariID, 
     'TARIH;BELGENO;TUTARI', 
	 'Tarih;Belge No;Tutarı', 
	 'KARTNO', 
	 'Tarih / Belge No', 
	 'TARIH;BELGENO', 
	 True);

  Result:= IDS <> '';
  if Result Then
	  F:= IDS;
End;

Function ReadFilter(Var F: String): Boolean;
Var
  cariID, FID: String;
Begin
  Result:= False;
  if readCariFilter(cariID) Then
  Begin
    if ReadFaturaFilter(cariID, FID) Then
	Begin
	  Result:= FID <> '';
	  if Result Then
	    F:= cariID + '#' + FID;
	End;
  End;
End;

procedure Execute;
Var
  ID: String;
Begin
  if ReadFilter(ID) Then
  Begin
    FFilter:= ID;
    CreateReport(ID, ReadSQL, 
     'STOKKODU:0;CINSI;MIKTARI;BIRIMADI;FIYATI;KDVTUTARI;BARKOD:0;KDV:0;ISKONTO1:0;ISKONTO2:0;ISKONTO3:0;ISKONTO4:0', 
	 'Stok Kodu;Cinsi;Miktarı;Birimi;Fiyatı;KDV Tutarı;Barkod;KDV %;İskonto 1;İskonto 2;İskonto 3;İskonto 4', 
	  0);		
  End;
End;

Begin
End.