antelio
11/6/2015 - 7:01 PM

ZR_BR_TAXCODES

ZR_BR_TAXCODES

*&---------------------------------------------------------------------*
*& Report  ZR_BR_TAXCODES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zr_br_taxcodes.

TYPES:
  BEGIN OF ztaxb_mwskz,
    mwskz TYPE mwskz,
    text1 TYPE text1_007s,
  END OF ztaxb_mwskz,

  ztaxb_tt_mwskz TYPE TABLE OF ztaxb_mwskz.

TABLES: t007a.
PARAMETERS:
  p_land1 TYPE land1 OBLIGATORY DEFAULT 'BR',
  p_kalsm TYPE kalsm_d MODIF ID ro.

SELECT-OPTIONS:
  s_mwskz FOR t007a-mwskz.

DATA: "Dynamic Table
  dy_table TYPE REF TO data.

DATA: "ALV Data
  t_fcat    TYPE lvc_t_fcat,
  t_filters TYPE lvc_t_filt,
  w_layout  TYPE lvc_s_layo.

DATA: "Tax Data
  t_tax_codes TYPE ztaxb_tt_mwskz.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-group1 = 'RO'.
      screen-input = 0.
      MODIFY SCREEN.            " Disable for input.</b>
    ENDIF.
    %_p_land1_%_app_%-text = 'País'.
    %_p_kalsm_%_app_%-text = 'Esq. Cálculo'.
    %_s_mwskz_%_app_%-text = 'Cod. Impost'.

  ENDLOOP.


INITIALIZATION.
  PERFORM get_tax_codes
    USING p_land1 sy-langu p_kalsm t_tax_codes.

START-OF-SELECTION.
  PERFORM get_tax_codes
    USING p_land1 sy-langu p_kalsm t_tax_codes.

  PERFORM build_fcat
    USING t_tax_codes t_fcat.
  PERFORM build_dy_table
    USING t_fcat dy_table.
  PERFORM fill_tax_data
    USING t_tax_codes dy_table.


END-OF-SELECTION.
  PERFORM alv_output
    USING t_fcat w_layout t_filters dy_table.


*&---------------------------------------------------------------------*
*&      Form  get_tax_codes
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->VALUE(LAND)       text
*      -->VALUE(LANGU)      text
*      -->VALUE(KALSM)      text
*      -->VALUE(TAX_CODES)  text
*----------------------------------------------------------------------*
FORM get_tax_codes
  USING
      VALUE(land) TYPE land1
      VALUE(langu) TYPE sy-langu
      kalsm TYPE kalsm_d
      tax_codes TYPE ztaxb_tt_mwskz.

  IF land IS INITIAL. land = 'BR'. ENDIF.
  SELECT SINGLE kalsm INTO kalsm
    FROM t005 WHERE land1 = land.

  IF sy-subrc IS NOT INITIAL.
    CLEAR: kalsm, tax_codes[].
    EXIT.
  ENDIF.

  SELECT mwskz
    FROM t007a
    INTO CORRESPONDING FIELDS OF TABLE tax_codes
    WHERE kalsm = kalsm.

  DATA: t_t007s TYPE TABLE OF t007s,
        w_t007s LIKE LINE OF t_t007s.
  FIELD-SYMBOLS <tax_codes> LIKE LINE OF tax_codes.
  SELECT * FROM t007s INTO TABLE t_t007s
    WHERE spras = langu AND kalsm = kalsm.
  LOOP AT tax_codes ASSIGNING <tax_codes>.
    READ TABLE t_t007s INTO w_t007s
      WITH KEY mwskz = <tax_codes>-mwskz kalsm = kalsm.
    IF sy-subrc IS INITIAL.
      <tax_codes>-text1 = w_t007s-text1.
    ENDIF.
  ENDLOOP.

ENDFORM.                    "get_tax_codes
*&---------------------------------------------------------------------*
*&      Form  ALV_OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FIELD_CATALOG  text
*      -->P_P_LAYOUT  text
*      -->P_P_FILTERS  text
*      -->P_DY_TABLE  text
*----------------------------------------------------------------------*
FORM alv_output
  USING
    p_fcat TYPE lvc_t_fcat
    p_layout TYPE lvc_s_layo
    p_filters TYPE lvc_t_filt
    p_dy_table TYPE data.

  FIELD-SYMBOLS:
    <dyn_table> TYPE STANDARD TABLE.

  ASSIGN dy_table->* TO <dyn_table>.
  CHECK <dyn_table> IS ASSIGNED.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
