Friday, 15 May 2015

Multiple Payment / Clearing of FI Documents

In trying to find the equivalent of F-28, with the most promising best result being this one I've got a program that now can perform clearing of multiple invoices... It reads in a file with Invoice#s and Payment amounts, creates payment documents, then clears them against the SumTotalled Incoming Payment.

Header program here:

REPORT  YPD_INV_PAY_DEV.

include YPD_INV_PAY_DEV_data.
include YPD_INV_PAY_DEV_sels.

start-of-selection.
  perform load_file.

"PD- this was the bit that did the process / clearing incrementally....
*  loop at lt_remittance_file into ls_remittance_file.
*      perform process_payment.
*      IF p_clear = 'X'.
*        perform do_clearing.
*      endif.
*  endloop.



  loop at lt_remittance_file into ls_remittance_file.
      perform process_payment.
  endloop.

  append ls_currencyamount1 to it_currencyamount.
  append ls_currencyamount2 to it_currencyamount.

perform call_bapi using '15'.

commit work and wait.

wait up to 4 seconds.

      IF p_clear = 'X'.
        perform do_clearing2.
      endif.


include YPD_INV_PAY_DEV_forms.


---------------------------------------------------------------------------------------
include YPD_INV_PAY_DEV_data.

data: w_bsid type bsid.


data:
  gd_documentheader    like bapiache09,
  gd_customercpd       like bapiacpa09,
  gd_fica_hd           like bapiaccahd,
  it_accountreceivable like table of bapiacar09 with header line,
  it_accountgl         like table of bapiacgl09 with header line,
  it_accounttax        like table of bapiactx09 with header line,
  it_criteria          like table of bapiackec9 with header line,
  it_valuefield        like table of bapiackev9 with header line,
  it_currencyamount    like table of bapiaccr09 with header line,
  ls_currencyamount1    type bapiaccr09,
  ls_currencyamount2    type bapiaccr09,

  it_return            like table of bapiret2   with header line,
  it_receivers         like table of bdi_logsys with header line,
  it_fica_it           like table of bapiaccait with header line,
  it_accountpayable    like table of bapiacap09 with header line,
  it_paymentcard       like table of bapiacpc09 with header line,
  it_ext               like table of bapiacextc with header line.
*  it_re                LIKE TABLE OF bapiacre09 WITH HEADER LINE,
*  it_ext2              LIKE TABLE OF bapiparex  WITH HEADER LINE.

  data: w_type like gd_documentheader-obj_type,
        w_key  like gd_documentheader-obj_key,
        w_sys  like gd_documentheader-obj_sys.

  data: w_difference type wrbtr,
        w_overpayment type wdy_boolean.


types:
  begin of ty_li_remittance_file,
    bschl(2)  type c,               "posting key
    belnr(10) type c,               "doc number
    wrbtr(17) type c,               "amount
    hkont(10) type c,               "GL account
    kunnr(10) type c,               "customer
    kostl(10) type c,               "cost centre
    prctr(10) type c,               "profit centre
    zuonr(18) type c,               "allocation
    sgtxt(50) type c,               "text
  end   of ty_li_remittance_file.


data:
  lt_remittance_file type standard table of ty_li_remittance_file,
  ls_remittance_file type ty_li_remittance_file.


"PD+ for multiple invoice / payment clearances in 1 clearing doc't.
data: t_ausz1 type table of ausz1,
      w_ausz1 type ausz1,
      t_ausz2 type table of ausz2,
      w_ausz2 type ausz2.

data: w_bsid2 type bsid,
      w_bkpf type bkpf,
      w_bkpf2 type bkpf,
      l_belnr type belnr_d,
      l_bukrs type bukrs,
      l_gjahr type gjahr.


types: begin of tty_payments,
        belnr type belnr_d,
        bukrs type bukrs,
        gjahr type gjahr,
       end of tty_payments.

data: w_payments type tty_payments,
      t_payments type table of tty_payments.

data: w_invoices type tty_payments,
      t_invoices type table of tty_payments.



---------------------------------------------------------------------------------------
include YPD_INV_PAY_DEV_sels.

parameters: p_bukrs type bukrs default '2000',
*            p_belnr type belnr_d default '1800000016',
            p_gjahr type gjahr default '2015',
            p_hkont type hkont default '113108'.
*            P_AMT TYPE WRBTR default '100.00'.



