Cerca nel blog

giovedì 12 novembre 2015

Programma: Creazione Cespiti per Copia da un altro mediante BAPI_FIXEDASSET_CREATE1


Programma Creazione Cespiti per Copia da un altro mediante BAPI_FIXEDASSET_CREATE1

Questo programma custom legge un file .csv che ha la seguente struttuta:

Societa;Cespite;Secondario;Nuova_Classe_Cespite
SOCIETA;2100000005;0001;N2410000SOCIETA;2100000011;0002;N2410000
SOCIETA;2100000033;0002;N2410000

Per ogni cespite letto, viene creato un nuovo cespite principale (Nr. Sec = 0000), mediante la BAPI_FIXEDASSET_CREATE1, e la particolarità è quella di settare e usare il parametro di input della Bapi “REFERENCE” con i valori del vecchio cespite.

*&---------------------------------------------------------------------*
*& Report  ZF15_AA_CREA_ANAG_CESPITI
*&---------------------------------------------------------------------*
*& Description Crea Anagrafica cespiti
*&---------------------------------------------------------------------*

REPORT zf15_aa_crea_anag_cespiti LINE-SIZE 181.

*======================================================================*
*                              INCLUDES                                *
*======================================================================*
*&---------------------------------------------------------------------*
*&  Include           ZF15_AA_CREA_ANAG_CESPITI_TOP
*&---------------------------------------------------------------------*

*======================================================================*
*                               TABLES                                 *
*======================================================================*
TABLES: anla,
        anlb,
        zaa_terminat_sca,
        zaa_proseguiment.
*======================================================================*
*                                TYPES
*======================================================================*
TYPES: BEGIN OF ty_input,
         bukrs TYPE bukrs,
         anln1 TYPE anln1,
         anln2 TYPE anln2,
         anlkl TYPE anlkl,
         row   TYPE i,
       END OF ty_input,
       BEGIN OF ty_output,
         bukrs     TYPE bukrs,
         anln1     TYPE anln1,
         anln2     TYPE anln2,
         anlkl     TYPE anlkl,
         anln1_new TYPE anln1,
         row       TYPE i,
         descr     TYPE char200,
       END OF ty_output.

*======================================================================*
*                  GLOBAL       TABLES                                 *
*======================================================================*
DATA: BEGIN OF tabin OCCURS 0,
        record TYPE string.
DATA: END OF tabin.

DATA: gt_input  TYPE TABLE OF ty_input,
      gt_output TYPE TABLE OF ty_output.

*======================================================================*
*                              CONSTANTS                               *
*======================================================================*

CONSTANTS: gc_x          TYPE char1 VALUE 'X',
           gc_soc        TYPE char3 VALUE 'SOC',
           gc_separatore TYPE c LENGTH 1 VALUE ';'.

*======================================================================*
*                             GLOBAL DATA                              *
*======================================================================*
DATA: dir       TYPE string,
      gv_error  TYPE c LENGTH 1,
      ls_output TYPE ty_output,
      gv_ok     TYPE i,
      gv_tot    TYPE i.

FIELD-SYMBOLS: <fs_input> TYPE ty_input.


*======================================================================*
*                            ALV GRID                                  *
*======================================================================*
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      gs_layout   TYPE slis_layout_alv.
     
     
     
     
*&---------------------------------------------------------------------*
*&  Include           ZF15_AA_CREA_ANAG_CESPITI_SEL
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK 1 WITH FRAME TITLE text-s01. "Percorso file
PARAMETERS: p_ser RADIOBUTTON GROUP a2 USER-COMMAND sele.
PARAMETERS: p_filese  LIKE rlgrap-filename DEFAULT '/'.
PARAMETERS: p_loc RADIOBUTTON GROUP a2  DEFAULT 'X'.
PARAMETERS: p_filelo  LIKE rlgrap-filename DEFAULT 'C:\'.
SELECTION-SCREEN END OF BLOCK 1.

PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.



*&---------------------------------------------------------------------*
*&  Include           ZF15_AA_CREA_ANAG_CESPITI_FORM
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      Form  CHECK_RADIO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM check_radio .

  IF  p_ser IS INITIAL AND NOT
      p_loc  IS INITIAL.
    LOOP AT SCREEN.
      IF screen-name CS 'P_FILESE'.
        screen-input = 0.
        screen-invisible = 1.
        screen-output = 0.
      ENDIF.
      MODIFY SCREEN.
    ENDLOOP.


  ELSEIF p_loc   IS INITIAL AND NOT
         p_ser  IS INITIAL.
    LOOP AT SCREEN.
      IF screen-name CS 'P_FILELO'.
        screen-input = 0.
        screen-invisible = 1.
        screen-output = 0.
      ENDIF.
      MODIFY SCREEN.
    ENDLOOP.

  ENDIF.

