  ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
  FILE-CONTROL.
      SELECT ORGANIZATION-LIST  ASSIGN TO ORGLIST.
      SELECT ORDERS-SUBMITTED   ASSIGN TO ORDFILE.
 *    SELECT COMPANY-ORDERS     ASSIGN TO ORDRPRT.
      SELECT ORDER-SUMMARY      ASSIGN TO ORDSUMM.

  FILE SECTION.
  FD  ORGANIZATION-LIST
      LABEL RECORDS STANDARD.
  01  ORGANIZATION-RECORD.
      05  ORG-CODE             PIC X(5).
      05  ORG-NAME             PIC X(30).

  FD  ORDERS-SUBMITTED
      LABEL RECORDS STANDARD.
  01  ORDER-RECORD.
      05  ORD-COMPANY          PIC X(10).
      05  ORD-ITEM-CODE        PIC X(10).
      05  ORD-ITEM-NAME        PIC X(30).
      05  ORD-ITEM-COUNT       PIC 9(5).
      05  ORD-ITEM-RATE        PIC 9(6)V99.
      05  ORD-ITEM-DATE        PIC X(8).
      05  ORD-RECIPIENT        PIC X(5).

 *FD  COMPANY-ORDERS.
  FD  ORDER-SUMMARY
       LABEL RECORDS STANDARD.
  01  ORDER-SUMMARY-LINE       PIC X(132).

  WORKING-STORAGE SECTION.
  01  INPUT-EOF-CONTROL           PIC XXX VALUE ‘NO ‘.
    88  END-OF-INPUT                      VALUE ‘YES’.
  01  ORGANIZATION-EOF-CONTROL    PIC XXX VALUE ‘NO ‘.
    88  END-OF-ORGANIZATIONS              VALUE ‘YES’.

  01  COMPUTER-DATE.
      05  THIS-YEAR               PIC 9999.
      05  THIS-MONTH              PIC 99.
      05  THIS-DAY                PIC XX.
      05  FILLER                  PIC X(13).

  01  MONTH-LIST.
      05  FILLER PIC X(30) VALUE ‘January   February  March’.
      05  FILLER PIC X(30) VALUE ‘April     May       June’.
      05  FILLER PIC X(30) VALUE ‘July      August    September’.
      05  FILLER PIC X(30) VALUE ‘October   November  December’.
  01  MONTH-TABLE REDEFINES MONTH-LIST.
      05  MONTH-NAME OCCURS 12 TIMES PIC X(10).

  01  ORG-LIMIT                   PIC 99      COMP VALUE 50.
  01  ORG-SUB                     PIC 99      COMP.
  01  TOTAL-COUNT                 PIC 9(5)    COMP VALUE ZERO.
  01  TOTAL-AMOUNT                PIC 9(8)V99 COMP VALUE ZERO.
  01  DIVISION-NAME-TABLE.
     05  DIVISION-ENTRY OCCURS 1 TO 50 TIMES
          DEPENDING ON ORG-LIMIT
          ASCENDING KEY DIVISION-CODE
          INDEXED BY DIV-IX.
          10  DIVISION-CODE      PIC X(5).
          10  DIVISION-NAME      PIC X(30).
  01  DIVISION-TOTALS-TABLE.
      05  DIVISION-TOTAL-ENTRY OCCURS 50 TIMES.
         10  DIVISION-COUNT      PIC 9(5)    COMP.
         10  DIVISION-AMOUNT     PIC 9(8)V99 COMP.

 *01  COMPANY-ORDER-DEFINITIONS.

  01  SUMMARY-TITLE.
      05  FILLER                  PIC X(20) VALUE SPACES.
      05  FILLER                  PIC X(38) VALUE
          ‘COMPANY ORDER SUMMARY BY DIVISION FOR ‘.
      05  SUM-TITLE-DATE          PIC X(16) VALUE SPACES.
  01  SUMMARY-HEADING.
      05  FILLER                  PIC X(50) VALUE
          ‘                    ORGANIZATION NAME’.
      05  FILLER                  PIC X(10) VALUE ‘ COUNT’.
      05  FILLER                  PIC X(13) VALUE ‘       AMOUNT’.
  01  SUMMARY-DETAIL.
      05  FILLER                  PIC X(20) VALUE SPACE.
      05  SUM-DTL-NAME            PIC X(30).
      05  FILLER                  PIC X(5)  VALUE SPACES.
      05  SUM-DTL-COUNT           PIC ZZ,ZZ9.
      05  FILLER                  PIC X(5)  VALUE SPACES.
      05  SUM-DTL-AMOUNT          PIC ZZ,ZZZ,ZZ9.99.

  PROCEDURE DIVISION.

  0100-MAIN-CONTROL.
      MOVE FUNCTION CURRENT-DATE TO COMPUTER-DATE.
      SUBTRACT 1 FROM THIS-MONTH.
      IF THIS-MONTH = ZERO
          MOVE 12 TO THIS-MONTH
          SUBTRACT 1 FROM THIS-YEAR.
      STRING MONTH-NAME (THIS-MONTH) DELIMITED BY SPACE
        ‘  ‘ DELIMITED BY SIZE
        THIS-YEAR DELIMITED BY SIZE
        INTO SUM-TITLE-DATE.

      OPEN INPUT ORGANIZATION-LIST.
      LIST

