Create sales scheduling agreement with FM

M517發表於2012-03-29
Standard function module SD_SALESDOCUMENT_CREATE with Business Object BUS2035. (BAPI Importing parameter BUSINESS_OBJECT)[@more@]附:BAPI:BAPI_SALESORDER_CREATEFROMDAT2 建立銷售訂單

FORM frm_create_salesorder TABLES tp_item STRUCTURE zrmxsds001
tp_cond STRUCTURE zrmxsds002
tp_message STRUCTURE zifsret01
USING up_head LIKE zrmxsds000
CHANGING cp_eind TYPE c.

DATA: ls_item LIKE zrmxsds001,
ls_cond LIKE zrmxsds002,
ls_msg LIKE zifsret01.

DATA: wa_header TYPE bapisdhd1, "表頭
wa_headerx TYPE bapisdhd1x, "表頭標誌

wa_partner TYPE bapiparnr, "業務夥伴
wa_item TYPE bapisditm, "行專案
wa_itemx TYPE bapisditmx,

wa_cond TYPE bapicond, "價格條件
wa_condx TYPE bapicondx,

wa_schdl TYPE bapischdl, "交付計劃
wa_schdlx TYPE bapischdlx,

wa_return TYPE bapiret2,

wa_text TYPE bapisdtext. "文字

DATA:
lt_partner TYPE STANDARD TABLE OF bapiparnr,

lt_item TYPE STANDARD TABLE OF bapisditm,
lt_itemx TYPE STANDARD TABLE OF bapisditmx,

lt_schdl TYPE STANDARD TABLE OF bapischdl,
lt_schdlx TYPE STANDARD TABLE OF bapischdlx,

lt_cond TYPE STANDARD TABLE OF bapicond,
lt_condx TYPE STANDARD TABLE OF bapicondx,

lt_return TYPE STANDARD TABLE OF bapiret2,

lt_text LIKE STANDARD TABLE OF bapisdtext.

DATA: lt_sokey TYPE STANDARD TABLE OF zrmxsds015,
ls_sokey TYPE zrmxsds015.


* Header
CLEAR wa_header.
wa_header-doc_type = up_head-auart. "銷售憑證型別
wa_header-sales_org = up_head-vkorg. "銷售組織
wa_header-distr_chan = up_head-vtweg. "分銷渠道
wa_header-division = up_head-spart. "產品組
wa_header-sales_off = up_head-vkbur. "銷售部門
wa_header-sales_dist = up_head-bzirk. "銷售地區
wa_header-incoterms1 = up_head-inco1. "國際貿易條件
wa_header-incoterms2 = up_head-inco2. "國際貿易條件
wa_header-pmnttrms = up_head-zterm. "付款條件

* Header texts
CLEAR: wa_text,lt_text[].
wa_text-itm_number = space.
wa_text-text_id = 'ZC01'.
wa_text-langu = sy-langu.
wa_text-format_col = '*'.
wa_text-text_line = up_head-tknum.
APPEND wa_text TO lt_text.

* Partners
CLEAR: wa_partner, lt_partner[].
wa_partner-partn_role = 'AG'. "售達方
wa_partner-partn_numb = up_head-kunnr_ag.
IF up_head-name1_ag IS NOT INITIAL. "一次性客戶
wa_partner-name = up_head-name1_ag.
wa_partner-country = 'CN'.
ENDIF.
APPEND wa_partner TO lt_partner.

CLEAR wa_partner.
wa_partner-partn_role = 'RE'. "收票方
wa_partner-partn_numb = up_head-kunnr_re.
IF up_head-name1_re IS NOT INITIAL. "一次性客戶
wa_partner-name = up_head-name1_re.
wa_partner-country = 'CN'.
ENDIF.
APPEND wa_partner TO lt_partner.

"RG 付款方
CLEAR wa_partner.
wa_partner-partn_role = 'RG'.
wa_partner-partn_numb = up_head-kunnr_rg.
IF up_head-name1_rg IS NOT INITIAL. "一次性客戶
wa_partner-name = up_head-name1_rg.
wa_partner-country = 'CN'.
ENDIF.
APPEND wa_partner TO lt_partner.

