PS:物资需求导入 BAPI_NETWORK_MAINTAIN

FORM call_bapi_post_data USING fu_fm_name TYPE rs38l-name.

  "bapi for BAPI_NETWORK_MAINTAIN
  DATA: lt_method_project          TYPE STANDARD TABLE OF bapi_method_project,
        ls_method_project          LIKE LINE OF  lt_method_project,
        lt_network                 TYPE STANDARD TABLE OF bapi_network,
        ls_network                 LIKE LINE OF lt_network,
        lt_network_update          TYPE STANDARD TABLE OF bapi_network_update,
        ls_network_update          LIKE LINE OF lt_network_update,
        lt_activity                TYPE STANDARD TABLE OF bapi_network_activity, "BAPI_NETWORK_ACTIVITY,
        ls_activity                LIKE LINE OF lt_activity,
        lt_activity_update         TYPE STANDARD TABLE OF bapi_network_activity_up,
        ls_activity_update         LIKE LINE OF lt_activity_update,
        lt_message_table           TYPE STANDARD TABLE OF bapi_meth_message,
        ls_message_table           LIKE LINE OF lt_message_table,
        lt_activity_element        TYPE STANDARD TABLE OF bapi_act_element,
        ls_bapi_act_element        LIKE LINE OF lt_activity_element,
        lt_activity_element_update TYPE STANDARD TABLE OF bapi_act_element_upd,
        ls_activity_element_update LIKE LINE OF lt_activity_element_update,
        lt_activity_milestone      TYPE STANDARD TABLE OF bapi_act_milestone,
        ls_activity_milestone      LIKE LINE OF lt_activity_milestone,
        lt_return                  TYPE STANDARD TABLE OF bapiret2,
        lv_message                 TYPE string.

  " bapi for BAPI_BUS2002_ACT_CREATE_MULTI
  DATA:lt_activity_2002 LIKE TABLE OF bapi_bus2002_act_new,
       ls_activity_2002 LIKE LINE OF lt_activity_2002.
  DATA:lt_et_return LIKE TABLE OF bapiret2.
  DATA:lt_et_return2 LIKE TABLE OF bapiret2.

  "bapi for BAPI_NETWORK_COMP_ADD
  DATA:lt_return_comp_add  TYPE TABLE OF bapiret2,
       ls_return_comp_add  LIKE LINE OF  lt_return_comp_add,
       lt_i_components_add TYPE TABLE OF bapi_network_comp_add,
       ls_i_components_add LIKE LINE OF lt_i_components_add,
       lt_e_message_table  TYPE TABLE OF bapi_meth_message,
       ls_e_message_table  LIKE LINE OF lt_e_message_table.

  DATA:ls_output LIKE LINE OF it_output.

  DATA:l_num(6) TYPE c VALUE '000000'.
  TYPES:BEGIN OF ly_network,
          aufnr TYPE aufk-aufnr,
        END OF ly_network.
  DATA:lt_net_work TYPE TABLE OF ly_network,
       ls_net_work LIKE LINE OF lt_net_work,
       l_network   LIKE aufk-aufnr.
  DATA:l_tabix LIKE sy-tabix.
  DATA:l_tabix_copy LIKE sy-tabix.
  DATA:l_output_num TYPE i VALUE 0."行项目计数

  DATA:lt_output_copy TYPE STANDARD TABLE OF ty_input,
       ls_output_copy LIKE LINE OF lt_output_copy.
  DATA:lt_output_temp TYPE STANDARD TABLE OF ty_input,
       ls_output_temp LIKE LINE OF lt_output_copy.
  DATA:lt_output_temp2 TYPE STANDARD TABLE OF ty_input,
       ls_output_temp2 LIKE LINE OF lt_output_copy.

  APPEND LINES OF it_output_check_after TO lt_output_copy.


  " it_output
  LOOP AT it_output_check_after INTO ls_output.
    ls_output_temp = ls_output.
    CLEAR:lt_output_temp[].
    APPEND ls_output_temp TO lt_output_temp.

    IF ls_output-vornr EQ '0010' AND ls_output-posnr EQ '0010'.
      PERFORM get_bapi_head TABLES lt_method_project lt_network         lt_network_update lt_output_temp
                                   lt_activity       lt_activity_update  CHANGING l_network.
    ENDIF.
    IF ls_output-zfinal = 1.
      PERFORM get_bapi_item TABLES lt_i_components_add lt_output_temp lt_output_temp2 lt_activity_2002 CHANGING l_output_num.
      PERFORM call_bapi TABLES lt_method_project  lt_network       lt_network_update lt_activity
                               lt_activity_update lt_activity_2002 lt_output_copy    lt_i_components_add
                               lt_output_temp2
                        USING l_network l_output_num.
      CLEAR:lt_method_project[],
            ls_method_project,
            l_network,
            ls_method_project,
            lt_method_project[],
            ls_network,
            lt_network,
            ls_network_update,
            ls_activity_update,
            lt_return,
            lt_message_table,
            lt_message_table,
            lt_activity_2002,
            ls_activity_2002,
            lt_et_return,
            lt_et_return2,
            l_output_num,
            lt_i_components_add[],
            lt_output_temp2[].
    ELSE.
      PERFORM get_bapi_item TABLES lt_i_components_add lt_output_temp lt_output_temp2 lt_activity_2002 CHANGING l_output_num.
    ENDIF.
  ENDLOOP.
  "lt_output_copy
  LOOP AT lt_output_copy INTO ls_output_copy.
    READ TABLE it_output INTO ls_output WITH KEY zrow = ls_output_copy-zrow.
    IF sy-subrc = 0.
      MODIFY it_output FROM ls_output_copy INDEX sy-tabix.
    ENDIF.
    CLEAR:ls_output_copy,
          ls_output.
  ENDLOOP.


ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  GET_BAPI_HEAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_METHOD_PROJECT  text
*      -->P_LT_NETWORK  text
*      -->P_LT_NETWORK_UPDATE  text
*      -->P_L_TABIX  text
*      -->P_LS_OUTPUT_TEMP  text
*      <--P_L_NETWORK  text
*----------------------------------------------------------------------*
FORM get_bapi_head  TABLES   p_lt_method_project STRUCTURE bapi_method_project
                               "为 <...> 插入正确的名称
                             p_lt_network STRUCTURE bapi_network
                             p_lt_network_update STRUCTURE bapi_network_update
                             p_lt_output_temp STRUCTURE is_input
                             p_lt_activity STRUCTURE bapi_network_activity
                             p_lt_activity_update STRUCTURE bapi_network_activity_up
                    CHANGING p_l_network.

  DATA:p_ls_output_temp LIKE LINE OF p_lt_output_temp.
  "BAPI HEAD STRUCTURE DEFINE
  DATA: ls_method_project TYPE bapi_method_project,
        ls_network        TYPE bapi_network,
        ls_network_update TYPE bapi_network_update.

  "AUFNR TABLE DEFINE
  TYPES:BEGIN OF ly_network,
          aufnr TYPE aufk-aufnr,
        END OF ly_network.
  DATA:lt_net_work TYPE TABLE OF ly_network,
       ls_net_work LIKE LINE OF lt_net_work.


  SELECT
         aufnr
    INTO TABLE lt_net_work
    FROM aufk
    UP TO 1 ROWS
    WHERE aufnr LIKE '0000070%'
    ORDER BY aufnr DESCENDING.

  READ TABLE lt_net_work INTO ls_net_work INDEX 1.
  p_l_network = ls_net_work-aufnr + 1.

  READ TABLE p_lt_output_temp INTO p_ls_output_temp INDEX 1.
  ls_method_project-refnumber =  '000001'."流水号
  ls_method_project-objecttype = 'Network'.
  ls_method_project-method = 'CREATE'.
  ls_method_project-objectkey = p_l_network.
  SHIFT ls_method_project-objectkey LEFT DELETING LEADING space.
  APPEND ls_method_project TO p_lt_method_project.


  ls_method_project-refnumber = '000000'."流水号
  ls_method_project-objecttype = ''.
  ls_method_project-method = 'SAVE'.
  ls_method_project-objectkey = ''.
  APPEND ls_method_project TO p_lt_method_project.

  "HEAD
  ls_network-network = p_l_network."NETWORK
  SHIFT ls_network-network LEFT DELETING LEADING space.
  ls_network-profile = p_ls_output_temp-profidnetz."网络参数文件
  ls_network-network_type = p_ls_output_temp-auart."网络类型
  ls_network-plant = p_ls_output_temp-werks.       "工厂
  ls_network-short_text = p_ls_output_temp-ktext.  "网络描述
  ls_network-mrp_controller = '001'.        "MRP控制者
  ls_network-wbs_element = p_ls_output_temp-posid. "WBS元素
  ls_network-start_date = p_ls_output_temp-gstrp_start."网络-开始日期
  ls_network-finish_date = p_ls_output_temp-gstrp_end. "网络-结束日期
  APPEND ls_network TO p_lt_network.

  "HEAD UPDATE
  ls_network_update-profile        = 'X'."网络参数文件
  ls_network_update-network_type   = 'X'."网络类型
  ls_network_update-plant          = 'X'.       "工厂
  ls_network_update-short_text     = 'X'.  "网络描述
  ls_network_update-mrp_controller = 'X'.        "MRP控制者
  ls_network_update-wbs_element    = 'X'. "WBS元素
  ls_network_update-start_date     = 'X'."网络-开始日期
  ls_network_update-finish_date    = 'X'. "网络-结束日期
  APPEND ls_network_update TO p_lt_network_update.


  "JOB
  DATA:ls_activity        TYPE bapi_network_activity,
       ls_activity_update TYPE bapi_network_activity_up
