       IDENTIFICATION DIVISION.
       PROGRAM-ID.
           ONELEVEL. 

       ENVIRONMENT DIVISION.
           CONFIGURATION SECTION.
           SPECIAL-NAMES.
               C01 IS TO-TOP-OF-PAGE.
           INPUT-OUTPUT SECTION.
           FILE-CONTROL.
               SELECT INPUT-FILE ASSIGN TO UT-S-CARDS.
               SELECT REPORT-FILE ASSIGN TO UT-S-PRINTER. 

       DATA DIVISION.
           FILE SECTION. 

           FD INPUT-FILE
               LABEL RECORDS ARE OMITTED.
           01 INPUT-RECORD.
             05 ACCOUNT-NUMBER PIC X(5).
             05 ACCOUNT-DOLLARS PIC 9(5)V99.
             05 FILLER PIC X(68). 

           FD REPORT-FILE
               LABEL RECORDS ARE OMITTED.
           01 REPORT-RECORD PIC X(133).

       WORKING-STORAGE SECTION. 

          01 FLAGS.
            05 MORE-DATA-REMAINS-FLAG PIC XXX VALUE 'YES'
              88 MORE-DATA-REMAINS VALUE 'YES'
              88 NO-MORE-DATA-REMAINS VALUE 'NO'. 

          01 LINE-AND-PAGE-COUNTERS.
            05 LINE-NUMBER PIC $99 VALUE +1.
            05 PAGE-NUMBER PIC $999 VALUE +1.
 
          01 SAVE-ITEM.
            05 PREVIOUS-ACCOUNT-NUMBER PIC X(5). 

          01 TOTALS.
            05 ACCOUNT-TOTAL PIC $9(6)V99 VALUE ZERO.
            05 FINAL-TOTAL PIC $9(6)V99 VALUE ZERO. 

          01 DETAIL-LINE.
            05 CARRIAGE-CONTROL PIC X.
            05 ACCOUNT-NUMBER-OUT PIC ZZZZ9.
            05 FILLER PIC XXX.
            05 ACCOUNT-TOTAL-OUT PIC $$$$,$$9.99.
            05 FILLER PIC X(8).
            05 FINAL-TOTAL-OUT PIC $$$$,$$9.99. 

          01 HEADING-LINE.
            05 CARRIAGE-CONTROL PIC X.
            05 FILLER PIC X(48) VALUE 'ACCOUNT TOTAL FINAL TOTAL PAGE'.
            05 PAGE-NUMBER-OUT PIC Z(6)9.

       PROCEDURE DIVISION.
           PREPARE-SALES-REPORT.
           OPEN INPUT INPUT-FILE
                OUTPUT REPORT-FILE.
           READ INPUT-FILE
               AT END MOVE 'NO' TO MORE-DATA-REMAINS-FLAG.
           IF MORE-DATA-REMAINS
               MOVE ACCOUNT-NUMBER TO PREVIOUS-ACCOUNT-NUMBER
               PERFORM PROCESS-INPUT-RECORDS
                   UNTIL NO-MORE-DATA-REMAINS
               PERFORM FINAL-TOTAL-PROCESSING.
            CLOSE INPUT-FILE
                  REPORT-FILE.
            STOP RUN. 

       PROCESS-INPUT-RECORDS.
           IF ACCOUNT-NUMBER IS NOT EQUAL TO PREVIOUS-ACCOUNT-NUMBER
               PERFORM ACCOUNT-TOTAL-PROCESSING.
           ADD ACCOUNT-DOLLARS TO ACCOUNT-TOTAL
               FINAL-TOTAL.
           READ INPUT-FILE
               AT END MOVE 'NO' TO MORE-DATA-REMAINS-FLAG. 

       FINAL-TOTAL-PROCESSING.
           PERFORM ACCOUNT-TOTAL-PROCESSING.
           MOVE SPACES TO DETAIL-LINE.
           MOVE FINAL-TOTAL TO FINAL-TOTAL-OUT.
           PERFORM LINE-OUT. 

       ACCOUNT-TOTAL-PROCESSING.
           MOVE SPACES TO DETAIL-LINE.
           MOVE PREVIOUS-ACCOUNT-NUMBER TO ACCOUNT-NUMBER-OUT.
           MOVE ACCOUNT-TOTAL TO ACCOUNT-TOTAL-OUT.
           PERFORM LINE-OUT.
           MOVE ACCOUNT-NUMBER TO PREVIOUS-ACCOUNT-NUMBER.
           MOVE ZERO TO ACCOUNT-TOTAL. 

       LINE-OUT.
           IF LINE-NUMBER = 1
               MOVE PAGE-NUMBER TO PAGE-NUMBER-OUT
               WRITE REPORT-RECORD FROM HEADING-LINE
               AFTER ADVANCING TO-TOP-OF-PAGE
               MOVE SPACES TO REPORT-RECORD
               WRITE REPORT-RECORD AFTER ADVANCING 2 LINES
               MOVE 4 TO LINE-NUMBER
               ADD 1 TO PAGE-NUMBER.
           WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINES.
           IF LINE-NUMBER = 55
               MOVE 1 TO LINE-NUMBER
           ELSE
               ADD 1 TO LINE-NUMBER. 
