Last active
July 23, 2019 18:19
-
-
Save mydoghasworms/41232a1b8595704b090b to your computer and use it in GitHub Desktop.
Example of using Ruby nwrfc gem to post documents to an SAP ECC system using BAPI_ACC_DOCUMENT_POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'nwrfc' | |
require 'logger' | |
include NWRFC | |
# This is an example of a Ruby script using the nwrfc gem to post Accounting documents to | |
# an SAP ECC system using BAPI_ACC_DOCUMENT_POST. You can obviously easily adapt it to | |
# your needs | |
# Set up logger | |
logger = Logger.new('postings.log', 10, 4096000) | |
#-- Configuration options | |
# File from which to read input | |
INPUT_FILE = 'prod1.txt' | |
# NetWeaver connection details | |
NW_LOGON = {'user' => 'username', 'passwd' => 'password', #'trace' => 2, | |
'client' => '100', 'ashost' => 'prod.domain.example.com', 'sysnr' => '00'} | |
# Warning: Setting commit work to false will cause nothing to be actually saved, but document numbers | |
# do get used up from the number range! | |
COMMIT_WORK = true | |
#-- Connect to NW system | |
conn = Connection.new(NW_LOGON) | |
#-- Get function definitions and caller instances | |
fn_accdoc = conn.get_function('BAPI_ACC_DOCUMENT_POST') | |
fc_accdoc = fn_accdoc.get_function_call | |
fn_commit = conn.get_function('BAPI_TRANSACTION_COMMIT') | |
fc_commit = fn_commit.get_function_call | |
logger.info('START OF RUN') | |
logger.info("Commit work option set to #{COMMIT_WORK}") | |
#-- Open the input file | |
logger.info("Opening file #{INPUT_FILE} for input") | |
logger.info('---------------------------------------------------------------------------------------') | |
input_file = open(INPUT_FILE) | |
#-- Process individual lines in file | |
input_file.each {|line| | |
fields = line.split("\t").map {|r| r.strip} | |
logger.info("Posting doc for ref doc #{fields[6]} in co.code #{fields[4]} pst date #{fields[2]}") | |
#-- Fill header for accounting document | |
fc_accdoc[:DOCUMENTHEADER][:USERNAME] = NW_LOGON['user'] | |
fc_accdoc[:DOCUMENTHEADER][:DOC_DATE] = fields[1] | |
fc_accdoc[:DOCUMENTHEADER][:PSTNG_DATE] = fields[2] | |
fc_accdoc[:DOCUMENTHEADER][:DOC_TYPE] = fields[3] | |
fc_accdoc[:DOCUMENTHEADER][:COMP_CODE] = fields[4] | |
fc_accdoc[:DOCUMENTHEADER][:REF_DOC_NO] = fields[6] | |
# Item 1 | |
ar = fc_accdoc[:ACCOUNTRECEIVABLE].new_row | |
ar[:ITEMNO_ACC] = 1 | |
ar[:CUSTOMER] = fields[8] | |
ar[:ALLOC_NMBR] = fields[13] | |
ar[:PROFIT_CTR] = fields[12] | |
ar[:PAYMT_REF] = fields[16] | |
ar[:PMNTTRMS] = fields[15] | |
cur = fc_accdoc[:CURRENCYAMOUNT].new_row | |
cur[:ITEMNO_ACC] = 1 | |
cur[:CURRENCY_ISO] = fields[5] | |
cur[:AMT_DOCCUR] = fields[9] | |
# Item 2 | |
ar = fc_accdoc[:ACCOUNTRECEIVABLE].new_row | |
ar[:ITEMNO_ACC] = 2 | |
ar[:CUSTOMER] = fields[10] | |
ar[:ALLOC_NMBR] = fields[13] | |
ar[:PROFIT_CTR] = fields[12] | |
ar[:PAYMT_REF] = fields[16] | |
cur = fc_accdoc[:CURRENCYAMOUNT].new_row | |
cur[:ITEMNO_ACC] = 2 | |
cur[:CURRENCY_ISO] = fields[5] | |
cur[:AMT_DOCCUR] = fields[11] | |
#-- Execute the function | |
fc_accdoc.invoke | |
#-- Log Output messages | |
fc_accdoc[:RETURN].each { |row| | |
log_entry = "#{row[:TYPE]} #{row[:ID]} #{row[:NUMBER]} #{row[:MESSAGE]}" | |
if row[:TYPE] == 'A' or row[:TYPE] == 'E' or row[:TYPE] == 'X' | |
logger.error(log_entry) | |
elsif row[:TYPE] == 'W' | |
logger.warn(log_entry) | |
else | |
logger.info(log_entry) | |
end | |
} | |
logger.info('---------------------------------------------------------------------------------------') | |
fc_accdoc[:ACCOUNTRECEIVABLE].clear | |
fc_accdoc[:CURRENCYAMOUNT].clear | |
#-- Invoke function to commit if specified | |
fc_commit.invoke if COMMIT_WORK | |
} | |
logger.info('END OF RUN') | |
conn.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fields for BAPI_ACC_DOCUMENT_POST:
:OBJ_KEY,
:OBJ_SYS,
:OBJ_TYPE,
:CONTRACTHEADER,
:CUSTOMERCPD,
:DOCUMENTHEADER,
:ACCOUNTGL,
:ACCOUNTPAYABLE,
:ACCOUNTRECEIVABLE,
:ACCOUNTTAX,
:ACCOUNTWT,
:CONTRACTITEM,
:CRITERIA,
:CURRENCYAMOUNT,
:EXTENSION1,
:EXTENSION2,
:PAYMENTCARD,
:REALESTATE,
:RETURN,
:VALUEFIELD