BAPI_SALESORDER_CREATEFROMDAT2不带参考订单创建销售订单

项目场景:

创建不带参考的销售订单

实例:

**定义输入输出参数
*销售订单
  DATA:
    ls_saveic         LIKE  bapisdls,
    is_inputer_in     LIKE  bapisdhd1,
    is_inputer_inx    LIKE  bapisdhd1x,
    ls_header_in      LIKE  bapisdhd1,
    ls_header_inx     LIKE  bapisdhd1x,
    ls_partners       LIKE  bapiparnr,
    is_inputs_in      LIKE  bapisditm,
    is_inputs_inx     LIKE  bapisditmx,
    ls_conditions_in  LIKE  bapicond,
    ls_conditions_inx LIKE  bapicondx,
    ls_schedules_in   LIKE  bapischdl,
    ls_schedules_inx  LIKE  bapischdlx,
    ls_return         LIKE  bapiret2,
    lv_vbeln          TYPE  vbeln_va,
    lv_flag,
    lv_posnr          LIKE vbap-posnr. 

  DATA :
    ls_text           LIKE  bapisdtext,
    lt_return         LIKE  TABLE OF bapiret2,
    lt_text           LIKE  TABLE OF  bapisdtext,  "抬头数据
    lt_items_in       LIKE  TABLE OF  bapisditm,   "行项目
    lt_items_inx      LIKE  TABLE OF  bapisditmx,  
    lt_partners       LIKE  TABLE OF  bapiparnr,   "合作伙伴
    lt_schedules_in   LIKE  TABLE OF  bapischdl,   
    lt_schedules_inx  LIKE  TABLE OF  bapischdlx,  "计划行
    lt_conditions_in  LIKE  TABLE OF  bapicond,    "条件价格
    lt_conditions_inx LIKE  TABLE OF  bapicondx.


*日志表
  DATA lt_save LIKE TABLE OF zsdlogt009.
  DATA ls_save LIKE zsdlogt009.
  DATA es_return LIKE zsy_sd_dmss021.
*转化表
  DATA: lt_head  LIKE TABLE OF zsy_sd_dmss020, 
 ”因为有多行的可能,需要把数据存到另一个表里面,去重然后通过主键添加行项目。
        is_input LIKE zsy_sd_dmss020,
        ls_head  LIKE zsy_sd_dmss020.

  LOOP AT  it_input INTO is_input.
 "转移数据的同时增加前导零和判断触发条件
    IF is_input-zfklx = 'FKLX0003'.
      MOVE-CORRESPONDING is_input TO ls_head.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = ls_head-vbeln
        IMPORTING
          output = ls_head-vbeln.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = ls_head-kunnr
        IMPORTING
          output = ls_head-kunnr.

      APPEND ls_head TO lt_head .
    ENDIF.
    CLEAR: ls_head , is_input.
  ENDLOOP.

  SORT lt_head BY bstnk. "排序以后删除重复项
  DELETE ADJACENT DUPLICATES FROM lt_head COMPARING  bstnk. 

采购订单的创建 :va01.
创建的时候分为抬头数据和行项目数据,如果同一个订单有多个行项,就需要注意数据的备份和去重。
1、必填的信息:
订单类型 VBAK-auart
 IF lt_head[] IS NOT INITIAL.
    "数据处理
    LOOP AT lt_head INTO ls_head.
      "  CHECK ls_head-zfklx = 'FKLX0003'.
      ls_saveic-cond_handl = 'X'.
***销售订单抬头数据
      ls_header_in-doc_type   = 'Z010'.       "销售凭证类型
      ls_header_in-req_date_h = ls_head-audat."时间
      ls_header_in-purch_no_c = ls_head-bstnk."DMS单号
      ls_header_in-sales_off  = ls_head-vkbur."销售办事处
      ls_header_in-doc_date   = ls_head-audat."凭证日期
      ls_header_in-cust_grp1  = ls_head-kvgr1."客户组 1
      ls_header_in-cust_grp2  = ls_head-kvgr2."客户组 2
      ls_header_in-cust_grp3  = ls_head-kvgr3."客户组 3
      ls_header_in-cust_grp4  = ls_head-kvgr4."客户组 4
      ls_header_in-cust_grp5  = ls_head-kvgr5."客户组 5

