MD61->BAPI_REQUIREMENTS_CREATE
*&---------------------------------------------------------------------*
*& Form frm_create_pbdnr_matnr
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_create_pbdnr_matnr USING pv_versb TYPE versb
pv_zywbklike
CHANGING ps_alv TYPE ty_outtab.
DATA: lt_schedule_in LIKE TABLE OF bapisshdin WITH HEADER LINE,
lt__char_in LIKE TABLE OF bapischarr WITH HEADER LINE,
lt_requirements LIKE TABLE OF bapisitmeo WITH HEADER LINE,
lt_return LIKE TABLE OF bapireturn1 WITH HEADER LINE,
ls_bapisitemr TYPE bapisitemr.
DATA: lv_pbdnr TYPE pbdnr,
lv_fname TYPE fieldname,
lv_beg TYPE datum,
lv_end TYPE datum,
lv_monday TYPE datum,
lv_monday_end TYPE datum,
lv_month_beg TYPE datum,
lv_matnr TYPE matnr18,
lv_bedae TYPE bedae.
REFRESH:lt_schedule_in,lt_requirements,lt_return.
LOOP AT gt_pbed INTO gs_pbed WHERE matnr = ps_alv-matnr.
lt_schedule_in-date_type = '1'. "代表天
lt_schedule_in-req_date = gs_pbed-pdatu.
lt_schedule_in-req_qty = gs_pbed-plnmg.
lt_schedule_in-unit = gs_pbed-meins.
APPEND lt_schedule_in.
CLEAR:gs_pbed,lt_schedule_in.
ENDLOOP.
lv_beg = p_datum + 27. "第28天的日期
* 获取对应的周数
CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
EXPORTING
date = lv_beg
IMPORTING
monday = lv_monday. "第28天所在周对应周一日期
lv_month_beg = lv_monday + 336. "28天后第48个周周一的日期
*第一个月计划需求
CLEAR lt_schedule_in.
lt_schedule_in-date_type = '3'. "代表月
lt_schedule_in-req_date = lv_month_beg(6).
lt_schedule_in-req_date+6(2) = '01'.
ASSIGN COMPONENT 'M1' OF STRUCTURE ps_alv TO FIELD-SYMBOL(<fs>).
IF sy-subrc EQ 0 AND <fs> IS ASSIGNED.
lt_schedule_in-req_qty = <fs>.
ENDIF.
lt_schedule_in-unit = ps_alv-meins.
APPEND lt_schedule_in.
DO 48 TIMES.
* ***---8个周计划需求
* 8周修改为48周,去掉月
* IF sy-index LE 8.
IF sy-index LE 48.
lv_fname = 'W' && sy-index.
lv_monday = lv_monday + 7.
CLEAR lt_schedule_in.
lt_schedule_in-date_type = '2'. "代表周
lt_schedule_in-req_date = lv_monday.
UNASSIGN <fs>.
ASSIGN COMPONENT lv_fname OF STRUCTURE ps_alv TO <fs>.
IF sy-subrc EQ 0 AND <fs> IS ASSIGNED.
lt_schedule_in-req_qty = <fs>.
ENDIF.
lt_schedule_in-unit = ps_alv-meins.
APPEND lt_schedule_in.
****----8个月计划需求(从第二个月开始的计划需求)
* 获取计划需求日期第9个周周一对应月份的下一月
CALL FUNCTION 'MONTH_PLUS_DETERMINE'
EXPORTING
months = sy-index
olddate = lv_month_beg
IMPORTING
newdate = lv_end.
DATA(lv_index) = sy-index + 1.
lv_fname = 'M' && lv_index.
CLEAR lt_schedule_in.
lt_schedule_in-date_type = '3'. "代表月
lt_schedule_in-req_date = lv_end(6).
lt_schedule_in-req_date+6(2) = '01'.
UNASSIGN <fs>.
ASSIGN COMPONENT lv_fname OF STRUCTURE ps_alv TO <fs>.
IF sy-subrc EQ 0 AND <fs> IS ASSIGNED.
lt_schedule_in-req_qty = <fs>.
ENDIF.
lt_schedule_in-unit = ps_alv-meins.
APPEND lt_schedule_in.
ENDIF.
****--- 28天天计划需求
IF sy-index LE 28.
lv_fname = 'T' && sy-index.
CLEAR lt_schedule_in.
lt_schedule_in-date_type = '1'. "代表天
lt_schedule_in-req_date = p_datum + sy-index - 1.
UNASSIGN <fs>.
ASSIGN COMPONENT lv_fname OF STRUCTURE ps_alv TO <fs>.
IF sy-subrc EQ 0 AND <fs> IS ASSIGNED.
lt_schedule_in-req_qty = <fs>.
ENDIF.
lt_schedule_in-unit = ps_alv-meins.
READ TABLE gt_pbed TRANSPORTING NO FIELDS
WITH KEY matnr = ps_alv-matnr
pdatu = lt_schedule_in-req_date.
IF sy-subrc NE 0.
APPEND lt_schedule_in.
ENDIF.
ENDIF.
ENDDO.
CLEAR ls_bapisitemr.
lv_matnr = ps_alv-matnr.
ls_bapisitemr-material = lv_matnr.
ls_bapisitemr-plant = ps_alv-werks.
ls_bapisitemr-version = '00'.
ls_bapisitemr-vers_activ = 'X'.
ls_bapisitemr-req_number = ps_alv-pbdnr.
ls_bapisitemr-mrp_area = ps_alv-berid.
CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE'
EXPORTING
requirements_item = ls_bapisitemr
TABLES
requirements_schedule_in = lt_schedule_in
return = lt_return.
IF lt_return[] IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ps_alv-icon = icon_led_green.
ps_alv-msg = '物料' && ps_alv-matnr && '工厂' && ps_alv-werks && '主计划创建成功!'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ps_alv-icon = icon_led_red.
LOOP AT lt_return.
CONCATENATE ps_alv-msg lt_return-message INTO ps_alv-msg SEPARATED BY '/'.
CLEAR lt_return.
ENDLOOP.
ENDIF.
ENDFORM.
MD63读取需求计划-->BAPI:BAPI_MATERIAL_STOCK_REQ_LIST
*&---------------------------------------------------------------------*
*& Form frm_get_history_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_get_history_data .
******工厂级别4周前forecast获取
DATA:lv_datum TYPE sy-datum,
ls_ztpp0033 TYPE ztpp0033,
lt_ztpp0033 TYPE TABLE OF ztpp0033.
DATA:lt_mrp_ind_lines TYPE TABLE OF bapi_mrp_ind_lines.
DATA:lt_mrp_ind_lines_werks TYPE TABLE OF bapi_mrp_ind_lines.
DATA:lt_mrp_ind_lines_mrp TYPE TABLE OF bapi_mrp_ind_lines.
DATA:lv_str TYPE string.
DATA:lv_matnr TYPE bapi_mrp_mat_param-material,
lv_mrp TYPE bapi_mrp_mat_param-mrp_area.
lv_datum = p_datum + 21.
DATA(lv_len) = strlen( gs_ywbk-ywbk ).
"根据导入模版-工厂PBIM- WERKS并且PBIM~loevr NE 'D'(没有删除标识)取:PBIM-MATNR(物料编号);
SELECT matnr
FROM pbim
INTO TABLE @DATA(lt_pbim)
WHERE werks = @gs_ywbk-werks
AND loevr NE 'D'.
"根据导入模版-工厂PBID-WERKS 并且PBID ~loevr NE 'D'(没有删除标识),取:PBIM-MATNR(物料编号);
SELECT matnr
FROM pbid
APPENDING TABLE @lt_pbim
WHERE werks = @gs_ywbk-werks
AND loevr NE 'D'.
SORT lt_pbim BY matnr.
DELETE ADJACENT DUPLICATES FROM lt_pbim COMPARING matnr.
******获取物料单位及物料描述
SELECT a~matnr,a~meins,b~maktx
FROM @lt_pbim AS c
LEFT JOIN mara AS a
ON c~matnr EQ a~matnr
LEFT JOIN makt AS b
ON b~matnr EQ c~matnr
AND b~spras EQ @sy-langu
INTO TABLE @DATA(lt_mara).
SORT lt_mara BY matnr.
CLEAR:gt_pbed,gs_pbed.
"将‘1)’、‘2)’取出的物料合并去除重复项,调用BAPI_MATERIAL_STOCK_REQ_LIST
LOOP AT lt_pbim INTO DATA(ls_pbim).
CLEAR:lv_matnr,lt_mrp_ind_lines,lt_mrp_ind_lines_werks,lt_mrp_ind_lines_mrp.
lv_matnr = ls_pbim-matnr.
"MRP范围不等于公司代码时,执行两次:一次根据公司代码查询 一次根据MRP范围查询
IF gs_ywbk-mrp NE gs_ywbk-werks.
CLEAR:lv_mrp.
lv_mrp = gs_ywbk-werks.
CALL FUNCTION 'BAPI_MATERIAL_STOCK_REQ_LIST'
EXPORTING
material = lv_matnr
plant = gs_ywbk-werks
mrp_area = lv_mrp
get_ind_lines = 'X'
TABLES
mrp_ind_lines = lt_mrp_ind_lines_werks.
CLEAR:lv_mrp.
lv_mrp = gs_ywbk-mrp.
CALL FUNCTION 'BAPI_MATERIAL_STOCK_REQ_LIST'
EXPORTING
material = lv_matnr
plant = gs_ywbk-werks
mrp_area = lv_mrp
get_ind_lines = 'X'
TABLES
mrp_ind_lines = lt_mrp_ind_lines_mrp.
APPEND LINES OF lt_mrp_ind_lines_werks TO lt_mrp_ind_lines.
APPEND LINES OF lt_mrp_ind_lines_mrp TO lt_mrp_ind_lines.
ELSE.
CLEAR:lv_mrp.
lv_mrp = gs_ywbk-werks.
CALL FUNCTION 'BAPI_MATERIAL_STOCK_REQ_LIST'
EXPORTING
material = lv_matnr
plant = gs_ywbk-werks
mrp_area = lv_mrp
get_ind_lines = 'X'
TABLES
mrp_ind_lines = lt_mrp_ind_lines_werks.
APPEND LINES OF lt_mrp_ind_lines_werks TO lt_mrp_ind_lines.
ENDIF.
IF lt_mrp_ind_lines IS NOT INITIAL.
"MRP_IND_LINES保留MRP元素为PP(独立需求)的记录MRP_ELEMENT_IND=' PP'
"SORT_DATE(需求日期)< =T-21(计划开始日期+21天)
DELETE lt_mrp_ind_lines WHERE mrp_element_ind <> 'PP'
OR sort_date >= lv_datum.
"ELEMNT_DATA(MRP元素数据)= 业务版块 ‘-’ 前字符 匹配MRP元素字符 (例如:PS-公共销售、BSF /PS240129,PS匹配PS)
LOOP AT lt_mrp_ind_lines ASSIGNING FIELD-SYMBOL(<fs_lines>).
IF <fs_lines>-elemnt_data IS NOT INITIAL.
TRY .
"取ELEMNT_DATA(MRP元素数据)'/'后作为业务板块
lv_str = segment( val = <fs_lines>-elemnt_data index = 2 sep = '/' ).
IF lv_str(lv_len) NE gs_ywbk-ywbk.
DELETE lt_mrp_ind_lines.
ENDIF.
CATCH cx_sy_strg_par_val INTO DATA(exc).
DELETE lt_mrp_ind_lines.
ENDTRY.
ELSE.
DELETE lt_mrp_ind_lines.
ENDIF.
ENDLOOP.
LOOP AT lt_mrp_ind_lines INTO DATA(ls_lines).
gs_pbed-matnr = ls_pbim-matnr.
gs_pbed-werks = gs_ywbk-werks.
gs_pbed-zywbk = gs_ywbk-ywbk.
gs_pbed-pdatu = ls_lines-sort_date.
gs_pbed-plnmg = abs( ls_lines-rec_reqd_qty )."数量绝对值
READ TABLE lt_mara INTO DATA(ls_mara)
WITH KEY matnr = ls_pbim-matnr
BINARY SEARCH.
IF sy-subrc EQ 0.
gs_pbed-meins = ls_mara-meins.
ENDIF.
APPEND gs_pbed TO gt_pbed.
ENDLOOP.
ENDIF.
ENDLOOP.
CLEAR:ls_ztpp0033,lt_ztpp0033.
"导入周次forecast(作为前3周滚动历史数据) 。
LOOP AT gt_pbed INTO DATA(ls_pbed).
"获取seq_key
PERFORM frm_get_seq_key CHANGING ls_ztpp0033-seq_key.
"物料
ls_ztpp0033-matnr = ls_pbed-matnr.
READ TABLE lt_mara INTO ls_mara
WITH KEY matnr = ls_pbed-matnr
BINARY SEARCH.
IF sy-subrc EQ 0.
"物料描述
ls_ztpp0033-matnr = ls_pbed-matnr.
ENDIF.
"开始日期
ls_ztpp0033-datve = p_datum.
"计划日期
ls_ztpp0033-pdatu = ls_pbed-pdatu.
"数量
ls_ztpp0033-plnmg = ls_pbed-plnmg.
"工厂
ls_ztpp0033-werks = ls_pbed-werks.
"业务板块
ls_ztpp0033-zywbk = ls_pbed-zywbk.
"时间戳赋值
ls_ztpp0033-erdat = sy-datum.
ls_ztpp0033-erzet = sy-uzeit.
ls_ztpp0033-ernam = sy-uname.
ls_ztpp0033-aedat = sy-datum.
ls_ztpp0033-aenam = sy-uname.
ls_ztpp0033-aezet = sy-uzeit.
APPEND ls_ztpp0033 TO lt_ztpp0033.
CLEAR:gs_pbed,ls_ztpp0033.
ENDLOOP.
IF lt_ztpp0033 IS NOT INITIAL.
MODIFY ztpp0033 FROM TABLE lt_ztpp0033.
ENDIF.
ENDFORM.
MD62删除需求计划->BDC
*&---------------------------------------------------------------------*
*& Form frm_maintenance
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> <FS_ALV>_WERKS
*& --> LV_VERSBDEL_PBDNR
*& --> <FS_ALV>_BERID
*&---------------------------------------------------------------------*
FORM frm_maintenance USING p_lv_matnr
p_lv_werks.
DATA:lv_message TYPE string.
DATA:lv_mon(2).
PERFORM bdc_dynpro USING 'SAPMM60X' '0106'.
PERFORM bdc_field USING 'BDC_CURSOR'
'AM60X-MATNR'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ANF1'.
PERFORM bdc_field USING 'AM60X-MATAW'
'X'.
PERFORM bdc_field USING 'AM60X-MATNR'
p_lv_matnr.
PERFORM bdc_field USING 'AM60X-WERKS'
p_lv_werks.
PERFORM bdc_field USING 'AM60X-VERAK'
'X'.
PERFORM bdc_dynpro USING 'SAPLM60E' '0200'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ALMK'.
PERFORM bdc_field USING 'BDC_CURSOR'
'PBPT-MATNR(01)'.
PERFORM bdc_dynpro USING 'SAPLM60E' '0200'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=POLO'.
PERFORM bdc_field USING 'BDC_CURSOR'
'PBPT-MATNR(01)'.
PERFORM bdc_dynpro USING 'SAPLSPO1' '0500'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=OPT1'.
PERFORM bdc_dynpro USING 'SAPLM60E' '0200'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SICH'.
PERFORM bdc_field USING 'BDC_CURSOR'
'PBPT-MATNR(01)'.
gs_opt-dismode = 'N'.
gs_opt-updmode = 'S'.
CALL TRANSACTION 'MD62' USING bdcdata
OPTIONS FROM gs_opt
MESSAGES INTO gt_messtab.
CLEAR:gt_messtab[],gt_messtab,bdcdata[],bdcdata.
ENDFORM.