成本价自动创建功能自动函数

如果已发存在标准价,需要先删除,使用 BAPI_COSTESTIMATE_DELETE_MULTI

创建标准价使用 ZRFC_FICO_MR21_V1

 

根据传入的组件,自动创建P_MATNR的成本价

FUNCTION ZCTM_CALC_COST.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(P_MATNR) TYPE  BAPIMATERIAL
*"     VALUE(P_WERKS) TYPE  BAPIPLANT
*"     VALUE(P_BSTKD) TYPE  BSTKD
*"     VALUE(TEST) LIKE  BAPIREORGANIZATION-TESTRUN DEFAULT SPACE
*"  EXPORTING
*"     VALUE(SUBRC) TYPE  SY-SUBRC
*"  TABLES
*"      ITAB STRUCTURE  ZCTM_CALC_COST
*"      ET_LOG STRUCTURE  BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------



  DATA: TAB                  TYPE STANDARD TABLE OF RSMPE-FUNC WITH HEADER LINE,
        T_COST_ESTIMATE_LIST TYPE STANDARD TABLE OF BAPICOLIST WITH HEADER LINE,
        F_RETURN             TYPE BAPIRETURN,
        F_RETURN2            TYPE BAPIRET2.




  DATA: OUTPUTLIST         TYPE STANDARD TABLE OF BAPIDELETEMULTI WITH HEADER LINE,
*        T_RETURN           TYPE STANDARD TABLE OF BAPIRET2 WITH HEADER LINE,
        F_COSTINGCATEGORY1 TYPE BAPICOSTINGCATEGORY1,
        F_COSTINGCATEGORY2 TYPE BAPICOSTINGCATEGORY2.


  F_COSTINGCATEGORY1-COSTESTIMATE           = 'X'.
  F_COSTINGCATEGORY1-ADDITIVE_COSTESTIMATE  = 'X'.
  F_COSTINGCATEGORY1-WITH_QTY_STRUCTURE     = 'X'.
  F_COSTINGCATEGORY1-WITHOUT_QTY_STRUCTURE  = 'X'.

  F_COSTINGCATEGORY2-WITHOUTREFERENCE       = ''.
  F_COSTINGCATEGORY2-FUTURE_COSTESTIM       = 'X'.
  F_COSTINGCATEGORY2-CURRENT_COSTESTIM      = 'X'.
  F_COSTINGCATEGORY2-PREV_COSTESTIM         = 'X'.



  DELETE FROM ZCTM_CALC_COST WHERE BSTKD = P_BSTKD.



** checkman problems ...
  DATA: T_MATNR TYPE  BAPIMATERIAL OCCURS 0 WITH HEADER LINE.
  DATA: D_MATNR TYPE  BAPIMATERIAL OCCURS 0 WITH HEADER LINE.
  DATA: T_WERKS TYPE  BAPIPLANT OCCURS 0 WITH HEADER LINE.





  MOVE P_MATNR TO T_MATNR.
  APPEND T_MATNR.


  MOVE P_WERKS TO T_WERKS.
  APPEND T_WERKS.


  CLEAR ITAB.
  ITAB-WERKS = P_WERKS-LOW.
  ITAB-MATNR = P_MATNR-LOW.
  ITAB-BSTKD = P_BSTKD.
  MODIFY ITAB FROM ITAB
  TRANSPORTING WERKS MATNR
  WHERE MATNR IS INITIAL.


  DATA: LP  TYPE P DECIMALS 2.
  DATA: LN  TYPE P DECIMALS 2.

  DATA: ITM LIKE MBEW OCCURS 0 WITH HEADER LINE.
  CLEAR ITM[].

  SELECT
      MATNR
      BWKEY
      VPRSV
      VERPR
      STPRS
      PEINH
      INTO CORRESPONDING FIELDS OF TABLE ITM
    FROM MBEW
    FOR ALL ENTRIES IN ITAB
    WHERE MATNR = ITAB-COMPONENT AND BWKEY = ITAB-WERKS.


  LOOP AT ITAB.
    READ TABLE ITM WITH  KEY MATNR = ITAB-COMPONENT.

    IF SY-SUBRC  > 0.
      ET_LOG-TYPE = 'E'.
      CONCATENATE ITAB-COMPONENT  '成本价不存在' ITAB-WERKS INTO ET_LOG-MESSAGE.
      APPEND ET_LOG.
    ENDIF.

    MOVE ITAB-COMP_QTY TO LN.

    IF ITM-VPRSV = 'S'.
      LP = LP + ITM-STPRS * LN.
    ELSE.
      LP = LP + ITM-VERPR * LN.
    ENDIF.


  ENDLOOP.


  READ TABLE ET_LOG WITH KEY TYPE = 'E'.

  IF SY-SUBRC = 0.
    SUBRC = 1.
    RETURN.
  ENDIF.



**查找顶层物料成本价格

  DATA: LM LIKE MBEW.
  CLEAR LM.

  SELECT SINGLE

    MATNR
    BWKEY


      VPRSV
      VERPR
      STPRS
      PEINH


    ZPLPR
  LPLPR
  VPLPR

    INTO CORRESPONDING FIELDS OF LM

  FROM MBEW WHERE BWKEY = P_WERKS-LOW
    AND MATNR = P_MATNR-LOW


    .


  CLEAR LN.

  IF LM-VPRSV = 'S'.

    LN = LM-VERPR.
  ELSE.
    LN = LM-STPRS.


  ENDIF.

