BAPI_PO_CREATE1 行项目内文处理

2021-05-24 浏览
*&---------------------------------------------------------------------*
*& Report  ZMM_UPLOAD_PO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zmm_upload_po.

DATA:BEGIN OF gt_data OCCURS 0,
          bsart TYPE string,"采购凭证类型
          lifnr  TYPE string,"供应商帐户号
          ekorg TYPE string,"采购组织
          ekgrp TYPE string,"采购组
          bukrs TYPE string,"公司代码
          ihrez  TYPE string,"您的参考
          ebelp  TYPE string,"采购凭证的项目编号
          knttp TYPE string,"科目分配类别
          txz01 TYPE string,"短文本
          menge TYPE string,"采购订单数量
          meins TYPE string,"采购订单的计量单位
          eeind TYPE string,"交货日期W
          netpr TYPE string,"净价
          waers TYPE string,"货币 W
          matkl TYPE string,"物料组
          werks TYPE string,"工厂
          bednr TYPE string,"需求跟踪号
          afnam TYPE string,"需求者/请求者姓名
          mwskz TYPE string,"销售税代码
          sakto TYPE string,"总帐科目
          kostl TYPE string,"成本中心
          anln1 TYPE string,"资产
          aufnr TYPE string,"订单
          str1   TYPE string,"行项目文本-来自采购申请项目详细清单
          str2   TYPE string,"行项目文本-规格型号
          str3   TYPE string,"行项目文本-RFQ Reference
          bstae TYPE string,"确认控制
        END OF gt_data.

DATA: BEGIN OF gt_out OCCURS 0,
            text(255),
          END OF gt_out.


DATA:  poheader  LIKE TABLE OF bapimepoheader  WITH HEADER LINE ,
           poheaderx LIKE TABLE OF bapimepoheaderx WITH HEADER LINE,
           return       LIKE TABLE OF bapiret2  WITH HEADER LINE,
           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,
           poaccount LIKE TABLE OF bapimepoaccount WITH HEADER LINE,
           poaccountx LIKE TABLE OF bapimepoaccountx WITH HEADER LINE,
           pocond LIKE TABLE OF bapimepocond WITH HEADER LINE,
           pocondx LIKE TABLE OF bapimepocondx WITH HEADER LINE,

           potextitem LIKE TABLE OF bapimepotext WITH HEADER LINE.

DATA: epo LIKE bapimepoheader-po_number.

DATA: l_return LIKE return.
DATA: lv_message(255).

CONSTANTS: con VALUE 'X'.

DATA: pp_file TYPE string.

DATA: lifnr TYPE elifn.
DATA: netpr TYPE bprei.
DATA: bstae TYPE bstae.

PARAMETERS:p_file(128) .


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM get_file.

START-OF-SELECTION.
  PERFORM get_data.
  PERFORM create_po.
  PERFORM write_out.


*&---------------------------------------------------------------------*
*&      Form  GET_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_file .
  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            def_filename     = p_file
            mask             = ',*.txt,*.TXT.'
            mode             = 'O'
            title            = 'File Name'
       IMPORTING
            filename         =  p_file
       EXCEPTIONS
            inv_winsys       = 1
            no_batch         = 2
            selection_cancel = 3
            selection_error  = 4
            OTHERS           = 5.
  pp_file = p_file.
ENDFORM.                    " GET_FILE
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_data .
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                      = pp_file
     filetype                      = 'ASC'
     has_field_separator           = 'X'
*   HEADER_LENGTH                 = 0
     read_by_line                  = 'X'
