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.

posted on 2010-08-23 10:42  zhumk  阅读(10512)  评论(0编辑  收藏  举报