Var
  faturaID, sWhere: String;

procedure FindParam;
Var 
  I: Integer;
  W: String;
Begin
  W:= FFilter;
  faturaID:= W;	
  
  sWhere:= '';
  
  if faturaID <> '' Then
    sWhere:= ' WHERE B.KARTNO IN(' + faturaID + ')';
End;

Function ReadSQL1: 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 ReadSQL: String;
Begin
  findParam;
  
  Result:= 'Select ' + 
     '   B.KARTNO, B.TARIH, B.BELGENO, C.FIRMAADI, B.TUTARI ' + 
	 ' from TBLSATFATBASLIK B ' + 
	 ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = B.CARIID ' + 
	 sWhere;
End;

Function RaporDefaultSQL: String;
Begin
  Result:= ReadSQL;
End;

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

procedure RaporLoad;
Begin
  findParam;
  
  setQuerySQL('baslikQ', 'Select '#13 + 
       '   B.*, P.AD + '' ''+ P.SOYAD AS PERSONEL, D.ACIKLAMA AS DEPO '#13 +
       ' from TBLSATFATBASLIK B '#13 +
       ' LEFT OUTER JOIN TBLPERSONEL P ON B.PERSONELID = P.KARTNO '#13 +
       ' LEFT OUTER JOIN TBLDEPOLAR D ON ISNULL(D.DELETED, 0) = 0 AND B.DEPOID = D.KARTNO '#13 +
	   ' WHERE B.KARTNO IN (' + faturaID + ')');

//   setQuerySQL('detayQ', ReadSQL);
  
//   setQuerySQL('cariQ', 'Select * from TBLCARI WHERE KARTNO = ' + cariID);
{	   
  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 ReadFaturaFilter(Var F: String): Boolean;
Var
  IDS: String;
Begin
  IDS:= SelectRecord('Select F.KARTNO, TARIH, BELGENO, FIRMAADI,TUTARI from TBLSATFATBASLIK F ' + 
      ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = F.CARIID WHERE ISNULL(F.DELETED, 0) = 0', 
     'TARIH;BELGENO;FIRMAADI;TUTARI', 
	 'Tarih;Belge No;Firma;Tutarı', 
	 'KARTNO', 
	 'Tarih / Belge No', 
	 'TARIH;BELGENO', 
	 True);

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

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

procedure Execute;
Var
  ID: String;
Begin
  if ReadFilter(ID) Then
  Begin
    FFilter:= ID;	
    CreateReport(ID, ReadSQL, 
     'TARIH;BELGENO;FIRMAADI;TUTARI', 
	 'Tatih;Belge No;Firma Adı;Tutarı', 
	  0);
    CreateSummary('TUTARI', skSum); 
	CreateSummary('TARIH', skCount); 
  End;
End;

Begin
End.