FORM frm_execute . CLEAR:gw_return, gt_return,gw_gb008,gw_gb008_i,gt_gb008,gt_gb008_i. PERFORM frm_check_input. PERFORM frm_get_basic_data. " LOOP AT gt_output INTO gw_output. CLEAR g_err_flag. IF p_fl EQ 'X'. PERFORM frm_create_sto_po. "创建STO采购订单 IF g_err_flag NE 'X'. PERFORM frm_create_delivery. "创建交货单 ENDIF. IF g_err_flag NE 'X'. PERFORM frm_vl02n_post. "交货单发货过账 ENDIF. IF g_err_flag NE 'X'. PERFORM frm_goodsmvt_create. "MIGO采购入库 ENDIF. IF g_err_flag NE 'X'. PERFORM frm_prodorder_ltl . "生产订单领料 ENDIF. ELSEIF p_tl EQ 'X'. PERFORM frm_prodorder_ltl . "生产订单退料 IF g_err_flag NE 'X'. PERFORM frm_create_sto_po. "创建STO采购订单 ENDIF. IF g_err_flag NE 'X'. PERFORM frm_create_delivery. "创建交货单 ENDIF. IF g_err_flag NE 'X'. PERFORM frm_vl02n_post. "交货单发货过账 ENDIF. IF g_err_flag NE 'X'. PERFORM frm_goodsmvt_create. "MIGO采购入库 ENDIF. ENDIF. "解锁 PERFORM frm_unlock. PERFORM frm_save_jg. ENDFORM. " FRM_EXECUTE
FORM frm_create_sto_po . DATA:lt_return LIKE TABLE OF bapiret2, lw_return TYPE bapiret2. DATA:l_ebelp TYPE ekpo-ebelp. CLEAR:g_msg,g_ebeln,g_vbeln,g_mblnr1,g_mblnr2 . REFRESH:gt_poitem,gt_poitemx,gt_poschedule,gt_poschedulex, gt_poaccount,gt_poaccountx,gt_potextitem , gt_poheader,gt_poheaderx. CLEAR:gw_poitem,gw_poitemx,gw_poschedule,gw_poschedulex, gw_poaccount,gw_poaccountx,gw_potextitem , gw_poheader,gw_poheaderx. "po header gw_poheader-doc_type = 'DL05'. gw_poheader-vendor = '0020200200'. gw_poheader-purch_org = 'DL01'. gw_poheader-pur_group = 'A04'."'A04'. gw_poheader-comp_code = '2001'. gw_poheader-doc_date = sy-datum. gw_poheader-langu = sy-langu. gw_poheader-currency = 'CNY'."货币 gw_poheaderx-doc_type = 'X'. gw_poheaderx-vendor = 'X'. gw_poheaderx-purch_org = 'X'. gw_poheaderx-pur_group = 'X'. gw_poheaderx-comp_code = 'X'. gw_poheaderx-doc_date = 'X'. gw_poheaderx-langu = 'X'. gw_poheaderx-currency = 'X'."货币 l_ebelp = 10. LOOP AT gt_output INTO gw_output WHERE matnr IS NOT INITIAL. CLEAR gw_mara. READ TABLE gt_mara INTO gw_mara WITH KEY matnr = gw_output-matnr BINARY SEARCH. CLEAR:gw_a017,gw_konp. READ TABLE gt_a017 INTO gw_a017 WITH KEY matnr = gw_output-matnr BINARY SEARCH. IF sy-subrc = 0. READ TABLE gt_konp INTO gw_konp WITH KEY knumh = gw_a017-knumh BINARY SEARCH. ENDIF. "po ITEM gw_poitem-po_item = l_ebelp. "采购凭证的项目编号 gw_poitem-material = gw_output-matnr. gw_poitem-quantity = gw_output-psmng."采购订单数量 gw_poitem-po_unit = gw_output-meins ."采购订单的计量单位 gw_poitem-net_price = gw_konp-kbetr."净价 gw_poitem-price_unit = '1'. "gw_konp-KPEIN. gw_poitem-tax_code = 'J2'. gw_poitem-matl_group = gw_mara-matkl."物料组 gw_poitem-plant = '2001'. "工厂 gw_poitem-stge_loc = gw_output-lgort. "库存地点 " gw_poitem-info_rec = '5300000076'. IF p_tl EQ 'X'. gw_poitem-ret_item = 'X'."退货项目 ENDIF. APPEND gw_poitem TO gt_poitem. CLEAR gw_poitem. gw_poitemx-po_item = l_ebelp. "采购凭证的项目编号 gw_poitemx-material = 'X'. gw_poitemx-quantity = 'X'."采购订单数量 gw_poitemx-po_unit = 'X' ."采购订单的计量单位 gw_poitemx-net_price = 'X'."净价 gw_poitemx-price_unit = 'X'. gw_poitemx-tax_code = 'X'. gw_poitemx-matl_group = 'X'."物料组 gw_poitemx-plant = 'X'."工厂 gw_poitemx-stge_loc = 'X'. gw_poitemx-info_rec = 'X'. IF p_tl EQ 'X'. gw_poitemx-ret_item = 'X'."退货项目 ENDIF. APPEND gw_poitemx TO gt_poitemx. CLEAR gw_poitemx. "交货数据 gw_poschedule-po_item = l_ebelp. "采购凭证的项目编号 gw_poschedule-sched_line = '1'. "采购凭证的项目编号 gw_poschedule-del_datcat_ext = 'D'."交货日期的类别 gw_poschedule-delivery_date = sy-datum."交货日期 gw_poschedule-quantity = gw_output-psmng."采购订单数量 APPEND gw_poschedule TO gt_poschedule. CLEAR gw_poschedule. gw_poschedulex-po_item = l_ebelp. "采购凭证的项目编号 gw_poschedulex-sched_line = '1'. "采购凭证的项目编号 gw_poschedulex-del_datcat_ext = 'X'."交货日期的类别 gw_poschedulex-delivery_date = 'X'."交货日期 gw_poschedulex-quantity = 'X'."采购订单数量 APPEND gw_poschedulex TO gt_poschedulex. CLEAR gw_poschedulex. "条件数据 gw_pocond-itm_number = l_ebelp. "(item number) gw_pocond-cond_type = 'ZB21'. "(condition type) gw_pocond-cond_value = '13'. "(condition value) gw_pocond-currency = '%'. gw_pocond-change_id = 'U'. gw_pocond-condclass = 'A'. gw_pocond-calctypcon = 'A'. APPEND gw_pocond TO gt_pocond. CLEAR gw_pocond. gw_pocondx-itm_number = l_ebelp. gw_pocondx-itm_numberx = 'X'. gw_pocondx-cond_type = 'X'. gw_pocondx-cond_value = 'X'. gw_pocondx-currency = 'X'. gw_pocondx-condclass = 'X'. gw_pocondx-change_id = 'X'. gw_pocondx-calctypcon = 'X'. APPEND gw_pocondx TO gt_pocondx. CLEAR gw_pocondx.* gw_pocond-itm_number = l_ebelp. "(item number)* gw_pocond-cond_type = 'PB00'. "(condition type)* gw_pocond-cond_value = gw_konp-kbetr. "(condition value)* gw_pocond-currency = gw_konp-konwa.* gw_pocond-cond_p_unt = 1.* gw_pocond-condclass = 'B'.* gw_pocond-calctypcon = 'C'.* gw_pocond-change_id = 'I'.* APPEND gw_pocond TO gt_pocond.** gw_pocondx-itm_number = l_ebelp.* gw_pocondx-itm_numberx = 'X'.* gw_pocondx-cond_type = 'X'.* gw_pocondx-cond_value = 'X'.* gw_pocondx-currency = 'X'.* gw_pocondx-cond_p_unt = 'X'.* gw_pocondx-condclass = 'X'.* gw_pocondx-calctypcon = 'X'.* gw_pocondx-change_id = 'X'.* APPEND gw_pocondx TO gt_pocondx. l_ebelp = l_ebelp + 10. ENDLOOP. sy-tcode = 'ME21N'. CALL FUNCTION 'BAPI_PO_CREATE1' EXPORTING poheader = gw_poheader poheaderx = gw_poheaderx IMPORTING exppurchaseorder = g_ebeln TABLES return = lt_return poitem = gt_poitem poitemx = gt_poitemx " poschedule = gt_poschedule " poschedulex = gt_poschedulex " POCONDHEADER = " POCONDHEADERX = pocond = gt_pocond pocondx = gt_pocondx. LOOP AT lt_return INTO lw_return WHERE type = 'E' OR type = 'A'. EXIT. ENDLOOP. IF sy-subrc = 0. g_err_flag = 'X'. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. gw_return-type = 'E'. gw_return-message = '采购定单创建失败,原因如下:'. APPEND gw_return TO gt_return. CLEAR gw_return. DELETE lt_return WHERE type NE 'E'. APPEND LINES OF lt_return TO gt_return. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. CONCATENATE '采购订单号:' g_ebeln INTO g_msg. gw_return-type = 'S'. gw_return-message = g_msg. APPEND gw_return TO gt_return. CLEAR gw_return. ENDIF. WAIT UP TO 1 SECONDS. ENDFORM. " FRM_CREATE_STO_PO
FORM frm_create_delivery . DATA:l_vstel TYPE vstel, l_ledat TYPE ledat, lt_sto_items TYPE shp_sto_itm_t, lw_sto_item TYPE shp_sto_itm, lt_dlv_items TYPE shp_dlv_itm_t, lw_dlv_item TYPE shp_dlv_itm, lt_message TYPE vbfs_t, lw_message TYPE vbfs. DATA:l_ebelp TYPE ekpo-ebelp. SELECT SINGLE vstel INTO l_vstel" = 'Z030'. FROM ekpv WHERE ebeln = g_ebeln. l_ledat = sy-datum. l_ebelp = 10. LOOP AT gt_output INTO gw_output WHERE matnr IS NOT INITIAL. lw_sto_item-rfbel = g_ebeln. lw_sto_item-rfpos = l_ebelp. lw_sto_item-lfimg = gw_output-psmng. lw_sto_item-vrkme = gw_output-meins. APPEND lw_sto_item TO lt_sto_items. l_ebelp = l_ebelp + 10. ENDLOOP. CALL FUNCTION 'SHP_DELIVERY_CREATE_FROM_STO' EXPORTING if_vstel = l_vstel if_ledat = l_ledat it_sto_items = lt_sto_items IMPORTING et_dlv_items = lt_dlv_items et_messages = lt_message. CLEAR g_msg. READ TABLE lt_dlv_items INTO lw_dlv_item INDEX 1. IF lw_dlv_item-vbeln IS NOT INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. g_vbeln = lw_dlv_item-vbeln. CONCATENATE '交货单号:' g_vbeln INTO g_msg. gw_return-type = 'S'. gw_return-message = g_msg. APPEND gw_return TO gt_return. CLEAR gw_return. ELSE. g_err_flag = 'X'. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. gw_return-type = 'E'. gw_return-message = '交货单创建失败,原因如下:'. APPEND gw_return TO gt_return. CLEAR gw_return. LOOP AT lt_message INTO lw_message WHERE msgty = 'E'. gw_return-type = lw_message-msgty. gw_return-id = lw_message-msgid. gw_return-number = lw_message-msgno. gw_return-message_v1 = lw_message-msgv1. gw_return-message_v2 = lw_message-msgv2. gw_return-message_v3 = lw_message-msgv3. gw_return-message_v4 = lw_message-msgv4. MESSAGE ID gw_return-id TYPE gw_return-type NUMBER gw_return-number INTO gw_return-message WITH gw_return-message_v1 gw_return-message_v2 gw_return-message_v3 gw_return-message_v4. APPEND gw_return TO gt_return. CLEAR gw_return. ENDLOOP. ENDIF. WAIT UP TO 1 SECONDS. ENDFORM. " FRM_CREATE_DELIVERY
FORM frm_vl02n_post . DATA: lv_wadat_ist LIKE likp-wadat_ist. DATA: lw_vbkok LIKE vbkok. DATA: lv_error TYPE xfeld. DATA: lw_vbpok LIKE vbpok, lw_prot LIKE prott. DATA: lt_vbpok LIKE STANDARD TABLE OF vbpok, lt_prot LIKE STANDARD TABLE OF prott. DATA: lv_msg TYPE string. DATA: lw_werks LIKE lips-werks. DATA:lv_check TYPE c. DATA:lt_lips TYPE TABLE OF lips, lw_lips TYPE lips. CLEAR: lw_vbkok. "交货单上面的实际发货日期 WADAT_IST CLEAR: lv_wadat_ist. SELECT SINGLE wadat_ist INTO lv_wadat_ist FROM likp WHERE likp~vbeln = g_vbeln. SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_lips FROM lips WHERE vbeln = g_vbeln. IF lv_wadat_ist IS NOT INITIAL. lw_vbkok-wadat_ist = lv_wadat_ist. "实际货物移动日期 lw_vbkok-wadat = lv_wadat_ist. "发货日期 即过账日期 ENDIF. "客户输入了过账日期,则最优先 IF p_gzrq IS NOT INITIAL. lw_vbkok-wadat_ist = p_gzrq. "实际货物移动日期 lw_vbkok-wadat = p_gzrq. "发货日期 即过账日期 ENDIF. lw_vbkok-vbeln_vl = g_vbeln. lw_vbkok-wabuc = 'X'.* IF P_TL EQ 'X'.* LOOP AT lt_lips INTO lw_lips WHERE matnr IS NOT INITIAL.* lw_vbpok-vbeln_vl = g_vbeln.* lw_vbpok-posnr_vl = lw_lips-posnr.** lw_vbpok-vbeln = g_vbeln.* lw_vbpok-posnn = lw_lips-posnr.* lw_vbpok-matnr = lw_lips-matnr.* LW_VBPOK-WERKS = LW_LIPS-WERKS.* LW_VBPOK-PIKMG = LW_LIPS-LFIMG.* LW_VBPOK-kzlgo = 'X'.* lw_vbpok-lgort = 'YK02'.* APPEND lw_vbpok TO lt_vbpok.* ENDLOOP.* ENDIF. SET UPDATE TASK LOCAL . CALL FUNCTION 'WS_DELIVERY_UPDATE' EXPORTING vbkok_wa = lw_vbkok synchron = ' ' no_messages_update = ' ' commit = 'X' delivery = g_vbeln update_picking = 'X' IMPORTING ef_error_in_goods_issue_0 = lv_error TABLES vbpok_tab = lt_vbpok prot = lt_prot. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. CLEAR lv_check. CLEAR: lw_prot. LOOP AT lt_prot INTO lw_prot WHERE msgty = 'E' OR msgty = 'A'. lv_check = 'X'. EXIT. ENDLOOP.*--函数失败也会将过账日期写到货物移动日期 DATA:lw_likp TYPE likp. CLEAR lw_likp. IF lv_check = 'X' AND lv_wadat_ist IS INITIAL ."过账出错并且原来的货物移动日期为空,则清空货物移动日期 SELECT SINGLE * INTO lw_likp FROM likp WHERE likp~vbeln = g_vbeln. CLEAR lw_likp-wadat_ist. MODIFY likp FROM lw_likp . IF sy-subrc = 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. ENDIF. ENDIF. CLEAR g_msg. IF lv_check = 'X'. g_err_flag = 'X'. gw_return-type = 'E'. gw_return-message = '交货单发货过账失败,原因如下:'. APPEND gw_return TO gt_return. CLEAR gw_return. LOOP AT lt_prot INTO lw_prot WHERE msgty = 'E' OR msgty = 'A'. gw_return-type = lw_prot-msgty. gw_return-id = lw_prot-msgid. gw_return-number = lw_prot-msgno. gw_return-message_v1 = lw_prot-msgv1. gw_return-message_v2 = lw_prot-msgv2. gw_return-message_v3 = lw_prot-msgv3. gw_return-message_v4 = lw_prot-msgv4. MESSAGE ID gw_return-id TYPE gw_return-type NUMBER gw_return-number INTO gw_return-message WITH gw_return-message_v1 gw_return-message_v2 gw_return-message_v3 gw_return-message_v4. APPEND gw_return TO gt_return. CLEAR gw_return. ENDLOOP. ELSE. gw_return-type = 'S'. gw_return-message = '交货单发货过账成功'. APPEND gw_return TO gt_return. CLEAR gw_return. ENDIF. ENDFORM. " FRM_VL02N_POST
FORM frm_goodsmvt_create . DATA:lw_header TYPE bapi2017_gm_head_01, lw_mblnr TYPE bapi2017_gm_head_ret, lt_item TYPE TABLE OF bapi2017_gm_item_create, lw_item TYPE bapi2017_gm_item_create, lt_return TYPE TABLE OF bapiret2, lw_return TYPE bapiret2. DATA:l_code TYPE bapi2017_gm_code. DATA:l_ebelp TYPE ekpo-ebelp. l_code = '01'. lw_header-pstng_date = p_gzrq. lw_header-doc_date = sy-datum. l_ebelp = 10. LOOP AT gt_output INTO gw_output WHERE matnr IS NOT INITIAL. lw_item-material = gw_output-matnr. lw_item-plant = '2001'. lw_item-entry_qnt = gw_output-psmng. lw_item-entry_uom = gw_output-meins. "lw_item-plant = '2001'. IF p_fl = 'X'. lw_item-move_type = '101'. ELSE. lw_item-move_type = '161'. ENDIF. IF lw_item-move_type = '161'. lw_item-move_type = '101'. " lw_item-xstob = 'X'. ENDIF. CLEAR gw_marc. READ TABLE gt_marc INTO gw_marc WITH KEY werks = '2001' matnr = gw_output-matnr BINARY SEARCH. IF gw_marc-lgpro IS NOT INITIAL. lw_item-stge_loc = gw_marc-lgpro. ELSE. lw_item-stge_loc = gw_output-lgort. ENDIF. lw_item-po_number = g_ebeln. lw_item-po_item = l_ebelp. lw_item-no_more_gr = 'X'. lw_item-mvt_ind = 'B'. APPEND lw_item TO lt_item. l_ebelp = l_ebelp + 10. ENDLOOP. CALL FUNCTION 'BAPI_GOODSMVT_CREATE' EXPORTING goodsmvt_header = lw_header goodsmvt_code = l_code IMPORTING goodsmvt_headret = lw_mblnr TABLES goodsmvt_item = lt_item return = lt_return. CLEAR g_msg. IF lw_mblnr IS INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. g_err_flag = 'X'. gw_return-type = 'E'. IF p_fl = 'X'. gw_return-message = '采购订单入库失败,原因如下:'. ELSE. gw_return-message = '采购订单退料失败,原因如下:'. ENDIF. APPEND gw_return TO gt_return. CLEAR gw_return. DELETE lt_return WHERE type NE 'E'. APPEND LINES OF lt_return TO gt_return. CLEAR gw_return. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. g_mblnr1 = lw_mblnr-mat_doc. IF p_fl = 'X'. CONCATENATE '采购订单入库物料凭证:' g_mblnr1 INTO g_msg. ELSE. CONCATENATE '采购订单退料物料凭证:' g_mblnr1 INTO g_msg. ENDIF. gw_return-type = 'S'. gw_return-message = g_msg. APPEND gw_return TO gt_return. CLEAR gw_return. ENDIF. WAIT UP TO 1 SECONDS. ENDFORM. " FRM_GOODSMVT_CREATE
FORM frm_prodorder_ltl . DATA:lw_header TYPE bapi2017_gm_head_01, lw_mblnr TYPE bapi2017_gm_head_ret, lt_item TYPE TABLE OF bapi2017_gm_item_create, lw_item TYPE bapi2017_gm_item_create, lt_return TYPE TABLE OF bapiret2, lw_return TYPE bapiret2. DATA:l_code TYPE bapi2017_gm_code. lw_header-pstng_date = p_gzrq. lw_header-doc_date = sy-datum. lw_header-pr_uname = sy-uname. CHECK gt_output IS NOT INITIAL. l_code = '03'. "03 - MB1A - Goods Issue LOOP AT gt_output INTO gw_output WHERE matnr IS NOT INITIAL. lw_item-material = gw_output-matnr. lw_item-plant = '2001'. CLEAR gw_marc. READ TABLE gt_marc INTO gw_marc WITH KEY werks = '2001' matnr = gw_output-matnr BINARY SEARCH. IF gw_marc-lgpro IS NOT INITIAL. lw_item-stge_loc = gw_marc-lgpro. ELSE. lw_item-stge_loc = gw_output-lgort. ENDIF. IF p_fl = 'X'. lw_item-move_type = '261'. ELSE. lw_item-move_type = '262'. ENDIF. IF lw_item-move_type = '262'. lw_item-move_type = '261'. lw_item-xstob = 'X'. ENDIF. lw_item-entry_uom = gw_output-meins. "单位 KAR&BOT lw_item-orderid = gw_output-aufnr. "生产订单号 lw_item-unload_pt = gw_output-zbanc. "班次 lw_item-mvt_ind = ''. lw_item-entry_qnt = gw_output-psmng. lw_item-item_text = gw_output-zbz. CLEAR gw_resb. READ TABLE gt_resb INTO gw_resb WITH KEY aufnr = gw_output-aufnr matnr = gw_output-matnr BINARY SEARCH. lw_item-reserv_no = gw_resb-rsnum. lw_item-res_item = gw_resb-rspos. APPEND lw_item TO lt_item. CLEAR:lw_item. ENDLOOP. CALL FUNCTION 'BAPI_GOODSMVT_CREATE' EXPORTING goodsmvt_header = lw_header goodsmvt_code = l_code IMPORTING goodsmvt_headret = lw_mblnr TABLES goodsmvt_item = lt_item return = lt_return.*如果过帐失败 IF lw_mblnr IS INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. g_err_flag = 'X'. gw_return-type = 'E'. IF p_fl EQ 'X'. gw_return-message = '生产订单发料失败,原因如下:'. ELSE. gw_return-message = '生产订单退料失败,原因如下:'. ENDIF. APPEND gw_return TO gt_return. CLEAR gw_return. DELETE lt_return WHERE type NE 'E'. APPEND LINES OF lt_return TO gt_return. CLEAR gw_return. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. g_mblnr2 = lw_mblnr-mat_doc. IF p_fl EQ 'X'. CONCATENATE '生产订单发料物料凭证:' g_mblnr2 INTO g_msg. ELSE. CONCATENATE '生产订单退料物料凭证:' g_mblnr2 INTO g_msg. ENDIF. gw_return-type = 'S'. gw_return-message = g_msg. APPEND gw_return TO gt_return. CLEAR gw_return. ENDIF. IF p_tl EQ 'X'. WAIT UP TO 1 SECONDS. ENDIF. ENDFORM. " FRM_PRODORDER_SFL