SAP MI01、MI04、MI07、MI10 批量盘点凭证创建+盘点数量+差异过账

 本人诚心接ABAP远程开发任务,价格公道,有需要的联系我,欢迎个人,甲方爸爸,乙方私信联系。

这次的用户是一家启用了序列号管理的客户。

客户比较懒。所以在盘点的时候,只用MI10去一步做完盘点。

查阅资料发现,MI10没有标准的bapi 去调用,

但是MI10其实就是MI01(创建盘点凭证)+MI04(盘点数量)+MI07(差异过账)的一个集合事务代码。

那么我们的批导程序就可以分步骤调用三个bapi

分别是:MI01-->BAPI_MATPHYSINV_CREATE

              MI04-->BAPI_MATPHYSINV_COUNT

              MI07-->BAPI_MATPHYSINV_POSTDIFF

其中遇到一个问题就是客户是车企,启用的序列号管理,所以 MI04的时候输入完数量之后,要根据数量,输入相应数量的不同的序列号

下面简单列一下代码。

  "MI01 对应参数
  DATA:LS_HEAD   TYPE BAPI_PHYSINV_CREATE_HEAD.
  DATA:LT_ITEMS  TYPE STANDARD TABLE OF BAPI_PHYSINV_CREATE_ITEMS.
  DATA:LS_ITEMS  TYPE BAPI_PHYSINV_CREATE_ITEMS.
  DATA:LT_RETURN TYPE STANDARD TABLE OF BAPIRET2 .
  DATA:LS_RETURN TYPE BAPIRET2 .

  "MI04 对应参数
  DATA:LT_COUNT  TYPE STANDARD TABLE OF  BAPI_PHYSINV_COUNT_ITEMS.
  DATA:LS_COUNT  TYPE BAPI_PHYSINV_COUNT_ITEMS.
  DATA:LT_SERNR  TYPE STANDARD TABLE OF BAPI_PHYSINV_SERIALNUMBERS.
  DATA:LS_SERNR  TYPE BAPI_PHYSINV_SERIALNUMBERS.
  DATA:LV_LINES  TYPE DZEILE.

  "MI07 对应参数
  DATA:LT_POST   TYPE STANDARD TABLE OF BAPI_PHYSINV_POST_ITEMS.
  DATA:LS_POST   TYPE BAPI_PHYSINV_POST_ITEMS.

"创建盘点凭证BAPI  参数封装
*-------------------------------------------------------------
    CLEAR: LS_HEAD,LS_ITEMS,LT_ITEMS,LT_RETURN,LS_RETURN,LV_LINES,LS_COUNT,LT_COUNT,LS_SERNR,LT_SERNR
    ,LS_POST, LT_POST.
    LS_HEAD-PLANT      = P_WERKS.     "工厂
    LS_HEAD-STGE_LOC   = P_LGORT.     "库存地点
    LS_HEAD-DOC_DATE   = P_BUDAT.     "凭证中的凭证日期
    LS_HEAD-PLAN_DATE  = P_COUNT.     "库存盘点的计划日期

   LT_TEMP = GT_DATA.
"首先根据物料号去重去创建盘点凭证
   SORT LT_TEMP BY MATNR.
   DELETE ADJACENT DUPLICATES FROM LT_TEMP COMPARING MATNR.

   LOOP AT  LT_TEMP INTO LS_TEMP.

"创建盘点凭证BAPI  参数封装
*-------------------------------------------------------------
     LS_ITEMS-MATERIAL    = LS_TEMP-MATNR.       "物料号
     LS_ITEMS-STOCK_TYPE  = LS_TEMP-BSTAR.       "库存类型
     APPEND LS_ITEMS TO LT_ITEMS.
     CLEAR  LS_ITEMS.
"MI04的时候传入序列号 盘点数量
"凭证数量盘点BAPI  参数封装
*-------------------------------------------------------------
      LV_LINES            = LV_LINES + 1.
      LS_COUNT-ITEM       = LV_LINES.       "行号
      LS_COUNT-MATERIAL   = LS_TEMP-MATNR.       "物料编码
      LS_COUNT-ENTRY_UOM  = LS_TEMP-MEINS.       "计量单位
      LS_COUNT-ZERO_COUNT = LS_TEMP-XNULL.

     "差异过账BAPI 参数封装