*         P_LS_OUTPUT_TEMP LIKE LINE OF P_LT_OUTPUT_TEMP,
       .


  "ITEM
  READ TABLE p_lt_output_temp INTO p_ls_output_temp INDEX 1.

  ls_activity-activity = p_ls_output_temp-vornr.
  ls_activity-description = p_ls_output_temp-ltxa1.
  ls_activity-constraint_type_start = 1.
  ls_activity-constraint_type_finish = 1.

  ls_activity-constraint_start_date = p_ls_output_temp-constraint_start_date.
  ls_activity-constraint_start_time = '000000'.
  ls_activity-constraint_finish_date = p_ls_output_temp-constraint_finish_date.
  ls_activity-constraint_finish_time = '000000'.
  ls_activity-control_key = p_ls_output_temp-steus.
  APPEND ls_activity TO p_lt_activity.


  "ITEM UPDATE
  ls_activity_update-activity               = 'X'.
  ls_activity_update-description            = 'X'.
  ls_activity_update-constraint_type_start  = 'X'.
  ls_activity_update-constraint_type_finish = 'X'.

  ls_activity_update-constraint_start_date  = 'X'.
  ls_activity_update-constraint_start_time  = 'X'.
  ls_activity_update-constraint_finish_date = 'X'.
  ls_activity_update-constraint_finish_time = 'X'.
  ls_activity_update-control_key            = 'X'.
  APPEND ls_activity_update TO p_lt_activity_update.




ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  GET_BAPI_ITEM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_I_COMPONENTS_ADD  text
*----------------------------------------------------------------------*
FORM get_bapi_item  TABLES   p_lt_i_components_add STRUCTURE bapi_network_comp_add
                             p_lt_output_temp STRUCTURE is_input
                             p_lt_output_temp2 STRUCTURE is_input
                             p_lt_activity_2002 STRUCTURE bapi_bus2002_act_new
                             CHANGING p_l_output_num
                             .
  DATA:p_ls_output_temp LIKE LINE OF p_lt_output_temp.
  DATA:p_ls_i_components_add LIKE LINE OF p_lt_i_components_add.
  DATA:ls_activity_2002   TYPE bapi_bus2002_act_new.
  DATA:ls_activity_2002_temp   TYPE bapi_bus2002_act_new.

  DATA:l_wbs TYPE ps_pspel.
  DATA:ls_output_temp2 LIKE LINE OF p_lt_output_temp2.

  p_l_output_num = p_l_output_num + 1.

  READ TABLE p_lt_output_temp INTO p_ls_output_temp INDEX 1.
  p_ls_i_components_add-activity = p_ls_output_temp-vornr.

