• BOM反查 原材料反查最上层的成本料号


    需求描述:

     分类    CLASS    "MBEW-BKLAS = 3010/3020 原材料
                                  = 3031 低值易耗品
                                  = 7010 半成品
                         = 7020 库存商品 "
    物料号    MATNR  

    1,半成品:从BOM里面取到对应的成品料号,如果对应多个成品则取当期入库最多的成品,如果有多层,则取最上层的成本料号;BOM可用 CS_WHERE_USED_MAT 获取。
    2,原材料:从BOM里面取对应的成品料号,如果是对应多个成品则取当期入库最多的成品。如果有多层,则取最上层的成本料号;
    3,低值易耗品:无
    4,库存商品:对应成品料号就是本物料号。

    取到的数据存到后台表 ZFIT_TZ07

     

    *----------------------------------------------------------------------*
    * Program ID/Name : ZFITZ0005
    * Author's name   : SAPWB0011
    * Program title   : 为存货可变现净值报表取最上层成品料号
    * Project Name    : S4/HANA
    * Version         :
    * Date written    : 2021.06.17
    * Last update     :
    * Function Spec ID: LSTZ_FICO_DEV_003
    *----------------------------------------------------------------------*
    REPORT ZFITZ0005 MESSAGE-ID ZTZ_001.
    
    TABLES: MARC, MAST,STKO.
    
    DATA: GT_MAT LIKE TABLE OF ZFIT_TZ07,
          GS_MAT LIKE ZFIT_TZ07.
    
    DATA:GV_START TYPE D,
         GV_END   TYPE D.
    
    
    SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
    SELECT-OPTIONS: S_WERKS FOR MARC-WERKS NO INTERVALS NO-EXTENSION OBLIGATORY.
    PARAMETERS:P_CXNY  TYPE S031-SPMON     OBLIGATORY. "查询年月
    SELECT-OPTIONS: S_MATNR FOR MARC-MATNR.
    PARAMETERS: P_MAX TYPE COUNT DEFAULT 25."反查最大阶数
    
    *PARAMETERS: RB_01 RADIOBUTTON GROUP G1 DEFAULT 'X',
    *            RB_02 RADIOBUTTON GROUP G1.
    SELECTION-SCREEN END OF BLOCK B1.
    SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-999.
    SELECTION-SCREEN COMMENT 01(78) TEXT-002.
    SELECTION-SCREEN COMMENT /1(78) TEXT-003.
    SELECTION-SCREEN COMMENT /1(78) TEXT-004.
    SELECTION-SCREEN END OF BLOCK B2.
    
    
    *------------------------------------
    *  月份 自定义字段 年(4位)月(2位)
    *------------------------------------
    AT SELECTION-SCREEN ON VALUE-REQUEST  FOR P_CXNY.
      DATA LT_ACTUAL_MONTH TYPE ISELLIST-MONTH .
      LT_ACTUAL_MONTH = SY-DATUM+0(6).
      CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
        EXPORTING
          ACTUAL_MONTH               = LT_ACTUAL_MONTH
    *     FACTORY_CALENDAR           = ' '
    *     HOLIDAY_CALENDAR           = ' '
          LANGUAGE                   = SY-LANGU
          START_COLUMN               = 8
          START_ROW                  = 5
        IMPORTING
          SELECTED_MONTH             = P_CXNY
    *     RETURN_CODE                = SY-SUBRC
        EXCEPTIONS
          FACTORY_CALENDAR_NOT_FOUND = 1
          HOLIDAY_CALENDAR_NOT_FOUND = 2
          MONTH_NOT_FOUND            = 3
          OTHERS                     = 4.
      IF SY-SUBRC <> 0.
      ENDIF.
    
    *----------------------------------------------------------------------*
    * start-of-selection
    *----------------------------------------------------------------------*
    START-OF-SELECTION.
      PERFORM FRM_GET_DATA.
      PERFORM FRM_SAVE_DATA.
      PERFORM FRM_DISPLAY_ALV.
    
    *&---------------------------------------------------------------------*
    *& Form FRM_GET_DATA
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM FRM_GET_DATA .
    
      SELECT MATNR,
             BWKEY AS WERKS
        FROM MBEW
       WHERE MATNR IN @S_MATNR
         AND BWKEY IN @S_WERKS
         AND BKLAS IN ( '3010' ,'3020', '7010' )
        INTO TABLE @DATA(LT_DATA).
      IF SY-SUBRC NE 0.
        MESSAGE S000 DISPLAY LIKE 'E'.
        LEAVE LIST-PROCESSING.
      ENDIF.
    
    * Caculate the first & last day of the Month
      CLEAR: GV_START,GV_END.
      GV_START = P_CXNY && '01'.
      CALL FUNCTION 'DATE_GET_MONTH_LASTDAY'
        EXPORTING
          I_DATE = GV_START
        IMPORTING
          E_DATE = GV_END.
    
      DATA: LT_MAT LIKE TABLE OF ZFIT_TZ07.
      LOOP AT LT_DATA INTO DATA(LS_DATA).
        "取对应成品料号
        CLEAR: LT_MAT.
        PERFORM FRM_GET_BOM_TOP TABLES LT_MAT USING LS_DATA-MATNR LS_DATA-WERKS.
        PERFORM FRM_MATNR_CP TABLES LT_MAT.
        GS_MAT-LFGJA = P_CXNY(4).
        GS_MAT-LFMON = P_CXNY+4(2).
        GS_MAT-WERKS = LS_DATA-WERKS.
        GS_MAT-MATNR = LS_DATA-MATNR.
        LOOP AT LT_MAT INTO DATA(LS_MAT).
          GS_MAT-MATNR_C = LS_MAT-MATNR_C.
          GS_MAT-MENGE   = LS_MAT-MENGE.
          GS_MAT-FLAG    = LS_MAT-FLAG.
          GS_MAT-STUEF   = LS_MAT-STUEF.
          APPEND GS_MAT TO GT_MAT.
          CLEAR: LS_MAT,
                 GS_MAT-MATNR_C,
                 GS_MAT-MENGE,
                 GS_MAT-FLAG ,
                 GS_MAT-STUEF.
        ENDLOOP.
        CLEAR LS_DATA.
      ENDLOOP.
    
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *& Form FRM_GET_BOM_TOP
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      --> <LF_DETAIL>
    *&---------------------------------------------------------------------*
    FORM FRM_GET_BOM_TOP TABLES LT_MAT STRUCTURE GS_MAT  USING P_MATNR P_WERKS.
      DATA: IT_WULTB   LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_WULTB2  LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_WULTB3  LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_WULTB4  LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_WULTB5  LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_WULTB6  LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_WULTB7  LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_EQUICT  LIKE CSCEQUI OCCURS 0 WITH HEADER LINE,
            IT_KNCAT   LIKE CSCKND OCCURS 0 WITH HEADER LINE,
            IT_MATCAT  LIKE CSCMAT OCCURS 0 WITH HEADER LINE,
            IT_STDCT   LIKE CSCSTD OCCURS 0 WITH HEADER LINE,
            IT_TPLCA   LIKE CSCTPL OCCURS 0 WITH HEADER LINE,
            IT_BOM     LIKE BOM_HEADER_API01 OCCURS 0 WITH HEADER LINE,
            IT_BOM_REV LIKE BOM_HEADER_API01 OCCURS 0 WITH HEADER LINE.
      DATA: LS_MAT LIKE LINE OF LT_MAT.
      DATA: LV_STUEF TYPE COUNT.
    
    
      IF P_MATNR IS NOT INITIAL AND P_WERKS IS NOT INITIAL.
        CLEAR:IT_WULTB,IT_WULTB[].
        CALL FUNCTION 'CS_WHERE_USED_MAT'
          EXPORTING
            DATUB                      = SY-DATUM
            DATUV                      = SY-DATUM
            MATNR                      = P_MATNR
            WERKS                      = P_WERKS
          TABLES
            WULTB                      = IT_WULTB
            EQUICAT                    = IT_EQUICT
            KNDCAT                     = IT_KNCAT
            MATCAT                     = IT_MATCAT
            STDCAT                     = IT_STDCT
            TPLCAT                     = IT_TPLCA
          EXCEPTIONS
            CALL_INVALID               = 1
            MATERIAL_NOT_FOUND         = 2
            NO_WHERE_USED_REC_FOUND    = 3
            NO_WHERE_USED_REC_SELECTED = 4
            NO_WHERE_USED_REC_VALID    = 5
            OTHERS                     = 6.
    
        IF IT_WULTB IS INITIAL.
          CLEAR LS_MAT.
          LS_MAT-MATNR = P_MATNR.
          LS_MAT-WERKS = P_WERKS.
          LS_MAT-MATNR_C = P_MATNR.
    *  LS_MAT-STUEF = 0."层级
          APPEND LS_MAT TO LT_MAT.
          EXIT."没有找到上一层,退出
        ELSE."找到了上一层,继续反查
          CLEAR: LV_STUEF.
          PERFORM FRM_FC_BOM TABLES LT_MAT IT_WULTB USING LV_STUEF.
        ENDIF.
      ENDIF.
    
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form FRM_FC_BOM
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      --> LT_MAT
    *&      --> IT_WULTB
    *&---------------------------------------------------------------------*
    FORM FRM_FC_BOM  TABLES   PT_MAT STRUCTURE GS_MAT
                              PT_WULTB STRUCTURE STPOV
                     USING LV_STUEF.
      DATA: IT_WULTB   LIKE STPOV OCCURS 0 WITH HEADER LINE,
            IT_EQUICT  LIKE CSCEQUI OCCURS 0 WITH HEADER LINE,
            IT_KNCAT   LIKE CSCKND OCCURS 0 WITH HEADER LINE,
            IT_MATCAT  LIKE CSCMAT OCCURS 0 WITH HEADER LINE,
            IT_STDCT   LIKE CSCSTD OCCURS 0 WITH HEADER LINE,
            IT_TPLCA   LIKE CSCTPL OCCURS 0 WITH HEADER LINE,
            IT_BOM     LIKE BOM_HEADER_API01 OCCURS 0 WITH HEADER LINE,
            IT_BOM_REV LIKE BOM_HEADER_API01 OCCURS 0 WITH HEADER LINE.
      DATA: LS_MAT LIKE GS_MAT.
    
      LV_STUEF = LV_STUEF + 1.
      IF P_MAX NE SPACE AND LV_STUEF > P_MAX.
        EXIT.
      ENDIF.
    
      LOOP AT PT_WULTB.
        CLEAR:IT_WULTB,IT_WULTB[].
        CALL FUNCTION 'CS_WHERE_USED_MAT'
          EXPORTING
            DATUB                      = SY-DATUM
            DATUV                      = SY-DATUM
            MATNR                      = PT_WULTB-MATNR
            WERKS                      = PT_WULTB-WERKS
          TABLES
            WULTB                      = IT_WULTB
            EQUICAT                    = IT_EQUICT
            KNDCAT                     = IT_KNCAT
            MATCAT                     = IT_MATCAT
            STDCAT                     = IT_STDCT
            TPLCAT                     = IT_TPLCA
          EXCEPTIONS
            CALL_INVALID               = 1
            MATERIAL_NOT_FOUND         = 2
            NO_WHERE_USED_REC_FOUND    = 3
            NO_WHERE_USED_REC_SELECTED = 4
            NO_WHERE_USED_REC_VALID    = 5
            OTHERS                     = 6.
        IF IT_WULTB IS INITIAL.
          CLEAR LS_MAT.
          LS_MAT-MATNR_C = PT_WULTB-MATNR.
          LS_MAT-WERKS = PT_WULTB-WERKS.
          LS_MAT-STUEF = LV_STUEF.
          APPEND LS_MAT TO PT_MAT.
        ELSE.
          PERFORM FRM_FC_BOM TABLES PT_MAT IT_WULTB USING LV_STUEF.
        ENDIF.
      ENDLOOP.
    
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form FRM_MATNR_CP
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      --> LT_MAT
    *&      <-- <LF_DETAIL>_MATNR_C
    *&---------------------------------------------------------------------*
    FORM FRM_MATNR_CP  TABLES   LT_MAT STRUCTURE ZFIT_TZ07.
    
      DATA(LV_LINE) = LINES( LT_MAT ).
      IF LV_LINE = 1."找到一个父件
    
      ELSEIF LV_LINE > 1.
        SORT LT_MAT BY WERKS MATNR_C.
        DELETE ADJACENT DUPLICATES FROM LT_MAT COMPARING WERKS MATNR_C.
        LOOP AT LT_MAT ASSIGNING FIELD-SYMBOL(<LF_MAT>).
          "针对单颗物料查询当期入库数量
          SELECT SUM( CASE SHKZG WHEN 'S' THEN MENGE WHEN 'H' THEN - MENGE END )
            FROM MSEG
           WHERE MATNR = @<LF_MAT>-MATNR_C
             AND WERKS = @<LF_MAT>-WERKS
             AND BUDAT_MKPF BETWEEN @GV_START AND @GV_END
             AND BWART IN ( '101','102' )"只取101/102 移动类型
            INTO @<LF_MAT>-MENGE.
        ENDLOOP.
        SORT LT_MAT BY MENGE DESCENDING."如果对应多个成品则取当期入库最多的成品
        READ TABLE LT_MAT ASSIGNING <LF_MAT> INDEX 1.
        IF SY-SUBRC EQ 0.
          <LF_MAT>-FLAG = 'X'.
        ENDIF.
        DELETE LT_MAT WHERE FLAG NE 'X'.
      ELSE.
    
      ENDIF.
    
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *& Form FRM_DISPLAY_ALV
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM FRM_DISPLAY_ALV .
    
      DATA:LT_FIELDCAT TYPE LVC_T_FCAT,
           LS_GRIDSET  TYPE LVC_S_GLAY.
    
      PERFORM FRM_GET_ALV_FIELDCAT CHANGING LT_FIELDCAT.
    
    *  LS_GRIDSET-EDT_CLL_CB = 'X'."编辑回调
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
        EXPORTING
          I_CALLBACK_PROGRAM = SY-REPID
    *     i_callback_pf_status_set = 'FRM_ALV_PF_STATUS_SET'
    *     i_callback_user_command  = 'FRM_ALV_USER_COMMAND'
          I_GRID_SETTINGS    = LS_GRIDSET
          IS_LAYOUT_LVC      = VALUE LVC_S_LAYO( ZEBRA = ABAP_TRUE CWIDTH_OPT = ABAP_TRUE )
          IT_FIELDCAT_LVC    = LT_FIELDCAT
          I_DEFAULT          = 'X'
          I_SAVE             = 'A'
          IS_VARIANT         = VALUE DISVARIANT( REPORT = SY-REPID )
    *     it_events          = gt_event
        TABLES
          T_OUTTAB           = GT_MAT
        EXCEPTIONS
          PROGRAM_ERROR      = 1
          OTHERS             = 2.
    
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form FRM_GET_ALV_FIELDCAT
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      <-- LT_FIELDCAT
    *&---------------------------------------------------------------------*
    FORM FRM_GET_ALV_FIELDCAT  CHANGING CT_FIELDCAT TYPE LVC_T_FCAT.
      DATA:LS_FIELDCAT TYPE LVC_S_FCAT.
      DEFINE MAC_FIELDCAT.
        ls_fieldcat-fieldname = &1.
        ls_fieldcat-coltext   = &2.
        APPEND ls_fieldcat TO ct_fieldcat.
        CLEAR ls_fieldcat.
      END-OF-DEFINITION.
    
      REFRESH CT_FIELDCAT.
      MAC_FIELDCAT:
              'LFGJA'            '年份',
              'LFMON'            '月份',
              'WERKS'            '工厂',
              'MATNR'            '材料',
              'MATNR_C'          '成品',
              'MENGE'            '成品当期入库数量',
              'FLAG'             '入库最多',
              'STUEF'            '反查层级'.
    
      LOOP AT CT_FIELDCAT INTO LS_FIELDCAT.
        CASE LS_FIELDCAT-FIELDNAME.
          WHEN 'MATNR' OR 'MATNR_C'.
            LS_FIELDCAT-OUTPUTLEN = '40'.
          WHEN 'MAKTX' OR 'MAKTX_C'.
            LS_FIELDCAT-OUTPUTLEN = '50'.
          WHEN 'XSFYL' OR 'QMJT'.
            LS_FIELDCAT-EDIT = 'X'.
          WHEN OTHERS.
        ENDCASE.
        MODIFY CT_FIELDCAT FROM LS_FIELDCAT.
      ENDLOOP.
    
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form FRM_SAVE_DATA
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM FRM_SAVE_DATA .
      IF GT_MAT IS NOT  INITIAL.
        DATA(LV_LINE) = LINES( GT_MAT ).
        MODIFY ZFIT_TZ07 FROM TABLE GT_MAT.
        IF SY-SUBRC EQ 0.
          COMMIT WORK AND WAIT.
          MESSAGE S028 WITH LV_LINE 'ZFIT_TZ07'."&1条数据保存到表:&2
        ELSE.
          ROLLBACK WORK.
          MESSAGE S005 DISPLAY LIKE 'E'.
        ENDIF.
      ENDIF.
    ENDFORM.
    本人常年接收SAP运维和远程项目,ECC,S/4HANA,CRM,WDA. 远程人天可谈,终身售后,有活请联系V信:18925782767(问问题免费,欢迎交流!) !!请关注本人技术分享公众号:SAP翔子(可扫头像二维码) 每天分享新知识,博客文章也会陆续整理并迁移至公众号,与您一起共同学习
  • 相关阅读:
    祈澈菇凉的高端知识资源分享星球开通
    使用mpvue开发小程序教程(五)
    使用mpvue开发小程序教程(四)
    使用mpvue开发小程序教程(三)
    使用mpvue开发小程序教程(二)
    使用mpvue开发小程序教程(一)
    手把手教你用vue-cli构建一个简单的路由应用
    wangEditor
    从列表中或数组中随机抽取固定数量的元素组成新的数组或列表
    js学习总结----核心解读
  • 原文地址:https://www.cnblogs.com/rainysblog/p/14930565.html
Copyright © 2020-2023  润新知