*&---------------------------------------------------------------------*
*& report zixomo_get_div_payment
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report zixomo_get_div_payment.
tables: bkpf, zxomodivpayhdr.
constants: c_wsid(4)            value 'IB01',
           c_spam(2)            value '01',
           c_blart(2)           value 'Z4',
           formname_top_of_page type slis_formname value 'TOP_OF_PAGE',
           ws_login(30)         type c value 'App_TW_CMS',
           ws_pass(30)          type c value '',
           ws_appid(30)         type c value 'TWCMS'.

parameters: p_chk1 radiobutton group ga default 'X'.
selection-screen begin of block b1 with frame title text-001.
parameters: p_tok    as checkbox default ' ',
            p_bukrs  type bkpf-bukrs default '1000',
            p_sbudat type bkpf-budat default sy-datum,
            p_ebudat type bkpf-budat default sy-datum,
            p_url    type string lower case,
            p_file   type string lower case,
            p_div    radiobutton group g1 default 'X',
            p_cancel radiobutton group g1.
selection-screen end of block b1.
parameters: p_chk2 radiobutton group ga.
selection-screen begin of block b2 with frame title text-002.
select-options: s_budat for bkpf-budat,
                s_zuonr for zxomodivpayhdr-payout_sequence_id.
parameters: p_div1  radiobutton group g2 default 'X',
            p_canc1 radiobutton group g2.
selection-screen end of block b2.


types: begin of itab_accdoc.
    include structure zgl_accdoc.
types:  end of itab_accdoc.

data: it_div type table of itab_accdoc with header line,
*      itab_rec type table of zxomodivlog with header line,
      wa_log type zxomodivpaylog.

data: fieldcatalog type slis_t_fieldcat_alv with header line,
      gd_tab_group type slis_t_sp_group_alv,
      gd_layout    type slis_layout_alv,
      gd_repid     like sy-repid.

data: begin of it_result occurs 0,
        chkbx       type  icon-id,
        index(4)    type  c,
        bukrs       type  bkpf-bukrs,
        bldat       type  bkpf-bldat,
        budat       type  bkpf-budat,
        wrbtr       type  bseg-wrbtr,
        waers       type  bkpf-waers,
        zuonr       type  bseg-zuonr,
        sgtxt       type  bseg-sgtxt,
        xref1       type  bseg-xref1,
        xref2       type  bseg-xref2,
        xref3       type  bseg-xref3,
        zzpolicy01  type  zgl_accdoc-zzpolicy01,
        doc_no      type  bkpf-belnr,
        debit       type  bseg-hkont,
        credit      type  bseg-hkont,
        seq(8),
        type(1)     type  c,
        message(50) type  c,
      end of it_result.

data: begin of it_canc occurs 0,
        chkbx       type  icon-id,
        index(4)    type  c,
        doc_no      type bkpf-belnr,   " Reversal Document
        bukrs       type bkpf-bukrs,
        gjahr       type bkpf-gjahr,
        zuonr       type  bseg-zuonr,  " Payout sequence ID
        sgtxt       type  bseg-sgtxt,  " Temp to store Original FI doc
        seq(8),
        type(1)     type  c,
        message(50) type  c,
      end of it_canc.

types: begin of identification_data,
         fund_id type string,
       end of identification_data.

types: begin of json_data,
         payer_bank_code               type string,
         payee_bank_code               type string,
         payee_branch_code             type string,
         payee_id                      type string,
         payee_name                    type string,
         payee_bank_account            type string,
         net_dividend_payout_amount    type string,
         net_dividend_payout_ref_curr  type string,
         fund_currency                 type string,
         payment_method                type string,
         payout_date                   type string,
         payout_sequence_id            type string,
         dividend_payout_exchange_date type string,
         fund_currency_exchange_rate   type string,
         fund_currency_ref_exch_rate   type string,
         identification                type identification_data,
       end of json_data.

types: begin of js_res,
         seq                type string,
         policy_number      type string,
         reference_currency type string,
         campaign_code      type string,
         dividend_fund      type table of json_data with non-unique default key,
       end of js_res.

