Function KDVSQL(ay: String): String;
Begin
  Result:= 'Select ' + 
    '   ''SATIŞ KDV'' AS TIP, H.KDV AS KDVDILIMI, 0.000 AS KDVALACAK, SUM(ROUND(H.KDVTUTARI, 2)) AS KDVBORC ' +
	' from TBLSATFATHAREKET H ' + 
	' LEFT OUTER JOIN TBLSATFATBASLIK B WITH(INDEX=TBLSATFATBASLIK_KARTNO) ON B.KARTNO = H.FATURAID ' + 
	' WHERE ISNULL(B.DELETED, 0) = 0 AND H.KDV > 0 AND B.YIL = DATEPART(Year, GETDATE()) and AY = ' + ay + 
	' GROUP BY H.KDV ' + 
	' UNION ALL ' + 
	' Select  ' + 
	'   ''ALIŞ KDV'' AS TIP, H.KDV AS KDVDILIMI, SUM(H.KDVTUTARI) AS KDVTUTARI, 0.00 ' + 
	' from TBLALFATHAREKET H ' + 
	' LEFT OUTER JOIN TBLALFATBASLIK B WITH(INDEX=TBLALFATBASLIK_KARTNO) ON B.KARTNO = H.FATURAID ' + 
	' WHERE ISNULL(B.DELETED, 0) = 0 AND H.KDV > 0 AND B.YIL = DATEPART(Year, GETDATE()) and AY = ' + ay + 
	' GROUP BY H.KDV ';
End;	

procedure RaporLoad;
Begin
  if FFilter <> '' Then
  Begin
    SetQueryParam('kullaniciQ', 'AY', StrToInt(FFilter));
  End;
End;

Function RaporFileName: String;
Begin
  Result:= parsDir + 'Fatura Raporları\KDV RAPORU.fr3';
End;

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

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadFilter(Var F: String): Boolean;
Var 
  I: Integer;
Begin
  F:= '';
  Result:= InputRadio('Ocak;Şubat;Mart;Nisan;Mayıs;Haziran;Temmuz;Ağustos;Eylül;Ekim;Kasım;Aralık', 
     'Aylık Bazda KDV Raporu',
	 'KDV Raporu Alınacak Ayı Seçin',
	 'Ay', I);
  if Result Then
    F:= IntToStr(I + 1);  
End;

Function ReadSQL(W: String): String;
Begin
  Result:= KDVSQL(W);
End;

procedure Execute;
Var
  ay: String;
Begin
  if ReadFilter(ay) Then
  CreateReport(ay, KDVSQL(ay), 
     'TIP;KDVDILIMI;KDVALACAK;KDVBORC', 
	 'KDV Tipi;KDV Dilimi;Alacak KDV; Borç KDV', 
	  0);
	
	CreateSummary('KDVALACAK', skSum); 
	CreateSummary('KDVBORC', skSum); 
End;

Begin
End.