Skip to end of metadata
Go to start of metadata

Description: Inbound Idoc Creation Program
--------------------------------------------------------------------------------

*&---------------------------------------------------------------------*
*& Report  ZINBOUNDIDOC
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report ZINBOUNDIDOC no standard page heading message-id zs.

data:
  w_h2_ctr(4)                  type c,
  file                         type string,
  tx_write,
  prtbuf(140),                 " new
  line1                        like sy-tabix, "new
  line2                        like sy-tabix, "new
  line_sel                     like sy-tabix, "new
  line_num                     like sy-tabix, "new
  line_last                    like sy-lilli, "new
  line_current                 like sy-lilli, "new
  delimiter                    type c value '0A'.

selection-screen skip 1.
selection-screen begin of block a with frame title text-001.
parameters:
* Input File name
 p_inpt like rlgrap-filename default 'C:\ciminv\CP072006AR.txt',

* File name for output
 p_flnm like rlgrap-filename default 'C:\ciminv\CP072006AR_out.txt'.

*Print input file (List of invalid data if any)
selection-screen skip 1.
selection-screen begin of line.
parameters: p_prt radiobutton group r1.
selection-screen comment (66) text-008.
selection-screen end of line.

*Input file from NT server
selection-screen skip 1.
selection-screen begin of line.
parameters: p_inbd radiobutton group r1.
selection-screen comment (33) text-004.
selection-screen end of line.

*Create Idocs
selection-screen skip 1.
selection-screen begin of line.
parameters: p_proc radiobutton group r1.
selection-screen comment (21) text-006.
selection-screen end of line.

selection-screen end of block a.

data:
 head1              like sy-title,
 ctr_input_recs(5)  type p,
 ctr_output_recs(5) type p,

* Internal table containing NT File server file contents
 begin of tab_input occurs 0,
   line type string,
 end of tab_input,
* dsn table
 begin of dsn_input occurs 0,
     line(101),
 end of dsn_input,
 begin of new_input occurs 0,
     line(101),
 end of new_input,

 begin of header,
 item_type(1)     type c, "Item Type     H
 doc_date(8)      type c, "Document Date ccyymmdd
 post_date(8)     type c, "Posting date  ccyymmdd
 comp_code(4)     type c, "Company Code
 reference(16)    type c, "Reference
 aar_ind(1)       type c, "AAR Indicator
 dt_cr(2)         type c, "Debit Credit Indicator
 end of header,

 begin of cust,
 item_type(1)     type c, "Item Type     C
 no(10)           type c, "Customer number
 amount(25)       type c, "Amount
 curr(1)          type c, "Currency
 billing_ym(6)    type c, "Item text
 provstate(2)     type c, "Account Key
 end of cust,

begin of gdetail,
 item_type(1)     type c, "Item Type     G
 amount(25)       type c, "Amount
 provstate(2)     type c, "Account Key
 taxcode(2)       type c, "Tax Code
 itemtext(20)     type c, "Item Text
 tax_type(1)      type c, "Item Type     T
 gst_amount(25)   type c, "Amount
 qst_amount(25)   type c, "Amount
 end of gdetail.
*******************************new
at user-command.
  clear line2.
  if sy-ucomm = 'MODIFYFILE'.
    if line_sel LT 20.
      message i319(01) with 'Select a ''Type'' line to delete'.
*      exit.
    endif.
    line1 = line_sel + 1.
    loop at new_input.
      line2 = sy-tabix - 1.
*      exit.
    endloop.

    sy-lsind = 0.              " keep lists 1 level deep
    set user-command 'SCROLL'.  " otherwise scroll doesn't work
  elseif sy-ucomm = 'SCROLL'.
    scroll list to page 1 line line_current.
  elseif sy-ucomm = 'SAVEFILE'.
*    perform save_file --> download
  endif.

at line-selection.
    line_sel = line_num.
    line_current = sy-lilli.
    if line_last <> 0.
      read line line_last.
      modify line line_last line format color 1 intensified off.
    endif.
    line_last = line_current.
    read line line_current.
    modify line line_current
                     line format color 6 inverse on intensified on.
    format reset.
*******************************end new
at selection-screen on value-request for p_inpt.
  perform f4_input_file.

at selection-screen on value-request for p_flnm.
  perform f4_input_file.

at selection-screen.
  if p_inpt is initial.
    message e020.
  endif.

* Output File Name is required when outputting
  if p_flnm is initial and p_inbd = 'X'.
    message e022.
  endif.

start-of-selection.

  set pf-status  'MAIN'.

*  if p_inbd = 'X'.
*   Read file from NT server into a table
    file = p_inpt.
    call function 'GUI_UPLOAD'
         exporting
              filename                = file
              filetype                = 'ASC'
         tables
              data_tab                = tab_input
         exceptions
              file_open_error         = 1
              file_read_error         = 2
              no_batch                = 3
              gui_refuse_filetransfer = 4
              invalid_type            = 5
              no_authority            = 6
              unknown_error           = 7
              bad_data_format         = 8
              header_not_allowed      = 9
              separator_not_allowed   = 10
              header_too_long         = 11
              unknown_dp_error        = 12
              access_denied           = 13
              dp_out_of_memory        = 14
              disk_full               = 15
              dp_timeout              = 16
              others                  = 17.

    if sy-subrc ne 0.
      write: / '**** Error uploading file', p_inpt,
             / 'Return code:', sy-subrc.
      skip 2.
      stop. " input
    endif.

* Split the Header(H),Customer(C),Detail(G) and Tax(T) records in such
* order that it is a workable file.(split at New Line character).
    loop at tab_input.
      split tab_input-line at delimiter into table dsn_input.
      append lines of dsn_input to new_input.
    endloop.

    clear w_h2_ctr.

*   Loop through the input file
    loop at new_input.
      if new_input-line(2) = 'H2'.
        add +1 to w_h2_ctr.
      endif.

      if new_input-line eq space.
        exit.
      endif.

      add 1 to ctr_input_recs.
      if p_prt = 'X'.
        format intensified off.
        write : / new_input.
      endif.

*      if p_flnm = 'X'.
        add 1 to ctr_output_recs.
*      endif.
**********new
      prtbuf = new_input-line.
      write: 1 prtbuf, / ' '.
**********end new
    endloop.

*  endif.

  perform proc_data.

* Save File
  if not p_flnm is initial and p_inbd = 'X'.
    file = p_flnm.
    call function 'GUI_DOWNLOAD'
         exporting
              filename         = file
         tables
              data_tab         = new_input
         exceptions
              file_write_error = 1.
  endif.

end-of-selection.

* Audit Trail
  format intensified off.
  skip 2.
  write :
   / 'No. of records read   : ', ctr_input_recs,
   / 'No. of records written: ', ctr_output_recs.

* Print page headings
top-of-page.