***价格一样,记录起来
  IF LN =  LP.
    CLEAR ITAB.
    ITAB-MATNR = LM-MATNR.
    ITAB-WERKS = LM-BWKEY.
    ITAB-BSTKD = P_BSTKD.
    ITAB-STPRS = LN.
    APPEND ITAB.
    MODIFY ZCTM_CALC_COST FROM TABLE ITAB.
    RETURN.
  ENDIF.



***是否存在估价,存在就要删除
  IF
    LM-ZPLPR > 0  OR
    LM-LPLPR > 0  OR
    LM-VPLPR > 0.



***1.删除估价
    CALL FUNCTION 'BAPI_COSTESTIMATE_DELETE_MULTI'
      EXPORTING
        TESTRUN          = TEST
        COSTINGCATEGORY1 = F_COSTINGCATEGORY1
        COSTINGCATEGORY2 = F_COSTINGCATEGORY2
      TABLES
        RETURN           = ET_LOG
        OUT_LIST         = OUTPUTLIST
        IN_MATERIAL      = T_MATNR
*       IN_VALUATIONTYPE = P_BWTAR
        IN_PLANT         = T_WERKS
*       IN_COMP_CODE     = P_BUKRS
*       IN_VALID_FROM    = P_KADAT
*       IN_COSTINGVARIANT  = P_KLVAR
*       IN_COSTING_STATUS  = P_FEH_ST
*       IN_COSTING_VERSION = P_TVERS
*       IN_COSTINGRUN    = KALAID
*       IN_COSTINGRUNDATE  = KALADAT
      .




    IF TEST EQ 'X'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
*     IMPORTING
*       RETURN        =

    ELSE.


      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'
*     IMPORTING
*         RETURN        =
        .
    ENDIF.



    .

*  DATA: LM LIKE MBEW.
*  CLEAR LM.

    SELECT SINGLE

      MATNR
      BWKEY

      ZPLPR
    LPLPR
    VPLPR

      INTO CORRESPONDING FIELDS OF LM

    FROM MBEW WHERE BWKEY = P_WERKS-LOW
      AND MATNR = P_MATNR-LOW
      AND (     ZPLPR > 0  OR
    LPLPR > 0 OR
    VPLPR > 0
   )

      .

    IF SY-SUBRC = 0.
      ET_LOG-TYPE = 'E'.
      ET_LOG-MESSAGE  = '成本估价删除错误'.
      APPEND ET_LOG.
      SUBRC = 2.
      RETURN.
    ENDIF.

  ENDIF.


  DATA: IM_TAB  LIKE  ZSTRU_MEBW_MR21 OCCURS 0 WITH HEADER LINE.
  DATA: LT_MSG LIKE CMFMSG OCCURS 0 WITH HEADER LINE.




  IM_TAB-WERKS = LM-BWKEY.
  IM_TAB-MATNR = LM-MATNR.
  IM_TAB-KBETR = LP.
  IM_TAB-KPEIN = LM-PEINH.
*  IM_TAB-WAERS =



  SELECT SINGLE BUKRS  INTO IM_TAB-BUKRS
    FROM T001K WHERE
   BWKEY = LM-BWKEY.


  SELECT SINGLE WAERS  INTO IM_TAB-WAERS
    FROM T001 WHERE
   BUKRS = IM_TAB-BUKRS.


  APPEND IM_TAB.
*****以下用MR21更新标准价
  CLEAR LT_MSG[].
  CALL FUNCTION 'ZRFC_FICO_MR21_V1'
* EXPORTING
*   BUDAT            =
    TABLES
      IM_TAB     = IM_TAB
      EX_MESSAGE = LT_MSG.



  READ TABLE LT_MSG WITH KEY MSGTY  = 'E'.
  IF SY-SUBRC = 0.

    LOOP AT LT_MSG.

      ET_LOG-TYPE = LT_MSG-MSGTY.

      CONCATENATE LT_MSG-MSGV1 LT_MSG-MSGV2 LT_MSG-MSGV3 LT_MSG-MSGV4  INTO ET_LOG-MESSAGE.
      APPEND ET_LOG.

    ENDLOOP.



    ET_LOG-TYPE = 'E'.
    ET_LOG-MESSAGE = '成本价计算错误'.
    APPEND ET_LOG.
    SUBRC = 3.
  ELSE.


****价格一样,记录起来
    CLEAR ITAB.
    ITAB-MATNR = LM-MATNR.
    ITAB-WERKS = LM-BWKEY.
    ITAB-BSTKD = P_BSTKD.
    ITAB-STPRS = LP.
    APPEND  ITAB.



    MODIFY ZCTM_CALC_COST FROM TABLE ITAB.

  ENDIF.







*
*  SELECT SINGLE
*
*    MATNR
*    BWKEY
*
*    ZPLPR
*  LPLPR
*  VPLPR
*
*    INTO CORRESPONDING FIELDS OF LM
*
*  FROM MBEW WHERE BWKEY = P_WERKS-LOW
*    AND MATNR = P_MATNR-LOW
*    AND (     ZPLPR > 0  OR
*  LPLPR > 0 OR
*  VPLPR > 0
* )
*
*    .
*
*  IF SY-SUBRC > 0.
*    ET_LOG-TYPE = 'E'.
*    ET_LOG-MESSAGE  = '成本估价删除错误'.
*    APPEND ET_LOG.
*    SUBRC = 2.
*    RETURN.
*  ENDIF.
*




ENDFUNCTION.




*FORM F_ADD_ZERO  USING VI_INPUT.
*  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
*    EXPORTING
*      INPUT  = VI_INPUT
*    IMPORTING
*      OUTPUT = VI_INPUT.
*
*ENDFORM.                    " f_add_zero

 

成本价自动创建功能自动函数

 

上一篇:SAP优化


下一篇:WMS函数组:交货单过帐(BAPI)