ME51N 创建采购申请


1业务说明

当寻源后,将寻源结果汇总,并创建采购申请,之后据此创建采购订单。

此文档使用BAPI:BAPI_REQUISITION_CREATE创建采购申请

2前台实现

事务代码:ME51N

3代码实现

3.1调用BAPI

传入相应的行项目数据

导入BAPI

3.2扩展字段

在EBAN表中增加自定义字段

在结构BAPI_TE_REQUISITION_ITEM中定义采购申请项目编号字段,该字段必须有,也必须传值。将结构CI_EBANDB引用进来。

将行项目和申请原因等字段传入结构中,再赋值给BAPI扩展内表GT_EXTENSION,传入BAPI中

创建后,采购申请原因已经传入成功

3.3创建服务类采购申请

创建服务类申请没有物料号,输入相应的数据

首先创建三个变量,用于存储几个编号

在行项目中需要传入软件包编号

输入总账科目和成本中心,并赋值序列号

在服务行中,先输入软件父包

再添加软件子包

在REQUISITION_SRV_ACCASS_VALUES中添加软件子包对应的信息

最后调用BAPI创建成功

软件包的流水号维护在这个对象中,但是在BAPI赋值的时候,直接赋值0000000001即可,如果通过流水号获取赋值进去,反倒不成功。

原因猜测可能是,系统每次都自动获取,不采用付的值,但是如果使用了流水号函数就打乱了系统自己赋值的逻辑。所以在BAPI中赋值0000000001和子包在此上+1,将服务行的父包和子包关联起来即可。

采购申请保存在EBAN表中,服务保存在ESLL表中。

3.4源代码

"-----------------------------@斌将军-----------------------------
DATA:LV_NUMBER    TYPE BAPIMEREQHEADER-PREQ_NO,
     GT_ITEM      TYPE TABLE OF BAPIEBANC,
     GS_ITEM      TYPE BAPIEBANC,
     GT_EXTENSION TYPE TABLE OF BAPIPAREX,
     GS_EXTENSION TYPE BAPIPAREX,
     GT_RETURN    TYPE TABLE OF BAPIRETURN,
     GS_RETURN    TYPE BAPIRETURN.

DATA:LV_MESSAGE TYPE CHAR200,
     LV_CHECK   TYPE CHAR1.

CLEAR:GS_ITEM.
GS_ITEM-PREQ_ITEM = '10'."行项目号
GS_ITEM-DOC_TYPE = 'CKSG'."采购申请凭证类型
GS_ITEM-PUR_GROUP = 'C10'."采购组
GS_ITEM-MATERIAL = '405481'."物料号
GS_ITEM-PLANT     = '2000'.   "工厂
GS_ITEM-STORE_LOC     = '2007'.   "库存地点
GS_ITEM-QUANTITY  = '1'."数量
GS_ITEM-PREQ_DATE = SY-DATUM."需求日期
GS_ITEM-TRACKINGNO = ''."需求跟踪号
GS_ITEM-FIXED_VEND = ''."固定供应商
GS_ITEM-PREQ_NAME = 'SRM'."申请者
GS_ITEM-VAL_TYPE = 'CR'."评估类型
GS_ITEM-DELIV_DATE = '20200129'."交货日期
GS_ITEM-UNIT = 'PC'."计量单位
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT  = GS_ITEM-MATERIAL"物料号
  IMPORTING
    OUTPUT = GS_ITEM-MATERIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT  = GS_ITEM-FIXED_VEND"固定供应商
  IMPORTING
    OUTPUT = GS_ITEM-FIXED_VEND.

CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
  EXPORTING
    INPUT    = GS_ITEM-UNIT"计量单位
    LANGUAGE = SY-LANGU
  IMPORTING
    OUTPUT   = GS_ITEM-UNIT.
APPEND GS_ITEM TO GT_ITEM.

