This one is roughly the same size as the form it's describing, and tells us absolutely nothing useful. Needless to say I scrubbed this pollution as quickly as I could, and got on with my code... (after ShamingButNotNaming on my blog, of course...)
Wednesday, 29 October 2014
Tuesday, 28 October 2014
Iterate Up the HR Structure
Thanks to Kat for this one... I've got to get the UserID of the Users Manager... trouble is, sometimes the OrgStructure positions aren't filled - so I have to see who's sitting in the position. If there's no-one in it, then I loop back round and get the OrgStructure's superior position (since there's no Personnel person to check on!). I repeat this until I find someone, and stop at 20 goes... If this has happened, it's either
the guy at the top of the tree (we try 20 times to work out if the guy has a useful boss, but fail because it's Paul Solomon :)
OR
there's such a massive Org Structure that's not populated with stuff, that you should probably question what the point of the structure is!!
do 20 times. "Don't want to infinite loop...
refresh t_leadpos.
CALL FUNCTION 'RH_GET_LEADING_POSITION'
EXPORTING
plvar = w_plvar
otype = l_otype "can be US (user), P (Person), S (position) or O (Org unit)
sobid = l_sobid
date = sy-datum
auth = ' '
TABLES
leading_pos = t_leadpos
EXCEPTIONS
no_lead_pos_found = 1
OTHERS = 2.
check t_leadpos[] is not initial.
read table t_leadpos into w_leadpos index 1.
REFRESH t_holders.
CALL FUNCTION 'HRWPC_RFC_PP_HOLDERS'
EXPORTING
plvar = w_plvar "usually 01
otype = 'S'
objid = w_leadpos-objid "from above table read
TABLES
holder = t_holders
EXCEPTIONS
no_active_plvar = 1
infty_not_found = 2
nothing_found = 3
OTHERS = 4.
IF sy-subrc = 0 AND t_holders IS NOT INITIAL.
READ TABLE t_holders INTO w_holders INDEX 1.
read table t_pa0105 into w_pa0105 with key pernr = w_holders-pernr
subty = '0001'.
if sy-subrc = 0.
p_output-manager = w_pa0105-usrid.
exit.
endif.
else. "Didn't find holders for the position... Go and find the Super-Position.
l_otype = w_leadpos-otype. "Probably 'S' but hey, let's make it dynamic.
l_sobid = w_leadpos-objid.
endif. "Found holders for the position.
enddo. "Iterate up the structure.
the guy at the top of the tree (we try 20 times to work out if the guy has a useful boss, but fail because it's Paul Solomon :)
OR
there's such a massive Org Structure that's not populated with stuff, that you should probably question what the point of the structure is!!
do 20 times. "Don't want to infinite loop...
refresh t_leadpos.
CALL FUNCTION 'RH_GET_LEADING_POSITION'
EXPORTING
plvar = w_plvar
otype = l_otype "can be US (user), P (Person), S (position) or O (Org unit)
sobid = l_sobid
date = sy-datum
auth = ' '
TABLES
leading_pos = t_leadpos
EXCEPTIONS
no_lead_pos_found = 1
OTHERS = 2.
check t_leadpos[] is not initial.
read table t_leadpos into w_leadpos index 1.
REFRESH t_holders.
CALL FUNCTION 'HRWPC_RFC_PP_HOLDERS'
EXPORTING
plvar = w_plvar "usually 01
otype = 'S'
objid = w_leadpos-objid "from above table read
TABLES
holder = t_holders
EXCEPTIONS
no_active_plvar = 1
infty_not_found = 2
nothing_found = 3
OTHERS = 4.
IF sy-subrc = 0 AND t_holders IS NOT INITIAL.
READ TABLE t_holders INTO w_holders INDEX 1.
read table t_pa0105 into w_pa0105 with key pernr = w_holders-pernr
subty = '0001'.
if sy-subrc = 0.
p_output-manager = w_pa0105-usrid.
exit.
endif.
else. "Didn't find holders for the position... Go and find the Super-Position.
l_otype = w_leadpos-otype. "Probably 'S' but hey, let's make it dynamic.
l_sobid = w_leadpos-objid.
endif. "Found holders for the position.
enddo. "Iterate up the structure.
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.
*&---------------------------------------------------------------------*
*& 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.
Subscribe to:
Posts (Atom)