Cerca nel blog

martedì 20 dicembre 2022

ABAP:Modifica del work center quando viene creato la schedulazione del programma di manutenzione IP10

 

Modifica del work center quando viene creato la schedulazione del programma di manutenzione IP10


Abbiamo la necessita di modificare il work center delle operazioni dell'ordine che viene creato quando viene creato la schedulazione del programma di manutenzione Tcode IP10.

Nella user exit EXIT_SAPLMCI1_001, leggiamo la tabella interna I_MCIPMB, per capire il nr dell'ordine che si sta creando.

 READ TABLE i_mcipmb ASSIGNING FIELD-SYMBOL(<lfs_mcipmb>) INDEX 1.

lv_aufnr = <lfs_mcipmb>-aufnr.

 lv_aufnr2  = |{ lv_aufnr     ALPHA = OUT } |.

Valorizzo il nome del job che creeremo.

CONCATENATE 'ZJOB_UGP_WORKORDER_' lv_aufnr2 INTO v_jobname.

Valorizzo il parametro che passeremo al programma che verrà chiamato dal job.

      FREE: lt_rsparams[].

      CLEAR ls_rspar.

      ls_rspar-selname = 'P_AUFNR'.

      ls_rspar-kind = 'S'.

      ls_rspar-sign = 'I'.

      ls_rspar-option = 'EQ'.

      ls_rspar-low = lv_aufnr.

      APPEND ls_rspar TO lt_rsparams.

Richiamo un FM in una nuova sessione. Il FM deve essere creato in modo da essere richiamato dall'esterno

      CALL FUNCTION 'ZPM_JOB_CREATE'

       STARTING NEW TASK 'TASK'

        EXPORTING

          i_aufnr   = lv_aufnr

          jobname   = v_jobname

          progname  = 'ZUGP_WORKORDER'

          strtimmed = abap_true

        TABLES

          rsparams  = lt_rsparams.

Il FM ha i seguenti parametri:


Nel FM, controlliamo che non esiste già un job con lo stesso nome. Poi verifichiamo che l'ordine sia stato creato e presente in tabella AFKO, e poi creiamo il job che richiamerà il programma ZUGP_WORKORDER.

FUNCTION zpm_job_create_ugp_workorder.

  DATA: vn_jobcount   LIKE tbtcjob-jobcount,

        lv_sdlstrtdt  TYPE tbtcjob-sdlstrtdt,

        lv_sdlstrttm  TYPE tbtcjob-sdlstrttm,

        lv_timestamp  TYPE string,

        ls_msg        TYPE bal_s_msg,

        lv_extnumber  TYPE balnrext,

        lv_errore     TYPE boolean,

        lv_log_handle TYPE balloghndl,

        lv_finished   LIKE tbtcv-fin.


*I check if the job already exists, in this case I exit the FM

  SELECT SINGLE @abap_true

           FROM tbtco

           INTO @DATA(gv_job_exists)

          WHERE jobname = @jobname.


  IF gv_job_exists = abap_true.

    EXIT.

  ENDIF.


  DO 3 TIMES.

    SELECT SINGLE @abap_true

          FROM afko

          INTO @DATA(gv_orderexists)

         WHERE aufnr = @i_aufnr.


    IF gv_orderexists = abap_false.

      WAIT UP TO 3 SECONDS.

    ELSE.

      EXIT.

    ENDIF.

  ENDDO.


  IF gv_orderexists = abap_false.

    EXIT.

  ENDIF.

  IF user IS INITIAL.

    user = sy-uname.

  ENDIF.


    CALL FUNCTION 'JOB_OPEN'

      EXPORTING

        jobname          = jobname

        jobclass         = 'A' "jobclass

      IMPORTING

        jobcount         = vn_jobcount

      EXCEPTIONS

        cant_create_job  = 1

        invalid_job_data = 2

        jobname_missing  = 3

        OTHERS           = 4.

    IF sy-subrc = 0.

      SUBMIT (progname)

      USER user

      VIA JOB jobname NUMBER vn_jobcount

      WITH SELECTION-TABLE rsparams

      AND RETURN.


      CALL FUNCTION 'JOB_CLOSE'

        EXPORTING

          jobcount             = vn_jobcount

          jobname              = jobname

          strtimmed            = abap_true " start immediately

        EXCEPTIONS

          cant_start_immediate = 1

          invalid_startdate    = 2

          jobname_missing      = 3

          job_close_failed     = 4

          job_nosteps          = 5

          job_notex            = 6

          lock_failed          = 7

          invalid_target       = 8

          OTHERS               = 9.

  ENDIF.

ENDFUNCTION.


Nel  programma ZUGP_WORKORDER:

Leggiamo i dati dell’ordine richiamando il FM BAPI_ALM_ORDER_GET_DETAIL

Se sono presenti le operazioni, aggiorniamo il work center delle operazioni con il FM BAPI_ALM_ORDER_MAINTAIN

REPORT zpm_ugp_workorder.

START-OF-SELECTION.

    PERFORM get_order_get_detail   USING lv_aufnr

                                CHANGING lv_modify_ok.

    PERFORM change_operation  USING lv_aufnr lv_modify_ok.

END-OF-SELECTION.


