000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. SalesRptRB.
000300 
000400* Read in a sales file with SALES-ID, PRODUCT-ID & QUANITY-SOLD.
000500* Program will access a subprogram, LOOKUP which will provide
000600* the Sales Person Name and the Product Name and Price.
000700* 
000800* The program will use the information from the file and the
000900* subprogram to create a sales report with the SALES-ID, Sale
001000* Person Name, Product ID, Product Name, Product Price, Quanity, 
001100* and Transaction SubTotal for each record.
001200* The program will create a total sales detail line at the
001300* conclusion of the report. 
001400  
001500* IF A PRODUCT CODE OR SALES ID IS NOT FOUND IN THE LOOKUP
001600* PROGRAM, THE PROGRAM SHOULD REPORT "NOT FOUND" FOR THE 
001700* NAMES AND ZEROS FOR THE PRICE.
001800  
001900 ENVIRONMENT DIVISION.
002000 CONFIGURATION SECTION.
002100 INPUT-OUTPUT SECTION.
002200 FILE-CONTROL.
002300     SELECT SALES-FILE
002400*       ASSIGN TO 'C:\Users\Call1\SALES23I.dat'
002500        ASSIGN TO 'C:\Users\Ryan\Downloads\SALES23I.dat'
002600        ORGANIZATION IS LINE SEQUENTIAL
002700        ACCESS IS SEQUENTIAL
002800        FILE STATUS IS WS-SALES23I-FILE-STATUS.
002900     SELECT SALES-RPT-FILE
003000*      ASSIGN TO "C:\Users\Call1\SALES23R.rpt"
003100       ASSIGN TO "C:\Users\Ryan\Downloads\SALES23R.rpt"
003200       ORGANIZATION IS LINE SEQUENTIAL
003300       FILE STATUS IS WS-SALES23R-FILE-STATUS.
003400         
003500 DATA DIVISION.
003600 FILE SECTION.
003700 FD  SALES-FILE                                    
003800     RECORDING MODE IS F  
003900     DATA RECORD IS SALES-RECORD. 
004000 01  SALES-RECORD               PIC X(12).
004100 
004200 FD  SALES-RPT-FILE                                    
004300     RECORDING MODE IS F  
004400     DATA RECORD IS SALES-RPT-RECORD. 
004500 01  SALES-RPT-RECORD          PIC X(80).
004600
004700 WORKING-STORAGE SECTION.
004800 01  WS-FILLER                 PIC X(80)
004900     VALUE 'START WS SECTION'.   
005000 
005100 01 SALES-RECORD-LAYOUT.
005200    05 SALES-ID                PIC X(03).
005300    05 PRODUCT-ID              PIC X(05).
005400    05 QUANITY-SOLD            PIC 9(04).
005500    
005600 01 WS-STATUS.
005700    05 WS-SALES23I-FILE-STATUS PIC X(02) VALUE SPACES.
005800       88 NO-DATA                        VALUE '01' THRU '99'.
005900    05 WS-SALES23R-FILE-STATUS PIC X(02) VALUE SPACES.
006000       88 GOOD-FILE-WRITE          VALUE "00".
006100 
006200 01 WS-RECORD-COUNT-IN         pic 9(03) VALUE ZEROES.
006300  
006400 01 RPT-LINE-01.
006500    05 FILLER                 PIC X(34) VALUE SPACES.
006600    05 FILLER                 PIC X(12) VALUE "SALES REPORT".
006700 01 RPT-LINE-02.
006800    05 FILLER                  PIC X(32) VALUE SPACES.
006900    05 FILLER                  PIC X(14) VALUE "BY RYAN BROOKS".
007000 01 RPT-LINE-03.
007100    05 FILLER                  PIC X(32) VALUE SPACES.
007200    05 FILLER                  PIC X(06) VALUE 'AS OF '.
007300    05 FILLER. 
007400       10 MM                   PIC X(02) VALUE SPACES.
007500       10 FILLER               PIC X(01) VALUE '/'.
007600       10 DD                   PIC X(02) VALUE SPACES.
007700       10 FILLER               PIC X(01) VALUE '/'.
007800       10 CC                   PIC X(02) VALUE '20'.
007900       10 YY                   PIC X(02) VALUE SPACES.
008000 01 RPT-LINE-04.
008100    05 FILLER                  PIC X(34) VALUE SPACES.
008200    05 FILLER                  PIC X(03) VALUE 'AT '.
008300    05 FILLER. 
008400       10 TIME-HH              PIC X(02) VALUE SPACES.
008500       10 FILLER               PIC X(01) VALUE ':'.
008600       10 TIME-MM              PIC X(02) VALUE SPACES.
008700       10 FILLER               PIC X(01) VALUE ':'.
008800       10 TIME-SS              PIC X(04) VALUE SPACES.        
008900       
009000 01 RPT-COLUMN-HDG.
009100    05 FILLER                  PIC X(12) VALUE " SALES ID".
009200    05 FILLER                  PIC X(15) VALUE "SALES NAME".
009300    05 FILLER                  PIC X(11) VALUE "PRODUCT#".
009400    05 FILLER                  PIC X(11) VALUE "PRODUCT".
009500    05 FILLER                  PIC X(11) VALUE "UNITS".
009600    05 FILLER                  PIC X(10) VALUE "PRICE".
009700    05 FILLER                  PIC X(11) VALUE "LINE TOTAL".
009800*   05 FILLER    
009900    
010000 01 WS-OUTPUT-DETAIL.
010100    05 FILLER                  PIC X(03) VALUE SPACES.
010200    05 SALES-ID-OUT            PIC 9(03) VALUE ZERO.
010300    05 FILLER                  PIC X(06) VALUE SPACES.
010400    05 SALES-NAME-OUT          PIC X(15).
010500    05 FILLER                  PIC X(01) VALUE SPACES.
010600    05 PRODUCT-CODE-OUT        PIC 9(05) VALUE ZERO.
010700    05 FILLER                  PIC X(05) VALUE SPACES.
010800    05 PRODUCT-NAME-OUT        PIC X(10).
010900    05 FILLER                  PIC X(02) VALUE SPACES.
011000    05 PRODUCT-QUANITY-OUT     PIC ZZ9 VALUE ZERO.
011100    05 FILLER                  PIC X(04) VALUE SPACES.
011200    05 PRODUCT-PRICE-OUT       PIC $Z,ZZ9.99.
011300    05 FILLER                  PIC X(03) VALUE SPACES.
011400    05 PRODUCT-TOTAL           PIC $ZZZ,ZZ9.99.
011500    
011600 01 RPT-TOTAL-LINE.
011700    05 FILLER                  PIC X(55) VALUE SPACES.
011800    05 FILLER                  PIC X(12) VALUE "TOTAL SOLD:  ".
          05 FILLER                  pic X(02) VALUE SPACES.
