antelio
4/1/2019 - 12:14 PM

z_webpa_parse

REPORT z_webpa_parse.

TYPES: BEGIN OF ty_header,
         h01  TYPE c LENGTH  4,
         h01a TYPE c LENGTH  1,
         h02  TYPE c LENGTH  8,
         h03  TYPE c LENGTH  6,
         h04  TYPE c LENGTH  275,
         h05  TYPE c LENGTH  6,
       END OF ty_header.

TYPES: BEGIN OF ty_detail,
         pa01  TYPE c LENGTH 4,   " Empresa
         pa02  TYPE c LENGTH 1,   " Tipo de Seguimento
         pa03  TYPE c LENGTH 1,   " Tipo de Nota
         pa04  TYPE c LENGTH 5,   " Numero da Loja
         pa05  TYPE c LENGTH 2,   " Departamento
         pa06  TYPE c LENGTH 2,   " Tipo de Documento SAP
         pa07  TYPE c LENGTH 16,  " Numero da NF
         pa08  TYPE c LENGTH 16,  " Numero da NF de Origem
         pa09  TYPE c LENGTH 8,   " Data de Emissão
         pa10  TYPE c LENGTH 8,   " Data de Lançamento
         pa11  TYPE c LENGTH 10,  " Numero de Documento
         pa12  TYPE c LENGTH 3,   " Numero do Item
         pa13  TYPE c LENGTH 8,   " Data de Vencimento
         pa14  TYPE c LENGTH 3,   " Código de Moeda
         pa15  TYPE c LENGTH 16,  " Valor de Dodocumento
         pa16  TYPE c LENGTH 1,   " Sinal
         pa17  TYPE c LENGTH 10,  " Numero do Fornecedor Legado
         pa18  TYPE c LENGTH 10,  " Numero do Fornecedor SAP
         pa19  TYPE c LENGTH 1,   " 1 - CPF, 2 - CNPJ
         pa20  TYPE c LENGTH 16,  " CPF/CNPJ
         pa21  TYPE c LENGTH 40,  " Razão Social
         pa22  TYPE c LENGTH 50,  " Motivo da Devolução
         pa22a TYPE c LENGTH 63,  " Brancos
         pa23  TYPE c LENGTH 6,   " Sequencial
       END OF ty_detail.

TYPES: BEGIN OF ty_trailler,
         t01  TYPE c LENGTH 4,
         t01a TYPE c LENGTH 1,
         t02  TYPE c LENGTH 6,
         t03  TYPE c LENGTH 283,
         t04  TYPE c LENGTH 6,
       END OF ty_trailler.

DATA: gs_header   TYPE ty_header,
      gs_trailler TYPE ty_trailler,
      gs_detail   TYPE ty_detail,
      gt_detail   TYPE TABLE OF ty_detail,
      gv_file     TYPE string,
      gs_data     TYPE string,
      gt_data     TYPE TABLE OF string.

PARAMETERS: p_file TYPE ibipparms-path OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.

START-OF-SELECTION.

  gv_file = p_file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = gv_file    " Name of file
*     filetype = 'ASC'    " File Type (ASC or BIN)
*     has_field_separator     = SPACE    " Columns Separated by Tabs in Case of ASCII Upload
*     header_length           = 0    " Length of Header for Binary Data
*     read_by_line            = 'X'    " The file will be written to the internal table line-by-line
*     dat_mode = SPACE    " Numeric and Date Fields Imported in ws_download 'DAT' Format
*     codepage =     " Character Representation for Output
*     ignore_cerr             = ABAP_TRUE    " Specifies whether to ignore errors converting character sets
*     replacement             = '#'    " Replacement Character for Non-Convertible Characters
*     check_bom               = SPACE    " The consistency of the codepage and byte order mark will be
*     virus_scan_profile      =     " Virus Scan Profile
*     no_auth_check           = SPACE    " Switch off Check for Access Rights
*  importing
*     filelength              =     " File Length
*     header   =     " File Header in Case of Binary Upload
    TABLES
      data_tab = gt_data    " Transfer table for file contents
