Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Calculating GRN days

Former Member
0 Likes
1,736

Hi everyone

I'm asked to make some ammendments to somebody else's program. I'm now faced with a problem where i am unsure as to how i should modify the original logic of the program that does a calculation that finds out the GRN day. Basically, GRN days is the result derived from doing a comparison between the GRN date and PO Creation Date (EKBE-CPUDT - EKPO-AEDAT).

However, when i did some testing with the original logic, the result is inaccurate. Take 2 examples, if the PO Creation Date is 21.11.2005 and the GRN Date is 22.11.2005, the difference in days should be 1 but at the same time, lets say that both dates are 22.11.2005, for some reason, the result that appeared is also 1 when it should have been 0. I know its abit confusing based on my explanation but i hope you guys could understand what i trying to say.

Just have a look at what was originally written:


*&---------------------------------------------------------------------*
*&      Form  GET_LATEST_GRNDATE
*&---------------------------------------------------------------------*
FORM GET_LATEST_GRNDATE.
DATA: D_LINES TYPE I.

  CLEAR T_DATE.
  DESCRIBE TABLE T_DATE LINES D_LINES.
  IF D_LINES GT 0.
    SORT T_DATE BY CPUDT EBELN EBELP DESCENDING.
    READ TABLE T_DATE WITH KEY EBELN = T_PUR-EBELN.
    IF SY-SUBRC = 0.
      READ TABLE T_DATE INDEX 1.
      MOVE: T_DATE-CPUDT TO T_PUR-CPUDT.
      MODIFY T_PUR TRANSPORTING CPUDT
             WHERE EBELN = T_PUR-EBELN AND EBELP = T_PUR-EBELP.
    ENDIF.
  ENDIF.

*--> GRN Days = GRN date - PO printed date
  IF T_PUR-VSTAT = 1.
    IF T_PUR-CPUDT IS INITIAL OR T_PUR-DATVR_PO IS INITIAL.
      V_GRNDAY = '0'.
    ELSE.
PERFORM CALC_ACT_WORKDAY
        USING T_PUR-DATVR_PO T_PUR-CPUDT V_GRNTEMP.
      PERFORM CONVERT_NEG_TO_BRACKET USING V_GRNTEMP
                                     CHANGING V_GRNDAY.
    ENDIF.
  ELSEIF T_PUR-VSTAT = 2 OR T_PUR-VSTAT = 0.
    IF T_PUR-CPUDT IS INITIAL OR T_PUR-AEDAT IS INITIAL.
      V_GRNDAY = '0'.
    ELSE.
PERFORM CALC_ACT_WORKDAY
        USING T_PUR-AEDAT T_PUR-CPUDT V_GRNTEMP.
      PERFORM CONVERT_NEG_TO_BRACKET USING V_GRNTEMP
                                     CHANGING V_GRNDAY.
    ENDIF.
  ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  CALC_ACT_WORKDAY                            -DEVK951017
*&---------------------------------------------------------------------*
*      -->lowdt  -Low Date                                             *
*      -->Highdt -High Date                                            *
*      -->days   -Total Days                                           *
*----------------------------------------------------------------------*
FORM CALC_ACT_WORKDAY USING LOWDT HIGHDT DAYS.
  DATA: DAT LIKE SY-DATUM.
  DAYS = 0.
  DAT = LOWDT.

  DO.

CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
     EXPORTING
          DATE                       = DAT
          FACTORY_CALENDAR_ID        = 'MY'
          MESSAGE_TYPE               = 'I'
     EXCEPTIONS
          DATE_AFTER_RANGE           = 1
          DATE_BEFORE_RANGE          = 2
          DATE_INVALID               = 3
          DATE_NO_WORKINGDAY         = 4
          FACTORY_CALENDAR_NOT_FOUND = 5
          MESSAGE_TYPE_INVALID       = 6
          OTHERS                     = 7.
    IF SY-SUBRC = 0.
      DAYS = DAYS + 1.
    ENDIF.

    DAT = DAT + 1.
    IF DAT GE HIGHDT.
      EXIT.
    ENDIF.
  ENDDO.