* Call function module to centre headings according to page width
  head1 = 'Direct Feed Upload'.
  call function 'Z_BC_PRINT_HEADINGS'
       exporting
            head1     = head1
            head2     = sy-title
            show_info = 'X'.
  skip 2.
  format intensified on.
  write: / 'Read file from NT File Server :'.
*  if p_inbd = 'X'.
*    write : 'Yes'.
    format intensified off.
    write: /5 'Input File       : ', p_inpt+0(50).
*  else.
*    write : 'No'.
*  endif.

  format intensified on.
  skip 1.
  write: / 'Write to Server               :'.
  if p_inbd = 'X'.
    write : 'Yes'.
    format intensified off.
    write: /5 'Output File      : ',  p_flnm+0(50).
  else.
    write : 'No'.
  endif.
  format intensified on.
  skip 1.
  write: / 'Create IDocs                  :'.
  if p_proc = 'X'.
    write : 'Yes'.
    format intensified off.
  else.
    write : 'No'.
  endif.
  skip.

*&---------------------------------------------------------------------*
*&      Form  F4_INPUT_FILE
*&---------------------------------------------------------------------*
form f4_input_file.
  call function 'F4_FILENAME'
       exporting
            field_name = ' '
       importing
            file_name  = p_inpt.

endform.                    " F4_INPUT_FILE

*&---------------------------------------------------------------------*
*&      Form  proc_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form proc_data.

  data: begin of wa_knb1 occurs 1.
          include structure knb1.
  data end of wa_knb1.

  data: begin of wa_a053 occurs 0.
          include structure a053.
  data end of wa_a053.

* IDOC Control Table
  data: begin of zedidc40 occurs 0.
          include structure edi_dc40.
  data end of zedidc40.

* IDOC Data Table
  data: begin of zedidd40 occurs 0.
          include structure edi_dd40.
  data end of zedidd40.

  data: ze1bpache01 like e1bpache01,
        ze1bpacar01 like e1bpacar01,
        ze1bpacgl01 like e1bpacgl01,
        ze1bpaccr01 like e1bpaccr01,
        ze1bpactx01 like e1bpactx01.

  data: wdocnum(16) type n value 0.
  data: wsegnum(6)  type n value 0.
  data: witemno(10) type n value 0.
  data: witemno_new(10)  type n value 0.
  data: witemno_gst(10)  type n value 0.
  data: witemno_qst(10)  type n value 0.
  data: mm_name(10).
  data: wa_land1(3).
*-------------------------HEADER--------------------------------------
  loop at new_input.
* header/control record
    if new_input-line(1) = 'H'.
      clear zedidc40.
      header = new_input-line.
      wdocnum = wdocnum + 1.
      zedidc40-tabnam  = 'EDI_DC40'.
      zedidc40-docnum    = wdocnum.
      zedidc40-direct    = '2'.
      concatenate 'SAP' sy-sysid into zedidc40-rcvpor.
      zedidc40-rcvprt    = 'LS'.
      zedidc40-rcvprn    = 'CIM'.
      zedidc40-sndpor    = ' '.
      zedidc40-sndprt    = 'LS'.
      zedidc40-sndprn    = 'CIM'.
      zedidc40-credat    = sy-datum.
      zedidc40-cretim    = sy-uzeit.
      zedidc40-mestyp    = 'ACC_BILLING'.
      zedidc40-idoctyp   = 'ACC_BILLING02'.
      append zedidc40.  " Control record
*-----------------------------------------------------------------------
*E1BPACHE01
      clear ze1bpache01.
      clear zedidd40.
      ze1bpache01-obj_type     = 'ZVBRK'.
      shift header-reference   left deleting leading ' '.
      ze1bpache01-obj_key      = header-reference.
      ze1bpache01-obj_sys      = 'CIM'.
      ze1bpache01-username     = sy-uname.  " 'PROD_MERC'.
      if header-aar_ind = 'Y'.
        ze1bpache01-header_txt = 'AAR RULE 23'.
      else.
        ze1bpache01-header_txt = 'AAR NON RULE 23'.
      endif.
      ze1bpache01-comp_code    = header-comp_code.
      ze1bpache01-doc_date     = header-doc_date.
      ze1bpache01-pstng_date   = header-post_date.
      concatenate header-post_date(6) '07' into ze1bpache01-trans_date.
      if header-dt_cr = 'DR'.             " Customer Invoice
        ze1bpache01-doc_type     = header-dt_cr.
      else.
        ze1bpache01-doc_type     = 'DG'.  " Customer Credit Memo
      endif.
      ze1bpache01-ref_doc_no   = header-reference.

      zedidd40-docnum          = wdocnum.
      zedidd40-segnum          = '000001'.
      zedidd40-segnam          = 'E1BPACHE01'.
      zedidd40-hlevel          = '01'.
      zedidd40-sdata           = ze1bpache01.
      append zedidd40.         " e1bpache01
      witemno = 0.
      witemno_gst = 0.
      witemno_qst = 0.
      witemno_new = 0.
      wsegnum = 1.
    endif. " end of header
*--------------------------CUSTOMER-----------------------------------
    if new_input-line(1) = 'C'.
      clear ze1bpacar01.
      clear zedidd40.
      cust = new_input-line.
      if cust-amount cs ' '.
        condense cust-amount.
      endif.

*E1BPACAR01
      witemno = witemno + 1.
      wsegnum = wsegnum + 1.
      ze1bpacar01-itemno_acc = witemno.
      if cust-no+9(1) = ' '.
        shift cust-no right.
        replace ' ' with '0' into cust-no.
      endif.
      ze1bpacar01-customer = cust-no.
      select single * from knb1 into wa_knb1
             where  kunnr       = cust-no
             and    bukrs       = header-comp_code.
      if sy-subrc <> 0.
        write : / 'Customer ', cust-no.
        write :  'does not exist in company code ', header-comp_code.
      endif.

      if header-aar_ind = 'Y'.
        ze1bpacar01-pmnttrms = 'Z511'.
      else.
        ze1bpacar01-pmnttrms = 'Z512'.
      endif.
      if header-comp_code = '1000' or
         header-comp_code = '2000'.
        concatenate 'Car Repairs Billing for' cust-billing_ym into
          ze1bpacar01-item_text separated by space.
      elseif header-comp_code = '3000' or
             header-comp_code = '4000'.
        select single ltx from  t247 into mm_name
            where spras  = 'EN'
            and   mnr    = cust-billing_ym+4(2).
        concatenate 'AAR CAR REPAIRS ' mm_name
          cust-billing_ym(4) into ze1bpacar01-item_text
              separated by space.
      endif.

      zedidd40-docnum          = wdocnum.
      zedidd40-segnum          = wsegnum.
      zedidd40-segnam          = 'E1BPACAR01'.
      zedidd40-hlevel          = '02'.
      zedidd40-sdata           = ze1bpacar01.
      append zedidd40.         " e1bpacar01