types: begin of json_response,
         status   type string,
         msg_code type string,
         msg_desc type string,
         response type table of js_res with non-unique default key,
       end of json_response.

data: response_wa type json_response,
      temp        type string,
      res_itab    type table of js_res,
      res_wa      type js_res,
      w_status    type json_response-status.
data: wa_iden type identification_data.
data: json_itab type table of json_data,
      json_wa   type json_data,
      cancel_wa type json_data.
"data: it_log type table of zxomodivpaylog with header line.

data: begin of it_skip occurs 0,
        seq   type zseq,
        zuonr like bseg-zuonr,
      end of it_skip.

data begin of it_payhdr2 occurs 0.
data: sel.
include structure zxomodivpayhdr.
data end of it_payhdr2.
data: it_payhdr type zxomodivpayhdr occurs 0 with header line.

data: it_payout type zxomodivpaylog occurs 0 with header line.

data: w_error.
data: w_datum like sy-datum,
      w_uzeit like sy-uzeit.

initialization.
  select single url into p_url from zws_master where wsid = c_wsid.
  w_datum = sy-datum.
  w_uzeit = sy-uzeit.

at selection-screen.
  if p_chk1 = 'X' and p_url is initial and p_file is initial.
    message e001(00) with 'Please enter URL or File Name'.
  endif.

at selection-screen on value-request for p_file.
  perform f4_filename.

start-of-selection.
  if p_chk1 = 'X'.   " Get from XO
    perform get_data.
    if w_error is initial.
      if p_div = 'X'.
        perform process_data.
        perform process_doc.
      else.
        perform process_cancel_data.
        perform process_cancel_doc.
      endif.
    endif.
    perform display_doc.
  endif.

  if p_chk2 = 'X'.    " Reprocess
    perform get_reprocess_data.
    if w_error is initial.
      if p_div1 = 'X'.
        perform process_data.
        perform process_doc.
      else.
        perform process_cancel_data.
        perform process_cancel_doc.
      endif.
    endif.
    perform display_doc.
  endif.

top-of-page.
  write: /1  'Ret code'(003),
            15 'Seq'(004),
            25 'Payout Sequence'(005),
            45 'Document No.'(006),
            60 'Message'(007).
  uline.

form get_data.
  data: lo_http_client type ref to if_http_client,
        lo_rest_client type ref to cl_rest_http_client,
        lv_response    type string,
        token(50)      type c,
        pass_token     type string.
  data: rawdata type truxs_t_text_data with header line.
*  data: wa_jslog type zxomodivjson.
  data: it_tmp_string               type table of string with header line,
        it_tmp_string2              type table of string with header line,
        it_tmp_string3              type table of string with header line,
        wa_tmp_string               type string,
        wa_tmp_string2              type string,
        wa_tmp_string3              type string,

        temp_payment_amount         type string,
        temp_string1                type string,
        temp_actual_dividend_option type string,
        temp_string2                type string,
        temp_string3                type string,
        temp_seq_id                 type string,
        temp_field                  type string,
        temp_value                  type string.

  data: w_line(4096).
  data: begin of outline occurs 0,
          text(255),
        end of outline.
  clear: response_wa,json_itab,json_wa.
  refresh it_div.

  if p_file is not initial.
    call function 'GUI_UPLOAD'
      exporting
        filename                = p_file
        filetype                = 'ASC'
        codepage                = '8300'
        "CODEPAGE  = '8300'
      tables
        data_tab                = rawdata
      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
        others                  = 17.

    loop at rawdata.
      temp = rawdata.
      condense temp.
      concatenate lv_response temp into lv_response.
    endloop.
