*&---------------------------------------------------------------------*
*& report zixomo_nondiv_payment
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report zixomo_get_nondiv_payment.

constants: c_wsid(5)    value 'IB01A',
           c_blart(2)   value 'Z5',
           c_hkont(6)   value '564000',
           ws_login(30) type c value 'App_TW_CMS',
           ws_pass(30)  type c value '',
           ws_appid(30) type c value 'TWCMS'.  " Bank Charge A/C

tables: zxomopayoutlog.
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 'X',
            p_budat  type bkpf-budat default sy-datum,
            p_bukrs  type bkpf-bukrs default '1000',
            p_url    type string lower case,
            p_file   type string lower case,
            p_pend   radiobutton group g1,
            p_na     radiobutton group g1,
            p_return 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_budat1 for zxomopayoutlog-payout_date,
                s_zuonr1 for zxomopayoutlog-payout_sequence_id.
parameters: p_pend1 radiobutton group g2 default 'X',
            p_na1   radiobutton group g2,
            p_ret1  radiobutton group g2.
selection-screen end of block b2.

data: it_pend type table of zgl_accdoc with header line,
      wa_log  type zxomopayoutlog.

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,
        type(1)      type  c,
        message(100) type  c,
      end of it_result.

data: begin of it_canc occurs 0,
        chkbx        type  icon-id,
        index(4)     type  c,
        bukrs        type bkpf-bukrs,
        doc_no       type bkpf-belnr,
        gjahr        type bkpf-gjahr,
        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,
        debit        type  bseg-hkont,
        credit       type  bseg-hkont,
        type(1)      type  c,
        message(100) type  c,
      end of it_canc.

data: begin of it_return occurs 0,
        cheque_number(20)            type c,
        cheque_status(10)            type c,
        cheque_cancellation_date(10) type c,
        payout_sequence_id(20)       type c,
        type(1)                      type  c,
        message(100)                 type  c,
      end of it_return.

data BEGIN OF it_payhdr2 occurs 0.
  DATA: sel.
  INCLUDE STRUCTURE zxomopayouthdr.
DATA END OF it_payhdr2.

data: it_payhdr type zxomopayouthdr occurs 0 with header line.
data: it_payout type zxomopayoutlog occurs 0 with header line.
data: begin of it_bdcdata occurs 0. "batch session
    include structure bdcdata.
data: end of it_bdcdata.
data:   messtab like bdcmsgcoll occurs 0 with header line.

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.

types: begin of json_data,
         policy_number                type string,
         campaign_code                type string,
         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,
         payment_method               type string,
         gross_amount                 type string,
         payment_status               type string,
         bank_charge                  type string,
         payout_currency              type string,
         event_remark                 type string,
         operation_name               type string,
         last_cancellation_date       type string,
         payout_date                  type string,
         payout_sequence_id           type string,
         withdrawal_strip_sequence_id type string,
         cheque_number                type string,
         cheque_status                type string,
         cheque_cancellation_date     type string,
         finance_product_code         type string,
         partner_id                   type string,
         accounting_t0_code           type string,
         accounting_t1_code           type string,
         accounting_t2_code           type string,
         accounting_t5_code           type string,
         accounting_t6_code           type string,
         accounting_t7_code           type string,
       end of json_data.

data: json_itab      type table of json_data,
      json_wa        type json_data,
      temp_json_itab type table of json_data,
      temp_json_wa   type json_data,
      cancel_itab    type table of json_data,
      cancel_wa      type json_data,
      return_itab    type table of json_data,
      return_wa      type json_data.

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

data: response_wa type json_response,
      temp        type string,
      w_status    type json_response-status.
data: w_line(4096).
data: begin of outline occurs 0,
        text(255),
      end of outline.