*-----------------------------------------------------------------------
*E1BPACCR01
      clear ze1bpaccr01.
      clear zedidd40.
      ze1bpaccr01-itemno_acc = witemno.                     "E1BPACAR01
      if cust-curr = 'U'.
        ze1bpaccr01-currency = 'USD'.
      else.
        ze1bpaccr01-currency = 'CAD'.
      endif.

      if header-dt_cr = 'DR'.
        ze1bpaccr01-amt_doccur = cust-amount.
        ze1bpaccr01-amt_base   = cust-amount.
      else.
        concatenate '-' cust-amount into ze1bpaccr01-amt_doccur.
        concatenate '-' cust-amount into ze1bpaccr01-amt_base.
      endif.

      zedidd40-docnum          = wdocnum.
*After sort, segnum gets a new value according to config.
      zedidd40-segnum          = '999997'.
      zedidd40-segnam          = 'E1BPACCR01'.
      zedidd40-hlevel          = '02'.
      zedidd40-sdata           = ze1bpaccr01.
      append zedidd40.         " e1bpaccr01
    endif.   " end of customer
*----------------------------DETAIL-----------------------------------
    if new_input-line(1) = 'G'.
      clear ze1bpacgl01.
      clear zedidd40.
*E1BPACGL01
      gdetail = new_input-line.
      if gdetail-itemtext cs 'REBUTTAL'.
        gdetail-provstate = cust-provstate.
      endif.
      if gdetail-amount cs ' '.
        condense gdetail-amount.
      endif.
      if gdetail-gst_amount cs ' '.
        condense gdetail-gst_amount.
      endif.
      if gdetail-qst_amount cs ' '.
        condense gdetail-qst_amount.
      endif.

      shift gdetail-itemtext left deleting leading ' '.

      witemno = witemno + 1.
      wsegnum = wsegnum + 1.
      ze1bpacgl01-itemno_acc = witemno.
      ze1bpacgl01-gl_account = '0000064520'.
      ze1bpacgl01-tax_code   = gdetail-taxcode.

      if gdetail-itemtext+7(8) = 'REBUTTAL'.
* Rebilling - build tax jurisdiction based on customer region/state
        if ( cust-provstate = 'NB' or cust-provstate = 'NS'
          or cust-provstate = 'NF' )
        and ( header-comp_code = '1000' or  header-comp_code = '2000' ).
      concatenate 'CH' cust-provstate '000' into ze1bpacgl01-taxjurcode.
        elseif
           ( cust-provstate = 'BC' or cust-provstate = 'AB'
          or cust-provstate = 'MB'  or cust-provstate = 'SK'
          or cust-provstate = 'ON'  or cust-provstate = 'PE' )
        and ( header-comp_code = '1000' or  header-comp_code = '2000' ).
      concatenate 'CA' cust-provstate '000' into ze1bpacgl01-taxjurcode.
        elseif
              ( cust-provstate = 'PQ' or cust-provstate = 'QC' )
        and ( header-comp_code = '1000' or  header-comp_code = '2000' ).
          move 'CAQC000' to ze1bpacgl01-taxjurcode.
        elseif
            ( cust-provstate = 'AK' or cust-provstate = 'AL'
           or cust-provstate = 'AR' or cust-provstate = 'AS'
           or cust-provstate = 'AZ' or cust-provstate = 'CO'
           or cust-provstate = 'CT' or cust-provstate = 'DC'
           or cust-provstate = 'DE' or cust-provstate = 'FL'
           or cust-provstate = 'GA' or cust-provstate = 'GU'
           or cust-provstate = 'HI' or cust-provstate = 'IA'
           or cust-provstate = 'ID' or cust-provstate = 'IL'
           or cust-provstate = 'IN' or cust-provstate = 'KS'
           or cust-provstate = 'KY' or cust-provstate = 'LA'
           or cust-provstate = 'MA' or cust-provstate = 'MD'
           or cust-provstate = 'ME' or cust-provstate = 'MI'
           or cust-provstate = 'MN' or cust-provstate = 'MO'
           or cust-provstate = 'MP' or cust-provstate = 'MS'
           or cust-provstate = 'MT' or cust-provstate = 'NC'
           or cust-provstate = 'ND' or cust-provstate = 'NE'
           or cust-provstate = 'NH' or cust-provstate = 'NJ'
           or cust-provstate = 'NM' or cust-provstate = 'NV'
           or cust-provstate = 'NY' or cust-provstate = 'OH'
           or cust-provstate = 'OK' or cust-provstate = 'OR'
           or cust-provstate = 'PA' or cust-provstate = 'PR'
           or cust-provstate = 'RI' or cust-provstate = 'SC'
           or cust-provstate = 'SD' or cust-provstate = 'TN'
           or cust-provstate = 'TX' or cust-provstate = 'UT'
           or cust-provstate = 'VA' or cust-provstate = 'VI'
           or cust-provstate = 'VT' or cust-provstate = 'WA'
           or cust-provstate = 'WI' or cust-provstate = 'WV'
           or cust-provstate = 'WY' or cust-provstate = 'DL' ).
         concatenate cust-provstate '00000' into ze1bpacgl01-taxjurcode.
        elseif
          cust-provstate = 'CA'.
          move 'CL00000' to ze1bpacgl01-taxjurcode.
        else.
          move 'CA00000' to ze1bpacgl01-taxjurcode.
        endif.
      else.  " when string text is NOT REBUTTAL