*     I_INTERFACE_CHECK                 = ' '
*     I_BYPASSING_BUFFER                =
*     I_BUFFER_ACTIVE =
*     I_CALLBACK_PROGRAM                = ' '
*     I_CALLBACK_PF_STATUS_SET          = ' '
*     I_CALLBACK_USER_COMMAND           = ' '
*     I_CALLBACK_TOP_OF_PAGE            = ' '
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME                  =
*     I_BACKGROUND_ID = ' '
*     I_GRID_TITLE    =
*     I_GRID_SETTINGS =
      is_layout_lvc   = p_layout
      it_fieldcat_lvc = p_fcat
*     IT_EXCLUDING    =
*     IT_SPECIAL_GROUPS_LVC             =
*     IT_SORT_LVC     =
      it_filter_lvc   = p_filters
*     IT_HYPERLINK    =
*     IS_SEL_HIDE     =
*     I_DEFAULT       = 'X'
*     I_SAVE          = ' '
*     IS_VARIANT      =
*     IT_EVENTS       =
*     IT_EVENT_EXIT   =
*     IS_PRINT_LVC    =
*     IS_REPREP_ID_LVC                  =
*     I_SCREEN_START_COLUMN             = 0
*     I_SCREEN_START_LINE               = 0
*     I_SCREEN_END_COLUMN               = 0
*     I_SCREEN_END_LINE                 = 0
*     I_HTML_HEIGHT_TOP                 =
*     I_HTML_HEIGHT_END                 =
*     IT_ALV_GRAPHICS =
*     IT_EXCEPT_QINFO_LVC               =
*     IR_SALV_FULLSCREEN_ADAPTER        =
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           =
*     ES_EXIT_CAUSED_BY_USER            =
    TABLES
      t_outtab        = <dyn_table>
*   EXCEPTIONS
*     PROGRAM_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.

ENDFORM.                    " ALV_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  BUILD_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_T_TAX_CODES  text
*      -->P_T_FCAT  text
*----------------------------------------------------------------------*
FORM build_fcat  USING    p_t_tax_codes TYPE ztaxb_tt_mwskz
                          p_t_fcat TYPE lvc_t_fcat.

  REFRESH p_t_fcat.
  DATA: w_fcat     LIKE LINE OF p_t_fcat,
        w_tax_code LIKE LINE OF p_t_tax_codes.


*  perform fill_fcat_from_ddic using .
  REFRESH p_t_fcat.

  PERFORM get_fcat_dtel USING 'STUNR' w_fcat. APPEND w_fcat TO p_t_fcat.
  PERFORM get_fcat_dtel USING 'KSCHL' w_fcat. APPEND w_fcat TO p_t_fcat.
  PERFORM get_fcat_dtel USING 'VTXTK' w_fcat. APPEND w_fcat TO p_t_fcat.

  LOOP AT p_t_tax_codes INTO w_tax_code.
    PERFORM get_fcat_dtel USING 'CHAR2' w_fcat.
    CONCATENATE 'MWSKZ_' w_tax_code-mwskz INTO w_fcat-fieldname.
    w_fcat-scrtext_s = w_tax_code-mwskz.
    w_fcat-tooltip = w_tax_code-text1.
    w_fcat-just = 'C'.
    APPEND w_fcat TO p_t_fcat.
  ENDLOOP.

ENDFORM.                    " BUILD_FCAT
*&---------------------------------------------------------------------*
*&      Form  BUILD_DY_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_T_TAX_CODES  text
*      -->P_DY_TABLE  text
*----------------------------------------------------------------------*
FORM build_dy_table  USING    p_t_fcat TYPE lvc_t_fcat
                              p_dy_table.


  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog  = p_t_fcat
      i_length_in_byte = 'X'
    IMPORTING
      ep_table         = dy_table.



ENDFORM.                    " BUILD_DY_TABLE
*&---------------------------------------------------------------------*
*&      Form  FILL_TAX_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_T_TAX_CODES  text
*      -->P_DY_TABLE  text
*----------------------------------------------------------------------*
FORM fill_tax_data  USING    p_t_tax_codes TYPE ztaxb_tt_mwskz
                             p_dy_table.

  FIELD-SYMBOLS:
    <dyn_table> TYPE STANDARD TABLE,
    <dyn_wa>    TYPE any,
    <dyn_value> TYPE any.

  ASSIGN dy_table->* TO <dyn_table>.
  CHECK <dyn_table> IS ASSIGNED.

  REFRESH <dyn_table>.
*  APPEND INITIAL LINE TO <dyn_table> ASSIGNING <dyn_wa>.
*
*  CHECK <dyn_wa> IS ASSIGNED.

