Apr 24, 2016

Function Modules related on Date calculations

By Avinash Kodarapu

Function Modules
Description
/SAPNEA/J_SC_CONVERT_DATE
Covert Date in Japanese Format
/SDF/CMO_DATETIME_DIFFERENCE
Calculates difference between two dates/timestamps.
ADD_TIME_TO_DATE
Add time to date
BKK_ADD_MONTH_TO_DATE
Add months to date
BKK_ADD_WORKINGDAY

BWSO_DATE_GET_FIRST_WEEKDAY
Gives the First date of the week for the given date
CACS_DATE_GET_TIMESTAMP
Concatenates the date and time format 11-12-2008 10:11:11
CACS_DATE_GET_YEAR_MONTH
Gives the Year and month for the given date
CACS_TIMESTAMP_GET_DATE
Get date and time from Timestamp
CONVERT_TIME_TERMS

DATE_CHECK_WORKINGDAY
Check the day is working day or not for given date & calendar id
DATE_CHECK_WORKINGDAY_MULTIPLE
Check the day is working day or not for given date in muliple calendar id
DATE_COMPUTE_DAY
Gives the day number in the week (EX 1-Mon,2-TUE etc)
DATE_CONVERT_TO_WORKINGDAY

DATE_TO_DAY
Gives the day of the date(Ex Monday, Tuesday etc)
DATE_TO_DAY
Get the day of the date (Ex Mon.Tue etc)
F4_DATE
FM for F4 help for Date
FIMA_DAYS_AND_MONTHS_AND_YEARS

FIMA_DECIMAL_MONTHS_AND_YEARS
Gives the difference between the dates in decimal foemat of number of month & years
GET_WEEK_INFO_BASED_ON_DATE
Get the week Number, Sunday date & Monday date for the given date
HR_99S_DATE_ADD_SUB_DURATION
Add/Subtract months(M)/days(D)/years(Y) to given date to get the future date
HR_99S_DATE_MINUS_TIME_UNIT
Subtract months(M)/days(D)/years(Y) to given date to get the future date
HR_99S_DATE_PLUS_TIME_UNIT
Add months(M)/days(D)/years(Y) to given date to get the future date
HR_99S_INTERVAL_BETWEEN_DATES
Gives the difference between the dates in days, weeks, years
HR_99S_MONTHS_BETWEEN_DATES
*  Returns months between two dates. Both dates are included in the

*  computation. An individual prorata is made for each month

*  Meaning of parameter p_compl:

*  'B' accumulate all complete months and in/complete first month

*  'E' accumulate all complete months and in/complete last month

*  ' ' accumulate all months (incomplete and complete)

*  'X' accumulate only complete months
HR_CALC_YEAR_MONTH_DAY
Gives the difference between the dates in days, weeks, years
HR_E_NUM_OF_DAYS_OF_MONTH
Number of days in the given month
HR_HCP_ADD_MONTH_TO_DATE
Add months to date
HR_IN_GET_DATE_COMPONENTS
Splits rge given date in DAY, MONTH, YEAR, Month Small TEXT & Month Long TEXT
HR_MONTHS_BETWEEN_TWO_DATES
Gives months bteween the two dates
HR_SGPBS_YRS_MTHS_DAYS
Gives the difference between dates in Day,Months, years 7 calendar Days
HRAR_SUBTRACT_MONTH_TO_DATE
Subtract's one month from the date
IDWT_READ_MONTH_TEXT
Read the month Text
LAST_WEEK
Get the Last week Number, Sunday date & Monday date for the given Year and Week number
LOOK_FOR_TIME_TABLE

MONTH_NAMES_GET
Gives the list of 12 months text in the given language
MONTH_PLUS_DETERMINE
Add months to date
MONTHS_BETWEEN_TWO_DATES
Gives months bteween the two dates
MONTHS_BETWEEN_TWO_DATES_NEW

