关闭

BAPI_PO_CREATE1 批量创建采购订单改进版

标签: BAPI_PO_CREATE1PO
522人阅读 评论(0) 收藏 举报
分类:

突然想到看到采购项目编号(ebelp)和自己定义的flag数据是一样的,既然这样那为何还要用flag呢,直接用采购项目编号作为判断不就行了么。如果后面几行ebelp为递增的话,那么这几行就是在一个PO里的。还有个改动就是将一些步骤抽到一些函数里。


REPORT ZYHYBAPI_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,"科目分配类别
          matnr 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,
      netpr TYPE bprei,
      bstae TYPE bstae,
      matnr TYPE matnr,
      waers TYPE waers,
      frontLine like gt_data.

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 .
  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
*&---------------------------------------------------------------------*

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
*&---------------------------------------------------------------------*

FORM create_po .
  data: totalline TYPE i .
  data: i TYPE i VALUE 1.
  DESCRIBE TABLE gt_data LINES  totalline.
  LOOP AT gt_data.
    CLEAR waers.
    waers = gt_data-waers.

*    不如读到下一行时在判断前面的是否create

   PERFORM SETOTHER.
"为了一个po可以采购多个材料,如果下面几个为一个po则行项目编号为1 2 3  新的po的话就为1
    IF i <= totalline . "
        CLEAR frontLine.
        READ TABLE  gt_data[] INTO frontLine INDEX ( i + 1 ). "取下一行的行项目编号判断一个PO是否此采购完.

       IF frontLine-MENGE = 1 or frontLine-MENGE = ''."下一个数据行的flag为1的话就是新的一个PO 或者为空即当前行为最后一行
           PERFORM SETHEADER.
           PERFORM CREATEPO.
        ENDIF.
    i = i + 1.
      ENDIF.

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


                   " SETPOHEADER

FORM SETOTHER.
       CLEAR netpr.
    netpr = gt_data-netpr.

    CLEAR bstae.
    bstae = gt_data-bstae.

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

    CLEAR matnr.
    matnr = gt_data-matnr.

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

    poitem-po_item =  gt_data-ebelp. "采购凭证的项目编号
    poitem-acctasscat = gt_data-knttp."科目分配类别
    poitem-material = matnr."商品代码
*    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."确认控制代码
*    poitem-GR_BASEDIV = '' . "基于收货的发票校验 默认为选中 这样设置就是不选中
    APPEND poitem.
    CLEAR poitem.

    poitemx-po_item =  gt_data-ebelp. "采购凭证的项目编号
    poitemx-acctasscat = con ."科目分配类别
    poitemx-material = 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."退货项目
*    poitemx-GR_BASEDIV = 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.
ENDFORM.

FORM SETHEADER.
   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 = 1.
            poheader-langu         =  sy-langu.
            poheader-currency = waers."货币
            APPEND poheader.

            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."货币
            APPEND poheaderx.
  ENDFORM.




*&---------------------------------------------------------------------*
*&      Form  CREATEPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CREATEPO .
  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.
ENDFORM.   






0
0
查看评论

BAPI_PO_CREATE1 净价net_price 无效

今天测试发现用BAPI_PO_CREATE1 创建采购订单,虽然能成功创建采购订单,但是行项目的净价总是和自己导入的数据不对。然后在前台手工创建时,发现会自动带出一个净价,奇怪从哪来。原来是因为我采购信息记录没有维护净价,它就会采用上一个采购订单的净价。     查...
  • yang362046076
  • yang362046076
  • 2015-12-09 16:43
  • 2204

BAPI_PO_CREATE1 PO净价从信息记录获取问题解决

about bapi_po_create1 using BAPI_PO_CREATE1 to create POs meanwhile passing the netprice instead&#...
  • fiberoptics
  • fiberoptics
  • 2013-07-13 09:27
  • 2569

BAPI_PO_CREATE1 批量创建采购订单

查看BAPI_PO_CREATE1里面需要导入什么参数,读取文件填充进去即可。注意导入的数据文件和程序里定义的结构里的字段顺序相对应,每个字段用tab分隔,如果该字段为空的话,文件里也要tab一下。 *&--------------------------------------------...
  • yang362046076
  • yang362046076
  • 2015-11-16 15:45
  • 1559

采购订单批量创建 BAPI_PO_CREATE1

采购订单批量创建 BAPI_PO_CREATE1
  • champaignwolf
  • champaignwolf
  • 2015-03-19 16:26
  • 3850

BAPI_PO_CREATE1 批量创建采购订单(可一个PO采购多个材料)

改进http://blog.csdn.net/yang362046076/article/details/49866733 这里的程序,使其能在一个PO里采购多个原材料。需要定义文件格式,来说明输入的数据是否为同一PO。 代码修改的不少,但主要东西没变,poschedule-sched_lin...
  • yang362046076
  • yang362046076
  • 2015-11-17 16:46
  • 904

BAPI_PO_CREATE1创建采购订单

data:lt_poheader like table of bapimepoheader with header line,      lt_poheaderx li...
  • CS0123456789
  • CS0123456789
  • 2011-11-28 10:59
  • 2040

BAPI调用 - BAPI_PO_CREATE1手动指定行项目编号方法介绍

用BAPI: BAPI_PO_CREATE1创建采购订单,对于行项目总是自动编号的问题,真是让人头疼,参数POITEM中的PO_ITEM虽然可以随意指定,比如10,20,30等,但是这个BAPI会对行项目进行自动编号。而控制自动编号的参数居然在import参数中,由①POHEADER中的ITEM_I...
  • wanglei880526
  • wanglei880526
  • 2013-11-26 18:02
  • 2159

使用BAPI_REQUISITION_CREATE批量创建PR(采购申请)

原文地址: http://blog.chinaunix.net/u1/33519/showart.php?id=1674662 *&---------------------------------------------------------------------* *&...
  • wanglei880526
  • wanglei880526
  • 2013-05-27 15:28
  • 2039

释疑の采购订单的批量修改

现在有一个需求,我们需要冻结一批采购订单,但是由于批量冻结的后果无法预知,我们打算用一个折中的办法。 利用采购订单行项目中的“最近收货日期”字段,下面我们先看一下它的解释。 最近可能的收货 这是收货(GR)的最近的可接受日期。 如果在此日期后接收货物,将收到一个警告消息或一个错误消息...
  • zhongguomao
  • zhongguomao
  • 2017-03-08 09:49
  • 758

根据BAPI_PO_CREATE1创建采购订单

REPORT  zlm_test_026. DATA:BEGIN OF gt_data OCCURS 0,           bsart TYPE string,"采购凭证类型       ...
  • senlinmu110
  • senlinmu110
  • 2014-01-22 09:23
  • 725
    个人资料
    • 访问:169972次
    • 积分:2376
    • 等级:
    • 排名:第18497名
    • 原创:74篇
    • 转载:31篇
    • 译文:4篇
    • 评论:14条
    文章分类
    最新评论