ABAP log

March 15, 2007

Working with SAP plant (factory) calendars in ABAP.

Filed under: ABAP, SAP — abaplog @ 8:01 pm

Every plant in SAP can have its own calendar that defines plant working days. This calendar is used by planning functions to determine whether a particular day is working one or not. You can see the effects if, for example, you try to create a delivery schedule on Sunday, and the system says that actually the next working day is another one.

Very ofter we have to account for working days and exclude weekends and holidays in our ABAP. To do that, SAP provides two functions modules that are used to convert between the “normal” date and the enumerated “factory day”. The factory day is a number assigned to each working day in plant calendar. The functions are FACTORYDATE_CONVERT_TO_DATE and DATE_CONVERT_TO_FACTORYDATE and they are documented directly in SAP, in SAP standalone help (BC Extended Applications Function Library) and, of course, at se37.com.
The typical use of those functions is to add a number of days to a given date, skipping holidays and weekends. This is often asked on SAP-related forums and is one of the subjects in SAPfans ABAP FAQ.

I use those functions also for simple checks whether a day is working or not, and to move a date to a working day (forwards or backwards). Below are forms that I use. The factory calendar (FABKL) can be read directly from the table T001W. The parameter pf_correct should be ‘+’ to move forward or ‘-‘ to move backwards.

form move_to_working
  using
    pf_fabkl   type fabkl
    pf_correct type cind
  changing
    pf_date    type datum.

  data:
    lf_newdate type datum.

  check not pf_fabkl is initial.
  call function 'DATE_CONVERT_TO_FACTORYDATE'
       exporting
            correct_option               = pf_correct
            date                         = pf_date
            factory_calendar_id          = pf_fabkl
       importing
            date                         = lf_newdate
       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.
  if sy-subrc = 0.
    pf_date = lf_newdate.
  endif.

endform.

form is_working_day
  using
    pi_werks like pbim-werks
    pi_date  like sy-datum
    pi_fabkl   type fabkl
  changing
    po_working like space.

  data:
    w_work  like SCAL-INDICATOR,
    w_fabkl like t001w-fabkl.

  clear po_working.
  call function 'DATE_CONVERT_TO_FACTORYDATE'
       EXPORTING
            date                         = pi_date
            factory_calendar_id          = pi_fabkl
       IMPORTING
            workingday_indicator         = w_work
       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.
  if sy-subrc = 0 and w_work is initial.
    po_working = c_x.
  endif.

endform.

2 Comments »

  1. Hey,

    just wanted to say hi, tell you I appreciate what you are doing on your blog. It’s always a nice read even though I try not to do ABAP anymore and have switched to the bright (Java) site of life;) But it’s good to see someone using this blogging stuff to post more technical notes. You’re probably on SDN also, right? Have a blog there too? Hope so, if not you should! Keep up the good work.

    Btw. this factory calendar in SAP is a really neat stuff. Have used it a lot in SD and PP. Helps you a lot but someone has to maintain it.

    Cheers,
    Oliver

    Comment by Oliver — March 16, 2007 @ 5:39 am

  2. Thanks! I’m not on SDN – I prefer to be free, i.e. sapfans.com and my own web log.

    Regarding factory calendars: SAP comes with all possible calendars in standard, you have to maintain your own only if you do something really special.

    Comment by abaplog — March 17, 2007 @ 8:17 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.