* REP START BY HANDLXD AT 20180712
** 项目 采购类型 判断
*  IF p_wzxq EQ 'X' .                                        "009415
*    IF p_ls_output_temp-profidnetz  EQ 'ZPS0001' OR
*       p_ls_output_temp-profidnetz  EQ 'ZPS0011' OR
*       p_ls_output_temp-profidnetz  EQ 'ZPS0021' .
*      IF p_ls_output_temp-sbdkz EQ '2'.
*        p_ls_i_components_add-type_of_pur_resv = '3'.
*      ELSE.
*        p_ls_i_components_add-type_of_pur_resv = '8'.
*      ENDIF.
*    ENDIF.
*  ELSEIF p_ysxq EQ 'X'.
*    IF p_ls_output_temp-profidnetz  EQ 'ZPS0003' OR
*       p_ls_output_temp-profidnetz  EQ 'ZPS0019' OR
*       p_ls_output_temp-profidnetz  EQ 'ZPS0029' .
*      IF p_ls_output_temp-sbdkz EQ '2'.
*        p_ls_i_components_add-type_of_pur_resv = '4'.
*      ELSE.
*        p_ls_i_components_add-type_of_pur_resv = '9'.
*      ENDIF.
*    ENDIF.
*  ELSEIF p_wzxqzj EQ 'X'.
*    IF p_ls_output_temp-profidnetz  EQ 'ZPS0001' OR
*    p_ls_output_temp-profidnetz  EQ 'ZPS0011' OR
*    p_ls_output_temp-profidnetz  EQ 'ZPS0021' .
*      IF p_ls_output_temp-sbdkz EQ '2'.
*        p_ls_i_components_add-type_of_pur_resv = '3'.
*      ELSE.
*        p_ls_i_components_add-type_of_pur_resv = '8'.
*      ENDIF.
*    ENDIF.
*
*    IF p_ls_output_temp-profidnetz  EQ 'ZPS0003' OR
*     p_ls_output_temp-profidnetz  EQ 'ZPS0019' OR
*     p_ls_output_temp-profidnetz  EQ 'ZPS0029' .
*      IF p_ls_output_temp-sbdkz EQ '2'.
*        p_ls_i_components_add-type_of_pur_resv = '4'.
*      ELSE.
*        p_ls_i_components_add-type_of_pur_resv = '9'.
*      ENDIF.
*
*    ENDIF.
*  ENDIF.
* 项目 采购类型 判断
  IF p_wzxq EQ 'X' .                                        "009415
    IF p_ls_output_temp-profidnetz  EQ 'ZPS0001' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0011' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0021' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0031' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0041' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0051' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0071' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0081' .
      IF p_ls_output_temp-sbdkz EQ '2'.
        p_ls_i_components_add-type_of_pur_resv = '3'.
      ELSE.
        p_ls_i_components_add-type_of_pur_resv = '8'.
      ENDIF.
    ELSEIF p_ls_output_temp-profidnetz  EQ 'ZPS0061'.
      p_ls_i_components_add-type_of_pur_resv = '5'.
    ENDIF.

  ELSEIF p_ysxq EQ 'X'.
    IF p_ls_output_temp-profidnetz  EQ 'ZPS0003' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0019' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0029' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0039' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0049' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0059' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0079' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0089'.
      IF p_ls_output_temp-sbdkz EQ '2'.
        p_ls_i_components_add-type_of_pur_resv = '4'.
      ELSE.
        p_ls_i_components_add-type_of_pur_resv = '9'.
      ENDIF.
    ELSEIF p_ls_output_temp-profidnetz  EQ 'ZPS0061'.
      p_ls_i_components_add-type_of_pur_resv = '5'.
    ENDIF.
  ELSEIF p_wzxqzj EQ 'X'.
    IF p_ls_output_temp-profidnetz  EQ 'ZPS0001' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0011' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0021' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0031' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0041' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0051' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0071' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0081' .
      IF p_ls_output_temp-sbdkz EQ '2'.
        p_ls_i_components_add-type_of_pur_resv = '3'.
      ELSE.
        p_ls_i_components_add-type_of_pur_resv = '8'.
      ENDIF.
    ELSEIF p_ls_output_temp-profidnetz  EQ 'ZPS0061'.
      p_ls_i_components_add-type_of_pur_resv = '5'.
    ENDIF.

    IF p_ls_output_temp-profidnetz  EQ 'ZPS0003' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0019' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0029' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0039' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0049' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0059' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0079' OR
       p_ls_output_temp-profidnetz  EQ 'ZPS0089'.
      IF p_ls_output_temp-sbdkz EQ '2'.
        p_ls_i_components_add-type_of_pur_resv = '4'.
      ELSE.
        p_ls_i_components_add-type_of_pur_resv = '9'.
      ENDIF.
    ELSEIF p_ls_output_temp-profidnetz  EQ 'ZPS0061'.
      p_ls_i_components_add-type_of_pur_resv = '5'.
    ENDIF.
  ENDIF.