*  ASSIGN COMPONENT 'KSCHL' OF  STRUCTURE <dyn_wa> TO <dyn_value>.
*  <dyn_value> = 'ZZZZ'.

  DATA: t_t683s TYPE TABLE OF t683s,
        w_t683s LIKE LINE OF t_t683s.
  SELECT * FROM t683s INTO TABLE t_t683s
    WHERE kalsm = p_kalsm
      AND kvewe = 'A'
      AND kappl = 'TX'.
  DELETE t_t683s WHERE kschl IS INITIAL.

  DATA: t_a003 TYPE TABLE OF a003,
        w_a003 LIKE LINE OF t_a003.
  SELECT * FROM a003 INTO TABLE t_a003
    WHERE aland = p_land1
      AND kappl = 'TX'.

  DATA: t_t685a TYPE TABLE OF t685a,
        w_t685a LIKE LINE OF t_t685a.
  SELECT * FROM t685a INTO TABLE t_t685a
    WHERE kappl = 'TX'
      AND koaid = 'D'. "Impostos

  DATA: t_t685t TYPE TABLE OF t685t,
        w_t685t LIKE LINE OF t_t685t.
  SELECT * FROM t685t INTO TABLE t_t685t
    WHERE kvewe = 'A'
      AND kappl = 'TX'
      AND spras = sy-langu.

  DATA: l_column(8).
  LOOP AT t_a003 INTO w_a003.
    READ TABLE <dyn_table> WITH KEY ('KSCHL') = w_a003-kschl ASSIGNING <dyn_wa>.
    IF sy-subrc IS NOT INITIAL.
      READ TABLE t_t685a WITH KEY kschl =  w_a003-kschl TRANSPORTING NO FIELDS.
      IF sy-subrc IS INITIAL.
        APPEND INITIAL LINE TO <dyn_table> ASSIGNING <dyn_wa>.
      ENDIF.
      CHECK <dyn_wa> IS ASSIGNED.
      ASSIGN COMPONENT 'KSCHL' OF STRUCTURE <dyn_wa> TO <dyn_value>.
      CHECK <dyn_value> IS ASSIGNED.
      <dyn_value> = w_a003-kschl.
      READ TABLE t_t685t WITH KEY kschl = w_a003-kschl INTO w_t685t.
      IF sy-subrc IS INITIAL..
        ASSIGN COMPONENT 'VTXTK' OF STRUCTURE <dyn_wa> TO <dyn_value>.
        CHECK <dyn_value> IS ASSIGNED.
        <dyn_value> = w_t685t-vtext.
      ENDIF.
      READ TABLE t_t683s INTO w_t683s WITH KEY kschl = w_a003-kschl.
      IF sy-subrc IS INITIAL.
        ASSIGN COMPONENT 'STUNR' OF STRUCTURE <dyn_wa> TO <dyn_value>.
        CHECK <dyn_value> IS ASSIGNED.
        <dyn_value> = w_t683s-stunr.
      ENDIF.
    ENDIF.

    CHECK <dyn_wa> IS ASSIGNED.
    CONCATENATE 'MWSKZ_' w_a003-mwskz INTO l_column.
    ASSIGN COMPONENT l_column OF STRUCTURE <dyn_wa> TO <dyn_value>.
    CHECK <dyn_value> IS ASSIGNED.
    <dyn_value> = 'X'.
  ENDLOOP.

  SORT  <dyn_table> BY ('STUNR').


ENDFORM.                    " FILL_TAX_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_FCAT_DTEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0312   text
*      -->P_W_FCAT  text
*----------------------------------------------------------------------*
FORM get_fcat_dtel  USING    VALUE(p_dtel)
                             p_w_fcat TYPE lvc_s_fcat.
  CLEAR p_w_fcat.
*  DATA:  ref_cl_abap_elemdescr TYPE REF TO cl_abap_elemdescr,
*         dd_x031l_table TYPE dd_x031l_table,
*         x031l LIKE LINE OF dd_x031l_table.

*  ref_cl_abap_elemdescr ?= cl_abap_typedescr=>describe_by_name( p_dtel ).
*  dd_x031l_table = ref_cl_abap_elemdescr->get_ddic_object( ).
*  READ TABLE dd_x031l_table INDEX 1 INTO x031l.

  DATA: dd04v TYPE dd04v.

  CALL FUNCTION 'DDIF_DTEL_GET'
    EXPORTING
      name          = p_dtel
*     STATE         = 'A'
      langu         = sy-langu
    IMPORTING
*     GOTSTATE      =
      dd04v_wa      = dd04v
*     TPARA_WA      =
    EXCEPTIONS
      illegal_input = 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.
  MOVE-CORRESPONDING dd04v TO p_w_fcat.
  p_w_fcat-fieldname = p_dtel.

ENDFORM.                    " GET_FCAT_DTEL