ENDFORM.                    " CHECK_RADIO
*&---------------------------------------------------------------------*
*&      Form  READ_FILE
*&---------------------------------------------------------------------*
*      Legge il file da server o da locale
*----------------------------------------------------------------------*
FORM read_file.

  IF p_ser = gc_x.
    PERFORM carica_da_server.
  ELSEIF p_loc EQ gc_x.
    PERFORM carica_da_locale.
  ENDIF.


ENDFORM.                    " read_file
*&---------------------------------------------------------------------*
*&      Form  CARICA_DA_SERVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM carica_da_server .
  DATA: con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
  DATA:  exit     TYPE c LENGTH 1,
         lv_app   TYPE string,
         lv_count TYPE i.

  IF p_filese IS NOT INITIAL.
* Carico file txt da server
    OPEN DATASET p_filese FOR INPUT IN TEXT MODE
                                 ENCODING NON-UNICODE.
*                               WITH SMART LINEFEED.
    IF sy-subrc EQ 0.
      WHILE exit IS INITIAL.
        READ DATASET p_filese INTO tabin-record.
        IF tabin IS INITIAL.
          exit = 'X'.
        ELSE.
          lv_count = strlen( tabin-record ).
          lv_count = lv_count - 1.
          lv_app = tabin-record(lv_count).
          tabin-record = lv_app.
          APPEND tabin.
        ENDIF.
      ENDWHILE.
      CLOSE DATASET p_filese.
    ELSE.
      MESSAGE text-e03 TYPE 'S'. "Errore: Controllare il Naming del File
      gv_error = gc_x.
    ENDIF.
  ELSE.
    gv_error = gc_x.
    MESSAGE text-e03 TYPE 'S'.
  ENDIF.


ENDFORM.                    " CARICA_DA_SERVER
*&---------------------------------------------------------------------*
*&      Form  CARICA_DA_LOCALE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM carica_da_locale .

  DATA: upload_file TYPE string.

  IF NOT  p_filelo IS INITIAL.
    CLEAR  upload_file.

    upload_file =  p_filelo.

    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename                = upload_file
        filetype                = 'ASC'
      TABLES
        data_tab                = tabin
      EXCEPTIONS
        file_open_error         = 1
        file_read_error         = 2
        no_batch                = 3
        gui_refuse_filetransfer = 4
        invalid_type            = 5
        no_authority            = 6
        unknown_error           = 7
        bad_data_format         = 8
        header_not_allowed      = 9
        separator_not_allowed   = 10
        header_too_long         = 11
        unknown_dp_error        = 12
        access_denied           = 13
        dp_out_of_memory        = 14
        disk_full               = 15
        dp_timeout              = 16
        OTHERS                  = 17.
    IF sy-subrc <> 0.
      gv_error = gc_x.
      MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ELSE.
    gv_error = gc_x.
    MESSAGE text-e04 "Errore: Inserire il File
            TYPE 'S'.
  ENDIF.

ENDFORM.                    " CARICA_DA_LOCALE
*&---------------------------------------------------------------------*
*&      Form  ESTRAI_DATI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM estrai_dati .

  FIELD-SYMBOLS: <fs_tabin> LIKE LINE OF tabin.
  DATA: num      TYPE i,
        ls_input TYPE ty_input.
*        filler type c LENGTH 50.
  CHECK gv_error NE gc_x.
  CLEAR num.

  LOOP AT tabin ASSIGNING <fs_tabin>.
    SPLIT <fs_tabin>-record AT gc_separatore INTO  ls_input-bukrs
                                    ls_input-anln1
                                    ls_input-anln2
                                    ls_input-anlkl.
    ADD 1 TO num.
    MOVE num TO ls_input-row.
    APPEND ls_input TO gt_input.
  ENDLOOP.

  " cancello riga etichette
  DELETE gt_input INDEX 1.

  REFRESH tabin.
ENDFORM.                    " ESTRAI_DATI
*&---------------------------------------------------------------------*
*&      Form  CREA_CESPITI
*&---------------------------------------------------------------------*
* CREAZIONE CESPITI PER COPIA
*----------------------------------------------------------------------*
FORM crea_cespiti .

  CHECK  gv_error NE gc_x.

  LOOP AT gt_input ASSIGNING <fs_input>.
    " creazione del cespite nuovo di copia di quello vecchio
    PERFORM call_as01 USING <fs_input>.
  ENDLOOP.