CLEAR:GS_EXTENSION.
GS_EXTENSION-STRUCTURE = 'BAPI_TE_REQUISITION_ITEM'.
GS_EXTENSION-VALUEPART1 = GS_ITEM-PREQ_ITEM && GS_ITEM-PREQ_NAME."申请原因
APPEND GS_EXTENSION TO GT_EXTENSION.

CLEAR:LV_NUMBER.
CALL FUNCTION 'BAPI_REQUISITION_CREATE'
  EXPORTING
*   SKIP_ITEMS_WITH_ERROR                = gt_item
    AUTOMATIC_SOURCE  = ' '
  IMPORTING
    NUMBER            = LV_NUMBER
  TABLES
    REQUISITION_ITEMS = GT_ITEM
*   REQUISITION_ACCOUNT_ASSIGNMENT       =
*   REQUISITION_ITEM_TEXT                =
*   REQUISITION_LIMITS                   =
*   REQUISITION_CONTRACT_LIMITS          =
*   REQUISITION_SERVICES                 =
*   REQUISITION_SRV_ACCASS_VALUES        =
    RETURN            = GT_RETURN
*   REQUISITION_SERVICES_TEXT            =
*   REQUISITION_ADDRDELIVERY             =
    EXTENSIONIN       = GT_EXTENSION.

LV_CHECK = 'S'.
LOOP AT GT_RETURN INTO GS_RETURN WHERE TYPE = 'A' OR TYPE = 'E'.
  LV_MESSAGE = LV_MESSAGE && GS_RETURN-MESSAGE.
  LV_CHECK = 'E'.
ENDLOOP.

IF LV_CHECK = 'S'.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      WAIT = 'X'.
  WRITE:LV_NUMBER.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF. 


------------------服务类采购申请源代码---------------------
*&---------------------------------------------------------------------*
*& Report  ZLCCTEST019
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZLCCTEST019.

DATA:GT_ITEM           TYPE TABLE OF BAPIEBANC,
     GS_ITEM           TYPE BAPIEBANC,
     GT_SER            TYPE TABLE OF BAPIESLLC,
     GS_SER            TYPE BAPIESLLC,
     GT_SER_VALUES     TYPE TABLE OF BAPIESKLC,
     GS_SER_VALUES     TYPE BAPIESKLC,
     GT_WBS            TYPE TABLE OF BAPIEBKN,
     GS_WBS            TYPE  BAPIEBKN,
     GT_RETURN         TYPE TABLE OF BAPIRETURN,
     L_NO1             TYPE BAPIMEREQHEADER-PREQ_NO,
     LV_MESSAGE        TYPE CHAR200,
     LV_PCKG_NO        TYPE BAPIEBANC-PCKG_NO VALUE 0000000001, "软件包编号
     LV_ACCASSERIAL_NO TYPE BAPIEBKN-SERIAL_NO VALUE 01, "序列号
     LV_LINE_NO        TYPE BAPIESLLC-LINE_NO VALUE 1. "内部行编号


GS_ITEM-PREQ_ITEM = '00010'.
GS_ITEM-DOC_TYPE = 'CKSR'.
GS_ITEM-PLANT = '2000'.
GS_ITEM-QUANTITY = '1'."数量
GS_ITEM-PREQ_NAME = '123'."申请者
GS_ITEM-UNIT = 'LE'."计量单位
GS_ITEM-ACCTASSCAT = 'W'."科目分配类别
GS_ITEM-ITEM_CAT_EXT = 'D'."科目分配类别
GS_ITEM-SHORT_TEXT = '123'."短文本
GS_ITEM-MAT_GRP = '&DB001'."物料组
GS_ITEM-PREQ_DATE = '20210307'."需求日期
GS_ITEM-DELIV_DATE = '20210307'."需求日期
GS_ITEM-DELIV_DATE = '20210307'."需求日期
GS_ITEM-PCKG_NO = LV_PCKG_NO."软件包编号
GS_ITEM-GR_IND = 'X'.
GS_ITEM-GR_NON_VAL = 'X'.
GS_ITEM-IR_IND = 'X'.
APPEND GS_ITEM  TO GT_ITEM.