*     /ui2/cl_json=>deserialize( exporting json = lv_response changing data = response_wa  ).
  else.

    if p_div = 'X'.
      concatenate p_url '?operationStartDate=' p_sbudat
                         '&operationEndDate=' p_ebudat
                         '&dataStatus=dividend' into p_url.

    else.
      concatenate p_url '?operationStartDate=' p_sbudat
                       '&operationEndDate=' p_ebudat
                       '&dataStatus=cancel' into p_url.
    endif.
    if not p_file is initial.
      write: / 'Test File: ', p_file.
    else.
      write: / 'URL: ', p_url,
             / 'Run Date: ', p_sbudat, ' to', p_ebudat.
    endif.
    cl_http_client=>create_by_url(
      exporting
        url = p_url
      importing
        client = lo_http_client
      exceptions
        argument_not_found = 1
        plugin_not_active = 2
        internal_error = 3
        others = 4 ).
    if sy-subrc <> 0.
      return.
    endif.

    create object lo_rest_client
      exporting
        io_http_client = lo_http_client.

    lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

    if p_tok eq 'X'.
      call function 'Z_BAPI_GET_TOKEN'
        exporting
          login         = ws_login
          password      = ws_pass
          applicationid = ws_appid
        importing
          token         = token.
      "Need to save at somewhere for further use?
    else.
      token = 'SAP'.
    endif.
    if token is not initial.
      pass_token = token.
    else.
      pass_token = 'SAP'.
    endif.
*  * passing the token value in header
    call method lo_http_client->request->set_header_field
      exporting
        name  = 'Token'
        value = pass_token.

*   passing the content-type value in header which is a mandatory field
    call method lo_http_client->request->set_header_field
      exporting
        name  = 'Content-Type'
        value = 'application/json;charset=utf-8'.

    lo_http_client->request->set_method( 'GET' ).
    lo_http_client->request->set_content_type( 'application/json' ).
    lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.

    lo_http_client->send(
                       exceptions
                       http_communication_failure = 1
                       http_invalid_state = 2 ).

*   Receiving the response
    lo_http_client->receive(
                    exceptions
                    http_communication_failure = 1
                    http_invalid_state = 2
                    http_processing_failed = 3 ).
    if sy-subrc eq 0.
      it_result-type = 'E'.
      it_result-message = 'Web Service Connection Failed'.
      append it_result.
      w_error = 'X'.
    endif.
  endif.
  "Rename the field name which is over 30 char

  check w_error is initial.
  lv_response = lo_http_client->response->get_cdata( ).
  replace all occurrences of 'net_dividend_payout_amount_reference_currency'
  in lv_response with 'net_dividend_payout_ref_curr'.

  replace all occurrences of 'fund_currency_to_nt_exchange_rate'
     in lv_response with 'fund_currency_exchange_rate'.

  replace all occurrences of 'fund_currency_to_reference_currency_exchange_rate'
     in lv_response with 'fund_currency_ref_exch_rate'.

  /ui2/cl_json=>deserialize( exporting json = lv_response changing data = response_wa  ).
  w_line = lv_response.
  w_status = response_wa-status.
  if w_status = '200' .
    move response_wa-response to res_itab.
    loop at res_itab into res_wa.
      move res_wa-dividend_fund to json_itab.
      sort json_itab by payout_sequence_id.
      loop at json_itab into json_wa.
        select single payout_sequence_id into temp from zxomodivpayhdr
        where payout_sequence_id = json_wa-payout_sequence_id
          and payout_date = json_wa-payout_date
          and status = '02'.   " ALready Processed
        check sy-subrc ne 0.
        on change of json_wa-payout_sequence_id.
          it_payout-sequence = 0.
          it_payhdr-payout_sequence_id = json_wa-payout_sequence_id.
          it_payhdr-payout_date = json_wa-payout_date.
          it_payhdr-policy_number = res_wa-policy_number.
          it_payhdr-reference_currency = res_wa-reference_currency.
          it_payhdr-campaign_code = res_wa-campaign_code.
          it_payhdr-status = '01'.   " Created
          it_payhdr-updatedate = w_datum.
          it_payhdr-updatetime = w_uzeit.
          if p_div = 'X'.
            it_payhdr-datastatus = 'DIVIDEND'.
          else.
            it_payhdr-datastatus = 'CANCEL'.
          endif.
          append it_payhdr.
        endon.
        it_payout-sequence = it_payout-sequence + 1.
        if p_div = 'X'.
          it_payout-datastatus = 'DIVIDEND'.
        else.
          it_payout-datastatus = 'CANCEL'.
        endif.
        move-corresponding json_wa to it_payout.
        append it_payout.
      endloop.
    endloop.
    modify zxomodivpayhdr from table it_payhdr.
    modify zxomodivpaylog from table it_payout.
  else.
    it_result-type  = 'E'.
    it_result-message = response_wa-msg_code.
    append it_result.
    w_error = 'X'.
  endif.