NEXT_WEEK
Get the Next week Number, Sunday date & Monday date for the given Year and Week number
NUMBER_OF_DAYS_PER_MONTH_GET
Gives you the number of days per month considering the Year
OIL_LAST_DAY_OF_MONTH
Gives the last day of the month
OIL_LAST_DAY_OF_PREVIOUS_MONTH
Gives the last day of the prevous month
OIL_MONTH_GET_FIRST_LAST
Gives the month first day date & last day date
RE_ADD_MONTH_TO_DATE
Add months to date
RH_GET_DATE_DAYNAME
Gives the day number in the week and day text
RH_PM_CONVERT_DATE_TO_MONTH

RH_PM_CONVERT_DATE_TO_MONTH
Gives the difference between the dates in month
RH_PM_CONVERT_DATE_TO_YEAR

RP_CALC_DATE_IN_INTERVAL
To calculate the new date by adding/Sub days, months & Year
SG_PS_GET_LAST_DAY_OF_MONTH
Last day of the month
TIME_CONVERSION

TM_DATE_CHECK_WORKINGDAY

WDKAL_DATE_ADD_FKDAYS

DATE_CHECK_PLAUSIBILITY

  DATE_IN_FUTURE
Calculate Future Dates
  DURATION_DETERMINE 
Calendar: Determine duration between tw
DELTA_TIME_DAY_HOUR 
Difference Between  Date & Time
CALCULATE_DATE
Year entry: 2-charact. to
HR_SEN_CRULE_0100_DATE
Calculation of a Date (1 Year = 12 Months, 1 Month = 30 Days)
DATE_2D_TO_4D_CONVERSION

Apr 6, 2016

Tree List report in ABAP

by Fareez Ahamed

Reports which has the capability to drill down to multiple levels are of good use. It serves as a simple overview as well as more detailed report when you want to look into a particular record in detail. SAP makes it easy to create Tree Listreports with Function Modules RS_TREE_CONSTRUCT and  RS_TREE_LIST_DISPLAY.
Multi-level Tree List Report
Now we are going to develop a Tree List report with three levels of details. First level will be the the list of Customers, second level will be the list of Sales Order Headers for that Customer and third level will be the list of Items in a particularSales Order.

The task can be broken into three steps,
  • Load the data
  • Prepare Node Table
  • Display the Tree List
Before going into the steps, we are going to declare the variables needed for our program. We have to store the three levels of data in three different internal tables and the first thing to do is defining the types for those three structures.
types:
      "Customer Structure (Level 1)
      begin of s_kna1,
        kunnr type kna1-kunnr,
        land1 type kna1-land1,
        name1 type kna1-name1,
        ort01 type kna1-ort01,
      end of s_kna1,

      "Sales order header structure (Level 2)
      begin of s_vbak,
        vbeln type vbak-vbeln,
        erdat type vbak-erdat,
        erzet type vbak-erzet,
        netwr type vbak-netwr,
        waerk type vbak-waerk,
        kunnr type vbak-kunnr,
      end of s_vbak,

      "Sales order items structure (Level 3)
      begin of s_vbap,
        vbeln type vbap-vbeln,
        posnr type vbap-posnr,
        matnr type vbap-matnr,
        netwr type vbap-netwr,
        waerk type vbap-waerk,
      end of s_vbap.
And now declare variables with above defined types...
data:
      "Internal tables and Work Areas
      "for all three levels of data
      gt_kna1 type standard table of s_kna1,
      gs_kna1 type s_kna1,
      gt_vbak type standard table of s_vbak,
      gs_vbak type s_vbak,
      gt_vbap type standard table of s_vbap,
      gs_vbap type s_vbap,

      "Table which represents the data
      "in tree format and passed to RS_TREE_CONSTRUCT
      gt_node type standard table of snodetext,
      gs_node type snodetext. 
We have colors in our report and it is good practice to declare them as constants, so that we will be able to change them without messing up the program. The values comes from the Type Pool col.
constants:
      c_col_key   type c length 1 value col_key,
      c_col_data  type c length 1 value col_normal,
      c_col_curr  type c length 1 value col_total,
      c_col_kunnr type c length 1 value col_key,
      c_col_vbeln type c length 1 value col_positive,
      c_col_posnr type c length 1 value col_group.
As I mentioned the program is just three steps...
start-of-selection.

  perform load_all_data.

  perform prepare_node_table.

  perform display_list_tree.

