SD_BAPI
创建销售订单BAPI及增强
http://blog.sina.com.cn/s/blog_1647b3eff0102wi32.html
DATA:lv_objtype TYPE nast-objtype.
"SO BAPI参数 DATA:ls_order_header_in LIKE bapisdhd1. DATA:lt_order_header_in LIKE TABLE OF bapisdhd1. DATA:ls_order_header_inx LIKE bapisdhd1x. DATA:lt_order_header_inx LIKE TABLE OF bapisdhd1x. DATA:lt_order_items_in TYPE TABLE OF bapisditm. DATA:lt_order_items_inx LIKE TABLE OF bapisditmx. DATA:ls_order_items_in LIKE bapisditm. DATA:ls_order_items_inx LIKE bapisditmx. DATA:lt_order_partners LIKE TABLE OF bapiparnr. DATA:ls_order_partners LIKE LINE OF lt_order_partners. DATA:lt_schedules_in LIKE TABLE OF bapischdl . DATA:lt_schedules_inx LIKE TABLE OF bapischdlx . DATA:ls_schedules_in LIKE bapischdl . DATA:ls_schedules_inx LIKE bapischdlx . DATA:lt_order_conditions_in LIKE TABLE OF bapicond . DATA:lt_order_conditions_inx LIKE TABLE OF bapicondx . DATA:ls_order_conditions_in LIKE bapicond . DATA:ls_order_conditions_inx LIKE bapicondx . DATA: lt_conditions_in TYPE TABLE OF bapicond , "WITH HEADER LINE, ls_conditions_in TYPE bapicond . "W DATA: lt_extensionin TYPE TABLE OF bapiparex, ls_extensionin TYPE bapiparex. LOOP AT p_gt_checkabox_so_header ASSIGNING FIELD-SYMBOL(<fs1>). *-------SO创建 CLEAR:lv_ebeln. CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT' EXPORTING input = <fs1>-matnr IMPORTING output = <fs1>-matnr. ls_order_header_in = VALUE #( doc_type = 'ZRE3' "订单类型 sales_org = '5100' distr_chan = '10' division = '10' req_date_h = <fs1>-zthrq doc_date = sy-datum ). ls_order_header_inx = VALUE #( updateflag = 'I' doc_type = 'X' sales_org = 'X' distr_chan = 'X' division = 'X' req_date_h = 'X' doc_date = 'X' ). CLEAR :lt_order_items_in,lt_order_items_in[]. CLEAR :lt_order_items_inx,lt_order_items_inx[]. CLEAR :lt_order_partners,lt_order_partners[]. CLEAR :lt_schedules_in,lt_schedules_in[]. CLEAR :lt_schedules_inx,lt_schedules_inx[]. CLEAR :lt_return,lt_return[]. lt_order_partners = VALUE #( FOR ls_tpar IN lt_tpar_so ( VALUE #( BASE CORRESPONDING #( ls_tpar ) partn_numb = <fs1>-kunnr ) ) ). LOOP AT p_gt_checkabox_so_items WHERE kunnr = <fs1>-kunnr AND zdbdh = <fs1>-zthdh AND s_werks = <fs1>-s_werks. CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT' EXPORTING input = p_gt_checkabox_so_items-matnr IMPORTING output = p_gt_checkabox_so_items-matnr. ls_order_items_in-itm_number = p_gt_checkabox_so_items-zposnr ."items 号 ls_order_items_in-material = p_gt_checkabox_so_items-matnr. ls_order_items_in-plant = p_gt_checkabox_so_items-s_werks." ls_order_items_in-store_loc = '1001'. ls_order_items_in-target_qty = p_gt_checkabox_so_items-menge." * ls_order_items_in-ship_point = p_vstel."装运点 ls_order_items_in-target_qu = p_gt_checkabox_so_items-meins." * ls_order_items_in-target_val = 888."BAPIs 的货币金额(带有 9 个小数位) * ls_order_items_in-purch_no_c = '客户参考' . ls_order_items_in-item_categ = 'REN' ."销售凭证项目类别 * ls_order_items_in-item_categ = COND #( WHEN ls_order_header_in-doc_type = 'ZOR2' THEN 'TAS' ELSE 'TAN' ). APPEND ls_order_items_in TO lt_order_items_in. ls_schedules_inx-updateflag = 'X'. ls_schedules_inx-itm_number = 'X'. ls_schedules_inx-req_qty = 'X'. ls_order_items_inx-material = 'X'. ls_order_items_inx-target_qty = 'X'. ls_order_items_inx-sales_unit = 'X'. ls_order_items_inx-plant = 'X'. ls_order_items_inx-purch_no_c = 'X'. ls_order_items_inx-item_categ = 'X'."销售凭证项目类别 ls_order_items_inx-updateflag = 'I'. ls_order_items_inx-target_val = 'X'. APPEND ls_schedules_inx TO lt_schedules_inx. *SCHED_TYPE ls_schedules_in-itm_number = p_gt_checkabox_so_items-zposnr."items 号" ls_schedules_in-req_qty = p_gt_checkabox_so_items-menge . APPEND ls_schedules_in TO lt_schedules_in. ls_schedules_inx-updateflag = 'X'. ls_schedules_inx-itm_number = 'X'. ls_schedules_inx-req_qty = 'X'. APPEND ls_schedules_inx TO lt_schedules_inx. * IF ls_header-doc_type <> 'ZS12'. * l_date_sign = gs_alv-etdat. * l_sum = gs_alv-kwmeng. * IF gs_alv-zsfss = 'X'. * ls_conditions_in-itm_number = l_posnr. "ITM_NUMBER * ls_conditions_in-cond_type = 'MWSI'. "定价条件 * ls_conditions_in-cond_value = 13. ** ls_conditions_in-currency = gs_alv-waerk. ** ORDER_CONDITIONS_IN-COND_UNIT = LW_INFO-BSTME. "条件单位 ** ORDER_CONDITIONS_IN-COND_P_UNT = '1'. "条件定价单位 * APPEND ls_conditions_in TO lt_conditions_in. * CLEAR ls_conditions_inx. * ls_conditions_inx-itm_number = l_posnr. * ls_conditions_inx-cond_type = 'MWSI'. "定价条件 * ls_conditions_inx-cond_value = 'X'. ** ls_conditions_inx-currency = 'X'. ** ORDER_CONDITIONS_INX-OND_UNIT = 'X'. ** ORDER_CONDITIONS_INX-COND_P_UNT = 'X'. * APPEND ls_conditions_inx TO lt_conditions_inx. * ENDIF. * ls_conditions_in-itm_number = l_posnr. "ITM_NUMBER * ls_conditions_in-cond_type = 'ZP01'. "定价条件 * ls_conditions_in-cond_value = gs_alv-kbetr. * ls_conditions_in-currency = gs_alv-waerk. ** ORDER_CONDITIONS_IN-COND_UNIT = LW_INFO-BSTME. "条件单位 ** ORDER_CONDITIONS_IN-COND_P_UNT = '1'. "条件定价单位 * * APPEND ls_conditions_in TO lt_conditions_in. * * CLEAR ls_conditions_inx. * ls_conditions_inx-itm_number = l_posnr. * ls_conditions_inx-cond_type = 'ZP01'. "定价条件 * ls_conditions_inx-cond_value = 'X'. * ls_conditions_inx-currency = 'X'. ** ORDER_CONDITIONS_INX-OND_UNIT = 'X'. ** ORDER_CONDITIONS_INX-COND_P_UNT = 'X'. * APPEND ls_conditions_inx TO lt_conditions_inx. * ENDIF. * ENDIF. * "--> PROCESS SCHEDULE * * CLEAR: ls_extensionin,ls_bape_vbap. * ls_extensionin-structure = 'BAPE_VBAP'. ** ls_bape_vbak-zppdy = 'CD01'. "品牌单元 * ls_bape_vbap-posnr = l_posnr. * ls_bape_vbap-zjx = gs_alv-zjx. * ls_bape_vbap-zkhjhbh = gs_alv-zkhjhbh . * ls_bape_vbap-ztsyq = gs_alv-ztsyq . * * ls_extensionin+30(960) = ls_bape_vbap. * APPEND ls_extensionin TO lt_extensionin. * * * CLEAR: ls_extensionin,ls_bape_vbapx. * ls_extensionin-structure = 'BAPE_VBAPX'. ** ls_bape_vbakx-zppdy = 'X'. * " ls_bape_vbap-POSNR = l_posnr. * ls_bape_vbapx-zjx = 'X'. * ls_bape_vbap-zkhjhbh = 'X'. * ls_bape_vbap-ztsyq = 'X'. * ls_extensionin+30(960) = ls_bape_vbapx. * APPEND ls_extensionin TO lt_extensionin. *ENDLOOP. **---------------------------------------------------------------------* *CLEAR: ls_extensionin,ls_bape_vbak. *ls_extensionin-structure = 'BAPE_VBAK'. ** ls_bape_vbak-zppdy = 'CD01'. "品牌单元 *ls_bape_vbak-zjgxyh = gs_alv-zjgxyh. *ls_bape_vbak-zjgrwlyh = gs_alv-zjgrwlyh. *ls_bape_vbak-zthyy = gs_alv-zthyy. *ls_bape_vbak-zthlx = gs_alv-zthlx. *ls_extensionin+30(960) = ls_bape_vbak. *APPEND ls_extensionin TO lt_extensionin. * * *CLEAR: ls_extensionin,ls_bape_vbakx. *ls_extensionin-structure = 'BAPE_VBAKX'. ** ls_bape_vbakx-zppdy = 'X'. *ls_bape_vbakx-zjgxyh = 'X'. *ls_bape_vbakx-zthyy = 'X'. *ls_bape_vbakx-zthlx = 'X'. *ls_bape_vbakx-zjgrwlyh = 'X'. *ls_extensionin+30(960) = ls_bape_vbakx. *APPEND ls_extensionin TO lt_extensionin. *---------------------------------------------------------------------* ENDLOOP. * CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2' * EXPORTING * order_header_in = ls_order_header_in * order_header_inx = ls_order_header_inx * IMPORTING * salesdocument = lv_ebeln * TABLES * return = lt_return * order_items_in = lt_order_items_in * order_items_inx = lt_order_items_inx * order_partners = lt_order_partners * order_schedules_in = lt_schedules_in * order_schedules_inx = lt_schedules_inx. *----------------------------------------------------------------------------------------------------------- CALL FUNCTION 'BAPI_CUSTOMERRETURN_CREATE' EXPORTING * SALESDOCUMENTIN = return_header_in = ls_order_header_in return_header_inx = ls_order_header_inx IMPORTING salesdocument = lv_ebeln TABLES return = lt_return return_items_in = lt_order_items_in return_items_inx = lt_order_items_inx return_partners = lt_order_partners return_schedules_in = lt_schedules_in return_schedules_inx = lt_schedules_inx. *----------------------------------------------------------------------------------------------------------- LOOP AT lt_return WHERE type CA 'AE'. ENDLOOP. IF sy-subrc EQ 0. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. e_type = 'E'. e_msg = |销售订单 { lv_ebeln } 创建失败:{ lt_return-message } |. ELSE. e_type = 'S'. e_msg = |销售订单 { lv_ebeln } 创建成功 !|.. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. ENDIF.