Stock overview and cost assignment - 3

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

*&---------------------------------------------------------------------*
*& Form GET_WIP_DATA
*&---------------------------------------------------------------------*
* WIP data
*----------------------------------------------------------------------*
FORM GET_WIP_DATA .

DATA:
LW_AUFNR LIKE AFPO-AUFNR,
LW_WIPACT TYPE TKKAB-SAKHA,
LW_WIPACT_PL TYPE TKKAB-SAKSO.

DATA:
LW_AUFPL TYPE RESB-AUFPL,
LW_VORNR TYPE RESB-VORNR.

DATA:
BEGIN OF LTH_AUFNR,
BELNR LIKE BSIS-BELNR,
ZUONR LIKE BSIS-ZUONR, " Order
SGTXT LIKE BSIS-SGTXT,
END OF LTH_AUFNR,

BEGIN OF LTH_WIP,
BELNR LIKE BSIS-BELNR, "Account document No.
BUZEI LIKE BSIS-BUZEI, "Number of Line Item
HKONT LIKE BSIS-HKONT, "Accounting number
SHKZG LIKE BSIS-SHKZG, "Debit/Credit Indicator
DMBTR LIKE BSIS-DMBTR, "Amount in Local currency
AUFNR LIKE BSIS-AUFNR, "Order No.
SGTXT LIKE BSIS-SGTXT, "Assignement
ZUONR LIKE BSEG-ZUONR, "Assignment Number
END OF LTH_WIP.

DATA:
LTD_AUFNR LIKE TABLE OF LTH_AUFNR,
LTH_CT000401 LIKE LINE OF TD_CT000401,
LTH_TEMP LIKE LTH_WIP,
LTD_WIP LIKE TABLE OF LTH_WIP.

* Get WIP Account number
SELECT SINGLE SAKHA SAKSO
FROM TKKAB
INTO (LW_WIPACT,LW_WIPACT_PL)
WHERE BUKRS = P_BUKRS
AND LEDNR = '00'
AND VERSA = '000'
AND ABKAT = '51'
AND LFDSN = '0'.

* Get the Material data
SELECT BELNR "Account document No.
ZUONR "Order
SGTXT "Assignement
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 = LW_WIPACT "Account range Raw material consumption
OR HKONT = LW_WIPACT_PL ). "Account range PL.
CHECK LTD_AUFNR IS NOT INITIAL.

SELECT BELNR "Account document No.
BUZEI "Number of Line Item
HKONT "Accounting number
SHKZG "Debit/Credit Indicator
DMBTR "Amount in Local currency
SGTXT "Assignment added by zhang mei
ZUONR "Assignment Number
INTO CORRESPONDING FIELDS OF TABLE LTD_WIP
FROM BSEG
FOR ALL ENTRIES IN LTD_AUFNR
WHERE BUKRS = P_BUKRS "Company Code
AND BELNR = LTD_AUFNR-BELNR "Accounting document
AND GJAHR = P_GJAHR "Year
AND ZUONR = LTD_AUFNR-ZUONR "
AND MATNR IN S_MATNR "Material
AND WERKS = P_WERKS "Plant
AND ( HKONT = LW_WIPACT "Account range WIP
OR HKONT = LW_WIPACT_PL ). "Account range PL.
IF SY-SUBRC = 0.
SORT LTD_WIP BY HKONT ASCENDING.

LOOP AT LTD_WIP INTO LTH_WIP.

LTH_CT000401-BUKRS = P_BUKRS. "Company code
LTH_CT000401-GJAHR = P_GJAHR. "Fiscal year
LTH_CT000401-MONAT = P_PERID. "Perid
LTH_CT000401-BELNR = LTH_WIP-BELNR. "document No
LTH_CT000401-BUZEI = LTH_WIP-BUZEI."Line Item
LTH_CT000401-HKONT = LTH_WIP-HKONT."Acc number

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = LTH_WIP-ZUONR
IMPORTING
OUTPUT = LW_AUFNR.

LTH_CT000401-AUFNR = LW_AUFNR.
* Storage Location, Batch Number and Plant
SELECT SINGLE MATNR
LGORT
CHARG
DWERK
PSMNG
MEINS
INTO (LTH_CT000401-MATNR, LTH_CT000401-LGORT,
LTH_CT000401-CHARG, LTH_CT000401-WERKS,
LTH_CT000401-QUANT, LTH_CT000401-MEINS)
FROM AFPO
WHERE AUFNR = LW_AUFNR .

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

* Get Valuation Class
SELECT SINGLE BKLAS
FROM MBEW
INTO LTH_CT000401-BKLAS
WHERE BWKEY = P_WERKS
AND MATNR = LTH_CT000401-MATNR.
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.
ENDIF.

