Home » ABAP » Getting Working Days and Holidays Of a Plant
Posted in

Getting Working Days and Holidays Of a Plant

calendar
calendar

Get Working Days of a Plant

Every plant has own factory calendar in SAP. A factory calendar gives us working days in giving date range. Working days are depends on country, year, public holidays and so on.

You can see factory calendar details in SCAL T-Code in SAP. We will see how to fetch exact same data from SCAL for our ABAP reports.

Following code block gives working days in 1 years by plant which is user entered.

I started by getting factory calendar first. Factory calendar is kept as FABKL in T001W table. I get next years current day by using function RP_CALC_DATE_IN_INTERVAL . I have may get next year by end_date = |{ begin_date(4) + 1 }{ begin_date+4 }|. but I wanted to mention this useful function.

Function RP_CALC_DATE_IN_INTERVAL is useful for adding, subtracting specific amount of days, months and years to a date.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PARAMETERS p_werks TYPE t001w-werks. "user enters plant
START-OF-SELECTION.
SELECT SINGLE fabkl FROM t001w
WHERE werks = @p_werks
INTO @DATA(factory_calendar).
DATA: begin_date TYPE datum,
end_date TYPE datum.
begin_date = sy-datum.
"get date after 1 year
"I'd like to mention following function.
"You can increase year by simply doing
""end_date = |{ begin_date(4) + 1 }{ begin_date+4 }|.
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = begin_date
days = '00'
months = '00'
signum = '+' "add (you should use '-' to subtract )
years = '01'
IMPORTING
calc_date = end_date.
DATA: working_days TYPE STANDARD TABLE OF rke_dat.
"get all working days by factory calendar
CALL FUNCTION 'RKE_SELECT_FACTDAYS_FOR_PERIOD'
EXPORTING
i_datab = begin_date
i_datbi = end_date
i_factid = factory_calendar
TABLES
eth_dats = working_days
EXCEPTIONS
date_conversion_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
"display
cl_demo_output=>display(
EXPORTING
data = working_days
name = 'Working days of this year'
).
PARAMETERS p_werks TYPE t001w-werks. "user enters plant START-OF-SELECTION. SELECT SINGLE fabkl FROM t001w WHERE werks = @p_werks INTO @DATA(factory_calendar). DATA: begin_date TYPE datum, end_date TYPE datum. begin_date = sy-datum. "get date after 1 year "I'd like to mention following function. "You can increase year by simply doing ""end_date = |{ begin_date(4) + 1 }{ begin_date+4 }|. CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL' EXPORTING date = begin_date days = '00' months = '00' signum = '+' "add (you should use '-' to subtract ) years = '01' IMPORTING calc_date = end_date. DATA: working_days TYPE STANDARD TABLE OF rke_dat. "get all working days by factory calendar CALL FUNCTION 'RKE_SELECT_FACTDAYS_FOR_PERIOD' EXPORTING i_datab = begin_date i_datbi = end_date i_factid = factory_calendar TABLES eth_dats = working_days EXCEPTIONS date_conversion_error = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. "display cl_demo_output=>display( EXPORTING data = working_days name = 'Working days of this year' ).
PARAMETERS p_werks TYPE t001w-werks. "user enters plant

START-OF-SELECTION.
  SELECT SINGLE fabkl FROM t001w
       WHERE werks = @p_werks
       INTO @DATA(factory_calendar).

  DATA: begin_date TYPE datum,
        end_date   TYPE datum.

  begin_date = sy-datum.
  "get date after 1 year
  "I'd like to mention following function.
  "You can increase year by simply doing
  ""end_date = |{ begin_date(4) + 1 }{ begin_date+4 }|.
  CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
    EXPORTING
      date      = begin_date
      days      = '00'
      months    = '00'
      signum    = '+' "add (you should use '-' to subtract )
      years     = '01'
    IMPORTING
      calc_date = end_date.

  DATA: working_days TYPE STANDARD TABLE OF rke_dat.

  "get all working days by factory calendar
  CALL FUNCTION 'RKE_SELECT_FACTDAYS_FOR_PERIOD'
    EXPORTING
      i_datab               = begin_date
      i_datbi               = end_date
      i_factid              = factory_calendar
    TABLES
      eth_dats              = working_days
    EXCEPTIONS
      date_conversion_error = 1
      OTHERS                = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  "display
  cl_demo_output=>display(
    EXPORTING
      data = working_days
      name = 'Working days of this year'
  ).

Get holidays of a plant

We’ve got working days but we may need holidays in a specific date range. SAP also presents an useful function to get holidays.

To fetch holidays, we need holiday calendar. We fetch this field from TFACD table as HOCID by factory calendar which we have already got from T001W. You may also join these two table up to your case.

Function HOLIDAY_GET gives us holidays between given dates.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"get holiday calendar
SELECT SINGLE hocid FROM tfacd "#EC CI_BYPASS
WHERE ident = @factory_calendar
INTO @DATA(holiday_calendar).
DATA holidays TYPE STANDARD TABLE OF iscal_day.
"get holidays
CALL FUNCTION 'HOLIDAY_GET'
EXPORTING
holiday_calendar = holiday_calendar
factory_calendar = factory_calendar
date_from = begin_date
date_to = end_date
TABLES
holidays = holidays
EXCEPTIONS
factory_calendar_not_found = 1
holiday_calendar_not_found = 2
date_has_invalid_format = 3
date_inconsistency = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
"display
cl_demo_output=>display(
EXPORTING
data = working_days
name = 'Holiday days of this year'
).
"get holiday calendar SELECT SINGLE hocid FROM tfacd "#EC CI_BYPASS WHERE ident = @factory_calendar INTO @DATA(holiday_calendar). DATA holidays TYPE STANDARD TABLE OF iscal_day. "get holidays CALL FUNCTION 'HOLIDAY_GET' EXPORTING holiday_calendar = holiday_calendar factory_calendar = factory_calendar date_from = begin_date date_to = end_date TABLES holidays = holidays EXCEPTIONS factory_calendar_not_found = 1 holiday_calendar_not_found = 2 date_has_invalid_format = 3 date_inconsistency = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. "display cl_demo_output=>display( EXPORTING data = working_days name = 'Holiday days of this year' ).
  "get holiday calendar
  SELECT SINGLE hocid FROM tfacd  "#EC CI_BYPASS
      WHERE ident = @factory_calendar
      INTO @DATA(holiday_calendar).

  DATA holidays  TYPE STANDARD TABLE OF iscal_day.

  "get holidays
  CALL FUNCTION 'HOLIDAY_GET'
    EXPORTING
      holiday_calendar           = holiday_calendar
      factory_calendar           = factory_calendar
      date_from                  = begin_date
      date_to                    = end_date
    TABLES
      holidays                   = holidays
    EXCEPTIONS
      factory_calendar_not_found = 1
      holiday_calendar_not_found = 2
      date_has_invalid_format    = 3
      date_inconsistency         = 4
      OTHERS                     = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  "display
  cl_demo_output=>display(
    EXPORTING
      data = working_days
      name = 'Holiday days of this year'
  ).

We had two different useful data from SAP. You may use both of them up to your case.

We mentioned 3 different functions which are good to keep in mind:

  1. RP_CALC_DATE_IN_INTERVAL: Used to do add/subtract operations on date
  2. RKE_SELECT_FACTDAYS_FOR_PERIOD : Gives working days in a date range
  3. HOLIDAY_GET : Give holidays in a date range

Leave a Reply

Your email address will not be published. Required fields are marked *