*&---------------------------------------------------------------------*
*& Report ZTEST001
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zpprp024.
INCLUDE zpprp024top.
INCLUDE zpprp024a01.
INCLUDE zpprp024alv.
INITIALIZATION.
AT SELECTION-SCREEN OUTPUT.
AT SELECTION-SCREEN.
START-OF-SELECTION.
PERFORM frm_getdata.
gs_layout-cwidth_opt = 'X'.
gs_layout-cwidth_opt = 'X'.
gs_layout-stylefname = 'FIELD_STYLE'.
PERFORM fields_build CHANGING it_fieldcat[].
PERFORM alv_show. "展示alv"
END-OF-SELECTION.
*======================================================================*
* List Events
*======================================================================*
*** page header
TOP-OF-PAGE.
*** page header after first list
TOP-OF-PAGE DURING LINE-SELECTION.
*** page footer
END-OF-PAGE.
*** when double click
AT LINE-SELECTION.
*&---------------------------------------------------------------------*
*& Include ZTEST001TOP
*&---------------------------------------------------------------------*
TABLES: marc,plaf,makt,afko,afpo,eban.
DATA: BEGIN OF wtab,
check(1),
pwwrk LIKE plaf-pwwrk, "工厂"
plnum LIKE plaf-plnum, "计划单号"
kdauf LIKE plaf-kdauf, "销售订单"
kdpos LIKE plaf-kdpos, "行项目"
matnr LIKE plaf-matnr, "物料"
maktx LIKE makt-maktx, "物料描述"
gsmng LIKE plaf-gsmng, "数量(单位)"
psttr LIKE plaf-psttr, "计划订单创建日期"
pedtr LIKE plaf-pedtr,
paart LIKE plaf-paart,
aufnr LIKE afko-aufnr, "生产订单号"
menge_cg LIKE afko-gamng, "创建采购申请数量"
waers TYPE string, "货币码"
ekgrp TYPE string, "采购组"
vdatu LIKE vbak-vdatu, "交货日期"
charg LIKE ztmm014_01-band, "品牌"
banfn LIKE eban-banfn, "采购申请号"
field_style TYPE lvc_t_styl, "-------------------------------------为内表添加设置编辑状态所需的字段-------------------------------------
END OF wtab.
DATA: itab LIKE TABLE OF wtab WITH HEADER LINE.
DATA: it_fieldcat TYPE TABLE OF lvc_s_fcat WITH HEADER LINE,gs_layout TYPE lvc_s_layo.
SELECTION-SCREEN BEGIN OF BLOCK blk_1 WITH FRAME TITLE TEXT-001.
PARAMETER:
p_pwwrk LIKE marc-werks OBLIGATORY DEFAULT '1001' MEMORY ID kun.
SELECT-OPTIONS:
p_kdauf FOR plaf-kdauf,"销售订单号"
p_kdpos FOR plaf-kdpos,"销售订单行项目"
p_matnr FOR marc-matnr,"物料编号"
p_plnum FOR plaf-plnum,"计划订单号 "
p_dispo FOR marc-dispo."MRP控制者"
SELECTION-SCREEN END OF BLOCK blk_1.
*&---------------------------------------------------------------------*
*& Include ZTEST001A01
*&---------------------------------------------------------------------*
FORM frm_getdata.
SELECT
DISTINCT
pl~pwwrk"工厂"
pl~plnum"计划单号"
pl~kdauf"销售订单"
pl~kdpos"行项目 "
pl~matnr"物料"
mak~maktx"物料描述"
pl~gsmng"数量(单位)"
pl~psttr
pl~pedtr
pl~paart
INTO CORRESPONDING FIELDS OF TABLE itab
FROM plaf AS pl
INNER JOIN marc AS mar ON pl~pwwrk = mar~werks
INNER JOIN makt AS mak ON pl~matnr = mak~matnr
WHERE
pl~pwwrk = p_pwwrk"工厂"
AND pl~kdauf IN p_kdauf"销售订单号"
AND pl~kdpos IN p_kdpos"销售订单行项目"
AND pl~matnr IN p_matnr"物料编号"
AND pl~plnum IN p_plnum"计划订单号"
AND mar~dispo IN p_dispo"MRP控制者"
.
* 修改内表
LOOP AT itab.
* 评估价格
* 货币码
* 价格单位
* 采购组
* 交货日期
* 创建采购申请数量
* itab-preis = '1'.
itab-waers = 'CNY'.
* itab-peinh = '100'.
* itab-ekgrp = 'A04'.
* itab-vdatu = '20180120'.
itab-menge_cg = itab-gsmng.
* itab-charg = 'ABB'.
* itab-meins_cg = 'PC'.
MODIFY itab.
ENDLOOP.
IF sy-subrc IS NOT INITIAL.
MESSAGE '没有数据' TYPE 'I'.
STOP.
ENDIF.
ENDFORM.
FORM func001.
itab-check = 'X'.
MODIFY itab FROM itab
TRANSPORTING check WHERE check = ''.
CLEAR itab.
ENDFORM.
FORM func002.
itab-check = ''.
MODIFY itab FROM itab
TRANSPORTING check WHERE check = 'X'.
CLEAR itab.
ENDFORM.
FORM func003.
READ TABLE itab WITH KEY check = 'X'. "是否有选中的记录
IF sy-subrc <> 0.
MESSAGE '请选择' TYPE 'S'.
RETURN.
ENDIF.
DATA:
msg001(5000) TYPE c VALUE '',
production_order LIKE bapi_order_copy-order_number,
prod_order_type LIKE bapi_order_copy-order_type,
return LIKE bapiret2 OCCURS 0 WITH HEADER LINE
.
LOOP AT itab WHERE check NE ''.
IF itab[] IS NOT INITIAL.
CLEAR: production_order,prod_order_type,return.
CALL FUNCTION 'BAPI_PRODORD_CREATE_FROM_PLORD'
EXPORTING
planned_order = itab-plnum
IMPORTING
production_order = production_order
prod_order_type = prod_order_type
return = return
EXCEPTIONS
error_message = 1.
IF production_order = ''.
CONCATENATE msg001 cl_abap_char_utilities=>cr_lf'失败:计划单号:' itab-plnum ' 原因是:' return-message INTO msg001.
ELSE.
CONCATENATE msg001 cl_abap_char_utilities=>cr_lf '成功:计划单号:' itab-plnum ' >>>生产订单号:' production_order INTO msg001."成功的计划订单"
* itab-aufnr = production_order."生成生产订单后, 对应的计划订单会没有了"
* 修改内表
LOOP AT itab WHERE
pwwrk = itab-pwwrk "工厂"
AND plnum = itab-plnum "计划单号"
AND kdauf = itab-kdauf "销售订单"
AND kdpos = itab-kdpos "行项目"
AND matnr = itab-matnr "物料".
.
itab-aufnr = production_order.
MODIFY itab.
ENDLOOP.
ENDIF.
ENDIF.
ENDLOOP.
IF msg001 <> ''.
MESSAGE msg001 TYPE 'I'.
ENDIF.
ENDFORM.
FORM func004.
READ TABLE itab WITH KEY check = 'X'. "是否有选中的记录
IF sy-subrc <> 0.
MESSAGE '请选择' TYPE 'S'.
RETURN.
ENDIF.
LOOP AT itab WHERE check = 'X'.
* 评估价格
* 货币码
* 价格单位
* 采购组
* 交货日期
* 创建采购申请数量
IF
"itab-preis = '' OR"
itab-waers = '' OR
"itab-peinh = '' OR"
itab-ekgrp = '' OR
itab-vdatu = '' OR
itab-menge_cg <= 0.
MESSAGE '请填写必填数据' TYPE 'S'.
RETURN.
ENDIF.
ENDLOOP.
IF sy-subrc <> 0 .
MESSAGE '请选择创建数据' TYPE 'S'.
RETURN.
ENDIF.
DATA:name TYPE ad_namelas,
bnfpo TYPE bnfpo,
pr_no TYPE bapiebanc-preq_no,
pr_item LIKE TABLE OF bapiebanc WITH HEADER LINE,
pr_account LIKE TABLE OF bapiebkn WITH HEADER LINE,
pr_item_id LIKE TABLE OF bapiebantx WITH HEADER LINE,
pr_return LIKE TABLE OF bapireturn WITH HEADER LINE,
msg001(5000) TYPE c VALUE '',
l_pr_return LIKE pr_return
.
LOOP AT itab WHERE check NE ''.
IF itab[] IS NOT INITIAL.
CLEAR: bnfpo, pr_no, pr_item, pr_account, pr_item_id, pr_return.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = bnfpo
IMPORTING
output = bnfpo.
pr_item-doc_type = '11'. "凭证类型
pr_item-preq_item = bnfpo. "项目
pr_item-material = itab-matnr.
pr_item-quantity = itab-menge_cg. "数量
pr_item-deliv_date = itab-vdatu. "交货日期
pr_item-plant = itab-pwwrk. "工厂
pr_item-pur_group = itab-ekgrp. "采购组
pr_item-preq_name = name. "申请者
pr_item-currency = itab-waers. "货币码
pr_item-batch = itab-charg. "品牌"
APPEND pr_item.
CLEAR pr_item.
pr_item_id-preq_item = bnfpo. "项目
pr_item_id-text_id = 'B01'.
APPEND pr_item_id.
CLEAR pr_item_id.
CALL FUNCTION 'BAPI_REQUISITION_CREATE'
* EXPORTING
* skip_items_with_error =
* automatic_source = 'X'
IMPORTING
number = pr_no
TABLES
requisition_items = pr_item
requisition_account_assignment = pr_account
requisition_item_text = pr_item_id
* requisition_limits =
* requisition_contract_limits =
* requisition_services =
* requisition_srv_accass_values =
return = pr_return
* requisition_services_text =
* requisition_addrdelivery =
* extensionin =
.
LOOP AT pr_return WHERE type = 'E' OR type = 'A'.
ENDLOOP.
IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT pr_return INTO l_pr_return WHERE type = 'E' .
CONCATENATE msg001 cl_abap_char_utilities=>cr_lf '失败:计划单号:' itab-plnum ' 原因是:' l_pr_return-message INTO msg001.
ENDLOOP.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
CONCATENATE msg001 cl_abap_char_utilities=>cr_lf '成功:计划单号:' itab-plnum ' >>>采购订单号:' pr_no INTO msg001."成功的计划订单"
* 修改内表
LOOP AT itab WHERE
pwwrk = itab-pwwrk "工厂"
AND plnum = itab-plnum "计划单号"
AND kdauf = itab-kdauf "销售订单"
AND kdpos = itab-kdpos "行项目"
AND matnr = itab-matnr "物料".
.
itab-banfn = pr_no.
MODIFY itab.
ENDLOOP.
ENDIF.
FREE pr_item.
FREE pr_account.
FREE pr_item_id.
FREE pr_return.
ENDIF.
ENDLOOP.
IF msg001 <> ''.
MESSAGE msg001 TYPE 'I'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Include ZTEST001ALV
*&---------------------------------------------------------------------*
FORM set_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'ZTEST001STATUS' EXCLUDING rt_extab .
ENDFORM.
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
DATA: l_valid TYPE c.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
CALL METHOD lr_grid->check_changed_data.
rs_selfield-refresh = 'X'.
READ TABLE itab INDEX rs_selfield-tabindex.
CASE r_ucomm.
WHEN '&FUNC001'.
PERFORM func001.
WHEN '&FUNC002'.
PERFORM func002.
WHEN '&FUNC003'.
PERFORM func003.
WHEN '&FUNC004'.
PERFORM func004.
ENDCASE.
ENDFORM.
FORM fields_build CHANGING m_fieldcat TYPE lvc_t_fcat.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'CHECK' '选择' '' '' '' '' 'X' 'X' '' '' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'PWWRK' '工厂' '' '' '' '' '' '' '' '' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'PLNUM' '计划单号' '' '' '' '' '' '' '' 'X' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'KDAUF' '销售订单' '' '' '' '' '' '' '' 'X' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'KDPOS' '行项目' '' '' '' '' '' '' '' 'X' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'MATNR' '物料' '' '' '' '' '' '' '' 'X' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'MAKTX' '物料描述' '' '' '' '' '' '' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'GSMNG' '数量(单位)' '' '' '' '' '' '' '' '' .
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'PSTTR' '计划订单开始日期' '' '' '' '' '' '' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'PEDTR' '计划订单结束日期' '' '' '' '' '' '' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'AUFNR' '生产订单号' '' '' '' '' '' '' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'MENGE_CG' '创建采购申请数量' '' '' '' '' '' '' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'WAERS' '货币码' '' '' '' '' '' 'X' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'EKGRP' '采购组' '' '' '' '' '' 'X' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'VDATU' '交货日期' '' '20' '' '' '' 'X' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'CHARG' '品牌' '' '20' '' '' '' 'X' '' ''.
PERFORM frm_fieldcat_init TABLES m_fieldcat USING 'BANFN' '采购申请号' '' '' '' '' '' '' '' ''.
ENDFORM.
FORM frm_fieldcat_init TABLES t_fieldcat TYPE lvc_t_fcat
USING fieldname TYPE c " 字段名
scrtext_m TYPE c " 标题
col_pos TYPE i " 所在列
outputlen TYPE i " 输出长度
emphasize TYPE c " 高亮显示
key TYPE c " 主键
checkbox TYPE c
edit TYPE c
fix_column TYPE c " 固定列
no_zero TYPE c .
DATA: ls_fieldcat TYPE lvc_s_fcat.
ls_fieldcat-fieldname = fieldname.
ls_fieldcat-scrtext_m = scrtext_m.
ls_fieldcat-col_pos = col_pos.
ls_fieldcat-outputlen = outputlen.
ls_fieldcat-emphasize = emphasize.
ls_fieldcat-key = key.
ls_fieldcat-checkbox = checkbox.
ls_fieldcat-edit = edit.
ls_fieldcat-fix_column = fix_column.
ls_fieldcat-no_zero = no_zero .
IF ls_fieldcat-fieldname EQ 'VDATU'.
ls_fieldcat-ref_field = 'VDATU'.
ls_fieldcat-ref_table = 'VBAK'.
ls_fieldcat-coltext = '交货日期'.
ENDIF.
IF ls_fieldcat-fieldname EQ 'EKGRP'.
ls_fieldcat-ref_field = 'EKGRP'.
ls_fieldcat-ref_table = 'EBAN'.
ls_fieldcat-coltext = '采购组'.
ENDIF.
IF ls_fieldcat-fieldname EQ 'CHARG'.
ls_fieldcat-ref_field = 'BAND'.
ls_fieldcat-ref_table = 'ZTMM014_01'.
ls_fieldcat-f4availabl = 'X'.
ls_fieldcat-coltext = '品牌'.
ENDIF.
APPEND ls_fieldcat TO t_fieldcat.
CLEAR ls_fieldcat.
ENDFORM.
FORM alv_show.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-cprog " /SY-REPID " 程式名称
i_callback_pf_status_set = 'SET_STATUS'
i_callback_user_command = 'USER_COMMAND' " 自定义工具栏的事件
it_fieldcat_lvc = it_fieldcat[] " 必须要设置的参数 , 设定输出目录
is_layout_lvc = gs_layout " 对输出样式的设定
TABLES
t_outtab = itab "ALV 输出的内表
EXCEPTIONS
program_error = 1
OTHERS.
ENDFORM.