ITPub博客

首页 > Linux操作系统 > Linux操作系统 > bdc应用

bdc应用

原创 Linux操作系统 作者:103104521 时间:2013-01-08 10:26:43 0 删除 编辑

*&---------------------------------------------------------------------*
*& Report  /TDK/CB00180
*&---------------------------------------------------------------------*
* PROGRAM : /TDK/CB00180
* CREATED BY  : Li Yan (SAE)
* DATE  : 2011/05/20
* DESCRIPTION : Yield Loss Calculation
*&---------------------------------------------------------------------*
* CHANGE LOG
* DATE  NO  CHANGED BY  CONTENTS
* ----------  ------  ------------  -------------------------------------

REPORT  /TDK/CB00180.
*&---------------------------------------------------------------------*
*&          TABLES
*&---------------------------------------------------------------------*

TABLES:/TDK/CT000103, "Add-on Table(Yield Loss Calculation)
       T001,          "Company codes
       T001K,         "Valuation area
       T683S,         "Pricing Procedure: Data
       TKZU1,         "Overhead Base Cost Elements
       TKZU3,         "Overhead Credit
       CSKB,          "Cost Elements (Data Dependent on Controlling Area)
       SETLEAF,       "Values in Sets
       /TDK/CT000102, "Add-on Table(Yield Loss Maintainance)
       MBEW,          "Material Valuation
       KEKO.          "Product Costing - Header Data
*&---------------------------------------------------------------------*
*&            BDC DATA
*&---------------------------------------------------------------------*
DATA: T_BDC LIKE TABLE OF BDCDATA WITH HEADER LINE.
DATA: T_MSG TYPE TABLE OF BDCMSGCOLL WITH HEADER LINE.
DATA: T_YL TYPE I,
      C_YL(15) TYPE C,
      C_LOSGR(16) TYPE C,
      C_PEINH(5) TYPE C,
      C_LAST_DAY(10) TYPE C,
      C_FIRST_DAY(10) TYPE C,
      T_MEINS TYPE MEINS,
      L_MATNR TYPE MATNR.


*&---------------------------------------------------------------------*
*&            TYPE DATA
*&---------------------------------------------------------------------*

DATA:T_KALKA  TYPE KALKA,
     T_KLVAR TYPE CK_KLVAR,
     T_BWVAR  TYPE BWVAR,
     T_KOKRS TYPE KOKRS,
     T_KTOPL TYPE KTOPL,
     T_WAERS TYPE WAERS,
     T_PEINH TYPE PEINH,
     T_OPERATION TYPE VORNR,
     T_MATEIRAL TYPE MATNR,
     S_VORNR TYPE VORNR,
     N TYPE I,
     LF_BAPI1090_1 LIKE BAPI1090_1.
DATA:T_COUNT_D TYPE I.
DATA:T_COUNT_S  TYPE I.
DATA:L_COUNT_S  TYPE I.

DATA:L_TABIX  TYPE  SY-TABIX,
     T_TABIX TYPE SY-TABIX,
     M_TABIX TYPE SY-TABIX.
DATA:FIRST_DAY LIKE AFRU-BUDAT,
     LAST_DAY LIKE AFRU-BUDAT.

DATA:L_FIRST_DAY TYPE STRING,
     L_LAST_DAY TYPE STRING.
DATA:T_FIRST_DAY TYPE STRING,
     T_LAST_DAY TYPE STRING.

DATA:T_KSTAR TYPE KSTAR,
     W_WERTN TYPE WERTN,
     T_VORNR TYPE VORNR,
     W_VORNR TYPE VORNR,
     T_WERTN TYPE WERTN,
     T_TOAL  TYPE WERTN.

DATA:BEGIN OF WA_MAT_HIER OCCURS 0.
        INCLUDE STRUCTURE  /TDK/SC0002.
DATA:END OF WA_MAT_HIER.


DATA:T_MATNR  LIKE STANDARD TABLE OF /TDK/SC0001,     "input parameter
     WA_MATNR LIKE LINE OF  T_MATNR.
DATA:T_MAT_HIER  TYPE STANDARD TABLE OF /TDK/SC0002,  "hierarchy
     TH_MAT_HIER TYPE /TDK/SC0002.                    "header of hierarchy

DATA:BEGIN OF COST_DATA OCCURS 0,
     BZOBJ   LIKE KEKO-BZOBJ,
     KALNR   LIKE  KEKO-KALNR,
     KALKA   LIKE  KEKO-KALKA,
     KADKY   LIKE  KEKO-KADKY,
     TVERS   LIKE  KEKO-TVERS,
     BWVAR   LIKE  KEKO-BWVAR,
     KKZMA   LIKE  KEKO-KKZMA,
     MATNR   LIKE  KEKO-MATNR,
     WERKS   LIKE  KEKO-WERKS,
     LOSGR   LIKE  KEKO-LOSGR,
     MEINS   LIKE  KEKO-MEINS,
     FEH_STA LIKE  KEKO-FEH_STA,
     KALSM   LIKE  KEKO-KALSM,
     DISST   LIKE  KEKO-DISST,
     PRCTR   LIKE  KEKO-PRCTR,
     TYPPS   LIKE  CKIS-TYPPS,
     KSTAR   LIKE  CKIS-KSTAR,
     ELEMT   LIKE  CKIS-ELEMT,
     WERKS1  LIKE  CKIS-WERKS,
     MATNR1  LIKE  CKIS-MATNR,
     WERTN   LIKE  CKIS-WERTN,
     MENGE   LIKE  CKIS-MENGE,
     MEEHT   LIKE  CKIS-MEEHT,
     VORNR   LIKE  CKIS-VORNR,
     UKALN   LIKE  CKIS-UKALN,
     UKALKA  LIKE  CKIS-UKALKA,
     UKADKY  LIKE  CKIS-UKADKY,
     UTVERS  LIKE  CKIS-UTVERS,
     UBWVAR  LIKE  CKIS-UBWVAR,
     BAUGR   LIKE  CKIS-BAUGR,
END OF COST_DATA.

DATA:BEGIN OF Z_TAB OCCURS 0,
     VORNR   LIKE  CKIS-VORNR,
     BZOBJ   LIKE KEKO-BZOBJ,
     KALNR   LIKE  KEKO-KALNR,
     KALKA   LIKE  KEKO-KALKA,
     KADKY   LIKE  KEKO-KADKY,
     TVERS   LIKE  KEKO-TVERS,
     BWVAR   LIKE  KEKO-BWVAR,
     KKZMA   LIKE  KEKO-KKZMA,
     MATNR   LIKE  KEKO-MATNR,
     WERKS   LIKE  KEKO-WERKS,
     LOSGR   LIKE  KEKO-LOSGR,
     MEINS   LIKE  KEKO-MEINS,
     FEH_STA LIKE  KEKO-FEH_STA,
     KALSM   LIKE  KEKO-KALSM,
     DISST   LIKE  KEKO-DISST,
     PRCTR   LIKE  KEKO-PRCTR,
     TYPPS   LIKE  CKIS-TYPPS,
     KSTAR   LIKE  CKIS-KSTAR,
     ELEMT   LIKE  CKIS-ELEMT,
     WERTN   LIKE  CKIS-WERTN,
     MENGE   LIKE  CKIS-MENGE,
     MEEHT   LIKE  CKIS-MEEHT,
     UKALN   LIKE  CKIS-UKALN,
     UKALKA  LIKE  CKIS-UKALKA,
     UKADKY  LIKE  CKIS-UKADKY,
     UTVERS  LIKE  CKIS-UTVERS,
     UBWVAR  LIKE  CKIS-UBWVAR,
     BAUGR   LIKE  CKIS-BAUGR,
END OF Z_TAB.

DATA:BEGIN OF BASE_DATA OCCURS 0,
     KVEWE LIKE T683S-KVEWE,
     KAPPL LIKE T683S-KAPPL,
     KALSM LIKE T683S-KALSM,
     STUNR LIKE T683S-STUNR,
     KSCHL LIKE T683S-KSCHL,
     KAGRU LIKE TKZU1-KAGRU ,
 END OF BASE_DATA.

DATA:BEGIN OF CREDIT_DATA OCCURS 0,
     KVEWE LIKE T683S-KVEWE,
     KAPPL LIKE T683S-KAPPL,
     KALSM LIKE T683S-KALSM,
     STUNB LIKE T683S-STUNB,
     STUNR LIKE T683S-STUNR,
     KSCHL LIKE T683S-KSCHL,
     KVSL1 LIKE T683S-KVSL1,
END OF CREDIT_DATA.

DATA:BEGIN OF WA_SETLEAF OCCURS 0,
     VALFROM LIKE SETLEAF-VALFROM ,
     VALTO   LIKE SETLEAF-VALTO,
END OF   WA_SETLEAF.

DATA:BEGIN OF WA_CSKB OCCURS 0,
     KSTAR LIKE  CSKB-KSTAR,
END OF WA_CSKB.

DATA:BEGIN OF WA_TKZU3 OCCURS 0,
     KSTAR LIKE  TKZU3-KSTAR,
END OF WA_TKZU3.

DATA:BEGIN OF T_COST_SHEET_DATA OCCURS 0,
     KVEWE   LIKE T683S-KVEWE,
     KAPPL   LIKE T683S-KAPPL,
     KALSM   LIKE T683S-KALSM,
     STUNR   LIKE T683S-STUNR,
     KSCHL   LIKE T683S-KSCHL,   "codition type for base_data
     KSCHL1  LIKE T683S-KSCHL,  "codition type for credit_data
     KVSL1   LIKE T683S-KVSL1,
     KAGRU   LIKE TKZU1-KAGRU ,
     KSTAR   LIKE TKZU3-KSTAR,   " for base_data
     KSTAR1  LIKE TKZU3-KSTAR,  " for credit_data
     VALFROM LIKE SETLEAF-VALFROM ,
     VALTO   LIKE SETLEAF-VALTO,
 END OF   T_COST_SHEET_DATA .

