본문 바로가기
MM/ETC

[MM] 월별 대비 실적 조회

by clode 2023. 3. 2.
728x90
반응형

3개월 조회

  GV_DATE00    =  P_PLANM && GC_01.

  GV_DATE01    =  |{ GV_DATE00+0(6)  + 1 }| && GC_01.
  IF GV_DATE01+4(2) > 12.
    GV_DATE01  =  |{ GV_DATE01+0(4) + 1 }|  &&  GC_0101.
  ENDIF.

  GV_DATE02    =  |{ GV_DATE01+0(6)  + 1 }| && GC_01.
  IF GV_DATE02+4(2) > 12.
    GV_DATE02  =  |{ GV_DATE02+0(4) + 1 }|  &&  GC_0101.
  ENDIF.

  GV_ENDATE    =  |{ GV_DATE02+0(6)  + 1 }| && GC_01.
  IF GV_ENDATE+4(2) > 12.
    GV_ENDATE  =  |{ GV_ENDATE+0(4) + 1 }|  &&  GC_0101.
  ENDIF.

  GV_ENDATE   =  GV_ENDATE - 1.  "3개월째의 마지막 날

GT_DATA 계획데이터 INT
LT_ACT 실적데이터 INT
LT_COLT 실적데이터 합산 INT
계획데이터 없이 실적데이터만 존재할경우 실적데이터만이라도 나오도록 한다.