endform.

form get_reprocess_data.
  if p_div1 = 'X'.
    select * into corresponding fields of table it_payhdr2
       from zxomodivpayhdr
      where payout_sequence_id in s_zuonr
        and payout_date in s_budat
        and datastatus = 'DIVIDEND'
        and ( status = '01' or status = '99' ).

*    "show header list to select.
*    DO 10 TIMES.
*      it_payhdr-payout_sequence_id = 'a'.
*      APPEND it_payhdr.
*    ENDDO.
    if lines( it_payhdr2 ) ne 0.
      perform display_alv_list.
*      perform display_alv_list1.
    endif.

    loop at it_payhdr.
      move-corresponding it_payhdr to res_wa.
      append res_wa to res_itab.
      select * appending corresponding fields of table it_payout
        from zxomodivpaylog
        where payout_sequence_id = it_payhdr-payout_sequence_id
          and payout_date = it_payhdr-payout_date.
    endloop.

    loop at it_payout.
      move-corresponding it_payout to json_wa.
      wa_iden-fund_id = it_payout-fund_id.
      json_wa-identification = wa_iden.
      append json_wa to json_itab.
    endloop.
  else.
    select * into corresponding fields of table it_payhdr
       from zxomodivpayhdr
      where payout_sequence_id in s_zuonr
        and payout_date in s_budat
        and datastatus = 'CANCEL'
           and ( status = '01' or status = '99' ).

    if lines( it_payhdr ) ne 0.
      perform display_alv_list.
    endif.

    loop at it_payhdr.
      move-corresponding it_payhdr to res_wa.
      append res_wa to res_itab.
      select * appending corresponding fields of table it_payout
       from zxomodivpaylog
       where payout_sequence_id = it_payhdr-payout_sequence_id
         and payout_date = it_payhdr-payout_date.
    endloop.

    loop at it_payout.
      move-corresponding it_payout to json_wa.
      wa_iden-fund_id = it_payout-fund_id.
      json_wa-identification = wa_iden.
      append json_wa to json_itab.
    endloop.
  endif.

endform.

form process_data.
  data: gv_item   type i,
        dr_acc    type bseg-hkont,
        cr_acc    type bseg-hkont,
        hktid     type t012a-hktid,
        hbkid     type t012a-hbkid,
        wa_it_div type itab_accdoc.
  data: tran_itab type table of zbk_acc_det with header line.

  data: temp        type string,
        local_waers type bkpf-waers,
        temp2       type string.
  data: year(4)  type c,
        month(2) type c,
        day(2)   type c.

  if w_error = ''.
    if p_chk1 = 'X'.
      res_itab = response_wa-response.  " Move JSON Response to ITAB
    endif.
    loop at res_itab into res_wa.

      if p_chk1 = 'X'.
        json_itab = res_wa-dividend_fund.
      endif.
      loop at json_itab into json_wa.
        add 1 to gv_item.
        clear: it_div, w_error.
* check if already process or not
        select single payout_sequence_id into temp from zxomodivpayhdr
        where payout_sequence_id = json_wa-payout_sequence_id
          and payout_date = json_wa-payout_date
          and datastatus = 'DIVIDEND'
          and status = '02'.
        if sy-subrc eq 0. " Already Processed
          it_skip-seq   = res_wa-seq.
          it_skip-zuonr = json_wa-payout_sequence_id.
          append it_skip.
          exit.   " Skip
        endif.