DATA:BEGIN OF COST_SHEET_DATA OCCURS 0,
     KVEWE   LIKE T683S-KVEWE,
     KAPPL   LIKE T683S-KAPPL,
     KALSM   LIKE T683S-KALSM,
     STUNR   LIKE T683S-STUNR,
     KSCHL   LIKE T683S-KSCHL,   "codition type for base_data
     KSCHL1  LIKE T683S-KSCHL,  "codition type for credit_data
     KVSL1   LIKE T683S-KVSL1,
     KAGRU   LIKE TKZU1-KAGRU ,
     KSTAR   LIKE TKZU3-KSTAR,   " for base_data
     KSTAR1  LIKE TKZU3-KSTAR,  " for credit_data
     VALFROM LIKE SETLEAF-VALFROM ,
     VALTO   LIKE SETLEAF-VALTO,
 END OF   COST_SHEET_DATA .

DATA:BEGIN OF YIELD_LOSS OCCURS 0,
     WERKS        LIKE /TDK/CT000102-WERKS,
     PLNBEZ       LIKE /TDK/CT000102-PLNBEZ,
     VORNR        LIKE /TDK/CT000102-VORNR,
     FIX_VALUE    LIKE /TDK/CT000102-FIX_VALUE,
     PLAN_F_QTY   LIKE /TDK/CT000102-PLAN_F_QTY ,
     GMNGA        LIKE /TDK/CT000102-GMNGA ,
     XMNGA        LIKE /TDK/CT000102-XMNGA ,
     GMEIN        LIKE /TDK/CT000102-GMEIN ,
     YIELD_RATE_C LIKE /TDK/CT000102-YIELD_RATE_C ,
     YIELD_RATE_M LIKE /TDK/CT000102-YIELD_RATE_M,
END OF YIELD_LOSS.

DATA: BEGIN OF COST_COMPONENT_DATA OCCURS 0,
      KSTAV LIKE TCKH2-KSTAV,
      KSTAB LIKE TCKH2-KSTAB,
      ELEMT LIKE TCKH2-ELEMT,
      KSTAR LIKE CSKB-KSTAR,
END OF COST_COMPONENT_DATA .

DATA:BEGIN OF Z_ALLOCATE OCCURS 0,
     KSTAR LIKE CSKB-KSTAR,
     VORNR LIKE /TDK/CT000102-VORNR,
     WERTN LIKE  CKIS-WERTN  ,
END OF Z_ALLOCATE.

DATA:BEGIN OF Z_MATNR OCCURS 0,
     MATNR TYPE MATNR,
END OF Z_MATNR.

DATA:BEGIN OF Z_KSTAR OCCURS 0,
     KSTAR TYPE KSTAR,
END OF Z_KSTAR.

DATA:BEGIN OF Z_LOW_LEVEL OCCURS 0,
     MATNR TYPE CKIS-MATNR,
     DISST TYPE CKIS-DISST,
END OF Z_LOW_LEVEL.

DATA:BEGIN OF T_WORK_TAB OCCURS 0,
     MATNR      LIKE KEKO-MATNR,
     WERKS      LIKE KEKO-WERKS,
     LOSGR      LIKE KEKO-LOSGR,
     FEH_STA    LIKE KEKO-FEH_STA,
     DISST      LIKE KEKO-DISST,
     MEINS      LIKE KEKO-MEINS,
     VORNR      LIKE CKIS-VORNR,
     YIELD_RATE LIKE /TDK/CT000103-YIELD_RATE,
     TXT(18)    TYPE C ,                         "Set in Process"
     WERTN      LIKE CKIS-WERTN,                 "Value in CoArea Curr.
     WERTN1     LIKE CKIS-WERTN,                 "Yield Loss Amount
END OF T_WORK_TAB.


DATA:BEGIN OF WORK_TAB OCCURS 0.
        INCLUDE STRUCTURE T_WORK_TAB.
DATA:END OF WORK_TAB.

DATA: BEGIN OF RELAT_TAB OCCURS 0,
      TXT(18) TYPE C,
      ELEMT LIKE  CKIS-ELEMT  ,
END OF   RELAT_TAB.

DATA: BEGIN OF ITAB OCCURS 0.
        INCLUDE STRUCTURE COST_DATA.
DATA:END OF ITAB.

DATA: BEGIN OF ITAB1 OCCURS 0.
        INCLUDE STRUCTURE COST_DATA.
DATA:END OF ITAB1.

DATA:BEGIN OF Z_OPERATION OCCURS 0,
      MATNR LIKE CKIS-MATNR,
      VORNR LIKE  CKIS-VORNR  ,
END OF  Z_OPERATION.

DATA:BEGIN OF PRE_TAB OCCURS 0 .  "Previous Process Cost TBL
        INCLUDE STRUCTURE WORK_TAB.
DATA:END OF PRE_TAB.

DATA:BEGIN OF SF_TAB OCCURS 0 .   "Semi-Finished Material TBL
        INCLUDE STRUCTURE WORK_TAB.
DATA:END OF SF_TAB.

DATA: BEGIN OF OUTPUT_TAB OCCURS 0.
        INCLUDE STRUCTURE /TDK/CT000103.
DATA:END OF OUTPUT_TAB.

DATA:BEGIN OF YL_TAB OCCURS 0,
     BUKRS  TYPE BUKRS,
     WERKS  TYPE WERKS_D,
     GJAHR  TYPE GJAHR,
     MONAT  TYPE MONAT,
     MATNR  TYPE MATNR,
     KLVAR  TYPE CK_KLVAR,
     TVERS  TYPE CK_TVERS,
     LOSGR  TYPE CK_LOSGR,
     MEINS  TYPE MEINS,
     VORNR  TYPE VORNR,
     VAR_YL TYPE CK_KWT,
     DIR_YL TYPE CK_KWT,
     IND_YL TYPE CK_KWT,
END OF YL_TAB.

DATA:BEGIN OF YL_BDC OCCURS 0,
     KLVAR       TYPE CK_KLVAR,
     MATNR       TYPE MATNR,
     WERKS       TYPE WERKS_D,
     TVERS       TYPE CK_TVERS,
     LOSGR(16)   TYPE C,
     MEINS       TYPE MEINS,
     VORNR       TYPE VORNR,
     PEINH(5)    TYPE C,
     YL_LOSS(15) TYPE C,
     KSTAR       TYPE KSTAR,
END OF YL_BDC.


*&---------------------------------------------------------------------*
*&            SELECT-SCREEN
*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME. TITLE TEXT-001.
PARAMETERS: P_BUKRS LIKE T001-BUKRS OBLIGATORY,
            P_WERKS LIKE AFRU-WERKS OBLIGATORY,
            P_GJAHR LIKE BKPF-GJAHR OBLIGATORY,
            P_MONAT LIKE BKPF-MONAT OBLIGATORY,
            P_KLVAR LIKE TCK03-KLVAR OBLIGATORY,
            P_TVERS LIKE KEKO-TVERS  OBLIGATORY,
            P_BU(34) TYPE C,
            P_CBU(34) TYPE C,
            P_SBU(34) TYPE C,
            P_GOUP(34) TYPE C.
SELECT-OPTIONS:P_MATNR FOR KEKO-MATNR  .
PARAMETERS: R1 AS CHECKBOX USER-COMMAND CHECK DEFAULT 'X'.
PARAMETERS: R2 AS CHECKBOX USER-COMMAND CHECK .
SELECTION-SCREEN END OF BLOCK B1.

AT SELECTION-SCREEN.
  PERFORM. CHECK_VALUE.
  IF R1 = '' AND R2 = ''.
    MESSAGE 'Check at least one Check Box is selected at Selection Parameter' TYPE 'E'.
  ENDIF.

*&---------------------------------------------------------------------*
*&            START OF SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*Check the Value
  PERFORM. PRE_DATA .
  IF SY-BATCH = 'X'.
    IF R1 = 'X'.
      PERFORM. CALCULATION_POCESS.
    ENDIF.
    IF R2 = 'X'.
      PERFORM. POCESS_DATA.
    ENDIF.
    PERFORM. OUTPUT.
  ELSE.
    MESSAGE 'Only Background Execution !' TYPE 'E'.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  CHECK_VALUE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. CHECK_VALUE .
*Check Company Code
  SELECT SINGLE  KTOPL  FROM T001 INTO T_KTOPL
    WHERE BUKRS = P_BUKRS.
  IF SY-SUBRC <> 0.
    MESSAGE E001(/TDK/CB00180).
  ENDIF.

*Authority Check
  AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'  " Check authority of compay code
           ID 'BUKRS' FIELD P_BUKRS.
  IF SY-SUBRC <> 0.
    MESSAGE 'You have no authority for the company code' TYPE 'E'.
  ENDIF.

*Get CO Area
  SELECT SINGLE KOKRS FROM TKA02 INTO  T_KOKRS
      WHERE BUKRS = P_BUKRS.
  IF SY-SUBRC <> 0.
    MESSAGE E002(/TDK/CB00180).
  ENDIF.

*Get Currency
  SELECT SINGLE WAERS INTO T_WAERS
    FROM TKA01
    WHERE KOKRS = T_KOKRS.
  IF SY-SUBRC <> 0.
    MESSAGE E003(/TDK/CB00180).
  ENDIF.

*Controll Regulation for Data Selection
  IF P_MATNR <> ''.
    IF ( P_BU <> '' OR  P_SBU <> '' OR  P_CBU <> '' OR  P_GOUP <> '' ).
      MESSAGE E004(/TDK/CB00180) WITH  P_MATNR .
    ENDIF.
  ELSEIF P_GOUP <> ''.
    IF ( P_BU <> '' OR  P_CBU <> '' OR  P_SBU <> '' ).
      MESSAGE E004(/TDK/CB00180) WITH P_GOUP .
    ENDIF.
  ELSEIF P_SBU <>''.
    IF ( P_BU <> '' OR  P_CBU <> '' OR  P_GOUP <> '' ).
      MESSAGE E004(/TDK/CB00180) WITH P_SBU .
    ENDIF.
  ELSEIF P_CBU <> ''.
    IF   P_BU <> ''.
      MESSAGE E004(/TDK/CB00180) WITH  P_CBU .
    ENDIF.
  ENDIF.