*-------------------------------------------------------------
      LS_POST-ITEM       = LV_LINES.       "行号
      LS_POST-MATERIAL   = LS_TEMP-MATNR.       "物料编码
      APPEND LS_POST TO LT_POST.
      CLEAR: LS_POST.

   LOOP AT GT_DATA INTO GS_DATA WHERE MATNR = LS_TEMP-MATNR.
    "这里是累计同一个物料号有多少数量
    LS_COUNT-ENTRY_QNT = GS_DATA-MENGE + LS_COUNT-ENTRY_QNT. "录入系统实盘数
    "这里是将序列号一一对应传入
    LS_SERNR-ITEM       = LV_LINES.
    LS_SERNR-SERIALNO   = GS_DATA-SERNR.
    APPEND LS_SERNR TO LT_SERNR.
    CLEAR  LS_SERNR.

   ENDLOOP.

      APPEND LS_COUNT TO LT_COUNT.
      CLEAR: LS_COUNT.

   ENDLOOP.

      CALL FUNCTION 'BAPI_MATPHYSINV_CREATE'
        EXPORTING
          HEAD            = LS_HEAD
        TABLES
          ITEMS           = LT_ITEMS
          RETURN          = LT_RETURN.
     READ TABLE LT_RETURN INTO LS_RETURN WITH KEY TYPE = 'E'.
     IF SY-SUBRC = 0 .
       CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
        LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE = 'E'.
            CONCATENATE LV_MESSAGE LS_RETURN-MESSAGE ',' INTO LV_MESSAGE.
        ENDLOOP.
       LOOP AT GT_DATA INTO GS_DATA .

        GS_DATA-ICONS   = ICON_RED_LIGHT.
        GS_DATA-MESSAGE = LV_MESSAGE.
        MODIFY GT_DATA FROM GS_DATA.
       ENDLOOP.
    ELSE.
    "提交并 盘点凭证数量
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT = 'X'.


      READ TABLE LT_RETURN INTO LS_RETURN INDEX 1.
      IF SY-SUBRC = 0.
        LV_IBLNR = LS_RETURN-MESSAGE_V1.
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          INPUT              =  LV_IBLNR
        IMPORTING
         OUTPUT              =  LV_IBLNR.
      ENDIF.

      LV_MESSAGE = 'Counting voucher created successfully!'.
*----------------------------------------------------------盘点凭证数量
    CLEAR:LT_RETURN.

   LV_GJAHR = P_COUNT+0(4).

    CALL FUNCTION 'BAPI_MATPHYSINV_COUNT'
      EXPORTING
        PHYSINVENTORY = LV_IBLNR          "盘点凭证号
        FISCALYEAR    = LV_GJAHR
        COUNT_DATE    = P_COUNT
      TABLES
        ITEMS         = LT_COUNT
        RETURN        = LT_RETURN
        SERIALNUMBERS = LT_SERNR .
     READ TABLE LT_RETURN INTO LS_RETURN WITH KEY TYPE = 'E'.
     IF SY-SUBRC = 0 .
       CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
        LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE = 'E'.
            CONCATENATE LV_MESSAGE LS_RETURN-MESSAGE ',' INTO LV_MESSAGE.
        ENDLOOP.
       LOOP AT GT_DATA INTO GS_DATA .
        GS_DATA-IBLNR   = LV_IBLNR.
        GS_DATA-ICONS   = ICON_RED_LIGHT.
        GS_DATA-MESSAGE = LV_MESSAGE.
        MODIFY GT_DATA FROM GS_DATA.
       ENDLOOP.
    ELSE.
    "提交并 盘点凭证数量
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT = 'X'.

     CLEAR:LT_RETURN,LS_RETURN.
     CONCATENATE LV_MESSAGE  'Quantity counting succeeded!' INTO LV_MESSAGE.

     CALL FUNCTION 'BAPI_MATPHYSINV_POSTDIFF'
       EXPORTING
         PHYSINVENTORY         = LV_IBLNR
         FISCALYEAR            = LV_GJAHR
*        PSTNG_DATE            =
*        THRESHOLD_VALUE       =
       TABLES
         ITEMS                 = LT_POST
         RETURN                = LT_RETURN .
          READ TABLE LT_RETURN INTO LS_RETURN WITH KEY TYPE = 'E'.
     IF SY-SUBRC = 0 .
       CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
        LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE = 'E'.
            CONCATENATE LV_MESSAGE LS_RETURN-MESSAGE ',' INTO LV_MESSAGE.
        ENDLOOP.
      LOOP AT GT_DATA INTO GS_DATA .

        GS_DATA-ICONS   = ICON_RED_LIGHT.
        GS_DATA-MESSAGE = LV_MESSAGE.
        MODIFY GT_DATA FROM GS_DATA.
       ENDLOOP.
    ELSE.
   "差异过账成功
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT = 'X'.
     CONCATENATE LV_MESSAGE  'Variance posting succeeded!' INTO LV_MESSAGE.
     READ TABLE LT_RETURN INTO LS_RETURN INDEX 1.
      IF SY-SUBRC = 0.
        LV_BELNR = LS_RETURN-MESSAGE_V2.
      ENDIF.
      LOOP AT GT_DATA INTO GS_DATA .
        GS_DATA-IBLNR   = LV_IBLNR.
        GS_DATA-BELNR   = LV_BELNR.
        GS_DATA-ICONS   = ICON_GREEN_LIGHT.
        GS_DATA-MESSAGE = LV_MESSAGE.
        MODIFY GT_DATA FROM GS_DATA.
       ENDLOOP.
    ENDIF.

   ENDIF.

 ENDIF.

你可能感兴趣的:(MM,ABAP,sap)