* header record
        it_div-index = gv_item.
        it_div-bldat = p_sbudat.
        it_div-budat = json_wa-payout_date.
        it_div-blart = c_blart.    " Doc Type
        it_div-bukrs = p_bukrs. " Company Code

        select single sap_curr into it_div-waers from zws_curr
            where xo_curr = res_wa-reference_currency.   " Currency
        if it_div-waers is initial.
          concatenate it_div-message 'No matching currency in ZWS_CURR: ' res_wa-reference_currency
          into it_div-message separated by space.
          it_div-type = 'E'.
          w_error = 'X'.
        elseif it_div-waers <> 'TWD'.
          it_div-kursf = json_wa-fund_currency_exchange_rate.
          it_div-wwert_d = json_wa-dividend_payout_exchange_date.
        endif.
        it_div-bktxt = it_div-waers && json_wa-net_dividend_payout_amount.
        select single debit credit zxoty
           from zxo_acc_det
           into ( dr_acc , cr_acc, it_div-xblnr )
          where bankcode = json_wa-payee_bank_code
            and currency = it_div-waers
            and bukrs = p_bukrs.
        if sy-subrc ne 0.
          concatenate it_div-message 'No matching A/C in ZXO_ACCT_DET:'
           json_wa-payee_bank_code ':' res_wa-reference_currency
          into it_div-message.
          it_div-type = 'E'.
          w_error = 'X'.
        else.
          perform conversion_exit changing dr_acc.
          perform conversion_exit changing cr_acc.
          it_div-hkont = dr_acc.
        endif.

*   Debit Line
        it_div-bschl      = '40'.  " Debit
        it_div-wrbtr      = json_wa-net_dividend_payout_ref_curr.
        it_div-dmbtr      = 0."let it cal in post function
        it_div-zuonr      = json_wa-payout_sequence_id.
        it_div-xref1      = json_wa-payee_id.
        it_div-xref2      = json_wa-payee_name.
        wa_iden          = json_wa-identification.
        it_div-xref3      = wa_iden-fund_id.
        it_div-sgtxt      = wa_iden-fund_id.
        it_div-zzpolicy01 = res_wa-policy_number.
        it_div-zfieldname = 'ZZPOLICY01;ZZCAMPCD1'.
        it_div-zfieldval = res_wa-policy_number && ';' &&  res_wa-campaign_code.

*   Credit Line
        move it_div to wa_it_div.
        wa_it_div-bschl = '50'.
        wa_it_div-hkont = cr_acc.
        wa_it_div-sgtxt = json_wa-payee_bank_code && json_wa-payee_branch_code
                         && json_wa-payee_bank_account.
        wa_it_div-valut = it_div-budat.
        if wa_it_div-hkont is initial.
          concatenate it_div-message 'No matching A/C in ZXO_ACCT_DET:'
           json_wa-payee_bank_code ':' res_wa-reference_currency
          into it_div-message.
          w_error = 'X'.
          it_div-type = 'E'.
        endif.

* Move to RESULT_ITAB
        clear it_result.
        move-corresponding it_div to it_result.
        it_result-seq     = res_wa-seq.
        it_result-debit   = it_div-hkont.
        it_result-credit  = wa_it_div-hkont.
        it_result-message = it_div-message.
        append it_result.

* Move to it_div if need to Post
        if it_div-type <> 'E'.
          append it_div.             " Append Debit Line
          append wa_it_div to it_div. " Append Credit Line
        else.
          update zxomodivpayhdr set status = '99'
                                 message = it_div-message
                 where payout_sequence_id = json_wa-payout_sequence_id
                   and payout_date = json_wa-payout_date.
        endif.
      endloop.
    endloop.
  endif.
endform.

