Thursday, 29 June 2017

FF67 BRC upload BDC by using Call Transaction



report zff67_upload.

typesbegin of ty_upload,
         valut    type    char10,
         kwbtr    type    string,
         chect_kf type    string,
       end of ty_upload.
datait_upload type table of  ty_upload,
      wa_upload type ty_upload.

typesbegin of ty_upload1,

         bankl type    string,
         bankn type    string,
         waers type    string,
         aznum type    string,
         azdat type    string,
         ssald type    string,
         esald type    string,
         budtm type    string,
         vgman type    string,
       end of ty_upload1.
datait_upload1 type table of  ty_upload1,
      wa_upload1 type ty_upload1.


data w_fname  type rlgrap-filename.
data bdcdata like bdcdata occurs with header line,
       messtab like bdcmsgcoll occurs with header line,
       it_raw  type truxs_t_text_data,
       v_text  type length 200,
       chk     type c.
datagv_string  type string,
      gv_row     type i,
      gv_row_str type string.
datait_intrn       type standard table of alsmex_tabline.
datait_intrn1      type standard table of alsmex_tabline.
data:  is_intrn     type alsmex_tabline.

data:  flag   type value 0,
       c_bcol type i       value  1                      ,     "START COLUMN
       c_brow type i       value  4                    ,     "START ROW
       c_ecol type i       value  200                    ,     "Ending column
       c_erow type i       value  40000                  .     "ENDING ROW

selection-screen begin of block b2 with frame title text-001 .

parameters:p_path type rlgrap-filename.". OBLIGATORY.
parameters:mode type ctu_params-dismode default 'E'.
selection-screen end of block b2 .
parameters ptype type febmka-wvar_art DEFAULT 3.

at selection-screen on value-request for p_path.
  call function 'F4_FILENAME'
    exporting
      field_name 'PATH'
    importing
      file_name  p_path.

  if not p_path is initial.
    w_fname p_path.
  endif.

start-of-selection.
  perform sub_uploadexcel.
form sub_uploadexcel .
  if not w_fname is initial.
    data wl_index  type i.
    field-symbols <fs>.
    refresh:  it_intrn,it_upload,it_intrn1,it_upload1.


    call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      exporting
        filename    w_fname
        i_begin_col c_bcol
        i_begin_row c_brow
        i_end_col   c_ecol
        i_end_row   c_erow
      tables
        intern      it_intrn.

    call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      exporting
        filename    w_fname
        i_begin_col 01
        i_begin_row 02
        i_end_col   200
        i_end_row   03
      tables
        intern      it_intrn1.

    loop at it_intrn1 into is_intrn.

      move is_intrn-col to wl_index.
*--- Assigning the each record to an internal table row
      assign component wl_index of structure wa_upload1 to <fs>.
      if sy-subrc  0.
*--- Asigning the field value to a field symbol
        condense is_intrn-value.
        move is_intrn-value to <fs>.
      endif.
      at end of row.

        append wa_upload1 to it_upload1.
        clear wa_upload1.
      endat.

    endloop.

    loop at it_intrn into is_intrn.
      move is_intrn-col to wl_index.
*--- Assigning the each record to an internal table row
      assign component wl_index of structure wa_upload to <fs>.
      if sy-subrc  0.