* Get Work Center
SELECT SINGLE AUFPL
VORNR
INTO (LW_AUFPL, LW_VORNR)
FROM RESB
WHERE AUFNR = LTH_WIP-AUFNR
AND MATNR = LTH_CT000401-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_CT000401-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, LTH_CT000401.

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 /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. ENDIF.
ENDIF.
ENDIF.

ENDIF.


ENDFORM. " GET_WIP_DATA
*&---------------------------------------------------------------------*
*& Form GET_INVENTORY_DATA
*&---------------------------------------------------------------------*
* Get Inventory data
*----------------------------------------------------------------------*
FORM GET_INVENTORY_DATA .

DATA:
LW_QUANT TYPE /TDK/CT000401-QUANT,
LW_AMOUNT TYPE /TDK/CT000401-AMOUNT,
LTH_TEMP401 LIKE LINE OF TD_TEMP401,
LTH_CT000401 LIKE LINE OF TD_CT000401.

* Get FG Account Number
SELECT SINGLE KONTS
INTO W_FACCOUNT
FROM T030
WHERE KTOSL = C_BSX
AND KTOPL = C_A010
AND BKLAS = C_2110.

* Get Semi-FG Account Number
SELECT SINGLE KONTS
INTO W_SACCOUNT
FROM T030
WHERE KTOSL = C_BSX
AND KTOPL = C_A010
AND BKLAS = C_2100.

* Arrange the normal stock
PERFORM ARRANGE_NORMAL_STOCK.

* Arrange the sales order stock
PERFORM ARRANGE_SO_STOCK.

* Arrange the Valuated Stock with Subcontractor
PERFORM ARRANGE_CONS_STOCK.

SORT TD_TEMP401 BY BUKRS ASCENDING
GJAHR ASCENDING
MONAT ASCENDING
HKONT ASCENDING
BELNR ASCENDING
BUZEI ASCENDING
AUFNR ASCENDING
MATNR ASCENDING
WERKS ASCENDING
LGORT ASCENDING
CHARG ASCENDING.

* Append material data which without bacth
LOOP AT TD_TEMP401 INTO LTH_TEMP401.

LTH_CT000401 = LTH_TEMP401.

LW_QUANT = LW_QUANT + LTH_TEMP401-QUANT.
LW_AMOUNT = LW_AMOUNT + LTH_TEMP401-AMOUNT.

AT END OF CHARG.
LTH_CT000401-QUANT = LW_QUANT.
LTH_CT000401-AMOUNT = LW_AMOUNT.

APPEND LTH_CT000401 TO TD_CT000401.

CLEAR: LW_QUANT, LW_AMOUNT.
ENDAT.

CLEAR: LTH_TEMP401, LTH_CT000401.

ENDLOOP.

DELETE TD_CT000401
WHERE QUANT = 0
AND AMOUNT = 0.

* Delete the old data
IF CB_DEL IS NOT INITIAL.

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

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

ENDFORM. " GET_INVENTORY_DATA
*&---------------------------------------------------------------------*
*& Form GET_ROUTING_DATA
*&---------------------------------------------------------------------*
* Get routing data
*----------------------------------------------------------------------*
FORM GET_ROUTING_DATA USING I_AUFNR TYPE AUFNR
I_WERKS TYPE WERKS_D
I_LGORT TYPE LGORT_D
I_MATNR TYPE MATNR
I_CHARG TYPE CHARG_D
CHANGING O_RTGRP1 TYPE PLNNR
O_RTGRP2 TYPE PLNNR
O_RTGRP3 TYPE PLNNR
O_RTGRP4 TYPE PLNNR
O_RTGRP5 TYPE PLNNR
O_RTGRP6 TYPE PLNNR
O_RTGRP7 TYPE PLNNR
O_RTGRP8 TYPE PLNNR.

IF ( I_AUFNR IS NOT INITIAL )
OR ( I_MATNR IS NOT INITIAL
AND I_WERKS IS NOT INITIAL
AND I_LGORT IS NOT INITIAL
AND I_CHARG IS NOT INITIAL ).

CALL FUNCTION '/aaa/C_GET_ROUTING_TG'
EXPORTING
I_AUFNR = I_AUFNR
I_WERKS = I_WERKS
I_LGORT = I_LGORT
I_MATNR = I_MATNR
I_CHARG = I_CHARG
IMPORTING
ET_PLNNR_1 = O_RTGRP1
ET_PLNNR_2 = O_RTGRP2
ET_PLNNR_3 = O_RTGRP3
ET_PLNNR_4 = O_RTGRP4
ET_PLNNR_5 = O_RTGRP5
ET_PLNNR_6 = O_RTGRP6
ET_PLNNR_7 = O_RTGRP7
ET_PLNNR_8 = O_RTGRP8.
ENDIF.

ENDFORM. " GET_ROUTING_DATA
*&---------------------------------------------------------------------*
*& Form UPDATE_TABLE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM UPDATE_TABLE.