*  changing
*     isscanperformed         = SPACE    " File already scanned
*  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
    .

  LOOP AT gt_data INTO gs_data.
    CASE gs_data+4(1).
      WHEN 'D'.
        MOVE gs_data TO gs_detail.
        APPEND gs_detail TO gt_detail.
        CLEAR gs_detail.
      WHEN 'H'.
        MOVE gs_data TO gs_header.

      WHEN 'T'.
        MOVE gs_data TO gs_trailler.

      WHEN OTHERS.
    ENDCASE.
  ENDLOOP.


*  " Process Vendor
*  DATA: gs_lfa1 TYPE lfa1,
*        l_lifnr TYPE lifnr.
*  LOOP AT gt_detail INTO gs_detail.
*    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
*      EXPORTING
*        input  = gs_detail-pa18
*      IMPORTING
*        output = l_lifnr.
*    .
*    SELECT SINGLE * FROM lfa1 INTO gs_lfa1
*      WHERE lifnr = l_lifnr.
*    IF sy-subrc IS NOT INITIAL.
*      PERFORM create_vendor USING gs_detail.
*    ENDIF.
*  ENDLOOP.
**&---------------------------------------------------------------------*
**&      Form  CREATE_VENDOR
**&---------------------------------------------------------------------*
**       text
**----------------------------------------------------------------------*
**      -->P_GS_DETAIL  text
**----------------------------------------------------------------------*
*FORM create_vendor  USING    p_gs_detail TYPE ty_detail.
*
*ENDFORM.

  LOOP AT gt_detail INTO gs_detail.
*    PERFORM process_vendor USING gs_detail.
    PERFORM post_document USING gs_detail.

  ENDLOOP.



FORM post_document USING detail TYPE ty_detail.

  DATA: bukrs  TYPE bukrs,
        waers  TYPE waers,
        blart  TYPE blart,
        lifnr  TYPE lifnr,
        h_text TYPE bktxt,
        belnr  TYPE belnr_d,
        gjahr  TYPE gjahr,
        sgtxt  TYPE sgtxt.


* Hard Codeds  (temp)
  waers = 'BRL'.
  blart = 'SA'.
  bukrs = '1000'.
  sgtxt = ''.
  h_text = ''.


*IB  Rcbto. Boleto SI
*IC  Rcbto. Cart.Créd. SI
*ID  Rcbto. Depósito SI
*II  Impostos s/Fatur. SI
*IP  Créd.Pgto.Maior SI
*IR  Receita SI

  DATA: header    TYPE bapiache09,
        accountgl TYPE TABLE OF bapiacgl09,
        accountap TYPE TABLE OF bapiacap09,
        accountcr TYPE TABLE OF bapiaccr09,
        return    TYPE TABLE OF bapiret2,
        document  TYPE awkey.

  CLEAR: header, accountgl[], accountap[], accountcr[].

  DATA: bldat TYPE bldat,
        budat TYPE budat.

  CONCATENATE detail-pa10+4(4) detail-pa10+2(2) detail-pa10(2) INTO budat.
  CONCATENATE detail-pa09+4(4) detail-pa09+2(2) detail-pa09(2) INTO bldat.

  PERFORM post_pay_fill_header USING header
                                    bukrs budat bldat detail-pa11 h_text blart.


  PERFORM post_pay_fill_accountap TABLES accountap
                      USING sgtxt detail.


  PERFORM post_pay_fill_accountcr TABLES accountcr USING detail.

  PERFORM post_pay_fill_accountgl TABLES accountgl USING header detail.

  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      documentheader = header
    IMPORTING
      obj_key        = document
    TABLES
      accountgl      = accountgl
      accountpayable = accountap
      currencyamount = accountcr
      return         = return.

  READ TABLE return WITH KEY type = 'E' TRANSPORTING NO FIELDS.
  IF sy-subrc IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.

    belnr = document+0(10).
    gjahr = document+14(4).
  ENDIF.
ENDFORM.