data: begin of it_skip occurs 0,
        zuonr type bseg-zuonr,
        budat type bkpf-budat,
      end of it_skip.

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_url is initial and p_file is initial AND p_chk1 EQ 'X'.
    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'.
    case 'X'.
      when p_pend.
        perform get_data.
        if w_error is initial.
          perform process_data.
          perform process_doc.
        endif.
      when p_na.
        perform get_data.
        if w_error is initial.
          perform process_cancel_data.
          perform process_cancel_doc.
        endif.
      when p_return.
        perform get_data.
        if w_error is initial.
          perform process_return_data.
          perform process_return_doc.
        endif.
    endcase.
    perform display_doc.
  endif.
  if p_chk2 = 'X'.
    case 'X'.
      when p_pend1.
        perform get_log.
        if w_error is initial.
          perform process_data.
          perform process_doc.
        endif.
      when p_na1.
        perform get_log.
        if w_error is initial.
          perform process_cancel_data.
          perform process_cancel_doc.
        endif.
      when p_ret1.
        perform get_log.
        if w_error is initial.
          perform process_return_data.
          perform process_return_doc.
        endif.
    endcase.
    perform display_doc.
  endif.

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.
  clear: response_wa,json_itab,json_wa.
  refresh it_pend.

  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.
    case 'X'.
      when p_pend.
        concatenate p_url '?queryDate=' p_budat
                     '&paymentStatus=pending' into p_url.
      when p_na.
        concatenate p_url '?queryDate=' p_budat
                 '&paymentStatus=na' into p_url.
      when p_return.
        concatenate p_url '?queryDate=' p_budat
                     '&paymentStatus=return' into p_url.
    endcase.
    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.
    endif.

    lv_response = lo_http_client->response->get_cdata( ).
  endif.
  /ui2/cl_json=>deserialize( exporting json = lv_response changing data = response_wa  ).

  if not p_file is initial.
    write: / 'Test File: ', p_file.
  else.
    write: / 'URL     : ', p_url,
           / 'Run Date: ', p_budat.
  endif.

  w_line = lv_response.
  w_status = response_wa-status.
  if w_status = '200' .
    move response_wa-response 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 zxomopayouthdr
       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-status = '01'.   " Created
        it_payhdr-updatedate = w_datum.
        it_payhdr-updatetime = w_uzeit.
        case 'X'.
          when p_pend.
            it_payhdr-datastatus = 'PENDING'.
          when p_na.
            it_payhdr-datastatus = 'NA'.
          when p_return.
            it_payhdr-datastatus = 'RETURN'.
        endcase.
        append it_payhdr.
      endon.
      it_payout-sequence = it_payout-sequence + 1.
      case 'X'.
        when p_pend.
          it_payout-datastatus = 'PENDING'.
        when p_na.
          it_payout-datastatus = 'NA'.
        when p_return.
          it_payout-datastatus = 'RETURN'.
      endcase.
      move-corresponding json_wa to it_payout.
      append it_payout.
    endloop.
    modify zxomopayouthdr from table it_payhdr.
    modify zxomopayoutlog 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_log.
  clear: it_payhdr[], it_payout[].
  case 'X'.
    when p_pend1.
      select *
        from zxomopayouthdr
        into corresponding fields of table it_payhdr2
       where payout_sequence_id in s_zuonr1
         and payout_date in s_budat1
         and datastatus = 'PENDING'
         and ( status = '01' or status = '99' ).