*--- Asigning the field value to a field symbol
        condense is_intrn-value.
        move is_intrn-value to <fs>.
      endif.
      at end of row.

        append wa_upload to it_upload.
        clear wa_upload.
      endat.
    endloop.
    clear p_pathw_fname.
  else.
    message i002(sywith 'Select valid file'.


  endif.



  if it_upload[] is not initial.
    loop at it_upload into wa_upload.
      if flag '0'.
        read table it_upload1 into wa_upload1  index  1.
        condense:wa_upload1-banklwa_upload1-bankn,wa_upload1-aznum.
        perform bdc_dynpro      using 'SAPMF40K' '0102'.
        perform bdc_field       using 'BDC_OKCODE'
                                      '/EVORG'.
        perform bdc_field       using 'BDC_CURSOR'
                                      'FEBMKA-BANKL'.
        perform bdc_dynpro      using 'SAPMF40K' '0110'.
        perform bdc_field       using 'BDC_CURSOR'
                                      'FEBMKA-WVAR_ART'.
        perform bdc_field       using 'BDC_OKCODE'
                                      '=ENTE'.
        perform bdc_field       using 'FEBMKA-FDIS_SEL'
                                      'X'.
        perform bdc_field       using 'FEBMKA-VARI_START'
                                      'CS01'.
        perform bdc_field       using 'FEBMKA-DEBI_MID'
                                      'D'.
        perform bdc_field       using 'FEBMKA-KRED_MID'
                                      'K'.

        perform bdc_field       using 'FEBMKA-WVAR_ART'
                                    ptype ."  '3'.
        perform bdc_field       using 'FEBMKA-BUCH_VAL'
                                      'X'.

        perform bdc_dynpro      using 'SAPMF40K' '0102'.
        perform bdc_field       using 'BDC_CURSOR'
                                      'FEBMKA-BUDTM'.
        perform bdc_field       using 'BDC_OKCODE'
                                      '/00'.
        perform bdc_field       using 'FEBMKA-BANKL'
                                    wa_upload1-bankl"  '911020029681621'.
        perform bdc_field       using 'FEBMKA-BANKN'
                                    wa_upload1-bankn."  '911020029681621'.
        perform bdc_field       using 'FEBMKA-WAERS'
                                    wa_upload1-waers."  'INR'.
        perform bdc_field       using 'FEBMKA-AZNUM'
                                    wa_upload1-aznum."  '11'.
        perform bdc_field       using 'FEBMKA-AZDAT'
                                    wa_upload1-azdat."  '02.01.2017'.
        perform bdc_field       using 'FEBMKA-SSALD'
                                     wa_upload1-ssald." '                     10'.
        perform bdc_field       using 'FEBMKA-ESALD'
                                     wa_upload1-esald."   '                     10'.
        perform bdc_field       using 'FEBMKA-BUDTM'
                                      wa_upload1-budtm."."  '11.01.2017'.
        if ptype '3'.
        perform bdc_field       using 'FEBMKA-JNAME'
                            sy-uname"  'SLV111986'.
       endif.

        flag 1.
        perform bdc_dynpro      using 'SAPMF40K' '8004'.
        perform bdc_field       using 'BDC_CURSOR'
                                      'FEBMKA-VGMAN(01)'.
        perform bdc_field       using 'BDC_OKCODE'
                                      '=ZINS'.
        perform bdc_dynpro      using 'SAPMF40K' '8004'.
        perform bdc_field       using 'BDC_CURSOR'
                                     'FEBMKK-ZUONR_KF(01)'.
        perform bdc_field       using 'BDC_OKCODE'
                              '=ZINS'.
        find '-' in wa_upload-kwbtr .
        if sy-subrc 0.

          perform bdc_field       using 'FEBMKA-VGMAN(01)'
                               'C200'.
        else.
          perform bdc_field       using 'FEBMKA-VGMAN(01)'
                             'C100'.


        endif.
        perform bdc_field       using 'FEBEP-VALUT(01)'
                              wa_upload-valut."        '01.01.2017'.
        condensewa_upload-kwbtr.
        perform bdc_field       using 'FEBMKA-KWBTR(01)'
                                   wa_upload-kwbtr."   '             10'.
        perform bdc_field       using 'FEBMKK-CHECT_KF(01)'
                           wa_upload-chect_kf."  'City'.
        perform bdc_field       using 'FEBMKK-ZUONR_KF(01)'
                                  wa_upload-chect_kf."

        perform bdc_dynpro      using 'SAPMF40K' '8004'.
        perform bdc_field       using 'BDC_CURSOR'
                                      'FEBMKK-ZUONR_KF(01)'.
        perform bdc_field       using 'BDC_OKCODE'
                                       '=ZINS'.



      else.


        find '-' in wa_upload-kwbtr .
        if sy-subrc 0.

          perform bdc_field       using 'FEBMKA-VGMAN(01)'
                                    'C200'.
        else.
          perform bdc_field       using 'FEBMKA-VGMAN(01)'
                                  'C100'.
        endif.
        perform bdc_field       using 'FEBEP-VALUT(01)'
                              wa_upload-valut."        '01.01.2017'.
        condensewa_upload-kwbtr.
        perform bdc_field       using 'FEBMKA-KWBTR(01)'
                                   wa_upload-kwbtr."   '             10'.
        perform bdc_field       using 'FEBMKK-CHECT_KF(01)'
                           wa_upload-chect_kf."  'City'.
        perform bdc_field       using 'FEBMKK-ZUONR_KF(01)'
                                    wa_upload-chect_kf."
        perform bdc_dynpro      using 'SAPMF40K' '8004'.
        perform bdc_field       using 'BDC_CURSOR'
                                      'FEBMKK-ZUONR_KF(01)'.
        perform bdc_field       using 'BDC_OKCODE'
                                      '=ZINS'.

      endif.
      clear:wa_upload.
    endloop.


    perform bdc_dynpro      using 'SAPMF40K' '8004'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'FEBMKK-CHECT_KF(01)'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=SICH'.

    perform bdc_dynpro      using 'SAPMF40K' '0102'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'FEBMKA-BANKL'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=BUCH'.
    perform bdc_field       using 'FEBMKA-BANKL'
                                 wa_upload1-bankl"  '911020029681621'.
    perform bdc_field       using 'FEBMKA-BANKN'
                                wa_upload1-bankn"   '911020029681621'.
    perform bdc_field       using 'FEBMKA-WAERS'
                               wa_upload1-waers"    'INR'.
    perform bdc_field       using 'FEBMKA-AZNUM'
                                 wa_upload1-aznum"  '11'.
    perform bdc_field       using 'FEBMKA-AZDAT'
                                   wa_upload1-azdat"'02.01.2017'.
    perform bdc_field       using 'FEBMKA-SSALD'
                                 wa_upload1-ssald" '                 10,00'.
    perform bdc_field       using 'FEBMKA-ESALD'
                                wa_upload1-esald"  '                 10,00'.
    perform bdc_field       using 'FEBMKA-BUDTM'
                                wa_upload1-budtm"  '11.01.2017'.

     if ptype '3'.
        perform bdc_field       using 'FEBMKA-JNAME'
                            sy-uname"  'SLV111986'.
       endif.


    perform bdc_dynpro      using 'SAPMSSY0' '0120'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=&F03'.
    perform bdc_dynpro      using 'SAPMF40K' '0102'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '/EABBR'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'FEBMKA-BANKL'.
    perform bdc_transaction using 'FF67'.



*      clear: wa_upload.
*
*
*    endloop.
    uline.
  else.

    message 'No data found from this file path' type 'E'.

  endif.

endform.

form bdc_dynpro using program dynpro.
  clear bdcdata.
  bdcdata-program  program.
  bdcdata-dynpro   dynpro.
  bdcdata-dynbegin 'X'.
  append bdcdata.
endform"BDC_DYNPRO


*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*

form bdc_field using fnam fval.
  if fval <> ' '.
    clear bdcdata.
    bdcdata-fnam fnam.
    bdcdata-fval fval.
    append bdcdata.
  endif.
endform"BDC_FIELD

form bdc_transaction using tcode.
  data ctu_params type ctu_params.

  ctu_params-dismode =  mode ."'N'.
  ctu_params-updmode 'S'.
  ctu_params-defsize 'X'.
  call transaction tcode using bdcdata
options from ctu_params
                         messages into messtab.
  clearbdcdatabdcdata[].
  commit work.
  gv_row gv_row + 1.
  gv_row_str gv_row.
  if gv_row 1.
    write/ sy-vline,'Row ID'10 sy-vline,12 'Error',19 sy-vline,21 'Bank Key' ,42 sy-vline,44  'Message'.
    uline.
  endif.
  if messtab[] is not initial.
    loop at messtab.
      message id     messtab-msgid
              type   messtab-msgtyp
              number messtab-msgnr
              into gv_string
              with messtab-msgv1
                   messtab-msgv2
                   messtab-msgv3
                   messtab-msgv4.

      write/ sy-vline,gv_row_str10 sy-vline,12 messtab-msgtyp,19 sy-vline,21 wa_upload1-bankl ,42 sy-vline,44  gv_string.
    endloop.
  else.
    write/ sy-vline,gv_row_str10 sy-vline,12 'S',19 sy-vline,21 wa_upload1-bankl ,42 sy-vline,44  'Success'.
  endif.
endform.                    " BDC_TRANSACTION

How to display bapi success or Error Meaasge in ALV?


typesbegin of ty_msg_str,
         sno     type i,
         pernr_d type pernr_d,
         msg  type string,
         belnr   type bseg-belnr,
       end of ty_msg_str.

datagt_fmsg type table of ty_msg_str,
      gs_fmsg type ty_msg_str.



data gt_fieldcat type slis_t_fieldcat_alv,
       gs_fieldcat type slis_fieldcat_alv,
       gt_layout   type lvc_s_layo,
       gs_layout   type slis_layout_alv.


data gt_return            type table of bapiret2,
       gt_return1           type table of bapiret2,
       gs_return            like line of gt_return,
       gt_accountgl         type table of bapiacgl09,
       gt1_accountgl        type table of bapiacgl09,
       gs_accountgl         like line of gt_accountgl,
       gt_accountpayable    type table of bapiacap09,
       gs_accountpayable    like line of gt_accountpayable,
       gt_accountreceivable type table of bapiacar09,
       gs_accountreceivable like line of gt_accountreceivable,
       gt_cur               type table of bapiaccr09,
       gt_cur1              type table of bapiaccr09,
       gs_cur               like line of gt_cur,
       gs_head              type bapiache09,
       gt_extention         type table of bapiextc,
       gs_extention         type bapiextc,
       gv_amt               type char20,
       gv_acc               type sy-tabix,
       lv_itemno(4)         type c,
       lv_indicator(1)      type c,
       obj_type             type bapiache02-obj_type,
       obj_key              type bapiache02-obj_key,
       obj_sys              type bapiache02-obj_sys,
       it_extension1        type standard table of bapiacextc,
       wa_extension1        type bapiacextc...


datasum_cur              type    decimals 2.

datalv_ind type .


***********************************************************************************

form bapi_update.

  lv_ind lv_ind + 1.

  refresh:gt_cur1.
  movegt_cur[]  to gt_cur1[].
  loop at gt_cur into gs_cur.

    sum_cur =   sum_cur + gs_cur-amt_doccur.

    cleargs_cur.
  endloop.

  if sum_cur  < 0.  .
    sum_cur sum_cur * -1.
    if sum_cur between '0.0000'  and '0.9000'.
      sum_cur sum_cur * -1.
      loop at gt_cur into gs_cur.

        if gs_cur-amt_doccur > '0' .
          gs_cur-amt_doccur gs_cur-amt_doccur sum_cur.

          modify  gt_cur from gs_cur.
          exit.
        endif.

        cleargs_cur.
      endloop.
    endif.
  endif.
  if sum_cur between '0.0000'  and '0.9000'.
    loop at gt_cur into gs_cur.

      if gs_cur-amt_doccur < '0'  .
        gs_cur-amt_doccur gs_cur-amt_doccur sum_cur.

        modify  gt_cur from gs_cur.
        exit.
      endif.

      cleargs_cur.
    endloop.


  endif.
  clearsum_cur.

  call function 'BAPI_ACC_DOCUMENT_POST'
    exporting
      documentheader gs_head
    importing
      obj_type       obj_type
      obj_key        obj_key
      obj_sys        obj_sys
    tables
      accountgl      gt_accountgl
      accountpayable gt_accountpayable
*     EXTENSION1     =
      currencyamount gt_cur
      return         gt_return.


 read table gt_return into gs_return with key type 'S'.
  if sy-subrc 0.

    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait 'X'
* IMPORTING
*       RETURN        =
      .
    gs_fmsg-sno lv_ind .
    gs_fmsg-pernr_d pernr_d.
    gs_fmsg-msg 'Document Successfully Posted!!' .
    gs_fmsg-belnr gs_return-message_v2+0(10.
    append gs_fmsg to gt_fmsg .
    clear gs_fmsg ,pernr_d .
    refresh gt_return[] .

  endif.

  loop at gt_return into gs_return where type 'E' .
    gs_fmsg-sno lv_ind .
    gs_fmsg-msg gs_return-message .
    gs_fmsg-pernr_d pernr_d.
    gs_fmsg-belnr ' '.
    append gs_fmsg to gt_fmsg .
    clear gs_fmsg ,pernr_d ..
    clear gs_return .

  endloop .


endform.


*******************************************************************************




form build_layout  using    value(p_2015)
                            value(p_2016)
                            value(p_2017)
                            value(p_2018)
                            value(p_2019)
                            value(p_2020).

  gs_fieldcat-col_pos      p_2015.
  gs_fieldcat-fieldname    p_2016.
  gs_fieldcat-tabname      p_2017.
  gs_fieldcat-reptext_ddic p_2018.
  gs_fieldcat-outputlen    p_2019.
  gs_fieldcat-do_sum       p_2020.

  append gs_fieldcat to gt_fieldcat.
  clear gs_fieldcat .

endform.



*********************************************************************************




form  display_bapi_message .

  perform build_layout using '1'  'SNO'    'GT_FMSG'  'Record Number'               '12'  ''.
  perform build_layout using '2'  'PERNR_D'    'GT_FMSG'  'Employee Number'               '12'  ''.
  perform build_layout using '3'  'MSG'    'GT_FMSG'  'Message'                     '45'  ''.
  perform build_layout using '4'  'BELNR'  'GT_FMSG'  'Accounting Document Number'  '10'  ''.

  gs_layout-colwidth_optimize 'X'.
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_background_id    'ALV_BACKGROUND'
      i_callback_program sy-repid
      it_fieldcat        gt_fieldcat
      is_layout          gs_layout
      i_save             'A'
    tables
      t_outtab           gt_fmsg.

endform.



*************************************************************************