【ABAP】BAPI_SALESORDER_CREATEFROMDAT2创建销售订单

注意点:
1、几个表的updateflag值为I;
2、要配置设备参数,要设置order_items_in-po_itm_no且ORDER_ITEMS_IN-PO_ITM_NO = ORDER_CFGS_REF-POSEX,配置表的order_cfgs_ref-config_id = order_cfgs_value-config_id。表order_cfgs_value传递特性值。并且,测试发现order_cfgs_value-inst_id 应等于 order_cfgs_ref-root_id。
3、extensionin增强传值的字段如果表VBAKKOM中没有,则在表VBAKKOM中APPEND字段后还需要在MV45AFZB中FORM userexit_move_field_to_vbakkom 添加代码把增强字段加进去。
【ABAP】BAPI_SALESORDER_CREATEFROMDAT2创建销售订单_第1张图片
【ABAP】BAPI_SALESORDER_CREATEFROMDAT2创建销售订单_第2张图片

仅为实现功能测试代码如下

FUNCTION zc_salesdocument_create.
*"----------------------------------------------------------------------
*"*"局部接口:
*"----------------------------------------------------------------------


  DATA: lt_return  LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
  DATA:
     l_vbeln         LIKE bapivbeln-vbeln,
  "订单头
       order_header_in  LIKE bapisdhd1 OCCURS 0 WITH HEADER LINE,
       order_header_inx LIKE bapisdhd1x OCCURS 0 WITH HEADER LINE,
  "订单行项
       order_items_in   LIKE bapisditm OCCURS 0 WITH HEADER LINE,
       order_items_inx  LIKE bapisditmx OCCURS 0 WITH HEADER LINE,
  "计划行
       order_schedules_in  LIKE bapischdl OCCURS 0 WITH HEADER LINE,
       order_schedules_inx LIKE bapischdlx OCCURS 0 WITH HEADER LINE,
  "定价条件
       order_condition_in  LIKE bapicond OCCURS 0 WITH HEADER LINE,
       order_condition_inx LIKE bapicondx  OCCURS 0 WITH HEADER LINE,
  "伙伴
       order_partners  LIKE bapiparnr OCCURS 0 WITH HEADER LINE,
  "逻辑设计,更新价格
       ls_logic_switch TYPE bapisdls,

       lt_extensionin TYPE TABLE OF bapiparex WITH HEADER LINE,
       ls_bape_vbak TYPE bape_vbak,
       ls_bape_vbakx TYPE bape_vbakx,


       lt_order_cfgs_value TYPE TABLE OF bapicuval WITH HEADER LINE,
       ls_order_cfgs_value TYPE bapicuval,

       lt_order_cfgs_ref  TYPE TABLE OF bapicucfg WITH HEADER LINE,
       ls_order_cfgs_ref  TYPE bapicucfg,

       lt_order_cfgs_inst TYPE TABLE OF bapicuins WITH HEADER LINE,
       ls_order_cfgs_inst TYPE bapicuins.


  order_header_in-doc_type  = 'ZOR2'  . "销售凭证类型
  order_header_in-sales_org  = '1000'  . "销售组织
  order_header_in-distr_chan = '10'  . "销售渠道
  order_header_in-division  = '00'  . "产品组
  order_header_in-sales_off  = '1111'  . "销售部门
  order_header_in-sales_grp  = '111'  . "销售组


  order_header_in-req_date_h = '20200101'  . "请求交货日期

*  order_header_in-war_date  = sy-datum  . "担保日期
  order_header_in-purch_date = sy-datum  . "客户采购订单日期
  order_header_in-purch_no_c = 'SA10040007'. "客户采购订单编号
  order_header_in-price_date = sy-datum  . "定价日期.
  order_header_in-pmnttrms   = 'Z01'.
  order_header_in-sales_dist = 'D1221'.
  APPEND order_header_in.

  order_header_inx-updateflag = 'I'.
  order_header_inx-doc_type   = 'X'.
  order_header_inx-sales_org  = 'X'.
  .  order_header_inx-distr_chan = 'X'.
  order_header_inx-division   = 'X'.
  order_header_inx-sales_off  = 'X'.
  order_header_inx-sales_grp  = 'X'  .
  .
*  order_header_inx-war_date   = 'X'.
  order_header_inx-req_date_h = 'X'.
  order_header_inx-purch_date = 'X'.
  order_header_inx-purch_no_c = 'X'.
*  order_header_inx-line_time  = 'X'.
  order_header_inx-price_date = 'X'.
  order_header_inx-pmnttrms  = 'X'.
  order_header_inx-sales_dist = 'X'.
  APPEND order_header_inx.

  order_partners-partn_role = 'WE'.
  order_partners-partn_numb = '0000200067'.
  APPEND order_partners.

  order_items_in-itm_number  = '000010'.
  order_items_in-material   = '000000000030000310'.
*  order_items_in-item_categ = 'ZKMN'.
*    order_items_in-sales_unit  = lt_vbap-vrkme .
* po_itm_no与配置表匹配
  order_items_in-po_itm_no = '000010'.
  order_items_in-target_qty  = 1.

  APPEND order_items_in .


  order_items_inx-updateflag  = 'I'.
  order_items_inx-itm_number  = '000010'.
  order_items_inx-material   = 'X'.
  order_items_inx-po_itm_no = 'X'.
