可以结合BAPI和ME54N查看效果
其实修改采购申请,可以直接调用BAPI修改,没必要撤批审批
而且调用审批的时候,经常发生一种现象:直接执行审批出错,debug进去发现没问题,都能成功,
具体原因不是很清楚,猜测是系统太快,一级审批没走完就进行二级审批,然后就报错了,
解决方案之一是每级审批后,等一秒……
"1、撤批
SELECT SINGLE frggr
"审批组
frgst
"批准策略
frgzu
"审批状态
INTO (l_frggr, l_frgst ,l_frgzu)
FROM eban
WHERE banfn = l_wa-banfn
"采购申请号
AND bnfpo = l_wa_-bnfpo.
"行项目号
"各级审批
SELECT SINGLE
frgc1
frgc2
frgc3
frgc4
frgc5
FROM t16fs
INTO (l_frgc1,l_frgc2,l_frgc3,l_frgc4,l_frgc5)
WHERE frggr = l_frggr
AND frgsx = l_frgst.
l_rel_code = l_frgc1.
"如果想指定到某一级的撤批,选择t16fs-frgc1
t16fs-frgc2
t16fs-frgc3
分别三级撤批
l_rel_code = l_frgc1."这里是扯批到创建者,
CALL FUNCTION 'BAPI_REQUISITION_RESET_RELEASE'
EXPORTING
number
= l_wa_preq_data-banfn
"采购申请号
item
= l_wa_preq_data-bnfpo
"行项目号
rel_code
= l_rel_code
EXCEPTIONS
authority_check_fail
= 1
requisition_not_found
= 2
enqueue_fail
= 3
prerequisite_fail
= 4
release_already_posted
= 5
responsibility_fail
= 6
no_release_already
= 7
no_new_release_indicator = 8
OTHERS
= 9.
IF sy-subrc NE 0.
异常处理
ENDIF.
"2、修改数量和交货日期
DATA: lt_items_old
LIKE TABLE OF bapiebanv
WITH HEADER LINE.
DATA: lt_items_new
LIKE TABLE OF bapiebanv
WITH HEADER LINE.
DATA: lt_items
LIKE TABLE OF bapieban
WITH HEADER LINE.
DATA: lt_account
LIKE TABLE OF bapiebkn
WITH HEADER LINE.
DATA: lt_bapireturn
LIKE TABLE OF bapireturn
WITH HEADER LINE.
DATA: number
TYPE bapieban-preq_no.
number = l_wa-banfn.
CALL FUNCTION 'BAPI_REQUISITION_GETDETAIL'
EXPORTING
number
= number
account_assignment = 'X'
TABLES
requisition_items
= lt_items.
READ TABLE lt_items WITH KEY preq_item = l_wa-bnfpo.
IF sy-subrc = 0.
MOVE-CORRESPONDING lt_items TO lt_items_old.
APPEND lt_items_old.
CLEAR lt_items_old.
MOVE-CORRESPONDING lt_items TO lt_items_new.
lt_items_new-quantity
= l_wa-menge_m.
lt_items_new-deliv_date
= l_wa-lfdat_m.
APPEND lt_items_new.
CLEAR lt_items_new.
ENDIF.
CALL FUNCTION 'BAPI_REQUISITION_CHANGE'
EXPORTING
number
= l_wa-banfn
TABLES
requisition_items_old = lt_items_old
requisition_items_new = lt_items_new
return
= lt_return.
*异常处理 lt_return……
"3、审批
"如果想指定到某一级的撤批,
rel_code选择t16fs-frgc1
t16fs-frgc2
t16fs-frgc3
"分别三级审批,必须一级级审批,不能跨级
"审批 必须分级审批
"获取审批级别
l_shenpi = strlen( l_frgzu ).
DO l_shenpi TIMES.
"一级级审批
CLEAR l_rel_code.
CASE sy-index.
WHEN 1.
l_rel_code = l_frgc1.
WHEN 2.
l_rel_code = l_frgc2.
WHEN 3.
l_rel_code = l_frgc3.
WHEN 4.
l_rel_code = l_frgc4.
WHEN 5.
l_rel_code = l_frgc5.
ENDCASE.
CALL FUNCTION 'BAPI_REQUISITION_RELEASE'
EXPORTING
number
= l_wa_057-banfn
"采购申请号
item
= l_wa_057-bnfpo
"行项目号
rel_code
= l_rel_code
TABLES
return
= lt_return
EXCEPTIONS
authority_check_fail
= 1
requisition_not_found
= 2
enqueue_fail
= 3
prerequisite_fail
= 4
release_already_posted = 5
responsibility_fail
= 6
OTHERS
= 7.
IF sy-subrc NE 0.
*
MESSAGE
ENDIF.
ENDDO.
CLEAR l_wa_preq_data.
"最开始使用的修改bapi是BAPI_PR_CHANGE,
发现执行后,没有报错,但是采购申请并没有修改
一直查原因,网上说跟服务采购申请有关,具体没深入研究
lt_pritem-preq_item
=
l_wa_preq_data-bnfpo.
"行项目号
lt_pritem-quantity
=
l_wa_preq_data-menge_m.
"数量
lt_pritem-deliv_date =
l_wa_preq_data-lfdat_m.
"交货日期
APPEND lt_pritem.
"影子字段,交货日期和数量打修改标示
lt_pritemx-deliv_date = 'X'.
lt_pritemx-quantity = 'X'.
APPEND lt_pritemx.
"修改采购申请BAPI
CALL FUNCTION 'BAPI_PR_CHANGE'
EXPORTING
number
= l_wa_preq_data-banfn
"采购申请号
TABLES
return
= lt_return
pritem
= lt_pritem
pritemx = lt_pritemx.
最后,提供一个比较完整的采购申请修改的例子:
data: lt_items_old
like table of bapiebanv
with header line.
data: lt_items_new
like table of bapiebanv
with header line.
data: lt_account_old
like table of bapiebknv
with header line.
data: lt_account_new
like table of bapiebknv
with header line.
data: lt_items
like table of bapieban
with header line.
data: lt_account
like table of bapiebkn
with header line.
data: lt_bapireturn
like table of bapireturn
with header line.
call function 'BAPI_REQUISITION_GETDETAIL'
exporting
number
= i_banfn
account_assignment
= 'X'
tables
requisition_items
= lt_items
requisition_account_assignment = lt_account.
loop at lt_items.
move-corresponding lt_items to lt_items_old.
append lt_items_old.
clear lt_items_old.
move-corresponding lt_items to lt_items_new.
loop at t_eban where bnfpo = lt_items-preq_item.
lt_items_new-short_text
= t_eban-txz01.
lt_items_new-store_loc
= t_eban-lgort. "库存地点
lt_items_new-quantity
= t_eban-menge.
lt_items_new-unit
= t_eban-meins.
lt_items_new-deliv_date
= t_eban-lfdat.
lt_items_new-mat_grp
= t_eban-matkl.
lt_items_new-store_loc
= t_eban-lgort.
lt_items_new-pur_group
= t_eban-ekgrp.
lt_items_new-c_amt_bapi
= t_eban-preis.
lt_items_new-trackingno
= t_eban-bednr.
lt_items_new-currency
= t_eban-waers.
lt_items_new-price_unit
= t_eban-peinh.
endloop.
append lt_items_new.
clear lt_items_new.
endloop.
loop at lt_account.
move-corresponding lt_account to lt_account_old.
append lt_account_old.
clear lt_account_old.
move-corresponding lt_account to lt_account_new.
loop at t_ebkn where bnfpo
= lt_account-preq_item.
lt_account_new-wbs_elem_e = t_ebkn-ps_psp_pnr.
lt_account_new-g_l_acct
= t_ebkn-sakto.
lt_account_new-cost_ctr
= t_ebkn-kostl.
lt_account_new-asset_no
= t_ebkn-anln1.
lt_account_new-order_no
= t_ebkn-aufnr.
lt_account_new-co_area
= t_ebkn-kokrs.
endloop.
append lt_account_new.
clear lt_account_new.
endloop.
loop at t_eban.
read table lt_items with key preq_item = t_eban-bnfpo.
if sy-subrc <> 0.
lt_items_new-preq_item
= t_eban-bnfpo. "采购申请的项目编号
lt_items_new-short_text = t_eban-txz01.
lt_items_new-store_loc
= t_eban-lgort. "库存地点
lt_items_new-quantity
= t_eban-menge.
lt_items_new-unit
= t_eban-meins.
lt_items_new-deliv_date = t_eban-lfdat.
lt_items_new-mat_grp
= t_eban-matkl.
lt_items_new-store_loc
= t_eban-lgort.
lt_items_new-pur_group
= t_eban-ekgrp.
lt_items_new-c_amt_bapi = t_eban-preis.
lt_items_new-trackingno = t_eban-bednr.
lt_items_new-currency
= t_eban-waers.
lt_items_new-price_unit = t_eban-peinh.
lt_items_new-des_vendor = t_eban-lifnr. "期望的供应商
lt_items_new-fixed_vend = t_eban-flief. "固定的供应商
lt_items_new-info_rec
= t_eban-infnr. "采购信息记录的编号
lt_items_new-purch_org
= t_eban-ekorg. "采购组织
append lt_items_new.
clear lt_items_new.
endif.
endloop.
loop at t_ebkn.
read table lt_account with key preq_item = t_ebkn-bnfpo.
if sy-subrc <> 0.
lt_account_new-preq_item
= t_ebkn-bnfpo.
lt_account_new-wbs_elem_e = t_ebkn-ps_psp_pnr.
lt_account_new-g_l_acct
= t_ebkn-sakto.
lt_account_new-cost_ctr
= t_ebkn-kostl.
lt_account_new-asset_no
= t_ebkn-anln1.
lt_account_new-order_no
= t_ebkn-aufnr.
lt_account_new-co_area
= t_ebkn-kokrs.
append lt_account_new.
clear lt_account_new.
endif.
endloop.
call function 'BAPI_REQUISITION_CHANGE'
exporting
number
= i_banfn
tables
requisition_items_old
= lt_items_old
requisition_items_new
= lt_items_new
requisition_account_old = lt_account_old
requisition_account_new = lt_account_new
return
= lt_bapireturn.