Calculate Service Duration using Service Level Agreement Management (SLAM)

*"----------------------------------------------------------------------
*"Methoden Signatur:
*"  Importing
*"    IV_GUID TYPE CRMT_OBJECT_GUID
*"    IV_START TYPE TIMESTAMP
*"    IV_END TYPE TIMESTAMP
*"    IV_UNIT TYPE TIMEUNITDU
*"  Returning
*"     VALUE(RV_DURATION) TYPE TIMEDURA
*"----------------------------------------------------------------------
************************************************************************
* Method CALCULATE_SERVICE_DURATION                                    *
* Date   05.02.2015                                                    *
*                                                                      *
*----------------------------------------------------------------------*
* Calculate Service Duration.                                          *
* Please ensure, that a Service Profile is maintained.                 *
* Please ensure, that this Service Profile could be determined by SLAM.*
* If SLAM is not used, please hack method GET_SERVICE_WINDOW           *
* of class CL_AI_CRM_SERVICE to set a default service profile.         *
************************************************************************
METHOD calculate_service_duration.

*This solution was found during a little research analysis of report AI_CRM_PROCESS_SLA and class CL_AI_CRM_SLA_UTIL.
*Here I found class cl_ai_crm_service. This class is used at multiple places which was found using where used analyis.

  DATA:
    lo_service      TYPE REF TO cl_ai_crm_service,
    lv_start_date   TYPE dats,
    lv_start_time   TYPE tims,
    lv_end_date     TYPE dats,
    lv_end_time     TYPE tims.

*Ensure valid result.
  rv_duration = -1.

*Ensure valid time frame.
  CHECK iv_start <= iv_end AND iv_end < '99991231235959'.

*Get Service Object.
  CALL METHOD cl_ai_crm_service=>get_instance
    EXPORTING
      iv_header_guid = iv_guid
    RECEIVING
      rr_instance    = lo_service.

*Split Timestamps and convert in Application Server Time.
  CONVERT TIME STAMP iv_start TIME ZONE sy-zonlo
      INTO DATE lv_start_date TIME lv_start_time.

  CONVERT TIME STAMP iv_end TIME ZONE sy-zonlo
      INTO DATE lv_end_date TIME lv_end_time.

*Calculate Service Duration.
  CALL METHOD lo_service->calculate_service_duration
    EXPORTING
      iv_start_date    = lv_start_date
      iv_start_time    = lv_start_time
      iv_end_date      = lv_end_date
      iv_end_time      = lv_end_time
      iv_unit          = iv_unit
    RECEIVING
      rv_duration      = rv_duration
    EXCEPTIONS
      object_not_found = 1
      OTHERS           = 2.

ENDMETHOD.