*      DATA: temp(10) TYPE n.
*      DO 10 TIMES.
*        temp = temp + 1.
*        it_payhdr-payout_sequence_id = temp.
*        APPEND it_payhdr.
*      ENDDO.
      IF lines( it_payhdr2 ) NE 0.
        perform display_alv_list.
      ENDIF.
      loop at it_payhdr.

        select * from zxomopayoutlog
          appending corresponding fields of table it_payout
         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.
        append json_wa to json_itab.
      endloop.
    when p_na1.
      select * from zxomopayouthdr
        into corresponding fields of table it_payhdr
       where payout_sequence_id in s_zuonr1
         and payout_date in s_budat1
         and datastatus = 'NA'
         and ( status = '01' or status = '99' ).
      IF lines( it_payhdr ) NE 0.
        perform display_alv_list.
      ENDIF.

      loop at it_payhdr.
        select * from zxomopayoutlog
         into corresponding fields of table it_payout
         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 cancel_wa.
        append cancel_wa to cancel_itab.
      endloop.

    when p_ret1.
      select * from zxomopayouthdr
        into corresponding fields of table it_payhdr
       where payout_sequence_id in s_zuonr1
         and payout_date in s_budat1
         and datastatus = 'RETURN'
         and ( status = '01' or status = '99' ).

      IF lines( it_payhdr ) NE 0.
        perform display_alv_list.
      ENDIF.

      loop at it_payhdr.
        select * from zxomopayoutlog
         into corresponding fields of table it_payout
         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 return_wa.
        append return_wa to return_itab.
      endloop.
  endcase.
  "TBD

  if lines( json_itab ) eq 0 and lines( cancel_itab ) eq 0 and lines( return_itab ) eq 0.
    w_error = 'X'.
    write: / 'No Record Found.'.
  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_pend type zgl_accdoc,
        wa_itab2   type zgl_accdoc
        .
  data: w_trtype type ztrtyp.
  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.

  temp_json_itab = json_itab.
  loop at json_itab into json_wa.
    on change of json_wa-payout_sequence_id.
      add 1 to gv_item.
      clear: it_pend, w_error.
      select single payout_sequence_id
        into temp
        from zxomopayouthdr
      where payout_sequence_id = json_wa-payout_sequence_id
        and payout_date = json_wa-payout_date
        and datastatus = 'PENDING'
        and ( status = '02' ).
      if sy-subrc eq 0. " Already Processed
        it_skip-zuonr = temp.
        append it_skip.
        continue.   " Skip
      endif.
      it_pend-index = gv_item.
      it_pend-bldat = p_budat.
      it_pend-budat = json_wa-payout_date.
      it_pend-blart = c_blart.    " Doc Type
      it_pend-bukrs = p_bukrs. " Company Code
      it_pend-bktxt = json_wa-operation_name.

      select single sap_curr into it_pend-waers from zws_curr
         where xo_curr = json_wa-payout_currency.   " Currency
      if it_pend-waers is initial.
        concatenate 'No matching currency in ZWS_CURR: ' json_wa-payout_currency
        into it_pend-message separated by space.
        it_pend-type = 'E'.
        w_error = 'X'.
      endif.

* credit line
      it_pend-bschl = '50'.  " Credit
      select single clearing ztrtype
        into ( cr_acc, w_trtype )
        from zxo_clr_acc_det
       where bankcode = json_wa-payer_bank_code
         and currency = it_pend-waers
         and payout_type = json_wa-payment_method.

      if sy-subrc ne 0.
        concatenate 'No matching A/C in ZXO_CLR_ACCT_DET: '
        json_wa-payer_bank_code it_pend-waers json_wa-payment_method
        into it_pend-message separated by space.
        it_pend-type = 'E'.
        w_error = 'X'.
      else.
        perform conversion_exit changing cr_acc.
        it_pend-hkont = cr_acc.
        select single description into it_pend-xblnr
        from zbk_transtype
        where ztrtype = w_trtype.
      endif.
*
      loop at temp_json_itab into temp_json_wa where payout_sequence_id = json_wa-payout_sequence_id.
        it_pend-wrbtr = it_pend-wrbtr + temp_json_wa-gross_amount.
      endloop.
      it_pend-wrbtr = it_pend-wrbtr + json_wa-bank_charge.
      it_pend-dmbtr = 0."let it cal in post function
      it_pend-zuonr = json_wa-payout_sequence_id.
      it_pend-sgtxt = json_wa-payee_bank_code && json_wa-payee_branch_code
                    && json_wa-payee_bank_account.
      it_pend-valut = json_wa-payout_date.
      it_pend-xref1 = json_wa-payee_id.
      it_pend-xref2 = json_wa-payee_name.
      it_pend-xref3 = json_wa-withdrawal_strip_sequence_id.
      it_pend-zzpolicy01 = json_wa-policy_number.
      it_pend-zfieldname = 'ZZPOLICY01;ZZCAMPCD1;ZZT0;ZZT1;ZZT2;ZZT5;ZZT6;ZZT7;ZZPRODLI1;ZZPARTNR01'.
      it_pend-zfieldval = json_wa-policy_number
                      && ';' && json_wa-campaign_code
                      && ';' && json_wa-accounting_t0_code
                      && ';' && json_wa-accounting_t1_code
                      && ';' && json_wa-accounting_t2_code
                      && ';' && json_wa-accounting_t5_code
                      && ';' && json_wa-accounting_t6_code
                      && ';' && json_wa-accounting_t7_code
                      && ';' && json_wa-finance_product_code
                      && ';' && json_wa-partner_id.