ENDFORM.                    " CHECK_VALUE
*&---------------------------------------------------------------------*
*&      Form  PRE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. PRE_DATA .
*Valuation area Selection
  SELECT SINGLE * FROM T001K
      WHERE BUKRS = P_BUKRS
      AND   BWKEY =  P_WERKS.
  IF SY-SUBRC <> 0.
    MESSAGE E005(/TDK/CB00180).
  ENDIF.

*Get First and Last Date of Fiscal Year and Period
  CALL FUNCTION 'BAPI_COAREA_GETPERIODLIMITS'
    EXPORTING
      CONTROLLINGAREAID         =  T_KOKRS
      FISCAL_PERIOD             =  P_MONAT
      FISCAL_YEAR               =  P_GJAHR
   IMPORTING
      FIRST_DAY_OF_PERIOD       =  FIRST_DAY
      LAST_DAY_OF_PERIOD        =  LAST_DAY
*     LAST_NORMAL_PERIOD        =
*     RETURN                    =
            .
  IF SY-SUBRC <> 0.
    MESSAGE E006(/TDK/CB00180).
  ENDIF.

*Change the Date format to User Setting

  L_FIRST_DAY = FIRST_DAY.
  L_LAST_DAY = LAST_DAY.
  CALL FUNCTION '/SAPDII/SPP05_CONVERT_DATE'
   EXPORTING
     IF_DATE             = L_FIRST_DAY
   IMPORTING
*     EF_DATEFORMAT       =
     EF_DATE             = T_FIRST_DAY
*     ES_RETURN           =
*   TABLES
*     ET_DATETEXT         =
            .
  IF SY-SUBRC <> 0.
    MESSAGE 'First Date could not set for Additive Cost' TYPE 'E'.
  ENDIF.

  CALL FUNCTION '/SAPDII/SPP05_CONVERT_DATE'
   EXPORTING
     IF_DATE             = L_LAST_DAY
   IMPORTING
*     EF_DATEFORMAT       =
     EF_DATE             = T_LAST_DAY
*     ES_RETURN           =
*   TABLES
*     ET_DATETEXT         =
            .

  IF SY-SUBRC <> 0.
    MESSAGE 'Last Date could not set for Additive Cost' TYPE 'E'.
  ENDIF.

 

*Get Costing Type and Valuation Variant
  SELECT SINGLE KLVAR KALKA BWVAR  FROM TCK03
    INTO (T_KLVAR ,T_KALKA ,T_BWVAR)
    WHERE KLVAR = P_KLVAR.
  IF SY-SUBRC <> 0.
    MESSAGE E007(/TDK/CB00180).
  ENDIF.

*Get Cost Component data
**Get Cost Component's Cost Element(Range)
**Get Cost Element assigned to Cost Component
  SELECT TCKH2~KSTAV TCKH2~KSTAB TCKH2~ELEMT CSKB~KSTAR
      INTO CORRESPONDING FIELDS OF TABLE COST_COMPONENT_DATA
       FROM TCKH2 INNER JOIN CSKB ON CSKB~KSTAR <= TCKH2~KSTAV
                                 AND CSKB~KSTAR >= TCKH2~KSTAB
       WHERE TCKH2~KTOPL  = T_KTOPL
         AND TCKH2~ELEHK  = 'Y1'
         AND CSKB~KOKRS   = T_KOKRS
         AND CSKB~DATAB   <=  LAST_DAY
         AND CSKB~DATBI   >=  LAST_DAY.
  IF SY-SUBRC <> 0.
    MESSAGE E008(/TDK/CB00180).
  ENDIF.
ENDFORM.                    " PRE_DATA
*&---------------------------------------------------------------------*
*&      Form  CALCULATION_POCESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. CALCULATION_POCESS .
  PERFORM. GET_DATA.
  PERFORM. DATA_POCESS.
ENDFORM.                    " CALCULATION_POCESS
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. GET_DATA .
*Select Costing Data(Header)
  SELECT KEKO~BZOBJ KEKO~KALNR KEKO~KALKA KEKO~KADKY KEKO~TVERS KEKO~BWVAR KEKO~KKZMA
         KEKO~MATNR KEKO~WERKS KEKO~LOSGR KEKO~MEINS KEKO~FEH_STA KEKO~KALSM KEKO~DISST
         KEKO~PRCTR
         CKIS~TYPPS CKIS~KSTAR CKIS~ELEMT  CKIS~WERKS CKIS~MATNR CKIS~WERTN
         CKIS~MENGE CKIS~MEEHT CKIS~VORNR CKIS~UKALN CKIS~UKALKA CKIS~UKADKY CKIS~UTVERS
         CKIS~UBWVAR  CKIS~BAUGR
   INTO  TABLE COST_DATA
   FROM KEKO INNER JOIN CKIS ON CKIS~BZOBJ = KEKO~BZOBJ
                            AND CKIS~KALNR = KEKO~KALNR
                            AND CKIS~KALKA = KEKO~KALKA
                            AND CKIS~KADKY = KEKO~KADKY
                            AND CKIS~TVERS = KEKO~TVERS
                            AND CKIS~BWVAR = KEKO~BWVAR
                            AND CKIS~KKZMA = KEKO~KKZMA
    WHERE KEKO~BZOBJ = '0'
    AND   KEKO~KALKA = T_KALKA
    AND   KEKO~KADKY = FIRST_DAY
    AND   KEKO~TVERS = P_TVERS
    AND   KEKO~BWVAR = T_BWVAR
    AND   KEKO~KKZMA = ''
    AND   KEKO~PLNNR <> ''
    AND   CKIS~LEDNR = '00'.


  IF SY-SUBRC <> 0.
    MESSAGE E009(/TDK/CB00180).
  ENDIF.

  LOOP AT COST_DATA WHERE FEH_STA = 'KF'.
    ON CHANGE OF COST_DATA-MATNR.
      MESSAGE S010(/TDK/CB00180) WITH COST_DATA-MATNR.
    ENDON.
  ENDLOOP.
  CLEAR COST_DATA.

  SORT COST_DATA BY KALSM.
*Get Costing Sheet Data
  LOOP AT COST_DATA.
    ON CHANGE OF COST_DATA-KALSM.
**Get Costing Sheet "Row-Base" data
      SELECT T683S~KVEWE T683S~KAPPL T683S~KALSM T683S~STUNR T683S~KSCHL
            TKZU1~KAGRU
        APPENDING CORRESPONDING FIELDS OF TABLE BASE_DATA
        FROM T683S
        INNER JOIN TKZU1 ON TKZU1~KAPPL = T683S~KAPPL
                        AND TKZU1~BKOND = T683S~KSCHL

        WHERE T683S~KVEWE   = 'A'
          AND T683S~KAPPL   = 'KA'
          AND T683S~KALSM   = COST_DATA-KALSM
          AND T683S~STUNB   = ''
          AND T683S~KAUTO   = ''
          AND TKZU1~KOKRS   = T_KOKRS.
    ENDON.
  ENDLOOP.
  CLEAR COST_DATA.
  DELETE ADJACENT DUPLICATES FROM BASE_DATA COMPARING ALL FIELDS.
  IF BASE_DATA[] IS  INITIAL.
    MESSAGE E012(/TDK/CB00180) .
  ENDIF.
**Get Costing Sheet "Row-Credit" data
  LOOP AT BASE_DATA.
*    ON CHANGE OF BASE_DATA-KALSM.
    SELECT  *   FROM T683S
*     APPENDING CORRESPONDING FIELDS OF TABLE CREDIT_DATA
        WHERE T683S~KVEWE  = 'A'
          AND T683S~KAPPL  = 'KA'
          AND T683S~KALSM  = BASE_DATA-KALSM
          AND T683S~KAUTO  = ''
          AND T683S~STUNB  = BASE_DATA-STUNR.
      CHECK SY-SUBRC = 0.
      MOVE-CORRESPONDING T683S TO CREDIT_DATA.
      APPEND CREDIT_DATA.
**Get Cost Element used for Costing Sheet
      SELECT SINGLE * FROM TKZU3
      WHERE KAPPL = T683S-KAPPL
        AND ZUENT = T683S-KVSL1
        AND KOKRS = T_KOKRS.
      CHECK SY-SUBRC = 0.
      MOVE-CORRESPONDING TKZU3 TO WA_TKZU3.
      APPEND WA_TKZU3.

      MOVE-CORRESPONDING BASE_DATA TO T_COST_SHEET_DATA.
      T_COST_SHEET_DATA-KSTAR1 = TKZU3-KSTAR.
      T_COST_SHEET_DATA-KSCHL1 = T683S-KSCHL.
      T_COST_SHEET_DATA-KVSL1  = T683S-KVSL1.
      APPEND T_COST_SHEET_DATA.

    ENDSELECT.
*    ENDON.
  ENDLOOP.
  DELETE ADJACENT DUPLICATES FROM CREDIT_DATA COMPARING ALL FIELDS.
  IF CREDIT_DATA[] IS  INITIAL.
    MESSAGE E013(/TDK/CB00180) .
  ENDIF.


  LOOP AT T_COST_SHEET_DATA.
*    ON CHANGE OF T_COST_SHEET_DATA-KAGRU.
*Get Costing Sheet "Base" 's Cost Element(Range)
    SELECT *    FROM SETLEAF
       WHERE SETCLASS = '0102'
         AND SUBCLASS = T_KTOPL
         AND SETNAME  = BASE_DATA-KAGRU.
      CHECK SY-SUBRC = 0.
      MOVE-CORRESPONDING  SETLEAF TO WA_SETLEAF.
      APPEND  WA_SETLEAF.
