Stock overview and cost assignment - 2

mmccking發表於2012-10-13
* Get Gross Sales data
* Get Cost of Goods Sold data
* Get the Material data
* Get WIP data
* Get Inventory data
[@more@]

*&---------------------------------------------------------------------*
*& Form GET_GOODS_SOLD_DATA
*&---------------------------------------------------------------------*
* Get Cost of Goods Sold data
*----------------------------------------------------------------------*
FORM GET_GOODS_SOLD_DATA .

DATA:
LTH_CT000401 LIKE LINE OF TD_CT000401,
LW_MBLNR LIKE MSEG-MBLNR,
LW_ZEILE LIKE MSEG-ZEILE,
LTD_DJAMON TYPE TABLE OF STRING,
LTH_DJAMON LIKE LINE OF LTD_DJAMON.

DATA:
BEGIN OF LTH_MSEG,
MATNR LIKE MSEG-MATNR,
WERKS LIKE MSEG-WERKS,
LGORT LIKE MSEG-LGORT,
CHARG LIKE MSEG-CHARG,
DMBTR LIKE MSEG-DMBTR,
MENGE LIKE MSEG-MENGE,
MEINS LIKE MSEG-MEINS,
SHKZG LIKE MSEG-SHKZG, "Debit/Credit Indicator
END OF LTH_MSEG.

DATA:
LTD_MSEG LIKE TABLE OF LTH_MSEG,
LTH_MSEG_TEMP LIKE LTH_MSEG.

DATA:
BEGIN OF LTH_BSIS,
AWKEY TYPE BKPF-AWKEY, "Reference Key
BELNR TYPE BSIS-BELNR, "Accounting document number
BUZEI TYPE BSIS-BUZEI, "Number of Line Item
HKONT TYPE BSIS-HKONT, "Account number
SHKZG TYPE BSIS-SHKZG, "Debit/Credit Indicator
DMBTR TYPE BSIS-DMBTR, "Amount in Local currency
AUFNR TYPE BSIS-AUFNR, "Order No.
PRCTR TYPE BSIS-PRCTR, "Profit center
END OF LTH_BSIS.

DATA:
LW_DATE TYPE SY-DATUM,
LW_PERIV TYPE T001-PERIV,
LW_PERID TYPE T009B-POPER.

DATA:
LTD_BSIS LIKE TABLE OF LTH_BSIS.

CLEAR RH_ACCOUNT.
REFRESH RD_ACCOUNT.

* Get the Account
SELECT VALFROM
FROM SETLEAF
INTO TABLE TD_SETLEAF
WHERE SETCLASS = '0000'
AND SETNAME = 'AG01_2'.

* Set account range table
LOOP AT TD_SETLEAF INTO TH_SETLEAF.
RH_ACCOUNT-SIGN = 'I'.
RH_ACCOUNT-OPTION = 'EQ'.
RH_ACCOUNT-LOW = TH_SETLEAF-VALFROM.
APPEND RH_ACCOUNT TO RD_ACCOUNT.
CLEAR: TH_SETLEAF, RH_ACCOUNT.
ENDLOOP.

* Company Code existence check
SELECT SINGLE PERIV
INTO LW_PERIV
FROM T001
WHERE BUKRS = P_BUKRS.

LW_PERID = P_PERID.

* Get Gross Sales data
SELECT BKPF~AWKEY "Reference Key
BSIS~BELNR "Accounting document number
BSIS~BUZEI "Number of Line Item
BSIS~HKONT "Accounting number
BSIS~SHKZG "Debit/Credit Indicator
BSIS~DMBTR "Amount in Local currency
BSIS~AUFNR "Order No.
BSIS~PRCTR "Profit center
FROM BSIS INNER JOIN BKPF
ON BSIS~BUKRS = BKPF~BUKRS
AND BSIS~BELNR = BKPF~BELNR
AND BSIS~GJAHR = BKPF~GJAHR
INTO TABLE LTD_BSIS
WHERE BSIS~BUKRS = P_BUKRS
AND BSIS~WERKS = P_WERKS
AND BSIS~GJAHR = P_GJAHR
AND BSIS~MONAT = P_PERID
AND BKPF~STBLG = SPACE
AND BSIS~HKONT IN RD_ACCOUNT.

IF SY-SUBRC = 0.

SORT LTD_BSIS BY AWKEY BELNR BUZEI ASCENDING.

LOOP AT LTD_BSIS INTO LTH_BSIS.

AT NEW AWKEY.