*   DAT_MODE                      = ' '
*   CODEPAGE                      = ' '
*   IGNORE_CERR                   = ABAP_TRUE
*   REPLACEMENT                   = '#'
*   CHECK_BOM                     = ' '
*   VIRUS_SCAN_PROFILE            =
*   NO_AUTH_CHECK                 = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
    TABLES
      data_tab                      = gt_data
   EXCEPTIONS
     file_open_error               = 1
     file_read_error               = 2
     no_batch                      = 3
     gui_refuse_filetransfer       = 4
     invalid_type                  = 5
     no_authority                  = 6
     unknown_error                 = 7
     bad_data_format               = 8
     header_not_allowed            = 9
     separator_not_allowed         = 10
     header_too_long               = 11
     unknown_dp_error              = 12
     access_denied                 = 13
     dp_out_of_memory              = 14
     disk_full                     = 15
     dp_timeout                    = 16
     OTHERS                        = 17
            .
  IF sy-subrc <> 0.
    CASE sy-subrc.
      WHEN 1.MESSAGE 'FILE_OPEN_ERROR ' TYPE 'E'.
      WHEN 2.MESSAGE 'FILE_READ_ERROR ' TYPE 'E'.
      WHEN 3.MESSAGE 'NO_BATCH' TYPE 'E'.
      WHEN 4.MESSAGE 'GUI_REFUSE_FILETRANSFER  ' TYPE 'E'.
      WHEN 5.MESSAGE 'INVALID_TYPE ' TYPE 'E'.
      WHEN 6.MESSAGE ' NO_AUTHORITY' TYPE 'E'.
      WHEN 7.MESSAGE 'UNKNOWN_ERROR' TYPE 'E'.
      WHEN 8.MESSAGE 'BAD_DATA_FORMAT ' TYPE 'E'.
      WHEN 9.MESSAGE 'HEADER_NOT_ALLOWED' TYPE 'E'.
      WHEN 10.MESSAGE 'SEPARATOR_NOT_ALLOWED' TYPE 'E'.
      WHEN 11.MESSAGE 'HEADER_TOO_LONG ' TYPE 'E'.
      WHEN 12.MESSAGE 'UNKNOWN_DP_ERROR' TYPE 'E'.
      WHEN 13.MESSAGE ' ACCESS_DENIED ' TYPE 'E'.
      WHEN 14.MESSAGE 'DP_OUT_OF_MEMORY ' TYPE 'E'.
      WHEN 15.MESSAGE 'DISK_FULL  ' TYPE 'E'.
      WHEN 16.MESSAGE 'DP_TIMEOUT' TYPE 'E'.
      WHEN 17.MESSAGE ' OTHERS  ' TYPE 'E'.
      WHEN OTHERS.
    ENDCASE.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  CREATE_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_po .
  LOOP AT gt_data.
    AT NEW  ihrez.
      CLEAR lifnr.
      lifnr = gt_data-lifnr.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = lifnr
        IMPORTING
          output = lifnr.

      poheader-doc_type    = gt_data-bsart.
      poheader-vendor       = lifnr.
      poheader-purch_org  = gt_data-ekorg.
      poheader-pur_group  = gt_data-ekgrp.
      poheader-comp_code = gt_data-bukrs.
      poheader-ref_1          = gt_data-ihrez.
      poheader-doc_date   = sy-datum.
      poheader-langu         =  sy-langu.
      poheader-currency = gt_data-waers."货币


      poheaderx-doc_type    = con.
      poheaderx-vendor       = con.
      poheaderx-purch_org  = con.
      poheaderx-pur_group  = con.
      poheaderx-comp_code = con.
      poheaderx-ref_1          = con.
      poheaderx-doc_date   = con.
      poheaderx-langu         =  con.
      poheaderx-currency = con."货币

    ENDAT.

    CLEAR netpr.
    netpr = gt_data-netpr.

    CLEAR bstae.
    bstae = gt_data-bstae.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = bstae
      IMPORTING
        output = bstae.


    poitem-po_item =  gt_data-ebelp. "采购凭证的项目编号
    poitem-acctasscat = gt_data-knttp."科目分配类别
    poitem-short_text = gt_data-txz01."短文本
    poitem-quantity = gt_data-menge."采购订单数量
    poitem-po_unit = gt_data-meins ."采购订单的计量单位
    poitem-net_price = netpr."净价
    poitem-matl_group = gt_data-matkl."物料组
    poitem-plant = gt_data-werks. "工厂
    poitem-trackingno =  gt_data-bednr."需求跟踪号
    poitem-preq_name = gt_data-afnam."需求者/请求者姓名
    poitem-tax_code = gt_data-mwskz."销售税代码
    poitem-conf_ctrl = bstae."确认控制代码
    APPEND poitem.
    CLEAR poitem.

    poitemx-po_item =  gt_data-ebelp. "采购凭证的项目编号
    poitemx-acctasscat = con ."科目分配类别
    poitemx-short_text = con."短文本
    poitemx-quantity = con."采购订单数量
    poitemx-po_unit = con ."采购订单的计量单位
    poitemx-net_price = con."净价
    poitemx-matl_group = con."物料组
    poitemx-plant = con."工厂
    poitemx-trackingno = con."需求跟踪号
    poitemx-preq_name = con."需求者/请求者姓名
    poitemx-tax_code = con."销售税代码
    poitemx-conf_ctrl = con."确认控制代码
    poitemx-ret_item = con."退货项目
    APPEND poitemx.
    CLEAR poitemx.

    poschedule-po_item =   gt_data-ebelp. "采购凭证的项目编号
    poschedule-sched_line =  gt_data-ebelp. "采购凭证的项目编号
    poschedule-del_datcat_ext = 'D'."交货日期的类别
    poschedule-delivery_date = gt_data-eeind."交货日期
    poschedule-quantity  = gt_data-menge."采购订单数量
    APPEND poschedule.
    CLEAR poschedule.

    poschedulex-po_item =  gt_data-ebelp. "采购凭证的项目编号
    poschedulex-sched_line =  gt_data-ebelp. "采购凭证的项目编号
    poschedulex-del_datcat_ext = con."交货日期的类别
    poschedulex-delivery_date = con."交货日期
    poschedulex-quantity  = con."采购订单数量
    APPEND poschedulex.
    CLEAR poschedulex.

    poaccount-po_item =   gt_data-ebelp. "采购凭证的项目编号
    poaccount-gl_account = gt_data-sakto."总帐科目
    poaccount-costcenter = gt_data-kostl."成本中心
    poaccount-asset_no = gt_data-anln1."主资产号
    poaccount-orderid = gt_data-aufnr."订单号
    poaccount-co_area = 'BELL'."成本控制
    APPEND poaccount.
    CLEAR  poaccount.

    poaccountx-po_item =  gt_data-ebelp. "采购凭证的项目编号
    poaccountx-gl_account = con."总帐科目
    poaccountx-costcenter = con."成本中心
    poaccountx-asset_no = con."主资产号
    poaccountx-orderid = con."订单号
    poaccountx-co_area = con."成本控制
    APPEND poaccountx.
    CLEAR  poaccountx.

    potextitem-po_item =  gt_data-ebelp. "采购凭证的项目编号
    potextitem-text_id = 'F03'."来自采购申请项目详细清单
    potextitem-text_line = gt_data-str1."行项目文本-来自采购申请项目详细清单
    APPEND  potextitem.
    CLEAR  potextitem.

    potextitem-po_item =  gt_data-ebelp. "采购凭证的项目编号
    potextitem-text_id = 'F04'." 规格/型号
    potextitem-text_line = gt_data-str2."行项目文本-规格型号
    APPEND  potextitem.
    CLEAR  potextitem.

    potextitem-po_item =  gt_data-ebelp. "采购凭证的项目编号
    potextitem-text_id = 'F09'."  RFQ Reference
    potextitem-text_line = gt_data-str3."行项目文本- RFQ Reference
    APPEND  potextitem.
    CLEAR  potextitem.


    AT END OF ihrez.
      CALL FUNCTION 'BAPI_PO_CREATE1'
     EXPORTING
       poheader                     =  poheader
       poheaderx                    =  poheaderx