Load the data

We have to load the list of Customers into gt_kna1, followed by corresponding records of Sales Order Headers and Sales Order Items into gt_vbak and gt_vbap respectively.
form load_all_data .

  "Read Customer data (Level 1)
  select
    kunnr
    land1
    name1
    ort01
    from kna1
    into table gt_kna1
    up to 50 rows.

  "Read corresponding Sales Order headers (Level 2)
  select
    vbeln
    erdat
    erzet
    netwr
    waerk
    kunnr
    from vbak
    into table gt_vbak
    for all entries in gt_kna1
    where
      kunnr = gt_kna1-kunnr.

  "Read corresponding Sales Order items (Level 3)
  select
    vbeln
    posnr
    matnr
    netwr
    waerk
    from vbap
    into table gt_vbap
    for all entries in gt_vbap
    where
      vbeln = gt_vbap-vbeln.

endform.
That's it and we have loaded the data needed.

Prepare Node Table

We are going to prepare a table of structure snodetext which represents the data we have in tree structure and it will also contain the length and color specifications of fields. Important fields in SNODETEXT are,
  • tlevel - Level of the data in the tree. (Root is considered as Level 1)
  • name - First text field of the tree
  • nlength - Length of the first field
  • color - Color of the first field
  • texttext1, ... text9 - Next fields in order left to right
  • tlengthtlength1, ... tlength9 - Lengths of the corresponding fields
  • tcolortcolor1, ... tcolor9 - Colors of the correspoding fields
So our logic will be,
Add root node tlevel = 1

loop at Customers.

    Add Customer with tlevel = 2

    loop at corresponding Sales Order Headers.

        Add Sales Order Header with tlevel = 3

        loop at corresponding Sales Order Items.

            Add Sales Order Item with tlevel = 4

        endloop.

    endloop.

endloop.
  Now the exact code is as follows.
form prepare_node_table .

  data:
        l_netwr type c length 15.

  "Create a root node
  gs_node-tlevel = 1.
*  gs_node-type   = 'H'.
  gs_node-name   = 'Customers'.
  gs_node-nlength = 20.
  gs_node-color = c_col_key.
  gs_node-text   = 'Customer Sales Report'.
  gs_node-tlength = 50.
  append gs_node to gt_node.


  loop at gt_kna1 into gs_kna1.

    clear gs_node.

    gs_node-tlevel = 2.

    "Customer Number
    gs_node-name   = gs_kna1-kunnr.
    gs_node-nlength = 20.
    gs_node-color = c_col_kunnr.

    "Country code
    gs_node-text   = gs_kna1-land1.
    gs_node-tlength = 5.
    gs_node-tcolor = c_col_data.

    "Customer Name
    gs_node-text1   = gs_kna1-name1.
    gs_node-tlength1 = 40.
    gs_node-tcolor1 = c_col_data.

    "City
    gs_node-text2   = gs_kna1-ort01.
    gs_node-tlength2 = 25.
    gs_node-tcolor2 = c_col_data.

    append gs_node to gt_node.

    loop at gt_vbak into gs_vbak
      where kunnr = gs_kna1-kunnr.

      clear gs_node.

      gs_node-tlevel = 3.

      "Sales Document Number
      gs_node-name   = gs_vbak-vbeln.
      gs_node-nlength = 15.
      gs_node-color = c_col_vbeln.

      "Created date
      gs_node-text   = gs_vbak-erdat.
      gs_node-tlength = 12.
      gs_node-tcolor = c_col_data.

      "Created Time
      gs_node-text1   = gs_vbak-erzet.
      gs_node-tlength1 = 10.
      gs_node-tcolor1 = c_col_data.

      "Net worth
      l_netwr = gs_vbak-netwr.
      gs_node-text2   = l_netwr.
      gs_node-tlength2 = 15.
      gs_node-tcolor2 = c_col_curr.

      "Currency
      gs_node-text3   = gs_vbak-waerk.
      gs_node-tlength3 = 5.
      gs_node-tcolor3 = c_col_curr.

      append gs_node to gt_node.

      loop at gt_vbap into gs_vbap
        where vbeln = gs_vbak-vbeln.

        clear gs_node.

        gs_node-tlevel = 4.

        "Item Number
        gs_node-name   = gs_vbap-posnr.
        gs_node-nlength = 15.
        gs_node-color = c_col_posnr.

        "Material Number
        gs_node-text   = gs_vbap-matnr.
        gs_node-tlength = 12.
        gs_node-tcolor = c_col_data.

        "Net worth
        l_netwr = gs_vbap-netwr.
        gs_node-text1   = l_netwr.
        gs_node-tlength1 = 15.
        gs_node-tcolor1 = c_col_curr.

        "Currency
        gs_node-text2   = gs_vbap-waerk.
        gs_node-tlength2 = 5.
        gs_node-tcolor2 = c_col_curr.

        append gs_node to gt_node.

      endloop.

    endloop.

  endloop.