form process_doc.
  data: curr_index(4)  type c,
        bapi_result(4) type c.

  if it_div[] is not initial.
    call function 'Z_BAPI_POST_DOC'
      exporting
        park_flag   = ''
        print_flag  = 'X'
        alv         = 'X'
        test_run    = ''
      tables
        acc_doc     = it_div
      exceptions
        check_fail  = 1
        create_fail = 2.

    bapi_result = sy-subrc."Read table in below loop for it_result may replace sy-subrc.
  else.
    bapi_result = -1.
  endif.

  clear curr_index.
  loop at it_div.
    if curr_index <> it_div-index."skip duplicate line.for D / C balance record in it_div.
      curr_index = it_div-index.
      " Update Posting Result to it_result
      read table it_result with key index = it_div-index.
      it_result-doc_no  = it_div-doc_no.
      it_result-type    = it_div-type.
      it_result-message = it_div-message.
      modify it_result index sy-tabix.
    endif.
  endloop.

  clear curr_index.

  "LOG
  loop at it_div."result tab and record tab is create in same time. tabix<->record will be match.

    loop at it_payhdr where payout_sequence_id = it_div-zuonr.
      if it_div-type = ''.
        it_payhdr-status = '02'.
      elseif it_div-type = 'E'.
        it_payhdr-status = '99'.
        it_payhdr-message = it_div-message.
      endif.
      modify it_payhdr.
    endloop.
    modify zxomodivpayhdr from table it_payhdr.

  endloop.


endform.

form process_cancel_data.
  data: year(4)  type c,
        month(2) type c,
        day(2)   type c.
  if w_error = ''.
    if p_chk1 = 'X'.
      res_itab = response_wa-response.
    endif.
    loop at res_itab into res_wa.
      if p_chk1 = 'X'.
        json_itab = res_wa-dividend_fund.
      endif.
      loop at json_itab into cancel_wa.
        select single payout_sequence_id into temp
        from zxomodivpayhdr
          where payout_sequence_id = cancel_wa-payout_sequence_id
            and payout_date = cancel_wa-payout_sequence_id
            and datastatus = 'CANCEL'
            and status = '02'.
        if sy-subrc eq 0. " Already Processed
          it_skip-seq = res_wa-seq.
          it_skip-zuonr = cancel_wa-payout_sequence_id.
          append it_skip.
          continue.   " Skip
        endif.
        select single bkpf~bukrs bkpf~belnr as doc_no bkpf~gjahr
              bseg~zuonr
          into corresponding fields of it_canc
          from bseg
         inner join bkpf on bkpf~bukrs = bseg~bukrs
                        and bkpf~belnr = bseg~belnr
                        and bkpf~gjahr = bseg~gjahr
                        and bkpf~stblg = ''
         where bseg~zuonr = cancel_wa-payout_sequence_id.
        if sy-subrc ne 0.
          it_canc-type = 'E'.
          concatenate cancel_wa-payout_sequence_id ': Document not found'
          into it_canc-message.
          update zxomodivpayhdr set status = '99'
                                  message = it_canc-message
                  where payout_sequence_id = cancel_wa-payout_sequence_id
                    and payout_date = cancel_wa-payout_date.
        endif.
        it_canc-seq   = res_wa-seq.
        it_canc-zuonr = cancel_wa-payout_sequence_id.

        append it_canc.
      endloop.
    endloop.
  endif.
endform.

form process_cancel_doc.
  data: re_budat    type bkpf-budat,
        temp_month  type i,
        temp_str(2) type c.
  data: gv_item  type i.
  data: w_reversal type bapiacrev.
  data: obj_key type bapiacrev-obj_key.
  data: it_return type table of bapiret2,
        wa_return type bapiret2.

  loop at it_canc where type <> 'E'.
    add 1 to gv_item.
    it_canc-index = gv_item.
    w_reversal-obj_type = 'BKPFF'.
    w_reversal-reason_rev = 'CP'.
    concatenate it_canc-doc_no
                it_canc-bukrs
                it_canc-gjahr into w_reversal-obj_key_r.
    call function 'BAPI_ACC_DOCUMENT_REV_POST'
      exporting
        reversal = w_reversal
        bus_act  = 'RFBU'
      importing
        obj_key  = obj_key
      tables
        return   = it_return.
    loop at it_return into wa_return where type = 'E'. " Error
      it_canc-type = 'E'.
      it_canc-message = wa_return-message.
    endloop.
    if sy-subrc ne 0.
      call function 'BAPI_TRANSACTION_COMMIT'.
      it_canc-sgtxt = obj_key.
      modify it_canc.
    endif.
    modify it_canc.
  endloop.
  "LOG
  loop at it_canc."result tab and record tab is create in same time. tabix<->record will be match.

    loop at it_payhdr where payout_sequence_id = it_canc-zuonr.
      if it_canc-type = ''.
        it_payhdr-status = '02'.
      elseif it_canc-type = 'E'.
        it_payhdr-status = '99'.
        it_payhdr-message = it_canc-message.
      endif.
      modify it_payhdr.
    endloop.
    modify zxomodivpayhdr from table it_payhdr.
  endloop.