*Get Costing Sheet "Base" 's Cost Element

      SELECT * FROM CSKB
        WHERE KSTAR   >= SETLEAF-VALFROM
          AND KSTAR   <= SETLEAF-VALTO
          AND DATAB   <= LAST_DAY
          AND DATBI   >= LAST_DAY.
        CHECK SY-SUBRC = 0.
        MOVE-CORRESPONDING CSKB TO WA_CSKB.
        APPEND WA_CSKB.
        MOVE-CORRESPONDING T_COST_SHEET_DATA TO COST_SHEET_DATA.
        COST_SHEET_DATA-VALFROM = SETLEAF-VALFROM.
        COST_SHEET_DATA-VALTO = SETLEAF-VALTO.
        COST_SHEET_DATA-KSTAR = CSKB-KSTAR.
        APPEND  COST_SHEET_DATA.
      ENDSELECT.
    ENDSELECT.
*    ENDON.
  ENDLOOP.
  IF WA_SETLEAF[] IS INITIAL.
    MESSAGE E015(/TDK/CB00180) .
  ENDIF.
  IF WA_CSKB[] IS INITIAL.
    MESSAGE E016(/TDK/CB00180) .
  ENDIF.

  IF WA_TKZU3[] IS INITIAL.
    MESSAGE E017(/TDK/CB00180) .
  ENDIF.

  SORT   COST_SHEET_DATA BY KSTAR.
  DELETE ADJACENT DUPLICATES FROM COST_SHEET_DATA .

*Get Yield Data
  SELECT WERKS PLNBEZ VORNR FIX_VALUE PLAN_F_QTY GMNGA XMNGA
         GMEIN YIELD_RATE_C YIELD_RATE_M
  FROM /TDK/CT000102
   INTO CORRESPONDING FIELDS OF TABLE YIELD_LOSS
    WHERE BUKRS = P_BUKRS
      AND WERKS = P_WERKS
      AND KLVAR = P_KLVAR
      AND T_GJAHR = P_GJAHR
      AND T_MONAT = P_MONAT.
  IF YIELD_LOSS[] IS INITIAL.
    MESSAGE E018(/TDK/CB00180) .
  ENDIF.

*Get Material Organization
  LOOP AT COST_DATA.
    WA_MATNR-MATNR = COST_DATA-MATNR.
    APPEND WA_MATNR TO T_MATNR.
    CLEAR WA_MATNR.
  ENDLOOP.
  SORT T_MATNR BY MATNR ASCENDING.
  DELETE ADJACENT DUPLICATES FROM T_MATNR COMPARING MATNR.

  CALL FUNCTION '/TDK/C_MAT_ORG'
    EXPORTING
      I_PATTERN                = '1'
      I_BUKRS                  = P_BUKRS
      IT_MATNR                 = T_MATNR
      I_DATE                   = LAST_DAY
      I_SETNAME                = '0000YSGAT_T'
*     I_BU                     =
   IMPORTING
     ET_MAT_HIER              = T_MAT_HIER
   EXCEPTIONS
     INVALID_MATERIAL         = 1
     INVALID_ITEM_GROUP       = 2
     INVALID_SETNAME          = 3
     INVALID_BU               = 4
     INVALID_SEGMENT          = 5
     INVALID_PARAMETER        = 6
     INVALID_COMPANY          = 7
     OTHERS                   = 8
            .
  IF SY-SUBRC <> 0.
    MESSAGE E019(/TDK/CB00180).
  ENDIF.
**Data Limitation(Material / Organization)
  LOOP AT T_MAT_HIER INTO TH_MAT_HIER.

*    IF ( P_MATNR <> '' OR P_BU <> '' OR P_CBU <> '' OR P_SBU <> '' OR P_GOUP <> '' ).
*      IF  TH_MAT_HIER-ORG_LV3 = P_BU OR TH_MAT_HIER-ORG_LV4 = P_CBU OR TH_MAT_HIER-ORG_LV5 = P_SBU
*          OR TH_MAT_HIER-/EPCOS/TDKKL = P_GOUP OR TH_MAT_HIER-MATNR IN  P_MATNR.
*        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
*        APPEND WA_MAT_HIER.
*      ENDIF.
    IF  P_MATNR <> ''.
      IF  TH_MAT_HIER-MATNR IN P_MATNR.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_BU <> '' .
      IF  TH_MAT_HIER-ORG_LV3 = P_BU.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_CBU <> '' .
      IF  TH_MAT_HIER-ORG_LV4 = P_CBU.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_SBU <> ''.
      IF  TH_MAT_HIER-ORG_LV5 = P_SBU.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_GOUP <> ''.
      IF  TH_MAT_HIER-/EPCOS/TDKKL = P_GOUP.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSE.
      MOVE TH_MAT_HIER TO WA_MAT_HIER.
      APPEND WA_MAT_HIER.
    ENDIF.
  ENDLOOP.
  IF WA_MAT_HIER[] IS INITIAL.
    MESSAGE E020(/TDK/CB00180).
  ENDIF.

ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  DATA_POCESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. DATA_POCESS .
*Assign the Cost Component to Costing Data TBL

  LOOP AT COST_DATA.
    LOOP AT COST_COMPONENT_DATA  WHERE KSTAR = COST_DATA-KSTAR.
      IF SY-SUBRC = 0.
        COST_DATA-ELEMT = COST_COMPONENT_DATA-ELEMT.
        MODIFY COST_DATA.
      ELSE.
        MESSAGE S021(/TDK/CB00180) WITH COST_DATA-MATNR.
        DELETE COST_DATA.
      ENDIF.
    ENDLOOP.
    CLEAR   COST_COMPONENT_DATA.
  ENDLOOP.
  CLEAR  COST_DATA.
  SORT COST_DATA  BY  MATNR VORNR.

*Allocate the Overhead Cost to Operation
  LOOP AT COST_DATA.        "Get the material no.
    MOVE COST_DATA-MATNR TO Z_MATNR.
    APPEND Z_MATNR.
  ENDLOOP.
  DELETE ADJACENT DUPLICATES FROM Z_MATNR COMPARING MATNR.

  LOOP AT Z_MATNR.
    REFRESH Z_TAB.

    LOOP AT COST_DATA WHERE MATNR = Z_MATNR-MATNR AND VORNR <> ''.  "Get the cost data by each material no.
      MOVE-CORRESPONDING COST_DATA TO Z_TAB.
      APPEND Z_TAB.
    ENDLOOP.
    LOOP AT COST_DATA WHERE TYPPS  = 'G' AND MATNR = Z_MATNR-MATNR AND VORNR = ''.
      REFRESH Z_KSTAR.
      REFRESH Z_ALLOCATE.


      T_KSTAR = COST_DATA-KSTAR.
      W_WERTN = COST_DATA-WERTN.
      LOOP AT COST_SHEET_DATA WHERE KSTAR1 = T_KSTAR. "Get the cost sheet data by cost element
        MOVE  COST_SHEET_DATA-KSTAR TO Z_KSTAR.
        APPEND Z_KSTAR.
      ENDLOOP.
      SORT Z_KSTAR BY KSTAR.
      DELETE ADJACENT DUPLICATES FROM Z_KSTAR.
*    endloop.
      IF Z_KSTAR[] IS INITIAL.
*        EXIT.
      ELSE.
        CLEAR Z_TAB .
        CLEAR T_VORNR.
        CLEAR T_WERTN.
        LOOP AT Z_TAB.
          AT END OF VORNR.
            T_VORNR = Z_TAB-VORNR.
            LOOP AT Z_KSTAR.
              LOOP AT Z_TAB WHERE VORNR = T_VORNR AND KSTAR = Z_KSTAR-KSTAR.
                T_WERTN = Z_TAB-WERTN + T_WERTN.
              ENDLOOP.
            ENDLOOP.
            Z_ALLOCATE-KSTAR = T_KSTAR.
            Z_ALLOCATE-VORNR = T_VORNR.
            Z_ALLOCATE-WERTN = T_WERTN.
            APPEND Z_ALLOCATE.
          ENDAT.
        ENDLOOP.
        IF Z_ALLOCATE[] IS INITIAL.
          EXIT.
        ENDIF.
        LOOP AT Z_ALLOCATE.
          T_TABIX = SY-TABIX.
          AT END OF  KSTAR.
            TRY.
                SUM.
                T_TOAL =  Z_ALLOCATE-WERTN.
                LOOP AT Z_ALLOCATE.
                  MOVE-CORRESPONDING COST_DATA TO COST_DATA.
                  COST_DATA-WERTN = W_WERTN * ( Z_ALLOCATE-WERTN / T_TOAL ).
                  COST_DATA-VORNR = Z_ALLOCATE-VORNR.
                  APPEND COST_DATA.
                ENDLOOP.
              CATCH CX_SY_ARITHMETIC_OVERFLOW.
                MESSAGE S022(/TDK/CB00170) WITH Z_MATNR-MATNR.
                DELETE COST_DATA WHERE MATNR = Z_MATNR-MATNR.  "Process Next Produced Material
            ENDTRY.
          ENDAT.
        ENDLOOP.
*        LOOP AT Z_ALLOCATE.
*               MOVE-CORRESPONDING COST_DATA TO COST_DATA.
*
*               COST_DATA-WERTN = W_WERTN * ( Z_ALLOCATE-WERTN / T_TOAL ).
*               COST_DATA-VORNR = Z_ALLOCATE-VORNR.
*               APPEND COST_DATA.
*        ENDLOOP.
        DELETE   COST_DATA WHERE VORNR = '' AND MATNR = Z_MATNR-MATNR AND KSTAR = COST_DATA-KSTAR.
      ENDIF.
    ENDLOOP.

  ENDLOOP.
  SORT COST_DATA BY MATNR DISST.