* Regular billing - build tax jurisdiction based on line item
* region/state (where work is performed)
        if ( gdetail-provstate = 'NB' or gdetail-provstate = 'NS'
           or gdetail-provstate = 'NF' ).
          concatenate 'CH' gdetail-provstate '000'
                       into ze1bpacgl01-taxjurcode.
        elseif ( gdetail-provstate = 'BC' or gdetail-provstate = 'AB'
          or gdetail-provstate = 'MB'  or gdetail-provstate = 'SK'
          or gdetail-provstate = 'ON'  or gdetail-provstate = 'PE' ).
          concatenate 'CA' gdetail-provstate '000'
                      into ze1bpacgl01-taxjurcode.
        elseif ( gdetail-provstate = 'PQ' or gdetail-provstate = 'QC' ).
          move 'CAQC000' to ze1bpacgl01-taxjurcode.
        elseif ( gdetail-provstate = 'AK' or gdetail-provstate = 'AL'
           or gdetail-provstate = 'AR' or gdetail-provstate = 'AS'
           or gdetail-provstate = 'AZ' or gdetail-provstate = 'CO'
           or gdetail-provstate = 'CT' or gdetail-provstate = 'DC'
           or gdetail-provstate = 'DE' or gdetail-provstate = 'FL'
           or gdetail-provstate = 'GA' or gdetail-provstate = 'GU'
           or gdetail-provstate = 'HI' or gdetail-provstate = 'IA'
           or gdetail-provstate = 'ID' or gdetail-provstate = 'IL'
           or gdetail-provstate = 'IN' or gdetail-provstate = 'KS'
           or gdetail-provstate = 'KY' or gdetail-provstate = 'LA'
           or gdetail-provstate = 'MA' or gdetail-provstate = 'MD'
           or gdetail-provstate = 'ME' or gdetail-provstate = 'MI'
           or gdetail-provstate = 'MN' or gdetail-provstate = 'MO'
           or gdetail-provstate = 'MP' or gdetail-provstate = 'MS'
           or gdetail-provstate = 'MT' or gdetail-provstate = 'NC'
           or gdetail-provstate = 'ND' or gdetail-provstate = 'NE'
           or gdetail-provstate = 'NH' or gdetail-provstate = 'NJ'
           or gdetail-provstate = 'NM' or gdetail-provstate = 'NV'
           or gdetail-provstate = 'NY' or gdetail-provstate = 'OH'
           or gdetail-provstate = 'OK' or gdetail-provstate = 'OR'
           or gdetail-provstate = 'PA' or gdetail-provstate = 'PR'
           or gdetail-provstate = 'RI' or gdetail-provstate = 'SC'
           or gdetail-provstate = 'SD' or gdetail-provstate = 'TN'
           or gdetail-provstate = 'TX' or gdetail-provstate = 'UT'
           or gdetail-provstate = 'VA' or gdetail-provstate = 'VI'
           or gdetail-provstate = 'VT' or gdetail-provstate = 'WA'
           or gdetail-provstate = 'WI' or gdetail-provstate = 'WV'
           or gdetail-provstate = 'WY' or gdetail-provstate = 'DL' ).
          concatenate gdetail-provstate '00000'
                     into ze1bpacgl01-taxjurcode.
        elseif gdetail-provstate = 'CA'.
          move 'CL00000' to ze1bpacgl01-taxjurcode.
        else.
          move 'CA00000' to ze1bpacgl01-taxjurcode.
        endif.
      endif.

      ze1bpacgl01-alloc_nmbr = 'AAR'.

      if header-comp_code = '1000' or
           header-comp_code = '2000'.
        concatenate 'Car Repairs Billing for'  gdetail-itemtext
                 into ze1bpacgl01-item_text separated by space.
      elseif header-comp_code = '3000' or
             header-comp_code = '4000'.
        select single ltx from  t247 into mm_name
               where  spras  = 'EN'
               and    mnr    = cust-billing_ym+4(2).
        concatenate 'AAR CAR REPAIRS ' mm_name gdetail-itemtext
           into ze1bpacgl01-item_text separated by space.
      endif.
      if header-comp_code = '1000'.
        ze1bpacgl01-costcenter = '0000001346'.
      elseif header-comp_code = '3000'.
        ze1bpacgl01-costcenter = '0000001338'.
      elseif header-comp_code = '4000'.
        ze1bpacgl01-costcenter = '0000002353'.
      else.
        ze1bpacgl01-costcenter = ''.
      endif.

      zedidd40-docnum          = wdocnum.
      zedidd40-segnum          = wsegnum.
      zedidd40-segnam          = 'E1BPACGL01'.
      zedidd40-hlevel          = '02'.
      zedidd40-sdata           = ze1bpacgl01.
      append zedidd40.         " e1bpacgl01
*-----------------------------------------------------------------------
*E1BPACCR01
      clear ze1bpaccr01.
      clear zedidd40.
      ze1bpaccr01-itemno_acc = witemno. " E1BPACGL01
      if cust-curr = 'U'.
        ze1bpaccr01-currency = 'USD'.
      else.
        ze1bpaccr01-currency = 'CAD'.
      endif.
      if header-dt_cr = 'DR'.
        concatenate '-' gdetail-amount into ze1bpaccr01-amt_doccur.
        concatenate '-' gdetail-amount into ze1bpaccr01-amt_base.
      else.
        ze1bpaccr01-amt_doccur  = gdetail-amount.
        ze1bpaccr01-amt_base   = gdetail-amount.
      endif.

      zedidd40-docnum          = wdocnum.
*After sort, segnum gets a new value according to config.
      zedidd40-segnum          = '999997'.
      zedidd40-segnam          = 'E1BPACCR01'.
      zedidd40-hlevel          = '02'.
      zedidd40-sdata           = ze1bpaccr01.
      append zedidd40.         " e1bpaccr01

*---------------------------------GST--------------------------------
      clear ze1bpactx01.
      clear zedidd40.
*E1BPACTX01
      witemno_gst = witemno_gst + 1.
      ze1bpactx01-itemno_acc = witemno_gst.
      ze1bpactx01-gl_account = '0000023200'.                "T030K
      ze1bpactx01-tax_code    = gdetail-taxcode.            "no 1
      ze1bpactx01-acct_key    = 'MW1'.
*      SELECT SINGLE ktosl FROM  t030k INTO ze1bpactx01-acct_key
*             WHERE  ktopl  = 'CPR'
*             AND    mwskz  = ''
*             AND    konts  = ze1bpactx01-gl_account
*             AND    konth  = ze1bpactx01-gl_account.

      if gdetail-itemtext+7(8) = 'REBUTTAL'.
* Rebilling - build tax jurisdiction based on customer region/state
        if ( header-comp_code = '1000' or header-comp_code = '2000'
          or header-comp_code = '3000' ) and
           ( cust-provstate = 'NB' or cust-provstate = 'NS'
          or cust-provstate = 'NF' or cust-provstate = 'BC'
          or cust-provstate = 'AB' or cust-provstate = 'MB'
          or cust-provstate = 'SK' or cust-provstate = 'ON'
          or cust-provstate = 'PE' or cust-provstate = 'PQ'
          or cust-provstate = 'QC' ).
          move 'CA00000' to ze1bpactx01-taxjurcode.
        elseif ( cust-provstate = 'AK' or cust-provstate = 'AL'
           or cust-provstate = 'AR' or cust-provstate = 'AS'
           or cust-provstate = 'AZ' or cust-provstate = 'CO'
           or cust-provstate = 'CT' or cust-provstate = 'DC'
           or cust-provstate = 'DE' or cust-provstate = 'FL'
           or cust-provstate = 'GA' or cust-provstate = 'GU'
           or cust-provstate = 'HI' or cust-provstate = 'IA'
           or cust-provstate = 'ID' or cust-provstate = 'IL'
           or cust-provstate = 'IN' or cust-provstate = 'KS'
           or cust-provstate = 'KY' or cust-provstate = 'LA'
           or cust-provstate = 'MA' or cust-provstate = 'MD'
           or cust-provstate = 'ME' or cust-provstate = 'MI'
           or cust-provstate = 'MN' or cust-provstate = 'MO'
           or cust-provstate = 'MP' or cust-provstate = 'MS'
           or cust-provstate = 'MT' or cust-provstate = 'NC'
           or cust-provstate = 'ND' or cust-provstate = 'NE'
           or cust-provstate = 'NH' or cust-provstate = 'NJ'
           or cust-provstate = 'NM' or cust-provstate = 'NV'
           or cust-provstate = 'NY' or cust-provstate = 'OH'
           or cust-provstate = 'OK' or cust-provstate = 'OR'
           or cust-provstate = 'PA' or cust-provstate = 'PR'
           or cust-provstate = 'RI' or cust-provstate = 'SC'
           or cust-provstate = 'SD' or cust-provstate = 'TN'
           or cust-provstate = 'TX' or cust-provstate = 'UT'
           or cust-provstate = 'VA' or cust-provstate = 'VI'
           or cust-provstate = 'VT' or cust-provstate = 'WA'
           or cust-provstate = 'WI' or cust-provstate = 'WV'
           or cust-provstate = 'WY' or cust-provstate = 'DL' ).
         concatenate cust-provstate '00000' into ze1bpactx01-taxjurcode.
        elseif cust-provstate = 'CA'.
          move 'CL00000' to ze1bpactx01-taxjurcode.
        endif.
      else.  " when string text is NOT REBUTTAL
