Produce Bookkeeping Voucher

*&---------------------------------------------------------------------*
*&      Form  BUCHHALTUNGSBELEG_ERZEUGEN (bookkeepingvoucher_produce)
*&---------------------------------------------------------------------*
*       text                                                           *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form buchhaltungsbeleg_erzeugen " bookkeepingvoucher_produce
       tables
         lt_acchd structure acchd " FI header 
         lt_accit structure accit " FI line items 
         lt_acccr structure acccr " FI Currency Information
         lt_vbreve structure vbrevevb " revenue recognition lines
         ot_xvbfs  structure vbfs " Error Log for Collective Processing
         lt_error  structure vbrevevb
       using
         ld_zukri_old like vbrk-zukri " doc, line, period - level 3
         ld_logsys    like t000-logsys
       changing
         os_vbsk  like vbskvb  " Collective Processing - Sales Doc
         ld_vbnum like vbsk-vbnum " number of created documents
         ld_anzlp like vbsk-anzlp " Number of delivery items created
         ld_vbeln_old like vbrevav-vbeln " sales doc
         ld_returncode like sy-subrc
         ld_posnr      like sy-tabix  " line item
         ld_sammg      like vbsk-sammg " Collective group number
         ld_rrrel      like vbrevk-rrrel. " Rev rec cat D (time - after bill)

  data: doc_vbnum like vbsk-vbnum.
  data: doc_anzlp like vbsk-anzlp.
  data: lvf_awtyp like acchd-awtyp.

  describe table lt_acchd lines doc_vbnum.
  describe table lt_accit lines doc_anzlp.
  ld_returncode = 4.
  check : not doc_vbnum is initial.         " first time through leave no line items
  append lines of gt_col_accit to lt_accit. " adds line 1 to line coming in
  sort lt_accit by awtyp awref aworg posnr. " VBRK |0090013185|          |0000000002 from fst to last
  append lines of gt_col_acccr to lt_acccr.
  sort lt_acccr by awtyp awref aworg posnr.

  call function 'OUTBOUND_CALL_00503110_E' " this might be a user exit - reads "event" table
       tables
            fit_acchd = lt_acchd
            fit_acccr = lt_acccr
            fit_accit = lt_accit.

  call function 'AC_DOCUMENT_CREATE'
       tables
            t_acchd       = lt_acchd
            t_accit       = lt_accit
            t_acccr       = lt_acccr
       exceptions
            error_message = 01.

  ld_returncode = sy-subrc.
  if not ld_returncode is initial. " error is returned
    perform fehlertabelle_fuellen
              tables
                lt_accit
                lt_error
              using
                ld_sammg.
    refresh: lt_acchd.
    refresh: lt_accit.
    refresh: lt_acccr.

*   Fill error protokol
    loop at lt_error where vbeln = ld_vbeln_old.
      perform vbfs_hinzufuegen
                tables
                  ot_xvbfs
                using
                  os_vbsk
                  lt_error-vbeln
                  lt_error-posnr
                  sy-msgno      " 151
                  sy-msgv1      " $       1 - incorrect document # for RV
                  sy-msgv2      " 0090000000
                  sy-msgv3      " 0099999999
                  sy-msgid.     " F5
    endloop.

  else.
    ld_vbnum = ld_vbnum + doc_vbnum.
    ld_anzlp = ld_anzlp + doc_anzlp.
    ld_vbeln_old = ld_zukri_old.
    if ld_rrrel eq gcf_rrrel_billtime.
      lvf_awtyp = gcf_vbrk.
    else.
      lvf_awtyp = gcf_vbak.
    endif.

    call function 'AC_DOCUMENT_POST'
         exporting
              i_awtyp = lvf_awtyp
              i_awref = ld_vbeln_old
              i_awsys = ld_logsys
         exceptions
              others  = 1.
    ld_returncode = sy-subrc.
  endif.

* Initialisation RW-Interface tables
  clear:   lt_accit,
           lt_acccr,
           gt_col_accit,
           gt_col_acccr.
  refresh: gt_col_accit,
           gt_col_acccr.

endform.