*Calculate the Material Cost by Operation and Cost Component
**Get On Process Low-Level Code's (Costing Data) Records

  LOOP AT COST_DATA.
    MOVE-CORRESPONDING COST_DATA   TO  Z_LOW_LEVEL.
    APPEND Z_LOW_LEVEL.
  ENDLOOP.
  DELETE ADJACENT DUPLICATES FROM Z_LOW_LEVEL.
  SORT Z_LOW_LEVEL BY DISST DESCENDING.

*Classification and Cost Component relation

  RELAT_TAB-TXT = 'Variable Cost'.
  RELAT_TAB-ELEMT = '10'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Variable Cost'.
  RELAT_TAB-ELEMT = '20'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Variable Cost'.
  RELAT_TAB-ELEMT = '30'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Variable Cost'.
  RELAT_TAB-ELEMT = '40'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Variable Cost'.
  RELAT_TAB-ELEMT = '50'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = ''.
  RELAT_TAB-ELEMT = '60'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Fix Direct Cost'.
  RELAT_TAB-ELEMT = '70'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Fix Direct Cost'.
  RELAT_TAB-ELEMT = '80'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Fix Direct Cost'.
  RELAT_TAB-ELEMT = '90'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Fix Direct Cost'.
  RELAT_TAB-ELEMT = '100'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Fix Direct Cost'.
  RELAT_TAB-ELEMT = '110'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = 'Fix Indirect Cost'.
  RELAT_TAB-ELEMT = '120'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = ''.
  RELAT_TAB-ELEMT = '130'.
  APPEND RELAT_TAB.
  RELAT_TAB-TXT = ''.
  RELAT_TAB-ELEMT = '140'.
  APPEND RELAT_TAB.


**Get On Process Operation/Activity's (Costing Data) Records
  LOOP AT Z_LOW_LEVEL.
    REFRESH ITAB.
    REFRESH Z_OPERATION.
    REFRESH PRE_TAB.

*    READ TABLE Z_LOW_LEVEL INDEX 1.  "get the NO.1 item
*    T_MATEIRAL = Z_LOW_LEVEL-MATNR.
    LOOP AT COST_DATA WHERE MATNR = Z_LOW_LEVEL-MATNR  AND DISST = Z_LOW_LEVEL-DISST
*                         AND TYPPS  <> 'G' AND VORNR <> ''.
                          AND VORNR <> ''.
      MOVE-CORRESPONDING COST_DATA TO ITAB.
      MOVE-CORRESPONDING COST_DATA TO Z_OPERATION.
      APPEND ITAB.
      APPEND Z_OPERATION.
    ENDLOOP.

    SORT Z_OPERATION BY VORNR.
    SORT ITAB BY VORNR.
    DELETE ADJACENT DUPLICATES FROM Z_OPERATION.
    DELETE ADJACENT DUPLICATES FROM ITAB.

**Own Process (Accumulated) Cost Calculation
    LOOP AT Z_OPERATION.
      REFRESH ITAB1.
      REFRESH T_WORK_TAB.

      T_TABIX = SY-TABIX.
      LOOP AT ITAB  WHERE VORNR = Z_OPERATION-VORNR.
        MOVE-CORRESPONDING ITAB TO ITAB1.
        APPEND ITAB1.
      ENDLOOP.
***Get Previous Process Cost
*      IF PRE_TAB[] IS INITIAL.
*        MESSAGE S023(/TDK/CB00180) WITH Z_OPERATION-MATNR Z_OPERATION-VORNR.
*      ENDIF.


      LOOP AT ITAB1 WHERE BAUGR <> 'X'.
        MOVE-CORRESPONDING ITAB1 TO T_WORK_TAB.
        LOOP AT RELAT_TAB WHERE ELEMT = ITAB1-ELEMT.
          IF RELAT_TAB-TXT <> ''.
            T_WORK_TAB-TXT =  RELAT_TAB-TXT.
          ENDIF.
        ENDLOOP.
        APPEND T_WORK_TAB.
      ENDLOOP.
      SORT T_WORK_TAB BY TXT.
      LOOP AT  T_WORK_TAB.
        L_TABIX = SY-TABIX.
        AT END OF  TXT.
          READ TABLE T_WORK_TAB INDEX L_TABIX.
          MOVE-CORRESPONDING T_WORK_TAB TO WORK_TAB.
          TRY.
              SUM.
              WORK_TAB-WERTN = T_WORK_TAB-WERTN.
              APPEND WORK_TAB.
            CATCH CX_SY_ARITHMETIC_OVERFLOW.
              MESSAGE S026(/TDK/CB00180) WITH T_WORK_TAB-MATNR.
              DELETE T_WORK_TAB WHERE MATNR = T_WORK_TAB-MATNR.  "Process Next Produced Material
          ENDTRY.
        ENDAT.
      ENDLOOP.
*      READ TABLE Z_LOW_LEVEL INDEX 1.  "get the NO.1 item
*      T_MATEIRAL = Z_LOW_LEVEL-MATNR.
*
*      IF Z_OPERATION-MATNR = T_MATEIRAL.
*      LOOP AT  ITAB WHERE BAUGR = 'X' AND MATNR = Z_OPERATION-MATNR.
*      ENDLOOP.
      READ TABLE ITAB WITH  KEY BAUGR = 'X'  MATNR = Z_OPERATION-MATNR.
      IF SY-SUBRC <> 0.
*        ON CHANGE OF Z_OPERATION-MATNR.
*          MESSAGE S024(/TDK/CB00180) WITH Z_OPERATION-MATNR.
*        ENDON.
*Calculate Yield Loss Amount
        READ TABLE Z_OPERATION INDEX 1 .
        S_VORNR = Z_OPERATION-VORNR.

        READ TABLE Z_OPERATION INDEX T_TABIX.
        LOOP AT WORK_TAB WHERE VORNR = Z_OPERATION-VORNR AND MATNR = Z_OPERATION-MATNR.

*        READ TABLE WORK_TAB INDEX 1 .
*        S_VORNR = WORK_TAB-VORNR.
*        LOOP AT WORK_TAB WHERE VORNR = Z_OPERATION-VORNR .

          IF  WORK_TAB-VORNR  = S_VORNR.
            SELECT SINGLE * FROM /TDK/CT000102
              WHERE PLNBEZ = WORK_TAB-MATNR
              AND   WERKS = WORK_TAB-WERKS
              AND   VORNR = WORK_TAB-VORNR
              AND   BUKRS = P_BUKRS
              AND   T_GJAHR = P_GJAHR
              AND   T_MONAT = P_MONAT
              AND   KLVAR = P_KLVAR .
            IF SY-SUBRC = 0.
              IF /TDK/CT000102-FIX_VALUE = 'Maintained'.
                IF /TDK/CT000102-YIELD_RATE_M <> ''.
                  TRY.
                      WORK_TAB-WERTN1 = WORK_TAB-WERTN / /TDK/CT000102-YIELD_RATE_M - WORK_TAB-WERTN.
                      WORK_TAB-YIELD_RATE = /TDK/CT000102-YIELD_RATE_M.
                      MODIFY  WORK_TAB.
                    CATCH CX_SY_ARITHMETIC_OVERFLOW.
                      MESSAGE S027(/TDK/CB00180) WITH WORK_TAB-MATNR.
                      DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.   "Process Next Produced
                  ENDTRY.
                ELSE.
                  MESSAGE S029(/TDK/CB00180) WITH WORK_TAB-MATNR.
                  DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.       "Process Next Produced
                ENDIF.
              ELSE.
                IF /TDK/CT000102-YIELD_RATE_C <> ''.
                  TRY.
                      WORK_TAB-WERTN1 = WORK_TAB-WERTN / /TDK/CT000102-YIELD_RATE_C - WORK_TAB-WERTN.
                      WORK_TAB-YIELD_RATE = /TDK/CT000102-YIELD_RATE_C.
                      MODIFY  WORK_TAB.
                    CATCH CX_SY_ARITHMETIC_OVERFLOW.
                      MESSAGE S027(/TDK/CB00180) WITH WORK_TAB-MATNR.
                      DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.   "Process Next Produced
                  ENDTRY.
                ELSE.
                  MESSAGE S029(/TDK/CB00180) WITH WORK_TAB-MATNR.
                  DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.       "Process Next Produced
                ENDIF.
              ENDIF.
*            MODIFY  WORK_TAB.
            ELSE.
              WORK_TAB-YIELD_RATE = 1.
              MODIFY  WORK_TAB.
              ON CHANGE OF WORK_TAB-MATNR.
                MESSAGE S028(/TDK/CB00180) WITH WORK_TAB-MATNR WORK_TAB-VORNR.