*    POADDRVENDOR                 =
*    TESTRUN                      =
*    MEMORY_UNCOMPLETE            =
*    MEMORY_COMPLETE              =
*    POEXPIMPHEADER               =
*    POEXPIMPHEADERX              =
*    VERSIONS                     =
*    NO_MESSAGING                 =
*    NO_MESSAGE_REQ               =
*    NO_AUTHORITY                 =
*    NO_PRICE_FROM_PO             =
    IMPORTING
      exppurchaseorder             = epo
*    EXPHEADER                    =
*    EXPPOEXPIMPHEADER            =
    TABLES
      return                       =  return
      poitem                       =  poitem
      poitemx                     =  poitemx
*    POADDRDELIVERY               =
      poschedule                   = poschedule
      poschedulex                 = poschedulex
      poaccount                    =  poaccount
*    POACCOUNTPROFITSEGMENT       =
      poaccountx                   = poaccountx
*    POCONDHEADER                 =
*    POCONDHEADERX                =
*    POCOND                       =
*    POCONDX                      =
*    POLIMITS                     =
*    POCONTRACTLIMITS             =
*    POSERVICES                   =
*    POSRVACCESSVALUES            =
*    POSERVICESTEXT               =
*    EXTENSIONIN                  =
*    EXTENSIONOUT                 =
*    POEXPIMPITEM                 =
*    POEXPIMPITEMX                =
*    POTEXTHEADER                 =
      potextitem                   =  potextitem
*    ALLVERSIONS                  =
*    POPARTNER                    =
*    POCOMPONENTS                 =
*    POCOMPONENTSX                =
*    POSHIPPING                   =
*    POSHIPPINGX                  =
*    POSHIPPINGEXP                =
             .
      LOOP AT return WHERE type = 'E' OR type = 'A'.
      ENDLOOP.
      IF sy-subrc = 0.
        CLEAR: lv_message.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        LOOP AT return INTO l_return WHERE type = 'E' .
          CONCATENATE lv_message l_return-message ';'
            INTO lv_message.
        ENDLOOP.
        CONCATENATE gt_data-ihrez lv_message INTO gt_out-text.
        APPEND gt_out.
        CLEAR gt_out.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
        CONCATENATE epo '创建成功' INTO gt_out-text.
        APPEND gt_out.
        CLEAR gt_out.
      ENDIF.
      REFRESH: poitem,poitemx,poschedule,poschedulex,
                     poaccount,poaccountx,potextitem ,
                     poheader,poheaderx,return.
      CLEAR:   poitem,poitemx,poschedule,poschedulex,
                     poaccount,poaccountx,potextitem ,
                      poheader,poheaderx,return.

    ENDAT.
  ENDLOOP.


ENDFORM.                    " CREATE_PO
*&---------------------------------------------------------------------*
*&      Form  WRITE_OUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM write_out .
  LOOP AT gt_out.
    WRITE: / gt_out-text.
  ENDLOOP.
ENDFORM.

免费注册