"Working days" and factory calendar in ABAP

 There are some functions and classes available to work with factory calendar. Some real life scenarios, however, could be more tricky than just finding next working day.
 Say you need to know which date would be a 5th working day of any given month. Even if requirement has fixed number, it might not be very helpful, as factory calendars can be very different from basic calendar. Let's take the most simple example - 01.03.2017. 5th day of the months is Sunday, which makes 07.03.2017 the 5th working day. Holiday season, or any other business related reason, can push 5th working day to even later dates. That means, we really need to count working days to get the proper result.
 Unfortunately, SAP systems do not have ready made functions to get just that (even though, such functions might be a part of some specific applications/module, but let's assume we have none or using SAP BW system, with only generic ABAP/4 functions available).

 In our case we need to start from finding 1st working day of the month and convert result into factory calendar. There is a standard function which does both


CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
             EXPORTING
               DATE                               = '20170301'
               FACTORY_CALENDAR_ID                = 'XX'
            IMPORTING
              FACTORYDATE                        = lv_factorydate
            EXCEPTIONS
              CALENDAR_BUFFER_NOT_LOADABLE       = 1
              CORRECT_OPTION_INVALID             = 2
              DATE_AFTER_RANGE                   = 3
              DATE_BEFORE_RANGE                  = 4
              DATE_INVALID                       = 5
              FACTORY_CALENDAR_NOT_FOUND         = 6
              OTHERS                             = 7
                     .

 Function will return factory date ID and that is the date we need to increment and add 4 if need 5th working day

ADD 4 TO LV_FACTORYDATE.

 Factory date ID is not very meaningful indicator, so what we need to do is to convert it back to the normal date format, using another standard function

CALL FUNCTION 'FACTORYDATE_CONVERT_TO_DATE'
          EXPORTING
            FACTORYDATE                        = LV_FACTORYDATE
            FACTORY_CALENDAR_ID                = 'XX'
         IMPORTING
           DATE                               =  lv_period_to
         EXCEPTIONS
           CALENDAR_BUFFER_NOT_LOADABLE       = 1
           FACTORYDATE_AFTER_RANGE            = 2
           FACTORYDATE_BEFORE_RANGE           = 3
           FACTORYDATE_INVALID                = 4
           FACTORY_CALENDAR_ID_MISSING        = 5
           FACTORY_CALENDAR_NOT_FOUND         = 6
           OTHERS                             = 7
                  .

 Variable lv_period_to will have 20170307 - the 5th working day of the month.

Comments

Popular posts from this blog

Debugging planning functions in SAP BI-IP

Converting number to date data types