**取销售组织VKORG、分销渠道VTWEG、产品组SPART
      SELECT SINGLE vkorg vtweg spart
      INTO (ls_header_in-sales_org,ls_header_in-distr_chan,ls_header_in-division)
      FROM tvkbz
      WHERE vkbur = ls_head-vkbur.

更新方式:updateflag 分别是:(I、 U) I是新增 ,U是修改

      ls_header_inx-updateflag = 'I'."更新方式 
      ls_header_inx-doc_type   = biu."销售凭证类型
      ls_header_inx-req_date_h = biu."时间
      ls_header_inx-purch_no_c = biu."DMS单号
      ls_header_inx-sales_off  = biu."销售办事处
      ls_header_inx-doc_date   = biu."凭证日期
      ls_header_inx-cust_grp1  = biu."客户组 1
      ls_header_inx-cust_grp2  = biu."客户组 2
      ls_header_inx-cust_grp3  = biu."客户组 3
      ls_header_inx-cust_grp4  = biu."客户组 4
      ls_header_inx-cust_grp5  = biu."客户组 5
      ls_header_inx-sales_org  = biu."销售组织
      ls_header_inx-distr_chan = biu."分销渠道
      ls_header_inx-division   = biu."产品组

”in和inx两个表是一 一对应的,in是给值,inx是更新值。

**抬头 合作伙伴
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = ls_head-kunnr
        IMPORTING
          output = ls_partners-partn_numb.

      ls_partners-partn_role = 'AG'.
      APPEND ls_partners TO lt_partners.
      ls_partners-partn_role = 'RE'.
      APPEND ls_partners TO lt_partners.
      ls_partners-partn_role = 'RG'.
      APPEND ls_partners TO lt_partners.
      ls_partners-partn_role = 'WE'.
      APPEND ls_partners TO lt_partners.
      CLEAR ls_partners.

**销售订单行项目数据****
      CLEAR lv_posnr.
      LOOP AT it_input INTO is_input WHERE  bstnk = ls_head-bstnk.
        lv_posnr = lv_posnr + 10 .
        is_inputs_in-itm_number = lv_posnr."行项目号
        CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
          EXPORTING
            input        = is_input-matnr
          IMPORTING
            output       = is_inputs_in-material_long
          EXCEPTIONS
            length_error = 1
            OTHERS       = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        is_inputs_in-target_qty = is_input-kwmeng. "订单数量
        is_inputs_in-target_qu  = is_input-vrkme.  "单位
        is_inputs_in-purch_no_s = is_input-detail_id."dms 行项目id
        is_inputs_in-dlv_time   = is_input-vdatu.  "请求交货日期
        SELECT SINGLE dwerk INTO is_inputs_in-plant FROM mvke WHERE matnr = is_inputs_in-material_long
        AND vkorg = ls_header_in-sales_org
        AND vtweg = '00'.

        is_inputs_inx-itm_number     = lv_posnr."行项目号
        is_inputs_inx-updateflag     = 'I'.
        is_inputs_inx-material_long  = biu."物料编号
        is_inputs_inx-target_qty     = biu."订单数量
        is_inputs_inx-target_qu      = biu."单位
        is_inputs_inx-plant          = biu."工厂
        is_inputs_inx-purch_no_s     = biu."dms 行项目id
        is_inputs_inx-dlv_time       = biu."请求交货日期
        APPEND is_inputs_in TO lt_items_in.
        APPEND is_inputs_inx TO lt_items_inx.