SELECT MATNR
WERKS
LGORT
CHARG
DMBTR
MENGE
MEINS
SHKZG
FROM MSEG
APPENDING CORRESPONDING FIELDS OF TABLE LTD_MSEG
WHERE MBLNR = LTH_BSIS-AWKEY+0(10)
AND MJAHR = LTH_BSIS-AWKEY+10(4)
AND MATNR IN S_MATNR
AND DMBTR <> 0.

ENDAT.

LOOP AT LTD_MSEG INTO LTH_MSEG WHERE DMBTR = LTH_BSIS-DMBTR.

IF LTH_MSEG-SHKZG = 'S'.
LTH_CT000401-AMOUNT = LTH_MSEG-DMBTR * -1. "Amount
LTH_CT000401-QUANT = LTH_MSEG-MENGE * -1. "Quantity
ELSE.
LTH_CT000401-AMOUNT = LTH_MSEG-DMBTR. "Amount
LTH_CT000401-QUANT = LTH_MSEG-MENGE. "Quantity
ENDIF.

LTH_CT000401-WERKS = LTH_MSEG-WERKS.
LTH_CT000401-LGORT = LTH_MSEG-LGORT.
LTH_CT000401-MATNR = LTH_MSEG-MATNR.
LTH_CT000401-CHARG = LTH_MSEG-CHARG.
LTH_CT000401-MEINS = LTH_MSEG-MEINS. "Base Unit of Measure

DELETE LTD_MSEG INDEX SY-TABIX.
CLEAR LTH_MSEG.

EXIT.
ENDLOOP.
IF SY-SUBRC <> 0.
CLEAR: LTH_BSIS.
CONTINUE.
ENDIF.

LTH_CT000401-BUKRS = P_BUKRS. "Company Code
LTH_CT000401-GJAHR = P_GJAHR. "Fiscal Year
LTH_CT000401-MONAT = P_PERID. "Perid
LTH_CT000401-BELNR = LTH_BSIS-BELNR. "Accounting document number
LTH_CT000401-BUZEI = LTH_BSIS-BUZEI. "Number of Line Item
LTH_CT000401-HKONT = LTH_BSIS-HKONT. "GL Account
LTH_CT000401-PRCTR = LTH_BSIS-PRCTR. "GL Account


* Get Valuation Class
SELECT SINGLE BKLAS
FROM MBEW
INTO LTH_CT000401-BKLAS
WHERE BWKEY = P_WERKS
AND MATNR = LTH_CT000401-MATNR. "Material number
IF SY-SUBRC <> 0.
SELECT SINGLE BKLAS
FROM MBEWH
INTO LTH_CT000401-BKLAS
WHERE BWKEY = P_WERKS
AND LFGJA = P_GJAHR
AND LFMON = P_PERID
AND MATNR = LTH_CT000401-MATNR. "Material number
ENDIF.

* Get the routing data
PERFORM GET_ROUTING_DATA USING ''
LTH_CT000401-WERKS "LTH_MSEG-WERKS Changed by zhang mei on 2011.09.29
LTH_CT000401-LGORT "LTH_MSEG-LGORT Changed by zhang mei on 2011.09.29
LTH_CT000401-MATNR "LTH_MSEG-MATNR Changed by zhang mei on 2011.09.29
LTH_CT000401-CHARG "LTH_MSEG-CHARG Changed by zhang mei on 2011.09.29
CHANGING LTH_CT000401-RTGRP1
LTH_CT000401-RTGRP2
LTH_CT000401-RTGRP3
LTH_CT000401-RTGRP4
LTH_CT000401-RTGRP5
LTH_CT000401-RTGRP6
LTH_CT000401-RTGRP7
LTH_CT000401-RTGRP8.

APPEND LTH_CT000401 TO TD_CT000401.

TH_MATNR-MATNR = LTH_CT000401-MATNR.
APPEND TH_MATNR TO TD_MATNR.

CLEAR: TH_MATNR, LTH_CT000401, LW_ZEILE, LTH_MSEG,
LW_MBLNR.

ENDLOOP.

* Delete the old data
IF CB_DEL IS NOT INITIAL.

SELECT SINGLE * FROM /aaa/CT000401
WHERE BUKRS = P_BUKRS
AND GJAHR = P_GJAHR
AND MONAT = P_PERID
AND HKONT IN RD_ACCOUNT
AND MATNR IN S_MATNR
AND WERKS = P_WERKS.
IF SY-SUBRC = 0.

DELETE FROM /TDK/CT000401
WHERE BUKRS = P_BUKRS
AND GJAHR = P_GJAHR
AND MONAT = P_PERID
AND HKONT IN RD_ACCOUNT
AND MATNR IN S_MATNR
AND WERKS = P_WERKS.
IF SY-SUBRC <> 0.
MESSAGE E027.
ENDIF.
ENDIF.
ENDIF.
ENDIF.