endform.

form conversion_exit changing k_varbl.
  call function 'CONVERSION_EXIT_ALPHA_INPUT'
    exporting
      input  = k_varbl
    importing
      output = k_varbl.
endform.                    "CONVERSION_EXITs

form f4_filename.
  data: l_desktop   type string,
        l_i_files   type filetable,
        l_wa_files  type file_table,
        l_rcode     type int4,
        l_file_type type string.

  l_file_type = 'TXT|*.txt'.

  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      window_title            = 'Select file'
      default_extension       = l_file_type
      file_filter             = l_file_type
    changing
      file_table              = l_i_files
      rc                      = l_rcode
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5.
  if sy-subrc <> 0.
    message e000(00) with 'Error while opening file'.
  endif.
  check l_i_files is not initial.
  read table l_i_files index 1 into l_wa_files.
  if sy-subrc = 0.
    p_file = l_wa_files-filename.
  else.
    message e000(00) with 'Error while opening file'.
  endif.
endform.

form display_doc.
  data: w_text(10).
  data: w_skip    type i, w_success type i, w_error type i.

* Count no. of records
  clear: w_skip, w_success, w_error.
  describe table it_skip lines w_skip.
  loop at it_result.
    if it_result-type = 'E'.
      w_error = w_error + 1.
    else.
      w_success = w_success + 1.
    endif.
  endloop.
  loop at it_canc.
    if it_canc-type = 'E'.
      w_error = w_error + 1.
    else.
      w_success = w_success + 1.
    endif.
  endloop.
  write: / 'Record count: ',
        /5 'Skipped: ', w_skip,
        /5 'Posted:  ', w_success,
        /5 'Error    ', w_error.
  skip.

  loop at it_skip.
    write: / 'Skipped' under text-003,
              it_skip-seq under text-004,
              it_skip-zuonr under text-005.
  endloop.

  case 'X'.
    when p_div.
      loop at it_result.
        at first.
          write: / 'Status: ', w_status.
        endat.
        write: /  it_result-type under text-003,
                  it_result-seq   under text-004,
                  it_result-zuonr under text-005,
                  it_result-doc_no under text-006,
                  it_result-message under text-007.
      endloop.
      skip.

    when p_cancel.

      loop at it_canc.
        at first.
          write: / 'Status: ', w_status.
        endat.
        write: /  it_canc-type    under text-003,
                  it_canc-seq     under text-004,
                  it_canc-zuonr   under text-005,
                  it_canc-doc_no  under text-006,
                  it_canc-message under text-007.
      endloop.
      skip.
  endcase.
endform.

form display_alv_list.
  gd_repid = sy-repid.
  perform build_fieldcatalog.
  perform build_layout.
*  call function 'REUSE_ALV_GRID_DISPLAY'
  call function 'REUSE_ALV_LIST_DISPLAY'
    exporting
      i_callback_program       = gd_repid
      i_callback_user_command  = 'USER_COMMAND'
      i_callback_pf_status_set = 'SET_PF_STATUS'   "see FORM
      is_layout                = gd_layout
      it_fieldcat              = fieldcatalog[]
      i_save                   = 'X'
    tables
      t_outtab                 = it_payhdr2
    exceptions
      program_error            = 1
      others                   = 2.
  if sy-subrc <> 0.

  endif.
