调用BAPI: BAPI_INSPECTIONPLAN_CREATE
背景:需要根据系统已有的检验计划去单独扩展不同工厂的检验计划;能通过Excel表格进行批量创建,并且可以在选择屏幕输入想要扩张的工厂。
难点:根据扩展的工厂生成新的检验计划组,并且保证组计数器的自增,另外需要注意单独扩展其他工厂的检验计划时,要先从系统中取到同检验计划的最后一个组计数器,而批量导入也需要根据扩展的工厂计算表格中的检验计划组的组计数器。
单独扩展:
新增及扩展按钮:
执行结果:
仅供参考:
*-------------------------------------------------------------处理组计数器-----------------------------------------------------------------------
data: lv_index type i,
lv_plnnr type plnnr,
lv_count type i.
data: lv_conut_row type i.
data: lalv_plnal type n length 2,
lalv_plna2 type n length 2.
if p_cb1 = 'X'.
perform frm_merge_plan tables s_wer. "汇总扩展工厂
else.
perform frm_merge_plan tables s_plan.
endif.
perform frm_expa_inspection. "处理组计数器问题 获取已存在的检验计划的最后一个计数器编号
move-corresponding gt_upload_1[] to gt_upload_temp[].
sort gt_upload_temp by plnnr ascending plnal descending .
delete adjacent duplicates from gt_upload_temp comparing plnnr.
sort gt_plan by plnnr.
loop at gt_plan.
lv_count = sy-tabix.
if p_cb1 <> 'X'."针对已存在数据的扩展.
if lv_count = 1.
lv_plnnr = gt_plan-plnnr.
else.
if gt_plan-plnnr <> lv_plnnr.
clear: lv_index,lalv_plnal.
lv_plnnr = gt_plan-plnnr.
else.
lv_index = 1.
endif.
endif.
else.
if lv_count = 1.
lv_index = 1.
lv_plnnr = gt_plan-plnnr.
read table gt_inspection with key plnnr = gt_plan-plnnr.
if sy-subrc = 0.
lalv_plnal = gt_inspection-plnal + 1.
endif.
else.
if gt_plan-plnnr <> lv_plnnr.
clear: lv_index,lalv_plnal.
lv_plnnr = gt_plan-plnnr.
endif.
endif.
endif.
loop at gt_upload_1 where plnnr = gt_plan-plnnr.
gt_alv-row = sy-tabix.
move-corresponding gt_upload_1 to gt_alv.
gt_alv-zplan = gt_alv-werks. "原检验计划工厂
gt_alv-werks = gt_plan-werks. "扩展工厂
read table gt_upload_c with key plnnr = gt_alv-plnnr plnal = gt_alv-plnal.
if sy-subrc = 0.
gt_alv-lifnr = gt_upload_c-lifnr.
gt_alv-matnr = gt_upload_c-matnr.
endif.
gt_alv-plnal_f = gt_alv-plnal."原计划组计数器
if gt_alv-plnal_f <> ''. "组计数器 补零 自增+1
unpack gt_alv-plnal_f to gt_alv-plnal_f.
endif.
if lalv_plnal is not initial.
gt_alv-plnal = lalv_plnal.
endif.
if gt_alv-plnal <> ''. "组计数器 补零 自增+1
unpack gt_alv-plnal to gt_alv-plnal.
endif.
if not gt_alv-verwmerkm is initial."主检验特性 补零
unpack gt_alv-verwmerkm to gt_alv-verwmerkm.
endif.
if gt_alv-arbpl is not initial.
select single arbpl into gt_alv-arbpl from crhd where objid = gt_alv-arbpl.
endif.
if gt_alv-plnme is not initial.
read table gt_t006a with key msehi = gt_alv-plnme spras = sy-langu.
if sy-subrc = 0.
gt_alv-plnme = gt_t006a-mseh3.
endif.
endif.
if gt_alv-probemgeh is not initial.
read table gt_t006a with key msehi = gt_alv-probemgeh spras = sy-langu.
if sy-subrc = 0.
gt_alv-probemgeh = gt_t006a-mseh3.
endif.
endif.
gt_alv-datub = '99991231'.
"获取字段“标准要求”
if p_cb1 = 'X'.
perform frm_get_text changing gt_alv.
endif.
"检查数据
perform frm_che_data changing gt_alv.
perform frm_set_color changing gt_alv.
at end of plnal.
if p_cb1 <> 'X'."针对已存在数据的扩展
if lv_index = 1.
lalv_plnal = lalv_plnal + 1.
endif.
else.
if lv_index = 1. "针对相同的组计划 组计数器自增的开关
lalv_plnal = lalv_plnal + 1.
endif.
endif.
endat.
append gt_alv.
clear gt_alv.
endloop.
if p_cb1 <> 'X'."针对已存在数据的扩展.
if gt_plan-plnnr = lv_plnnr and lalv_plnal is initial."相同计划组 第二次循环的时候执行
read table gt_upload_temp with key plnnr = gt_plan-plnnr.
if sy-subrc = 0.
lalv_plnal = gt_upload_temp-plnal + 1.
endif.
endif.
endif.
endloop.
*---》物料清单
loop at gt_upload_c.
move-corresponding gt_upload_c to gt_upload_2.
if gt_upload_2-matnr <> '' and gt_upload_2-matnr co '0123456789' .
unpack gt_upload_2-matnr to gt_upload_2-matnr.
elseif gt_upload_2-matnr na 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
unpack gt_upload_2-matnr to gt_upload_2-matnr.
endif.
if gt_upload_2-plnal <> ''. "组计数器 补零
unpack gt_upload_2-plnal to gt_upload_2-plnal.
endif.
if not gt_upload_2-lifnr is initial.
unpack gt_upload_2-lifnr to gt_upload_2-lifnr.
endif.
append gt_upload_2.
clear gt_upload_2.
endloop.
"单独扩展
move-corresponding gt_alv[] to gt_alvk[].
sort gt_alvk by plnnr plnal werks.
delete adjacent duplicates from gt_alvk comparing plnnr plnal werks.
loop at gt_alvk. "显示扩展工厂字段
gt_alvk-row = sy-tabix.
modify gt_alvk transporting row.
endloop.
"汇总检验计划行
clear gt_plnnr[].
move-corresponding gt_alvk[] to gt_plnnr[].
*-----------------------------------------------------------调用BAPI相关代码---------------------------------------------------------------------------
data : lv_toleranzun type cha_class_view-sollwert, "规范下限
lv_toleranzob type cha_class_view-sollwert. "规范上限
**CREATE-BAPI 中的任务清单抬头数据用于检验计划
data : task like table of bapi1191_tsk_c with header line .
**CREATE-BAPI 中的工序用于检验批
data : operation like table of bapi1191_opr_c with header line.
**检验计划 CREATE-BAPI 中的检验特性
data : insp_char like table of bapi1191_cha_c with header line.
**CREATE_BAPI 中的物料任务清单用于检验计划
data : matnr_allc like table of bapi1191_mtk_c with header line .
data : textallocation like table of bapi1191_txt_hdr_c with header line.
data : text like table of bapi1012_txt_c with header line.
**返回参数
data : retn like table of bapiret2 with header line.
data : group like bapi1191_tsk_c-task_list_group .
data : gp_cnter like bapi1191_tsk_c-group_counter .
data:l_chk_error(1).
data:begin of lt_qpmk occurs 0,
zaehler like qpmk-zaehler, "主检验特性的工厂
mkmnr like qpmk-mkmnr, "主文件检验特性
version like qpmk-version, "主检验特性的版本号
steuerkz like qpmk-steuerkz, "检验特性/主检验特性的控制标识串
end of lt_qpmk.
data:begin of lt_qpmz occurs 0,
zaehler like qpmk-zaehler, "主检验特性的工厂
mkmnr like qpmk-mkmnr, "主文件检验特性
auswmenge1 like qpmz-auswmenge1,
auswmgwrk1 like qpmz-auswmgwrk1,
katalgart2 like qpmz-katalgart2,
auswmenge2 like qpmz-auswmenge2,
end of lt_qpmz.
select zaehler
mkmnr
version
steuerkz
from qpmk
into table lt_qpmk
for all entries in gt_alv
where zaehler = gt_alv-qpmk_werks
and mkmnr = gt_alv-verwmerkm.
select zaehler
mkmnr
auswmenge1
auswmgwrk1
katalgart2
auswmenge2
from qpmz
into table lt_qpmz
for all entries in gt_alv
where zaehler = gt_alv-qpmk_werks
and mkmnr = gt_alv-verwmerkm.
sort lt_qpmk by zaehler mkmnr version descending.
delete adjacent duplicates from lt_qpmk comparing zaehler mkmnr.
read table gt_alv with key mtype = 'E'.
if sy-subrc = 0.
message s024 with '请先处理错误信息。' display like 'E'.
else.
loop at gt_plnnr.
clear:task[],matnr_allc[],operation[],insp_char[],textallocation[],text[],l_chk_error.
loop at gt_upload_2 where plnnr = gt_plnnr-plnnr and plnal = gt_plnnr-plnal_f."分配物料到任务清单 MAPL
matnr_allc-task_list_group = gt_plnnr-plnnr ."检验计划组
matnr_allc-group_counter = gt_plnnr-plnal."组计数器
* matnr_allc-group_counter = gt_upload_2-plnal."组计数器
* matnr_allc-plant = gt_upload_2-werks_mt." 物料工厂
matnr_allc-plant = gt_plnnr-werks." 物料工厂
matnr_allc-material = gt_upload_2-matnr.
matnr_allc-vendor = gt_upload_2-lifnr."供应商
matnr_allc-valid_from = sy-datum.
append matnr_allc.
clear matnr_allc.
endloop.
loop at gt_alv where plnnr = gt_plnnr-plnnr and plnal = gt_plnnr-plnal and werks = gt_plnnr-werks.
translate gt_alv-werks to upper case. "任务清单抬头 PLKO
task-plant = gt_alv-werks."工厂
task-task_list_group = gt_alv-plnnr ."检验计划组
task-group_counter = gt_alv-plnal."组计数器
task-description = gt_alv-ktext."组说明
task-task_list_usage = gt_alv-verwe."用途
if gt_alv-plnme is not initial.
call function 'CONVERSION_EXIT_CUNIT_INPUT' "批量单位
exporting
input = gt_alv-plnme
language = sy-langu
importing
output = task-task_measure_unit
exceptions
unit_not_found = 1
others = 2.
endif.
task-ident_key = gt_alv-slwbez."检验点
task-valid_from = sy-datum."开始生效日期
task-task_list_status = '4'.
task-dyn_modif_level = '3'.
append task.
clear task.
operation-task_list_group = gt_alv-plnnr ."检验计划组 “工序作业 PLPO
operation-group_counter = gt_alv-plnal."组计数器
operation-activity = gt_alv-vornr."工序
operation-description = gt_alv-ltxal."工序说明
operation-control_key = 'QM01'."OPERATION-STEUS."控制码
operation-work_cntr = gt_alv-arbpl."工作中心 没找到数据源
operation-plant = gt_alv-werks."工作中心的工厂
operation-valid_from = sy-datum.
operation-denominator = 1."用于转换工艺路线和工序单位的分母
operation-nominator = 1. "用于转换任务清单和工序计量单位的计数器
operation-base_quantity = 1."基本数量
if gt_alv-slwbez is not initial.
operation-insp_point_complt_flow_variant = '2'."检验点的评估方式
endif.
if gt_alv-plnme is not initial.
call function 'CONVERSION_EXIT_CUNIT_INPUT' "工序单位
exporting
input = gt_alv-plnme
language = sy-langu
importing
output = operation-operation_measure_unit
exceptions
unit_not_found = 1
others = 2.
endif.
append operation.
clear operation.
insp_char-task_list_group = gt_alv-plnnr ."检验计划组 “PLMK 检验计划特性
insp_char-group_counter = gt_alv-plnal."组计数器
insp_char-activity = gt_alv-vornr."工序
insp_char-inspchar = gt_alv-merknr."检验特性编号
insp_char-mstr_char = gt_alv-verwmerkm."主检验特性
insp_char-pmstr_char = '2010'."主检验特性的工厂
insp_char-char_descr = gt_alv-kurztext."检验特性的短文本
if gt_alv-meas_unit is not initial.
select single msehi from t006a into insp_char-meas_unit
where mseht = gt_alv-meas_unit
and spras = sy-langu.
endif.
insp_char-dec_places = gt_alv-stellen."小数位
* lv_toleranzun = gt_alv-toleranzun.
* lv_toleranzob = gt_alv-toleranzob.
if gt_alv-toleranzun is not initial.
clear lv_toleranzun.
call function 'QSS0_FLTP_TO_CHAR_CONVERSION'
exporting
i_number_of_digits = gt_alv-stellen
i_fltp_value = gt_alv-toleranzun
i_value_not_initial_flag = 'X'
i_screen_fieldlength = 16
importing
e_char_field = lv_toleranzun.
insp_char-lw_tol_lmt = lv_toleranzun."规范下限
insp_char-lw_pls_lmt = lv_toleranzun."实际下限
endif.
if gt_alv-toleranzob is not initial.
clear lv_toleranzob.
call function 'QSS0_FLTP_TO_CHAR_CONVERSION'
exporting
i_number_of_digits = gt_alv-stellen
i_fltp_value = gt_alv-toleranzob
i_value_not_initial_flag = 'X'
i_screen_fieldlength = 16
importing
e_char_field = lv_toleranzob.
insp_char-up_pls_lmt = lv_toleranzob."实际上限
insp_char-up_tol_lmt = lv_toleranzob."规范上限
endif.
insp_char-method = gt_alv-pmtnr."检验方法
insp_char-pmethod = '2010'."检验方法的工厂
insp_char-smpl_procedure = gt_alv-stichprver."采样过程
if gt_alv-probemgeh is not initial.
call function 'CONVERSION_EXIT_CUNIT_INPUT' "采样计量单位
exporting
input = gt_alv-probemgeh
language = sy-langu
importing
output = insp_char-smpl_unit
exceptions
unit_not_found = 1
others = 2.
else.
if gt_alv-plnme is not initial.
call function 'CONVERSION_EXIT_CUNIT_INPUT'
exporting
input = gt_alv-plnme
language = sy-langu
importing
output = insp_char-smpl_unit
exceptions
unit_not_found = 1
others = 2.
endif.
endif.
if gt_alv-pruefeinh is initial."基本采样数
insp_char-smpl_quant = 1.
else.
insp_char-smpl_quant = gt_alv-pruefeinh.
endif.
insp_char-dyn_modif_rule = gt_alv-qdynregel."动态修改规则
insp_char-infofield1 = gt_alv-infofield1."字段信息 1
insp_char-infofield2 = gt_alv-infofield2."字段信息 2
insp_char-infofield3 = gt_alv-infofield3."字段信息 3
insp_char-res_org = gt_alv-res_org."结果数据源
insp_char-valid_from = sy-datum."有效日期
read table lt_qpmz with key zaehler = '2010'
mkmnr = gt_alv-verwmerkm.
if sy-subrc = 0.
insp_char-sel_set1 = lt_qpmz-auswmenge1.
insp_char-psel_set1 = lt_qpmz-auswmgwrk1.
insp_char-cat_type2 = lt_qpmz-katalgart2.
insp_char-code_group2 = lt_qpmz-auswmenge2.
endif.
read table lt_qpmk with key zaehler = '2010'
mkmnr = gt_alv-verwmerkm.
if sy-subrc = 0.
data: p_steuerkz type qpmk-steuerkz.
clear p_steuerkz.
p_steuerkz = lt_qpmk-steuerkz. "检验特性/主检验特性的控制标识串
insp_char-quantitative_ind = p_steuerkz+0(1)."数量特性
insp_char-cha_master_import_modus = 'N'."参考主文件检验特性的方式
insp_char-meas_value_confirm_ind = p_steuerkz+1(1)."必须记录计量值
insp_char-attribute_required_ind = p_steuerkz+2(1)."要求特性属性的参考
if gt_alv-toleranzob is not initial.
insp_char-up_tol_lmt_ind = 'X'."规范上限标识
endif.
if gt_alv-toleranzun is not initial.
insp_char-lw_tol_lmt_ind = 'X'."规范下限标识
endif.
insp_char-target_val_check_ind = p_steuerkz+5(1)."检查目标值
insp_char-scope_ind = p_steuerkz+6(1)."检验范围
insp_char-long_term_insp_ind = gt_alv-long_term_insp_ind."长期检验
insp_char-result_recording_type = p_steuerkz+8(1)."记录类型
insp_char-docu_requ = p_steuerkz+9(1)."检验结果的必需凭证
insp_char-confirmation_category = gt_alv-confirmation_category ."特性类别
insp_char-add_sample_quantity = p_steuerkz+12(1)."附加采样
insp_char-formula_ind = p_steuerkz+14(1)."计算的特性
insp_char-sampling_procedure_ind = p_steuerkz+15(1)."要求采样过程
insp_char-qscore_and_share_relevant = p_steuerkz+16(1)."质量记分和废品份额的相关特性
insp_char-defect_no_confirmation = p_steuerkz+18(1)."记录缺陷数
insp_char-destructive_insp_ind = gt_alv-destructive_insp_ind."是否破坏性采样
insp_char-insp_tool_ind = p_steuerkz+21(1)."记录缺陷数
insp_char-auto_defct_recording = p_steuerkz+22(1)."缺陷记录已自动调用
insp_char-change_documents_req = p_steuerkz+23(1)."在结果记录期间创建更改凭证
insp_char-spc_ind = p_steuerkz+24(1)."SPC 特性
insp_char-print_ind = p_steuerkz+25(1)."打印
endif.
append insp_char.
clear insp_char.
endloop.
textallocation-object_type = '10'. "固定值不用管
textallocation-valid_from = sy-datum.
textallocation-task_list_group = gt_alv-plnnr ."检验计划组
textallocation-group_counter = gt_alv-plnal."组计数器
textallocation-langu = sy-langu.
textallocation-line_from = '1'.
textallocation-line_to = '2'.
append textallocation.
clear textallocation.
text-format_col = '1'.
text-text_line = gt_alv-ktext.
append text.
clear text.
text-format_col = '2'.
text-text_line = gt_alv-text_line.
append text.
clear text.
sort task by task_list_group group_counter .
delete adjacent duplicates from task comparing task_list_group group_counter .
sort operation by task_list_group group_counter activity .
delete adjacent duplicates from operation comparing task_list_group group_counter activity .
"创建
data: sf.
data:l_message type string.
call function 'BAPI_INSPECTIONPLAN_CREATE'
importing
group = group
groupcounter = gp_cnter
tables
task = task
materialtaskallocation = matnr_allc
operation = operation
inspcharacteristic = insp_char
text = text
textallocation = textallocation
return = retn.
read table retn with key type = 'E' id = 'BAPI'.
if sy-subrc = 0.
clear l_message.
loop at retn where type eq 'E' or type eq 'A'.
call function 'MESSAGE_TEXT_BUILD'
exporting
msgid = retn-id
msgnr = retn-number
msgv1 = retn-message_v1
msgv2 = retn-message_v2
msgv3 = retn-message_v3
msgv4 = retn-message_v4
importing
message_text_output = retn-message.
concatenate l_message retn-message into l_message.
endloop.
gt_plnnr-msage = l_message.
gt_plnnr-mtype = 'E'.
else.
call function 'BAPI_TRANSACTION_COMMIT'.
call function 'MESSAGE_TEXT_BUILD'
exporting
msgid = retn-id
msgnr = retn-number
msgv1 = retn-message_v1
msgv2 = retn-message_v2
msgv3 = retn-message_v3
msgv4 = retn-message_v4
importing
message_text_output = gt_plnnr-msage.
gt_plnnr-mtype = 'S'.
gt_plnnr-msage = '组:' && gt_plnnr-plnnr && '计数器' && gt_plnnr-plnal && '创建成功!'.
endif.
modify gt_plnnr.
refresh : task,matnr_allc,operation,insp_char,retn.
clear: task, matnr_allc,operation,insp_char,retn,group,gp_cnter.
endloop.
"回写消息
if p_cb1 = 'X'.
loop at gt_alvk .
read table gt_plnnr with key plnnr = gt_alvk-plnnr plnal = gt_alvk-plnal werks = gt_alvk-werks.
if sy-subrc = 0.
gt_alvk-msage = gt_plnnr-msage.
gt_alvk-mtype = gt_plnnr-mtype.
perform frm_set_color changing gt_alv.
modify gt_alvk transporting msage mtype.
endif.
endloop.
else.
loop at gt_alv .
read table gt_plnnr with key plnnr = gt_alv-plnnr plnal = gt_alv-plnal werks = gt_alv-werks.
if sy-subrc = 0.
gt_alv-msage = gt_plnnr-msage.
gt_alv-mtype = gt_plnnr-mtype.
perform frm_set_color changing gt_alv.
modify gt_alv transporting msage mtype.
endif.
endloop.
endif.
endif.