011900    05 RPT-TOTAL-AMT           PIC $ZZZ,ZZ9.99.         
012000    
012100 01 WS-TOTALS.
012200    05 WS-LINE-TOTAL           PIC 9(05)V99.
012300    05 WS-REPORT-TOTAL         PIC 9(05)V99.
012400    
012500 01 WS-CURRENT-DATE-FIELDS.
012600      05  WS-CURRENT-DATE.
012700          10  WS-CURRENT-YEAR    PIC  9(4).
012800          10  WS-CURRENT-MONTH   PIC  9(2).
012900          10  WS-CURRENT-DAY     PIC  9(2).
013000      05  WS-CURRENT-TIME.
013100          10  WS-CURRENT-HOUR    PIC  9(2).
013200          10  WS-CURRENT-MINUTE  PIC  9(2).
013300          10  WS-CURRENT-SECOND  PIC  9(2).
013400          10  WS-CURRENT-MS      PIC  9(2).
013500      05  WS-DIFF-FROM-GMT       PIC S9(4).
013600     
013700* copy "LOOKUP.cpy".
013800 01 WS-LOOKUP-FIELDS.
013900    05 WS-LOOKUP-TYPE             PIC X(01) VALUE SPACES.
014000       88 PRODUCT-LOOKUP                    VALUE 'P'.
014100       88 SALES-LOOKUP                      VALUE 'S'.
014200       88 INVALID-LOOKUP-TYPE               VALUE 'I'.
014300       88 INVALID-LOOKUP-KEY                VALUE 'K'.
014400       88 SUCESSFUL-LOOKUP                  VALUE '0'.
014500    05 WS-LOOKUP-AREA.
014600       10 WS-SALES-ID             PIC X(03)    VALUE SPACES.
014700       10 WS-PRODUCT-ID           PIC X(05)    VALUE SPACES.
014800       10 WS-SALES-NAME           PIC X(15)    VALUE SPACES.
014900       10 WS-PRODUCT-NAME         PIC X(10)    VALUE SPACES.
015000       10 WS-PRODUCT-PRICE-X      PIC X(07)    VALUE SPACES.
015100       10 WS-PRODUCT-PRICE REDEFINES
015200          WS-PRODUCT-PRICE-X      PIC 9(05)V99.
015300          
015400 01 ANY-KEY                       pic x(01) VALUE SPACES.
015500 
015600 PROCEDURE DIVISION.
015700 
015800 0000-MAIN.
015900     PERFORM 0100-INITIALIZE.
016000     PERFORM 1000-PROCESS-FILE
016100       UNTIL NO-DATA.
016200     PERFORM 9000-COMPLETE-PROGRAM.
016300     display "PRESS ANY KEY TO EXIT."
016400         accept ANY-KEY.
016500     GOBACK.
016600 
016700 0100-INITIALIZE.   
016800*    OPEN THE FILES
016900     OPEN INPUT  SALES-FILE.
017000     OPEN OUTPUT SALES-RPT-FILE.
017100     
017200*    GET DATE/TIME INFORMATION & MOVE TO REPORT LINE
017300     perform 1010-GET-DATE.
017400     
017500*    WRITE THE HEADINGS
017600     perform 1020-WRITE-HEADINGS.
017700     
017800*    READ THE FIRST SALES RECORD
017900     perform 1030-READ-RECORDS.
018000 
018100 1010-GET-DATE.
018200     move FUNCTION current-date TO WS-CURRENT-DATE-FIELDS.
018300     move WS-CURRENT-MONTH to MM.
018400     move WS-CURRENT-DAY to DD.
018500     move WS-CURRENT-YEAR(3:4) to YY.
018600     move WS-CURRENT-HOUR to TIME-HH.
018700     move WS-CURRENT-MINUTE to TIME-MM.
018800     move WS-CURRENT-SECOND to TIME-SS.
018900 
019000 1020-WRITE-HEADINGS.
019100     WRITE SALES-RPT-RECORD 
019200      FROM RPT-LINE-01 after advancing 01 LINES.
019300     WRITE SALES-RPT-RECORD
019400      FROM RPT-LINE-02 after advancing 01 LINES.
019500     WRITE SALES-RPT-RECORD 
019600      FROM RPT-LINE-03 AFTER ADVANCING 01 LINES.
019700     WRITE SALES-RPT-RECORD 
019800      FROM RPT-LINE-04 AFTER ADVANCING 01 LINES.
019900     WRITE SALES-RPT-RECORD 
020000      FROM RPT-COLUMN-HDG AFTER ADVANCING 02 LINES.
020100 
020200 1030-READ-RECORDS.
020300     read SALES-FILE into SALES-RECORD-LAYOUT
020400       at end
020500         display "END OF PAYROLL FILE".
020600 
020700 1000-PROCESS-FILE.
020800*    THIS PARAGRAPH WILL PROCESS EACH RECORD.
020900*    1ST, PREPARE TO CALL LOOKUP TO GET SALES INFORMATION:
021000*    display WS-LOOKUP-TYPE.
021100     MOVE 'S'                    TO WS-LOOKUP-TYPE.
021200     MOVE SPACES                 TO WS-LOOKUP-AREA.
021300     MOVE SALES-ID               TO WS-SALES-ID
021400                                    SALES-ID-OUT.
021500                                    
021600*    2ND, CALL THE LOOKUP PROGRAM WITH THE CORRECT PARAMETERS.
021700     call "LOOKUP" using WS-LOOKUP-TYPE,
021800                         WS-LOOKUP-AREA,
021900                         WS-SALES-ID.
022000     
022100*    3RD, IF THE CALL WAS SUCESSFUL, USE THE RETURNED INFORMATION.
022200*    IF THE CALL WAS NOT SUCESSFUL, HANDLE THE ERROR AND
022300*      DISPLAY AN ERROR.
022400     IF WS-LOOKUP-TYPE = 0
022500*       display "SUCCESSFUL CALL TO LOOKUP"
              move SALES-ID to SALES-ID-OUT
              move WS-SALES-NAME to SALES-NAME-OUT
              perform 1500-PERFORM-SECOND-LOOKUP
