Tuesday, 14 October 2014

Creating an Invoice...

Clearly you'll need to make sure that the GLAccounts, Payment Terms etc exist, but feel free to use this as a template to create your Invoices...


*&---------------------------------------------------------------------*
*&  Include           Z_PD_CAPS1_DEMOS_INV_CREATE                      *
*&---------------------------------------------------------------------*

data:
obj_type like bapiache02-obj_type,
obj_key like bapiache02-obj_key,
obj_sys like bapiache02-obj_sys,
documentheader like bapiache09,

accountgl like bapiacgl09 occurs 0 with header line,
tax_accountgl like bapiacgl09 OCCURS 0 WITH HEADER LINE,
currencyamount like bapiaccr09 occurs 0 with header line,
tax_currencyamount like bapiaccr09 OCCURS 0 WITH HEADER LINE,
return like bapiret2 occurs 0 with header line,
extension1 like bapiextc occurs 0 with header line,

t_edidd like edidd occurs 0 with header line,
bapi_retn_info like bapiret2 occurs 0 with header line.
data: error_flag.
data: wa_akont TYPE akont.

data: t_accountreceivable TYPE TABLE OF BAPIACAR09 WITH HEADER LINE.
datA: T_MWDAT TYPE TABLE OF RTAX1U15 WITH HEADER LINE.
data: wa_wrbtr type wrbtr.
data: curr_li TYPE POSNR_ACC.
data: v_tabix like sy-tabix.


documentheader-username = sy-uname.
documentheader-header_txt = 'Test using BAPI'.
documentheader-comp_code = 'CF01'.
documentheader-doc_date = sy-datum.
documentheader-pstng_date = sy-datum.
documentheader-doc_type = 'SA'.
documentheader-REF_DOC_NO = 'INVOICE_01'.

clear accountgl.
clear wa_akont.

t_accountreceivable-ITEMNO_ACC = '1'.
t_accountreceivable-customer = 'C524'.
"t_accountreceivable-TAX_CODE = 'A1'.
t_accountreceivable-gl_account = '0000400000'.
t_accountreceivable-PMNTTRMS = 'Z030'.
t_accountreceivable-ALLOC_NMBR = 'LORDS0820'.
t_accountreceivable-ITEM_TEXT = 'Facility Name'.
append t_accountreceivable.


accountgl-itemno_acc = '2'.
accountgl-gl_account = '0000802010'.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-profit_ctr = '0000069314'.
"accountgl-costcenter = '0069314001'.
accountgl-ITEM_TEXT = 'Item Text'.
accountgl-ORDERID = 'LORDS0820'.
accountgl-TAX_CODE = 'A0'.
append accountgl.

clear accountgl.
accountgl-itemno_acc = '3'.
accountgl-gl_account = '0000802010'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT         = accountgl-gl_account
 IMPORTING
   OUTPUT        = accountgl-gl_account.

accountgl-ITEM_TEXT = 'Item Text'.
accountgl-ORDERID = 'LORDS0820'.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-TAX_CODE = 'A1'.
append accountgl.

clear accountgl.
accountgl-itemno_acc = '4'.
accountgl-gl_account = '0000711350'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT         = accountgl-gl_account
 IMPORTING
   OUTPUT        = accountgl-gl_account.
          .

accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-TAX_CODE = 'V0'.
accountgl-costcenter = '69314001'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT         = accountgl-costcenter
 IMPORTING
   OUTPUT        = accountgl-costcenter.

append accountgl.

currencyamount-itemno_acc = '1'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '470.00'.
append currencyamount.

currencyamount-itemno_acc = '2'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-230.00'.
append currencyamount.

currencyamount-itemno_acc = '3'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-235.00'.
append currencyamount.

currencyamount-itemno_acc = '4'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-5.00'.
append currencyamount.

"PD We're now calculating the tax. Need to calculate any tax at the
"appropriate rate.

"work out the first line that tax can go on.
loop at accountgl.
endloop.
curr_li = accountgl-ITEMNO_ACC.
curr_li = curr_li + 1.

loop at accountgl.
  READ TABLE currencyamount with key ITEMNO_ACC =
accountgl-ITEMNO_ACC.
  check sy-subrc = 0.
  v_tabix = sy-tabix.
  wa_wrbtr = currencyamount-amt_doccur.
  CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'
    EXPORTING
      I_BUKRS = accountgl-comp_code
      I_MWSKZ = accountgl-TAX_CODE
      I_WAERS = currencyamount-currency
      I_WRBTR = wa_wrbtr
    TABLES
      T_MWDAT = t_mwdat
    EXCEPTIONS
      BUKRS_NOT_FOUND = 1
      COUNTRY_NOT_FOUND = 2
      MWSKZ_NOT_DEFINED = 3
      MWSKZ_NOT_VALID   = 4
      ACCOUNT_NOT_FOUND = 5
      DIFFERENT_DISCOUNT_BASE = 6
      DIFFERENT_TAX_BASE            = 7
      TXJCD_NOT_VALID               = 8
      NOT_FOUND                     = 9
      KTOSL_NOT_FOUND               = 10
      KALSM_NOT_FOUND               = 11
      PARAMETER_ERROR               = 12
      KNUMH_NOT_FOUND               = 13
      KSCHL_NOT_FOUND               = 14
      UNKNOWN_ERROR                 = 15
      OTHERS                        = 16.

IF SY-SUBRC <> 0.
 CONTINUE.
ENDIF.

READ TABLE T_MWDAT INDEX 1.
check sy-subrc = 0.
"There was tax there!!
if t_mwdat-wmwst is not INITIAL.
clear tax_accountgl.
tax_accountgl-gl_account = '402560'.
tax_accountgl-ITEMNO_ACC = curr_li.
tax_accountgl-tax_code = accountgl-tax_code.


CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT         = tax_accountgl-gl_account
 IMPORTING
   OUTPUT        = tax_accountgl-gl_account.

append tax_accountgl.



clear tax_currencyamount.
tax_currencyamount = currencyamount.
tax_currencyamount-amt_doccur = T_MWDAT-wmwst.
tax_currencyamount-ITEMNO_ACC = curr_li.
append tax_currencyamount.

currencyamount-amt_doccur = T_MWDAT-KAWRT.
modify currencyamount index v_tabix.

curr_li = curr_li + 1.

endif.
endloop.

append LINES OF tax_accountgl to accountgl.
append LINES OF tax_currencyamount to currencyamount.


CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
  EXPORTING
    DOCUMENTHEADER          = documentheader
  TABLES
    ACCOUNTGL               = accountgl
    ACCOUNTRECEIVABLE       = t_accountreceivable
    CURRENCYAMOUNT          = currencyamount
    RETURN                  = return          .


CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
 EXPORTING
    WAIT          = 'X'.


loop at return.
  write return-message.
endloop.

No comments:

Post a Comment