* REP END   BY HANDLXD AT 20180712


  p_ls_i_components_add-item_number = p_ls_output_temp-posnr.
  CALL FUNCTION 'CONVERSION_EXIT_ABPSP_INPUT'
    EXPORTING
      input  = p_ls_output_temp-posid
    IMPORTING
      output = l_wbs
*     EXCEPTIONS
*     NOT_FOUND       = 1
*     OTHERS = 2
    .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  p_ls_i_components_add-wbs_element = l_wbs."P_LS_OUTPUT_TEMP-POSID.
  p_ls_i_components_add-material = p_ls_output_temp-matnr.
  p_ls_i_components_add-plant = p_ls_output_temp-werks.
  p_ls_i_components_add-entry_quantity = p_ls_output_temp-menge.
  p_ls_i_components_add-base_uom = p_ls_output_temp-meins.
  p_ls_i_components_add-req_date = p_ls_output_temp-bdter.
  p_ls_i_components_add-bomexpl_no = p_ls_output_temp-sernr.
  p_ls_i_components_add-unload_pt = p_ls_output_temp-ablad.
  p_ls_i_components_add-gr_rcpt = p_ls_output_temp-gr_rcpt.
  p_ls_i_components_add-item_text = p_ls_output_temp-gr_rcpt.
*  p_ls_i_components_add-TYPE_OF_PUR_RESV = '5'.

  "写死
  p_ls_i_components_add-mrp_relevant = '2'.
  p_ls_i_components_add-item_cat = 'L'.