DATA LTH_CT000401 LIKE LINE OF TD_CT000401.
DATA LW_COUNT(5) TYPE N.
DATA LW_UPDATE_NUM(5) TYPE N.
DATA LW_DELETE_NUM(5) TYPE N.
DATA LW_INSERT_NUM(5) TYPE N.
DATA LW_FAIL_NUM(5) TYPE N.

IF TD_CT000401 IS INITIAL.
MESSAGE E041.
ENDIF.

SORT TD_CT000401 BY BUKRS ASCENDING
GJAHR ASCENDING
MONAT ASCENDING
HKONT ASCENDING
BELNR ASCENDING
BUZEI ASCENDING
AUFNR ASCENDING
MATNR ASCENDING
WERKS ASCENDING
LGORT ASCENDING
CHARG ASCENDING
SOBKZ ASCENDING.

DELETE ADJACENT DUPLICATES FROM TD_CT000401
COMPARING BUKRS GJAHR MONAT HKONT BELNR BUZEI
AUFNR MATNR WERKS LGORT CHARG SOBKZ.

DESCRIBE TABLE TD_CT000401 LINES LW_COUNT.

LOOP AT TD_CT000401 INTO LTH_CT000401.

CALL FUNCTION 'ENQUEUE_/aaa/ECT000401'
EXPORTING
MODE_/TDK/CT000401 = 'E'
MANDT = SY-MANDT
BUKRS = LTH_CT000401-BUKRS
GJAHR = LTH_CT000401-GJAHR
MONAT = LTH_CT000401-MONAT
HKONT = LTH_CT000401-HKONT
BELNR = LTH_CT000401-BELNR
BUZEI = LTH_CT000401-BUZEI
AUFNR = LTH_CT000401-AUFNR
MATNR = LTH_CT000401-MATNR
WERKS = LTH_CT000401-WERKS
LGORT = LTH_CT000401-LGORT
CHARG = LTH_CT000401-CHARG
SOBKZ = LTH_CT000401-SOBKZ
X_BUKRS = C_X
X_GJAHR = C_X
X_MONAT = C_X
X_HKONT = C_X
X_MATNR = C_X
X_WERKS = C_X
X_LGORT = C_X
X_CHARG = C_X
_SCOPE = '2'
_WAIT = C_X
_COLLECT = C_X
EXCEPTIONS
FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE E065.
ENDIF.

SELECT SINGLE COUNT(*)
FROM /aaa/CT000401
WHERE BUKRS = LTH_CT000401-BUKRS
AND GJAHR = LTH_CT000401-GJAHR
AND MONAT = LTH_CT000401-MONAT
AND HKONT = LTH_CT000401-HKONT
AND BELNR = LTH_CT000401-MATNR
AND BUZEI = LTH_CT000401-WERKS
AND AUFNR = LTH_CT000401-AUFNR
AND MATNR = LTH_CT000401-MATNR
AND WERKS = LTH_CT000401-WERKS
AND LGORT = LTH_CT000401-LGORT
AND CHARG = LTH_CT000401-CHARG
AND SOBKZ = LTH_CT000401-SOBKZ.

IF SY-SUBRC = 0.

DELETE FROM /add/CT000401
WHERE BUKRS = LTH_CT000401-BUKRS
AND GJAHR = LTH_CT000401-GJAHR
AND MONAT = LTH_CT000401-MONAT
AND HKONT = LTH_CT000401-HKONT
AND BELNR = LTH_CT000401-MATNR
AND BUZEI = LTH_CT000401-WERKS
AND AUFNR = LTH_CT000401-AUFNR
AND MATNR = LTH_CT000401-MATNR
AND WERKS = LTH_CT000401-WERKS
AND LGORT = LTH_CT000401-LGORT
AND CHARG = LTH_CT000401-CHARG
AND SOBKZ = LTH_CT000401-SOBKZ.

LW_DELETE_NUM = LW_DELETE_NUM + 1.

ENDIF.

* Insert process
INSERT /add/CT000401 FROM LTH_CT000401.
IF SY-SUBRC = 0.
COMMIT WORK AND WAIT.
LW_INSERT_NUM = LW_INSERT_NUM + 1.
ELSE.
ROLLBACK WORK.
LW_FAIL_NUM = LW_FAIL_NUM + 1.
ENDIF.

CALL FUNCTION 'DEQUEUE_/aaa/ECT000401'
.

ENDLOOP.

WRITE : /'The result:',
/'**************************************'.
WRITE /'Total records:'.
WRITE LW_COUNT.
WRITE /'Deleted records:'.
WRITE LW_DELETE_NUM.
WRITE /'Insert records:'.
WRITE LW_INSERT_NUM.
WRITE /'Fail records:'.
WRITE LW_FAIL_NUM.

ENDFORM. "UPDATE_TABLE

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

相關文章