* Regular billing - build tax jurisdiction based on line item
* region/state (where work is performed)
        if ( header-comp_code = '1000' or  header-comp_code = '2000' )
            and ( gdetail-provstate = 'NB' or gdetail-provstate = 'NS'
               or gdetail-provstate = 'NF' or gdetail-provstate = 'BC'
               or gdetail-provstate = 'AB' or gdetail-provstate = 'MB'
               or gdetail-provstate = 'SK' or gdetail-provstate = 'ON'
               or gdetail-provstate = 'PE' or gdetail-provstate = 'PQ'
               or gdetail-provstate = 'QC' ).
          move 'CA00000' to ze1bpactx01-taxjurcode.
        elseif ( gdetail-provstate = 'AK' or gdetail-provstate = 'AL'
           or gdetail-provstate = 'AR' or gdetail-provstate = 'AS'
           or gdetail-provstate = 'AZ' or gdetail-provstate = 'CO'
           or gdetail-provstate = 'CT' or gdetail-provstate = 'DC'
           or gdetail-provstate = 'DE' or gdetail-provstate = 'FL'
           or gdetail-provstate = 'GA' or gdetail-provstate = 'GU'
           or gdetail-provstate = 'HI' or gdetail-provstate = 'IA'
           or gdetail-provstate = 'ID' or gdetail-provstate = 'IL'
           or gdetail-provstate = 'IN' or gdetail-provstate = 'KS'
           or gdetail-provstate = 'KY' or gdetail-provstate = 'LA'
           or gdetail-provstate = 'MA' or gdetail-provstate = 'MD'
           or gdetail-provstate = 'ME' or gdetail-provstate = 'MI'
           or gdetail-provstate = 'MN' or gdetail-provstate = 'MO'
           or gdetail-provstate = 'MP' or gdetail-provstate = 'MS'
           or gdetail-provstate = 'MT' or gdetail-provstate = 'NC'
           or gdetail-provstate = 'ND' or gdetail-provstate = 'NE'
           or gdetail-provstate = 'NH' or gdetail-provstate = 'NJ'
           or gdetail-provstate = 'NM' or gdetail-provstate = 'NV'
           or gdetail-provstate = 'NY' or gdetail-provstate = 'OH'
           or gdetail-provstate = 'OK' or gdetail-provstate = 'OR'
           or gdetail-provstate = 'PA' or gdetail-provstate = 'PR'
           or gdetail-provstate = 'RI' or gdetail-provstate = 'SC'
           or gdetail-provstate = 'SD' or gdetail-provstate = 'TN'
           or gdetail-provstate = 'TX' or gdetail-provstate = 'UT'
           or gdetail-provstate = 'VA' or gdetail-provstate = 'VI'
           or gdetail-provstate = 'VT' or gdetail-provstate = 'WA'
           or gdetail-provstate = 'WI' or gdetail-provstate = 'WV'
           or gdetail-provstate = 'WY' or gdetail-provstate = 'DL' ).
          concatenate gdetail-provstate '00000'
                    into ze1bpactx01-taxjurcode.
        elseif gdetail-provstate = 'CA'.
          move 'CL00000' to ze1bpactx01-taxjurcode.
        endif.
      endif.

      if gdetail-itemtext+7(8) = 'REBUTTAL'.
* Rebilling - build tax jurisdiction based on customer region/state
        if ( header-comp_code = '1000' or  header-comp_code = '2000' )
           and ( cust-provstate = 'NB' or cust-provstate = 'NS'
              or cust-provstate = 'NF' or cust-provstate = 'BC'
              or cust-provstate = 'AB' or cust-provstate = 'MB'
              or cust-provstate = 'SK' or cust-provstate = 'ON'
              or cust-provstate = 'PE' or cust-provstate = 'PQ'
              or cust-provstate = 'QC' )
              or gdetail-taxcode = 'SE' .
          move 'JRC1' to ze1bpactx01-cond_key.
        else.
          move 'JR1' to ze1bpactx01-cond_key.
        endif.
      else. " text do NOT have string REBUTTAL
* Regular billing - build tax jurisdiction based on line item
* region/state (where work is performed)
        if ( header-comp_code = '1000' or  header-comp_code = '2000' )
           and ( gdetail-provstate = 'NB' or gdetail-provstate = 'NS'
              or gdetail-provstate = 'NF' or gdetail-provstate = 'BC'
              or gdetail-provstate = 'AB' or gdetail-provstate = 'MB'
              or gdetail-provstate = 'SK' or gdetail-provstate = 'ON'
              or gdetail-provstate = 'PE' or gdetail-provstate = 'PQ'
              or gdetail-provstate = 'QC' )
              or gdetail-taxcode = 'SE' .
          move 'JRC1' to ze1bpactx01-cond_key.
        else.
          move 'JR1' to ze1bpactx01-cond_key.
        endif.
      endif.

      if gdetail-itemtext+7(8) = 'REBUTTAL'.
