SAP CRM

From SapWiki
Revision as of 19:04, 28 June 2022 by WikiSysop (talk | contribs) (Created page with "==CRM_ORDER_MAINTAIN== <nowiki> FUNCTION ZCRM_ORDER_TIMESTAMP. *"---------------------------------------------------------------------- *"*"Interfase local *" IMPORTING *"...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

CRM_ORDER_MAINTAIN

FUNCTION ZCRM_ORDER_TIMESTAMP.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(IV_GUID) TYPE  CRMT_OBJECT_GUID
*"     VALUE(IV_APPTYPE) TYPE  CRMT_APPTYPE
*"     VALUE(IV_TIMESTAMP_TO) TYPE  CRMT_TIMESTAMP OPTIONAL
*"     VALUE(IV_TIMESTAMP_FROM) TYPE  CRMT_TIMESTAMP OPTIONAL
*"     VALUE(IV_COMMIT_FLAG) TYPE  CHAR1 OPTIONAL
*"  EXCEPTIONS
*"      UPDATE_FAILED
*"----------------------------------------------------------------------


*     Requerimiento CRA-08-07. Fecha real

* Guid para trabajar con la función de lectura.
  data: WV_GUID            like          IV_GUID,
        WT_GUID            type          CRMT_OBJECT_GUID_TAB.

* Cabecera de administración del doc. vecino.
  data: WT_ORDERADM_H      type          CRMT_ORDERADM_H_WRKT,
        WS_ORDERADM_H      type line of  CRMT_ORDERADM_H_WRKT.

* Status del documento vecino.
  data: WT_STATUS          type          CRMT_STATUS_WRKT,
        WS_STATUS          type line of  CRMT_STATUS_WRKT.

* Apuntes
  data: WT_APPOINTMENT      type         CRMT_APPOINTMENT_WRKT,
        WS_APPOINTMENT      type line of CRMT_APPOINTMENT_WRKT.

* Objetos pendientes de grabar
  data: WT_OBJECTS_TO_SAVE  type         CRMT_OBJECT_NAME_TAB,
        WS_OBJECTS_TO_SAVE  type line of CRMT_OBJECT_NAME_TAB.

* Información para la modificación global.
  data: WT_M_INPUT_FIELDS     type         CRMT_INPUT_FIELD_TAB,
        WS_M_INPUT_FIELDS     type line of CRMT_INPUT_FIELD_TAB,
        WT_M_INPUT_FIELD_NAMES  type       CRMT_INPUT_FIELD_NAMES_TAB,
        WS_M_INPUT_FIELD_NAMES  type line of CRMT_INPUT_FIELD_NAMES_TAB,
        WT_M_ACTIVITY_H       type         CRMT_ACTIVITY_H_COMT,
        WS_M_ACTIVITY_H       type line of CRMT_ACTIVITY_H_COMT,
        WT_M_APPOINTMENT      type         CRMT_APPOINTMENT_COMT,
        WS_M_APPOINTMENT      type line of CRMT_APPOINTMENT_COMT.

* Estado modificado.
  data: WV_CHANGED.

* Apunte IV_APPTYPE, clave para modificación.
  data: WS_LOGICAL_DATE_KEY type         CRMT_DATE_LOGICAL_DATE_KEY.

  data: lv_TZNZONESYS type TZNZONESYS.

clear: WS_M_APPOINTMENT, WS_M_ACTIVITY_H, WS_M_INPUT_FIELD_NAMES,
       WS_M_INPUT_FIELDS, WS_OBJECTS_TO_SAVE, WS_APPOINTMENT,
       WS_STATUS, WS_ORDERADM_H, wv_guid.
clear: WT_M_APPOINTMENT, WT_M_ACTIVITY_H, WT_M_INPUT_FIELD_NAMES,
       WT_M_INPUT_FIELDS, WT_OBJECTS_TO_SAVE, WT_APPOINTMENT,
       WT_STATUS, WT_ORDERADM_H, WT_GUID.
refresh: WT_M_APPOINTMENT, WT_M_ACTIVITY_H, WT_M_INPUT_FIELD_NAMES,
       WT_M_INPUT_FIELDS, WT_OBJECTS_TO_SAVE, WT_APPOINTMENT,
       WT_STATUS, WT_ORDERADM_H, WT_GUID.

  check not IV_TIMESTAMP_TO is initial or
        not IV_TIMESTAMP_FROM is initial.

* Recupera los status de los vecinos aceptados.
  append IV_GUID to WT_GUID.
  call function 'CRM_ORDER_READ'
    EXPORTING
      IT_HEADER_GUID       = WT_GUID
    IMPORTING
      ET_ORDERADM_H        = WT_ORDERADM_H
      ET_STATUS            = WT_STATUS
      ET_APPOINTMENT       = WT_APPOINTMENT
    EXCEPTIONS
      DOCUMENT_NOT_FOUND   = 1
      ERROR_OCCURRED       = 2
      DOCUMENT_LOCKED      = 3
      NO_CHANGE_AUTHORITY  = 4
      NO_DISPLAY_AUTHORITY = 5
      NO_CHANGE_ALLOWED    = 6
      others               = 7.
  if SY-SUBRC <> 0.
    message id SY-MSGID type SY-MSGTY number SY-MSGNO
            with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  endif.

** Verifica el cambio de estado.
*  call function 'CRM_ORDER_GET_OBJECTS_TO_SAVE'
*    exporting
*      IV_REF_GUID        = IV_GUID
*      IV_REF_KIND        = 'A'
*    importing
*      ET_OBJECTS_TO_SAVE = WT_OBJECTS_TO_SAVE.
*
*  clear WV_CHANGED.
*  loop at WT_OBJECTS_TO_SAVE into WS_OBJECTS_TO_SAVE.
*    if WS_OBJECTS_TO_SAVE = 'STATUS'.
*      loop at WT_STATUS into WS_STATUS.
*        if WS_STATUS-STATUS = 'I1005'.
*          WV_CHANGED = 'X'.
*          exit.
*        endif.
*      endloop.
*      if WV_CHANGED = SPACE.
*        exit.
*      endif.
*    endif.
*  endloop.
*
** El resto del código solo tiene sentido si se ha cambiado el
** status a cerrado.
*  check WV_CHANGED = 'X'.

  read table WT_APPOINTMENT into WS_APPOINTMENT
    with key APPT_TYPE = IV_APPTYPE.

*  check SY-SUBRC = 0.

  read table WT_ORDERADM_H into WS_ORDERADM_H index 1.

* Cambia las fechas del apunte.
* ADVERTENCIA: Cambiar datos por datos del systema!!!!

* Zona horaria del sistema
*  select single TZONESYS
*    from TTZCU
*    into lv_TZNZONESYS.
*clear lv_TZNZONESYS.

  if not IV_TIMESTAMP_FROM is initial.
    convert time stamp IV_TIMESTAMP_FROM
            time zone  lv_TZNZONESYS
            into date  WS_APPOINTMENT-DATE_FROM
                 time  WS_APPOINTMENT-TIME_FROM.

    convert date       WS_APPOINTMENT-DATE_FROM
            time       WS_APPOINTMENT-TIME_FROM
       into time stamp WS_APPOINTMENT-TIMESTAMP_FROM
            time zone  WS_APPOINTMENT-TIMEZONE_FROM.
  endif.

  if not IV_TIMESTAMP_TO is initial.
    convert time stamp IV_TIMESTAMP_TO
            time zone  lv_TZNZONESYS
            into date  WS_APPOINTMENT-DATE_TO
                 time  WS_APPOINTMENT-TIME_TO.

    convert date       WS_APPOINTMENT-DATE_TO
            time       WS_APPOINTMENT-TIME_TO
       into time stamp WS_APPOINTMENT-TIMESTAMP_TO
            time zone  WS_APPOINTMENT-TIMEZONE_TO.
  endif.

* Actualiza información de modificación para el apunte.
  refresh WT_M_INPUT_FIELD_NAMES.
  clear WS_M_INPUT_FIELDS.
* Prepara la clave lógica del apunte ORDERPLANNED.
  WS_LOGICAL_DATE_KEY-APPT_TYPE = IV_APPTYPE.
  clear WS_LOGICAL_DATE_KEY-IS_DURATION.
* Clave del apunte a modificar.
  WS_M_APPOINTMENT-REF_GUID       = IV_GUID.
  WS_M_APPOINTMENT-REF_KIND       = 'A'.
  WS_M_APPOINTMENT-APPT_TYPE      = WS_LOGICAL_DATE_KEY-APPT_TYPE.
  WS_M_APPOINTMENT-IS_DURATION    = WS_LOGICAL_DATE_KEY-IS_DURATION.
* Cambia los valores del apunte.
  WS_M_APPOINTMENT-TIMESTAMP_TO   = WS_APPOINTMENT-TIMESTAMP_TO.
  WS_M_APPOINTMENT-TIMESTAMP_FROM = WS_APPOINTMENT-TIMESTAMP_FROM.
  insert WS_M_APPOINTMENT into table WT_M_APPOINTMENT.
  WS_M_INPUT_FIELDS-REF_GUID          = IV_GUID.
  WS_M_INPUT_FIELDS-REF_KIND          = 'A'.
  WS_M_INPUT_FIELDS-OBJECTNAME        = 'APPOINTMENT'.
  WS_M_INPUT_FIELDS-LOGICAL_KEY       = WS_LOGICAL_DATE_KEY.
* Indica los campos modificados.

  if not IV_TIMESTAMP_TO is initial.
    WS_M_INPUT_FIELD_NAMES-FIELDNAME    = 'TIMESTAMP_TO'.
    WS_M_INPUT_FIELD_NAMES-CHANGEABLE   = 'X'.
    insert WS_M_INPUT_FIELD_NAMES into table WT_M_INPUT_FIELD_NAMES.
  endif.

  if not IV_TIMESTAMP_FROM is initial.
    WS_M_INPUT_FIELD_NAMES-FIELDNAME    = 'TIMESTAMP_FROM'.
    insert WS_M_INPUT_FIELD_NAMES into table WT_M_INPUT_FIELD_NAMES.
  endif.

* Combina los campos modificados con la clave del apunte.
  WS_M_INPUT_FIELDS-FIELD_NAMES = WT_M_INPUT_FIELD_NAMES.
  insert WS_M_INPUT_FIELDS into table WT_M_INPUT_FIELDS.

  call function 'CRM_ORDER_MAINTAIN'
    EXPORTING
      IT_ACTIVITY_H     = WT_M_ACTIVITY_H
      IT_APPOINTMENT    = WT_M_APPOINTMENT
    CHANGING
      CT_INPUT_FIELDS   = WT_M_INPUT_FIELDS
    EXCEPTIONS
      ERROR_OCCURRED    = 1
      DOCUMENT_LOCKED   = 2
      NO_CHANGE_ALLOWED = 3
      NO_AUTHORITY      = 4
      others            = 5.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      raise update_failed.
    ENDIF.

  if IV_COMMIT_FLAG = 'X'.
    CALL FUNCTION 'ZCRM_ORDER_SAVE_COMMIT'
      EXPORTING
        IV_GUID        = IV_GUID
      EXCEPTIONS
        UPDATE_FAILED  = 1
        others         = 2.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      raise update_failed.
    ENDIF.

  endif.


endfunction.


FUNCTION ZCRM_ORDER_SAVE_COMMIT.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(IV_GUID) TYPE  CRMT_OBJECT_GUID
*"  EXCEPTIONS
*"      UPDATE_FAILED
*"----------------------------------------------------------------------
  data: WV_GUID            like          IV_GUID,
        WT_GUID            type          CRMT_OBJECT_GUID_TAB.

    append IV_GUID to WT_GUID.
    CALL FUNCTION 'CRM_ORDER_SAVE'
      EXPORTING
        IT_OBJECTS_TO_SAVE         = WT_GUID
*   IV_UPDATE_TASK_LOCAL       = FALSE
*   IV_SAVE_FRAME_LOG          = FALSE
*   IV_NO_BDOC_SEND            = FALSE
* IMPORTING
*   ET_SAVED_OBJECTS           =
*   ET_EXCEPTION               =
*   ET_OBJECTS_NOT_SAVED       =
* CHANGING
*   CV_LOG_HANDLE              =
     EXCEPTIONS
       DOCUMENT_NOT_SAVED         = 1
       OTHERS                     = 2
              .
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      raise update_failed.
    ENDIF.

    CALL FUNCTION 'CRM_ORDER_DEQUEUE'
      EXPORTING
        IV_GUID            = IV_GUID
*   IV_COLLECT         = ' '
*   IV_FROM_INIT       = FALSE
              .

    commit work and wait.

ENDFUNCTION.