FORM get_order_get_detail  USING  pv_aufnr CHANGING pv_modify_ok.

  DATA: lv_errore      TYPE boolean,

        lv_arbpl       TYPE arbpl,

        lwa_operations TYPE bapi_alm_order_operation_e,

        lt_return      TYPE STANDARD TABLE OF bapiret2,

        lt_olist       TYPE STANDARD TABLE OF

                            bapi_alm_order_objectlist,

        lt_rules       TYPE STANDARD TABLE OF

                            bapi_alm_order_srule_e.


  CLEAR: pv_modify_ok, lv_errore, lv_arbpl, lwa_operations,

         gv_header, gv_srvdata, gv_reforder_item.


  FREE: lt_return[], lt_rules[], lt_olist[].

  CALL FUNCTION 'BAPI_ALM_ORDER_GET_DETAIL'

    EXPORTING

      number           = pv_aufnr

    IMPORTING

      es_header        = gv_header

      es_srvdata       = gv_srvdata

      es_reforder_item = gv_reforder_item

    TABLES

      et_operations    = gt_operations

      et_srules        = lt_rules

      et_olist         = lt_olist

      return           = lt_return.


  IF   gt_operations[] IS NOT INITIAL.

       lv_arbpl = '999999'.

      lwa_operations-work_cntr = lv_arbpl.

      MODIFY gt_operations  FROM lwa_operations TRANSPORTING work_cntr

       WHERE work_cntr NE lv_arbpl.

  ENDIF.

ENDFORM.


FORM change_operation  USING    pv_aufnr pv_modify_ok.


  DATA: lv_aufnr        TYPE aufnr,

        lt_methods      TYPE bapi_alm_order_method_t,

        lt_return       TYPE STANDARD TABLE OF bapiret2,

        lt_operation    TYPE bapi_alm_order_operation_t,

        lt_operation_up TYPE bapi_alm_order_operation_ut,

        lt_header       TYPE bapi_alm_order_header_t,

        lwa_header      TYPE bapi_alm_order_headers_i,

        lt_header_up    TYPE  bapi_alm_order_headers_i_ut,

        lwa_header_up   TYPE  bapi_alm_order_headers_up,

        lv_errore       TYPE boolean.


  CHECK pv_modify_ok = abap_true.

  lv_aufnr = pv_aufnr.

  FREE: lt_methods[], lt_operation_up[], lt_operation[],

        lt_header[], lt_header_up[].


  CLEAR: lwa_header, lv_errore.

  lwa_header-orderid = gv_header-orderid.

  lwa_header-order_type = gv_header-order_type.

  APPEND lwa_header TO lt_header.


  CLEAR: lwa_header_up.

  lwa_header_up-orderid = gv_header-orderid.

  APPEND lwa_header_up TO lt_header_up.


  lv_aufnr  = |{ lv_aufnr ALPHA = IN  } |.


  PERFORM fill_tables     USING lv_aufnr

                      CHANGING lt_methods[]

                               lt_operation[]

                               lt_operation_up[].


  CALL FUNCTION 'BAPI_ALM_ORDER_MAINTAIN'

    TABLES

      it_methods      = lt_methods

      it_header       = lt_header

      it_header_up    = lt_header_up

      it_operation    = lt_operation

      it_operation_up = lt_operation_up

      return          = lt_return.


  IF lv_errore = abap_true.

    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

  ELSE.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

      EXPORTING

        wait = 'X'.

  ENDIF.

ENDFORM.


FORM fill_tables     USING lv_aufnr

                  CHANGING pt_methods       TYPE bapi_alm_order_method_t

                           pt_operation     TYPE bapi_alm_order_operation_t

                           pt_operation_up  TYPE bapi_alm_order_operation_ut.


  DATA: lwa_methods      TYPE bapi_alm_order_method,

        lwa_operation    TYPE bapi_alm_order_operation,

        lwa_operation_up TYPE bapi_alm_order_operation_up,

        lv_nr_item       TYPE ifrefnum,

        lv_nr_object(4)  TYPE n,

        lv_nr_objectc(4) TYPE c,

        lv_objectkey     TYPE objidext.


  CLEAR: lv_nr_item, lv_objectkey, lv_nr_object.

  LOOP AT gt_operations ASSIGNING FIELD-SYMBOL(<lfs_oper>).

    lv_nr_item = lv_nr_item + 1.

    lv_nr_object = lv_nr_object + 10.

    lv_nr_objectc = lv_nr_object.

    lv_objectkey = |{ lv_aufnr }{ lv_nr_object }|.


*******Fill structure methods

    CLEAR: lwa_methods.

    lwa_methods-refnumber   = lv_nr_item.

    lwa_methods-objecttype  = gc_obj_operation.

    lwa_methods-method      = gc_meth_change.

    lwa_methods-objectkey   = lv_objectkey.

    APPEND lwa_methods TO pt_methods.


*******Fill structure operation

    CLEAR: lwa_operation.

    lwa_operation-activity   = lv_nr_objectc.

    lwa_operation-work_cntr  = <lfs_oper>-work_cntr.

    APPEND lwa_operation TO pt_operation.


*******Fill structure operation_up

    CLEAR: lwa_operation_up.

    lwa_operation_up-activity   = abap_true.

    lwa_operation_up-work_cntr  = abap_true.

    APPEND lwa_operation_up TO pt_operation_up.

  ENDLOOP.


*******Fill structure methods FINAL

  CLEAR: lwa_methods.

  lwa_methods-refnumber = lv_nr_item.

  lwa_methods-method    = gc_meth_save.

  APPEND lwa_methods TO pt_methods.

ENDFORM.