* debit line for bank charge if bank charge > 0
      clear wa_it_pend.
      if json_wa-bank_charge > 0.
        move it_pend to wa_it_pend.
        wa_it_pend-bschl = '40'.
        wa_it_pend-hkont = c_hkont.
        wa_it_pend-wrbtr = json_wa-bank_charge.
        wa_it_pend-sgtxt = ''.
      endif.

* all others debit line
      loop at temp_json_itab into temp_json_wa where payout_sequence_id = json_wa-payout_sequence_id.
        clear wa_itab2.
        move it_pend to wa_itab2.
        wa_itab2-bschl = '40'.
        wa_itab2-wrbtr = temp_json_wa-gross_amount.
        wa_itab2-sgtxt = temp_json_wa-event_remark.
        select single hkont
           into wa_itab2-hkont
           from zxo_evt_desc
          where prod_line = temp_json_wa-finance_product_code
            and evt_desc = temp_json_wa-event_remark.
        if sy-subrc ne 0.
          concatenate 'No matching record in ZXO_EVT_DESC: '
          temp_json_wa-finance_product_code
          temp_json_wa-event_remark
          into it_pend-message separated by space.
          wa_itab2-type = 'E'.
          it_pend-type = 'E'.
          w_error = 'X'.
        else.
          append wa_itab2 to it_pend.
        endif.
      endloop.

      " Records to Result itab
      clear it_result.
      move-corresponding it_pend to it_result.
      it_result-credit  = it_pend-hkont.
      it_result-debit   = wa_it_pend-hkont.
      it_result-message = it_pend-message.
      append it_result.

      " Records to IT_PEND
      if it_pend-type <> 'E'.
        append it_pend.
        if wa_it_pend is not initial.
          append wa_it_pend to it_pend.
        endif.
      else.
        update zxomopayouthdr set status = '99'
                                  message = it_pend-message
                  where payout_sequence_id = json_wa-payout_sequence_id
                    and payout_date = json_wa-payout_date.
      endif.
    endon.
  endloop.

endform.

form process_doc.
  data: curr_index(4)  type c,
        bapi_result(4) type c.
  data: w_datum like sy-datum,
        w_uzeit like sy-uzeit.

  if it_pend[] is not initial.
    call function 'Z_BAPI_POST_DOC'
      exporting
        park_flag   = ''
        print_flag  = 'X'
        alv         = 'X'
        test_run    = ' '
      tables
        acc_doc     = it_pend
      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_pend.
    if curr_index <> it_pend-index."skip duplicate line.for D / C balance record in it_pend.
      curr_index = it_pend-index.
      "update result tab
      read table it_result with key index = it_pend-index.
      move it_pend-doc_no to it_result-doc_no.
      move it_pend-type to it_result-type.
      move it_pend-message to it_result-message.
      modify it_result index sy-tabix.
    endif.
  endloop.
  clear curr_index.
  w_datum = sy-datum.
  w_uzeit = sy-uzeit.
  loop at it_pend.
    on change of it_pend-index.

*      loop at it_payout where payout_sequence_id = it_pend-zuonr.
*        if it_pend-type = ''.
*          it_payout-status = '02'.
*        elseif it_pend-type = 'E'.
*          it_payout-status = '99'.
*          it_payout-message = it_pend-message.
*        endif.
*        modify it_payout.
*      endloop.
*      modify zxomopayoutlog from table it_payout.

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

form process_cancel_data.
  "Get bukrs,belnr,gjahr

  data: year(4)  type c,
        month(2) type c,
        day(2)   type c.

  if w_error = ''.
    move response_wa-response to cancel_itab.
    skip.