*              EXIT.
              ENDON.
            ENDIF.
          ELSE.

            LOOP AT PRE_TAB WHERE TXT = WORK_TAB-TXT.
              TRY.
                  WORK_TAB-WERTN =  PRE_TAB-WERTN + WORK_TAB-WERTN + PRE_TAB-WERTN1.
                CATCH CX_SY_ARITHMETIC_OVERFLOW.
                  MESSAGE S026(/TDK/CB00180) WITH WORK_TAB-MATNR.
                  DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.     "Process Next Produced Material
                  EXIT.
              ENDTRY.
              SELECT SINGLE * FROM /TDK/CT000102
                 WHERE PLNBEZ = WORK_TAB-MATNR
                 AND   WERKS = WORK_TAB-WERKS
                 AND   VORNR = WORK_TAB-VORNR
                 AND   BUKRS = P_BUKRS
                 AND   T_GJAHR = P_GJAHR
                 AND   T_MONAT = P_MONAT
                 AND   KLVAR = P_KLVAR .
              IF SY-SUBRC = 0.
                IF /TDK/CT000102-FIX_VALUE = 'Maintained'.
                  IF /TDK/CT000102-YIELD_RATE_M <> ''.
                    TRY.
                        WORK_TAB-WERTN1 = WORK_TAB-WERTN / /TDK/CT000102-YIELD_RATE_M - WORK_TAB-WERTN.
                        WORK_TAB-YIELD_RATE = /TDK/CT000102-YIELD_RATE_M.
                        MODIFY  WORK_TAB.
                      CATCH CX_SY_ARITHMETIC_OVERFLOW.
                        MESSAGE S027(/TDK/CB00180) WITH WORK_TAB-MATNR.
                        DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.     "Process Next Produced Material
                        EXIT.
                    ENDTRY.
                  ELSE.
                    MESSAGE S029(/TDK/CB00180) WITH WORK_TAB-MATNR.
                    DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.         "Process Next Produced Material
                    EXIT.
                  ENDIF.
                ELSE.
                  IF /TDK/CT000102-YIELD_RATE_C <> ''.
                    TRY.
                        WORK_TAB-WERTN1 = WORK_TAB-WERTN / /TDK/CT000102-YIELD_RATE_C - WORK_TAB-WERTN.
                        WORK_TAB-YIELD_RATE = /TDK/CT000102-YIELD_RATE_C.
                        MODIFY  WORK_TAB.
                      CATCH CX_SY_ARITHMETIC_OVERFLOW.
                        MESSAGE S027(/TDK/CB00180) WITH WORK_TAB-MATNR.
                        DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.     "Process Next Produced Material
                        EXIT.
                    ENDTRY.
                  ELSE.
                    MESSAGE S029(/TDK/CB00180) WITH WORK_TAB-MATNR.
                    DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.         "Process Next Produced Material
                    EXIT.
                  ENDIF.
                ENDIF.
*              MODIFY WORK_TAB.
              ELSE.
                WORK_TAB-YIELD_RATE = 1.
                MODIFY  WORK_TAB.
                ON CHANGE OF WORK_TAB-MATNR.
                  MESSAGE S028(/TDK/CB00180) WITH WORK_TAB-MATNR WORK_TAB-VORNR.
*                  EXIT.
                ENDON.
              ENDIF.
            ENDLOOP.
          ENDIF.
        ENDLOOP.
        REFRESH PRE_TAB.

        LOOP AT  WORK_TAB WHERE VORNR = Z_OPERATION-VORNR AND MATNR = Z_OPERATION-MATNR .
          MOVE-CORRESPONDING WORK_TAB TO PRE_TAB.
          APPEND PRE_TAB.
        ENDLOOP.
      ELSE.
        REFRESH SF_TAB.
        SORT WORK_TAB BY MATNR VORNR DESCENDING.
*Semi-Finished Material Cost Calculation
        LOOP AT COST_DATA WHERE BAUGR = 'X' AND MATNR = Z_OPERATION-MATNR AND VORNR = Z_OPERATION-VORNR.
          LOOP AT WORK_TAB WHERE MATNR = COST_DATA-MATNR1.
            W_VORNR = WORK_TAB-VORNR.
            EXIT.
          ENDLOOP.
        ENDLOOP.
*        READ TABLE WORK_TAB INDEX 1.
*        W_VORNR = WORK_TAB-VORNR.
        LOOP AT COST_DATA WHERE BAUGR = 'X' AND MATNR = Z_OPERATION-MATNR AND VORNR = Z_OPERATION-VORNR.
          LOOP AT WORK_TAB WHERE MATNR = COST_DATA-MATNR1 AND VORNR =  W_VORNR.
            TRY.
*                WORK_TAB-WERTN = WORK_TAB-WERTN / WORK_TAB-LOSGR * COST_DATA-MENGE.
*                WORK_TAB-WERTN1 = WORK_TAB-WERTN1 / WORK_TAB-LOSGR * COST_DATA-MENGE.
*                MODIFY WORK_TAB.
*                MOVE-CORRESPONDING WORK_TAB TO SF_TAB.
*                APPEND SF_TAB.
                MOVE-CORRESPONDING WORK_TAB TO SF_TAB.
                SF_TAB-WERTN = WORK_TAB-WERTN / WORK_TAB-LOSGR * COST_DATA-MENGE.
                SF_TAB-WERTN1 = WORK_TAB-WERTN1 / WORK_TAB-LOSGR * COST_DATA-MENGE.
                APPEND SF_TAB.
              CATCH CX_SY_ARITHMETIC_OVERFLOW.
                MESSAGE S025(/TDK/CB00180) WITH WORK_TAB-MATNR.
                DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.          "Process Next Produced Material
            ENDTRY.
          ENDLOOP.
        ENDLOOP.
*       LOOP AT WORK_TAB WHERE MATNR = Z_OPERATION-MATNR.
        LOOP AT WORK_TAB WHERE MATNR = Z_OPERATION-MATNR AND VORNR = Z_OPERATION-VORNR.
          LOOP AT SF_TAB WHERE TXT = WORK_TAB-TXT .
            TRY.
                WORK_TAB-WERTN = WORK_TAB-WERTN + SF_TAB-WERTN + SF_TAB-WERTN1.
                MODIFY WORK_TAB.
              CATCH CX_SY_ARITHMETIC_OVERFLOW.
                MESSAGE S026(/TDK/CB00180) WITH WORK_TAB-MATNR.
                DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.           "Process Next Produced Material
                EXIT.
            ENDTRY.

            SELECT SINGLE * FROM /TDK/CT000102
                  WHERE PLNBEZ = WORK_TAB-MATNR
                  AND   WERKS = WORK_TAB-WERKS
                  AND   VORNR = WORK_TAB-VORNR
                  AND   BUKRS = P_BUKRS
                  AND   T_GJAHR = P_GJAHR
                  AND   T_MONAT = P_MONAT
                  AND   KLVAR = P_KLVAR .
            IF SY-SUBRC = 0.
              IF /TDK/CT000102-FIX_VALUE = 'Maintained'.
                IF /TDK/CT000102-YIELD_RATE_M <> ''.
                  TRY.
                      WORK_TAB-WERTN1 = WORK_TAB-WERTN / /TDK/CT000102-YIELD_RATE_M - WORK_TAB-WERTN.
                      WORK_TAB-YIELD_RATE = /TDK/CT000102-YIELD_RATE_M.
                      MODIFY WORK_TAB.
                    CATCH CX_SY_ARITHMETIC_OVERFLOW.
                      MESSAGE S027(/TDK/CB00180) WITH WORK_TAB-MATNR.
                      DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.           "Process Next Produced Material
                      EXIT.
                  ENDTRY.

                ELSE.
                  MESSAGE S029(/TDK/CB00180) WITH WORK_TAB-MATNR.
                  DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.                "Process Next Produced Material
                  EXIT.
                ENDIF.
              ELSE.
                IF /TDK/CT000102-YIELD_RATE_C <> ''.
                  TRY.
                      WORK_TAB-WERTN1 = WORK_TAB-WERTN / /TDK/CT000102-YIELD_RATE_C - WORK_TAB-WERTN.
                      WORK_TAB-YIELD_RATE = /TDK/CT000102-YIELD_RATE_C.
                      MODIFY WORK_TAB.
                    CATCH CX_SY_ARITHMETIC_OVERFLOW.
                      MESSAGE S027(/TDK/CB00180) WITH WORK_TAB-MATNR.
                      DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.           "Process Next Produced Material
                      EXIT.
                  ENDTRY.
                ELSE.
                  MESSAGE S029(/TDK/CB00180) WITH WORK_TAB-MATNR.
                  DELETE WORK_TAB WHERE MATNR = WORK_TAB-MATNR.               "Process Next Produced Material
                  EXIT.

                ENDIF.
              ENDIF.
*              MODIFY WORK_TAB.
            ELSE.
              WORK_TAB-YIELD_RATE = 1.
              MODIFY  WORK_TAB.
              ON CHANGE OF WORK_TAB-MATNR.
                MESSAGE S028(/TDK/CB00180) WITH WORK_TAB-MATNR WORK_TAB-VORNR.
*                EXIT.
              ENDON.
            ENDIF.


          ENDLOOP.
        ENDLOOP.
      ENDIF.
    ENDLOOP.
  ENDLOOP.

*Data Limitation
  LOOP AT WORK_TAB .
    READ TABLE WA_MAT_HIER WITH KEY MATNR = WORK_TAB-MATNR .
*    LOOP AT  WA_MAT_HIER WHERE  MATNR = WORK_TAB-MATNR .
*    ENDLOOP.
    IF SY-SUBRC <> 0.
      DELETE WORK_TAB.
    ENDIF.
  ENDLOOP.

  LOOP AT WORK_TAB.
    AT END OF VORNR.
      MOVE-CORRESPONDING WORK_TAB TO OUTPUT_TAB.
      OUTPUT_TAB-BUKRS = P_BUKRS.
      OUTPUT_TAB-GJAHR = P_GJAHR.
      OUTPUT_TAB-MONAT = P_MONAT.
      OUTPUT_TAB-KLVAR = P_KLVAR.
      OUTPUT_TAB-TVERS = P_TVERS.
      APPEND OUTPUT_TAB.
    ENDAT.
  ENDLOOP.
  LOOP AT  OUTPUT_TAB.
    LOOP AT  WORK_TAB WHERE MATNR = OUTPUT_TAB-MATNR AND  VORNR = OUTPUT_TAB-VORNR.
      IF WORK_TAB-TXT = 'Variable Cost'.
        OUTPUT_TAB-VAR_CO = WORK_TAB-WERTN.
        OUTPUT_TAB-VAR_YL = WORK_TAB-WERTN1.
      ELSEIF WORK_TAB-TXT = 'Fix Direct Cost'.
        OUTPUT_TAB-DIR_CO = WORK_TAB-WERTN.
        OUTPUT_TAB-DIR_YL = WORK_TAB-WERTN1.
      ELSEIF  WORK_TAB-TXT = 'Fix Indirect Cost'.
        OUTPUT_TAB-IND_CO = WORK_TAB-WERTN.
        OUTPUT_TAB-IND_YL = WORK_TAB-WERTN1.
      ENDIF.
      OUTPUT_TAB-YIELD_RATE = WORK_TAB-YIELD_RATE.
      MODIFY OUTPUT_TAB.
    ENDLOOP.
  ENDLOOP.

