Turn from "http://www.cnblogs.com/elegantok/archive/2009/10/18/1585398.html"
SALES ORDER INPUT CREATION.
Report Soi.
Parameters:p_auart TYPE Auart obligatory.
parameters:p_vkorg TYPE vkorg obligatory.
Parameters:p_vtweg TYPE Vtweg obligatory.
Parameters:p_spart TYPE Vtweg obligatory.
Parameters:p_sold TYPE Kunnr obligatory.
Parameters:p_ship TYPE Kunnr obligatory.
*item
Parameters:p_matnr TYPE Matnr obligatory.
Parameters:p_menge TYPE Kwmeng obligatory.
Parameters:p_plant TYPE werks_d obligatory.
Parameters:p_itcat TYPE pstyv obligatory.
* DATA DECLARATIONS.
Data:v_vbeln like Vbak-vbeln.
Data:header like Bapisdhead1.
Data:headerx like bapisdhead1x.
Data:item like Bapisditem occurs 0 with HEADER line.
Data:itemx like bapisditemx occurs 0 with HEADER line.
Data:partner like Bapipartnr occurs 0 with HEADER line.
Data:return like Bapiret2 occurs 0 with HEADER line.
Data:lt_schedules_inx TYPE Standard TABLE of BAPISCHDLX
With the HEADER line.
Data:lt_schedules_in TYPE Standard TABLE of BAPISCHDL
With the HEADER line.
* HEADER DATA
Header-doc_type = P_auart.
Headerx-doc_type = ' X '.
header-sales_org = p_vkorg.
headerx-sales_org = ' X '.
Header-distr_chan = P_vtweg.
Headerx-distr_chan = ' X '.
Header-division = P_spart.
headerx-division = ' X '.
Headerx-updateflag = ' I '.
* PARTNER DATA
Partner-partn_role = ' AG '.
Partner-partn_numb = P_sold.
APPEND partner.
Partner-partn_role = ' WE '.
Partner-partn_numb = p_ship.
APPEND partner.
* ITEM DATA
Itemx-updateflag = ' I '.
Item-itm_number = ' 000010 '.
Itemx-itm_number = ' X '.
Item-material = P_matnr.
itemx-material = ' X '.
Item-plant = P_plant.
itemx-plant = ' X '.
Item-target_qty = P_menge.
Itemx-target_qty = ' X '.
Item-target_qu = ' EA '.
Itemx-target_qu = ' X '.
Item-item_categ = P_itcat.
Itemx-item_categ = ' X '.
APPEND item.
APPEND Itemx.
* Fill Schedule lines
Lt_schedules_in-itm_number = ' 000010 '.
Lt_schedules_in-sched_line = ' 0001 '.
Lt_schedules_in-req_qty = P_menge.
APPEND lt_schedules_in.
* Fill Schedule Line Flags
Lt_schedules_inx-itm_number = ' 000010 '.
Lt_schedules_inx-sched_line = ' 0001 '.
Lt_schedules_inx-updateflag = ' X '.
Lt_schedules_inx-req_qty = ' X '.
APPEND Lt_schedules_inx.
* Call the BAPI
Call FUNCTION ' bapi_salesdocu_createfromdata1 ' Sales order creation BAPI, after creation, modify the delivery freeze as required.
Exporting
sales_header_in = Header
Sales_header_inx = Headerx
Importing
SALESDOCUMENT_EX = V_vbeln
TABLES
return = return
sales_items_in = Item
Sales_items_inx = Itemx
sales_schedules_in = lt_schedules_in
Sales_schedules_inx = Lt_schedules_inx
Sales_partners = partner.
* Check the return table.
LOOP at return WHERE type = ' E ' OR type = ' A '.
EXIT.
Endloop.
IF SY-SUBRC = 0.
WRITE:/' Error in creating document '.
ELSE.
COMMIT work and WAIT.
WRITE:/' Document ', V_vbeln, ' created '.
ENDIF.
DELIVERY CREATION.
Data:begin of t_vbap occurs 0,
Vbeln like Vbap-vbeln,
Posnr like Vbap-posnr,
Zmeng like Vbap-kwmeng,
Matnr like Vbap-matnr,
Werks like Vbap-werks,
END of T_VBAP.
Data:t_request TYPE Standard TABLE of bapideliciousrequest with HEADER line.
data:t_created TYPE Standard TABLE of Bapideliciouscreateditems with HEADER line.
Data:t_return TYPE Standard TABLE of Bapiret2 with HEADER line.
SELECT Vbeln Posnr Zmeng Matnr Werks
Into TABLE t_vbap
From VBAP
WHERE Vbeln = V_vbeln.
LOOP at T_VBAP.
T_request-document_numb = T_vbap-vbeln.
T_request-document_item = T_vbap-posnr.
T_request-quantity_sales_uom = T_vbap-zmeng.
T_request-quantity_base__uom = T_vbap-zmeng.
T_request-id = 1.
T_request-document_type = ' A '.
T_request-delivery_date = Sy-datum.
T_request-material = T_vbap-matnr.
T_request-plant = T_vbap-werks.
T_request-date = Sy-datum.
T_request-goods_issue_date = Sy-datum.
T_request-goods_issue_time = Sy-uzeit.
APPEND T_request.
Endloop.
Call FUNCTION ' bapi_deliveryprocessing_exec ' Outward delivery order creation, both the delivery quantity and the pick quantity are empty after creation. Need to fill in
TABLES
Request = T_request
Createditems = t_created
return = T_return.
READ TABLE t_return with KEY type = ' E '.
IF SY-SUBRC = 0.
MESSAGE e208 (xx) with ' Delivery creation error '.
ENDIF.
COMMIT work.
Post goods issue.
READ TABLE t_created INDEX 1.
Data:vbkok_wa TYPE Vbkok.
VBKOK_WA-VBELN_VL = T_created-document_numb.
Vbkok_wa-wabuc = ' X '.
Data:v_error.
Call FUNCTION ' ws_delivery_update ' "outward delivery BAPI
Exporting
Vbkok_wa = Vbkok_wa
Delivery = T_created-document_numb
Importing
Ef_error_in_goods_issue_0 = V_error.
COMMIT work.
Sales orders, outgoing delivery orders, delivery BAPI