022600     else if WS-LOOKUP-TYPE = "K"
              display "PRODUCT NOT FOUND FOR SALES ID " SALES-ID
022700        move SALES-ID to SALES-ID-OUT
              move "NOT FOUND" to SALES-NAME-OUT
              move zeros to PRODUCT-PRICE-OUT
              move zeroes to PRODUCT-TOTAL
              move "NOT FOUND" to PRODUCT-NAME-OUT
022800     else if WS-LOOKUP-TYPE = "I"
              move SALES-ID to SALES-ID-OUT
              move "DOES NOT EXIST" to SALES-NAME-OUT
023000     ELSE
              move SALES-ID to SALES-ID-OUT
              move "ERROR WITH ID" to SALES-NAME-OUT.
025300     
025400*    7TH, COMPLETE THE DETAIL LINE FIELDS
025500     perform 3000-COMPLETE-DETAIL-LINE.
025600     
025700*    8TH, WRITE THE DETAIL LINE
025800     perform 4000-WRITE-RECORDS.
025900     
026000*    9TH, READ THE NEXT RECORD   
026100     perform 5000-READ-RECORDS.
026200 
       1500-PERFORM-SECOND-LOOKUP.
      *    4TH, SET UP THE PARAMETERS TO CALL LOOKUP AND GET 
