ABAP:采购订单创建BAPI

采购订单创建BAPI:BAPI_PO_CREATE1

FORM create_po  TABLES   p_et_return STRUCTURE zrfct005
                USING    p_ls_zsdt044 TYPE zsdt044
                         p_ls_zsdt041 TYPE zsdt041
                         p_ls_zsdt044_1 TYPE zsdt044
                         p_ls_zsdt045 TYPE zsdt045
                         p_i_id
                         p_i_canum
                         pv_werks
                CHANGING p_e_code
                         p_e_msg.
  DATA:poheader    TYPE bapimepoheader,
       poheaderx   TYPE bapimepoheaderx,
       poitem      LIKE TABLE OF bapimepoitem        WITH HEADER LINE,
       poitemx     LIKE TABLE OF bapimepoitemx       WITH HEADER LINE,
       poschedule  LIKE TABLE OF bapimeposchedule    WITH HEADER LINE,
       poschedulex LIKE TABLE OF bapimeposchedulx    WITH HEADER LINE,
       pocond      LIKE TABLE OF bapimepocond        WITH HEADER LINE,
       pocond2     LIKE TABLE OF bapimepocond        WITH HEADER LINE,
       pocondx     LIKE TABLE OF bapimepocondx       WITH HEADER LINE,
       pocondx2    LIKE TABLE OF bapimepocondx       WITH HEADER LINE,
       popartner   LIKE TABLE OF bapiekkop           WITH HEADER LINE,
       return      LIKE TABLE OF bapiret2            WITH HEADER LINE,
       l_eblen     TYPE ebeln,
       lt_zsdt008  LIKE TABLE OF zsdt008,
       ls_zsdt008  LIKE LINE OF lt_zsdt008.
  DATA:lv_from_cur TYPE bapi1093_1-from_curr,
       lv_to_cur   TYPE bapi1093_1-to_currncy,
       ls_rate     TYPE bapi1093_0,
       ls_return   TYPE bapiret1,
       ls_zrfct002 TYPE zrfct002,
       ls_zsdt044  TYPE zsdt044.

  DATA:lv_f_curr TYPE tcurr-fcurr,
       lv_l_curr TYPE tcurr-tcurr,
       lv_rate   TYPE string.

  "查询采购订单数据
  SELECT SINGLE *
    FROM ekko
    INTO @DATA(ls_ekko)
   WHERE ebeln EQ @p_ls_zsdt044_1-objkey.

  "抬头字段赋值
  "采购订单类型
  poheader-doc_type = p_ls_zsdt041-zauart.
  poheaderx-doc_type = 'X'.
  "采购组织
  poheader-purch_org = ls_ekko-ekorg.
  poheaderx-purch_org = 'X'.
  "采购组
  poheader-pur_group = ls_ekko-ekgrp.
  poheaderx-pur_group = 'X'.
  "公司代码
  poheader-comp_code = ls_ekko-bukrs.
  poheaderx-comp_code = 'X'.
  "供应商
  poheader-vendor = ls_ekko-llief.
  poheaderx-vendor = 'X'.
  "参考
  poheader-ref_1 = ''.
  poheaderx-ref_1 = 'X'.
  "凭证日期
  poheader-doc_date = p_ls_zsdt045-budat.
  poheaderx-doc_date = 'X'.
  "语言
  poheader-langu = sy-langu.
  poheaderx-langu = 'X'.
  "币别
  poheader-currency = p_ls_zsdt041-waerk.
  poheaderx-currency = 'X'.
  "行项目手动编号
  poheader-item_intvl = space .
  poheaderx-item_intvl = 'X'.
  "查询SO数据
  SELECT *
    FROM vbap
    INTO TABLE @DATA(lt_vbap)
   WHERE vbeln EQ @p_ls_zsdt044-objkey.

  LOOP AT lt_vbap INTO DATA(ls_vbap).
    "如果销售订单已拒绝,跳过
    IF ls_vbap-abgru NE ''.
      CLEAR:ls_vbap.
      CONTINUE.
    ENDIF.
    "行项目数据赋值
    "行项目
    poitem-po_item = ls_vbap-posnr.
    poitemx-po_item = ls_vbap-posnr.
    "物料编码
    poitem-material = ls_vbap-matnr.
    poitemx-material = 'X'.
    "行项目数量
    poitem-quantity = ls_vbap-zmeng.
    poitemx-quantity = 'X'.
    "行项目数量单位
    poitem-po_unit = ls_vbap-vrkme.
    poitemx-po_unit = 'X'.
    "基于收获的发票校验
    poitem-gr_basediv = ''.
    poitemx-gr_basediv = 'X'.
    "收货
    poitem-gr_ind = 'X'.
    poitemx-gr_ind = 'X'.
    "收获已完成
    poitem-no_more_gr = 'X'.
    poitemx-no_more_gr = 'X'.
    "工厂
    poitem-plant = pv_werks.
    poitemx-plant = 'X'.
    "税码
    poitem-tax_code = p_ls_zsdt041-mwskz.
    poitemx-tax_code = 'X'.
    APPEND:poitem,poitemx.
    CLEAR:poitem,poitemx.

    "赋值条件
    "条件行项目
    pocond-itm_number = ls_vbap-posnr.
    pocondx-itm_number = ls_vbap-posnr.
    "条件类型
    pocond-cond_type = p_ls_zsdt041-kschl.
    pocondx-cond_type = 'X'.
    "查找条件号
    SELECT SINGLE knumv
     INTO @DATA(lv_knumv)
     FROM vbak
    WHERE vbeln = @ls_vbap-vbeln.
    "查询币别,价格单位,金额
    SELECT SINGLE waers kpein kmein kbetr
      INTO ( pocond-currency,
             pocond-cond_p_unt,
             pocond-cond_unit,
             pocond-cond_value
           )
      FROM konv
     WHERE knumv = lv_knumv
       AND kposn = ls_vbap-posnr
       AND kschl = p_ls_zsdt041-zkschl
       AND kinak = ''.
    CLEAR:lv_knumv.
    "币别
    pocondx-currency = 'X'.
    "条件定价单位
    pocondx-cond_p_unt = 'X'.
    "条件单位
    pocondx-cond_unit = 'X'.
    "金额
    pocondx-cond_value = 'X'.
    "抬头币别不一致做汇率转换
    IF pocond-currency NE poheader-currency.
      CLEAR:lv_f_curr,lv_l_curr,lv_rate.
      lv_f_curr = poheader-currency.
      lv_l_curr = pocond-currency.
      CALL FUNCTION 'READ_EXCHANGE_RATE'
        EXPORTING
          date             = sy-datum
          foreign_currency = lv_f_curr
          local_currency   = lv_l_curr
        IMPORTING
          exchange_rate    = lv_rate.

      pocond-conexchrat = lv_rate.
      pocondx-conexchrat = 'X'.
    ENDIF.

    pocondx-change_id = 'X'.
    DO 2 TIMES.
      IF sy-index = 1.
        pocond-change_id = 'D'.
      ELSE.
        pocond-change_id = 'I'.
      ENDIF.

      APPEND:pocond,pocondx.
    ENDDO.
    CLEAR:pocondx,pocond.
    "条件行项目
    pocond-itm_number = ls_vbap-posnr.
    pocondx-itm_number = ls_vbap-posnr.

    "条件类型
    pocond-cond_type = 'PBXX'.
    pocondx-cond_type = 'X'.

    "删除PB00条件类型
    pocond-change_id = 'D'.
    pocondx-change_id = 'X'.

    APPEND:pocond,pocondx.
    CLEAR:pocond,pocondx.
    CLEAR:ls_vbap.
  ENDLOOP.

  CALL FUNCTION 'BAPI_PO_CREATE1'
    EXPORTING
      poheader         = poheader
      poheaderx        = poheaderx
    IMPORTING
      exppurchaseorder = l_eblen
    TABLES
      return           = return
      poitem           = poitem
      poitemx          = poitemx
      pocond           = pocond
      pocondx          = pocondx.

  LOOP AT return WHERE type = 'E' OR type = 'A'.
    CONCATENATE p_e_msg return-message INTO p_e_msg SEPARATED BY '/'.
  ENDLOOP.
  IF sy-subrc NE 0.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = p_i_id
        i_canum    = p_i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.
    "更新zsdt044表
    ls_zsdt044-zrfc_logid = p_i_id.
    ls_zsdt044-canum = p_i_canum.
    ls_zsdt044-zindex = 1.
    ls_zsdt044-objtype = ls_zrfct002-objtype_save.
    ls_zsdt044-objkey = l_eblen.
    ls_zsdt044-zobjkey = p_ls_zsdt045-vbeln.
    ls_zsdt044-zrfcid = p_ls_zsdt041-zrfcid.

    MODIFY zsdt044 FROM ls_zsdt044.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.

    p_e_code = 'S'.
    CONCATENATE 'PO:' l_eblen '创建成功' INTO p_e_msg.
    DATA:ls_zrfct005 TYPE zrfct005.
    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = p_e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = p_e_msg.
    ls_zrfct005-msgv4 = l_eblen.
    APPEND ls_zrfct005 TO p_et_return.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    p_e_code = 'E'.
  ENDIF.
ENDFORM.

 创建采购订单时,创建完成后的采购订单数量可能跟输入的采购订单数量不一致,原因可能是物料主数据中设置了最小起订量或者舍入值.

如果想要忽略最小起订量和舍入值,仍然按照输入数量进行创建,则需要给行项目中的NO_ROUNDING字段打上'X'标识.

 

posted @ 2022-12-16 16:47  阿胖的阿多  阅读(823)  评论(0编辑  收藏  举报