*  order_items_inx-item_categ = 'X'.
*    order_items_inx-target_qu   = 'X'.
  order_items_inx-target_qty  = 'X'.

  APPEND order_items_inx.

  order_schedules_in-itm_number = '000010'.
  order_schedules_in-sched_line = '0001'.
  order_schedules_in-req_qty   = 1.

  APPEND order_schedules_in.

  order_schedules_inx-itm_number  = '000010'.
  order_schedules_inx-sched_line = '0001'.
  order_schedules_inx-updateflag = 'I'.
  order_schedules_inx-req_qty    = 'X'.

  APPEND order_schedules_inx.

  ls_logic_switch-pricing = 'G'.

*PRICING不同值含义的解释:
*
*A       复制价格组件和重新确定等级
*B       执行新的定价
*C       复制手工定价因素且重新确定其它因素
*D       复制不变的定价因素
*E       采用价格组件和固定值
*F       复制价格元素,调整价值与固定
*G       复制不变定价因素且重定税款
*H       重新确定运费条件
*J       重新确定确认的采购净价格/价值(KNTYPD*K       采纳价格组件和重新确定税收
*M       复制价格要素,转换值
*N       传输定价组件未更改, 新成本
*O       重新确定变式条件(KNTYP0*Q       重新确定计算条件(KNTYP=Q*R       应用价格部分和奖金条件
*U       重新确定贵重金属条件(KNTYP=U*S       装运&借方 (IBU HiTec)

  CLEAR: order_condition_in, order_condition_inx.

  order_condition_in-itm_number = '000010'.
  order_condition_in-cond_type  = 'ZR01' .
  order_condition_in-cond_value = '10000' .
*    order_condition_in-currency   = 'CNY' .
*    order_condition_in-cond_unit  = 'PC'.
  order_condition_in-cond_p_unt = '1'.
  order_condition_in-cond_updat = 'X'.

  APPEND order_condition_in .


  order_condition_inx-itm_number = '000010'.
  order_condition_inx-cond_type = 'ZR01' .  "定价条件
  order_condition_inx-updateflag = 'I'.
  order_condition_inx-cond_value = 'X'.  "价格
*    order_condition_inx-currency   = 'X'.  "货币
*    order_condition_inx-cond_unit = 'X'.  "条件单位
  order_condition_inx-cond_p_unt = 'X'.  "条件定价单位
  APPEND order_condition_inx  .

* 增强
  lt_extensionin-structure = 'BAPE_VBAK'.
  ls_bape_vbak-vbelh = 'SA10040007'.
  ls_bape_vbak-zvtweg = '10'.
  lt_extensionin-valuepart1 = ls_bape_vbak.
  APPEND lt_extensionin.

  lt_extensionin-structure = 'BAPE_VBAKX'.
  ls_bape_vbakx-vbelh = 'X'.
  ls_bape_vbakx-zvtweg = 'X'.
  lt_extensionin-valuepart1 = ls_bape_vbakx.
  APPEND lt_extensionin.

* 特性
  ls_order_cfgs_ref-posex = '000010'.
  ls_order_cfgs_ref-config_id = '000001'.
  ls_order_cfgs_ref-root_id = '00000001'.
  APPEND ls_order_cfgs_ref TO lt_order_cfgs_ref.

  ls_order_cfgs_value-config_id = '000001'.
  ls_order_cfgs_value-inst_id = '00000001'.
  ls_order_cfgs_value-charc = 'KLF_0010'.
  ls_order_cfgs_value-value = 'SA10040007'.
  APPEND ls_order_cfgs_value TO lt_order_cfgs_value.

  ls_order_cfgs_value-config_id = '000001'.
  ls_order_cfgs_value-inst_id =  '00000001'.
  ls_order_cfgs_value-charc = 'KLF_0020'.
  ls_order_cfgs_value-value = 'TEST'.
  APPEND ls_order_cfgs_value TO lt_order_cfgs_value.

*  ls_order_cfgs_inst-config_id =  '000010'.
*  ls_order_cfgs_inst-inst_id = '000010'.
*  APPEND ls_order_cfgs_inst TO lt_order_cfgs_inst.

*call FUNCTION

  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
      order_header_in      = order_header_in
      order_header_inx     = order_header_inx
      logic_switch         = ls_logic_switch
    IMPORTING
      salesdocument        = l_vbeln
    TABLES
      return               = lt_return
      order_items_in       = order_items_in
      order_items_inx      = order_items_inx
      order_schedules_in   = order_schedules_in
      order_schedules_inx  = order_schedules_inx
      order_partners       = order_partners
      order_conditions_in  = order_condition_in
      order_conditions_inx = order_condition_inx
      extensionin          = lt_extensionin
      order_cfgs_ref       = lt_order_cfgs_ref
      order_cfgs_value     = lt_order_cfgs_value.

  DATA: e_message(60).
  LOOP AT lt_return WHERE  type = 'E' OR  type = 'A'.
    e_message = lt_return-message .

    EXIT.
  ENDLOOP.

  IF  sy-subrc = 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    COMMIT WORK AND WAIT.

  ENDIF.

ENDFUNCTION.

你可能感兴趣的:(sap,abap,sap,abap)