023400*    PRODUCT INFOMATION.
           MOVE 'P'                    TO WS-LOOKUP-TYPE.
021200     MOVE SPACES                 TO WS-LOOKUP-AREA.
021300     MOVE PRODUCT-ID               TO WS-PRODUCT-ID
021400                                      PRODUCT-CODE-OUT.
023600     
023700*    5TH, CALL THE LOOKUP PROGRAM TO GET THE PRODUCT INFORMATION
023800     call "LOOKUP" using WS-LOOKUP-TYPE,
021800                         WS-LOOKUP-AREA,
021900                         WS-PRODUCT-ID.
023900     
024000*    6TH, IF THE CALL WAS SUCESSFUL, USE THE RETURNED INFORMATION
024100*    TO REPORT AND THE CALCULATE THE SALES TOTAL.    
024200*    IF THE CALL WAS NOT SUCESSFUL, HANDLE THE ERROR AND
024300*      DISPLAY AN ERROR.
024400     IF WS-LOOKUP-TYPE = 0
024500*       display "SUCCESSFUL CALL TO LOOKUP"
024600        perform 2000-DO-CALCULATIONS
              move WS-PRODUCT-PRICE to PRODUCT-PRICE-OUT
              move WS-PRODUCT-NAME to PRODUCT-NAME-OUT
024700     else if WS-LOOKUP-TYPE = "K"
024800        display "PRODUCT NOT FOUND FOR PRODUCT ID " PRODUCT-ID
              move "NOT FOUND" to SALES-NAME-OUT
              move zeros to PRODUCT-PRICE-OUT
              move zeroes to PRODUCT-TOTAL
              move "NOT FOUND" to PRODUCT-NAME-OUT
024900     else if WS-LOOKUP-TYPE = "I"
025000        display "NO PRODUCT OR SALES FOR THAT ID"
025100     ELSE
025200        display "ERROR WITH PRODUCT ID " PRODUCT-ID.
       
026300 2000-DO-CALCULATIONS.
026400     set WS-LINE-TOTAL to zeroes.
           compute WS-LINE-TOTAL = WS-PRODUCT-PRICE * QUANITY-SOLD.
026500     move WS-LINE-TOTAL to PRODUCT-TOTAL.
026600     compute WS-REPORT-TOTAL = WS-REPORT-TOTAL + WS-LINE-TOTAL.
           
026700 
026800 3000-COMPLETE-DETAIL-LINE.
           move QUANITY-SOLD to PRODUCT-QUANITY-OUT.
           
027000 
027100 4000-WRITE-RECORDS.
027200 WRITE SALES-RPT-RECORD FROM WS-OUTPUT-DETAIL after advancing 01
                                                    line.
027300     if GOOD-FILE-WRITE 
027400*       display "SUCCESSFUL FILE WRITE"
027500     else  
027600        display "BAD WRITE - FILE STATUS: " 
027700          WS-SALES23R-FILE-STATUS.
027800 
027900 5000-READ-RECORDS.
028000     READ SALES-FILE INTO SALES-RECORD-LAYOUT
028100       AT END
028200         DISPLAY "END OF FILE"
028300       NOT AT END
028400         ADD 1 TO WS-RECORD-COUNT-IN.
028500 
028600 9000-COMPLETE-PROGRAM.
028700*    MOVE THE REPORT TOTAL TO TOTAL DETAIL LINE AND
028800*    WRITE THE DETAIL LINE OUT.
028900     move WS-REPORT-TOTAL to RPT-TOTAL-AMT.
029000     WRITE SALES-RPT-RECORD 
029100      FROM RPT-TOTAL-LINE AFTER ADVANCING 02 LINES.
029200     
029300*    CLOSE THE FILES.
029400     CLOSE SALES-RPT-FILE
029500           SALES-FILE.