ENDFORM.

I can post the entire program if you guys need to have a look at the entire flow.

I'm at a lost on how i should modify it

1 ACCEPTED SOLUTION
Read only

suresh_datti
Active Contributor
0 Likes
1,494

Hi Bernanrd,

PL use the function call DAYS_BETWEEN_TWO_DATES instead of EKBE-CPUDT - EKPO-AEDAT.

Regards,

Suresh Datti

10 REPLIES 10
Read only

suresh_datti
Active Contributor
0 Likes
1,495

Hi Bernanrd,

PL use the function call DAYS_BETWEEN_TWO_DATES instead of EKBE-CPUDT - EKPO-AEDAT.

Regards,

Suresh Datti

Read only

0 Likes
1,494

hi

use the following func mod:

HR_HK_DIFF_BT_2_DATES

plz reward points if it helps!!

Regards

Gunjan

Read only

0 Likes
1,494

Could you pls explain more on this? I'm still new to ABAP and have yet the opportunity to do functions calls in my previous work so far

Read only

0 Likes
1,494

Hi again,

I've been asked to use SD_DATETIME_DIFFERENCE instead. How am i to use it?

Read only

0 Likes
1,494

Do i just simply comment out the original function call and write the new one? Or i would need to make adjustments to other parts of the program before i do anything like that?

Read only

0 Likes
1,494

Hi Bernard,

If GRN date & PO creation date being equal is the only issue, it requires only a minor change to the exsiting code. PL put the following check after the statement DAYS = 0. in your code.

******************************

  • insert a check to see if GRN date = Po creation date

CHECK HIGHDT gt LOWDT.

***************************

hope this helps

Regards,

Suresh Datti

Read only

0 Likes
1,494

Yes, simply replace the code in your "FORM CALC_ACT_WORKDAY" with the call to the function modules suggested here. What you can do is comment out the code here and then click on the button 'Pattern' and enter the function module name against the radiobutton "Call function module". This will give you the template of the call in the code. Simply give the appropriate values to the parameters and you should be good to go.

Regards,

Srinivas

Read only

0 Likes
1,494

Wow...thanks Suresh...it fixed whatever it was that went wrong...

Many many reward points for you, friend

Read only

Former Member
0 Likes
1,494

But it looks like your code is looking for the number of working days between the two given dates. But the logic is slightly wrong in the sense that, you are incrementing the days when SY-SUBRC = 0. But it will be zero if the date is a working day, and you will end up adding one to it even though you entered the same date for from and to dates. Here is how you can change that if the number of working days is what your business requirement is.


FORM CALC_ACT_WORKDAY USING LOWDT HIGHDT DAYS.
  DATA: DAT LIKE SY-DATUM.
  DAYS = 0.
  DAT = LOWDT.
 
  DO.
    IF DAT GE HIGHDT.
      EXIT.
    ENDIF.
    CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
      EXPORTING
          DATE                       = DAT
          FACTORY_CALENDAR_ID        = 'MY'
          MESSAGE_TYPE               = 'I'
      EXCEPTIONS
          DATE_AFTER_RANGE           = 1
          DATE_BEFORE_RANGE          = 2
          DATE_INVALID               = 3
          DATE_NO_WORKINGDAY         = 4
          FACTORY_CALENDAR_NOT_FOUND = 5
          MESSAGE_TYPE_INVALID       = 6
          OTHERS                     = 7.
    IF SY-SUBRC = 0 and DAT NE HIGHDT.
      DAYS = DAYS + 1.
    ENDIF.
    DAT = DAT + 1.
  ENDDO.
ENDFORM.

Read only

0 Likes
1,494

Srinivas, i tried out the solution you wrote...it works too...thank you...

Many reward points for you, too