*    P_LS_I_COMPONENTS_ADD-ITEM_CAT = 'L'.
  APPEND p_ls_i_components_add TO p_lt_i_components_add.

  ls_output_temp2-zdj = p_ls_output_temp-zdj.
  APPEND ls_output_temp2 TO p_lt_output_temp2.

  ls_activity_2002-activity = p_ls_output_temp-vornr.
  ls_activity_2002-wbs_element = p_ls_output_temp-posid.
  ls_activity_2002-description = p_ls_output_temp-ltxa1.
  ls_activity_2002-constraint_type_start = 1.
  ls_activity_2002-constraint_type_finish = 1.

  ls_activity_2002-constraint_start_date = p_ls_output_temp-constraint_start_date.
  ls_activity_2002-constraint_start_time = '000000'.
  ls_activity_2002-constraint_finish_date = p_ls_output_temp-constraint_finish_date.
  ls_activity_2002-constraint_finish_time = '000000'.
  ls_activity_2002-control_key = p_ls_output_temp-steus.
  READ TABLE p_lt_activity_2002 INTO ls_activity_2002_temp WITH KEY activity = ls_activity_2002-activity.
  IF sy-subrc = 0.
    MODIFY TABLE p_lt_activity_2002 FROM ls_activity_2002.
  ELSE.
    APPEND ls_activity_2002 TO p_lt_activity_2002.
  ENDIF.
  CLEAR:ls_activity_2002,
        ls_activity_2002_temp.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  CALL_BAPI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_METHOD_PROJECT  text
*      -->P_LT_NETWORK  text
*      -->P_LT_NETWORK_UPDATE  text
*      -->P_LT_ACTIVITY  text
*      -->P_LT_ACTIVITY_UPDATE  text
*      -->P_LT_ACTIVITY_2002  text
*      -->P_L_NETWORK  text
*----------------------------------------------------------------------*
FORM call_bapi  TABLES  p_lt_method_project STRUCTURE bapi_method_project
                           "为 <...> 插入正确的名称
                         p_lt_network STRUCTURE bapi_network
                         p_lt_network_update STRUCTURE bapi_network_update
                         p_lt_activity STRUCTURE bapi_network_activity
                         p_lt_activity_update STRUCTURE bapi_network_activity_up
                         p_lt_activity_2002 STRUCTURE bapi_bus2002_act_new
                         p_lt_output_copy STRUCTURE is_input
                         p_lt_i_components_add STRUCTURE bapi_network_comp_add
                         p_lt_output_temp2 STRUCTURE is_input
                USING    p_l_network
                         p_l_output_num.

  "BAPI MESSAGE DEFINE
  DATA:lt_message_table TYPE STANDARD TABLE OF bapi_meth_message,
       ls_message_table LIKE LINE OF lt_message_table.
  DATA:lt_et_return LIKE TABLE OF bapiret2,
       ls_et_return LIKE LINE OF lt_et_return.
  DATA:lt_et_return2 LIKE TABLE OF bapiret2,
       ls_et_return2 LIKE LINE OF lt_et_return2.
  DATA:ls_output_copy LIKE LINE OF p_lt_output_copy.
  DATA:num_dec      TYPE i VALUE 0,
       num_dec_copy TYPE i VALUE 0. "行项目倒数计数
  DATA:l_temp TYPE i VALUE 1.
  DATA:l_net_work_temp LIKE aufk-aufnr."暂时存AUFNR的数据
  DATA:l_network LIKE aufk-aufnr,
       l_rsnum   LIKE resb-rsnum.
  l_network = p_l_network.
  SHIFT l_network LEFT DELETING LEADING space.

  DATA:lt_return          TYPE bapiret2,
       lt_e_message_table TYPE TABLE OF bapi_meth_message,
       ls_e_message_table LIKE LINE OF lt_e_message_table
       .
  DATA:p_ls_i_components_add LIKE LINE OF p_lt_i_components_add.

  DATA:p_ls_output_temp2 LIKE LINE OF p_lt_output_temp2.

  l_net_work_temp = l_network.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = l_net_work_temp
    IMPORTING
      output = l_net_work_temp.

  MOVE p_l_output_num TO num_dec.