ENDFORM.                    " CREA_CESPITI
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_output .

  DATA: lv_repid LIKE sy-repid.

  CLEAR lv_repid.

  CHECK NOT  gv_error EQ gc_x.

  lv_repid = sy-repid.

  PERFORM: set_layout,
           create_fieldcat.

  SORT gt_output BY anln1 anln2.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_bypassing_buffer     = 'X'
      i_callback_program     = lv_repid
      i_callback_top_of_page = 'TOP_OF_PAGE'
      is_layout              = gs_layout
      it_fieldcat            = gt_fieldcat
    TABLES
      t_outtab               = gt_output
    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.                    " DISPLAY_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
FORM set_layout .
  CLEAR gs_layout.
  gs_layout-colwidth_optimize   = gc_x.
ENDFORM.                    " SET_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  CREATE_FIELDCAT
*&---------------------------------------------------------------------*
*   Creazione lista campi output
*----------------------------------------------------------------------*
FORM create_fieldcat .

  DATA: ls_fieldcat LIKE LINE OF gt_fieldcat.

  REFRESH gt_fieldcat.

  " Num Riga
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ROW'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 1.
  ls_fieldcat-seltext_m = 'Num Riga'.
  ls_fieldcat-seltext_l = 'Num Riga'.
  ls_fieldcat-outputlen = 8.
  APPEND ls_fieldcat TO gt_fieldcat.


  " Società cespite modello
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'BUKRS'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 2.
  ls_fieldcat-seltext_m = 'Società'.
  ls_fieldcat-seltext_l = 'Società'.
  ls_fieldcat-outputlen = 8.
  APPEND ls_fieldcat TO gt_fieldcat.

  " Numero cespite modello
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ANLN1'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 3.
  ls_fieldcat-seltext_m = 'Cespite'.
  ls_fieldcat-seltext_l = 'Cespite'.
  ls_fieldcat-outputlen = 12.
  APPEND ls_fieldcat TO gt_fieldcat.

  " Numero sec cespite modello
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ANLN2'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 4.
  ls_fieldcat-seltext_m = 'Num Sec'.
  ls_fieldcat-seltext_l = 'Num Sec'.
  ls_fieldcat-outputlen = 7.
  APPEND ls_fieldcat TO gt_fieldcat.

  " classe cespite nuovo
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ANLKL'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 5.
  ls_fieldcat-seltext_m = 'Cl cesp New'.
  ls_fieldcat-seltext_l = 'Classe nuovo cesp'.
  ls_fieldcat-outputlen = 10.
  APPEND ls_fieldcat TO gt_fieldcat.

  " numero nuovo cespite
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ANLN1_NEW'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 6.
*  ls_fieldcat-no_zero   = gc_x.
  ls_fieldcat-seltext_m = 'Cesp New'.
  ls_fieldcat-seltext_l = 'Nuovo Cespite'.
  ls_fieldcat-outputlen = 15.
  APPEND ls_fieldcat TO gt_fieldcat.

  " messaggio
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'DESCR'.
  ls_fieldcat-tabname = 'GT_OUTPUT'.
  ls_fieldcat-col_pos   = 7.
  ls_fieldcat-seltext_m = 'Messaggio'.
  ls_fieldcat-seltext_l = 'Messaggio'.
  ls_fieldcat-outputlen = 200.
  APPEND ls_fieldcat TO gt_fieldcat.

ENDFORM.                    " CREATE_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  CALL_AS01
*&---------------------------------------------------------------------*
*   chiamo AS01 per creare il cespite stock di copia
*
*----------------------------------------------------------------------*
FORM call_as01  USING    ps_input   TYPE ty_input.

  DATA: ls_key          TYPE bapi1022_key,
        ls_reference    TYPE bapi1022_reference,
        ls_generaldata  TYPE bapi1022_feglg001,
        ls_generaldatax TYPE bapi1022_feglg001x,
        ls_return       TYPE bapiret2,
        lv_anln1_new    TYPE anln1.

  CLEAR: lv_anln1_new,
         ls_key,
         ls_reference,
         ls_generaldata,
         ls_generaldata,
         ls_return.

  " valorizzazione società nella key:
  MOVE 'SOC' TO ls_key-companycode.
  " valorizzazione cespite modello
  MOVE: 'SOC'         TO ls_reference-companycode,
        ps_input-anln1 TO ls_reference-asset,
        ps_input-anln2 TO ls_reference-subnumber.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ls_reference-asset
    IMPORTING
      output = ls_reference-asset.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ls_reference-subnumber
    IMPORTING
      output = ls_reference-subnumber.

  " valorizzazione classe cespite
  MOVE: ps_input-anlkl  TO ls_generaldata-assetclass,
        'X'         TO ls_generaldatax-assetclass.

  CALL FUNCTION 'BAPI_FIXEDASSET_CREATE1'
    EXPORTING
      key          = ls_key
      reference    = ls_reference
      testrun      = p_test
      generaldata  = ls_generaldata
      generaldatax = ls_generaldatax
    IMPORTING
