ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Date calculation for RPGLE in Free format

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date calculation for RPGLE in Free format

    I'm new at coding in RPG and am currently working on an assignment for a class. My problem area is with calculation of two dates (which we have not covered yet for the class). I'm using Bryan Meyer's "Programming in RPG IV", Third Edition, and though it is very helpful I'm just not getting the correct results.

    I'm trying to get a date calculation that compares if a payment is made after the last day a discount is able to be paid. When I display my results, it shows 0s in my Discount Amount column. I'm trying to do this with RPGLE in Free Form. I'm including some of the code and the display to give you an idea of what is wrong and what I am looking for (I know the positions are not correctly displaying in this post). If you need more information, please let me know. Thank you in advance for the help!

    Code:
    DDISDTE           S               D   DATFMT(*MDY)
    DPAYDTE           S               D   DATFMT(*MDY)
    DTOTAMTDUE        S              7  2             
    DDTEDIF           S              3  0             
    DDISCAMT          S              5  2             
    DNETDUE           S              7  2             
    DTOTDISAMT        S              5  2             
    DTOTNETDUE        S              7  2   
    
    /FREE                                    
      EXCEPT Headings;                       
      READ RPGEXAM1;                         
                                             
      DOW NOT %EOF;                          
        EXCEPT DETAIL;                       
        READ RPGEXAM1;                       
        DISDTE = %DATE(ADSDTE:*MDY);         
        PAYDTE = %DATE(APADTE:*MDY);         
        DTEDIF = %DIFF(PAYDTE:DISDTE:*DAYS); 
        IF DTEDIF >= 0;                      
          EVAL(H) DISCAMT = DISCAMT * ADSRTE;
        ELSE;                                
          DISCAMT = 0;                       
        ENDIF;                               
        TOTAMTDUE = AAMTDU + TOTAMTDUE;      
        TOTDISAMT = DISCAMT + TOTDISAMT;     
        TOTNETDUE = TOTAMTDUE - TOTDISAMT;   
      ENDDO;           
                       
      EXCEPT TOTALS;   
      EVAL *INLR = *ON;
      RETURN;          
    /END-FREE
    The Display is:
    Code:
       DATE      DATE    DISCOUNT        AMOUNT   DISCOUNT  AMOUNT   
      PAID      DUE       DUE    RATE     DUE      AMOUNT    DUE    
     2/17/07  3/01/07    2/19/07 .030  13,000.00     .00        .00 
     2/17/07  2/19/07   12/31/07 .040     500.00     .00        .00 
     2/18/07  2/15/07   12/31/06 .060  33,225.41     .00        .00 
     2/18/07  2/18/07    1/15/07 .020       5.00     .00        .00 
     2/18/07  4/01/07    3/02/07 .015      10.00     .00        .00 
     2/18/07  3/15/07    2/18/07 .020  18,000.00     .00        .00 
     2/19/07 12/15/07   11/14/07 .035     100.00     .00        .00 
     2/19/07  2/19/07    2/19/07 .020     200.00     .00        .00 
                                      $52,240.41    $.00 $52,240.41
    I also realize my Amount Total is not correct and that is just a matter of placing it correctly within the Free Form.

    Thanks again

  • #2
    Re: Date calculation for RPGLE in Free format

    BTW uppercase code is not only ugly..but it has been scientifically proven that is runs slower


    Code:
          *
          *--------------------------------------------------------
          *
          * Variable Definition
          *                 s
         d count           s              4  0
         d days            s              5  0
         d discountdate    s              6  0
         d paydate         s              6  0
         d ISOpay          s               d   datfmt(*iso)
         d ISOdiscount     s               d   datfmt(*iso)
    
    
          /Free
    
            //--------------------------------------------------------
            // MAIN PROGRAM
            //--------------------------------------------------------
    
                exsr  Hskpg;
    
                for count = 1 to 5;
    
                 select;
                  when count = 1;
                   discountdate =  011507;
                   paydate      =  010107;
                  when count = 2;
                   discountdate =  021007;
                   paydate      =  020207;
                  when count = 3;
                   discountdate =  031707;
                   paydate      =  031707;
                  when count = 4;
                   discountdate =  040507;
                   paydate      =  041107;
                  when count = 5;
                   discountdate =  050207;
                   paydate      =  052207;
                 endsl;
    
                test(de) *mdy  discountdate;
                if not%error and discountdate > *zeros;
                 ISOdiscount = %date(discountdate:*mdy);
                endif;
    
                test(de) *mdy  paydate;
                if not%error and paydate > *zeros;
                 ISOpay = %date(paydate:*mdy);
                endif;
    
                Days = %diff(ISOdiscount : ISOpay : *days);
                dsply %editc(days:'J')  ' ';
    
                if days >= *zeros;
    
                 // this guy gets his discount
    
                endif;
    
                endfor;
    
    
    
                 *inlr = *on;
    
            //--------------------------------------------------------
            // Hskpg - one time run subroutine
            //--------------------------------------------------------
                 begsr Hskpg;
    
    
                 endsr;
    
          /End-Free
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: Date calculation for RPGLE in Free format

      I guess I kinda know what you're saying but since I'm just starting to learn RPG it would've been nice to have you explain why you decided to go with a Select statement and enter the values that you did. I just don't see how yours would work if the database that a person would use would have a large number of customers. Anyway, I found a different way around it by doing a date comparison (using %date(discountdate) > %date(paydate)) that seems to work. Thanks for trying to help.

      Comment


      • #4
        Re: Date calculation for RPGLE in Free format

        the %date work cause its converting it to the system default date..
        which you being in Wisconsin I would guess is *ISO....
        prior to this you were trying t use *MDY rather than ISO.
        That is exact what the sample program did that I posted....

        I posted the code as an example if I were you I would have down loaded the source
        put it in debug and gave it a try...
        then if I had any questions I would have posted them.

        just goes to show you nothing is free not even free stuff.

        take care
        jamie
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: Date calculation for RPGLE in Free format

          The select statement was used to load test data into the variables. Because this is a test program, Jamie fifn't want to go to the trouble of creating a database to store test data, or a display file for data entry. That would have taken a lot of time, but provided very little toward the answer you needed.
          "Time passes, but sometimes it beats the <crap> out of you as it goes."

          Comment


          • #6
            Re: Date calculation for RPGLE in Free format

            Just a couple of bugs in the code (other than the date formating problem which you have fixed)
            Code:
            001  EXCEPT Headings;                       
            002  READ RPGEXAM1;                         
                                                     
            003  DOW NOT %EOF;                          
            004    EXCEPT DETAIL;                       
            005    READ RPGEXAM1;                       
            006    DISDTE = %DATE(ADSDTE:*MDY);         
            007    PAYDTE = %DATE(APADTE:*MDY);         
            008    DTEDIF = %DIFF(PAYDTE:DISDTE:*DAYS); 
            009    IF DTEDIF >= 0;                      
            010      EVAL(H) DISCAMT = DISCAMT * ADSRTE;
            011    ELSE;                                
            012      DISCAMT = 0;                       
            013    ENDIF;                               
            014    TOTAMTDUE = AAMTDU + TOTAMTDUE;      
            015    TOTDISAMT = DISCAMT + TOTDISAMT;     
            016    TOTNETDUE = TOTAMTDUE - TOTDISAMT;   
            017  ENDDO;           
                               
            018  EXCEPT TOTALS;
            The logic this program would follow would be:

            Print Headings
            Read file < --- First record is read
            Is end of file? No
            Print detail < --- This is wrong, no detail calculations have been made
            Read file < --- Now you are reading the SECOND record. Didn't you want to total the first record?
            Compare dates and calculate DETAIL columns
            Add Totals
            Check for EOF again
            If EOF Found, print totals, end program

            Notice on line 004 that you are printing a detail line BEFORE actually doing the calculations for your amount columns.

            Notice on line 005 that you are reading the a record, just like you did on line 002.

            Move these 2 lines to the BOTTOM of the loop (between 016 and 017) to get the logic to work as it should.

            The first time through, the database read is OUTSIDE of your loop at line 002. Once inside the loop, the read should be just before the loop exit point so that the information you read is processed correctly.

            So if you move those lines:
            Code:
            001  EXCEPT Headings;                       
            002  READ RPGEXAM1;                         
                                                     
            003  DOW NOT %EOF;                          
            004    DISDTE = %DATE(ADSDTE:*MDY);         
            005    PAYDTE = %DATE(APADTE:*MDY);         
            006    DTEDIF = %DIFF(PAYDTE:DISDTE:*DAYS); 
            007    IF DTEDIF >= 0;                      
            008      EVAL(H) DISCAMT = DISCAMT * ADSRTE;
            009    ELSE;                                
            010      DISCAMT = 0;                       
            011    ENDIF;                               
            012    TOTAMTDUE = AAMTDU + TOTAMTDUE;      
            013    TOTDISAMT = DISCAMT + TOTDISAMT;     
            014    TOTNETDUE = TOTAMTDUE - TOTDISAMT;   
            015    EXCEPT DETAIL;                       
            016    READ RPGEXAM1;                       
            017  ENDDO;           
                               
            018  EXCEPT TOTALS;
            The logic would flow like this:

            Print Headings
            Read file < --- First record is read
            Is end of file? No
            Compare dates and calculate DETAIL columns
            Add Totals
            Print detail < --- This is where it needs to be
            Read file < --- Now you are reading the SECOND record after the first has been processed correctly
            Check for EOF again
            If EOF Found, print totals, end program


            Hope that helps some. The pseudo logic will work for any language, not just RPG
            Last edited by KenM; October 29, 2007, 10:28 AM.
            Goodbye

            Comment

            Working...
            X