*    write: / 'Convert from JSON to SAP Format ...'.
    loop at cancel_itab into cancel_wa.
      select single payout_sequence_id
        into temp
        from zxomopayouthdr
      where payout_sequence_id = cancel_wa-payout_sequence_id
        and payout_date = cancel_wa-payout_date
        and datastatus = 'NA'
        and status = '02'.
      if sy-subrc eq 0. " ALreayd Processed
        it_skip-zuonr = temp.
        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~blart = c_blart
                      and bkpf~stblg = ''
       where bseg~zuonr = cancel_wa-payout_sequence_id.
      if sy-subrc ne 0.
        it_canc-zuonr = cancel_wa-payout_sequence_id.
        concatenate 'No matching Payout Seq ID: '
           cancel_wa-payout_sequence_id
           into it_canc-message separated by space.
        it_canc-type = 'E'.
        w_error = 'X'.
        update zxomopayouthdr set status = '99'
                                  message = it_pend-message
                  where payout_sequence_id = cancel_wa-payout_sequence_id
                    and payout_date = cancel_wa-payout_date.
      endif.
      append it_canc.
    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: obj_key type bapiacrev-obj_key.
  data: w_reversal type bapiacrev.
  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.
      modify it_canc.
    endloop.
    if sy-subrc ne 0.
      call function 'BAPI_TRANSACTION_COMMIT'.

      it_canc-sgtxt = obj_key.
      modify it_canc.
    endif.
    modify it_canc.
    "    move-corresponding it_canc to it_result.
    "    append it_result.
  endloop.

  "LOG
  loop at it_canc."result tab and record tab is create in same time. tabix<->record will be match.
    on change of it_canc-index.
      loop at it_payhdr where payout_sequence_id = it_canc-zuonr.
        if it_canc-type = ''.
          it_payhdr-status = '02'.
        elseif it_pend-type = 'E'.
          it_payhdr-status = '99'.
          it_payhdr-message = it_canc-message.
        endif.
        modify it_payhdr.
      endloop.
      modify zxomopayouthdr from table it_payhdr.
*    loop at it_payout where payout_sequence_id = it_canc-zuonr.
*      if it_canc-type = ''.
*        it_payout-status = '02'.
*      elseif it_canc-type = 'E'.
*        it_payout-status = '99'.
*        it_payout-message = it_pend-message.
*      endif.
*      modify it_payout.
*    endloop.
*    modify zxomopayoutlog from table it_payout.
    ENDON.
  endloop.
endform.

form process_return_data.
  data: w_chect like payr-chect,
        w_xbanc like payr-xbanc.
  if w_error = ''.
    move response_wa-response to return_itab.
    sort json_itab by payout_sequence_id.
    temp_json_itab = json_itab.
    loop at return_itab into return_wa.
      select single payout_sequence_id
       into temp
       from zxomopayouthdr
     where payout_sequence_id = return_wa-payout_sequence_id
       and datastatus = 'RETURN'
       and status = '02'.
      if sy-subrc eq 0. " Already Processed
        it_skip-zuonr = temp.
        append it_skip.
        continue.   " Skip
      endif.
      move-corresponding return_wa to it_return.
      if return_wa-cheque_status = '作廢'.
        it_return-cheque_status = '99'.
      else.
        it_return-cheque_status = '98'.
      endif.
      w_chect = it_return-cheque_number.
      shift w_chect left deleting leading '0'.
      select single chect xbanc from payr
        into ( w_chect, w_xbanc )
        where zbukr = p_bukrs
          and chect = w_chect.
      if sy-subrc ne 0. " Record not found
        it_return-type = 'E'.
        concatenate 'No check information found: '
                    it_return-cheque_number
        into it_return-message.
      elseif w_xbanc = 'X'.
        it_return-type = 'E'.
        concatenate 'Cashed Check cannot be processed: '
                    it_return-cheque_number
        into it_return-message.
      endif.
      append it_return.
    endloop.
  endif.
endform.