endform.

Display Tree List

Now to display the Tree List, you just have to call the Function Modules RS_TREE_CONSTRUCT and RS_TREE_LIST_DISPLAY sequentially.  RS_TREE_CONSTRUCT takes gt_node as input and processes it internally and fills in the necessary fields of that internal table while RS_TREE_LIST_DISPLAY is for displaying the Tree List in the screen.
form display_list_tree .

  call function 'RS_TREE_CONSTRUCT'
*   EXPORTING
*     INSERT_ID                = '000000'
*     RELATIONSHIP             = ' '
*     LOG                      =
    tables
      nodetab                  = gt_node
   exceptions
     tree_failure             = 1
     id_not_found             = 2
     wrong_relationship       = 3
     others                   = 4
            .
  if sy-subrc <> 0.
    write sy-subrc.
  endif.

  call function 'RS_TREE_LIST_DISPLAY'
   exporting
     callback_program                = sy-repid.

endform.                    
That's it :) The entire program is below.
report  zfar_list_tree.

type-pools:
  col. "For color constants

types:
      "Customer Structure (Level 1)
      begin of s_kna1,
        kunnr type kna1-kunnr,
        land1 type kna1-land1,
        name1 type kna1-name1,
        ort01 type kna1-ort01,
      end of s_kna1,

      "Sales order header structure (Level 2)
      begin of s_vbak,
        vbeln type vbak-vbeln,
        erdat type vbak-erdat,
        erzet type vbak-erzet,
        netwr type vbak-netwr,
        waerk type vbak-waerk,
        kunnr type vbak-kunnr,
      end of s_vbak,

      "Sales order items structure (Level 3)
      begin of s_vbap,
        vbeln type vbap-vbeln,
        posnr type vbap-posnr,
        matnr type vbap-matnr,
        netwr type vbap-netwr,
        waerk type vbap-waerk,
      end of s_vbap.

data:
      "Internal tables and Work Areas
      "for all three levels of data
      gt_kna1 type standard table of s_kna1,
      gs_kna1 type s_kna1,
      gt_vbak type standard table of s_vbak,
      gs_vbak type s_vbak,
      gt_vbap type standard table of s_vbap,
      gs_vbap type s_vbap,

      "Table which represents the data
      "in tree format and passed to RS_TREE_CONSTRUCT
      gt_node type standard table of snodetext,
      gs_node type snodetext.

constants:
      c_col_key   type c length 1 value col_key,
      c_col_data  type c length 1 value col_normal,
      c_col_curr  type c length 1 value col_total,
      c_col_kunnr type c length 1 value col_key,
      c_col_vbeln type c length 1 value col_positive,
      c_col_posnr type c length 1 value col_group.

start-of-selection.

  perform load_all_data.

  perform prepare_node_table.

  perform display_list_tree.

form load_all_data .

  "Read Customer data (Level 1)
  select
    kunnr
    land1
    name1
    ort01
    from kna1
    into table gt_kna1
    up to 50 rows.

  "Read corresponding Sales Order headers (Level 2)
  select
    vbeln
    erdat
    erzet
    netwr
    waerk
    kunnr
    from vbak
    into table gt_vbak
    for all entries in gt_kna1
    where
      kunnr = gt_kna1-kunnr.

  "Read corresponding Sales Order items (Level 3)
  select
    vbeln
    posnr
    matnr
    netwr
    waerk
    from vbap
    into table gt_vbap
    for all entries in gt_vbap
    where
      vbeln = gt_vbap-vbeln.