*计划行
        ls_schedules_in-itm_number  = lv_posnr."行项目号
        ls_schedules_in-sched_line  = 1.
        ls_schedules_in-req_qty     = is_input-kwmeng ."数量

        ls_schedules_inx-itm_number = lv_posnr."行项目号
        ls_schedules_inx-sched_line = 1.
        ls_schedules_inx-updateflag = 'I'.
        ls_schedules_inx-req_qty    = 'X'  ."数量
        APPEND ls_schedules_in TO lt_schedules_in.
        APPEND ls_schedules_inx TO lt_schedules_inx.

*价格条件
        ls_conditions_in-itm_number  = lv_posnr."行项目号
        ls_conditions_in-cond_type   = 'ZP01'.
        ls_conditions_in-cond_value  = is_input-zp01.

        ls_conditions_inx-itm_number = lv_posnr."行项目号
        ls_conditions_inx-cond_type  = 'ZP01'.
        ls_conditions_inx-updateflag = 'I'.
        ls_conditions_inx-cond_value = 'X'.

*      ls_conditions_in-itm_number = lv_posnr."行项目号
*      ls_conditions_in-cond_type  = 'ZF20'.
*      ls_conditions_in-cond_value = is_input-zF20.
*
*      ls_conditions_inx-itm_number = lv_posnr."行项目号
*      ls_conditions_inx-cond_type  = 'ZF20'.
*      ls_conditions_inx-updateflag = 'I'.
*      ls_conditions_inx-cond_value = 'X'.

        APPEND ls_conditions_in TO lt_conditions_in.
        APPEND ls_conditions_inx TO lt_conditions_inx.


        CLEAR :ls_partners,ls_conditions_in,ls_conditions_inx,
        ls_schedules_in ,ls_schedules_inx,is_inputs_in,is_inputs_inx.

*日志记录
        MOVE-CORRESPONDING is_input TO ls_save.
        ls_save-posnr = lv_posnr.
        ls_save-zdate = sy-datum.
        ls_save-ztime = sy-uzeit.
        APPEND ls_save TO lt_save.
        CLEAR ls_save.
      ENDLOOP.

创建采购订单的函数 :BAPI_SALESORDER_CREATEFROMDAT2
      CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
        EXPORTING
*         SALESDOCUMENTIN      =
          order_header_in      = ls_header_in
          order_header_inx     = ls_header_inx
          logic_switch         = ls_saveic
*         TESTRUN              =
        IMPORTING
          salesdocument        = lv_vbeln
        TABLES
          return               = lt_return
          order_items_in       = lt_items_in
          order_items_inx      = lt_items_inx
          order_partners       = lt_partners
          order_schedules_in   = lt_schedules_in
          order_schedules_inx  = lt_schedules_inx
          order_conditions_in  = lt_conditions_in
          order_conditions_inx = lt_conditions_inx.

      CLEAR lv_flag.
      LOOP AT lt_return INTO ls_return WHERE type CA 'AEX'.
        lv_flag = 'X'.
        EXIT.
      ENDLOOP.

      IF  lv_flag = 'X'.
        es_return-bstnk = ls_head-bstnk.
        es_return-ztype = 'E'.
        es_return-rsnum = ls_head-rsnum.
        es_return-zmessage = ls_return-message.
        APPEND es_return TO et_return.

更新数据库表,或者前台字段的值的时候,需要在调用函数以后调用commit。
 失败的话调用 rollback 。
        CLEAR ls_save.
        ls_save-ztype = 'E'.
        ls_save-zmessage = ls_return-message.
        MODIFY lt_save FROM ls_save TRANSPORTING ztype zmessage WHERE bstnk = ls_head-bstnk.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        CONTINUE.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
        es_return-bstnk = ls_head-bstnk.
        es_return-ztype = 'S'.
        es_return-rsnum = ls_head-rsnum.
        es_return-vbeln = lv_vbeln.
        APPEND es_return TO et_return.
        CLEAR es_return.
      ENDIF.



 


你可能感兴趣的:(BAPI_SALESORDER_CREATEFROMDAT2不带参考订单创建销售订单)