form process_return_doc.
  data: hbkid type t012k-hbkid,
        hktid type t012k-hktid.
  data: w_chect like payr-chect.
  data: w_date(10).
  loop at it_return where type <> 'E'.
    w_chect = it_return-cheque_number.
    shift w_chect left deleting leading '0'.
    select single hbkid hktid
     into ( hbkid, hktid ) from payr
    where zbukr = p_bukrs
      and chect = w_chect.
    concatenate it_return-cheque_cancellation_date+6(2) '.'
                it_return-cheque_cancellation_date+4(2) '.'
                it_return-cheque_cancellation_date(4)
    into w_date.
    refresh it_bdcdata.
    perform bdc_dynpro      using: 'SAPMFCHK' '0800'.
    perform bdc_field       using: 'BDC_OKCODE' '=RAGL',
                                   'PAYR-HBKID' hbkid,
                                   'PAYR-HKTID' hktid,
                                   'PAYR-CHECT' it_return-cheque_number,
                                   'PAYR-VOIDR' it_return-cheque_status,
                                   'UF05A-STGRD' 'CQ',
                                   'RF05R-BUDAT' w_date.
    call transaction 'FCH8' using it_bdcdata
                     mode 'N' update 'S'
                     messages into messtab.
    loop at messtab where msgtyp = 'E'.
      w_error = 'X'.
      message id messtab-msgid
              type messtab-msgtyp
              number messtab-msgnr
              with messtab-msgv1 messtab-msgv2 messtab-msgv3 messtab-msgv4
              into it_return-message.
      it_return-type = messtab-msgtyp.
      modify it_return.
    endloop.
    "Result itab?
  endloop.

  "LOG
  loop at it_return."result tab and record tab is create in same time. tabix<->record will be match.
    loop at it_payhdr where payout_sequence_id = it_return-payout_sequence_id.
      if it_return-type = ''.
        it_payhdr-status = '02'.
      elseif it_return-type = 'E'.
        it_payhdr-status = '99'.
        it_payhdr-message = it_pend-message.
      endif.
      modify it_payhdr.
    endloop.
    modify zxomopayouthdr from table it_payhdr.
  endloop.
endform.


form bdc_dynpro using program dynpro.
  clear it_bdcdata.
  it_bdcdata-program  = program.
  it_bdcdata-dynpro   = dynpro.
  it_bdcdata-dynbegin = 'X'.
  append it_bdcdata.
endform.                    "

form bdc_field using fnam fval.
  clear it_bdcdata.
  it_bdcdata-fnam = fnam.
  it_bdcdata-fval = fval.
  append it_bdcdata.
endform.                    "BDC_FIELD


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.

  clear: w_skip, w_success, w_error.
  describe table it_skip lines w_skip.
  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.
  loop at it_return.
    if it_return-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.
  uline.
  loop at it_skip.
    write: / 'Skipped' under text-003,
              it_skip-zuonr under text-004.
  endloop.

  if ( p_pend = 'X' and p_chk1 = 'X' ) or
     ( p_pend1 = 'X' and p_chk2 = 'X' ).

    loop at it_result.
      at first.
        write: / 'Status: ', w_status.
      endat.
      write: /  it_result-type under text-003,
                it_result-zuonr under text-004,
                40 it_result-budat,
                it_result-doc_no under text-005,
                it_result-message under text-006.
    endloop.

  elseif ( p_na = 'X' and p_chk1 = 'X' ) or
         ( p_na1 = 'X' and p_chk2 = 'X' ).

    loop at it_canc.
      at first.
        write: / 'Status: ', w_status.
      endat.
      write: / it_canc-type under text-003,
              it_canc-zuonr under text-004,
              40 it_canc-budat,
              it_canc-doc_no under text-005,
              it_canc-message under text-006.
    endloop.


  elseif ( p_return = 'X' and p_chk1 = 'X' ) or
         ( p_ret1 = 'X' and p_chk2 = 'X' ).

    loop at it_return.
      at first.
        write: / 'Status: ', w_status.
      endat.
      write: / it_return-type under text-003,
              it_return-payout_sequence_id under text-004,
              it_return-cheque_number under text-005,
              40 it_return-cheque_cancellation_date,
              it_return-message under text-006.
    endloop.

  endif.

endform.


FORM display_alv_list.
  gd_repid = sy-repid.
  PERFORM build_fieldcatalog.
  PERFORM build_layout.
  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.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
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'.
*  fieldcatalog-do_sum      = 'X'.
*  fieldcatalog-no_zero     = '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   = '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'.
*            DELETE it_payhdr.
          MOVE-CORRESPONDING it_payhdr2 TO it_payhdr.
          APPEND it_payhdr.
*       Process records that have been selected
        ENDIF.
      ENDLOOP.
      "rs_selfield-refresh = 'X'.
      rs_selfield-exit = 'X'.
      SET PF-STATUS space.
      LEAVE SCREEN.
*      SUPPRESS DIALOG.
*      LEAVE TO LIST-PROCESSING.

  ENDCASE.
ENDFORM.                    "user_command


top-of-page.
  write: /1  'Status'(003),
          15 'Payment Seq'(004),
          40 'Date'(008),
          55 'Doc No'(005),
          70 'Message'(006).
  uline.

  • 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