*     COMPANYCODE  =
      asset        = lv_anln1_new
*     SUBNUMBER    =
*     ASSETCREATED =
      return       = ls_return.


  MOVE-CORRESPONDING ps_input TO ls_output.

  IF ls_return-type EQ 'S'.
    IF p_test IS INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ls_output-descr = text-e02. " Cespite stock non creato
    ENDIF.
    ADD 1 TO gv_ok.
    MOVE lv_anln1_new TO ls_output-anln1_new.
    MOVE ls_return-message TO ls_output-descr.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ls_output-descr = ls_return-message.
  ENDIF.

  APPEND ls_output TO gt_output.

ENDFORM.                    " CALL_AS01
*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
*  Evento TOP OF PAGE per ALV
*----------------------------------------------------------------------*
FORM top_of_page.

  DATA: lt_header TYPE slis_t_listheader,
        ls_header TYPE slis_listheader,
        ld_linesc TYPE c LENGTH 10.

* Titolo
  ls_header-typ  = 'H'.
  ls_header-info = text-hea. "Creazione Anagrafica Cespiti
  APPEND ls_header TO lt_header.
  CLEAR ls_header.

* Data
  ls_header-typ  = 'S'.
  ls_header-key = text-dat. " Data:
  CONCATENATE  sy-datum+6(2) '.'
               sy-datum+4(2) '.'
               sy-datum(4) INTO ls_header-info.   "todays date
  APPEND ls_header TO lt_header.
  CLEAR: ls_header.

* Numero totale di record elaborati
  DESCRIBE TABLE gt_output LINES gv_tot.
  ld_linesc = gv_tot.
  ls_header-typ  = 'S'.
  ls_header-key = text-rtt. "Record totali:
  ls_header-info = ld_linesc.
  APPEND ls_header TO lt_header.
  CLEAR: ls_header, ld_linesc.

* Numero totale di record corretti
  ld_linesc = gv_ok.
  ls_header-typ  = 'S'.
  ls_header-key = text-rok. "Record corretti:
  ls_header-info = ld_linesc.
  APPEND ls_header TO lt_header.
  CLEAR: ls_header, ld_linesc.

* Numero di record scartati
  ld_linesc = gv_tot - gv_ok.
  ls_header-typ  = 'S'.
  ls_header-key = text-rko. "Record errati:
  ls_header-info = ld_linesc.
  APPEND ls_header TO lt_header.

* Modalità
  ls_header-typ  = 'S'.
  ls_header-key = text-mod. "Modalità:
  IF p_test IS INITIAL.
    ls_header-info = text-eff. "Effettiva
  ELSE.
    ls_header-info = text-tst. "Test
  ENDIF.
  APPEND ls_header TO lt_header.
  CLEAR: ls_header.


  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = lt_header.

ENDFORM.                    " CREATE_LAYOUT

*======================================================================*
* AT SELECTION-SCREEN OUTPUT
*======================================================================*
AT SELECTION-SCREEN OUTPUT.
  PERFORM check_radio.

*======================================================================*
*               AT SELECTION-SCREEN  ON VALUE REQUEST                  *
*======================================================================*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_filese.
  CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
    EXPORTING
      directory        = dir
    IMPORTING
      serverfile       = p_filese
    EXCEPTIONS
      canceled_by_user = 1
      OTHERS           = 2.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_filelo.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      program_name  = syst-repid
      mask          = '*.*'
    CHANGING
      file_name     = p_filelo
    EXCEPTIONS
      mask_too_long = 1
      OTHERS        = 2.

*======================================================================*
*               START-OF-SELECTION                                     *
*======================================================================*
START-OF-SELECTION.

  PERFORM read_file.
  PERFORM estrai_dati.
  PERFORM crea_cespiti.


*======================================================================*
*               END-OF-SELECTION                                       *
*======================================================================*
END-OF-SELECTION.
  IF gt_output[] IS NOT INITIAL.
    PERFORM display_output.
  ELSE.
* Nessun dato estratto 
    MESSAGE text-e01 TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.

Nessun commento:

Posta un commento