*Add-on TBL Deletion Process
  LOOP AT WA_MAT_HIER.
    SELECT  * FROM /TDK/CT000103
      WHERE BUKRS = P_BUKRS
          AND WERKS = P_WERKS
          AND KLVAR = P_KLVAR
          AND GJAHR = P_GJAHR
          AND MONAT = P_MONAT
          AND MATNR = WA_MAT_HIER-MATNR.
*          AND VORNR = OUTPUT_TAB-VORNR.

      IF SY-SUBRC = 0.
        DELETE /TDK/CT000103 .
        T_COUNT_D = T_COUNT_D + 1.
      ENDIF.
    ENDSELECT.
  ENDLOOP.

 

*Add-on TBL Updating Process
  LOOP AT OUTPUT_TAB.

    CALL FUNCTION 'ENQUEUE_/TDK/ECT000103'
      EXPORTING
        MODE_/TDK/CT000103 = 'E'
        BUKRS              = P_BUKRS
        WERKS              = P_WERKS
        GJAHR              = P_GJAHR
        MONAT              = P_MONAT
        MATNR              = OUTPUT_TAB-MATNR
        KLVAR              = P_KLVAR.
    IF SY-SUBRC <> 0.
      MESSAGE E030(/TDK/CB00180).
    ENDIF.

    MOVE-CORRESPONDING OUTPUT_TAB TO /TDK/CT000103.
    T_COUNT_S = T_COUNT_S + 1.
    INSERT /TDK/CT000103.

    CALL FUNCTION 'DEQUEUE_/TDK/ECT000103'
      EXPORTING
        MODE_/TDK/CT000103 = 'E'
        BUKRS              = P_BUKRS
        WERKS              = P_WERKS
        GJAHR              = P_GJAHR
        MONAT              = P_MONAT
        MATNR              = OUTPUT_TAB-MATNR
        KLVAR              = P_KLVAR.

  ENDLOOP.
ENDFORM.                    " DATA_POCESS
*&---------------------------------------------------------------------*
*&      Form  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. OUTPUT .
  IF R1 = 'X'.
    WRITE:/ 'Updated for Add-On TBL'.
    WRITE: / T_COUNT_S, 'records are successfully added'.
    WRITE: / T_COUNT_D,' records are successfully deleted'.
  ENDIF.
  IF R2 = 'X'.
    WRITE:/ 'Updated for Additive Cost'.
    WRITE: / L_COUNT_S, 'records are successfully added'.
  ENDIF.
ENDFORM.                    " OUTPUT
*&---------------------------------------------------------------------*
*&      Form  POCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. POCESS_DATA .
  IF P_MATNR <> ''.
    SELECT BUKRS WERKS GJAHR MONAT MATNR  KLVAR TVERS LOSGR MEINS VORNR
           VAR_YL DIR_YL IND_YL
    INTO CORRESPONDING FIELDS OF TABLE YL_TAB
    FROM /TDK/CT000103
    WHERE  BUKRS = P_BUKRS
      AND  WERKS = P_WERKS
      AND GJAHR = P_GJAHR
      AND MONAT = P_MONAT
      AND KLVAR = P_KLVAR
      AND TVERS = P_TVERS
      AND MATNR IN P_MATNR.
  ELSE.
    SELECT BUKRS WERKS GJAHR MONAT MATNR  KLVAR TVERS LOSGR MEINS VORNR
        VAR_YL DIR_YL IND_YL
 INTO CORRESPONDING FIELDS OF TABLE YL_TAB
 FROM /TDK/CT000103
 WHERE  BUKRS = P_BUKRS
   AND  WERKS = P_WERKS
   AND  GJAHR = P_GJAHR
   AND  MONAT = P_MONAT
   AND  KLVAR = P_KLVAR
   AND  TVERS = P_TVERS.
  ENDIF.
  IF SY-SUBRC <> 0.
    MESSAGE E032(/TDK/CB00180).
  ENDIF.

*Get Material Organization
  LOOP AT YL_TAB.
    WA_MATNR-MATNR = YL_TAB-MATNR.
    APPEND WA_MATNR TO T_MATNR.
    CLEAR WA_MATNR.
  ENDLOOP.
  SORT T_MATNR BY MATNR ASCENDING.
  DELETE ADJACENT DUPLICATES FROM T_MATNR COMPARING MATNR.

  CALL FUNCTION '/TDK/C_MAT_ORG'
    EXPORTING
      I_PATTERN                = '1'
      I_BUKRS                  = P_BUKRS
      IT_MATNR                 = T_MATNR
      I_DATE                   = LAST_DAY
      I_SETNAME                = '0000YSGAT_T'
*     I_BU                     =
   IMPORTING
     ET_MAT_HIER              = T_MAT_HIER
   EXCEPTIONS
     INVALID_MATERIAL         = 1
     INVALID_ITEM_GROUP       = 2
     INVALID_SETNAME          = 3
     INVALID_BU               = 4
     INVALID_SEGMENT          = 5
     INVALID_PARAMETER        = 6
     INVALID_COMPANY          = 7
     OTHERS                   = 8
            .
  IF SY-SUBRC <> 0.
    MESSAGE E033(/TDK/CB00180).
  ENDIF.
**Data Limitation(Material / Organization)
  LOOP AT T_MAT_HIER INTO TH_MAT_HIER.

*    IF ( P_MATNR <> '' OR P_BU <> '' OR P_CBU <> '' OR P_SBU <> '' OR P_GOUP <> '' ).
*      IF  TH_MAT_HIER-ORG_LV3 = P_BU OR TH_MAT_HIER-ORG_LV4 = P_CBU OR TH_MAT_HIER-ORG_LV5 = P_SBU
*          OR TH_MAT_HIER-/EPCOS/TDKKL = P_GOUP OR TH_MAT_HIER-MATNR IN P_MATNR.
*        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
*        APPEND WA_MAT_HIER.
*      ENDIF.
    IF  P_MATNR <> ''.
      IF  TH_MAT_HIER-MATNR IN P_MATNR.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_BU <> '' .
      IF  TH_MAT_HIER-ORG_LV3 = P_BU.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_CBU <> '' .
      IF  TH_MAT_HIER-ORG_LV4 = P_CBU.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_SBU <> ''.
      IF  TH_MAT_HIER-ORG_LV5 = P_SBU.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSEIF P_GOUP <> ''.
      IF  TH_MAT_HIER-/EPCOS/TDKKL = P_GOUP.
        MOVE-CORRESPONDING  TH_MAT_HIER TO WA_MAT_HIER.
        APPEND WA_MAT_HIER.
      ENDIF.
    ELSE.
      MOVE TH_MAT_HIER TO WA_MAT_HIER.
      APPEND WA_MAT_HIER.
    ENDIF.

  ENDLOOP.
  IF WA_MAT_HIER[] IS INITIAL.
    MESSAGE E034(/TDK/CB00180).
  ENDIF.

*  LOOP AT WA_MAT_HIER.
*    LOOP AT  YL_TAB WHERE MATNR = WA_MAT_HIER-MATNR.
*    ENDLOOP.
  LOOP AT YL_TAB.
    READ TABLE WA_MAT_HIER WITH KEY MATNR = YL_TAB-MATNR..
    IF SY-SUBRC <> 0.
      DELETE YL_TAB.
    ENDIF.
  ENDLOOP.
  IF YL_TAB[] IS INITIAL.
    MESSAGE E035(/TDK/CB00180).
  ENDIF.