*    ADD 1 TO NUM_DEC.
  MOVE p_l_output_num TO num_dec_copy.

  "call bapi
  CALL FUNCTION 'BAPI_NETWORK_MAINTAIN'
*       IMPORTING
*         RETURN                            =
    TABLES
      i_method_project  = p_lt_method_project[]
      i_network         = p_lt_network[]
      i_network_update  = p_lt_network_update[]
      i_activity        = p_lt_activity[]
      i_activity_update = p_lt_activity_update[]
*     I_RELATION        =
*     I_RELATION_UPDATE =
      e_message_table   = lt_message_table
*     I_ACTIVITY_ELEMENT                =
*     I_ACTIVITY_ELEMENT_UPDATE         =
*     I_ACTIVITY_MILESTONE              =
*     I_ACTIVITY_MILESTONE_UPDATE       =
    .
  READ TABLE lt_message_table INTO ls_message_table WITH KEY message_type = 'E'.
  IF sy-subrc = 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    READ TABLE p_lt_output_copy INTO ls_output_copy WITH KEY message = ''.
    IF sy-subrc = 0.
      ls_output_copy-status = icon_led_red.
      ls_output_copy-message = '网络作业创建失败!'.
      MODIFY p_lt_output_copy FROM ls_output_copy INDEX sy-tabix.
    ENDIF.
  ELSE.

    CLEAR:ls_message_table.
    LOOP AT lt_message_table INTO ls_message_table WHERE internal_object_id <> '' .
      l_network = ls_message_table-internal_object_id.
      EXIT.
    ENDLOOP.


    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.

*        WHILE L_TEMP = 1."等网络创建成功后才创建作业
*          SELECT SINGLE
*            AUFNR
*            INTO L_NET_WORK_TEMP
*            FROM AUFK
*            WHERE AUFNR = L_NET_WORK_TEMP.
*
*            IF SY-SUBRC = 0.
*              L_TEMP = 0.
*              EXIT.
*            ENDIF.
*        ENDWHILE.
    WAIT UP TO 1 SECONDS.

    "when u want to create components of activity in network ,u must init the network
    CALL FUNCTION 'BAPI_PS_INITIALIZATION'.
*        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*          EXPORTING
*            wait = 'X'.

    "create activity
    CALL FUNCTION 'BAPI_BUS2002_ACT_CREATE_MULTI'
      EXPORTING
        i_number    = l_network "P_L_NETWORK
      TABLES
        it_activity = p_lt_activity_2002
        et_return   = lt_et_return
*       EXTENSIONIN =
*       EXTENSIONOUT       =
      .
    READ TABLE lt_et_return INTO ls_et_return WITH KEY type = 'E'.
    IF sy-subrc = 0.
      LOOP AT p_lt_output_copy INTO ls_output_copy WHERE aufnr = ''."通过倒数的方式去更新内表,因为不能用key值去更新
        IF num_dec = 0.
          EXIT.
        ELSE.
          ls_output_copy-status = icon_led_red.
          ls_output_copy-message = '网络作业创建失败!'.
          CONCATENATE ls_output_copy-message ls_et_return-message INTO ls_output_copy-message.
          MODIFY p_lt_output_copy FROM ls_output_copy.
          num_dec = num_dec - 1.
        ENDIF.
      ENDLOOP.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      EXIT.
    ENDIF.