ENDFORM. " GET_GOODS_SOLD_DATA
*&---------------------------------------------------------------------*
*& Form GET_MATERIAL_DATA
*&---------------------------------------------------------------------*
* Get the Material data
*----------------------------------------------------------------------*
FORM GET_MATERIAL_DATA .

DATA:
BEGIN OF LTH_BSEG,
BELNR LIKE BSEG-BELNR, "Accounting document number
BUZEI LIKE BSEG-BUZEI, "Number of Line Item
DMBTR LIKE BSEG-DMBTR, "Amount in Local currency
HKONT LIKE BSEG-HKONT, "Accounting number
SHKZG LIKE BSEG-SHKZG, "Debit/Credit Indicator
AUFNR LIKE BSEG-AUFNR, "Order No.
MATNR LIKE BSEG-MATNR, "Material
MENGE LIKE BSEG-MENGE, "Quantity
MEINS LIKE BSEG-MEINS, "UNIT
END OF LTH_BSEG,

BEGIN OF LTH_AUFNR,
BELNR LIKE BSIS-BELNR,
AUFNR LIKE BSIS-AUFNR,
END OF LTH_AUFNR.

DATA:
LTD_AUFNR LIKE TABLE OF LTH_AUFNR,
LW_AWKEY LIKE BKPF-AWKEY,
LW_ZEILE LIKE MSEG-ZEILE,
LW_AUFPL TYPE RESB-AUFPL,
LW_VORNR TYPE RESB-VORNR,
LTH_CT000401 LIKE LINE OF TD_CT000401,
LTD_BSEG LIKE TABLE OF LTH_BSEG.

CLEAR RH_ACCOUNT.
REFRESH RD_ACCOUNT.

* Get the Account
SELECT VALFROM
FROM SETLEAF
INTO TABLE TD_SETLEAF
WHERE SETCLASS = '0000'
AND SETNAME = 'AG01_3'.

LOOP AT TD_SETLEAF INTO TH_SETLEAF.
RH_ACCOUNT-SIGN = 'I'.
RH_ACCOUNT-OPTION = 'EQ'.
RH_ACCOUNT-LOW = TH_SETLEAF-VALFROM.
APPEND RH_ACCOUNT TO RD_ACCOUNT.
CLEAR: TH_SETLEAF, RH_ACCOUNT.
ENDLOOP.

* Get the Material data
SELECT BELNR
AUFNR "Order No.
INTO TABLE LTD_AUFNR
FROM BSIS
WHERE BUKRS = P_BUKRS "Company Code
AND GJAHR = P_GJAHR "Year
AND MONAT = P_PERID "Period
AND WERKS = P_WERKS "Plant
AND HKONT IN RD_ACCOUNT. "Account range Raw material consumption

CHECK LTD_AUFNR IS NOT INITIAL.

LOOP AT LTD_AUFNR INTO LTH_AUFNR.

SELECT BELNR "Accounting document number
BUZEI "Number of Line Item
DMBTR "Amount in Local currency
HKONT "Accounting number
SHKZG "Debit/Credit Indicator
AUFNR "Order No.
MATNR "Material
MENGE "Quantity
MEINS "UNIT
APPENDING TABLE LTD_BSEG
FROM BSEG
WHERE BUKRS = P_BUKRS "Company Code
AND BELNR = LTH_AUFNR-BELNR "Accounting document number
AND GJAHR = P_GJAHR "Year
AND AUFNR = LTH_AUFNR-AUFNR "Order No.
AND MATNR IN S_MATNR "Material
AND WERKS = P_WERKS "Plant
AND HKONT IN RD_ACCOUNT. "Account range Raw material consumption

ENDLOOP.

IF SY-SUBRC = 0.

LOOP AT LTD_BSEG INTO LTH_BSEG.

LTH_CT000401-BUKRS = P_BUKRS.
LTH_CT000401-GJAHR = P_GJAHR.
LTH_CT000401-WERKS = P_WERKS. "Plant
LTH_CT000401-MONAT = P_PERID. "Perid

IF LTH_BSEG-SHKZG = 'H'.
LTH_CT000401-AMOUNT = LTH_BSEG-DMBTR * -1. "Amount
LTH_CT000401-QUANT = LTH_BSEG-MENGE * -1. "Quantity
ELSE.
LTH_CT000401-AMOUNT = LTH_BSEG-DMBTR. "Amount
LTH_CT000401-QUANT = LTH_BSEG-MENGE. "Quantity
ENDIF.

