FUNCTION zsd_dn_create1. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(SODATE) TYPE ZDATE *" EXPORTING *" VALUE(LV_DELIVERY) TYPE LIPS-VBELN *" TABLES *" CREATE_DN STRUCTURE ZMMT_DN_CREAT1 *" GT_RETURN STRUCTURE BAPIRET2 OPTIONAL *"---------------------------------------------------------------------- DATA: p_date LIKE sy-datum. DATA: lt_vbap TYPE TABLE OF zmmt_dn_creat1. DATA: ls_vbap TYPE zmmt_dn_creat1. DATA:lt_vbap_temp TYPE TABLE OF zmmt_dn_creat1. DATA:ls_vbap_temp TYPE zmmt_dn_creat1. DATA:lt_sales_order_items TYPE TABLE OF bapidlvreftosalesorder. DATA:ls_sales_order_items TYPE bapidlvreftosalesorder. DATA: ls_header_data TYPE bapiobdlvhdrcon. DATA: ls_header_control TYPE bapiobdlvhdrctrlcon. DATA: lt_header_deadlines TYPE TABLE OF bapidlvdeadln . DATA: ls_header_deadlines TYPE bapidlvdeadln . DATA: BEGIN OF gt_lgort OCCURS 0, vbeln TYPE vbeln, posnr TYPE vbelp, lgort TYPE lgort_d, charg TYPE charg_d, spe_alternate TYPE lips-spe_alternate, budat TYPE budat, END OF gt_lgort. DATA: l_aufnr TYPE aufnr. DATA: l_lgort TYPE lgort_d. DATA: l_charg TYPE charg_d. DATA: clabs TYPE mchb-clabs. DATA: gs_return TYPE bapiret2. DATA: gv_message TYPE char200. DATA: text1(6) TYPE c. DATA: text2(2) TYPE c. DATA: zvrkme TYPE vbap-vrkme. * CLEAR:text1,text2. p_date = sodate. lt_vbap = create_dn[]. lt_vbap_temp = lt_vbap. SORT lt_vbap_temp BY vbeln. DELETE ADJACENT DUPLICATES FROM lt_vbap_temp COMPARING vbeln. CALL FUNCTION 'FRE_CONVERT_DATE_TO_TIMESTMP' EXPORTING ip_start_date = p_date ip_time_zone = 'UTC' IMPORTING ep_start_date = ls_header_deadlines-timestamp_utc. ls_header_deadlines-timetype = 'WSHDRWADTI'. LOOP AT lt_vbap_temp INTO ls_vbap_temp. CLEAR: lt_sales_order_items,ls_sales_order_items,gt_lgort[],gt_lgort,zvrkme. LOOP AT lt_vbap INTO ls_vbap WHERE vbeln = ls_vbap_temp-vbeln. CLEAR ls_sales_order_items. ls_sales_order_items-ref_doc = ls_vbap-vbeln. ls_sales_order_items-ref_item = ls_vbap-posnr. ls_sales_order_items-dlv_qty = ls_vbap-kwmeng. SELECT SINGLE vrkme INTO zvrkme FROM vbap WHERE vbap~vbeln = ls_vbap-vbeln and vbap~POSNR = ls_vbap-POSNR. ls_sales_order_items-sales_unit = zvrkme. APPEND ls_sales_order_items TO lt_sales_order_items. gt_lgort-vbeln = ls_vbap-vbeln. gt_lgort-posnr = ls_vbap-posnr. gt_lgort-lgort = ls_vbap-lgort. gt_lgort-budat = p_date. CLEAR: l_aufnr,l_lgort,l_charg,zvrkme. gt_lgort-charg = ls_vbap-charg. APPEND gt_lgort. ENDLOOP. EXPORT itab = gt_lgort TO MEMORY ID 'GT_LGORT'. CLEAR:lv_delivery,gt_return. ***创建交货单 CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_SLS' IMPORTING delivery = lv_delivery TABLES sales_order_items = lt_sales_order_items * SERIAL_NUMBERS = * EXTENSION_IN = * DELIVERIES = * CREATED_ITEMS = * EXTENSION_OUT = return = gt_return. IF lv_delivery IS NOT INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. CLEAR:ls_header_data, ls_header_control. ls_header_data-deliv_numb = lv_delivery . ls_header_control-deliv_numb = lv_delivery . ls_header_control-post_gi_flg = 'X'. ls_header_control-volume_flg = 'X'. CLEAR:lt_header_deadlines, ls_header_deadlines-deliv_numb. ls_header_deadlines-deliv_numb = lv_delivery. APPEND ls_header_deadlines TO lt_header_deadlines. ***交货单发货过账 CLEAR:gt_return. CALL FUNCTION 'BAPI_OUTB_DELIVERY_CONFIRM_DEC' EXPORTING header_data = ls_header_data header_control = ls_header_control delivery = lv_delivery TABLES header_deadlines = lt_header_deadlines return = gt_return. LOOP AT gt_return INTO gs_return WHERE type = 'A' OR type = 'E'. ENDLOOP. IF gs_return IS NOT INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. *************************************************删除没有交货成功的交货单 DATA: wa_hdata LIKE bapiobdlvhdrchg, wa_hcont LIKE bapiobdlvhdrctrlchg, d_delivy LIKE bapiobdlvhdrchg-deliv_numb, it_bapiret2 LIKE bapiret2 OCCURS 0 WITH HEADER LINE. wa_hdata-deliv_numb = lv_delivery. wa_hcont-deliv_numb = lv_delivery. wa_hcont-DLV_DEL = 'X'. d_delivy = lv_delivery. CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE' EXPORTING header_data = wa_hdata header_control = wa_hcont delivery = d_delivy TABLES return = it_bapiret2. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. *************************************************删除没有交货成功的交货单 ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. ENDIF. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. ENDIF. ENDLOOP. ENDFUNCTION.
^_^很多人都把资料锁进了note里,以后还怎么百度呢^_^