sap批量创建盘点凭证以及盘点凭证过账

业务场景:wms传入单号到sapsap通过bapi生成盘点凭证并过账(实际业务是由财务过账,这里只说明技术)

前台一步输入物料号和盘点数量生成盘点凭证,在进行过账,(2步),sap是先生成盘点凭证,再输入数据最后差异过账(3步)

注意:bapi创建盘点凭证 :BAPI_MATPHYSINV_CREATE_MULT

                             盘点数量:BAPI_MATPHYSINV_COUNT(一定要记得传入item行号)

                             差异过账:BAPI_MATPHYSINV_POSTDIFF(传入行号item

*&---------------------------------------------------------------------*

*&      Form  CREATE_WSCODE

*&---------------------------------------------------------------------*

*        在这里创键盘点凭证。

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM FRM_CREATE_WSCODE  TABLES  GT_ALL STRUCTURE GS_ALL

                            T_RETURN STRUCTURE ZSMM0005

                             CHANGING E_RTYPE E_RTMSG.

  LOOP AT GT_ALL INTO GS_ALL.

    GS_DATA2 = GS_ALL.

    APPEND GS_DATA2 TO GT_DATA2.

  ENDLOOP.

  DELETE ADJACENT DUPLICATES FROM GT_DATA2 COMPARING WMSORD."删除重复项

*IF SY-SUBRC = 0.

  LOOP AT GT_DATA2 INTO GS_DATA2.

    CLEAR: GS_HEAD_MUTT,

           GS_ITEMS_MUTT,

           GS_ITEMS_COUNT,

           GS_ITEMS_POSTDIFF,

           GS_RETURN.

    CLEAR: GT_ITEMS_MUTT[],

           GT_ITEMS_COUNT[],

           GT_ITEMS_POSTDIFF[].

    GV_INDEX2 = 0.

    LOOP AT GT_ALL INTO GS_ALL WHERE WMSORD = GS_DATA2-WMSORD .

      GV_INDEX2 = GV_INDEX2 + 1.

*          -----------------------------

* 把同样的订单号对应的数据传入到一个表中,然后统一处理一个仓库中的所有物料号生成一个盘点凭证

* 处理wms传进来的盘点单号

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = GS_ALL-WMSORD

        IMPORTING

          OUTPUT = GS_ALL-WMSORD.

      GV_INDOC = GS_ALL-WMSORD."wms盘点单号

*根据物料号获取对应单位

      READ TABLE GT_ALL INTO GS_ALL WITH  KEY MATNR = GS_ALL-MATNR.

      SELECT MEINS

        INTO TABLE GT_DETAIL

        FROM MARA

        WHERE MATNR = GS_ALL-MATNR.

      CLEAR GS_DETAIL.

      LOOP AT GT_DETAIL INTO GS_DETAIL.

“内外码转换”

        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'

          EXPORTING

            INPUT          = GS_DETAIL-MEINS

          IMPORTING

            OUTPUT         = GS_DETAIL-MEINS

          EXCEPTIONS

            UNIT_NOT_FOUND = 1

            OTHERS         = 2.

 

      ENDLOOP.

 

*      获取年度

      GV_YEAR    = GS_ALL-BLDAT+0(4).

*      head

      GS_HEAD_MUTT-PLANT            = GS_ALL-WERKS. "工厂

      GS_HEAD_MUTT-STGE_LOC         = GS_ALL-LGORT. "库存地点

      GS_HEAD_MUTT-SPEC_STOCK       = GS_ALL-SOBKZ. "特殊库存标识

      GS_HEAD_MUTT-DOC_DATE         = GS_ALL-BLDAT.    "系统时间"T_DATA-BLDAT. "凭证中的凭证日期

      GS_HEAD_MUTT-PLAN_DATE        = GS_ALL-ZLDAT. "存货盘点的计划日期

      GS_HEAD_MUTT-PHYS_INV_REF     = GS_ALL-WMSORD.     "wms盘点单号

 

*       items

      CLEAR GS_ITEMS_MUTT.

      GS_ITEMS_MUTT-MATERIAL_LONG   = GS_ALL-MATNR. "物料编号(18)

      GS_ITEMS_MUTT-BATCH           = GS_ALL-CHARG. "批号

      APPEND  GS_ITEMS_MUTT TO GT_ITEMS_MUTT.

**      count

      CLEAR GS_ITEMS_COUNT.

      GS_ITEMS_COUNT-ENTRY_QNT       = GS_ALL-ERFMG. "以录入项单位表示的数量

      GS_ITEMS_COUNT-ENTRY_UOM       = GS_DETAIL-MEINS. "单位

      GS_ITEMS_COUNT-MATERIAL_LONG   = GS_ALL-MATNR. "物料编号(18)

      GS_ITEMS_COUNT-BATCH           = GS_ALL-CHARG. "批号

      GS_ITEMS_COUNT-ITEM            = GV_INDEX2.

      APPEND  GS_ITEMS_COUNT TO GT_ITEMS_COUNT.

*      差异过账

      CLEAR GS_ITEMS_POSTDIFF.

      GS_ITEMS_POSTDIFF-MATERIAL_LONG   = GS_ALL-MATNR. "物料编号(18)

      GS_ITEMS_POSTDIFF-BATCH           = GS_ALL-CHARG. "批号

      GS_ITEMS_POSTDIFF-ITEM            = GV_INDEX2.

      APPEND   GS_ITEMS_POSTDIFF TO GT_ITEMS_POSTDIFF.

**---------------------更新序列号--------------------------------------------------*

      MODIFY GT_ALL  INDEX GV_INDEX2.

      CLEAR:GS_ALL.

    ENDLOOP.

**----------------------------------创建盘点凭证------------------------*

    CALL FUNCTION 'BAPI_MATPHYSINV_CREATE_MULT'

      EXPORTING

        HEAD   = GS_HEAD_MUTT

      TABLES

        ITEMS  = GT_ITEMS_MUTT

        RETURN = GT_RETURN.

    CLEAR GS_RETURN.

    IF GT_RETURN IS  NOT INITIAL.

      READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'S' ID = 'M7' NUMBER = '710'.

      IF SY-SUBRC <> 0.

 

        READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'E' .

        T_RETURN-MTYPE = 'E'.

        T_RETURN-MBLNR = GS_RETURN-MESSAGE_V3 .

        T_RETURN-MTMSG = '该盘点凭证还未过账,请先过账或者删除,删除时需传入LSTATX,同时输入新的数据可创建新的盘点凭证'.

        APPEND T_RETURN.

      ELSE.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            WAIT = ABAP_TRUE.

        GV_OUTDOC      = GS_RETURN-MESSAGE_V1."获取bapi产生的盘点凭证

"内外码转换

        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

          EXPORTING

            INPUT  = GV_OUTDOC

          IMPORTING

            OUTPUT = GV_OUTDOC.

        T_RETURN-MTYPE  = 'S'.

        T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.

        T_RETURN-MBLNR  = GV_YEAR.

        T_RETURN-MTMSG  = GS_RETURN-MESSAGE.

        E_RTYPE = 'S'.

        E_RTMSG = GS_RETURN-MESSAGE.

        APPEND T_RETURN.

        CLEAR GS_RETURN.

      ENDIF.

    ELSE.

      E_RTYPE = 'E'.

      E_RTMSG = TEXT-002.

      RETURN.

    ENDIF.

****count-------

    IF SY-SUBRC = 0.

      CALL FUNCTION 'BAPI_MATPHYSINV_COUNT'

        EXPORTING

          PHYSINVENTORY = GV_OUTDOC

          FISCALYEAR    = GV_YEAR

        TABLES

          ITEMS         = GT_ITEMS_COUNT

          RETURN        = GT_RETURN.

      CLEAR GS_RETURN.

      IF GT_RETURN IS  NOT INITIAL.

        READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'S' ID = 'M7' NUMBER = '713'.

        IF SY-SUBRC = 0.

 

          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              WAIT = ABAP_TRUE.

          T_RETURN-MTYPE  = 'S'.

          T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.

          T_RETURN-MBLNR  = GV_YEAR.

          T_RETURN-MTMSG  = GS_RETURN-MESSAGE.

          APPEND T_RETURN.

          CLEAR GS_RETURN.

        ENDIF.

      ENDIF.

    ENDIF.

****----------------------差异过账-------------------------------------*

*    IF SY-SUBRC = 0.

*      CALL FUNCTION 'BAPI_MATPHYSINV_POSTDIFF'

*        EXPORTING

*          PHYSINVENTORY = GV_OUTDOC

*          FISCALYEAR    = GV_YEAR

*        TABLES

*          ITEMS         = GT_ITEMS_POSTDIFF

*          RETURN        = GT_RETURN.

*      IF GT_RETURN IS  NOT INITIAL.

*        LOOP AT GT_RETURN INTO GS_RETURN WHERE TYPE = 'S' AND ID ='M7' AND NUMBER = '716' OR NUMBER = '715'.

*          IF SY-SUBRC <> 0.

*            T_RETURN-MTYPE = 'E'.

*            T_RETURN-MTMSG = GS_RETURN-MESSAGE.

*            E_RTYPE = 'E'.

*            E_RTMSG = GS_RETURN-MESSAGE.

*          ELSE.

*            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

*              EXPORTING

*                WAIT = ABAP_TRUE.

*            T_RETURN-MTYPE  = 'S'.

*            T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.

*            T_RETURN-MBLNR  = GS_RETURN-MESSAGE_V2.

*            T_RETURN-MTMSG  = GS_RETURN-MESSAGE.

*            E_RTYPE = 'S'.

*            E_RTMSG = GS_RETURN-MESSAGE.

*            APPEND T_RETURN.

*          ENDIF.

*        ENDLOOP.

**        READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'S' ID = 'M7' NUMBER = '716'.

**        IF SY-SUBRC <> 0.

**          T_RETURN-MTYPE = 'E'.

**          T_RETURN-MTMSG = GS_RETURN-MESSAGE.

**          E_RTYPE = 'E'.

**          E_RTMSG = GS_RETURN-MESSAGE.

**        ELSE.

**          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

**            EXPORTING

**              WAIT = ABAP_TRUE.

**          T_RETURN-MTYPE  = 'S'.

**          T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.

**          T_RETURN-MBLNR  = GS_RETURN-MESSAGE_V2.

**          T_RETURN-MTMSG  = GS_RETURN-MESSAGE.

**          E_RTYPE = 'S'.

**          E_RTMSG = GS_RETURN-MESSAGE.

**          APPEND T_RETURN.

**        ENDIF.

*      ENDIF.

*    ENDIF.

    CLEAR:GS_ALL,GS_DATA2.

  ENDLOOP.

  CLEAR:GT_ALL,GS_ALL,GT_DATA2,GS_DATA2.

ENDFORM.

                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/xiaoxiaoanhong" target="_blank">
                    <img src="https://profile.csdnimg.cn/2/C/B/3_xiaoxiaoanhong" class="avatar_pic" username="xiaoxiaoanhong">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/5.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/xiaoxiaoanhong" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">TLucky</a></span>
                                            </div>
                    <div class="text"><span>发布了15 篇原创文章</span> · <span>获赞 3</span> · <span>访问量 1万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=xiaoxiaoanhong" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
发布了32 篇原创文章 · 获赞 32 · 访问量 10万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览