endform.                    " LOAD_ALL_DATA

form prepare_node_table .

  data:
        l_netwr type c length 15.

  "Create a root node
  gs_node-tlevel = 1.
*  gs_node-type   = 'H'.
  gs_node-name   = 'Customers'.
  gs_node-nlength = 20.
  gs_node-color = c_col_key.
  gs_node-text   = 'Customer Sales Report'.
  gs_node-tlength = 50.
  append gs_node to gt_node.


  loop at gt_kna1 into gs_kna1.

    clear gs_node.

    gs_node-tlevel = 2.

    "Customer Number
    gs_node-name   = gs_kna1-kunnr.
    gs_node-nlength = 20.
    gs_node-color = c_col_kunnr.

    "Country code
    gs_node-text   = gs_kna1-land1.
    gs_node-tlength = 5.
    gs_node-tcolor = c_col_data.

    "Customer Name
    gs_node-text1   = gs_kna1-name1.
    gs_node-tlength1 = 40.
    gs_node-tcolor1 = c_col_data.

    "City
    gs_node-text2   = gs_kna1-ort01.
    gs_node-tlength2 = 25.
    gs_node-tcolor2 = c_col_data.

    append gs_node to gt_node.

    loop at gt_vbak into gs_vbak
      where kunnr = gs_kna1-kunnr.

      clear gs_node.

      gs_node-tlevel = 3.

      "Sales Document Number
      gs_node-name   = gs_vbak-vbeln.
      gs_node-nlength = 15.
      gs_node-color = c_col_vbeln.

      "Created date
      gs_node-text   = gs_vbak-erdat.
      gs_node-tlength = 12.
      gs_node-tcolor = c_col_data.

      "Created Time
      gs_node-text1   = gs_vbak-erzet.
      gs_node-tlength1 = 10.
      gs_node-tcolor1 = c_col_data.

      "Net worth
      l_netwr = gs_vbak-netwr.
      gs_node-text2   = l_netwr.
      gs_node-tlength2 = 15.
      gs_node-tcolor2 = c_col_curr.

      "Currency
      gs_node-text3   = gs_vbak-waerk.
      gs_node-tlength3 = 5.
      gs_node-tcolor3 = c_col_curr.

      append gs_node to gt_node.

      loop at gt_vbap into gs_vbap
        where vbeln = gs_vbak-vbeln.

        clear gs_node.

        gs_node-tlevel = 4.

        "Item Number
        gs_node-name   = gs_vbap-posnr.
        gs_node-nlength = 15.
        gs_node-color = c_col_posnr.

        "Material Number
        gs_node-text   = gs_vbap-matnr.
        gs_node-tlength = 12.
        gs_node-tcolor = c_col_data.

        "Net worth
        l_netwr = gs_vbap-netwr.
        gs_node-text1   = l_netwr.
        gs_node-tlength1 = 15.
        gs_node-tcolor1 = c_col_curr.

        "Currency
        gs_node-text2   = gs_vbap-waerk.
        gs_node-tlength2 = 5.
        gs_node-tcolor2 = c_col_curr.

        append gs_node to gt_node.

      endloop.

    endloop.

  endloop.

endform.                    " PREPARE_NODE_TABLE

form display_list_tree .

  call function 'RS_TREE_CONSTRUCT'
*   EXPORTING
*     INSERT_ID                = '000000'
*     RELATIONSHIP             = ' '
*     LOG                      =
    tables
      nodetab                  = gt_node
   exceptions
     tree_failure             = 1
     id_not_found             = 2
     wrong_relationship       = 3
     others                   = 4
            .
  if sy-subrc <> 0.
    write sy-subrc.
  endif.

  call function 'RS_TREE_LIST_DISPLAY'
   exporting
     callback_program                = sy-repid.

endform.                    " DISPLAY_LIST_TREE
From http://sap.fareez.info/blog/abap/how-to-create-tree-list-report-in-abap/