LTH_CT000401-BELNR = LTH_BSEG-BELNR. "Account document
LTH_CT000401-BUZEI = LTH_BSEG-BUZEI. "Number of Line Item
LTH_CT000401-HKONT = LTH_BSEG-HKONT. "Accounting number
LTH_CT000401-MATNR = LTH_BSEG-MATNR. "Material
LTH_CT000401-AUFNR = LTH_BSEG-AUFNR. "Order NO.
LTH_CT000401-MEINS = LTH_BSEG-MEINS. "Unit

* Get referance key
SELECT SINGLE AWKEY
FROM BKPF
INTO LW_AWKEY
WHERE BUKRS = P_BUKRS
AND BELNR = LTH_BSEG-BELNR
AND GJAHR = P_GJAHR.
IF SY-SUBRC = 0.

LW_ZEILE = ( LTH_BSEG-BUZEI / 2 ).
* Get batch
SELECT SINGLE CHARG
FROM MSEG
INTO LTH_CT000401-CHARG
WHERE MBLNR = LW_AWKEY+0(10)
AND MJAHR = LW_AWKEY+10(4)
AND ZEILE = LW_ZEILE.
ENDIF.

* Get Valuation Class
SELECT SINGLE BKLAS
FROM MBEW
INTO LTH_CT000401-BKLAS
WHERE BWKEY = P_WERKS
AND MATNR = LTH_BSEG-MATNR. "Material number
IF SY-SUBRC <> 0.
SELECT SINGLE BKLAS
FROM MBEWH
INTO LTH_CT000401-BKLAS
WHERE BWKEY = P_WERKS
AND LFGJA = P_GJAHR
AND LFMON = P_PERID
AND MATNR = LTH_BSEG-MATNR. "Material number
ENDIF.

* Get Work Center
SELECT SINGLE AUFPL
VORNR
LGORT
INTO (LW_AUFPL, LW_VORNR, LTH_CT000401-LGORT)
FROM RESB
WHERE AUFNR = LTH_BSEG-AUFNR
AND MATNR = LTH_BSEG-MATNR.

IF SY-SUBRC = 0.
* Get Work Center
SELECT SINGLE CRHD~ARBPL
INTO LTH_CT000401-ARBPL
FROM CRHD INNER JOIN AFVC
ON CRHD~OBJID = AFVC~ARBID
WHERE CRHD~OBJTY = 'A'
AND AFVC~AUFPL = LW_AUFPL
AND AFVC~VORNR = LW_VORNR.
ENDIF.

* Get the routing data
PERFORM GET_ROUTING_DATA USING LTH_BSEG-AUFNR
LTH_CT000401-WERKS
LTH_CT000401-LGORT
LTH_CT000401-MATNR
LTH_CT000401-CHARG
CHANGING LTH_CT000401-RTGRP1
LTH_CT000401-RTGRP2
LTH_CT000401-RTGRP3
LTH_CT000401-RTGRP4
LTH_CT000401-RTGRP5
LTH_CT000401-RTGRP6
LTH_CT000401-RTGRP7
LTH_CT000401-RTGRP8.

APPEND LTH_CT000401 TO TD_CT000401.

TH_MATNR-MATNR = LTH_CT000401-MATNR.
APPEND TH_MATNR TO TD_MATNR.
CLEAR: TH_MATNR, LW_AUFPL,
LW_ZEILE, LTH_CT000401, LW_VORNR.

ENDLOOP.

* Delete the old data
IF CB_DEL IS NOT INITIAL.

SELECT SINGLE * FROM /aaaCT000401
WHERE BUKRS = P_BUKRS
AND GJAHR = P_GJAHR
AND MONAT = P_PERID
AND HKONT IN RD_ACCOUNT
AND MATNR IN S_MATNR
AND WERKS = P_WERKS.
IF SY-SUBRC = 0.

DELETE FROM /aaa/CT000401
WHERE BUKRS = P_BUKRS
AND GJAHR = P_GJAHR
AND MONAT = P_PERID
AND HKONT IN RD_ACCOUNT
AND MATNR IN S_MATNR
AND WERKS = P_WERKS.
IF SY-SUBRC <> 0.
MESSAGE E027(/aaa/P001) WITH '/aaa/CT000401'.
ENDIF.
ENDIF.
ENDIF.

ENDIF.

ENDFORM. " GET_MATERIAL_DATA

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8356764/viewspace-1059578/,如需轉載,請註明出處,否則將追究法律責任。

相關文章