*   注:P_LT_I_COMPONENTS_ADD->TYPE_OF_PUR_RESV改判断逻辑
    "create components of activity
    CALL FUNCTION 'BAPI_NETWORK_COMP_ADD'
      EXPORTING
        number           = l_network
      IMPORTING
        return           = lt_return
      TABLES
        i_components_add = p_lt_i_components_add
        e_message_table  = lt_e_message_table.

    READ TABLE lt_e_message_table INTO ls_e_message_table WITH KEY message_type = 'E'.
    IF sy-subrc = 0.
      LOOP AT p_lt_output_copy INTO ls_output_copy WHERE aufnr = ''."通过倒数的方式去更新内表,因为不能用key值去更新
        IF num_dec = 0.
          EXIT.
        ELSE.
          ls_output_copy-status = icon_led_red.
          ls_output_copy-message = '网络作业创建失败!'.
          CONCATENATE ls_output_copy-message ls_e_message_table-message_text INTO ls_output_copy-message.
          MODIFY p_lt_output_copy FROM ls_output_copy.
          num_dec = num_dec - 1.
        ENDIF.
      ENDLOOP.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      EXIT.
    ELSE.

      "finally, if it is successful,use this bapi to commit
      CALL FUNCTION 'BAPI_PS_PRECOMMIT'
        TABLES
          et_return = lt_et_return2.

      READ TABLE lt_et_return2 INTO ls_et_return2 WITH KEY type  = 'E'.
      IF sy-subrc = 0.
        LOOP AT p_lt_output_copy INTO ls_output_copy WHERE message = ''."通过倒数的方式去更新内表,因为不能用key值去更新
          IF num_dec = 0.
            EXIT.
          ELSE.
            ls_output_copy-status = icon_led_red.
            ls_output_copy-message = '网络作业创建失败!'.
            CONCATENATE ls_output_copy-message ls_et_return2-message INTO ls_output_copy-message.
            MODIFY p_lt_output_copy FROM ls_output_copy.
            num_dec = num_dec - 1.
          ENDIF.
        ENDLOOP.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        EXIT.
      ENDIF.


      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.

      LOOP AT p_lt_output_copy INTO ls_output_copy WHERE aufnr = ''."通过倒数的方式去更新内表,因为不能用key值去更新
        IF num_dec = 0.
          EXIT.
        ELSE.
          ls_output_copy-status = icon_led_green.
          READ TABLE lt_message_table INTO ls_message_table INDEX 1.
          ls_output_copy-aufnr = l_network."P_L_NETWORK.
          ls_output_copy-message = '网络作业创建成功!'.
          MODIFY p_lt_output_copy FROM ls_output_copy.
          num_dec = num_dec - 1.
        ENDIF.
      ENDLOOP.




      "修改预留RESB表-自建字段-ZWLDJ的值
      " The bapi of BAPI_RESERVATION_CHANGE can not realize to modify the customer Z-field.
      " So,just direct to updata the Table-RESB by RESB~RSNUM and RESB~RSPOS.

      TYPES:BEGIN OF ty_rsnum,
              rsnum TYPE afko-rsnum,
            END OF ty_rsnum.
      DATA:lt_rsnum TYPE TABLE OF ty_rsnum,
           ls_rsnum LIKE LINE OF lt_rsnum.

      SELECT
         afko~aufnr,
         afko~rsnum
    INTO TABLE @DATA(lt_afko_resb)
    FROM afko
    WHERE afko~aufnr = @l_network.


      LOOP AT lt_afko_resb INTO DATA(ls_afko_resb).
        ls_rsnum-rsnum = ls_afko_resb-rsnum.
        APPEND ls_rsnum TO lt_rsnum.
      ENDLOOP.
      SORT lt_rsnum.
      DELETE ADJACENT DUPLICATES FROM lt_rsnum.

      IF lt_rsnum IS NOT INITIAL.
        SELECT *
          INTO TABLE @DATA(lt_resb)
          FROM resb
          FOR ALL ENTRIES IN @lt_rsnum
          WHERE rsnum = @lt_rsnum-rsnum.

        LOOP AT lt_resb INTO DATA(ls_resb).
          READ TABLE p_lt_output_temp2 INTO p_ls_output_temp2 INDEX sy-tabix.
          ls_resb-zprice = p_ls_output_temp2-zdj.
          MODIFY lt_resb FROM ls_resb.
          CLEAR:ls_resb,
                p_ls_output_temp2.
        ENDLOOP.
        UPDATE resb FROM TABLE lt_resb.
      ENDIF.



