Está en la página 1de 9

*&---------------------------------------------------------------------*

*& Report ZPRUEBAS_CONDICIONES


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZPRUEBAS_CONDICIONES NO STANDARD PAGE HEADING LINE-SIZE 80.

* Variable para trabajar con ALV


tables sscrfields.
TYPE-POOLS: slis.
DATA: g_repid TYPE sy-repid.
DATA: gx_variant LIKE disvariant,
g_variant LIKE disvariant,
g_save(1) TYPE c,
g_default(1) TYPE c,
g_exit(1) TYPE c.

DATA: mi_fieldcat TYPE slis_t_fieldcat_alv.


DATA: mi_sort TYPE slis_t_sortinfo_alv.
data: mi_layout TYPE slis_layout_alv.

data: linea type i.


* Para cargas desde Excel
TYPES KCDE_ROW_COL(4) TYPE N.
TYPES: KCDE_INTERN_VALUE(32).
TYPES: BEGIN OF KCDE_INTERN_STRUC,
ROW TYPE KCDE_ROW_COL,
COL TYPE KCDE_ROW_COL,
VALUE TYPE KCDE_INTERN_VALUE,
END OF KCDE_INTERN_STRUC.
TYPES: KCDE_INTERN TYPE KCDE_INTERN_STRUC OCCURS 0.
DATA: EXCEL TYPE KCDE_INTERN,
W_EXCEL TYPE KCDE_INTERN_STRUC.

DATA texto(100).

* At least the key fields of the conditiontable


* (here 800) must be filled in structure ls_komg.
DATA:ls_komk like komk,
ls_komg like komg,
ls_komv like komv,
ls_komp like komp,
lt_komv type table of komv,
lv_new_record.

*TYPES: BEGIN OF ST_TARIF,


* KSCHL TYPE KOMV-KSCHL,
* VKORG TYPE KOMG-VKORG,
* VTWEG TYPE KOMG-VTWEG,
* MATNR TYPE KOMG-MATNR,
* KBETR TYPE KOMV-KBETR,
* WAERS TYPE KOMV-WAERS,
* KPEIN TYPE KOMV-KPEIN,
* KMEIN TYPE KOMV-KMEIN,
* DATAB TYPE A004-DATAB,
* DATBI TYPE A004-DATBI,
*END OF ST_TARIF.
*
*DATA: GT_TARIF TYPE TABLE OF ST_TARIF,
* LT_TARIF TYPE ST_TARIF.

DATA: BEGIN OF GT_TARIF OCCURS 0,


KSCHL like KOMV-KSCHL,
VKORG LIKE KOMG-VKORG,
VTWEG LIKE KOMG-VTWEG,
MATNR LIKE KOMG-MATNR,
KBETR LIKE KOMV-KBETR,
WAERS LIKE KOMV-WAERS,
KPEIN LIKE KOMV-KPEIN,
KMEIN LIKE KOMV-KMEIN,
DATAB like A004-DATAB,
DATBI LIKE A004-DATBI,
END OF GT_TARIF.

DATA: LT_TARIF LIKE LINE OF GT_TARIF.

PARAMETERS:
NOM_FIC LIKE RLGRAP-FILENAME OBLIGATORY,
p_vari LIKE disvariant-variant. " ALV Variant

AT SELECTION-SCREEN ON VALUE-REQUEST FOR NOM_FIC.


* achtung: die hilfe zeigt das dateisystem des präsentationsservers
MESSAGE I041(DU).
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_FILENAME = NOM_FIC
DEF_PATH = '/'
MASK = ',*.*,*.*.'
MODE = 'O'
TITLE = TEXT-DSP
IMPORTING
FILENAME = NOM_FIC
EXCEPTIONS
INV_WINSYS = 01
NO_BATCH = 02
SELECTION_CANCEL = 03
SELECTION_ERROR = 04.

INITIALIZATION.
g_repid = sy-repid.
g_save = 'A'.
PERFORM variant_init.
* Get default variant
gx_variant = g_variant.
CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
i_save = g_save
CHANGING
cs_variant = gx_variant
EXCEPTIONS
not_found = 2.
IF sy-subrc = 0.
p_vari = gx_variant-variant.
ENDIF.
* Process on value request
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
PERFORM f4_for_variant.

* PAI
AT SELECTION-SCREEN.
PERFORM pai_of_selection_screen.

start-of-SELECTION.

* Cargamos el fichero desde Excel


CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'
EXPORTING
FILENAME = NOM_FIC
* Migracion Enterprise
I_BEGIN_COL = 1
I_BEGIN_ROW = 2 "Saltamos primera linea
I_END_COL = 50
I_END_ROW = 25000
* RANGE_BEGIN = 'A1'
* RANGE_END = 'O9999'
* Migracion Enterprise
TABLES
INTERN = EXCEL
EXCEPTIONS
OTHERS = 1.
CLEAR GT_tarif.
CLEAR LT_TARIF.
REFRESH GT_tarif.
LINEA = 1.
LOOP AT EXCEL INTO W_EXCEL.
IF LINEA NE W_EXCEL-ROW.
APPEND LT_TARIF to GT_tarif .
CLEAR LT_tarif.
LINEA = W_EXCEL-ROW.
ENDIF.
IF W_EXCEL-COL = 1.
LT_TARIF-KSCHL = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 2.
LT_TARIF-VKORG = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 3.
LT_TARIF-VTWEG = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 4.
LT_TARIF-MATNR = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 5.
REPLACE ',' WITH '.' INTO W_EXCEL-VALUE.
LT_TARIF-KBETR = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 6.
LT_TARIF-WAERS = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 7.
LT_TARIF-KPEIN = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 8.
call function 'CONVERSION_EXIT_CUNIT_INPUT'
EXPORTING
input = w_excel-value
IMPORTING
output = LT_TARIF-KMEIN
EXCEPTIONS
unit_not_found = 1
others = 2.
* LT_TARIF-KMEIN = W_EXCEL-VALUE.
ENDIF.
IF W_EXCEL-COL = 9.
LT_TARIF-DATAB+0(4) = W_EXCEL-VALUE+6(4).
LT_TARIF-DATAB+4(2) = W_EXCEL-VALUE+3(2).
LT_TARIF-DATAB+6(2) = W_EXCEL-VALUE+0(2).
ENDIF.
IF W_EXCEL-COL = 10.
LT_TARIF-DATBI+0(4) = W_EXCEL-VALUE+6(4).
LT_TARIF-DATBI+4(2) = W_EXCEL-VALUE+3(2).
LT_TARIF-DATBI+6(2) = W_EXCEL-VALUE+0(2).
ENDIF.
ENDLOOP.
APPEND LT_tarif to gt_tarif.

end-of-selection.
* Generar el catalogo del ALV
PERFORM fill_fieldcat USING mi_fieldcat.
PERFORM fill_layout using mi_layout.

* Call ABAP List Viewer (ALV)


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
it_fieldcat = mi_fieldcat
it_sort = mi_sort
I_CALLBACK_USER_COMMAND = 'FUNCION_ALV'
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
i_save = g_save
is_variant = g_variant
is_layout = mi_layout
TABLES
t_outtab = gt_tarif.

*&---------------------------------------------------------------------*
*& Form funcion_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM funcion_alv USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
case r_ucomm.
when 'PRUEBA' or 'DATA_SAVE'. break-point. perform crear_condicion.
when OTHERS. break-point.
endcase.
ENDFORM. "funcion_alv

*&---------------------------------------------------------------------*
*& Form set_pf_status
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM set_pf_status using rt_extab type slis_t_extab.
set pf-status 'MAIN'.
ENDFORM. "set_pf_status
*&---------------------------------------------------------------------*
*& Form fill_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->E01_LT_FIELDCAT text
*----------------------------------------------------------------------*
FORM fill_fieldcat USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'


EXPORTING
i_program_name = g_repid
i_internal_tabname = 'GT_TARIF'
i_inclname = g_repid
CHANGING
ct_fieldcat = E01_LT_FIELDCAT.
* I_INCLNAME ='
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =

* EXCEPTIONS
* INCONSISTENT_INTERFACE = 1
* PROGRAM_ERROR = 2
* OTHERS = 3
.
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. "FILL_FIELDCAT
*---------------------------------------------------------------------*
* FORM F4_FOR_VARIANT *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM f4_for_variant.
*
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
is_variant = g_variant
i_save = g_save
* it_default_fieldcat =
IMPORTING
e_exit = g_exit
es_variant = gx_variant
EXCEPTIONS
not_found = 2.
IF sy-subrc = 2.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
IF g_exit = space.
p_vari = gx_variant-variant.
ENDIF.
ENDIF.
ENDFORM. "F4_FOR_VARIANT
*&---------------------------------------------------------------------*
*& Form PAI_OF_SELECTION_SCREEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM pai_of_selection_screen.
*
IF NOT p_vari IS INITIAL.
MOVE g_variant TO gx_variant.
MOVE p_vari TO gx_variant-variant.
CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
EXPORTING
i_save = g_save
CHANGING
cs_variant = gx_variant.
g_variant = gx_variant.
ELSE.
PERFORM variant_init.
ENDIF.
ENDFORM. " PAI_OF_SELECTION_SCREEN
*&---------------------------------------------------------------------*
*& Form VARIANT_INIT
*&---------------------------------------------------------------------*
FORM variant_init.
CLEAR g_variant.
g_variant-report = g_repid.
ENDFORM. " VARIANT_INIT
*&---------------------------------------------------------------------*
*& Form FILL_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_MI_LAYOUT text
*----------------------------------------------------------------------*
FORM FILL_LAYOUT USING P_MI_LAYOUT TYPE slis_layout_alv.
* p_mi_layout-edit = ' '.
* p_mi_layout-no_input = 'X'.
ENDFORM. " FILL_LAYOUT

*&---------------------------------------------------------------------*
*& Form CREAR_CONDICION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM CREAR_CONDICION.

loop at GT_TARIF.
* clear ls_komg.
* ls_komg-VKORG = '1003'.
* ls_komg-VTWEG = '00'.
* ls_komg-MATNR = '000000000000000631'.
clear ls_komg.
ls_komg-VKORG = GT_TARIF-VKORG.
ls_komg-VTWEG = GT_TARIF-VTWEG.
* ls_komg-MATNR = GT_TARIF-MATNR.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
INPUT = GT_TARIF-MATNR
IMPORTING
OUTPUT = ls_komg-MATNR
EXCEPTIONS
LENGTH_ERROR = 1
OTHERS = 2
.

* clear ls_komv.
* ls_komv-kappl = 'V '. " Application V = Sales
* ls_komv-kschl = 'PR00'. " Condition type
* ls_komv-waers = 'EUR'. " Currency
* ls_komv-kmein = 'CJ'. " Unit of measurement
* ls_komv-kpein = '1'.
* ls_komv-krech = 'M'. " calculation type;
* "M = Quantity - monthy price
* ls_komv-kbetr = '97.22'. " new condition value
* append ls_komv to lt_komv.
refresh lt_komv.
clear ls_komv.
ls_komv-kappl = 'V '. " Application V = Sales
ls_komv-kschl = GT_TARIF-KSCHL.
ls_komv-waers = GT_TARIF-WAERS.
ls_komv-kmein = GT_TARIF-KMEIN.
ls_komv-kpein = GT_TARIF-KPEIN.
ls_komv-krech = 'C'. " calculation type;
"Segun la clase de condicion
ls_komv-kbetr = GT_TARIF-KBETR.
append ls_komv to lt_komv.

* call function 'RV_CONDITION_COPY'


* exporting
* application = 'V'
* condition_table = '004' " cond. table
* condition_type = 'PR00' " cond. type
* date_from = '20161101' " valid on
* date_to = '20161105' " valid to
* enqueue = 'X' " lock entry
* i_komk = ls_komk
* i_komp = ls_komp
* key_fields = ls_komg " key fields
* maintain_mode = 'A' " A= create
* " B= change,
* " C= display
* " D= create
* no_authority_check = 'X'
* keep_old_records = 'X'
* overlap_confirmed = 'X'
* no_db_update = space

call function 'RV_CONDITION_COPY'


exporting
application = 'V'
condition_table = '004' " cond. table
condition_type = GT_TARIF-KSCHL
date_from = GT_TARIF-DATAB
date_to = GT_TARIF-DATBI
enqueue = 'X' " lock entry
i_komk = ls_komk
i_komp = ls_komp
key_fields = ls_komg " key fields
maintain_mode = 'A' " A= create
" B= change,
" C= display
" D= create
no_authority_check = 'X'
keep_old_records = 'X'
overlap_confirmed = 'X'
no_db_update = space
importing
e_komk = ls_komk
e_komp = ls_komp
new_record = lv_new_record
tables
copy_records = lt_komv
EXCEPTIONS
ENQUEUE_ON_RECORD = 1
INVALID_APPLICATION = 2
INVALID_CONDITION_NUMBER = 3
INVALID_CONDITION_TYPE = 4
NO_AUTHORITY_EKORG = 5
NO_AUTHORITY_KSCHL = 6
NO_AUTHORITY_VKORG = 7
NO_SELECTION = 8
TABLE_NOT_VALID = 9
NO_MATERIAL_FOR_SETTLEMENT = 10
NO_UNIT_FOR_PERIOD_COND = 11
NO_UNIT_REFERENCE_MAGNITUDE = 12
INVALID_CONDITION_TABLE = 13
OTHERS = 14.
call function 'RV_CONDITION_SAVE'.
call function 'RV_CONDITION_RESET'.
* necessary to write data
commit work.
ENDLOOP.
ENDFORM. "CREAR_CONDICION