"WE 送達方
CLEAR wa_partner.
wa_partner-partn_role = 'WE'.
wa_partner-partn_numb = up_head-kunnr_we.
IF up_head-name1_we IS NOT INITIAL. "一次性客戶
wa_partner-name = up_head-name1_we.
wa_partner-country = 'CN'.
ENDIF.
APPEND wa_partner TO lt_partner.


*Items
REFRESH: lt_item,lt_cond,lt_schdl.

LOOP AT tp_item INTO ls_item.
CLEAR wa_item.
wa_item-itm_number = ls_item-posnr.
wa_item-material = ls_item-mabnr. "物料
wa_item-sales_unit = ls_item-vrkme. "計量單位
wa_item-plant = ls_item-werks. "工廠
wa_item-store_loc = ls_item-lgort. "庫存地
wa_item-val_type = ls_item-bwtar. "評估型別
wa_item-ship_type = 'Z1'. "裝運型別-汽車

APPEND wa_item TO lt_item.

CLEAR wa_schdl.
wa_schdl-itm_number = ls_item-posnr.
wa_schdl-req_qty = ls_item-kwmeng. "數量

APPEND wa_schdl TO lt_schdl.

CLEAR ls_sokey.
ls_sokey-vbeln = up_head-vbeln.
ls_sokey-posnr = ls_item-posnr.
APPEND ls_sokey TO lt_sokey.
ENDLOOP.

*Item Conditions
LOOP AT tp_cond INTO ls_cond.
CLEAR: wa_cond.
wa_cond-itm_number = ls_cond-posnr.
wa_cond-cond_type = ls_cond-kschl. "定價條件
wa_cond-cond_value = ls_cond-kbetr. "價格
wa_cond-currency = ls_cond-koein. "貨幣或%
wa_cond-cond_unit = ls_cond-kmein. "條件單位
wa_cond-cond_p_unt = ls_cond-kpein. "條件定價單位
APPEND wa_cond TO lt_cond.
ENDLOOP.

* Call BAPI
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
salesdocumentin = up_head-vbeln
order_header_in = wa_header
* ORDER_HEADER_INX =
* SENDER =
* BINARY_RELATIONSHIPTYPE =
* INT_NUMBER_ASSIGNMENT =
* BEHAVE_WHEN_ERROR =
* LOGIC_SWITCH =
* TESTRUN =
* CONVERT = ' '
* IMPORTING
* SALESDOCUMENT =
TABLES
return = lt_return
order_items_in = lt_item
* ORDER_ITEMS_INX =
order_partners = lt_partner
order_schedules_in = lt_schdl
* ORDER_SCHEDULES_INX =
order_conditions_in = lt_cond
* ORDER_CONDITIONS_INX =
* ORDER_CFGS_REF =
* ORDER_CFGS_INST =
* ORDER_CFGS_PART_OF =
* ORDER_CFGS_VALUE =
* ORDER_CFGS_BLOB =
* ORDER_CFGS_VK =
* ORDER_CFGS_REFINST =
* ORDER_CCARD =
order_text = lt_text
* ORDER_KEYS =
* EXTENSIONIN =
* PARTNERADDRESSES =
.

* 處理錯誤訊息:透過判斷訊息的型別,來判斷BAPI是否成功
LOOP AT lt_return INTO wa_return.
CLEAR ls_msg.
ls_msg-class = 'BUS'.
ls_msg-msgtyp = wa_return-type.
ls_msg-msgno = wa_return-number.
ls_msg-msgtxt = wa_return-message.
APPEND ls_msg TO tp_message.

IF wa_return-type EQ 'E' OR
wa_return-type = 'A' OR
wa_return = 'X'.
cp_eind = 'X'. "失敗
ENDIF.

ENDLOOP.

IF cp_eind NE 'X'.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.

COMMIT WORK AND WAIT.

ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
ENDIF.

EndFrom.

引自:http://www.cnblogs.com/zhumk/archive/2010/08/23/1806236.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/21882360/viewspace-1057762/,如需轉載,請註明出處,否則將追究法律責任。

相關文章