*          DATA:LT_RESERVATIONITEMS_CHANGED TYPE TABLE OF BAPI2093_RES_ITEM_CHANGE,
*               LS_RESERVATIONITEMS_CHANGED LIKE LINE OF LT_RESERVATIONITEMS_CHANGED,
*               LT_RESERVATIONITEMS_CHANGEDX TYPE TABLE OF BAPI2093_RES_ITEM_CHANGEX,
*               LS_RESERVATIONITEMS_CHANGEDX LIKE LINE OF LT_RESERVATIONITEMS_CHANGEDX,
*               RESERVATIONITEMS_NEW TYPE TABLE OF BAPI2093_RES_ITEM_NEW,
*               LT_RETURN_RESERVATIONITEMS TYPE TABLE OF BAPIRET2,
*               LT_EXTENSIONIN TYPE TABLE OF BAPIPAREX,
*               LS_EXTENSIONIN LIKE LINE OF LT_EXTENSIONIN.
*
*          DATA:LS_ZRESB_APPEND_ZWLJE LIKE ZRESB_APPEND_ZWLJE.

*
*            SELECT
*               AFKO~AUFNR,
*               RESB~RSNUM,
*               RESB~RSPOS
*          INTO TABLE @DATA(LT_AFKO_RESB)
*          FROM AFKO JOIN RESB ON AFKO~RSNUM = RESB~RSNUM
*          WHERE AFKO~AUFNR = @l_network.
*
*       LOOP AT LT_AFKO_RESB INTO DATA(LS_AFKO_RESB).
*         L_RSNUM = LS_AFKO_RESB-RSNUM.
*         "预留行
*         LS_RESERVATIONITEMS_CHANGED-RES_ITEM = LS_AFKO_RESB-RSPOS.
*         APPEND LS_RESERVATIONITEMS_CHANGED TO LT_RESERVATIONITEMS_CHANGED.
*
*       LS_RESERVATIONITEMS_CHANGEDX-RES_ITEM = LS_AFKO_RESB-RSPOS.
*       APPEND LS_RESERVATIONITEMS_CHANGEDX TO LT_RESERVATIONITEMS_CHANGEDX.
*
*         "增强字段赋值
*         READ TABLE P_lT_output_temp2 INTO P_lS_output_temp2 INDEX SY-TABIX.
*         LS_ZRESB_APPEND_ZWLJE-ZWLJE = P_lS_output_temp2-ZDJ.
*
*         LS_EXTENSIONIN-STRUCTURE = 'ZRESB_APPEND_ZWLJE'.
**         CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C
**               EXPORTING
**                 IM_VALUE               =  LS_ZRESB_APPEND_ZWLJE
**               IMPORTING
**                 EX_CONTAINER           =  LS_EXTENSIONIN-VALUEPART1
**               EXCEPTIONS
**                 ILLEGAL_PARAMETER_TYPE = 1
**                 OTHERS                 = 2.
*         LS_EXTENSIONIN-VALUEPART1 = LS_ZRESB_APPEND_ZWLJE.
*         SHIFT LS_EXTENSIONIN-VALUEPART1 LEFT DELETING LEADING SPACE.
*         APPEND LS_EXTENSIONIN TO LT_EXTENSIONIN.
*         CLEAR:LS_RESERVATIONITEMS_CHANGED,
*               LS_EXTENSIONIN.
*       ENDLOOP.
*
*
*      CALL FUNCTION 'BAPI_RESERVATION_CHANGE'
*        EXPORTING
*          RESERVATION                     = L_RSNUM
**         TESTRUN                         =
**         ATPCHECK                        =
*        TABLES
*          RESERVATIONITEMS_CHANGED        = LT_RESERVATIONITEMS_CHANGED
*          RESERVATIONITEMS_CHANGEDX       = LT_RESERVATIONITEMS_CHANGEDX
**         RESERVATIONITEMS_NEW            =
*          RETURN                          = LT_RETURN_RESERVATIONITEMS
*         EXTENSIONIN                      = LT_EXTENSIONIN
*                .
*      READ TABLE lt_et_return2 INTO ls_et_return2 WITH KEY type  = 'E'.
*      IF sy-subrc = 0.
*        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
*      ELSE.
*        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*          EXPORTING
*            wait = 'X'.
*      ENDIF.

    ENDIF.
  ENDIF.
ENDFORM.

需要注意的是调BAPI时候的有个先后顺序,否则会报错

?mid=&wid=51824&sid=&tid=8555&rid=LOADED&custom1=mp.csdn.net&custom2=%2Fpostedit%2F81451435&t=1570963360788

?mid=&wid=51824&sid=&tid=8555&rid=FINISHED&custom1=mp.csdn.net&t=1570963360789

posted @ 2021-11-25 10:15  linhuang  阅读(24)  评论(0编辑  收藏  举报  来源