```
TYPES: BEGIN OF TY_EKBE, "collect int
BUDAT(6),
WERKS TYPE EKBE-WERKS,
MATNR TYPE EKBE-MATNR,
LIFNR TYPE EKKO-LIFNR,
WAERS TYPE WAERS,
MENGE TYPE MENGE_D,
WRBTR TYPE WRBTR,
DMBTR TYPE DMBTR,
NETPR TYPE DMBTR,
END OF TY_EKBE,
TTY_EKBE TYPE STANDARD TABLE OF TY_EKBE WITH EMPTY KEY.

SELECT
CWERKS,
LEFT( C
BUDAT, 6 ) AS BUDAT,
CMATNR,
D
MAKTX,
EWRKST,
A
EKGRP,
AEKORG,
A
LIFNR,
FNAME1,
E
MTART,
GBESKZ,
G
SOBSL,
BMEINS,
C
WAERS,
CSHKZG,
CASE C
SHKZG WHEN @GC_H THEN CMENGE * ( -1 )
ELSE C
MENGE END AS MENGE, "SHKZG = 'H' 이면 수량(MENGE)과
CASE CSHKZG WHEN @GC_H THEN CWRBTR * ( -1 )
ELSE CWRBTR END AS WRBTR, "금액(WRBTR)과
CASE C
SHKZG WHEN @GC_H THEN CDMBTR * ( -1 )
ELSE C
DMBTR END AS DMBTR, "원화환산금액(AREWR) 마이너스 처리
@GC_KRW AS WAERS_KRW
FROM EKKO AS A
INNER JOIN EKPO AS B ON AEBELN EQ BEBELN
INNER JOIN EKBE AS C ON BEBELN EQ CEBELN
AND BEBELP EQ CEBELP
LEFT OUTER JOIN MAKT AS D ON CMATNR EQ DMATNR
AND DSPRAS EQ @SY-LANGU
LEFT OUTER JOIN MARA AS E ON C
MATNR EQ EMATNR
LEFT OUTER JOIN LFA1 AS F ON A
LIFNR EQ FLIFNR
LEFT OUTER JOIN MARC AS G ON C
MATNR EQ GMATNR
AND C
WERKS EQ GWERKS
WHERE C
GJAHR EQ @P_PLANM+0(4)
AND AEKORG IN @S_EKORG
AND C
WERKS IN @S_WERKS
AND CMATNR IN @S_MATNR
AND C
MATNR NE @SPACE
AND ALIFNR IN @S_LIFNR
AND C
VGABE EQ @GC_2
AND C~BUDAT BETWEEN @GV_DATE00 AND @GV_ENDATE
INTO TABLE @DATA(LT_ACT).
SORT LT_ACT BY BUDAT WERKS MATNR LIFNR WAERS.

*-- 월별(BUDAT(6)/공급업체(LIFNR)/자재(MATNR)별로 수량과 금액 COLLECT
TRY .
DATA(LT_COLT) = VALUE TTY_EKBE(
FOR GROUPS OF IN LT_ACT
GROUP BY ( BUDAT = -BUDAT WERKS = -WERKS MATNR
= -MATNR LIFNR = -LIFNR WAERS = -WAERS )
LET COLL_LINE = REDUCE #( INIT LINE TYPE TY_EKBE FOR
IN GROUP
NEXT LINE-WERKS = -WERKS
LINE-BUDAT = -BUDAT
LINE-MATNR = -MATNR
LINE-LIFNR = -LIFNR
LINE-WAERS = -WAERS
LINE-MENGE = LINE-MENGE + -MENGE
LINE-WRBTR = LINE-WRBTR + -WRBTR
LINE-DMBTR = LINE-DMBTR + -DMBTR
LINE-NETPR = LINE-WRBTR / LINE-MENGE )
IN ( COLL_LINE ) ) .
CATCH CX_ROOT INTO DATA(LX_EXC).
DATA(LV_MSG) = LX_EXC->GET_TEXT( ).
ENDTRY.

SORT: LT_ACT BY WERKS MATNR LIFNR,
LT_COLT BY WERKS MATNR LIFNR.

"금액 합산 후에는 금액/수량 제외한 필드값만 필요하기때문에 중복데이터 DELETE
DELETE ADJACENT DUPLICATES FROM LT_ACT COMPARING WERKS MATNR LIFNR.

*-- 합산한 실제 구매수량, 구매금액 ALV에 MOVE
LOOP AT LT_COLT INTO DATA(LS_COLT).

"합산 실제수량, 금액이 0인 경우
IF LS_COLT-MENGE EQ 0 AND LS_COLT-WRBTR EQ 0.
  CONTINUE.
ENDIF.

"원화 일때 구매금액(원화) : EKBE-WRBTR로 동일,
"외화 일때 구매금액(원화) : EKBE-DMBTR
DATA(LV_AKPRICE) = COND #( WHEN LS_COLT-WAERS EQ GC_KRW THEN LS_COLT-WRBTR
                                                        ELSE LS_COLT-DMBTR ).
"합산 월과 ALV월 매핑
"Ex) 7월-> GV_DATE00 , 8월 -> GV_DATE01,9월 -> GV_DATE02
DATA(LV_MONTH) = COND #( WHEN LS_COLT-BUDAT+0(6) = GV_DATE00+0(6) THEN GC_00
                         WHEN LS_COLT-BUDAT+0(6) = GV_DATE01+0(6) THEN GC_01
                         WHEN LS_COLT-BUDAT+0(6) = GV_DATE02+0(6) THEN GC_02 ).


READ TABLE GT_DATA INTO DATA(LS_DATA) WITH KEY WERKS = LS_COLT-WERKS
                                               MATNR = LS_COLT-MATNR
                                               LIFNR = LS_COLT-LIFNR.

IF SY-SUBRC EQ 0.
  PERFORM SET_ACT_VALUE USING: '_AMENGE'  LV_MONTH  LS_COLT-MENGE LS_DATA, "구매수량
                               '_APRICE'  LV_MONTH  LS_COLT-WRBTR LS_DATA, "구매금액
                               '_ANETPR'  LV_MONTH  LS_COLT-NETPR LS_DATA, "구매단가
                               '_AWAERS'  LV_MONTH  LS_DATA-WAERS LS_DATA, "구매통화
                               '_AKPRICE' LV_MONTH  LV_AKPRICE    LS_DATA. "원화환산구매금액

  MODIFY GT_DATA FROM LS_DATA INDEX SY-TABIX.
ELSE.
  "계획 테이블에 존재하지 않고 실제 테이블에만 존재할 경우 Insert
  READ TABLE LT_ACT INTO DATA(LS_ACT) WITH KEY WERKS = LS_COLT-WERKS
                                               MATNR = LS_COLT-MATNR
                                               LIFNR = LS_COLT-LIFNR
                                               BINARY SEARCH.
  IF SY-SUBRC EQ 0.
    MOVE-CORRESPONDING LS_ACT TO LS_DATA.
    PERFORM SET_ACT_VALUE USING: '_AMENGE'  LV_MONTH  LS_COLT-MENGE LS_DATA, "구매수량
                                 '_APRICE'  LV_MONTH  LS_COLT-WRBTR LS_DATA, "구매금액
                                 '_ANETPR'  LV_MONTH  LS_COLT-NETPR LS_DATA, "구매단가
                                 '_AWAERS'  LV_MONTH  LS_DATA-WAERS LS_DATA, "구매통화
                                 '_AKPRICE' LV_MONTH  LV_AKPRICE    LS_DATA.    "원화환산구매금액

    INSERT LS_DATA INTO TABLE GT_DATA.
  ENDIF.

ENDIF.

CLEAR: LS_DATA.

ENDLOOP.

&---------------------------------------------------------------------
& Form SET_ACT_VALUE
*&---------------------------------------------------------------------

& text
*&---------------------------------------------------------------------

  • -->P_ text
  • -->P_LS_COLT_BUDAT text
  • -->P_LS_COLT_MENGE text
  • &---------------------------------------------------------------------*
    FORM SET_ACT_VALUE USING VALUE(PV_NAME)
                        PV_MONTH
                        PV_VALUE
                        PS_DISP TYPE ZMMS_PUR106_DISP.

DATA(LV_FIELD) = 'PS_DISP-Z_' && PV_MONTH && PV_NAME.
ASSIGN (LV_FIELD) TO FIELD-SYMBOL().

CHECK SY-SUBRC = 0.
MOVE PV_VALUE TO .

ENDFORM.

728x90
반응형

'MM > ETC' 카테고리의 다른 글

특정 상수값이 존재하는 프로그램 찾는 법  (0) 2024.07.10
ALV로 조회하기 (feat. ME1M)  (0) 2024.07.10
[MM] Release PO BAPI  (0) 2023.03.02
[MM] Create PO BAPI  (0) 2023.03.02
User-exit  (0) 2023.02.27

댓글