IDENTIFICATION DIVISION.
       PROGRAM-ID. LIST0002.
       AUTHOR.     JIRO SUZUKI.
      *------------------------------------------------------------
      * リスト出力プログラム(単純明細出力 
      * ただし、キー重複は2行目以降キー部分を非表示。
      * 改ページ後1行目は全て表示。今回のキーは日付と商品コード。)
      *------------------------------------------------------------
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
         SELECT  IN01  ASSIGN  TO  IN01NAME
                 ACCESS  MODE  IS  SEQUENTIAL.  
         SELECT  OUT1  ASSIGN  TO  OUT1NAME
                 ACCESS  MODE  IS  SEQUENTIAL.  
       DATA DIVISION.
       FILE SECTION.
       FD  IN01.
       COPY I-PRDCTDTL.
       FD  OUT1.
       01  O-REC         PIC X(132).
       WORKING-STORAGE SECTION.
       01 IN01NAME.
         03              PIC X(16) VALUE "/HOME/OWNER/DAT/".
         03 NAME         PIC X(12) VALUE "LIST0002IN01".
         03              PIC X(04) VALUE ".DAT".
       01 OUT1NAME.
         03              PIC X(16) VALUE "/HOME/OWNER/DAT/".
         03 NAME         PIC X(12) VALUE "LIST0002OUT1".
         03              PIC X(04) VALUE ".DAT".
       01 CNS-AREA.
         03 CNS-INIT-MSG. 
           05            PIC X(10) VALUE "----- PGM=".
           05            PIC X(08) VALUE "LIST0002".
           05            PIC X(11) VALUE "START -----".
         03 CNS-TERM-MSG. 
           05            PIC X(10) VALUE "----- PGM=".
           05            PIC X(08) VALUE "LIST0002".
           05            PIC X(11) VALUE "END   -----".
         03 CNS-LINE-MAX PIC 9(02) COMP-3 VALUE 10. 
         03 CNS-HD-1. 
           05 CNS-HD1-DATE   PIC X(10).
           05            PIC X(17)  VALUE SPACE.
           05            PIC N(05)  VALUE "売上明細表".
           05            PIC X(20)  VALUE SPACE.
           05            PIC X(05)  VALUE "PAGE:".
           05 CNS-HD-1-PAGENO  PIC  Z,ZZ9.
           05            PIC X(75)  VALUE SPACE.
         03 CNS-HD-2. 
           05            PIC N(05)  VALUE "売上年月日".
           05            PIC X(05)  VALUE SPACE.
           05            PIC N(05)  VALUE "商品コード".
           05            PIC X(08)  VALUE SPACE.
           05            PIC N(02)  VALUE "単価".
           05            PIC X(08)  VALUE SPACE.
           05            PIC N(02)  VALUE "数量".
           05            PIC X(14)  VALUE SPACE.
           05            PIC N(02)  VALUE "金額".
           05            PIC X(59) VALUE SPACE.
         03 CNS-SLASH    PIC X(01) VALUE "/".
         03 CNS-GOKEI    PIC N(02) VALUE "合計".
         03 CNS-1        PIC 9(01) COMP-3 VALUE 1.
         03 CNS-8        PIC 9(01) COMP-3 VALUE 8.
       01 CNT-AREA.
         03 CNT-IN01     PIC 9(11) COMP-3 VALUE ZERO.
         03 CNT-PAGE     PIC 9(11) COMP-3 VALUE ZERO.
         03 CNT-LINE     PIC 9(11) COMP-3 VALUE 10.
       01 SW-AREA.
         03 SW-END-IN01  PIC 9(01) COMP-3 VALUE ZERO.
       01 WORK-AREA.
         03 W-DTL.
           05 W-DTL-PCHASEDATE.
             07 W-DTL-PCHASEYY PIC X(04).
             07 W-DTL-PCHASES1 PIC X(01).
             07 W-DTL-PCHASEMM PIC X(02).
             07 W-DTL-PCHASES2 PIC X(01).
             07 W-DTL-PCHASEDD PIC X(02).
           05                  PIC X(06).
           05 W-DTL-PRDCTCD    PIC X(08).
           05                  PIC X(02).
           05 W-DTL-PRICE      PIC ZZZ,ZZZ,ZZ9.
           05                  PIC X(01).
           05 W-DTL-QUANTITY   PIC ZZZ,ZZZ,ZZ9.
           05                  PIC X(01).
           05 W-DTL-AMOUNT     PIC Z,ZZZ,ZZZ,ZZZ,ZZ9.
           05                  PIC X(65).
         03 W-TTL.
           05                  PIC X(45).
           05 W-TTL-GOKEI      PIC N(02).
           05                  PIC X(01).
           05 W-TTL-AMOUNT     PIC Z,ZZZ,ZZZ,ZZZ,ZZ9.
           05                  PIC X(65).
         03 W-KEY-OLD.
           05 W-KEY-OLD-PCHASEDATE  PIC X(08).
           05 W-KEY-OLD-PRDCTCD     PIC X(08).
         03 W-KEY-NEW.
           05 W-KEY-NEW-PCHASEDATE  PIC X(08).
           05 W-KEY-NEW-PRDCTCD     PIC X(08).
         03 W-Z9               PIC Z9.
         03 W-AMOUNT           PIC 9(13) COMP-3.
         03 W-DATE             PIC X(06).
         03 W-DATE-EDIT.
            05 W-DATE-YY       PIC X(02).
            05 W-DATE-S1       PIC X(01).
            05 W-DATE-MM       PIC X(02).
            05 W-DATE-S2       PIC X(01).
            05 W-DATE-DD       PIC X(02).
         03 W-SUM-AMOUNT       PIC 9(13) COMP-3.
      *------------------------------------------------------------
       PROCEDURE DIVISION.
       MAIN SECTION.            *>全体処理
         PERFORM INIT-PROC THRU INIT-EXIT.
         PERFORM MAIN-PROC THRU MAIN-EXIT.
         PERFORM TERM-PROC THRU TERM-EXIT.
         STOP RUN.
      *------------------------------------------------------------
       INIT-PROC.               *>初期処理
         DISPLAY CNS-INIT-MSG.
      *
         OPEN INPUT  IN01
              OUTPUT OUT1.
      *
         INITIALIZE    WORK-AREA.
         MOVE LOW-VALUE TO W-KEY-OLD W-KEY-NEW.
      *
         ACCEPT W-DATE FROM DATE.
         MOVE W-DATE(1:2) TO W-DATE-YY.
         MOVE W-DATE(3:2) TO W-Z9.
         MOVE W-Z9        TO W-DATE-MM.
         MOVE W-DATE(5:2) TO W-Z9.
         MOVE W-Z9        TO W-DATE-DD.
         MOVE CNS-SLASH   TO W-DATE-S1 W-DATE-S2.
         MOVE W-DATE-EDIT TO CNS-HD1-DATE.
      *
       INIT-EXIT.
      *------------------------------------------------------------
       MAIN-PROC.               *>主処理
         PERFORM READ-IN01.
         PERFORM UNTIL SW-END-IN01=1
           COMPUTE CNT-LINE = CNT-LINE + 1
           IF CNT-LINE > CNS-LINE-MAX
             PERFORM WRITE-HEADER
           END-IF
           PERFORM EDIT-DTL
           PERFORM WRITE-DTL
           PERFORM READ-IN01
         END-PERFORM.
       MAIN-EXIT.
      *------------------------------------------------------------
       TERM-PROC.               *>終了処理
         PERFORM WRITE-TTL.
         CLOSE IN01
               OUT1.
         DISPLAY "IN01=" CNT-IN01.
         DISPLAY "PAGE=" CNT-PAGE.
         DISPLAY CNS-TERM-MSG.
       TERM-EXIT.
      *------------------------------------------------------------
       READ-IN01.               *>IN01読込処理
         MOVE W-KEY-NEW TO W-KEY-OLD.
         READ IN01 INTO I-PRDCTDTL
           AT END
             MOVE 1 TO SW-END-IN01
           NOT AT END
             MOVE I-PCHASEDATE TO W-KEY-NEW-PCHASEDATE
             MOVE I-PRDCTCD    TO W-KEY-NEW-PRDCTCD
             ADD 1 TO CNT-IN01
         END-READ.
      *------------------------------------------------------------
       EDIT-DTL.                *>明細編集処理
         INITIALIZE    W-DTL.
      *
         EVALUATE TRUE 
      *    改ページ後一行目はすべて表示
           WHEN  CNT-LINE = 1
      *    日付が変わったらすべて表示
           WHEN  W-KEY-NEW-PCHASEDATE NOT = W-KEY-OLD-PCHASEDATE
             PERFORM EDIT-DTL-SUB1  
      *    商品コードだけが変わったら商品コードは表示
           WHEN  W-KEY-NEW-PCHASEDATE  = W-KEY-OLD-PCHASEDATE  AND
                 W-KEY-NEW-PRDCTCD NOT = W-KEY-OLD-PRDCTCD
             PERFORM EDIT-DTL-SUB2
      *    キーが変わっていないときはキー項目は非表示
           WHEN OTHER
             PERFORM EDIT-DTL-SUB3
         END-EVALUATE.
      *
         MOVE I-PRICE      TO W-DTL-PRICE.
         MOVE I-QUANTITY   TO W-DTL-QUANTITY.
         COMPUTE W-AMOUNT = I-PRICE * I-QUANTITY.
         MOVE W-AMOUNT     TO W-DTL-AMOUNT.
      *
         ADD W-AMOUNT TO W-SUM-AMOUNT.
      *------------------------------------------------------------
       EDIT-DTL-SUB1.               *>明細編集サブ処理1
         MOVE I-PCHASEDATE (1:4) TO W-DTL-PCHASEYY.
         MOVE I-PCHASEDATE (5:2) TO W-Z9.
         MOVE W-Z9         TO W-DTL-PCHASEMM.
         MOVE I-PCHASEDATE (7:2) TO W-Z9.
         MOVE W-Z9         TO W-DTL-PCHASEDD.
         MOVE CNS-SLASH    TO W-DTL-PCHASES1 W-DTL-PCHASES2.
         MOVE I-PRDCTCD    TO W-DTL-PRDCTCD.
      *------------------------------------------------------------
       EDIT-DTL-SUB2.               *>明細編集サブ処理2
         MOVE SPACE        TO W-DTL-PCHASEDATE.
         MOVE I-PRDCTCD    TO W-DTL-PRDCTCD.
      *------------------------------------------------------------
       EDIT-DTL-SUB3.               *>明細編集サブ処理3
         MOVE SPACE TO W-DTL-PCHASEDATE W-DTL-PRDCTCD.
      *------------------------------------------------------------
       WRITE-DTL.               *>明細出力処理
         MOVE W-DTL TO O-REC.
         WRITE O-REC AFTER 1. 
      *------------------------------------------------------------
       WRITE-HEADER.            *>見出し出力処理
         COMPUTE CNT-PAGE = CNT-PAGE + 1.
      *
         MOVE CNT-PAGE TO CNS-HD-1-PAGENO.
         MOVE CNS-HD-1 TO O-REC.
         IF CNT-PAGE = CNS-1
           WRITE O-REC
         ELSE
           WRITE O-REC AFTER PAGE
         END-IF.
      *
         MOVE 1 TO CNT-LINE. 
      *
         MOVE CNS-HD-2 TO O-REC.
         WRITE O-REC AFTER 2. 
      *------------------------------------------------------------
       WRITE-TTL.               *>合計出力処理
      *
         INITIALIZE    W-TTL.
      *
         IF CNT-IN01 NOT = 0
      *
           IF CNT-LINE > CNS-8
             PERFORM WRITE-HEADER
           END-IF
      *
           MOVE CNS-GOKEI    TO W-TTL-GOKEI
           MOVE W-SUM-AMOUNT TO W-TTL-AMOUNT
           MOVE W-TTL        TO O-REC
           WRITE O-REC AFTER 2 
      *
         END-IF.