* Rebilling - build tax jurisdiction based on customer region/state
        if ( cust-provstate = 'NB' or cust-provstate = 'NS'
          or cust-provstate = 'NF' ) and
          ( header-comp_code = '1000' or header-comp_code = '2000' ).
          concatenate 'CH' cust-provstate '000' into
                 ze1bpactx01-taxjurcode_deep.
        elseif ( cust-provstate = 'BC' or cust-provstate = 'AB'
          or cust-provstate = 'MB' or cust-provstate = 'SK'
          or cust-provstate = 'ON' or cust-provstate = 'PE' )
       and ( header-comp_code = '1000' or header-comp_code = '2000' ).
          concatenate 'CA' cust-provstate '000' into
                 ze1bpactx01-taxjurcode_deep.
        elseif ( cust-provstate = 'PQ' or cust-provstate = 'QC' )
       and ( header-comp_code = '1000' or header-comp_code = '2000' ).
          move 'CAQC000' to ze1bpactx01-taxjurcode_deep.
        elseif ( cust-provstate = 'AK' or cust-provstate = 'AL'
           or cust-provstate = 'AR' or cust-provstate = 'AS'
           or cust-provstate = 'AZ' or cust-provstate = 'CO'
           or cust-provstate = 'CT' or cust-provstate = 'DC'
           or cust-provstate = 'DE' or cust-provstate = 'FL'
           or cust-provstate = 'GA' or cust-provstate = 'GU'
           or cust-provstate = 'HI' or cust-provstate = 'IA'
           or cust-provstate = 'ID' or cust-provstate = 'IL'
           or cust-provstate = 'IN' or cust-provstate = 'KS'
           or cust-provstate = 'KY' or cust-provstate = 'LA'
           or cust-provstate = 'MA' or cust-provstate = 'MD'
           or cust-provstate = 'ME' or cust-provstate = 'MI'
           or cust-provstate = 'MN' or cust-provstate = 'MO'
           or cust-provstate = 'MP' or cust-provstate = 'MS'
           or cust-provstate = 'MT' or cust-provstate = 'NC'
           or cust-provstate = 'ND' or cust-provstate = 'NE'
           or cust-provstate = 'NH' or cust-provstate = 'NJ'
           or cust-provstate = 'NM' or cust-provstate = 'NV'
           or cust-provstate = 'NY' or cust-provstate = 'OH'
           or cust-provstate = 'OK' or cust-provstate = 'OR'
           or cust-provstate = 'PA' or cust-provstate = 'PR'
           or cust-provstate = 'RI' or cust-provstate = 'SC'
           or cust-provstate = 'SD' or cust-provstate = 'TN'
           or cust-provstate = 'TX' or cust-provstate = 'UT'
           or cust-provstate = 'VA' or cust-provstate = 'VI'
           or cust-provstate = 'VT' or cust-provstate = 'WA'
           or cust-provstate = 'WI' or cust-provstate = 'WV'
           or cust-provstate = 'WY' or cust-provstate = 'DL' ).
          concatenate cust-provstate '00000'
              into ze1bpactx01-taxjurcode_deep.
        elseif cust-provstate = 'CA'.
          move 'CL00000' to ze1bpactx01-taxjurcode_deep.
        else.
          move 'CA00000' to ze1bpactx01-taxjurcode_deep.
        endif.
      else.  " when string text is NOT REBUTTAL
* Regular billing - build tax jurisdiction based on line item
* region/state (where work is performed)
        if ( gdetail-provstate = 'NB' or gdetail-provstate = 'NS'
          or gdetail-provstate = 'NF' ).
          concatenate 'CH' gdetail-provstate '000' into
                 ze1bpactx01-taxjurcode_deep.
        elseif ( gdetail-provstate = 'BC' or gdetail-provstate = 'AB'
          or gdetail-provstate = 'MB' or gdetail-provstate = 'SK'
          or gdetail-provstate = 'ON' or gdetail-provstate = 'PE' ).
          concatenate 'CA' gdetail-provstate '000' into
                 ze1bpactx01-taxjurcode_deep.
        elseif ( gdetail-provstate = 'PQ' or gdetail-provstate = 'QC' ).
          move 'CAQC000' to ze1bpactx01-taxjurcode_deep.
        elseif ( gdetail-provstate = 'AK' or gdetail-provstate = 'AL'
           or gdetail-provstate = 'AR' or gdetail-provstate = 'AS'
           or gdetail-provstate = 'AZ' or gdetail-provstate = 'CO'
           or gdetail-provstate = 'CT' or gdetail-provstate = 'DC'
           or gdetail-provstate = 'DE' or gdetail-provstate = 'FL'
           or gdetail-provstate = 'GA' or gdetail-provstate = 'GU'
           or gdetail-provstate = 'HI' or gdetail-provstate = 'IA'
           or gdetail-provstate = 'ID' or gdetail-provstate = 'IL'
           or gdetail-provstate = 'IN' or gdetail-provstate = 'KS'
           or gdetail-provstate = 'KY' or gdetail-provstate = 'LA'
           or gdetail-provstate = 'MA' or gdetail-provstate = 'MD'
           or gdetail-provstate = 'ME' or gdetail-provstate = 'MI'
           or gdetail-provstate = 'MN' or gdetail-provstate = 'MO'
           or gdetail-provstate = 'MP' or gdetail-provstate = 'MS'
           or gdetail-provstate = 'MT' or gdetail-provstate = 'NC'
           or gdetail-provstate = 'ND' or gdetail-provstate = 'NE'
           or gdetail-provstate = 'NH' or gdetail-provstate = 'NJ'
           or gdetail-provstate = 'NM' or gdetail-provstate = 'NV'
           or gdetail-provstate = 'NY' or gdetail-provstate = 'OH'
           or gdetail-provstate = 'OK' or gdetail-provstate = 'OR'
           or gdetail-provstate = 'PA' or gdetail-provstate = 'PR'
           or gdetail-provstate = 'RI' or gdetail-provstate = 'SC'
           or gdetail-provstate = 'SD' or gdetail-provstate = 'TN'
           or gdetail-provstate = 'TX' or gdetail-provstate = 'UT'
           or gdetail-provstate = 'VA' or gdetail-provstate = 'VI'
           or gdetail-provstate = 'VT' or gdetail-provstate = 'WA'
           or gdetail-provstate = 'WI' or gdetail-provstate = 'WV'
           or gdetail-provstate = 'WY' or gdetail-provstate = 'DL' ).
          concatenate gdetail-provstate '00000'
             into ze1bpactx01-taxjurcode_deep.
        elseif gdetail-provstate = 'CA'.
          move 'CL00000' to ze1bpactx01-taxjurcode_deep.
        else.
          move 'CA0000' to ze1bpactx01-taxjurcode_deep.
        endif.
      endif.

      zedidd40-docnum          = wdocnum.
* After sort, segnum gets a new value according to config.
      zedidd40-segnum          = '999995'.
      zedidd40-segnam          = 'E1BPACTX01'.
      zedidd40-hlevel          = '02'.
      zedidd40-sdata           = ze1bpactx01.
      append zedidd40.         " e1bpactx01

