Function ReadSQL(W: String): String;
Begin
  Result:= 'Set DateFormat DMY '#13#10 +
        '  '#13#10 +
        ' Declare @CariID Int, @Odeme Decimal(18, 4), @Borc Decimal(18, 4), @ID Int '#13#10 +
        '  '#13#10 +
        ' Declare @OzetTable TABLE(CARIID INT, VADE DATETIME, BORC DECIMAL(18, 4), ALACAK DECIMAL(18, 4),KALAN DECIMAL(18, 4), GUN INT) '#13#10 +
        ' INSERT INTO @OzetTable(CARIID, BORC, ALACAK) '#13#10 +
        ' Select  '#13#10 +
        '   CARIID, SUM(ROUND(ISNULL(BORC, 0), 2)), SUM(ROUND(ISNULL(ALACAK, 0), 2)) '#13#10 +
        ' FROM CARIDOKUM  '#13#10 +
        ' WHERE CARIID > 0   '#13#10 +
        ' GROUP BY CARIID '#13#10 +
        '  '#13#10 +
        ' Declare @OdemeTable TABLE(ID INT NOT NULL IDENTITY(1,1), CARIID INT, VADE DATETIME, BORC DECIMAL(18, 4), GUN INT) '#13#10 +
        ' INSERT INTO @OdemeTable(CARIID, VADE, BORC,GUN) '#13#10 +
        ' Select  '#13#10 +
        '   CARIID, VADE, ROUND(ISNULL(BORC, 0), 2),  '#13#10 +
        '   CAST(GETDATE() AS int) - CAST(VADE AS int) '#13#10 +
        ' FROM CARIDOKUM  '#13#10 +
        ' WHERE CARIID > 0   '#13#10 +
        ' ORDER BY CARIID,  VADE '#13#10 +
        '  '#13#10 +
        ' Declare OdemeCursor Cursor for '#13#10 +
        '   SELECT DISTINCT(CARIID) AS CARIID FROM @OdemeTable '#13#10 +
        '  '#13#10 +
        ' Open OdemeCursor '#13#10 +
        ' FETCH NEXT FROM OdemeCursor INTO @CariID '#13#10 +
        ' WHILE @@FETCH_STATUS = 0 '#13#10 +
        ' BEGIN '#13#10 +
        '   Set @Odeme = (Select SUM(ALACAK) From @OzetTable Where CARIID = @CariID) '#13#10 +
        '    '#13#10 +
        '   IF @Odeme > 0 '#13#10 +
        '   BEGIN '#13#10 +
        '     Declare AlacakCursor Cursor For  '#13#10 +
        ' 	  Select ID, BORC FROM @OdemeTable WHERE CARIID = @CariID '#13#10 +
        '  '#13#10 +
        '     Open AlacakCursor  '#13#10 +
        ' 	FETCH NEXT FROM AlacakCursor INTO @ID, @Borc '#13#10 +
        ' 	WHILE @@FETCH_STATUS = 0 AND @Odeme > 0 '#13#10 +
        ' 	BEGIN '#13#10 +
        ' 	  IF @Odeme > @Borc '#13#10 +
        ' 	  BEGIN '#13#10 +
        ' 	    UPDATE @OdemeTable Set BORC = 0 WHERE ID = @ID '#13#10 +
        ' 		Set @Odeme = @Odeme - @Borc '#13#10 +
        ' 	  END '#13#10 +
        ' 	  ELSE '#13#10 +
        ' 	  BEGIN '#13#10 +
        ' 	    UPDATE @OdemeTable Set BORC = BORC - @Odeme WHERE ID = @ID '#13#10 +
        ' 		Set @Odeme = 0 '#13#10 +
        ' 	  END '#13#10 +
        '  '#13#10 +
        ' 	  FETCH NEXT FROM AlacakCursor INTO @ID, @Borc '#13#10 +
        ' 	END '#13#10 +
        '  '#13#10 +
        ' 	Close AlacakCursor '#13#10 +
        ' 	DeAllocate AlacakCursor '#13#10 +
        '   END '#13#10 +
        '  '#13#10 +
        '   FETCH NEXT FROM OdemeCursor INTO @CariID '#13#10 +
        ' END '#13#10 +
        '  '#13#10 +
        ' Close OdemeCursor '#13#10 +
        ' DeAllocate OdemeCursor '#13#10 +
        '  '#13#10 +
        ' Delete From @OdemeTable Where BORC <= 0 '#13#10 +
        '  '#13#10 +
        ' UPDATE @OzetTable SET  '#13#10 +
        '   KALAN = (SELECT SUM(BORC) FROM @OdemeTable WHERE CARIID = [@OzetTable].CARIID), '#13#10 +
        '   GUN = (SELECT CAST((SUM(BORC * GUN) / SUM(BORC)) AS INT) FROM @OdemeTable WHERE BORC > 0 AND CARIID = [@OzetTable].CARIID) '#13#10 +
        '  '#13#10 +
        ' UPDATE @OzetTable SET VADE = CAST(GETDATE() - GUN AS DATE) '#13#10 +
        '  '#13#10 +
        ' DELETE FROM @OzetTable WHERE ISNULL(KALAN, 0) <= 0 '#13#10 +
        '  '#13#10 +
        ' Select  '#13#10 +
        '  C.FIRMAKODU, C.FIRMAADI, C.KOD1, C.KOD2, C.KOD3, C.KOD4, C.KOD5, C.BAKIYE, C.SEHIR, C.OPSIYONGUN, O.*  '#13#10 +
        ' from @OzetTable O '#13#10 +
        ' LEFT OUTER JOIN TBLCARI C WITH(INDEX=TBLCARI_KARTNO) ON C.KARTNO = O.CARIID ';
End;

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

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

procedure RaporLoad;
Begin
End;

procedure Start;
Begin
End;

Procedure Finish;
Begin
End;

Function ReadFilter(Var F: String): Boolean;
Begin
  Result:= True;
End;

procedure Execute;
Var 
  S: String;
Begin
  S:= ReadSQL('');
  
  CreateReport(S, S, 
     'FIRMAKODU;FIRMAADI;BAKIYE;SEHIR:0;KOD1;KOD2:0;KOD3:0;KOD4:0;KOD5:0;BORC;ALACAK;KALAN;VADE;GUN', 
	 'Firma Kodu;Firma Adı;Bakiye;Şehir;Ö.Kod 1;Ö.Kod 2;Ö.Kod 3;Ö.Kod 4;Ö.Kod 5;Borç;Alacak;Kalan;Vade;Gün', 
	  0);
	
  CreateSummary('FIRMAADI', skCount);	
  CreateSummary('ALACAK', skSum); 
  CreateSummary('BORC', skSum); 
  CreateSummary('KALAN', skSum); 
  CreateSummary('BAKIYE', skSum); 
	  
End;

Begin
End.