MD61 创建:
BAPI: BAPI_REQUIREMENTS_CREATE
CLEAR: lv_error,
ls_requirements_item,
lt_requirements_schedule_in,
ls_requirements_schedule_in,
lt_return_n,
ls_return_n,
lv_reqmtsplannumber.
"工厂
ls_requirements_item-plant = lv_werks.
"MRP AREA
* ls_requirements_item-mrp_area = lv_berid.
"
* ls_requirements_item-req_number = gs_data-pbdnr.
"激活
ls_requirements_item-vers_activ = 'X'.
"版本
ASSIGN COMPONENT 'VERSB' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_requirements_item-version = <fs_val>.
"Material
ls_requirements_item-material = lv_matnr.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname, lv_qty.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_qty.
lv_qty = <fs_val>.
IF lv_qty IS INITIAL. "如果数量为0,则不需要添加到销售订单的行项目上去
CONTINUE.
ENDIF.
"日期
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_week, lv_date_n.
lv_week = <fs_val>.
* 转成内部日期
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = lv_week
IMPORTING
date = lv_date_n
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ls_requirements_schedule_in-req_date = lv_date_n.
"计划周期
ls_requirements_schedule_in-date_type = '2'.
"数量
ls_requirements_schedule_in-req_qty = lv_qty.
APPEND ls_requirements_schedule_in TO lt_requirements_schedule_in.
CLEAR: ls_requirements_schedule_in.
ENDLOOP.
CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE'
EXPORTING
requirements_item = ls_requirements_item
* REQUIREMENT_PARAM =
* DO_COMMIT = 'X'
* UPDATE_MODE = 'X'
* REFER_TYPE = ' '
* PROFILID = ' '
IMPORTING
* MATERIAL =
* PLANT =
* REQUIREMENTSTYPE =
* VERSION =
reqmtsplannumber = lv_reqmtsplannumber
* MRP_AREA =
* MATERIAL_EVG =
* REQ_PLAN_ID_EXT =
* REQ_SEGMENT =
* MATERIAL_LONG =
* REQ_SEG_LONG =
TABLES
requirements_schedule_in = lt_requirements_schedule_in
* REQUIREMENTS_CHAR_IN =
return = lt_return_n.
MD62 修改:
BAPI: BAPI_REQUIREMENTS_CHANGE
* 修改计划独立需求
IF sy-subrc = 0."修改计划独立需求
CLEAR: lv_error,
lt_requirements_schedule_in,
ls_requirements_schedule_in,
lt_return_n,
ls_return_n,
lv_material.
"版本
ASSIGN COMPONENT 'VERSB' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_versb = <fs_val>.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname, lv_qty.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_qty.
lv_qty = <fs_val>.
IF lv_qty IS INITIAL. "如果数量为0,则不需要添加到销售订单的行项目上去
CONTINUE.
ENDIF.
"日期
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_week, lv_date_n.
lv_week = <fs_val>.
* 转成内部日期
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = lv_week
IMPORTING
date = lv_date_n
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ls_requirements_schedule_in-req_date = lv_date_n.
"计划周期
ls_requirements_schedule_in-date_type = '2'.
"数量
ls_requirements_schedule_in-req_qty = lv_qty.
APPEND ls_requirements_schedule_in TO lt_requirements_schedule_in.
CLEAR: ls_requirements_schedule_in.
ENDLOOP.
lv_material = lv_matnr.
CLEAR: lv_requirementstype.
SELECT SINGLE bedae INTO lv_requirementstype
FROM pbim
WHERE matnr = lv_matnr
AND werks = lv_werks
AND versb = lv_versb.
CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
EXPORTING
material = lv_material
plant = lv_werks
requirementstype = lv_requirementstype
version = lv_versb
reqmtsplannumber = ''
vers_activ = 'X'
* REQUIREMENT_PARAM =
* MRP_AREA =
* DO_COMMIT = 'X'
* UPDATE_MODE = 'X'
delete_old = 'X'
* NO_WITHDR = ' '
* MATERIAL_EVG =
* REQ_PLAN_ID_EXT =
* REQ_SEGMENT =
* MATERIAL_LONG =
* REQ_SEG_LONG =
* IMPORTING
* REQUIREMENT_ITEM_OUT =
TABLES
requirements_schedule_in = lt_requirements_schedule_in
* REQUIREMENTS_CHAR_IN =
return = lt_return_n.
完成代码:
*&---------------------------------------------------------------------*
*& Report ZPPE003
*&---------------------------------------------------------------------*
*&销售预测批量导入
*&---------------------------------------------------------------------*
REPORT zppe003.
*--------------------------------------------------------------------*
*变量定义
*--------------------------------------------------------------------*
TYPE-POOLS: icon.
TABLES: pbim.
TYPES: BEGIN OF gty_data,
werks TYPE marc-werks,
matnr TYPE marc-matnr,
maktx TYPE makt-maktx,
kunnr TYPE kna1-kunnr,
END OF gty_Data.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
dy_table_u TYPE REF TO data,
dy_line_u TYPE REF TO data,
gt_structure TYPE lvc_t_fcat,
gs_structure TYPE lvc_s_fcat,
gt_structure_u TYPE lvc_t_fcat,
gs_structure_u TYPE lvc_s_fcat,
gs_layout TYPE lvc_s_layo.
DATA : gt_excel LIKE TABLE OF alsmex_tabline.
DATA : gs_excel TYPE alsmex_tabline. "excel structure
DATA: gt_data TYPE TABLE OF gty_data,
gs_data TYPE gty_data.
DATA: gv_error TYPE c. "报错标识
FIELD-SYMBOLS: <dyntab> TYPE STANDARD TABLE,
<fs_val> TYPE any,
<fs_wa> TYPE any,
<dyntab_u> TYPE STANDARD TABLE,
<fs_val_u> TYPE any,
<fs_wa_u> TYPE any.
*--------------------------------------------------------------------*
*选择屏幕
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_werks TYPE pbim-werks OBLIGATORY, "工厂
p_file TYPE rlgrap-filename OBLIGATORY. "文件名
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: p_r1 TYPE c RADIOBUTTON GROUP grp DEFAULT 'X'. "带客户销售预测导入
SELECTION-SCREEN COMMENT (50) TEXT-002 FOR FIELD p_r1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: p_r2 TYPE c RADIOBUTTON GROUP grp. "计划独立需求导入
SELECTION-SCREEN COMMENT (50) TEXT-003 FOR FIELD p_r2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.
INITIALIZATION.
PERFORM frm_init.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM frm_select_path."选择路径
START-OF-SELECTION.
PERFORM frm_upload_data."数据导入
PERFORM frm_process_data. "处理数据
END-OF-SELECTION.
PERFORM frm_fieldcat_set.
PERFORM frm_layout_set.
PERFORM frm_show_alv. "处理消息输出
*&---------------------------------------------------------------------*
*& Form frm_select_path
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_select_path .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = ',*.* ,*.*.'
mode = '0'
title = '请选择要上传的信息文件'
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_upload_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_upload_data .
*&--- 导入内表
*&---EXCEL导入内表
TYPES: BEGIN OF lty_week,
week TYPE scal-week,
END OF lty_week,
BEGIN OF lty_data,
row TYPE alsmex_tabline-row,
week TYPE scal-week,
col TYPE alsmex_tabline-col,
value TYPE alsmex_tabline-value,
END OF lty_data.
DATA : lt_excel LIKE TABLE OF alsmex_tabline.
DATA : ls_excel TYPE alsmex_tabline. "excel structure
FIELD-SYMBOLS : <field>.
FIELD-SYMBOLS: <lfs_excel> TYPE alsmex_tabline.
DATA: lv_date TYPE scal-date,
lv_week TYPE scal-week,
lv_date_c TYPE string,
lt_week TYPE TABLE OF lty_week,
ls_week TYPE lty_week,
lt_data TYPE TABLE OF lty_data,
ls_data TYPE lty_data,
ls_data_c TYPE lty_data,
ls_data_n TYPE lty_data,
lt_data_n TYPE TABLE OF lty_data,
lv_col TYPE kcd_ex_col_n.
CLEAR: gt_data,
gs_data,
lt_data,
ls_data,
ls_data_c,
ls_data_n,
lt_data_n,
lv_col.
CLEAR: lv_date,
lv_week,
lv_date_c,
lt_week,
ls_week.
*&---读取EXCEL到内表
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = 1
i_begin_row = 1
i_end_col = 112
i_end_row = 9999
TABLES
intern = lt_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* MESSAGE 'EXCEL导入错误或文件路径错误' TYPE 'E'.
ENDIF.
IF p_r2 IS NOT INITIAL.
* 检查导入模板上的每一个日期所属是每年的哪一周,然后如果日期在同一周的需要进行数量汇总,放在同一周进行导入到系统
* 因为独立需求是按照周进行维护的
LOOP AT lt_excel ASSIGNING <lfs_excel> WHERE row = '0001' AND col > '0004'.
MOVE-CORRESPONDING <lfs_excel> TO ls_data.
CLEAR: lv_date_c, lv_date, lv_week.
lv_date_c = <lfs_excel>-value.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_date_c
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = lv_date
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CALL FUNCTION 'DATE_GET_WEEK' "获取日期对应的当前周
EXPORTING
date = lv_date
IMPORTING
week = lv_week
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ls_data-week = lv_week.
READ TABLE lt_week INTO ls_week WITH KEY week = lv_week. "如果算出的周在前面的日期中已经出现过,则打上删除标识
IF sy-subrc = 0.
<lfs_excel>-value = '$DELETE$'.
ELSE.
ls_week-week = lv_week.
COLLECT ls_week INTO lt_week.
CLEAR: ls_week.
ENDIF.
APPEND ls_data TO lt_data.
CLEAR: ls_data.
ENDLOOP.
* 统计每一个数量对应的当前周
LOOP AT lt_excel ASSIGNING <lfs_excel> WHERE row > '0001'
AND col > '0004'.
MOVE-CORRESPONDING <lfs_excel> TO ls_data.
READ TABLE lt_data INTO ls_data_c WITH KEY row = '0001' col = <lfs_excel>-col.
IF sy-subrc = 0.
ls_data-week = ls_data_c-week.
ENDIF.
APPEND ls_data TO lt_data.
CLEAR: ls_data.
ENDLOOP.
* 对在同一周的数量进行汇总,然后保留都同属同一周最小的那个导入日期
LOOP AT lt_data INTO ls_data WHERE row > '0001'.
CLEAR: ls_data_c.
ls_data_c = ls_data.
AT NEW week.
ls_data_n = ls_data_c.
CLEAR: ls_data_n-value.
ENDAT.
ls_data_n-value = ls_data_n-value + ls_data_c-value.
AT END OF week.
APPEND ls_data_n TO lt_data_n.
CLEAR: ls_data_n.
ENDAT.
CLEAR: ls_data.
ENDLOOP.
* 对属于同一周的其它日期都打上删除标识
LOOP AT lt_excel ASSIGNING <lfs_excel> WHERE row > '0001' AND col > '0004'.
READ TABLE lt_data_n INTO ls_data_n WITH KEY row = <lfs_excel>-row
col = <lfs_excel>-col.
IF sy-subrc = 0.
<lfs_excel>-value = ls_data_n-value.
ELSE.
<lfs_excel>-value = '$DELETE$'.
ENDIF.
CONDENSE <lfs_excel>-value.
ENDLOOP.
* 排除打上删除标识的行,这样就形成最终需要导入系统的数据
DELETE lt_excel WHERE value = '$DELETE$'.
* 对即将导入的数据列进行重新给值
LOOP AT lt_excel ASSIGNING <lfs_excel>.
CLEAR: <lfs_excel>-col.
CLEAR: ls_excel.
ls_excel = <lfs_excel>.
AT NEW row.
CLEAR: lv_col.
ENDAT.
lv_col = lv_col + 1.
<lfs_excel>-col = lv_col.
ENDLOOP.
ENDIF.
LOOP AT lt_excel INTO ls_excel WHERE row = '0001' AND col > '0004'. "获取Excel标题日期信息
gs_excel = ls_excel.
APPEND gs_excel TO gt_excel.
CLEAR: gs_excel, ls_excel.
ENDLOOP.
PERFORM frm_create_structure TABLES lt_excel. "定义内表结构
PERFORM frm_create_dynamic_table. "按照定义的内表结构,生成一个内表
* dynamic table data fill
ASSIGN dy_table_u->* TO <dyntab_u>.
CREATE DATA dy_line_u LIKE LINE OF <dyntab_u>.
ASSIGN dy_line_u->* TO <fs_wa_u>.
LOOP AT lt_excel INTO ls_excel WHERE row > '0001'.
IF ls_excel-col = '0001'. "工厂
gs_data-werks = ls_excel-value.
ENDIF.
IF ls_excel-col = '0002'. "物料编码
gs_data-matnr = ls_excel-value.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = gs_data-matnr
IMPORTING
output = gs_data-matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
IF ls_excel-col = '0003'. "物料描述
gs_data-maktx = ls_excel-value.
ENDIF.
IF ls_excel-col = '0004'. "客户编码
gs_data-kunnr = ls_excel-value.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_data-kunnr
IMPORTING
output = gs_data-kunnr.
ENDIF.
ASSIGN COMPONENT ls_excel-col OF STRUCTURE <fs_wa_u> TO <fs_val_u>.
<fs_val_u> = ls_excel-value.
AT END OF row.
IF gs_data-werks <> p_werks.
CONTINUE.
ENDIF.
APPEND <fs_wa_u> TO <dyntab_u>.
CLEAR : <fs_wa_u>.
COLLECT gs_data INTO gt_data. "获取导入数据的前四列信息
CLEAR: gs_data.
ENDAT.
CLEAR : ls_excel.
ENDLOOP.
*
* SORT gt_data.
* DELETE ADJACENT DUPLICATES FROM gt_data COMPARING ALL FIELDS.
FREE : lt_excel.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_process_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_process_data .
DATA: lv_index TYPE i,
lv_index_c TYPE string,
lv_fname TYPE string,
lt_marc TYPE TABLE OF marc,
ls_marc TYPE marc,
lv_matnr TYPE marc-matnr,
lv_werks TYPE marc-werks,
lv_kunnr TYPE kna1-kunnr,
lt_data TYPE TABLE OF gty_data,
ls_Data TYPE gty_Data,
lt_ppt003 TYPE TABLE OF zppt_003,
ls_ppt003 TYPE zppt_003,
lv_qty TYPE vbep-wmeng,
lv_qty_b TYPE vbep-wmeng,
lv_qty_c TYPE vbep-wmeng,
lv_qty_t TYPE vbep-wmeng,
lv_qty_s TYPE vbep-wmeng,
lv_date_s TYPE sy-datum,
lv_date_e TYPE sy-datum,
lv_date_c TYPE string,
lv_date_n TYPE sy-datum,
lv_week TYPE scal-week,
lv_weeks TYPE string.
DATA: lr_auart TYPE RANGE OF vbak-auart,
ls_auart LIKE LINE OF lr_auart.
DATA: lt_pbim TYPE TABLE OF pbim,
ls_pbim TYPE pbim,
lv_versb TYPE versb.
CLEAR: lt_pbim,
ls_pbim,
lv_versb.
DATA: lv_meins TYPE mara-meins,
lv_maktx TYPE makt-maktx.
CLEAR: lv_index,
lv_index_c,
lv_fname,
lt_marc,
ls_marc,
lv_matnr,
lv_werks,
lv_kunnr,
lt_data,
ls_Data,
lt_ppt003,
ls_ppt003,
lv_qty,
lv_qty_b,
lv_qty_c,
lv_qty_t,
lv_qty_s,
lv_date_s,
lv_date_e,
lr_auart,
ls_auart,
lv_date_c,
lv_date_n,
lv_week,
gv_error,
lv_meins,
lv_maktx,
lv_weeks.
* 获取销售订单类型-汇总计算销售订单类型配置表
SELECT * INTO TABLE lt_ppt003
FROM zppt_003.
LOOP AT lt_ppt003 INTO ls_ppt003.
ls_auart-sign = 'I'.
ls_auart-option = 'EQ'.
ls_auart-low = ls_ppt003-auart.
APPEND ls_auart TO lr_auart.
CLEAR: ls_auart.
ENDLOOP.
* dynamic table data fill
ASSIGN dy_table->* TO <dyntab>.
CREATE DATA dy_line LIKE LINE OF <dyntab>.
ASSIGN dy_line->* TO <fs_wa>.
LOOP AT <dyntab_u> ASSIGNING <fs_wa_u>.
CLEAR: lv_index.
MOVE-CORRESPONDING <fs_wa_u> TO <fs_wa>.
LOOP AT gt_excel INTO gs_excel.
CLEAR: lv_index_c, lv_fname.
lv_index = lv_index + 1.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = gs_excel-value.
ENDLOOP.
APPEND <fs_wa> TO <dyntab>.
CLEAR: <fs_wa>, <fs_wa_u>.
ENDLOOP.
IF p_r1 IS NOT INITIAL. "销售预测
CLEAR: lt_data.
IF gt_data IS NOT INITIAL.
SELECT * INTO TABLE lt_marc
FROM marc
FOR ALL ENTRIES IN gt_data
WHERE matnr = gt_data-matnr
AND werks = gt_data-werks.
LOOP AT <dyntab> ASSIGNING <fs_wa>.
CLEAR: lv_matnr,
lv_werks,
lv_kunnr.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_matnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lv_matnr
IMPORTING
output = lv_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* 获取物料单位
SELECT SINGLE meins INTO lv_meins
FROM mara
WHERE matnr = lv_matnr.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_meins.
* 获取物料描述
SELECT SINGLE maktx INTO lv_maktx
FROM makt
WHERE spras = sy-langu
AND matnr = lv_matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_maktx.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_werks = <fs_val>.
ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_kunnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_kunnr
IMPORTING
output = lv_kunnr.
* 获取物料的独立需求数据
IF p_r2 = 'X'.
SELECT * INTO TABLE lt_pbim
FROM pbim
WHERE matnr = lv_matnr
AND werks = lv_werks.
SORT lt_pbim.
ENDIF.
ASSIGN COMPONENT 'AUART' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'ZPLN'.
****************************************************************************************************
* 检查导入数据
****************************************************************************************************
* 按照工厂+物料编码+客户编码,检查导入模板内数据,若有重复项,则重复行校验结果显示E,
* 消息文本显示:导入模板中含重复项,请检查!(The The import template contains duplicate items, please check!)
READ TABLE lt_data INTO ls_data WITH KEY matnr = lv_matnr
werks = lv_werks
kunnr = lv_kunnr.
IF sy-subrc = 0.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '导入模板中含重复项,请检查!'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'The The import template contains duplicate items, please check!'.
ENDIF.
gv_error = 'X'.
RETURN.
ELSE.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '检验成功'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'Validation is OK'.
ENDIF.
ls_data-matnr = lv_matnr.
ls_data-werks = lv_werks.
ls_data-kunnr = lv_kunnr.
COLLECT ls_data INTO lt_data.
CLEAR: ls_Data.
ENDIF.
* 校验导入行项目中:工厂+物料编码在MARC表是否存在数据
* ,若不存在则校验结果显示E,消息文本显示:物料编码在工厂下不存在,请检查数据!
* (The material code does not exist in the plant, please check the data!)
READ TABLE lt_marc INTO ls_marc WITH KEY matnr = lv_matnr
werks = lv_werks.
IF sy-subrc = 0.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '检验成功'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'Validation is OK'.
ENDIF.
ELSE.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '物料编码在工厂下不存在,请检查数据!'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'The material code does not exist in the plant, please check the data!'.
ENDIF.
gv_error = 'X'.
RETURN.
ENDIF.
****************************************************************************************************
* 填入数据
****************************************************************************************************
IF lt_ppt003 IS NOT INITIAL.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_date_c, lv_date_n.
lv_date_c = <fs_val>.
* 转成内部日期
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_date_c
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = lv_date_n
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CLEAR: lv_qty,
lv_date_s,
lv_date_e.
* 获取当前日期周周一周日的日期
CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
EXPORTING
date = lv_date_n
IMPORTING
week = lv_week
monday = lv_date_s
sunday = lv_date_e.
* 当期销售订单数量:VBAP表中,以工厂+物料+售达方(=客户编码)+订单类型(VBAK-AUART)=销售订单类型配置表中的范围,
* 取订销售订单号+行号;VBEP表中,以销售订单号+行号,取交货日期(VBEP-EDATU)+订单数量(VBEP-WMENG),将预测日期所在周的交货日期的订单数量进行汇总;
SELECT SUM( vbep~wmeng )
INTO lv_qty
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
WHERE vbak~kunnr = lv_kunnr
AND vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'DQSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_qty.
* 预测导入数量
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCZL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
lv_qty_t = <fs_val>. "预测总量
lv_qty_s = lv_qty_t - lv_qty.
IF lv_qty_s < 0.
lv_qty_s = 0.
ENDIF.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_qty_s. "预测导入数量
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.
ELSE. "独立需求预测数据
CLEAR: lt_data.
IF gt_data IS NOT INITIAL.
SELECT * INTO TABLE lt_marc
FROM marc
FOR ALL ENTRIES IN gt_data
WHERE matnr = gt_data-matnr
AND werks = gt_data-werks.
LOOP AT <dyntab> ASSIGNING <fs_wa>.
CLEAR: lv_matnr,
lv_werks,
lv_kunnr.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_matnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lv_matnr
IMPORTING
output = lv_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* 获取物料单位
SELECT SINGLE meins INTO lv_meins
FROM mara
WHERE matnr = lv_matnr.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_meins.
* 获取物料描述
SELECT SINGLE maktx INTO lv_maktx
FROM makt
WHERE spras = sy-langu
AND matnr = lv_matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_maktx.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_werks = <fs_val>.
ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_kunnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_kunnr
IMPORTING
output = lv_kunnr.
* 获取物料的独立需求数据
IF p_r2 = 'X'.
SELECT * INTO TABLE lt_pbim
FROM pbim
WHERE matnr = lv_matnr
AND werks = lv_werks.
SORT lt_pbim BY versb DESCENDING.
CLEAR: lv_versb.
ASSIGN COMPONENT 'VERSB' OF STRUCTURE <fs_wa> TO <fs_val>. "版本号
READ TABLE lt_pbim INTO ls_pbim WITH KEY matnr = lv_matnr
werks = lv_werks
zkunnr = lv_kunnr..
IF sy-subrc = 0. "如果读到对应的版本号,按照现有的版本号处理
lv_versb = ls_pbim-versb.
* lv_versb = lv_versb + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_versb
IMPORTING
output = lv_versb.
CONDENSE lv_versb.
<fs_val> = lv_versb.
ELSE.
CLEAR: lv_versb.
READ TABLE lt_pbim INTO ls_pbim INDEX 1. "检查是否创建过计划独立需求
IF sy-subrc = 0. "读取现有的最大版本号,然后在此版本号上继续流水+1
lv_versb = ls_pbim-versb.
lv_versb = lv_versb + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_versb
IMPORTING
output = lv_versb.
CONDENSE lv_versb.
<fs_val> = lv_versb.
ELSE.
<fs_val> = '00'. "如果没有创建,默认为00
ENDIF.
ENDIF.
ENDIF.
IF p_r1 = 'X'.
ASSIGN COMPONENT 'AUART' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'ZPLN'.
ENDIF.
****************************************************************************************************
* 检查导入数据
****************************************************************************************************
* 按照工厂+物料编码+客户编码,检查导入模板内数据,若有重复项,则重复行校验结果显示E,
* 消息文本显示:导入模板中含重复项,请检查!(The The import template contains duplicate items, please check!)
READ TABLE lt_data INTO ls_data WITH KEY matnr = lv_matnr
werks = lv_werks
kunnr = lv_kunnr.
IF sy-subrc = 0.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '导入模板中含重复项,请检查!'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'The The import template contains duplicate items, please check!'.
ENDIF.
gv_error = 'X'.
RETURN.
ELSE.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '检验成功'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'Validation is OK'.
ENDIF.
ls_data-matnr = lv_matnr.
ls_data-werks = lv_werks.
ls_data-kunnr = lv_kunnr.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.
ENDIF.
* 校验导入行项目中:工厂+物料编码在MARC表是否存在数据
* ,若不存在则校验结果显示E,消息文本显示:物料编码在工厂下不存在,请检查数据!
* (The material code does not exist in the plant, please check the data!)
READ TABLE lt_marc INTO ls_marc WITH KEY matnr = lv_matnr
werks = lv_werks.
IF sy-subrc = 0.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '检验成功'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'Validation is OK'.
ENDIF.
ELSE.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '物料编码在工厂下不存在,请检查数据!'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'The material code does not exist in the plant, please check the data!'.
ENDIF.
gv_error = 'X'.
RETURN.
ENDIF.
****************************************************************************************************
* 填入数据
****************************************************************************************************
IF lt_ppt003 IS NOT INITIAL.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_date_c, lv_date_n.
lv_date_c = <fs_val>.
* 转成内部日期
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_date_c
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = lv_date_n
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CLEAR: lv_qty,
lv_date_s,
lv_date_e.
* 获取当前日期周周一周日的日期
CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
EXPORTING
date = lv_date_n
IMPORTING
week = lv_week
monday = lv_date_s
sunday = lv_date_e.
CLEAR: lv_weeks.
CONCATENATE lv_week+0(4) '.' lv_week+4(2) INTO lv_weeks.
CONDENSE lv_weeks.
<fs_val> = lv_weeks.
IF lv_kunnr IS NOT INITIAL.
* VBAP表中,以工厂+物料+客户编码(若模板上为空,则不限制客户编码),取订销售订单号+行号且VBAP-ABGRU=“空”;
* VBEP表中,以销售订单号+行号,取交货日期(VBEP-EDATU)+订单数量(VBEP-WMENG),将交货日期在对应周的订单数量进行汇总
SELECT SUM( vbep~wmeng )
INTO lv_qty
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
WHERE vbak~kunnr = lv_kunnr
AND vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbap~abgru = ''
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e.
"通过LIPS-VGBEL=VBAP-VBELN且VBAP-POSNR=LIPS-VGPOS且LIPS-WBSTA等于“C”取LIPS-LFIMG值
SELECT SUM( lips~lfimg )
INTO lv_qty_b
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
INNER JOIN lips ON vbap~vbeln = lips~vgbel
AND vbap~posnr = lips~vgpos
WHERE vbak~kunnr = lv_kunnr
AND vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbap~abgru = ''
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e
AND lips~wbsta = 'C'.
* VBAP表中,以工厂+物料+客户编码(若模板上为空,则不限制客户编码),取订销售订单号+行号且VBAP-ABGRU不等于“空”;
* 通过订单号及行项目号LIPS-VGBEL=VBAP-VBELN且VBAP-POSNR=LIPS-VGPOS且LIPS-WBSTA不等于“C”取LIPS-LFIMG值
SELECT SUM( lips~lfimg )
INTO lv_qty_c
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
INNER JOIN lips ON vbap~vbeln = lips~vgbel
AND vbap~posnr = lips~vgpos
WHERE vbak~kunnr = lv_kunnr
AND vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbap~abgru <> ''
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e
AND lips~wbsta <> 'C'.
ELSE.
* VBAP表中,以工厂+物料+客户编码(若模板上为空,则不限制客户编码),取订销售订单号+行号且VBAP-ABGRU=“空”;
* VBEP表中,以销售订单号+行号,取交货日期(VBEP-EDATU)+订单数量(VBEP-WMENG),将交货日期在对应周的订单数量进行汇总
SELECT SUM( vbep~wmeng )
INTO lv_qty
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
INNER JOIN lips ON vbap~vbeln = lips~vgbel
AND vbap~posnr = lips~vgpos
WHERE vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbap~abgru = ''
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e
AND lips~wbsta = 'C'.
"通过LIPS-VGBEL=VBAP-VBELN且VBAP-POSNR=LIPS-VGPOS且LIPS-WBSTA等于“C”取LIPS-LFIMG值
SELECT SUM( lips~lfimg )
INTO lv_qty_b
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
INNER JOIN lips ON vbap~vbeln = lips~vgbel
AND vbap~posnr = lips~vgpos
WHERE vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbap~abgru = ''
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e
AND lips~wbsta = 'C'.
* VBAP表中,以工厂+物料+客户编码(若模板上为空,则不限制客户编码),取订销售订单号+行号且VBAP-ABGRU不等于“空”;
* 通过订单号及行项目号LIPS-VGBEL=VBAP-VBELN且VBAP-POSNR=LIPS-VGPOS且LIPS-WBSTA不等于“C”取LIPS-LFIMG值
SELECT SUM( lips~lfimg )
INTO lv_qty_c
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
INNER JOIN vbep ON vbap~vbeln = vbep~vbeln
AND vbap~posnr = vbep~posnr
INNER JOIN lips ON vbap~vbeln = lips~vgbel
AND vbap~posnr = lips~vgpos
WHERE vbak~kunnr = lv_kunnr
AND vbap~matnr = lv_matnr
AND vbap~werks = lv_werks
AND vbap~abgru <> ''
AND vbak~auart IN lr_auart
AND vbep~edatu >= lv_date_s
AND vbep~edatu <= lv_date_e
AND lips~wbsta <> 'C'.
ENDIF.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'DQSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_qty - lv_qty_b + lv_qty_c.
* 预测导入数量
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCZL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
lv_qty_t = <fs_val>. "预测总量
lv_qty_s = lv_qty_t - lv_qty.
IF lv_qty_s < 0.
lv_qty_s = 0.
ENDIF.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = lv_qty_s. "预测导入数量
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_fieldcat_set
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_fieldcat_set .
*********************************
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_layout_set
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_layout_set .
gs_layout-zebra = 'X'.
gs_layout-cwidth_opt = 'X'.
gs_layout-box_fname = 'SEL'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_show_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_show_alv .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
i_callback_program = sy-repid
i_callback_pf_status_set = 'FRM_SET_PFSTATUS'
i_callback_user_command = 'FRM_USER_COMMAND'
* I_CALLBACK_TOP_OF_PAGE = ' '
i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_structure
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS_LVC =
* IT_SORT_LVC =
* IT_FILTER_LVC =
* IT_HYPERLINK =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT_LVC =
* IS_REPREP_ID_LVC =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP =
* I_HTML_HEIGHT_END =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO_LVC =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = <dyntab>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_init
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_init .
CLEAR: dy_table,
dy_line,
gt_structure,
gs_structure.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_create_structure
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_create_structure TABLES lt_excel STRUCTURE alsmex_tabline.
DATA: ls_excel TYPE alsmex_tabline,
lv_index TYPE i,
lv_weeks TYPE scal-week,
lv_date TYPE scal-date,
lv_index_c TYPE string,
lv_weeks_c TYPE string,
lv_colname TYPE string,
lv_fname TYPE string.
CLEAR: ls_excel,
lv_index,
lv_weeks,
lv_date,
lv_index_c,
lv_weeks_c,
lv_colname,
lv_fname.
DEFINE fcat.
gs_structure-fieldname = &1.
gs_structure-scrtext_l = &2.
gs_structure-edit = &3.
gs_structure-icon = &4.
gs_structure-intlen = &5.
gs_structure-outputlen = &5.
APPEND gs_structure TO gt_structure.
CLEAR gs_structure.
END-OF-DEFINITION.
DEFINE fcat_u.
gs_structure_u-fieldname = &1.
gs_structure_u-scrtext_l = &2.
gs_structure_u-intlen = &3.
APPEND gs_structure_u TO gt_structure_u.
CLEAR gs_structure_u.
END-OF-DEFINITION.
* ALV报表结构
IF sy-langu = 'E'.
fcat: 'STATUS' 'Status' '' 'X' 4,
'MSGTY' 'Message Type' '' '' 4,
'MSGTXT' 'Message Text' '' '' 220,
'ZINTYPE' 'Upload Type' '' '' 20,
'ZSCODE' 'Serial Number' '' '' 20,
'LAEDA' 'Import Date' '' '' 10,
'AENAM' 'Import Person' '' '' 20,
'ZDRBBN' 'Import version No.' '' '' 30.
IF p_r1 = 'X'.
fcat: 'AUART' 'Forecast Doc. Type' '' '' 10.
ENDIF.
fcat: 'WERKS' 'Plant' '' '' 4,
'KUNNR' 'Customer No.' '' '' 20,
'MATNR' 'Material No.' '' '' 40,
'MAKTX' 'Material Description' '' '' 40,
'MEINS' 'Unit' '' '' 5.
IF p_r2 = 'X'.
fcat: 'VERSB' 'Version No.' '' '' 15.
ENDIF.
* fcat:'CHK_BOX' 'Checkbox' '' '' 1.
ELSEIF sy-langu = '1'.
fcat:'SEL' '' '' '' 1.
fcat: 'STATUS' '状态' '' 'X' 4,
'MSGTY' '消息类型' '' '' 4,
'MSGTXT' '消息文本' '' '' 220,
'ZINTYPE' '上传类型' '' '' 20,
'ZSCODE' '流水码' '' '' 20,
'LAEDA' '导入日期' '' '' 10,
'AENAM' '导入人员' '' '' 20,
'ZDRBBN' '导入版本号' '' '' 30.
IF p_r1 = 'X'. "销售预测
fcat: 'AUART' '预测凭证类型' '' '' 10.
ENDIF.
fcat: 'WERKS' '工厂' '' '' 4,
'KUNNR' '客户编码' '' '' 20,
'MATNR' '物料编码' '' '' 40,
'MAKTX' '物料描述' '' '' 40,
'MEINS' '单位' '' '' 5.
IF p_r2 = 'X'.
fcat: 'VERSB' '版本号' '' '' 15.
ENDIF.
ENDIF.
LOOP AT lt_excel INTO ls_excel WHERE row = '0001'
AND col > '0004'.
* 预测日期/需求日期
CLEAR: lv_fname.
lv_index = lv_index + 1.
CLEAR: lv_index_c.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
CLEAR: lv_colname.
IF p_r1 IS NOT INITIAL.
IF sy-langu = '1'.
lv_colname = '预测日期'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Forecast Date'.
ENDIF.
ELSE.
IF sy-langu = '1'.
lv_colname = '需求日期'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Requirement Date'.
ENDIF.
ENDIF.
CONDENSE lv_colname.
fcat: lv_fname lv_colname '' '' 20.
* CLEAR: lv_date, lv_weeks.
* lv_date = ls_excel-value.
* CALL FUNCTION 'DATE_GET_WEEK'
* EXPORTING
* date = lv_date
* IMPORTING
* week = lv_weeks
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
** Implement suitable error handling here
* ENDIF.
*
* CLEAR: lv_weeks_c.
* lv_weeks_c = lv_weeks.
* CONDENSE lv_weeks_c.
*
* CLEAR: lv_colname.
* CONCATENATE lv_weeks_c+2(2) '年第' lv_weeks_c+4(2) '周' INTO lv_colname.
* CONDENSE lv_colname.
* fcat: lv_fname lv_colname.
* 预测总量/需求总量
CLEAR: lv_fname.
* lv_index = lv_index + 1.
CLEAR: lv_index_c.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCZL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
CLEAR: lv_colname.
IF p_r1 IS NOT INITIAL.
IF sy-langu = '1'.
lv_colname = '预测总量'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Forecast Gross'.
ENDIF.
ELSE.
IF sy-langu = '1'.
lv_colname = '需求总量'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Requirement Qty'.
ENDIF.
ENDIF.
CONDENSE lv_colname.
fcat: lv_fname lv_colname '' '' 20.
* 当期销售订单数量/当期已交货数量
CLEAR: lv_fname.
* lv_index = lv_index + 1.
CLEAR: lv_index_c.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'DQSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
CLEAR: lv_colname.
IF p_r1 IS NOT INITIAL.
IF sy-langu = '1'.
lv_colname = '当期销售订单数量'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Current Sales Order Qty'.
ENDIF.
ELSE.
IF sy-langu = '1'.
lv_colname = '当期已交货数量'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Current Delivered Qty'.
ENDIF.
ENDIF.
CONDENSE lv_colname.
fcat: lv_fname lv_colname '' '' 20.
* 预测导入数量/需求导入数量
CLEAR: lv_fname.
* lv_index = lv_index + 1.
CLEAR: lv_index_c.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
CLEAR: lv_colname.
IF p_r1 IS NOT INITIAL.
IF sy-langu = '1'.
lv_colname = '预测导入数量'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Forecast Import Qty'.
ENDIF.
ELSE.
IF sy-langu = '1'.
lv_colname = '需求导入数量'.
ELSEIF sy-langu = 'E'.
lv_colname = 'Requirement Import Qty'.
ENDIF.
ENDIF.
CONDENSE lv_colname.
fcat: lv_fname lv_colname 'X' '' 20.
ENDLOOP.
* 批导上传的数据结构
CLEAR: ls_excel,
lv_index,
lv_weeks,
lv_date,
lv_index_c,
lv_weeks_c,
lv_colname,
lv_fname.
fcat_u: 'WERKS' '工厂' 4,
'MATNR' '物料编码' 40,
'MAKTX' '物料描述' 40,
'KUNNR' '客户编码' 10.
LOOP AT lt_excel INTO ls_excel WHERE row = '0001'
AND col > '0004'.
CLEAR: lv_fname.
lv_index = lv_index + 1.
CLEAR: lv_index_c.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCZL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
CLEAR: lv_date, lv_weeks.
* CONVERT_DATE_TO_INTERNAL
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = ls_excel-value
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = lv_date
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* lv_date = ls_excel-value.
CALL FUNCTION 'DATE_GET_WEEK'
EXPORTING
date = lv_date
IMPORTING
week = lv_weeks
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CLEAR: lv_weeks_c.
lv_weeks_c = lv_weeks.
CONDENSE lv_weeks_c.
CLEAR: lv_colname.
CONCATENATE lv_weeks_c+0(4) '.' lv_weeks_c+4(2) INTO lv_colname.
CONDENSE lv_colname.
fcat_u: lv_fname lv_colname 20.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_create_dynamic_table
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_create_dynamic_table .
* 生成ALV相关的动态内表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_structure
IMPORTING
ep_table = dy_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'create dynamic table error' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
* 生成批导数据相关的动态内表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_structure_u
IMPORTING
ep_table = dy_table_u
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'create dynamic table error' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form html_top_of_page ALV标题
*&---------------------------------------------------------------------*
FORM html_top_of_page USING document TYPE REF TO cl_dd_document.
DATA: text TYPE sdydo_text_element,
lv_name TYPE t001w-name1.
CLEAR: text,
lv_name.
IF p_r1 IS NOT INITIAL.
IF sy-langu = '1'.
text = '销售预测导入'.
ELSEIF sy-langu = 'E'.
text = 'Batch Import of Sales Forecast'.
ENDIF.
ELSE.
IF sy-langu = '1'.
text = '独立需求导入'.
ELSEIF sy-langu = 'E'.
text = 'Batch Import of Plan Independent Requirement'.
ENDIF.
ENDIF.
CALL METHOD document->add_text
EXPORTING
text = text
sap_fontsize = cl_dd_area=>medium
sap_emphasis = cl_dd_area=>strong.
CALL METHOD document->add_gap
EXPORTING
width = 2.
* CALL METHOD document->new_line.
*
* CLEAR: text.
* SELECT SINGLE name1 INTO lv_name FROM t001w
* WHERE werks = p_werks.
* CONCATENATE '工厂:' p_werks lv_name INTO text SEPARATED BY space.
* CALL METHOD document->add_text
* EXPORTING
* text = text
* sap_fontsize = cl_dd_area=>medium
* sap_emphasis = cl_dd_area=>strong.
*
* CALL METHOD document->add_gap
* EXPORTING
* width = 2.
ENDFORM.
FORM frm_set_pfstatus USING pu_extab TYPE slis_t_extab.
DATA lt_fcode TYPE SORTED TABLE OF sy-ucomm
WITH NON-UNIQUE KEY table_line.
IF p_r1 = 'X'.
lt_fcode = VALUE #( ( |&BUT2| ) ) .
ELSE.
lt_fcode = VALUE #( ( |&BUT1| ) ) .
ENDIF.
SET PF-STATUS 'STATUS_ALV' EXCLUDING lt_fcode . "lt_fcode内表可自定义
ENDFORM.
FORM frm_user_command USING p_ucomm TYPE sy-ucomm
p_rs_selfield TYPE slis_selfield.
TYPES: BEGIN OF lty_vbap,
vbeln TYPE vbak-vbeln,
kunnr TYPE vbak-kunnr,
posnr TYPE vbap-posnr,
matnr TYPE vbap-matnr,
werks TYPE vbap-werks,
pstyv TYPE vbap-pstyv,
END OF lty_vbap.
DATA: lv_sel TYPE c,
lv_flag TYPE c,
lo_grid TYPE REF TO cl_gui_alv_grid,
lv_valid TYPE c,
ls_stbl TYPE lvc_s_stbl,
lt_vbap TYPE TABLE OF lty_vbap,
ls_vbap TYPE lty_vbap,
lv_kunnr TYPE vbak-kunnr,
lv_matnr TYPE vbap-matnr,
lv_werks TYPE vbap-werks,
order_header_in TYPE bapisdh1,
order_header_inx TYPE bapisdh1x,
order_item_in TYPE TABLE OF bapisditm,
ls_order_item_in TYPE bapisditm,
order_item_inx TYPE TABLE OF bapisditmx,
ls_order_item_inx TYPE bapisditmx,
schedule_lines TYPE TABLE OF bapischdl,
ls_schedule_lines TYPE bapischdl,
schedule_linesx TYPE TABLE OF bapischdlx,
ls_schedule_linesx TYPE bapischdlx,
lt_return TYPE TABLE OF bapiret2,
ls_return TYPE bapiret2,
lv_error TYPE c,
lv_posnr TYPE vbap-posnr,
lv_etenr TYPE vbep-etenr,
lv_msg TYPE string,
lv_index TYPE i,
lv_index_c TYPE string,
lv_fname TYPE string,
lv_date_c TYPE string,
lv_date_n TYPE sy-datum,
lv_qty TYPE vbep-wmeng,
lv_week TYPE scal-week,
lt_pbim_u TYPE TABLE OF pbim,
ls_pbim_u TYPE pbim,
lv_versb TYPE pbim-versb.
DATA: lv_requirementstype TYPE bapisitemr-requ_type,
lv_material TYPE bapisitemr-material.
CLEAR: lv_requirementstype,
lv_material.
DATA: ls_order_header_in TYPE bapisdhd1,
ls_order_header_inx TYPE bapisdhd1x,
lt_order_items_in TYPE TABLE OF bapisditm,
ls_order_items_in TYPE bapisditm,
lt_order_items_inx TYPE TABLE OF bapisditmx,
ls_order_items_inx TYPE bapisditmx,
lt_order_partners TYPE TABLE OF bapiparnr,
ls_order_partners TYPE bapiparnr,
lt_order_schedules_in TYPE TABLE OF bapischdl,
ls_order_schedules_in TYPE bapischdl,
lt_order_schedules_inx TYPE TABLE OF bapischdlx,
ls_order_schedules_inx TYPE bapischdlx.
DATA: lt_tp003_a TYPE TABLE OF zppt_003a,
ls_tp003_a TYPE zppt_003a,
lt_tp003_ac TYPE TABLE OF zppt_003a,
ls_tp003_ac TYPE zppt_003a,
lt_tp003_as TYPE TABLE OF zppt_003a,
ls_tp003_as TYPE zppt_003a,
lt_tp003_b TYPE TABLE OF zppt_003b,
ls_tp003_b TYPE zppt_003b,
lt_tp003_bs TYPE TABLE OF zppt_003b,
ls_tp003_bs TYPE zppt_003b.
DATA: lv_lsh TYPE c LENGTH 4,
lv_zscode TYPE zscode,
lv_zdrbbn TYPE zdrbbn,
lv_zdrbbn_c TYPE string.
CLEAR: lv_zdrbbn_c,
lt_pbim_u,
ls_pbim_u,
lv_versb.
DATA: lt_pbim TYPE TABLE OF pbim,
ls_pbim TYPE pbim,
ls_requirements_item TYPE bapisitemr,
lt_requirements_schedule_in TYPE TABLE OF bapisshdin,
ls_requirements_schedule_in TYPE bapisshdin,
lt_return_n TYPE TABLE OF bapireturn1,
ls_return_n TYPE bapireturn1,
lv_reqmtsplannumber TYPE bapisitemr-req_number.
CLEAR: lt_pbim,
ls_pbim,
ls_requirements_item,
lt_requirements_schedule_in,
ls_requirements_schedule_in,
lt_return_n,
ls_return_n,
lv_reqmtsplannumber.
CLEAR: lt_tp003_a,
ls_tp003_a,
lt_tp003_ac,
ls_tp003_ac,
lt_tp003_as,
ls_tp003_as,
lt_tp003_b,
ls_tp003_b,
lt_tp003_bs,
ls_tp003_bs,
lv_lsh,
lv_zscode,
lv_zdrbbn,
lv_week.
CLEAR: ls_order_header_in,
ls_order_header_inx,
lt_order_items_in,
ls_order_items_in,
lt_order_items_inx,
ls_order_items_inx,
lt_order_partners,
ls_order_partners,
lt_order_schedules_in,
ls_order_schedules_in,
lt_order_schedules_inx,
ls_order_schedules_inx.
CLEAR: lv_sel,
lv_flag,
lv_valid,
ls_stbl,
lt_vbap,
ls_vbap,
lv_kunnr,
lv_matnr,
lv_werks,
order_header_in,
order_header_inx,
order_item_in,
ls_order_item_in,
order_item_inx,
ls_order_item_inx,
schedule_lines,
ls_schedule_lines,
schedule_linesx,
ls_schedule_linesx,
lt_return,
ls_return,
lv_error,
lv_posnr,
lv_etenr,
lv_msg,
lv_index,
lv_index_c,
lv_fname,
lv_date_c,
lv_date_n,
lv_qty.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lo_grid.
CALL METHOD lo_grid->check_changed_data
IMPORTING
e_valid = lv_valid.
CASE p_ucomm.
WHEN '&BUT1'. "保存销售预测
IF gv_error IS NOT INITIAL.
IF sy-langu = '1'.
MESSAGE '检查未通过,不允许保存!' TYPE 'E'.
ELSEIF sy-langu = 'E'.
MESSAGE 'You can not submit the result Till the issues are fixxed!' TYPE 'E'.
ENDIF.
ENDIF.
LOOP AT <dyntab> ASSIGNING <fs_wa>.
ASSIGN COMPONENT 'SEL' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_sel = <fs_val>.
IF lv_sel = 'X'.
lv_flag = 'X'.
EXIT.
ENDIF.
ENDLOOP.
IF lv_flag IS INITIAL.
IF sy-langu = '1'.
MESSAGE '请至少选择一条数据!' TYPE 'E'.
ELSEIF sy-langu = 'E'.
MESSAGE 'Please choose at least one records!' TYPE 'E'.
ENDIF.
ENDIF.
* 销售订单创建VA01/修改VA02
IF gt_data IS NOT INITIAL.
SELECT vbak~vbeln
vbak~kunnr
vbap~posnr
vbap~matnr
vbap~werks
vbap~pstyv INTO TABLE lt_vbap
FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
FOR ALL ENTRIES IN gt_data
WHERE vbap~matnr = gt_data-matnr
AND vbap~werks = gt_data-werks
AND vbak~kunnr = gt_data-kunnr
AND vbak~auart = 'ZPLN'.
ENDIF.
LOOP AT <dyntab> ASSIGNING <fs_wa>.
CLEAR: lv_sel,
lv_kunnr,
lv_matnr,
lv_werks.
ASSIGN COMPONENT 'SEL' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_sel = <fs_val>.
ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_kunnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_kunnr
IMPORTING
output = lv_kunnr.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_matnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lv_matnr
IMPORTING
output = lv_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_werks = <fs_val>.
IF lv_sel IS NOT INITIAL.
READ TABLE lt_vbap INTO ls_vbap WITH KEY kunnr = lv_kunnr
matnr = lv_matnr
werks = lv_werks.
******************************************************************************
* 修改销售订单
IF sy-subrc = 0."修改销售订单
* 首先删除销售订单的行项目
CLEAR: order_header_inx,
order_item_in,
ls_order_item_in,
order_header_in,
order_item_inx,
ls_order_item_inx,
schedule_lines,
ls_schedule_lines,
schedule_linesx,
ls_schedule_linesx,
lt_return,
ls_return,
lv_error,
lv_posnr,
lv_etenr.
order_header_inx-updateflag = 'U'.
LOOP AT lt_vbap INTO ls_vbap WHERE kunnr = lv_kunnr
AND matnr = lv_matnr
AND werks = lv_werks.
ls_order_item_in-itm_number = ls_vbap-posnr.
APPEND ls_order_item_in TO order_item_in.
CLEAR: ls_order_item_in.
ls_order_item_inx-itm_number = ls_vbap-posnr.
ls_order_item_inx-updateflag = 'D'.
APPEND ls_order_item_inx TO order_item_inx.
CLEAR: ls_order_item_inx.
ENDLOOP.
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
EXPORTING
salesdocument = ls_vbap-vbeln
* ORDER_HEADER_IN =
order_header_inx = order_header_inx
* SIMULATION =
* BEHAVE_WHEN_ERROR = ' '
* INT_NUMBER_ASSIGNMENT = ' '
* LOGIC_SWITCH =
* NO_STATUS_BUF_INIT = ' '
TABLES
return = lt_return
order_item_in = order_item_in
order_item_inx = order_item_inx
* PARTNERS =
* PARTNERCHANGES =
* PARTNERADDRESSES =
* ORDER_CFGS_REF =
* ORDER_CFGS_INST =
* ORDER_CFGS_PART_OF =
* ORDER_CFGS_VALUE =
* ORDER_CFGS_BLOB =
* ORDER_CFGS_VK =
* ORDER_CFGS_REFINST =
* SCHEDULE_LINES =
* SCHEDULE_LINESX =
* ORDER_TEXT =
* ORDER_KEYS =
* CONDITIONS_IN =
* CONDITIONS_INX =
* EXTENSIONIN =
* EXTENSIONEX =
* NFMETALLITMS =
.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
lv_error = 'X'.
EXIT.
ENDLOOP.
CLEAR: ls_return.
IF lv_error IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
CONCATENATE lv_msg ls_return-message INTO lv_msg.
ENDLOOP.
<fs_val> = lv_msg.
lv_error = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return.
LOOP AT lt_return INTO ls_return WHERE type = 'S' AND id = 'V1' AND number = '311'.
CONCATENATE lv_msg ls_return-message INTO lv_msg.
ENDLOOP.
<fs_val> = lv_msg.
ENDIF.
* 再按照模板上的数据更新销售订单
IF lv_error IS INITIAL.
CLEAR: order_header_inx,
order_item_in,
ls_order_item_in,
order_header_in,
order_item_inx,
ls_order_item_inx,
schedule_lines,
ls_schedule_lines,
schedule_linesx,
ls_schedule_linesx,
lt_return,
ls_return,
lv_error,
lv_posnr,
lv_etenr.
order_header_inx-updateflag = 'U'.
CLEAR: lv_index.
CLEAR: lv_posnr.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
* 明细行
CLEAR: lv_index_c, lv_fname, lv_qty.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
lv_qty = <fs_val>.
IF lv_qty IS INITIAL. "如果数量为0,则不需要添加到销售订单的行项目上去
CONTINUE.
ELSE.
lv_posnr = lv_posnr + 10.
ENDIF.
ls_order_item_in-itm_number = lv_posnr.
ls_order_item_in-material = lv_matnr.
ls_order_item_in-plant = lv_werks.
APPEND ls_order_item_in TO order_item_in.
CLEAR: ls_order_item_in.
ls_order_item_inx-itm_number = lv_posnr.
ls_order_item_inx-material = 'X'.
ls_order_item_inx-plant = 'X'.
ls_order_item_inx-updateflag = 'I'.
APPEND ls_order_item_inx TO order_item_inx.
CLEAR: ls_order_item_inx.
* 计划行
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_date_c, lv_date_n.
lv_date_c = <fs_val>.
* 转成内部日期
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_date_c
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = lv_date_n
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CLEAR: lv_etenr.
lv_etenr = lv_etenr + 1.
ls_schedule_lines-itm_number = lv_posnr.
ls_schedule_lines-sched_line = lv_etenr.
ls_schedule_lines-req_date = lv_date_n.
ls_schedule_lines-req_qty = lv_qty.
APPEND ls_schedule_lines TO schedule_lines.
CLEAR: ls_schedule_lines.
ls_schedule_linesx-itm_number = lv_posnr.
ls_schedule_linesx-sched_line = lv_etenr.
ls_schedule_linesx-req_date = 'X'.
ls_schedule_linesx-req_qty = 'X'.
APPEND ls_schedule_linesx TO schedule_linesx.
CLEAR: ls_schedule_linesx.
ENDLOOP.
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
EXPORTING
salesdocument = ls_vbap-vbeln
* order_header_in = order_header_in
order_header_inx = order_header_inx
* SIMULATION =
* BEHAVE_WHEN_ERROR = ' '
* INT_NUMBER_ASSIGNMENT = ' '
* LOGIC_SWITCH =
* NO_STATUS_BUF_INIT = ' '
TABLES
return = lt_return
order_item_in = order_item_in
order_item_inx = order_item_inx
* PARTNERS =
* PARTNERCHANGES =
* PARTNERADDRESSES =
* ORDER_CFGS_REF =
* ORDER_CFGS_INST =
* ORDER_CFGS_PART_OF =
* ORDER_CFGS_VALUE =
* ORDER_CFGS_BLOB =
* ORDER_CFGS_VK =
* ORDER_CFGS_REFINST =
schedule_lines = schedule_lines
schedule_linesx = schedule_linesx
* ORDER_TEXT =
* ORDER_KEYS =
* CONDITIONS_IN =
* CONDITIONS_INX =
* EXTENSIONIN =
* EXTENSIONEX =
* NFMETALLITMS =
.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
lv_error = 'X'.
EXIT.
ENDLOOP.
CLEAR: ls_return.
IF lv_error IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
CONCATENATE lv_msg ls_return-message INTO lv_msg.
ENDLOOP.
<fs_val> = lv_msg.
lv_error = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return.
LOOP AT lt_return INTO ls_return WHERE type = 'S' AND id = 'V1' AND number = '311'.
CONCATENATE lv_msg ls_return-message INTO lv_msg.
ENDLOOP.
ENDIF.
ENDIF.
******************************************************************************************
* 创建销售订单
ELSE."创建销售订单
CLEAR: ls_order_header_in,
ls_order_header_inx,
lt_order_items_in,
ls_order_items_in,
lt_order_items_inx,
ls_order_items_inx,
lt_order_partners,
ls_order_partners,
lt_order_schedules_in,
ls_order_schedules_in,
lt_order_schedules_inx,
ls_order_schedules_inx,
lt_return,
ls_return,
lv_error.
* header
ls_order_header_in-doc_type = 'ZPLN'."销售订单类型
ls_order_header_in-sales_org = lv_werks. "销售组织
ls_order_header_in-distr_chan = '10'. "分销渠道
ls_order_header_in-division = '00'. "产品组
* Partners
ls_order_partners-partn_role = 'AG'. "售达方
ls_order_partners-partn_numb = lv_kunnr.
APPEND ls_order_partners TO lt_order_partners.
CLEAR: ls_order_partners.
ls_order_partners-partn_role = 'RG'. "付款方
ls_order_partners-partn_numb = lv_kunnr.
APPEND ls_order_partners TO lt_order_partners.
CLEAR: ls_order_partners.
ls_order_partners-partn_role = 'RE'. "收票方
ls_order_partners-partn_numb = lv_kunnr.
APPEND ls_order_partners TO lt_order_partners.
CLEAR: ls_order_partners.
ls_order_partners-partn_role = 'WE'. "送达方
ls_order_partners-partn_numb = lv_kunnr.
APPEND ls_order_partners TO lt_order_partners.
CLEAR: ls_order_partners.
* items
CLEAR: lv_posnr.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname, lv_qty.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
lv_qty = <fs_val>.
IF lv_qty IS INITIAL. "如果数量为0,则不需要添加到销售订单的行项目上去
CONTINUE.
ELSE.
lv_posnr = lv_posnr + 10.
ENDIF.
ls_order_items_in-itm_number = lv_posnr. "行项目号
ls_order_items_in-material = lv_matnr. "物料
ls_order_items_in-plant = lv_werks. "工厂
APPEND ls_order_items_in TO lt_order_items_in.
CLEAR: ls_order_items_in.
ls_order_items_inx-itm_number = lv_posnr. "行项目号
ls_order_items_inx-updateflag = 'I'. "
ls_order_items_inx-material = 'X'. "物料
ls_order_items_inx-plant = 'X'. "工厂
APPEND ls_order_items_inx TO lt_order_items_inx.
CLEAR: ls_order_items_inx.
CLEAR: lv_etenr.
lv_etenr = lv_etenr + 1.
ls_order_schedules_in-itm_number = lv_posnr. "行项目号
ls_order_schedules_in-sched_line = lv_etenr. "计划行号
ls_order_schedules_in-req_date = lv_date_n. "交货日期
ls_order_schedules_in-req_qty = lv_qty. "数量
APPEND ls_order_schedules_in TO lt_order_schedules_in.
CLEAR: ls_order_schedules_in.
ls_order_schedules_inx-itm_number = lv_posnr. "行项目号
ls_order_schedules_inx-sched_line = lv_etenr. "计划行号
ls_order_schedules_inx-req_date = 'X'. "交货日期
ls_order_schedules_inx-req_qty = 'X'. "数量
APPEND ls_order_schedules_inx TO lt_order_schedules_inx.
CLEAR: ls_order_schedules_inx.
ENDLOOP.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
* SALESDOCUMENTIN =
order_header_in = ls_order_header_in
order_header_inx = ls_order_header_inx
* SENDER =
* BINARY_RELATIONSHIPTYPE =
* INT_NUMBER_ASSIGNMENT =
* BEHAVE_WHEN_ERROR =
* LOGIC_SWITCH =
* TESTRUN =
* CONVERT = ' '
* IMPORTING
* SALESDOCUMENT =
TABLES
return = lt_return
order_items_in = lt_order_items_in
order_items_inx = lt_order_items_inx
order_partners = lt_order_partners
order_schedules_in = lt_order_schedules_in
order_schedules_inx = lt_order_schedules_inx
* ORDER_CONDITIONS_IN =
* ORDER_CONDITIONS_INX =
* ORDER_CFGS_REF =
* ORDER_CFGS_INST =
* ORDER_CFGS_PART_OF =
* ORDER_CFGS_VALUE =
* ORDER_CFGS_BLOB =
* ORDER_CFGS_VK =
* ORDER_CFGS_REFINST =
* ORDER_CCARD =
* ORDER_TEXT =
* ORDER_KEYS =
* EXTENSIONIN =
* PARTNERADDRESSES =
* EXTENSIONEX =
* NFMETALLITMS =
.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
lv_error = 'X'.
EXIT.
ENDLOOP.
CLEAR: ls_return.
IF lv_error IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
CONCATENATE lv_msg ls_return-message INTO lv_msg.
ENDLOOP.
<fs_val> = lv_msg.
lv_error = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return.
LOOP AT lt_return INTO ls_return WHERE type = 'S' AND id = 'V1' AND number = '311'.
CONCATENATE lv_msg ls_return-message INTO lv_msg.
ENDLOOP.
<fs_val> = lv_msg.
ENDIF.
ENDIF.
ENDIF.
IF lv_error IS INITIAL.
ASSIGN COMPONENT 'ZINTYPE' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '销售预测导入'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'Sales Forecast Import'.
ENDIF.
CLEAR: lt_tp003_a,
ls_tp003_a,
lt_tp003_b,
ls_tp003_b.
* 获取销售预测历史记录表
SELECT * INTO TABLE lt_tp003_a
FROM zppt_003a
WHERE zintype = '销售预测导入'
AND matnr = lv_matnr
AND werks = lv_werks
AND kunnr = lv_kunnr.
CLEAR: lv_zscode.
SELECT MAX( zscode ) INTO lv_zscode "获取当前日期最大的流水码(按照日期+工厂维护查找)
FROM zppt_003a
WHERE werks = lv_werks
AND erdat = sy-datum.
IF lt_tp003_a IS NOT INITIAL.
* LOOP AT lt_tp003_a INTO ls_tp003_a WHERE erdat = sy-datum. "获取当前日期的销售预测历史记录
* ls_tp003_ac = ls_tp003_a.
* APPEND ls_tp003_ac TO lt_tp003_ac.
* CLEAR: ls_tp003_ac, ls_tp003_a.
* ENDLOOP.
SORT lt_tp003_a BY zintype matnr werks kunnr
zdrbbn DESCENDING.
* SORT lt_tp003_ac BY zintype matnr werks kunnr
* zscode DESCENDING.
* SELECT * INTO TABLE lt_tp003_b
* FROM zppt_003b
* FOR ALL ENTRIES IN lt_tp003_a
* WHERE zscode = lt_tp003_a-zscode
* AND zdrbbn = lt_tp003_a-zdrbbn.
ENDIF.
ASSIGN COMPONENT 'LAEDA' OF STRUCTURE <fs_wa> TO <fs_val>. "导入日期
<fs_val> = sy-datum.
ASSIGN COMPONENT 'AENAM' OF STRUCTURE <fs_wa> TO <fs_val>. "导入人员
<fs_val> = sy-uname.
CLEAR: lv_lsh.
ASSIGN COMPONENT 'ZSCODE' OF STRUCTURE <fs_wa> TO <fs_val>. "流水码
IF lv_zscode IS NOT INITIAL.
lv_zscode = lv_zscode + 1.
CONDENSE lv_zscode.
<fs_val> = lv_zscode.
ELSE.
lv_lsh = '0001'.
CONCATENATE sy-datum+2(6) lv_werks lv_lsh INTO lv_zscode.
CONDENSE lv_zscode.
<fs_val> = lv_zscode.
ENDIF.
ASSIGN COMPONENT 'ZDRBBN' OF STRUCTURE <fs_wa> TO <fs_val>. "导入版本号
READ TABLE lt_tp003_a INTO ls_tp003_a INDEX 1.
IF sy-subrc = 0.
CLEAR: lv_zdrbbn.
lv_zdrbbn = ls_tp003_a-zdrbbn.
lv_zdrbbn = lv_zdrbbn + 1.
* lv_zdrbbn_c = lv_zdrbbn.
* CONDENSE lv_zdrbbn_c.
<fs_val> = lv_zdrbbn.
ELSE.
CLEAR: lv_zdrbbn.
lv_zdrbbn = 1.
* CONDENSE lv_zdrbbn.
<fs_val> = lv_zdrbbn.
ENDIF.
ls_tp003_as-zintype = '销售预测导入'.
ls_tp003_as-matnr = lv_matnr.
ls_tp003_as-werks = lv_werks.
ls_tp003_as-kunnr = lv_kunnr.
ls_tp003_as-zscode = lv_zscode.
ls_tp003_as-zdrbbn = lv_zdrbbn.
ls_tp003_as-erdat = sy-datum.
ls_tp003_as-ernam = sy-uname.
ls_tp003_as-auart = 'ZPLN'.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_as-maktx = <fs_val>.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_as-meins = <fs_val>.
APPEND ls_tp003_as TO lt_tp003_as.
CLEAR: ls_tp003_as.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
ls_tp003_bs-zscode = lv_zscode.
ls_tp003_bs-zdrbbn = lv_zdrbbn.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_date_c, lv_date_n.
lv_date_c = <fs_val>.
* 转成内部日期
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_date_c
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = lv_date_n
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ls_tp003_bs-zycrq = lv_date_n.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCZL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zyczl = <fs_val>.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'DQSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zdqsl = <fs_val>.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zycsl = <fs_val>.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-meins = <fs_val>.
ls_tp003_bs-zitem = lv_index.
CONDENSE ls_tp003_bs-zitem.
APPEND ls_tp003_bs TO lt_tp003_bs.
CLEAR: ls_tp003_bs.
ENDLOOP.
ENDIF.
ENDLOOP.
IF lt_tp003_as IS NOT INITIAL.
MODIFY zppt_003a FROM TABLE lt_tp003_as.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
ELSE.
ROLLBACK WORK.
ENDIF.
ENDIF.
IF lt_tp003_bs IS NOT INITIAL.
MODIFY zppt_003b FROM TABLE lt_tp003_bs.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
ELSE.
ROLLBACK WORK.
ENDIF.
ENDIF.
p_rs_selfield-refresh = 'X'.
WHEN '&BUT2'. "保存需求预测
IF gv_error IS NOT INITIAL.
IF sy-langu = '1'.
MESSAGE '检查未通过,不允许保存!' TYPE 'E'.
ELSEIF sy-langu = 'E'.
MESSAGE 'You can not submit the result Till the issues are fixxed!' TYPE 'E'.
ENDIF.
ENDIF.
LOOP AT <dyntab> ASSIGNING <fs_wa>.
ASSIGN COMPONENT 'SEL' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_sel = <fs_val>.
IF lv_sel = 'X'.
lv_flag = 'X'.
EXIT.
ENDIF.
ENDLOOP.
IF lv_flag IS INITIAL.
IF sy-langu = '1'.
MESSAGE '请至少选择一条数据!' TYPE 'E'.
ELSEIF sy-langu = 'E'.
MESSAGE 'Please choose at least one records!' TYPE 'E'.
ENDIF.
ENDIF.
* 创建MD61/修改MD62独立的计划需求
IF gt_data IS NOT INITIAL.
SELECT * INTO TABLE lt_pbim
FROM pbim
FOR ALL ENTRIES IN gt_data
WHERE matnr = gt_data-matnr
AND werks = gt_data-werks.
ENDIF.
LOOP AT <dyntab> ASSIGNING <fs_wa>.
CLEAR: lv_sel,
lv_kunnr,
lv_matnr,
lv_werks.
ASSIGN COMPONENT 'SEL' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_sel = <fs_val>.
ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_kunnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_kunnr
IMPORTING
output = lv_kunnr.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_matnr = <fs_val>.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lv_matnr
IMPORTING
output = lv_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_werks = <fs_val>.
IF lv_sel IS NOT INITIAL.
READ TABLE lt_pbim INTO ls_pbim WITH KEY zkunnr = lv_kunnr
matnr = lv_matnr
werks = lv_werks.
******************************************************************************
* 修改计划独立需求
IF sy-subrc = 0."修改计划独立需求
CLEAR: lv_error,
lt_requirements_schedule_in,
ls_requirements_schedule_in,
lt_return_n,
ls_return_n,
lv_material.
"版本
ASSIGN COMPONENT 'VERSB' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_versb = <fs_val>.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname, lv_qty.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_qty.
lv_qty = <fs_val>.
IF lv_qty IS INITIAL. "如果数量为0,则不需要添加到销售订单的行项目上去
CONTINUE.
ENDIF.
"日期
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_week, lv_date_n.
lv_week = <fs_val>.
* 转成内部日期
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = lv_week
IMPORTING
date = lv_date_n
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ls_requirements_schedule_in-req_date = lv_date_n.
"计划周期
ls_requirements_schedule_in-date_type = '2'.
"数量
ls_requirements_schedule_in-req_qty = lv_qty.
APPEND ls_requirements_schedule_in TO lt_requirements_schedule_in.
CLEAR: ls_requirements_schedule_in.
ENDLOOP.
lv_material = lv_matnr.
CLEAR: lv_requirementstype.
SELECT SINGLE bedae INTO lv_requirementstype
FROM pbim
WHERE matnr = lv_matnr
AND werks = lv_werks
AND versb = lv_versb.
CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
EXPORTING
material = lv_material
plant = lv_werks
requirementstype = lv_requirementstype
version = lv_versb
reqmtsplannumber = ''
vers_activ = 'X'
* REQUIREMENT_PARAM =
* MRP_AREA =
* DO_COMMIT = 'X'
* UPDATE_MODE = 'X'
delete_old = 'X'
* NO_WITHDR = ' '
* MATERIAL_EVG =
* REQ_PLAN_ID_EXT =
* REQ_SEGMENT =
* MATERIAL_LONG =
* REQ_SEG_LONG =
* IMPORTING
* REQUIREMENT_ITEM_OUT =
TABLES
requirements_schedule_in = lt_requirements_schedule_in
* REQUIREMENTS_CHAR_IN =
return = lt_return_n.
******************************************************************************************
* 创建计划独立需求
ELSE."创建计划独立需求
CLEAR: lv_error,
ls_requirements_item,
lt_requirements_schedule_in,
ls_requirements_schedule_in,
lt_return_n,
ls_return_n,
lv_reqmtsplannumber.
"工厂
ls_requirements_item-plant = lv_werks.
"MRP AREA
* ls_requirements_item-mrp_area = lv_berid.
"
* ls_requirements_item-req_number = gs_data-pbdnr.
"激活
ls_requirements_item-vers_activ = 'X'.
"版本
ASSIGN COMPONENT 'VERSB' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_requirements_item-version = <fs_val>.
"Material
ls_requirements_item-material = lv_matnr.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
CLEAR: lv_index_c, lv_fname, lv_qty.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_qty.
lv_qty = <fs_val>.
IF lv_qty IS INITIAL. "如果数量为0,则不需要添加到销售订单的行项目上去
CONTINUE.
ENDIF.
"日期
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_week, lv_date_n.
lv_week = <fs_val>.
* 转成内部日期
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = lv_week
IMPORTING
date = lv_date_n
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ls_requirements_schedule_in-req_date = lv_date_n.
"计划周期
ls_requirements_schedule_in-date_type = '2'.
"数量
ls_requirements_schedule_in-req_qty = lv_qty.
APPEND ls_requirements_schedule_in TO lt_requirements_schedule_in.
CLEAR: ls_requirements_schedule_in.
ENDLOOP.
CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE'
EXPORTING
requirements_item = ls_requirements_item
* REQUIREMENT_PARAM =
* DO_COMMIT = 'X'
* UPDATE_MODE = 'X'
* REFER_TYPE = ' '
* PROFILID = ' '
IMPORTING
* MATERIAL =
* PLANT =
* REQUIREMENTSTYPE =
* VERSION =
reqmtsplannumber = lv_reqmtsplannumber
* MRP_AREA =
* MATERIAL_EVG =
* REQ_PLAN_ID_EXT =
* REQ_SEGMENT =
* MATERIAL_LONG =
* REQ_SEG_LONG =
TABLES
requirements_schedule_in = lt_requirements_schedule_in
* REQUIREMENTS_CHAR_IN =
return = lt_return_n.
ENDIF.
ENDIF.
LOOP AT lt_return_n INTO ls_return_n WHERE type = 'E' OR type = 'A'.
lv_error = 'X'.
EXIT.
ENDLOOP.
CLEAR: ls_return.
IF lv_error IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
return = ls_return.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@0A@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'E'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return_n.
LOOP AT lt_return_n INTO ls_return_n WHERE type = 'E' OR type = 'A'.
CONCATENATE lv_msg ls_return_n-message INTO lv_msg.
ENDLOOP.
<fs_val> = lv_msg.
lv_error = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
CLEAR: lt_pbim_u,
ls_pbim_u,
lv_versb.
ASSIGN COMPONENT 'VERSB' OF STRUCTURE <fs_wa> TO <fs_val>.
lv_versb = <fs_val>.
SELECT * INTO TABLE lt_pbim_u
FROM pbim
WHERE matnr = lv_matnr
AND werks = lv_werks
AND versb = lv_versb.
IF sy-subrc = 0.
UPDATE pbim SET zkunnr = lv_kunnr
WHERE matnr = lv_matnr
AND werks = lv_werks
AND versb = lv_versb.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
ELSE.
ROLLBACK WORK.
ENDIF.
ENDIF.
ASSIGN COMPONENT 'STATUS' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = '@08@'.
ASSIGN COMPONENT 'MSGTY' OF STRUCTURE <fs_wa> TO <fs_val>.
<fs_val> = 'S'.
ASSIGN COMPONENT 'MSGTXT' OF STRUCTURE <fs_wa> TO <fs_val>.
CLEAR: lv_msg, ls_return_n.
IF sy-langu = '1'.
lv_msg = '导入成功'.
ELSE.
lv_msg = 'Import Successfully'.
ENDIF.
<fs_val> = lv_msg.
ENDIF.
IF lv_error IS INITIAL.
ASSIGN COMPONENT 'ZINTYPE' OF STRUCTURE <fs_wa> TO <fs_val>.
IF sy-langu = '1'.
<fs_val> = '需求预测导入'.
ELSEIF sy-langu = 'E'.
<fs_val> = 'Requirement Forecast Import'.
ENDIF.
CLEAR: lt_tp003_a,
ls_tp003_a,
lt_tp003_b,
ls_tp003_b.
* 获取独立需求预测历史记录表
SELECT * INTO TABLE lt_tp003_a
FROM zppt_003a
WHERE zintype = '需求预测导入'
AND matnr = lv_matnr
AND werks = lv_werks
AND kunnr = lv_kunnr.
CLEAR: lv_zscode.
SELECT MAX( zscode ) INTO lv_zscode "获取当前日期最大的流水码(按照日期+工厂维护查找)
FROM zppt_003a
WHERE werks = lv_werks
AND erdat = sy-datum.
IF lt_tp003_a IS NOT INITIAL.
* LOOP AT lt_tp003_a INTO ls_tp003_a WHERE erdat = sy-datum. "获取当前日期的销售预测历史记录
* ls_tp003_ac = ls_tp003_a.
* APPEND ls_tp003_ac TO lt_tp003_ac.
* CLEAR: ls_tp003_ac, ls_tp003_a.
* ENDLOOP.
SORT lt_tp003_a BY zintype matnr werks kunnr
zdrbbn DESCENDING.
* SORT lt_tp003_ac BY zintype matnr werks kunnr
* zscode DESCENDING.
* SELECT * INTO TABLE lt_tp003_b
* FROM zppt_003b
* FOR ALL ENTRIES IN lt_tp003_a
* WHERE zscode = lt_tp003_a-zscode
* AND zdrbbn = lt_tp003_a-zdrbbn.
ENDIF.
ASSIGN COMPONENT 'LAEDA' OF STRUCTURE <fs_wa> TO <fs_val>. "导入日期
<fs_val> = sy-datum.
ASSIGN COMPONENT 'AENAM' OF STRUCTURE <fs_wa> TO <fs_val>. "导入人员
<fs_val> = sy-uname.
CLEAR: lv_lsh.
ASSIGN COMPONENT 'ZSCODE' OF STRUCTURE <fs_wa> TO <fs_val>. "流水码
IF lv_zscode IS NOT INITIAL.
lv_zscode = lv_zscode + 1.
CONDENSE lv_zscode.
<fs_val> = lv_zscode.
ELSE.
lv_lsh = '0001'.
CONCATENATE sy-datum+2(6) lv_werks lv_lsh INTO lv_zscode.
CONDENSE lv_zscode.
<fs_val> = lv_zscode.
ENDIF.
ASSIGN COMPONENT 'ZDRBBN' OF STRUCTURE <fs_wa> TO <fs_val>. "导入版本号
READ TABLE lt_tp003_a INTO ls_tp003_a INDEX 1.
IF sy-subrc = 0.
CLEAR: lv_zdrbbn.
lv_zdrbbn = ls_tp003_a-zdrbbn.
lv_zdrbbn = lv_zdrbbn + 1.
* CONDENSE lv_zdrbbn.
<fs_val> = lv_zdrbbn.
ELSE.
CLEAR: lv_zdrbbn.
lv_zdrbbn = '1'.
* CONDENSE lv_zdrbbn.
<fs_val> = lv_zdrbbn.
ENDIF.
ls_tp003_as-zintype = '需求预测导入'.
ls_tp003_as-matnr = lv_matnr.
ls_tp003_as-werks = lv_werks.
ls_tp003_as-kunnr = lv_kunnr.
ls_tp003_as-zscode = lv_zscode.
ls_tp003_as-zdrbbn = lv_zdrbbn.
ls_tp003_as-erdat = sy-datum.
ls_tp003_as-ernam = sy-uname.
* ls_tp003_as-auart = 'ZPLN'.
ls_tp003_as-versb = lv_versb.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_as-maktx = <fs_val>.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_as-meins = <fs_val>.
APPEND ls_tp003_as TO lt_tp003_as.
CLEAR: ls_tp003_as.
CLEAR: lv_index.
LOOP AT gt_excel INTO gs_excel.
lv_index = lv_index + 1.
ls_tp003_bs-zscode = lv_zscode.
ls_tp003_bs-zdrbbn = lv_zdrbbn.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCRQ' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zycrq_n = <fs_val>.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCZL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zyczl = <fs_val>.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'DQSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zdqsl = <fs_val>.
CLEAR: lv_index_c, lv_fname.
lv_index_c = lv_index.
CONDENSE lv_index_c.
CONCATENATE 'YCSL' lv_index_c INTO lv_fname.
CONDENSE lv_fname.
ASSIGN COMPONENT lv_fname OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-zycsl = <fs_val>.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <fs_wa> TO <fs_val>.
ls_tp003_bs-meins = <fs_val>.
ls_tp003_bs-zitem = lv_index.
CONDENSE ls_tp003_bs-zitem.
APPEND ls_tp003_bs TO lt_tp003_bs.
CLEAR: ls_tp003_bs.
ENDLOOP.
ENDIF.
ENDLOOP.
IF lt_tp003_as IS NOT INITIAL.
MODIFY zppt_003a FROM TABLE lt_tp003_as.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
ELSE.
ROLLBACK WORK.
ENDIF.
ENDIF.
IF lt_tp003_bs IS NOT INITIAL.
MODIFY zppt_003b FROM TABLE lt_tp003_bs.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
ELSE.
ROLLBACK WORK.
ENDIF.
ENDIF.
p_rs_selfield-refresh = 'X'.
WHEN OTHERS.
ENDCASE.
ls_stbl-row = 'X'." 基于行的稳定刷新
ls_stbl-col = 'X'." 基于列稳定刷新
CALL METHOD lo_grid->refresh_table_display
EXPORTING
is_stable = ls_stbl
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDFORM.