PPCO0001
DATA:BEGIN OF itab OCCURS 0,
matkl TYPE matkl,
meins TYPE meins,
bdmng TYPE bdmng,
END OF itab.
DATA jtab LIKE TABLE OF itab WITH HEADER LINE.
DATA: bom LIKE stpox OCCURS 10 WITH HEADER LINE.
DATA msg TYPE string.
DATA iscb.
DATA ctab LIKE TABLE OF component_table WITH HEADER LINE.
CLEAR:itab,itab[],jtab,jtab[],bom,bom[],msg,iscb.
IF sy-tcode EQ 'CO01' OR sy-tcode EQ 'CO02' OR sy-tcode EQ 'CO40'.
READ TABLE header_table INDEX 1.
READ TABLE header_table_old INDEX 1.
IF header_table-werks EQ '1005' OR header_table-werks EQ '1001' OR header_table-werks EQ '1000'
OR header_table_old-werks EQ '1005' OR header_table_old-werks EQ '1001' OR header_table_old-werks EQ '1000'.
IF header_table-auart EQ 'ZP01' OR header_table-auart EQ 'ZP02' OR header_table-auart EQ 'ZP03' OR header_table-auart EQ 'ZP04' OR header_table-auart EQ 'ZP05' OR header_table-auart EQ 'ZP06'
OR header_table_old-auart EQ 'ZP01' OR header_table_old-auart EQ 'ZP02' OR header_table_old-auart EQ 'ZP03' OR header_table_old-auart EQ 'ZP04' OR header_table_old-auart EQ 'ZP05' OR header_table_old-auart EQ 'ZP06' .
LOOP AT component_table WHERE xloek NE 'X'.
READ TABLE itab WITH KEY matkl = component_table-matkl meins = component_table-meins.
IF sy-subrc NE 0.
itab-matkl = component_table-matkl.
itab-meins = component_table-meins.
APPEND itab.
CLEAR itab.
ENDIF.
CLEAR component_table.
ENDLOOP.
LOOP AT itab.
LOOP AT component_table WHERE matkl = itab-matkl AND meins = itab-meins AND xloek NE 'X'.
IF component_table-alpos EQ 'X'.
itab-bdmng = itab-bdmng + component_table-bdmng * component_table-ewahr.
ELSE.
itab-bdmng = itab-bdmng + component_table-bdmng.
ENDIF.
CLEAR component_table.
ENDLOOP.
MODIFY itab.
CLEAR itab.
ENDLOOP.
IF sy-tcode EQ 'CO01' OR sy-tcode EQ 'CO40' OR sy-tcode EQ 'CO02'.
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
capid = 'PP01' "BOM Application
datuv = sy-datum "有效开始日
emeng = header_table-gamng "BASE QUANTITY数量
mtnrv = header_table-plnbez
"MATERAILNUMBER物料
stlan = '1' "bom用途
* STLAL = P_STLAL
* CUOBJ = CUOBJ
mktls = 'X'
* MEHRS = 'X' "多阶层bom展开
werks = header_table-werks "'PDGM'工厂
TABLES
stb = bom "展开明细
EXCEPTIONS
alt_not_found = 1
call_invalid = 2
material_not_found = 3
missing_authorization = 4
no_bom_found = 5
no_plant_data = 6
no_suitable_bom_found = 7
OTHERS = 8.
IF sy-subrc EQ 0.
LOOP AT bom.
IF bom-ausch IS NOT INITIAL.
bom-mnglg = bom-mnglg * ( 1 + bom-ausch / 100 ).
bom-mnglg = ceil( bom-mnglg ).
MODIFY bom.
ENDIF.
READ TABLE jtab WITH KEY matkl = bom-matkl meins = bom-meins.
IF sy-subrc NE 0.
jtab-matkl = bom-matkl.
jtab-meins = bom-meins.
APPEND jtab.
CLEAR jtab.
ENDIF.
CLEAR bom.
ENDLOOP.
LOOP AT jtab.
LOOP AT bom WHERE matkl = jtab-matkl AND meins = jtab-meins.
IF bom-alpos EQ 'X'.
jtab-bdmng = jtab-bdmng + bom-mnglg * bom-ewahr * '1.01' .
ELSE.
jtab-bdmng = jtab-bdmng + bom-mnglg * '1.01'.
ENDIF.
CLEAR bom.
ENDLOOP.
MODIFY jtab.
CLEAR jtab.
ENDLOOP.
ENDIF.
ENDIF. "co01
* IF SY-TCODE EQ 'CO02'.
*
* CTAB[] = COMPONENT_TABLE[].
* LOOP AT COMPONENT_TABLE_OLD WHERE XLOEK NE 'X'.
* READ TABLE CTAB WITH KEY RSNUM = COMPONENT_TABLE_OLD-RSNUM RSPOS = COMPONENT_TABLE_OLD-RSPOS.
* IF SY-SUBRC EQ 0.
* DELETE CTAB WHERE RSNUM = COMPONENT_TABLE_OLD-RSNUM AND RSPOS = COMPONENT_TABLE_OLD-RSPOS.
* APPEND COMPONENT_TABLE_OLD TO CTAB.
* ENDIF.
* CLEAR:COMPONENT_TABLE_OLD,CTAB.
* ENDLOOP.
* SORT CTAB ASCENDING BY RSNUM RSPOS.
* LOOP AT CTAB WHERE XLOEK NE 'X'.
* READ TABLE JTAB WITH KEY MATKL = CTAB-MATKL MEINS = CTAB-MEINS.
* IF SY-SUBRC NE 0.
* JTAB-MATKL = CTAB-MATKL.
* JTAB-MEINS = CTAB-MEINS.
* APPEND JTAB.
* CLEAR JTAB.
* ENDIF.
* CLEAR CTAB.
* ENDLOOP.
*
* LOOP AT JTAB.
* LOOP AT CTAB WHERE MATKL = JTAB-MATKL AND MEINS = JTAB-MEINS AND XLOEK NE 'X'.
* IF CTAB-ALPOS EQ 'X'.
* JTAB-BDMNG = JTAB-BDMNG + CTAB-BDMNG * CTAB-EWAHR * '1.01'.
* ELSE.
* JTAB-BDMNG = JTAB-BDMNG + CTAB-BDMNG * '1.01'.
* ENDIF.
* CLEAR CTAB.
* ENDLOOP.
* MODIFY JTAB.
* CLEAR JTAB.
* ENDLOOP.
* ENDIF. "CO02
"判断超标
IF jtab[] IS NOT INITIAL.
LOOP AT itab.
READ TABLE jtab WITH KEY matkl = itab-matkl meins = itab-meins.
IF itab-bdmng GT jtab-bdmng.
iscb = 'X'.
LOOP AT component_table WHERE matkl = itab-matkl AND meins = itab-meins AND xloek NE 'X'.
SHIFT component_table-matnr LEFT DELETING LEADING '0'.
CONCATENATE msg component_table-matnr INTO msg SEPARATED BY '/'.
CLEAR component_table.
ENDLOOP.
SHIFT msg LEFT DELETING LEADING '/'.
CONCATENATE '物料组为' itab-matkl ' 单位为' itab-meins ' 的物料 ' msg '超标,保存失败' INTO msg.
MESSAGE msg TYPE 'I' .
CLEAR msg.
ENDIF.
CLEAR:itab,jtab..
ENDLOOP.
ENDIF.
IF iscb IS NOT INITIAL.
CLEAR iscb.
LEAVE TO TRANSACTION sy-tcode.
ENDIF.
ENDIF.
ENDIF.
ENDIF.