&---------------------------------------------------------------------
*& Form FRM_CHANGE_AUFRN
&---------------------------------------------------------------------
调整工单
----------------------------------------------------------------------
FORM frm_change_aufrn .
DATA:lt_return TYPE STANDARD TABLE OF bapiret2.
DATA:ls_return TYPE bapiret2.
DATA lv_error TYPE c.
DATA lv_msg TYPE char100.
DATA:ls_resb_d TYPE resb.
DATA:ls_resb_i TYPE resb.
DATA:ls_resb_m TYPE resb.
"删除组件
DATA:lt_resb_d TYPE TABLE OF resb.
DATA:lt_resb_i TYPE TABLE OF resb.
DATA:lt_resb_m TYPE TABLE OF resb.
DATA it_resb_d TYPE coxt_t_resbdel.
"新增组件
DATA: ls_storage_location TYPE coxt_s_storage_location,
ls_storage_locationx TYPE coxt_s_storage_locationx,
ls_requ_quan TYPE coxt_s_quantity,
lv_operation TYPE co_aplzl,
lv_positionno TYPE positionno,
lv_numc TYPE numc4.
"新增组件-重新刷新订单行号使用
TYPES: BEGIN OF ty_resb_bt.
INCLUDE TYPE resbb.
TYPES: indold TYPE syst_tabix.
TYPES: no_req_upd TYPE syst_datar.
TYPES: END OF ty_resb_bt.
TYPES tt_resb_bt TYPE TABLE OF ty_resb_bt.
FIELD-SYMBOLS:
*—修改生产订单组件定义
DATA:ls_order_key TYPE coxt_ord_key,
ls_order_component_key TYPE coxt_s_ord_comp_key,
ls_requirement_quantity TYPE coxt_s_quantity,
ls_requirement_quantityx TYPE coxt_s_quantityx,
ls_confirmed_quantity TYPE coxt_s_quantity,
ls_confirmed_quantityx TYPE coxt_s_quantityx,
lv_material TYPE coxt_material,
lv_materialx TYPE coxt_materialx,
ls_storage_location TYPE coxt_s_storage_location,
ls_storage_locationx TYPE coxt_s_storage_locationx,
lv_batch TYPE coxt_batch,
lv_batchx TYPE coxt_batchx,
lv_mi_configuration_object TYPE coxt_config_object,
lv_mi_configuration_objectx TYPE coxt_config_objectx,
lv_error_occurred TYPE c.
DATA:lt_resb_get LIKE TABLE OF resbdget,
ls_resb_get LIKE resbdget.
DATA lt_ztpplsbgb_d TYPE STANDARD TABLE OF ztpplsbgb.
DATA lt_ztpplsbgb_i TYPE STANDARD TABLE OF ztpplsbgb.
DATA lt_ztpplsbgb_m TYPE STANDARD TABLE OF ztpplsbgb.
DATA lt_ztpplsbgb_t TYPE STANDARD TABLE OF ztpplsbgb.
DATA lt_ztpplsbgb_s TYPE STANDARD TABLE OF ztpplsbgb.
DATA:lv_menge TYPE ztpplsbgb-menge.
DATA:lv_text TYPE string.
MOVE-CORRESPONDING gt_ztpplsbgb[] TO lt_ztpplsbgb_d[].
MOVE-CORRESPONDING gt_ztpplsbgb[] TO lt_ztpplsbgb_i[].
MOVE-CORRESPONDING gt_ztpplsbgb[] TO lt_ztpplsbgb_m[].
MOVE-CORRESPONDING gt_ztpplsbgb[] TO lt_ztpplsbgb_t[].
SORT lt_ztpplsbgb_d BY werks z_matnr1 z_matnr2 zsczj.
DELETE lt_ztpplsbgb_d WHERE zsczj IS INITIAL.
SORT lt_ztpplsbgb_i BY werks z_matnr1 z_matnr3 zxzbs.
DELETE lt_ztpplsbgb_i WHERE zxzbs IS INITIAL.
SORT lt_ztpplsbgb_m BY werks z_matnr1 z_matnr2 zylxg.
DELETE lt_ztpplsbgb_m WHERE zylxg IS INITIAL .
SORT lt_ztpplsbgb_t BY werks z_matnr1 z_matnr2 zylxg.
DELETE lt_ztpplsbgb_t WHERE zthbs IS INITIAL .
"取占用数量使用
MOVE-CORRESPONDING lt_ztpplsbgb_t[] TO lt_ztpplsbgb_s[].
SORT lt_ztpplsbgb_s BY z_matnr1 z_matnr2.
DELETE ADJACENT DUPLICATES FROM lt_ztpplsbgb_s COMPARING z_matnr1 z_matnr2.
LOOP AT gt_alv INTO gs_alv WHERE zbox = ‘X’
AND icon <> icon_led_green.
CLEAR gs_alv-icon.
"拣配单冻结状态 =X ,则报错“选择工单清单中有冻结工单”
IF gs_alv-zdjzt = ‘X’.
gs_alv-msg = ‘选择工单清单中有冻结工单’.
gs_alv-icon = icon_led_red.
MODIFY gt_alv FROM gs_alv.
CLEAR gs_alv.
CONTINUE.
ENDIF.
SELECT
rsnum,
rspos,
matnr,
werks,
bdmng,
enmng,
kzaus,
meins
INTO TABLE @DATA(ltt_resb)
FROM resb
WHERE aufnr EQ @gs_alv-aufnr
AND baugr EQ @gs_alv-matnr
AND posnr EQ @ls_stb-posnr
AND xloek NE 'X'.
REFRESH lt_resb_d.
REFRESH lt_resb_i.
REFRESH lt_resb_m.
LOOP AT ltt_resb INTO DATA(ls_resb).
"获取需要删除的工单行
IF ls_resb-kzaus <> '2'."截止类型不能等于2,等于2必须整个工单的2一起删除,不然会导致程序死循环
CLEAR ls_resb_d.
MOVE-CORRESPONDING ls_resb TO ls_resb_d.
READ TABLE lt_ztpplsbgb_d INTO DATA(ls_ztpplsbgb_d) WITH KEY werks = ls_resb_d-werks
z_matnr1 = gs_alv-matnr
z_matnr2 = ls_resb_d-matnr
zsczj = 'X'
BINARY SEARCH.
IF sy-subrc = 0.
APPEND ls_resb_d TO lt_resb_d.
ENDIF.
ENDIF.
"数量替换
CLEAR ls_resb_m.
MOVE-CORRESPONDING ls_resb TO ls_resb_m.
READ TABLE lt_ztpplsbgb_m INTO DATA(ls_ztpplsbgb_m) WITH KEY werks = ls_resb-werks
z_matnr1 = gs_alv-matnr
z_matnr2 = ls_resb-matnr
zylxg = 'X'
BINARY SEARCH.
IF sy-subrc = 0.
ls_resb_m-matnr = ls_ztpplsbgb_m-z_matnr2.
ls_resb_m-bdmng = ls_ztpplsbgb_m-menge * gs_alv-gamng.
APPEND ls_resb_m TO lt_resb_m.
ENDIF.
ENDLOOP.
"获取需要新增的工单行
LOOP AT lt_ztpplsbgb_i INTO DATA(ls_ztpplsbgb_i) WHERE werks = gs_alv-werks
AND z_matnr1 = gs_alv-matnr.
CLEAR ls_resb_i.
ls_resb_i-matnr = ls_ztpplsbgb_i-z_matnr3.
ls_resb_i-bdmng = ls_ztpplsbgb_i-menge * gs_alv-gamng.
APPEND ls_resb_i TO lt_resb_i.
ENDLOOP.
"取占用数量使用
LOOP AT lt_ztpplsbgb_s INTO DATA(ls_ztpplsbgb_s).
CLEAR lv_menge.
PERFORM frm_get_zzppco02 USING ls_ztpplsbgb_s lv_menge."替换数量逻辑处理
ls_ztpplsbgb_s-menge = lv_menge."当前占用数量
MODIFY lt_ztpplsbgb_s FROM ls_ztpplsbgb_s.
ENDLOOP.
"替换或者新增
SORT ltt_resb BY werks matnr.
LOOP AT lt_ztpplsbgb_t INTO DATA(ls_ztpplsbgb_t) WHERE z_matnr1 = gs_alv-matnr.
IF ls_ztpplsbgb_t-zthsl IS NOT INITIAL.
CLEAR lv_menge.
READ TABLE lt_ztpplsbgb_s ASSIGNING FIELD-SYMBOL()
WITH KEY z_matnr1 = ls_ztpplsbgb_t-z_matnr1
z_matnr2 = ls_ztpplsbgb_t-z_matnr2
BINARY SEARCH.
IF sy-subrc = 0.
lv_menge = -menge.
ENDIF.
IF ls_ztpplsbgb_t-zthsl > lv_menge.
READ TABLE ltt_resb INTO ls_resb WITH KEY matnr = ls_ztpplsbgb_t-z_matnr2 BINARY SEARCH.
IF sy-subrc = 0.
CLEAR ls_resb_m.
MOVE-CORRESPONDING ls_resb TO ls_resb_m.
IF ls_resb-bdmng > ls_ztpplsbgb_t-zthsl - lv_menge.
"现在新增的数量
ls_resb_m-matnr = ls_ztpplsbgb_t-z_matnr3.
ls_resb_m-bdmng = ls_ztpplsbgb_t-zthsl - lv_menge.
APPEND ls_resb_m TO lt_resb_i.
"调整原来的数量
ls_resb_m-matnr = ls_ztpplsbgb_t-z_matnr2.
ls_resb_m-bdmng = ls_resb-bdmng - ( ls_ztpplsbgb_t-zthsl - lv_menge ).
APPEND ls_resb_m TO lt_resb_m.
ELSE.
"调整原来的数量
ls_resb_m-matnr = ls_ztpplsbgb_t-z_matnr3.
ls_resb_m-bdmng = ls_resb_m-bdmng .
APPEND ls_resb_m TO lt_resb_m.
ENDIF.
ENDIF.
ENDIF.
IF lv_menge <> -menge OR lv_menge IS NOT INITIAL .
-menge = lv_menge.
ENDIF.
ELSE.
READ TABLE ltt_resb INTO ls_resb WITH KEY matnr = ls_ztpplsbgb_t-z_matnr2 BINARY SEARCH.
IF sy-subrc = 0.
CLEAR ls_resb_m.
MOVE-CORRESPONDING ls_resb TO ls_resb_m.
"调整原来的数量
ls_resb_m-matnr = ls_ztpplsbgb_t-z_matnr3.
ls_resb_m-bdmng = ls_resb_m-bdmng * ls_ztpplsbgb_t-menge.
APPEND ls_resb_m TO lt_resb_m.
ENDIF.
ENDIF.
ENDLOOP.
----------------------------------------------------------------------
"工单组件删除
IF lt_resb_d[] IS NOT INITIAL.
lv_text = '正在删除工单:' && gs_alv-aufnr && '的组件'.
PERFORM frm_display_progress USING lv_text."进度条显示
REFRESH it_resb_d.
REFRESH lt_return.
CLEAR lv_error.
it_resb_d[] = CORRESPONDING #( lt_resb_d[] ).
CALL FUNCTION 'CO_XT_COMPONENTS_DELETE'
EXPORTING
it_resbkeys_to_delete = it_resb_d[]
IMPORTING
e_error_occurred = lv_error
TABLES
ct_bapireturn = lt_return
EXCEPTIONS
delete_failed = 1
OTHERS = 2.
IF lv_error = space.
"刷新变
PERFORM change_flag_set(saplcozv).
"数据更新到DB
CALL FUNCTION 'CO_XT_ORDER_PREPARE_COMMIT'
IMPORTING
es_bapireturn = ls_return
e_error_occurred = lv_error
TABLES
et_bapireturn = lt_return.
COMMIT WORK AND WAIT.
WAIT UP TO 1 SECONDS.
IF ( ls_return-type = 'S'
OR ls_return-type = 'W'
OR ls_return-type = 'I' )
OR ls_return IS INITIAL.
DB COMMMIT
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = ‘X’.
刷新变量
CALL FUNCTION ‘CO_XT_ORDER_INITIALIZE’.
gs_alv-msg = '工单组件删除成功'.
gs_alv-icon = icon_led_green.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
gs_alv-msg = '工单组件删除失败:'&& ls_return-message.
gs_alv-icon = icon_led_red.
ENDIF.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
LOOP AT lt_return INTO ls_return.
gs_alv-msg = gs_alv-msg && ls_return-message.
ENDLOOP.
gs_alv-msg = '工单组件删除失败:'&& gs_alv-msg.
gs_alv-icon = icon_led_red.
ENDIF.
ELSE.
gs_alv-msg = ‘没有需要删除的组件’.
gs_alv-icon = icon_led_green.
ENDIF.
----------------------------------------------------------------------
"调整工单组件
IF lt_resb_m[] IS NOT INITIAL.
lv_text = '正在调整工单:' && gs_alv-aufnr && '的组件'.
PERFORM frm_display_progress USING lv_text."进度条显示
CLEAR lv_msg.
LOOP AT lt_resb_m INTO ls_resb_m.
CLEAR :
ls_order_key ,
ls_order_component_key ,
ls_requirement_quantity ,
ls_requirement_quantityx ,
ls_confirmed_quantity ,
ls_confirmed_quantityx ,
lv_material ,
lv_materialx ,
ls_storage_location ,
ls_storage_locationx ,
lv_batch ,
lv_batchx ,
lv_mi_configuration_object ,
lv_mi_configuration_objectx,
lv_error_occurred ,
ls_return .
ls_order_key = gs_alv-aufnr .
ls_order_component_key-rsnum = ls_resb_m-rsnum. "预留号
ls_order_component_key-rspos = ls_resb_m-rspos. "预留行号
ls_order_component_key-rsart = ls_resb_m-rsart. "记录类型
ls_confirmed_quantity-quantity = ls_resb_m-bdmng."数量
ls_confirmed_quantity-uom = ls_resb_m-meins."单位
ls_confirmed_quantityx-quantity = 'X'."数量
ls_confirmed_quantityx-uom = 'X'."单位
lv_material = ls_resb_m-matnr.
lv_materialx = 'X'.
ls_storage_location-werks = gs_alv-werks.
ls_storage_locationx-werks = 'X'.
CALL FUNCTION 'CO_XT_ORDER_INITIALIZE'."初始化
CALL FUNCTION 'CO_XT_COMPONENT_CHANGE'
EXPORTING
is_order_key = ls_order_key "生产订单号
is_order_component_key = ls_order_component_key "RESB主键
is_requirement_quantity = ls_requirement_quantity "需求量单位
is_requirement_quantityx = ls_requirement_quantityx "更改标识
is_confirmed_quantity = ls_confirmed_quantity "需求数量
is_confirmed_quantityx = ls_confirmed_quantityx "更改标识
i_material = lv_material "物料
i_materialx = lv_materialx "更改标识
is_storage_location = ls_storage_location "库存地点
is_storage_locationx = ls_storage_locationx "更改标识
i_batch = lv_batch "批号
i_batchx = lv_batchx "更改标识
i_mi_configuration_object = lv_mi_configuration_object "配置(内部对象号)
i_mi_configuration_objectx = lv_mi_configuration_objectx "更改标识
IMPORTING
es_bapireturn = ls_return
e_error_occurred = lv_error_occurred.
IF sy-subrc = 0.
IF lv_error_occurred IS INITIAL.
CALL FUNCTION 'CO_XT_ORDER_PREPARE_COMMIT' "保存准备
IMPORTING
e_order_key = ls_order_key
es_bapireturn = ls_return
e_error_occurred = lv_error_occurred
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
CLEAR:
lt_resb_get[],
ls_resb_get.
CALL FUNCTION 'CO_BC_RESBD_OF_ORDER_GET' "获取更新任务
EXPORTING
aufnr_act = ls_order_key
TABLES
resbd_get = lt_resb_get[].
LOOP AT lt_resb_get INTO ls_resb_get WHERE rsnum = ls_resb_m-rsnum
AND rspos = ls_resb_m-rspos
AND rsart = ls_resb_m-rsart.
ls_resb_get-nomng = ls_resb_m-bdmng.
ls_resb_get-bdmng = ls_resb_m-bdmng.
MODIFY lt_resb_get FROM ls_resb_get INDEX sy-tabix.
ENDLOOP.
CALL FUNCTION 'CO_BC_UPDATE_CMP_OF_ORDERS' "执行更新任务
TABLES
resbd_get = lt_resb_get[].
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' "保存
EXPORTING
wait = 'X'.
CALL FUNCTION 'CO_XT_ORDER_INITIALIZE'."初始化恢复
ELSE.
lv_msg = lv_msg && '/' && ls_return-message.
gs_alv-icon = icon_led_red .
ENDIF.
ELSE.
lv_msg = lv_msg && '/' && ls_return-message.
gs_alv-icon = icon_led_red .
ENDIF.
ELSE.
lv_msg = lv_msg && '/' && ls_return-message .
gs_alv-icon = icon_led_red .
ENDIF.
ENDLOOP.
IF lv_msg IS INITIAL.
gs_alv-msg = gs_alv-msg && '/' &&'组件调整成功'.
IF gs_alv-icon <> icon_led_red.
gs_alv-icon = icon_led_green.
ENDIF.
ELSE.
gs_alv-msg = gs_alv-msg && '/' && '组件调整失败:' && lv_msg.
gs_alv-icon = icon_led_red.
ENDIF.
ELSE.
gs_alv-msg = gs_alv-msg && '/' && '没有需要调整的组件'.
IF gs_alv-icon <> icon_led_red.
gs_alv-icon = icon_led_green.
ENDIF.
ENDIF.
----------------------------------------------------------------------
"新增工单组件
IF lt_resb_i[] IS NOT INITIAL AND gs_alv-icon = icon_led_green.
lv_text = '正在新增工单:' && gs_alv-aufnr && '的组件'.
PERFORM frm_display_progress USING lv_text."进度条显示
CLEAR lv_msg.
LOOP AT lt_resb_i INTO ls_resb_i.
CLEAR ls_requ_quan.
CLEAR lv_operation.
CLEAR ls_storage_location.
CLEAR ls_storage_locationx.
CLEAR lv_positionno.
CLEAR ls_return.
CLEAR lv_error.
ls_requ_quan-quantity = ls_resb_i-bdmng.
ls_requ_quan-uom = ls_resb_i-meins.
ls_storage_location-werks = gs_alv-werks.
ls_storage_locationx-werks = 'X'.
IF lt_resb-lgort IS NOT INITIAL.
ls_storage_location-lgort = lt_resb-lgort."选择屏幕
ls_storage_locationx-lgort = 'X'.
ENDIF.
CALL FUNCTION 'CO_XT_COMPONENT_ADD'
EXPORTING
is_order_key = gs_alv-aufnr
i_material = ls_resb_i-matnr
is_requ_quan = ls_requ_quan
i_operation = lv_operation
is_storage_location = ls_storage_location
is_storage_locationx = ls_storage_locationx
i_postp = 'L'
i_posno = lv_positionno
IMPORTING
es_bapireturn = ls_return
e_error_occurred = lv_error.
IF lv_error = space.
"对工单行号重新刷
ASSIGN ('(SAPLCOBC)RESB_BT[]') TO .
LOOP AT ASSIGNING .
CLEAR lv_numc.
lv_numc = sy-tabix * 10.
-posnr = lv_numc.
ENDLOOP.
Commit transaction
CALL FUNCTION 'CO_XT_ORDER_PREPARE_COMMIT'
IMPORTING
es_bapireturn = ls_return
e_error_occurred = lv_error
TABLES
et_bapireturn = lt_return.
IF ( ls_return-type = 'S' OR
ls_return-type = 'W' OR
ls_return-type = 'I' ) OR
ls_return IS INITIAL.
Commit data
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
CALL FUNCTION 'CO_XT_ORDER_INITIALIZE'.
ELSE.
Data Rollback
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
lv_msg = lv_msg && '/' && ls_return-message.
ENDIF.
ELSE.
Data Rollback
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
lv_msg = lv_msg && '/' && ls_return-message.
ENDIF.
ENDLOOP.
IF lv_msg IS INITIAL.
gs_alv-msg = gs_alv-msg && '/' &&'BOM组件新增成功'.
IF gs_alv-icon <> icon_led_red.
gs_alv-icon = icon_led_green.
ENDIF.
ELSE.
gs_alv-msg = gs_alv-msg && '/' && 'BOM组件新增失败:' && lv_msg.
gs_alv-icon = icon_led_red.
ENDIF.
ELSE.
gs_alv-msg = gs_alv-msg && ‘/’ && ‘没有需要新增的组件’.
IF gs_alv-icon <> icon_led_red.
gs_alv-icon = icon_led_green.
ENDIF.
ENDIF.
MODIFY gt_alv FROM gs_alv.
CLEAR gs_alv.
ENDLOOP.
ENDFORM.
&---------------------------------------------------------------------