*E1BPACCR01
      clear ze1bpaccr01.
      clear zedidd40.
      ze1bpaccr01-itemno_acc = witemno_gst. "E1BPACTX01 95
      if cust-curr = 'U'.
        ze1bpaccr01-currency = 'USD'.
      else.
        ze1bpaccr01-currency = 'CAD'.
      endif.

      if header-dt_cr = 'DR'.
        concatenate '-' gdetail-gst_amount into ze1bpaccr01-amt_doccur.
        concatenate '-' gdetail-amount into ze1bpaccr01-amt_base.
      else.
        ze1bpaccr01-amt_doccur = gdetail-gst_amount.
        ze1bpaccr01-amt_base   = gdetail-amount.
      endif.

      zedidd40-docnum          = wdocnum.
*After sort, segnum gets a new value according to config.
      zedidd40-segnum          = '999998'.
      zedidd40-segnam          = 'E1BPACCR01'.
      zedidd40-hlevel          = '02'.
      zedidd40-sdata           = ze1bpaccr01.
      append zedidd40.         " e1bpaccr01
*---------------------------------QST--------------------------------
      tx_write = 'N'.
      if ( gdetail-taxcode = 'SA' or gdetail-taxcode = 'SB' or
           gdetail-taxcode = 'SD' or gdetail-taxcode = 'SC' or
           gdetail-taxcode = 'SE' ) and
         ( gdetail-provstate = 'NB' or gdetail-provstate = 'NS' or
           gdetail-provstate = 'NF' or gdetail-provstate = 'BC' or
           gdetail-provstate = 'AB' or gdetail-provstate = 'MB' or
           gdetail-provstate = 'SK' or gdetail-provstate = 'ON' or
           gdetail-provstate = 'PE' or gdetail-provstate = 'PQ' or
        gdetail-provstate = 'QC' ) .                             "Canada
        tx_write = 'Y'.         " write a qst segment
      endif.

      if ( gdetail-taxcode = 'SA' or gdetail-taxcode = 'SC' ) and
         ( gdetail-provstate = 'AK' or gdetail-provstate = 'AL' or
           gdetail-provstate = 'AR' or gdetail-provstate = 'AS' or
           gdetail-provstate = 'AZ' or gdetail-provstate = 'CO' or
           gdetail-provstate = 'CT' or gdetail-provstate = 'DC' or
           gdetail-provstate = 'DE' or gdetail-provstate = 'FL' or
           gdetail-provstate = 'GA' or gdetail-provstate = 'GU' or
           gdetail-provstate = 'HI' or gdetail-provstate = 'IA' or
           gdetail-provstate = 'ID' or gdetail-provstate = 'IL' or
           gdetail-provstate = 'IN' or gdetail-provstate = 'KS' or
           gdetail-provstate = 'KY' or gdetail-provstate = 'LA' or
           gdetail-provstate = 'MA' or gdetail-provstate = 'MD' or
           gdetail-provstate = 'ME' or gdetail-provstate = 'MI' or
           gdetail-provstate = 'MN' or gdetail-provstate = 'MO' or
           gdetail-provstate = 'MP' or gdetail-provstate = 'MS' or
           gdetail-provstate = 'MT' or gdetail-provstate = 'NC' or
           gdetail-provstate = 'ND' or gdetail-provstate = 'NE' or
           gdetail-provstate = 'NH' or gdetail-provstate = 'NJ' or
           gdetail-provstate = 'NM' or gdetail-provstate = 'NV' or
           gdetail-provstate = 'NY' or gdetail-provstate = 'OH' or
           gdetail-provstate = 'OK' or gdetail-provstate = 'OR' or
           gdetail-provstate = 'PA' or gdetail-provstate = 'PR' or
           gdetail-provstate = 'RI' or gdetail-provstate = 'SC' or
           gdetail-provstate = 'SD' or gdetail-provstate = 'TN' or
           gdetail-provstate = 'TX' or gdetail-provstate = 'UT' or
           gdetail-provstate = 'VA' or gdetail-provstate = 'VI' or
           gdetail-provstate = 'VT' or gdetail-provstate = 'WA' or
           gdetail-provstate = 'WI' or gdetail-provstate = 'WV' or
           gdetail-provstate = 'WY' or gdetail-provstate = 'DL' or
           gdetail-provstate = 'CA' ).
        tx_write = 'Y'.         " write a qst segment
      endif.
*----------------------------------------------------------
      if tx_write = 'Y'.
        clear ze1bpactx01.
        clear zedidd40.
*E1BPACTX01
        witemno_qst = witemno_qst + 1.
        ze1bpactx01-itemno_acc = witemno_qst.

        if gdetail-provstate = 'PQ' or gdetail-provstate = 'QC'.
          ze1bpactx01-gl_account = '0000023210'.
          ze1bpactx01-acct_key   = 'MW2'.
        else.
          ze1bpactx01-gl_account = '0000023220'.
          ze1bpactx01-acct_key   = 'MW3'.
        endif.

*        SELECT SINGLE ktosl FROM  t030k INTO ze1bpactx01-acct_key
*               WHERE  ktopl  = 'CPR'
*               AND    mwskz  = ''
*               AND    konts  = ze1bpactx01-gl_account
*               AND    konth  = ze1bpactx01-gl_account.

        ze1bpactx01-tax_code    = gdetail-taxcode.          "no 2

        if ( gdetail-provstate = 'NB' or gdetail-provstate = 'NS'
          or gdetail-provstate = 'NF' ).
          concatenate 'CH' gdetail-provstate '000' into
                 ze1bpactx01-taxjurcode.
        elseif ( gdetail-provstate = 'BC' or gdetail-provstate = 'AB'
          or gdetail-provstate = 'MB' or gdetail-provstate = 'SK'
          or gdetail-provstate = 'ON' or gdetail-provstate = 'PE' ).
          concatenate 'CA' gdetail-provstate '000' into
                 ze1bpactx01-taxjurcode.
        elseif ( gdetail-provstate = 'PQ'
              or gdetail-provstate = 'QC' ).
          move 'CAQC000' to ze1bpactx01-taxjurcode.
        elseif ( gdetail-provstate = 'AK' or gdetail-provstate = 'AL'
           or gdetail-provstate = 'AR' or gdetail-provstate = 'AS'
           or gdetail-provstate = 'AZ' or gdetail-provstate = 'CO'
           or gdetail-provstate = 'CT' or gdetail-provstate = 'DC'
           or gdetail-provstate = 'DE' or gdetail-provstate = 'FL'
           or gdetail-provstate = 'GA' or gdetail-provstate = 'GU'
           or gdetail-provstate = 'HI' or gdetail-provstate = 'IA'
           or gdetail-provstate = 'ID' or gdetail-provstate = 'IL'
           or gdetail-provstate = 'IN' or gdetail-provstate = 'KS'
           or gdetail-provstate = 'KY' or gdetail-provstate = 'LA'
           or gdetail-provstate = 'MA' or gdetail-provstate = 'MD'
           or gdetail-provstate = 'ME' or gdetail-provstate = 'MI'
           or gdetail-provstate = 'MN' or gdetail-provstate = 'MO'
           or gdetail-provstate = 'MP' or gdetail-provstate = 'MS'
           or gdetail-provstate = 'MT' or gdetail-provstate = 'NC'
           or gdetail-provstate = 'ND' or gdetail-provstate = 'NE'
           or gdetail-provstate = 'NH' or gdetail-provstate = 'NJ'
           or gdetail-provstate = 'NM' or gdetail-provstate = 'NV'
           or gdetail-provstate = 'NY' or gdetail-provstate = 'OH'
           or gdetail-provstate = 'OK' or gdetail-provstate = 'OR'
           or gdetail-provstate = 'PA' or gdetail-provstate = 'PR'
           or gdetail-provstate = 'RI' or gdetail-provstate = 'SC'
           or gdetail-provstate = 'SD' or gdetail-provstate = 'TN'
           or gdetail-provstate = 'TX' or gdetail-provstate = 'UT'
           or gdetail-provstate = 'VA' or gdetail-provstate = 'VI'
           or gdetail-provstate = 'VT' or gdetail-provstate = 'WA'
           or gdetail-provstate = 'WI' or gdetail-provstate = 'WV'
           or gdetail-provstate = 'WY' or gdetail-provstate = 'DL' ).
          concatenate gdetail-provstate '00000'
             into ze1bpactx01-taxjurcode.
        elseif gdetail-provstate = 'CA'.
          move 'CL00000' to ze1bpactx01-taxjurcode.
        endif.

        ze1bpactx01-taxjurcode_deep = ze1bpactx01-taxjurcode.

        clear wa_land1.
        if gdetail-provstate = 'PQ'.  "Quebec - OLD
          select single land1 from  t005s into wa_land1
                 where  land1  = 'CA'
                 and    bland  = 'PQ'
                 and    fprcd  = ''
                 and    herbl  = ''.
        else.
          select single land1 from  t005s into wa_land1
                 where  land1  = 'CA'
                 and    bland  = gdetail-provstate
                 and    fprcd  = gdetail-provstate
                 and    herbl  = ''.
        endif.
        if sy-subrc = 0.
          wa_land1 = 'CA'.
        else.
          wa_land1 = 'US'.
        endif.

* Get the latest valid record.
        refresh wa_a053.
        clear wa_a053.
        select * from  a053 into wa_a053
               where  mwskz  = gdetail-taxcode
               and    aland  = wa_land1
               and    txjcd  = ze1bpactx01-taxjurcode.
               append wa_a053.
        endselect.
        sort wa_a053 by datab.
        loop at wa_a053.
          ze1bpactx01-cond_key = wa_a053-kschl.
        endloop.

        zedidd40-docnum          = wdocnum.
*After sort, segnum gets a new value according to config.
        zedidd40-segnum          = '999996'.
        zedidd40-segnam          = 'E1BPACTX01'.
        zedidd40-hlevel          = '02'.
        zedidd40-sdata           = ze1bpactx01.
        append zedidd40.         " e1bpactx01

*E1BPACCR01
        clear ze1bpaccr01.
        clear zedidd40.
        ze1bpaccr01-itemno_acc = witemno_qst. "E1BPACTX01 96
        if cust-curr = 'U'.
          ze1bpaccr01-currency = 'USD'.
        else.
          ze1bpaccr01-currency = 'CAD'.
        endif.

        if header-dt_cr = 'DR'.
         concatenate '-' gdetail-qst_amount into ze1bpaccr01-amt_doccur.
        else.
          ze1bpaccr01-amt_doccur = gdetail-gst_amount.
        endif.

        if gdetail-qst_amount <> 0.
          if header-dt_cr = 'DR'.
            ze1bpaccr01-amt_base = gdetail-amount + gdetail-gst_amount.
            condense ze1bpaccr01-amt_base.
         concatenate '-' ze1bpaccr01-amt_base into ze1bpaccr01-amt_base.
          else.
            ze1bpaccr01-amt_base = gdetail-amount + gdetail-gst_amount.
            condense ze1bpaccr01-amt_base.
          endif.
        else.  " qst amount is zero
          if header-dt_cr = 'DR'.
            ze1bpaccr01-amt_base = gdetail-amount.
            condense ze1bpaccr01-amt_base.
         concatenate '-' ze1bpaccr01-amt_base into ze1bpaccr01-amt_base.
          else.
            ze1bpaccr01-amt_base = gdetail-amount.
            condense ze1bpaccr01-amt_base.
          endif.
        endif.

        zedidd40-docnum          = wdocnum.
*After sort, segnum gets a new value according to config.
        zedidd40-segnum          = '999999'.
        zedidd40-segnam          = 'E1BPACCR01'.
        zedidd40-hlevel          = '02'.
        zedidd40-sdata           = ze1bpaccr01.
        append zedidd40.         " e1bpaccr01
      endif.    " end gdetail
    endif. "tx_write
  endloop.
*-------------------------SORT-----------------------------
* To have it the correct order according to config
  sort zedidd40 by docnum segnum sdata(10).
  loop at zedidd40.
    if zedidd40-segnam <> 'E1BPACCR01' and
      zedidd40-segnam <> 'E1BPACTX01'.
      wsegnum = zedidd40-segnum.
      witemno = zedidd40-sdata(10).
      witemno_new = 0.
    endif.
    if zedidd40-segnum = '999995'   "E1BPACTX01  gst
    or zedidd40-segnum = '999996'.  "E1BPACTX01  qst
      wsegnum = wsegnum + 1.
      zedidd40-segnum = wsegnum.
      witemno = witemno + 1.
      zedidd40-sdata(10) = witemno.
      modify zedidd40.
    endif.
    if zedidd40-segnum > '999996'."E1BPACCR01 gl,then gst, then qst.
      wsegnum = wsegnum + 1.
      zedidd40-segnum = wsegnum.
      witemno_new = witemno_new + 1.
      zedidd40-sdata(10) = witemno_new.
      modify zedidd40.
    endif.
  endloop.

*------------------------Idocs-----------------------------
  if p_proc = 'X'.
    call function 'IDOC_INBOUND_ASYNCHRONOUS'
         tables
              idoc_control_rec_40 = zedidc40
              idoc_data_rec_40    = zedidd40.
  endif.

  skip 2.
  write :/ 'Number of Idocs ', wdocnum.
endform.                    " proc_data

*Text elements
*----------------------------------------------------------
* 004 Input file from NT server
* 006 Create Idocs
* 008 Print input file

*Selection texts
*----------------------------------------------------------
* P_FLNM         File Name for Output
* P_INBD         Input file from NT server
* P_INPT         Input File name
* P_PROC         Create Idocs
* P_PRT         Print input file

*Messages
*----------------------------------------------------------
*
* Message class: 01
*319   & & & &
*
* Message class: ZS
*020
*022

--------------------------------------------------------------------------------

  • No labels