GS_WBS-PREQ_ITEM = '00010'.
GS_WBS-SERIAL_NO = LV_ACCASSERIAL_NO.
GS_WBS-G_L_ACCT = '6601039900'.
GS_WBS-COST_CTR = '0BE000'.
APPEND GS_WBS  TO GT_WBS.

GS_SER-PCKG_NO = LV_PCKG_NO."软件包编号
GS_SER-LINE_NO = LV_LINE_NO."内部行编号
GS_SER-OUTL_IND = 'X'."标识符:概要行
GS_SER-SUBPCKG_NO = LV_PCKG_NO + 1."分包编号
GS_SER-FROM_LINE = 1."下限
APPEND GS_SER TO GT_SER.

CLEAR:GS_SER.
GS_SER-PCKG_NO = LV_PCKG_NO + 1."软件包编号
GS_SER-LINE_NO = LV_LINE_NO + 1."内部行编号
GS_SER-EXT_LINE = 0000000010."行号
GS_SER-QUANTITY = 1."数量
GS_SER-BASE_UOM = 'LE'."计量单位
GS_SER-PRICE_UNIT = 1."价格单位
GS_SER-NET_VALUE = NETPR * QUANTITY."净值
GS_SER-SHORT_TEXT = 123."短文本
GS_SER-MATL_GROUP = '&DB001'."物料组
GS_SER-GR_PRICE = 10."总价
APPEND GS_SER TO GT_SER.

GS_SER_VALUES-PCKG_NO = LV_PCKG_NO + 1."软件包编号
GS_SER_VALUES-LINE_NO = LV_LINE_NO + 1."内部行编号
GS_SER_VALUES-SERNO_LINE = 01."服务行
GS_SER_VALUES-SERIAL_NO = GS_SER_VALUES-SERNO_LINE."帐户分配的顺序编号
GS_SER_VALUES-PERCENTAGE = 100."帐户分配值分配百分比
GS_SER_VALUES-NET_VALUE = NETPR * QUANTITY."项目净值
GS_SER_VALUES-QUANTITY = 1."数量带标记
APPEND GS_SER_VALUES TO GT_SER_VALUES.

CALL FUNCTION 'BAPI_REQUISITION_CREATE'
  EXPORTING
*   SKIP_ITEMS_WITH_ERROR          = gt_item
    AUTOMATIC_SOURCE               = ' '
  IMPORTING
    NUMBER                         = L_NO1
  TABLES
    REQUISITION_ITEMS              = GT_ITEM
    REQUISITION_ACCOUNT_ASSIGNMENT = GT_WBS
*   REQUISITION_ITEM_TEXT          =
*   REQUISITION_LIMITS             =
*   REQUISITION_CONTRACT_LIMITS    =
    REQUISITION_SERVICES           = GT_SER
    REQUISITION_SRV_ACCASS_VALUES  = GT_SER_VALUES
    RETURN                         = GT_RETURN
*   REQUISITION_SERVICES_TEXT      =
*   REQUISITION_ADDRDELIVERY       =
*   EXTENSIONIN                    = GT_EXTENSION1
  .

CLEAR:LV_MESSAGE.
LOOP AT GT_RETURN INTO DATA(GL_RETURN) WHERE TYPE = 'E' OR TYPE = 'A'.
  CONCATENATE LV_MESSAGE '/' GL_RETURN-MESSAGE INTO LV_MESSAGE.
  CLEAR GL_RETURN.
ENDLOOP.
IF LV_MESSAGE IS INITIAL AND L_NO1 IS NOT INITIAL.
*   提交事务
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      WAIT = 'X'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
ENDIF.

WRITE :L_NO1.
"-----------------------------@斌将军-----------------------------