PERFORM 0600-READ-ORG
      PERFORM
          VARYING ORG-LIMIT FROM 1 BY 1
          UNTIL ORG-LIMIT > 49
             OR END-OF-ORGANIZATIONS
             MOVE ORG-CODE TO DIVISION-CODE (ORG-LIMIT)
MOVE ORG-NAME TO DIVISION-NAME (ORG-LIMIT)
PERFORM 0600-READ-ORG



      END-PERFORM.
      CLOSE ORGANIZATION-LIST.
      MOVE HIGH-VALUES TO DIVISION-CODE (ORG-LIMIT).
      MOVE ‘UNKNOWN ORGANIZATION’ TO DIVISION-NAME (ORG-LIMIT).

      INITIALIZE DIVISION-TOTALS-TABLE.

      OPEN INPUT ORDERS-SUBMITTED.
 *    OPEN OUTPUT COMPANY-ORDERS.
      PERFORM 0500-READ-ORDER.
      PERFORM 0200-MAIN-LOOP
          UNTIL END-OF-INPUT.
      CLOSE ORDERS-SUBMITTED.
 *    CLOSE COMPANY-ORDERS.

      OPEN OUTPUT ORDER-SUMMARY.
      WRITE ORDER-SUMMARY-LINE FROM SUMMARY-TITLE
          AFTER ADVANCING 1 LINE.
      WRITE ORDER-SUMMARY-LINE FROM SUMMARY-HEADING
          AFTER ADVANCING 2 LINES.
      MOVE SPACES TO ORDER-SUMMARY-LINE.
      WRITE ORDER-SUMMARY-LINE
          AFTER ADVANCING 2 LINES.
      PERFORM 0300-PRINT-SUMMARY
          VARYING ORG-SUB FROM 1 BY 1
          UNTIL ORG-SUB > ORG-LIMIT.
      MOVE ‘GRAND TOTAL’ TO  SUM-DTL-NAME.
      MOVE TOTAL-COUNT TO SUM-DTL-COUNT.
      MOVE  TOTAL-AMOUNT TO SUM-DTL-AMOUNT.
      WRITE ORDER-SUMMARY-LINE FROM SUMMARY-DETAIL
          AFTER ADVANCING 3 LINES.
      CLOSE ORDER-SUMMARY.
      STOP RUN.

  0200-MAIN-LOOP.
 *    PERFORM 0250-FORMAT-ORDER-LINE.
      SEARCH ALL DIVISION-ENTRY
         AT END
           MOVE ORG-LIMIT TO ORG-SUB
         WHEN DIVISION-CODE (DIV-IX) = ORD-RECIPIENT
           SET ORG-SUB TO DIV-IX.
      ADD 1 TO DIVISION-COUNT (ORG-SUB).
      COMPUTE DIVISION-AMOUNT (ORG-SUB)
          = DIVISION-AMOUNT (ORG-SUB)
          + (ORD-ITEM-COUNT * ORD-ITEM-RATE).
      PERFORM 0500-READ-ORDER.
 *0250-FORMAT-ORDER-LINE.

  0300-PRINT-SUMMARY.
      ADD DIVISION-COUNT (ORG-SUB) TO TOTAL-COUNT.
      ADD DIVISION-AMOUNT (ORG-SUB) TO TOTAL-AMOUNT.
      MOVE DIVISION-NAME (ORG-SUB) TO SUM-DTL-NAME.
      MOVE DIVISION-COUNT (ORG-SUB) TO SUM-DTL-COUNT.
      MOVE DIVISION-AMOUNT (ORG-SUB) TO SUM-DTL-AMOUNT.
      WRITE ORDER-SUMMARY-LINE FROM SUMMARY-DETAIL
          AFTER ADVANCING 2 LINES.

  0500-READ-ORDER.
      READ ORDERS-SUBMITTED
          AT END
          MOVE ‘YES’ TO INPUT-EOF-CONTROL.
  0600-READ-ORG. 
      READ ORGANIZATION-LIST
         AT END
         MOVE ‘YES’ TO ORGANIZATION-EOF-CONTROL.
