根据销售订单创建交货单(BAPI_DELIVERYPROCESSING_EXEC)
可以指定对应的仓库和销售订单创建交货单
创建交货单:BAPI_DELIVERYPROCESSING_EXEC
交货单过账:WS_DELIVERY_UPDATE
创建交货单:
function ZTEST1. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(IV_WERKS) TYPE WERKS_D OPTIONAL *" VALUE(IV_MATNR) TYPE MATNR OPTIONAL *" VALUE(IV_ERSDA) TYPE ERSDA OPTIONAL *" VALUE(IV_KWMENG) TYPE KWMENG OPTIONAL *" VALUE(IV_LGORT) TYPE LGORT_D OPTIONAL *" EXPORTING *" VALUE(EV_TYPE) TYPE BAPI_MTYPE *" VALUE(EV_MESSAGE) TYPE BAPI_MSG *" TABLES *" ET_DT STRUCTURE ZSS_DT OPTIONAL *"---------------------------------------------------------------------- *ET_DT 返回的对应的交货单和交货单行项目 *ZCHDH VBELN_VL CHAR 10 0 交货 *ZPOSNR POSNR_VL NUMC 6 0 交货项目 *ZWMENG LFIMG QUAN 13 3 实际已交货量(按销售单位) types: begin of ty_mska, werks type mska-werks, "工厂 matnr type mska-matnr, "物料编号 lgort type mska-lgort, "库存地点 ersda type mska-ersda, "创建日期 vdatu type vbak-vdatu, "交货日期 vbeln type mska-vbeln, "销售订单号 posnr type mska-posnr, "行号 kalab type mska-kalab, "库存数 kwmeng type vbap-kwmeng, "交货数量 meins type vbap-meins, "单位 end of ty_mska. data: gt_mska type table of ty_mska, gt_mska_sum type table of ty_mska, gt_delivery type table of ty_mska, gt_lips type table of lips with header line, gs_mska type ty_mska. data:kalab_0202 type labst, "0202仓销售订单实际可用总库存 kalab_0303 type labst, kalab_0203 type labst, kalab_sum type labst, gv_kwmeng type kwmeng. "可用库存数量 * bapi data: gt_request type standard table of bapideliciousrequest with header line, gt_created type standard table of bapideliciouscreateditems with header line, gt_return type standard table of bapiret2 with header line. call function 'CONVERSION_EXIT_MATN1_INPUT' exporting input = iv_matnr importing output = iv_matnr exceptions length_error = 1 others = 2. select m~werks m~matnr m~lgort m~ersda k~vdatu m~vbeln m~posnr m~kalab p~kwmeng p~meins into corresponding fields of table gt_mska from mska as m inner join vbap as p on m~vbeln = p~vbeln and m~posnr = p~posnr left join vbak as k on m~vbeln = k~vbeln where m~kalab gt 0 and m~werks = iv_werks and m~matnr = iv_matnr and m~lgort = iv_lgort. if gt_mska is initial. concatenate iv_matnr iv_werks '中无销售订单库存!' into ev_message separated by space. ev_type = 'E'. return. endif. *获取计划交货数量 select * from lips into table gt_lips for all entries in gt_mska where vbelv = gt_mska-vbeln and posnv = gt_mska-posnr and gbsta = 'A'. *实际可用库存数量 = 库存数量 - 计划交货数量 loop at gt_lips . loop at gt_mska assigning field-symbol(<mska>) where vbeln = gt_lips-vbelv and posnr = gt_lips-posnv and lgort = gt_lips-lgort. <mska>-kalab = <mska>-kalab - gt_lips-lfimg. endloop. endloop. loop at gt_mska into gs_mska. add gs_mska-kalab to kalab_sum. endloop. if kalab_sum < iv_kwmeng. concatenate iv_werks iv_matnr iv_lgort '销售订单库存不足!' into ev_message separated by space. ev_type = 'E'. return. endif. sort gt_mska by ersda. "按时间先进先出 loop at gt_mska into gs_mska. gv_kwmeng = iv_kwmeng - gs_mska-kalab. if gv_kwmeng > 0. append gs_mska to gt_delivery. iv_kwmeng = gv_kwmeng. else. gs_mska-kalab = iv_kwmeng. append gs_mska to gt_delivery. exit. endif. endloop. sort gt_delivery by vbeln posnr. "同一销售订单(多行)生成一个交货单 不同销售订单一单一交货单 loop at gt_delivery into gs_mska. *gt_request-document_type_predecessor = 'A'. "Sales Ord *gt_request-document_type_delivery = 'LF'. gt_request-id = sy-tabix. gt_request-plant = iv_werks. gt_request-stge_loc = gs_mska-lgort. gt_request-document_numb = gs_mska-vbeln. gt_request-document_item = gs_mska-posnr. gt_request-quantity_sales_uom = gs_mska-kalab. gt_request-quantity_base__uom = gs_mska-kalab. gt_request-ordcombind = 'X'. gt_request-document_type = 'A'. "L交货 B采购订单 A销售订单 1外部系统中的销售订单 2外部系统中的采购订单 gt_request-delivery_date = sy-datum. append gt_request. endloop. call function 'BAPI_DELIVERYPROCESSING_EXEC' tables request = gt_request createditems = gt_created return = gt_return. read table gt_return with key type = 'E'. if sy-subrc <> 0. call function 'BAPI_TRANSACTION_COMMIT'. ev_type = 'S'. loop at gt_created. et_dt-zchdh = gt_created-document_numb. et_dt-zposnr = gt_created-document_item. et_dt-zwmeng = gt_created-quantity_sales_uom. append et_dt. endloop. else. call function 'BAPI_TRANSACTION_ROLLBACK'. ev_type = 'E'. loop at gt_return. concatenate ev_message gt_return-message into ev_message separated by ','. endloop. endif. endfunction.
交货单过账:
function ztest2. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(IV_VBELN) TYPE VBELN_VL OPTIONAL *" VALUE(IV_DATE) TYPE BUDAT OPTIONAL *" EXPORTING *" VALUE(EV_TYPE) TYPE BAPI_MTYPE *" VALUE(EV_MESSAGE) TYPE BAPI_MSG *"---------------------------------------------------------------------- data:ls_vbkok type vbkok, "交货单抬头 gt_vbpok like table of vbpok with header line, "交货单行项目 gt_prot like table of prott with header line, "输出数据 gv_error type xfeld. "错误标识 data:gt_lips type table of lips, gs_lips type lips. if iv_vbeln is initial or iv_date is initial. ev_type = 'E'. ev_message = '交货单或交货日期缺失!'. return. endif. ls_vbkok-vbeln_vl = iv_vbeln. "交货 ls_vbkok-wabuc = 'X'. "自动过帐货物移动 ls_vbkok-wadat_ist = iv_date. "实际货物移动日期 select * into table gt_lips from lips where vbeln = iv_vbeln. loop at gt_lips into gs_lips. gt_vbpok-vbeln_vl = gs_lips-vbeln. "交货 gt_vbpok-posnr_vl = gs_lips-posnr. "交货项目 gt_vbpok-vbeln = gs_lips-vgbel. "后续销售和分销凭证 gt_vbpok-posnn = gs_lips-vgpos. "SD凭证的后续项目 gt_vbpok-matnr = gs_lips-matnr. "物料编号 gt_vbpok-werks = gs_lips-werks. "工厂 gt_vbpok-lfimg = gs_lips-lfimg. gt_vbpok-lgmng = gs_lips-lgmng. "以仓库保管单位级的实际交货数量 gt_vbpok-pikmg = gs_lips-lfimg. "数量以基本计量单位计算 append gt_vbpok. endloop. call function 'WS_DELIVERY_UPDATE' exporting vbkok_wa = ls_vbkok synchron = 'X' commit = 'X' delivery = iv_vbeln update_picking = 'X' if_wmpp = 'X' importing ef_error_in_goods_issue_0 = gv_error tables vbpok_tab = gt_vbpok prot = gt_prot exceptions error_message = 1 others = 4. if sy-subrc <> 0 and gt_prot[] is initial. "交货单异常,过账失败 ev_type = 'E'. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 into ev_message. return. endif. read table gt_prot transporting no fields with key msgty = 'E' . if sy-subrc = 0. call function 'BAPI_TRANSACTION_ROLLBACK'. loop at gt_prot into data(gs_prot) where msgty ca 'AEX'. call function 'MESSAGE_TEXT_BUILD' exporting msgid = gs_prot-msgid msgnr = gs_prot-msgno msgv1 = gs_prot-msgv1 msgv2 = gs_prot-msgv1 msgv3 = gs_prot-msgv1 msgv4 = gs_prot-msgv1 importing message_text_output = ev_message. endloop. ev_type = 'E'. else. call function 'BAPI_TRANSACTION_COMMIT' exporting wait = 'X'. ev_type = 'S'. ev_message = '交货单过账成功'. wait up to 1 seconds. select single mblnr into ev_message from mseg where mjahr = iv_date+0(4) and matnr = gs_lips-matnr and werks = gs_lips-werks and kdauf = gs_lips-vgbel and xblnr_mkpf = gs_lips-vbeln. "获取物料凭证 endif. endfunction.
abap菜鸟,记录学习笔记。才疏学浅,如有错误,请直接在评论区留言。