FORM post_pay_fill_header  USING    p_header  TYPE bapiache09
                                    p_bukrs
                                    p_budat
                                    p_bldat
                                    p_xblnr
                                    p_text
                                    p_blart.

  CLEAR p_header.

  p_header-username        = sy-uname.
  p_header-header_txt      = p_text.
  p_header-comp_code       = p_bukrs.
  p_header-doc_date        = p_bldat.
  p_header-pstng_date      = p_budat.
  p_header-doc_type        = p_blart.
  p_header-ref_doc_no      = p_xblnr.

ENDFORM.                    " POST_PAY_FILL_HEADER
*&-------------------------------------------------------



*&---------------------------------------------------------------------*
*&      Form  POST_PAY_FILL_ACCOUNTAR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ACCOUNTAR  text
*      -->P_PAYMENTS  text
*----------------------------------------------------------------------*
FORM post_pay_fill_accountap  TABLES   p_accountpayable  STRUCTURE bapiacap09

                              USING p_sgtxt TYPE sgtxt
                                    p_detail TYPE ty_detail.

  CLEAR p_accountpayable .

  p_accountpayable-itemno_acc = 1.  "POSNR_ACC
  p_accountpayable-vendor_no = '0000300001'.
  p_accountpayable-pmnt_block = 'A'. "ACPI_ZLSPR
  p_accountpayable-item_text = p_detail-pa21.
  p_accountpayable-alloc_nmbr = p_detail-pa18.
  APPEND p_accountpayable.
*    APPEND p_accountreceivable.


ENDFORM.                    " POST_PAY_FILL_ACCOUNTAR
*&---------------------------------------------------------------------*
*&      Form  POST_PAY_FILL_ACCOUNTCR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ACCOUNTCR  text
*      -->P_PAYMENTS  text
*----------------------------------------------------------------------*
FORM post_pay_fill_accountcr  TABLES   p_currencyamount STRUCTURE bapiaccr09
                              USING    p_detail TYPE ty_detail.


  DATA: amount LIKE p_currencyamount-amt_doccur,
        text   TYPE string.
  text = p_detail-pa15.
  REPLACE ALL OCCURRENCES OF ',' IN text WITH '.'.
  amount = text.
  CLEAR p_currencyamount.

*  DATA: w_payment TYPE ztbsi_payment,
*        total     TYPE zwrbtr.
*  CLEAR total.
*  LOOP AT p_payments INTO w_payment.
  p_currencyamount-itemno_acc = 1.  "POSNR_ACC
  p_currencyamount-currency = 'BRL'.   "WAERS
  p_currencyamount-amt_doccur = amount * -1. "BAPIWRBTR
  APPEND p_currencyamount.


  p_currencyamount-itemno_acc = 2.  "POSNR_ACC
  p_currencyamount-currency = 'BRL'.   "WAERS
  p_currencyamount-amt_doccur = amount. "BAPIWRBTR
  APPEND p_currencyamount.
*  ENDLOOP.
*
*  p_currencyamount-itemno_acc = 1.  "POSNR_ACC
*  p_currencyamount-currency = 'BRL'.   "WAERS
*  p_currencyamount-amt_doccur = total. "BAPIWRBTR
*
*  APPEND p_currencyamount.


ENDFORM.                    " POST_PAY_FILL_ACCOUNTCR

FORM post_pay_fill_accountgl TABLES   p_accountgl STRUCTURE bapiacgl09
USING

      p_header TYPE bapiache09
      p_detail TYPE ty_detail.

  p_accountgl-itemno_acc      = '0000000002'.
  p_accountgl-gl_account      = '0091101002'.
*  p_accountgl-comp_code       = p_header-comp_code.
*  p_accountgl-pstng_date      = p_header-pstng_date.
*  p_accountgl-alloc_nmbr      = p_header-pstng_date.

  APPEND p_accountgl.

ENDFORM.                    " POST_PAY_FILL_ACCOUNTGL
*&---------------------------------------------------------------------*
*&      Form  PROCESS_VENDOR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GS_DETAIL  text
*----------------------------------------------------------------------*
FORM process_vendor  USING    p_gs_detail TYPE ty_detail.

ENDFORM.