*Create BDCDATA for Additive Cost Batch Input
  LOOP AT YL_TAB.
    CLEAR: C_YL,C_PEINH ,C_LOSGR ,T_YL.

    SELECT SINGLE PEINH  INTO T_PEINH FROM MBEW
    WHERE MATNR = YL_TAB-MATNR
      AND BWKEY =  YL_TAB-WERKS.
    IF SY-SUBRC <> 0.
      MESSAGE E036(/TDK/CB00180).
    ENDIF .

    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
      EXPORTING
        INPUT    = YL_TAB-MEINS
        LANGUAGE = SY-LANGU
      IMPORTING
        OUTPUT   = T_MEINS.

    CALL FUNCTION 'BAPI_CURRENCY_GETDECIMALS'
      EXPORTING
        CURRENCY          = T_WAERS
      IMPORTING
        CURRENCY_DECIMALS = LF_BAPI1090_1.

    IF LF_BAPI1090_1-CURDECIMALS = 0.
      T_YL = YL_TAB-VAR_YL.
      C_YL = T_YL.
      C_PEINH = T_PEINH.
      C_LOSGR = YL_TAB-LOSGR.

      IF YL_TAB-VAR_YL <> ''.
        YL_BDC-KLVAR = YL_TAB-KLVAR.
        YL_BDC-MATNR = YL_TAB-MATNR.
        YL_BDC-WERKS = YL_TAB-WERKS.
        YL_BDC-TVERS = YL_TAB-TVERS.
        YL_BDC-LOSGR = C_LOSGR.
        YL_BDC-MEINS = T_MEINS.
        YL_BDC-VORNR = YL_TAB-VORNR.
        YL_BDC-PEINH = C_PEINH.
        YL_BDC-YL_LOSS = C_YL.
        YL_BDC-KSTAR = 'S3VD060000'.
        APPEND YL_BDC.
      ENDIF.
      IF YL_TAB-DIR_YL <> ''.
        T_YL = YL_TAB-DIR_YL.
        C_YL = T_YL.

        YL_BDC-KLVAR = YL_TAB-KLVAR.
        YL_BDC-MATNR = YL_TAB-MATNR.
        YL_BDC-WERKS = YL_TAB-WERKS.
        YL_BDC-TVERS = YL_TAB-TVERS.
        YL_BDC-LOSGR = C_LOSGR.
        YL_BDC-MEINS = T_MEINS.
        YL_BDC-VORNR = YL_TAB-VORNR.
        YL_BDC-PEINH = C_PEINH.
        YL_BDC-YL_LOSS = C_YL.
        YL_BDC-KSTAR = 'S3FD130000'.
        APPEND YL_BDC.
      ENDIF.
      IF YL_TAB-IND_YL <> ''.
        T_YL = YL_TAB-IND_YL.
        C_YL = T_YL.

        YL_BDC-KLVAR = YL_TAB-KLVAR.
        YL_BDC-MATNR = YL_TAB-MATNR.
        YL_BDC-WERKS = YL_TAB-WERKS.
        YL_BDC-TVERS = YL_TAB-TVERS.
        YL_BDC-LOSGR = C_LOSGR.
        YL_BDC-MEINS = T_MEINS.
        YL_BDC-VORNR = YL_TAB-VORNR.
        YL_BDC-PEINH = C_PEINH.
        YL_BDC-YL_LOSS = C_YL.
        YL_BDC-KSTAR = 'S3FI140000'.
        APPEND YL_BDC.
      ENDIF.

    ELSE.

      C_PEINH = T_PEINH.
      C_LOSGR = YL_TAB-LOSGR.

      IF YL_TAB-VAR_YL <> ''.
        C_YL = YL_TAB-VAR_YL.
        YL_BDC-KLVAR = YL_TAB-KLVAR.
        YL_BDC-MATNR = YL_TAB-MATNR.
        YL_BDC-WERKS = YL_TAB-WERKS.
        YL_BDC-TVERS = YL_TAB-TVERS.
        YL_BDC-LOSGR = C_LOSGR.
        YL_BDC-MEINS = T_MEINS.
        YL_BDC-VORNR = YL_TAB-VORNR.
        YL_BDC-PEINH = C_PEINH.
        YL_BDC-YL_LOSS = C_YL.
        YL_BDC-KSTAR = 'S3VD060000'.
        APPEND YL_BDC.
      ENDIF.
      IF YL_TAB-DIR_YL <> ''.
        C_YL = YL_TAB-DIR_YL.
        YL_BDC-KLVAR = YL_TAB-KLVAR.
        YL_BDC-MATNR = YL_TAB-MATNR.
        YL_BDC-WERKS = YL_TAB-WERKS.
        YL_BDC-TVERS = YL_TAB-TVERS.
        YL_BDC-LOSGR = C_LOSGR.
        YL_BDC-MEINS = T_MEINS.
        YL_BDC-VORNR = YL_TAB-VORNR.
        YL_BDC-PEINH = C_PEINH.
        YL_BDC-YL_LOSS = C_YL.
        YL_BDC-KSTAR = 'S3FD130000'.
        APPEND YL_BDC.
      ENDIF.
      IF YL_TAB-IND_YL <> ''.
        C_YL = YL_TAB-IND_YL.
        YL_BDC-KLVAR = YL_TAB-KLVAR.
        YL_BDC-MATNR = YL_TAB-MATNR.
        YL_BDC-WERKS = YL_TAB-WERKS.
        YL_BDC-TVERS = YL_TAB-TVERS.
        YL_BDC-LOSGR = C_LOSGR.
        YL_BDC-MEINS = T_MEINS.
        YL_BDC-VORNR = YL_TAB-VORNR.
        YL_BDC-PEINH = C_PEINH.
        YL_BDC-YL_LOSS = C_YL.
        YL_BDC-KSTAR = 'S3FI140000'.
        APPEND YL_BDC.
      ENDIF.

    ENDIF.
  ENDLOOP.

  DATA: L_MODE TYPE C.
  MOVE 'N' TO L_MODE.

  LOOP AT YL_BDC.

    L_TABIX = SY-TABIX.
    AT NEW  MATNR.
      REFRESH T_BDC.
      CLEAR T_BDC.

      READ TABLE YL_BDC INDEX L_TABIX.

      PERFORM. BDC_DYNPRO      USING 'SAPLKKP4' '1000'.
      PERFORM. BDC_FIELD       USING 'BDC_CURSOR'
                                    'CKI64A-KLVAR'.
      PERFORM. BDC_FIELD       USING 'BDC_OKCODE'
                                    '/00'.
      PERFORM. BDC_FIELD       USING 'CKI64A-KLVAR'
                                    YL_BDC-KLVAR.
      PERFORM. BDC_FIELD       USING 'CKI64A-MATNR'
                                    YL_BDC-MATNR.
      PERFORM. BDC_FIELD       USING 'CKI64A-WERKS'
                                    YL_BDC-WERKS.
      PERFORM. BDC_FIELD       USING 'CKI64A-TVERS'
                                    YL_BDC-TVERS.
      PERFORM. BDC_FIELD       USING 'CKI64A-KADAT'
*                                    FIRST_DAY.
                                     T_FIRST_DAY.
      PERFORM. BDC_FIELD       USING 'CKI64A-BIDAT'
*                                    LAST_DAY.
                                     T_LAST_DAY.
*      PERFORM. BDC_FIELD       USING 'KKPP-REF_TVERS'
*                                    RECORD-REF_TVERS_007.
*      PERFORM. BDC_FIELD       USING 'KKPP-REF_KADAT'
*                                    RECORD-REF_KADAT_008.
*      PERFORM. BDC_FIELD       USING 'KKPP-REF_BIDAT'
*                                    RECORD-REF_BIDAT_009.

      SELECT SINGLE * FROM KEKO
        WHERE MATNR = YL_BDC-MATNR
          AND KALKA = T_KALKA
          AND TVERS = P_TVERS
          AND KKZMA = 'X'
          AND BWVAR = T_BWVAR
          AND KADKY = FIRST_DAY.
*          AND KADAT = FIRST_DAY
*          AND BIDAT = LAST_DAY.
      IF SY-SUBRC = 0.

        PERFORM. BDC_DYNPRO      USING 'SAPLKKP4' '1100'.
        PERFORM. BDC_FIELD       USING 'BDC_CURSOR'
                                      '%#AUTOTEXT001'.
        PERFORM. BDC_FIELD       USING 'BDC_OKCODE'
                                      '=CONT'.
      ENDIF.

      PERFORM. BDC_DYNPRO      USING 'SAPLKKP4' '0400'.
      PERFORM. BDC_FIELD       USING 'BDC_CURSOR'
                                    'CKI64A-BWDAT'.
      PERFORM. BDC_FIELD       USING 'BDC_OKCODE'
                                    '=CONT'.
      PERFORM. BDC_FIELD       USING 'CKI64A-BWDAT'
*                                    FIRST_DAY.
                                     T_FIRST_DAY.

    ENDAT.

    PERFORM. BDC_DYNPRO      USING 'SAPLKKDI' '1301'.
    PERFORM. BDC_FIELD       USING 'BDC_CURSOR'
                                  'RK70L-KSTAR(01)'.
    PERFORM. BDC_FIELD       USING 'BDC_OKCODE'
                                  '/00'.
*    PERFORM. BDC_FIELD       USING 'RK70E-LST_SETNR'
*                                  RECORD-LST_SETNR_011.
    PERFORM. BDC_FIELD       USING 'RK70L-TYPPS(01)'
                                  'V'.
    PERFORM. BDC_FIELD       USING 'RK70L-MENGE(01)'
*                                  YL_BDC-LOSGR.
                                   YL_BDC-PEINH.
    PERFORM. BDC_FIELD       USING 'RK70L-MEEHT(01)'
                                  YL_BDC-MEINS.
    PERFORM. BDC_FIELD       USING 'RK70L-LTEXT(01)'
                                  YL_BDC-VORNR.
    PERFORM. BDC_FIELD       USING 'RK70L-LPEINH(01)'
                                  YL_BDC-PEINH.
    PERFORM. BDC_FIELD       USING 'RK70L-LPREIS(01)'
                                  YL_BDC-YL_LOSS.
    PERFORM. BDC_FIELD       USING 'RK70L-KSTAR(01)'
                                  YL_BDC-KSTAR.

    PERFORM. BDC_DYNPRO      USING 'SAPLKKDI' '1301'.
    PERFORM. BDC_FIELD       USING 'BDC_CURSOR'
                                  'RK70L-TYPPS(01)'.
    PERFORM. BDC_FIELD       USING 'BDC_OKCODE'
                                  '=PINS'.
*    PERFORM. BDC_FIELD       USING 'RK70E-LST_SETNR'
*                                  RECORD-LST_SETNR_018.

    AT END OF  MATNR.
      PERFORM. BDC_FIELD       USING 'BDC_OKCODE'
                              '=SICH'.

      CALL TRANSACTION 'CK74'
                        USING    T_BDC
                        MODE    L_MODE
                        UPDATE  'S'
                        MESSAGES INTO T_MSG.

      IF SY-SUBRC <> 0.
        MESSAGE E037(/TDK/CB00180).
      ELSE.
        L_COUNT_S = L_COUNT_S + 1.
      ENDIF.
    ENDAT.

  ENDLOOP.

ENDFORM.                    " POCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_3289   text
*      -->P_3290   text
*----------------------------------------------------------------------*
FORM. BDC_DYNPRO  USING   PROGRAM DYNPRO.
  CLEAR T_BDC.
  T_BDC-PROGRAM  = PROGRAM.
  T_BDC-DYNPRO   = DYNPRO.
  T_BDC-DYNBEGIN = 'X'.
  APPEND T_BDC.

ENDFORM.                    " BDC_DYNPRO
*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_3623   text
*      -->P_3624   text
*----------------------------------------------------------------------*
FORM. BDC_FIELD USING FNAM FVAL.
  CLEAR T_BDC.
  T_BDC-FNAM = FNAM.
  T_BDC-FVAL = FVAL.
  APPEND T_BDC.
ENDFORM.                    " BDC_FIELD

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13973563/viewspace-752284/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 取最大值
下一篇: bdc另外程序
请登录后发表评论 登录
全部评论

注册时间:2011-03-30

  • 博文量
    18
  • 访问量
    21289