endform.                    " DISPLAY_ALV_REPORT

*form display_alv_list1.
*  set pf-status 'ZGUI_STATUS'.
*  loop at it_payhdr.
*    write: / it_payhdr-sel as checkbox,
*             it_payhdr-payout_sequence_id,
*             it_payhdr-payout_date,
*             it_payhdr-policy_number,
*             it_payhdr-reference_currency,
*             it_payhdr-campaign_code,
*             it_payhdr-updatedate,
*             it_payhdr-updatetime.
*    hide:    it_payhdr-sel,
*             it_payhdr-payout_sequence_id,
*             it_payhdr-payout_date,
*             it_payhdr-policy_number,
*             it_payhdr-reference_currency,
*             it_payhdr-campaign_code,
*             it_payhdr-updatedate,
*             it_payhdr-updatetime.
*  endloop.
*endform.                    " DISPLAY_ALV_REPORT

form set_pf_status using rt_extab type slis_t_extab.
  set pf-status 'ZGUI_STATUS'.
  "Copy of 'STANDARD' pf_status from fgroup SALV
endform.

form build_fieldcatalog.
  fieldcatalog-fieldname   = 'PAYOUT_SEQUENCE_ID'.
  fieldcatalog-seltext_m   = 'Payout Sequence ID'.
  fieldcatalog-col_pos     = 0.
  fieldcatalog-outputlen   = 10.
  fieldcatalog-emphasize   = 'X'.
  fieldcatalog-key         = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'PAYOUT_DATE'.
  fieldcatalog-seltext_m   = 'Payout Date'.
  fieldcatalog-col_pos     = 1.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'POLICY_NUMBER'.
  fieldcatalog-seltext_m   = 'Policy Number'.
  fieldcatalog-col_pos     = 2.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'REFERENCE_CURRENCY'.
  fieldcatalog-seltext_m   = 'Reference Currency'.
  fieldcatalog-col_pos     = 3.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'CAMPAIGN_CODE'.
  fieldcatalog-seltext_m   = 'Campaign Code'.
  fieldcatalog-col_pos     = 4.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'UPDATEDATE'.
  fieldcatalog-seltext_m   = 'Update Date'.
  fieldcatalog-col_pos     = 5.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'UPDATETIME'.
  fieldcatalog-seltext_m   = 'Update Time'.
  fieldcatalog-col_pos     = 6.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.
endform.                    " BUILD_FIELDCATALOG

form build_layout.
  gd_layout-box_fieldname     = 'SEL'.
  "set field name to store row selection
  gd_layout-edit              = 'X'. "makes whole ALV table editable
  gd_layout-zebra             = 'X'.
endform.                    " BUILD_LAYOUT

form user_command using r_ucomm like sy-ucomm
                   rs_selfield type slis_selfield.

* Check function code
  case r_ucomm.
    when 'EXEC'.  "user presses SAVE
      loop at it_payhdr2.
        if it_payhdr2-sel NE 'X'.
          "Process selected line. Remove the not selected line.
*          delete it_payhdr2.
          MOVE-CORRESPONDING it_payhdr2 TO it_payhdr.
          APPEND it_payhdr.
*       Process records that have been selected
        endif.
      endloop.
      rs_selfield-refresh = 'X'.
      set pf-status space.
*      LEAVE TO LIST-PROCESSING.
      LEAVE SCREEN.
  endcase.
ENDFORM.

  • LinkedIn Social Icon
  • Facebook Social Icon
  • Pinterest Social Icon
  • wordpress

HK Office

502 Office Plus@Sheung Wan,

93-103 Wing Lok Street

Sheung Wan, Hong Kong

Taiwan Office

Room 1, 12/F,  

No. 169, Sec 4 ZhongXiao East Road,

Da'an District, Taipei District 10690

Taiwan

Contact Us

info@avant.com.hk

Tel: +852 35902943