parameter: p_clear type wdy_boolean as checkbox default 'X'.
parameters: p_hkontD type hkont default '140000'.



parameter: p_file type char80 default 'C:\Users\Patrick.Dean\Desktop\test.txt'.


---------------------------------------------------------------------------------------
include YPD_INV_PAY_DEV_forms.

form do_clearing2.

loop at t_invoices into w_invoices.


select single * from bsid into w_bsid where belnr = w_invoices-belnr
                                        and gjahr = w_invoices-gjahr
                                        and bukrs = w_invoices-bukrs.

select single * from bkpf into w_bkpf where belnr = w_invoices-belnr
                                        and gjahr = w_invoices-gjahr
                                        and bukrs = w_invoices-bukrs.
check sy-subrc = 0.

"Invoice
move-corresponding w_bsid to w_AUSZ1.
move-corresponding w_bkpf to w_ausz1.

w_ausz2-aktio = 'A'.
append w_AUSZ1 to T_AUSZ1.

endloop.

loop at t_payments into w_payments.

select single * into w_bsid2 from bsid where belnr = w_payments-belnr
                                         and bukrs = w_payments-bukrs
                                         and gjahr = w_payments-gjahr.
  check sy-subrc = 0.

select single * from bkpf into w_bkpf2 where belnr = w_bsid2-belnr
                                         and gjahr = w_bsid2-gjahr
                                         and bukrs = w_bsid2-bukrs.


clear: w_ausz1, w_ausz2.
move-corresponding w_bsid2 to w_AUSZ1.
move-corresponding w_bkpf2 to w_ausz1.
append w_AUSZ1 to T_AUSZ1.


w_AUSZ2-augbl = w_bkpf2-belnr.
w_AUSZ2-augdt = w_bkpf2-budat.
w_AUSZ2-BUKRS = w_bsid2-BUKRS.
append w_AUSZ2 to T_AUSZ2.

endloop.

  CALL FUNCTION 'CLEAR_DOCUMENTS'
    TABLES
      T_AUSZ1       = t_ausz1
      T_AUSZ2       = t_ausz2.


COMMIT WORK AND WAIT.


endform.


form process_payment.

data: l_wrbtr type wrbtr,
      l_initialised.

select single * from bsid into w_bsid where gjahr = p_gjahr
                                        and belnr = ls_remittance_file-BELNR
                                        and bukrs = p_bukrs.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      INPUT         = w_bsid-kunnr
    IMPORTING
      OUTPUT        = w_bsid-kunnr.


l_wrbtr = ls_remittance_file-wrbtr.

if l_initialised is initial.
 perform fill_header.
 perform fill_accountgl using p_hkont.
 perform fill_accountar.
 l_initialised = 'X'.
endif.


perform fill_currencyamount using L_WRBTR.



w_invoices-gjahr = p_gjahr.
w_invoices-belnr = ls_remittance_file-belnr.
w_invoices-bukrs = p_bukrs.
append w_invoices to t_invoices.


endform.

form fill_header.

  gd_documentheader-username   = sy-uname.
  gd_documentheader-header_txt = 'BAPI Test'.               "#EC NOTEXT
  gd_documentheader-comp_code  = w_bsid-bukrs.
  gd_documentheader-fisc_year  = sy-datum(4).
  gd_documentheader-doc_date   = sy-datum.
  gd_documentheader-pstng_date = sy-datum.
  gd_documentheader-doc_type   = 'DZ'.
  gd_documentheader-ref_doc_no = 'xxxx'.
  gd_documentheader-bus_act    = 'RFBU'.

endform.

form fill_accountgl using lp_hkont type hkont.

  REFRESH IT_ACCOUNTGL.

  clear it_accountgl.
  it_accountgl-itemno_acc     = 1.
  it_accountgl-gl_account     = lp_hkont.
  it_accountgl-item_text      = 'BAPI Test G/L line item'.  "#EC NOTEXT
  it_accountgl-comp_code      = w_bsid-bukrs.
  append it_accountgl.

endform.

form fill_accountar.
  REFRESH it_accountreceivable.

 CLEAR it_accountreceivable.
  it_accountreceivable-itemno_acc = 2.
  it_accountreceivable-customer   = w_bsid-kunnr.
  IT_ACCOUNTRECEIVABLE-PMNTTRMS   = w_bsid-zterm.
  IT_ACCOUNTRECEIVABLE-GL_ACCOUNT = w_bsid-hkont.
  it_accountreceivable-comp_code = w_bsid-bukrs.
 APPEND it_accountreceivable.


endform.                    "fill_accountar


form fill_currencyamount using lp_amt type wrbtr.

  ls_currencyamount1-itemno_acc   = 1.
  ls_currencyamount1-curr_type    = '00'.
  ls_currencyamount1-currency     = w_bsid-waers.
  ls_currencyamount1-CURRENCY_ISO = w_bsid-waers.
  ls_currencyamount1-amt_doccur   = lp_amt + ls_currencyamount1-amt_doccur.


  ls_currencyamount2-itemno_acc   = 2.
  ls_currencyamount2-curr_type    = '00'.
  ls_currencyamount2-currency     = w_bsid-waers.
  ls_currencyamount2-CURRENCY_ISO = w_bsid-waers.
  ls_currencyamount2-amt_doccur   = ls_currencyamount1-amt_doccur  * -1.


endform.

form call_bapi using p_bschl type bschl.

data : it_BAPIACEXTC type table of BAPIACEXTC,
         wa_BAPIACEXTC  type BAPIACEXTC.

"     Populate the Extension table
      wa_BAPIACEXTC-field1  = 'POSTING_KEY'.
      wa_BAPIACEXTC-field2 = '2'.            " Item number
      wa_BAPIACEXTC-field3 = p_bschl.
      APPEND wa_BAPIACEXTC TO it_BAPIACEXTC.



   call function 'BAPI_ACC_DOCUMENT_POST'
      exporting
        documentheader    = gd_documentheader
      importing
        obj_type          = w_type
        obj_key           = w_key
        obj_sys           = w_sys
      tables
        accountgl         = it_accountgl
        accountreceivable = it_accountreceivable
        currencyamount    = it_currencyamount
        extension1        = it_BAPIACEXTC
        return            = it_return.

commit work and wait.

loop at it_return.
  write:/ it_return-message.
endloop.

w_payments-belnr = w_key(10).
w_payments-bukrs = w_key+10(4).
w_payments-gjahr = w_key+14(4).
append w_payments to t_payments.


endform.


form do_clearing.




  wait up to 2 seconds.



l_belnr = w_key(10).
l_bukrs = w_key+10(4).
l_gjahr = w_key+14(4).

select single * into w_bsid2 from bsid where belnr = l_belnr
                                         and bukrs = l_bukrs
                                         and gjahr = l_gjahr.

select single * from bkpf into w_bkpf where belnr = w_bsid-belnr
                                        and gjahr = w_bsid-gjahr
                                        and bukrs = w_bsid-bukrs.

select single * from bkpf into w_bkpf2 where belnr = w_bsid2-belnr
                                         and gjahr = w_bsid2-gjahr
                                         and bukrs = w_bsid2-bukrs.

"Invoice
move-corresponding w_bsid to w_AUSZ1.
move-corresponding w_bkpf to w_ausz1.

w_ausz2-aktio = 'A'.
append w_AUSZ1 to T_AUSZ1.
*append w_AUSZ2 to T_AUSZ2.
*
"Payment
clear: w_ausz1, w_ausz2.
move-corresponding w_bsid2 to w_AUSZ1.
move-corresponding w_bkpf2 to w_ausz1.
append w_AUSZ1 to T_AUSZ1.

"Difference.

*append w_AUSZ1 to T_AUSZ1.


w_AUSZ2-augbl = w_bkpf2-belnr.
w_AUSZ2-augdt = w_bkpf2-budat.
w_AUSZ2-BUKRS = w_bsid2-BUKRS.
append w_AUSZ2 to T_AUSZ2.

  CALL FUNCTION 'CLEAR_DOCUMENTS'
    TABLES
      T_AUSZ1       = t_ausz1
      T_AUSZ2       = t_ausz2.


COMMIT WORK AND WAIT.


endform.

form clear_bapi_tabs.
  refresh: it_accountgl, it_accountreceivable, it_currencyamount, it_return.

endform.

form load_file.

  data: l_filename type RLGRAP-FILENAME.

  l_filename = p_file.


  call function 'WS_UPLOAD'
    exporting
      filename                      = l_filename
      filetype                      = 'DAT'
    tables
      data_tab                      = lt_remittance_file.



commit work and wait.



endform.



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


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

No comments:

Post a Comment