P_MntrWrkCtrOrderOperations

DDL: P_MNTRWRKCTRORDEROPERATIONS SQL: PMWCORDOP Type: view COMPOSITE

P_MntrWrkCtrOrderOperations is a Composite CDS View in SAP S/4HANA. It reads from 4 data sources (I_CapacityRequirementItemCap, I_CapacityRequirementItemCap, P_ProcCapReqOpDurn, P_ProcCapReqOpDurn) and exposes 53 fields with key fields OrderID, CapacityRequirementItem, CapacityRqmtItemCapacity, OrderCategory. It has 1 association to related views.

Data Sources (4)

SourceAliasJoin Type
I_CapacityRequirementItemCap CapRqmtItmCap from
I_CapacityRequirementItemCap CapRqmtItmCap union_all
P_ProcCapReqOpDurn ProcOpDurn left_outer
P_ProcCapReqOpDurn ProcOpDurn left_outer

Associations (1)

CardinalityTargetAliasCondition
[1..1] I_StatusObjectStatus _StatusConfirmed CapRqmtItmCap.OrderObjectInternalID = _StatusConfirmed.StatusObject and _StatusConfirmed.StatusCode = 'I0009' and _StatusConfirmed.StatusIsActive = 'X'

Annotations (7)

NameValueLevelField
AbapCatalog.sqlViewName PMWCORDOP view
AbapCatalog.compiler.compareFilter true view
AccessControl.authorizationCheck #NOT_REQUIRED view
VDM.viewType #COMPOSITE view
VDM.private true view
ClientHandling.algorithm #SESSION_VARIABLE view
AbapCatalog.preserveKey true view

Fields (53)

KeyFieldSource TableSource FieldDescription
KEY OrderID
KEY CapacityRequirementItem I_CapacityRequirementItemCap CapacityRequirementItem
KEY CapacityRqmtItemCapacity I_CapacityRequirementItemCap CapacityRqmtItemCapacity
KEY OrderCategory
Operation
Material
Plant
MRPArea
MRPController
numc15astimestampendasLastChangeDateTime
CapacityRequirement I_CapacityRequirementItemCap CapacityRequirement
WorkCenterInternalID
WorkCenter
WorkCenterCategoryCode
WorkCenterText
WorkCenterTypeCode
CapacityInternalID I_CapacityRequirementItemCap CapacityInternalID
CapacityCategoryCode
WorkCenterResponsible
CapacityResponsiblePlanner
OrderInternalID I_CapacityRequirementItemCap OrderInternalID
ManufacturingOrder
PlannedOrder
ProductionVersion
ProductionLine
ScheduledBasicStartDate _Order ScheduledBasicStartDate
ScheduledBasicStartTime _Order ScheduledBasicStartTime
ScheduledBasicEndDate _Order ScheduledBasicEndDate
ScheduledBasicEndTime _Order ScheduledBasicEndTime
OperationLatestStartDate I_CapacityRequirementItemCap OperationLatestStartDate
OperationLatestStartTime I_CapacityRequirementItemCap OperationLatestStartTime
OperationLatestEndDate I_CapacityRequirementItemCap OperationLatestEndDate
OperationLatestEndTime I_CapacityRequirementItemCap OperationLatestEndTime
OpLtstSchedldProcgStrtDte I_CapacityRequirementItemCap LtstSchedldProcStrtDte
OpLtstSchedldProcgStrtTme I_CapacityRequirementItemCap LtstSchedldProcStrtTime
OpLtstSchedldTrdwnStrtDte I_CapacityRequirementItemCap LtstSchedldTrdwnStrtDte
OpLtstSchedldTrdwnStrtTme I_CapacityRequirementItemCap LtstSchedldTrdwnStrtTime
RequirementDate
OrderPlannedTotalQty
BaseUnit
BOOExplosionDate
BOMExplosionDate
PlndOrderPlannedStartDate
PlndOrderPlannedEndDate
OrderObjectInternalID I_CapacityRequirementItemCap OrderObjectInternalID
OperationSetupGroup
OperationSetupGroupCategory
OperationSetupType
HorizonStartDatetime
HorizonStartDate
clientNULLendasPlantCurrentDate
clientNULLendasPlantCurrentTime
PlantTimeZone
@AbapCatalog.sqlViewName: 'PMWCORDOP'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@VDM.viewType: #COMPOSITE
@VDM.private: true
@ClientHandling.algorithm: #SESSION_VARIABLE
@AbapCatalog.preserveKey: true

define view P_MntrWrkCtrOrderOperations

  as select from    I_CapacityRequirementItemCap as CapRqmtItmCap

  //Obtain all operations from KBED. Not just the first operation.

    left outer join P_ProcCapReqOpDurn           as ProcOpDurn on  CapRqmtItmCap.CapacityRequirement      = ProcOpDurn.CapacityRequirement
                                                               and CapRqmtItmCap.CapacityRequirementItem  = ProcOpDurn.SuperiorOperationInternalID
                                                               and CapRqmtItmCap.CapacityRqmtItemCapacity = ProcOpDurn.CapacityRqmtItemCapacity
                                                               and CapRqmtItmCap.OperationIsPhase         = '1'

  association [1..1] to I_StatusObjectStatus as _StatusConfirmed on  CapRqmtItmCap.OrderObjectInternalID = _StatusConfirmed.StatusObject
                                                                 and _StatusConfirmed.StatusCode         = 'I0009'
                                                                 and _StatusConfirmed.StatusIsActive     = 'X'

{

  key cast( CapRqmtItmCap.OrderID as pph_cfs_ordernumber )                  as OrderID,

  key CapRqmtItmCap.CapacityRequirementItem                                 as CapacityRequirementItem,

  key CapRqmtItmCap.CapacityRqmtItemCapacity,

  key cast(CapRqmtItmCap._Order.OrderCategory as pph_cfs_sch_ordercategory) as OrderCategory,

      cast( CapRqmtItmCap.Operation as vornr)                               as Operation,

      cast( CapRqmtItmCap._Order.Material as productnumber)                 as Material,

      CapRqmtItmCap._WorkCenter.Plant                                       as Plant,
////performance changes as this fields are unused      

//      case 

//      when CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone is null

//        then

//         tstmp_to_dats( tstmp_current_utctimestamp(),

//                         abap_system_timezone($session.client, 'NULL'),

//                         $session.client,

//                         'NULL' )

//

//        else

//         tstmp_to_dats( tstmp_current_utctimestamp(),

//                         CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone,

//                         $session.client,

//                         'NULL' )

//        end                                                                 as PlantCurrentDate,

//      case

//        when CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone is null

//        then

//           tstmp_to_tims( tstmp_current_utctimestamp(),

//                         abap_system_timezone($session.client, 'NULL'),

//                         $session.client,

//                         'NULL' )

//        else

//        tstmp_to_tims( tstmp_current_utctimestamp(),

//                         CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone,

//                         $session.client,

//                         'NULL' )

//

//        end                                                                 as PlantCurrentTime,


      cast( _Order._OrderMainItem.MRPArea as berid)                         as MRPArea,

      cast( CapRqmtItmCap._Order.MRPController as dispo)                    as MRPController,

      case CapRqmtItmCap._Order.LastChangeDate
      when '00000000'
      then cast(cast( concat( CapRqmtItmCap._Order.CreationDate, CapRqmtItmCap._Order.CreationTime)as abap.numc(15)) as timestamp)
      else cast(cast( concat( CapRqmtItmCap._Order.LastChangeDate, CapRqmtItmCap._Order.LastChangeTime)as abap.numc(15)) as timestamp)
      end                                                                   as LastChangeDateTime,

      CapRqmtItmCap.CapacityRequirement                                     as CapacityRequirement,

      CapRqmtItmCap._WorkCenter.WorkCenterInternalID                        as WorkCenterInternalID,

      CapRqmtItmCap._WorkCenter.WorkCenter                                  as WorkCenter,

      CapRqmtItmCap._WorkCenter.WorkCenterCategoryCode                      as WorkCenterCategoryCode,

      CapRqmtItmCap._WorkCenter._Text[1:Language=$session.system_language].WorkCenterText,

      cast(CapRqmtItmCap.WorkCenterTypeCode as cr_objty)                    as WorkCenterTypeCode,

      CapRqmtItmCap.CapacityInternalID,

      CapRqmtItmCap._Capacity.CapacityCategoryCode,

      CapRqmtItmCap._WorkCenter.WorkCenterResponsible                       as WorkCenterResponsible,

      CapRqmtItmCap._Capacity.CapacityResponsiblePlanner                    as CapacityResponsiblePlanner,

      CapRqmtItmCap.OrderInternalID                                         as OrderInternalID,

      cast(CapRqmtItmCap.OrderID as aufnr)                                  as ManufacturingOrder,

      cast(CapRqmtItmCap.PlannedOrder as plnum)                             as PlannedOrder,

      cast( _Order._OrderMainItem.ProductionVersion as verid)               as ProductionVersion,

      _Order._OrderMainItem._ProductionVersion.ProductionLine               as ProductionLine,

//       //Mapping Repetitive manufacturing to Discrete Industry

//      cast(

//      case

//      when _Order._OrderMainItem._ProductionVersion._BillOfOperationsType.BillOfOperationsApplication = 'R'

//      then cast ('P' as plnaw)

//      else cast (_Order._OrderMainItem._ProductionVersion._BillOfOperationsType.BillOfOperationsApplication as plnaw)

//      end as plnaw preserving type )                                        as IndustryType,

       // Mapping Repetetive manufacturing to Discrete Industry

        cast(
        case 
        when _Order._OrderMainItem._ProductionVersion._BillOfOperationsType.BillOfOperationsApplication = 'R' or
        (CapRqmtItmCap._Order.OrderCategory = '30' and CapRqmtItmCap._WorkCenter.WorkCenterCategoryCode <> '0008')
        then cast ('P' as plnaw)
        when (CapRqmtItmCap._Order.OrderCategory = '30' and CapRqmtItmCap._WorkCenter.WorkCenterCategoryCode = '0008')
        then cast ('C' as plnaw)
        else cast (_Order._OrderMainItem._ProductionVersion._BillOfOperationsType.BillOfOperationsApplication as plnaw)
        end as plnaw preserving type ) as IndustryType,

      case
      when _OrderOperation.OpDispatchedExecStartDate = '00000000'
      then cast('DALL' as pph_cfs_sch_status)
      else cast('DSPT' as pph_cfs_sch_status)
      end                                                                   as OperationPlanningStatusCode,

      _Order.ScheduledBasicStartDate                                        as ScheduledBasicStartDate,

      _Order.ScheduledBasicStartTime                                        as ScheduledBasicStartTime,

      _Order.ScheduledBasicEndDate                                          as ScheduledBasicEndDate,

      _Order.ScheduledBasicEndTime                                          as ScheduledBasicEndTime,

      CapRqmtItmCap.OperationLatestStartDate                                as OperationLatestStartDate,

      CapRqmtItmCap.OperationLatestStartTime                                as OperationLatestStartTime,

//When final confirmation is executed, remaining capacity requirement becomes ZERO.

//Hence scheduled capacity requirement does not change even after rescheduling.

//To handle this we need to pick Actual Execution Start/End date/time from OrderOperation.

      case 
      when _StatusConfirmed.StatusCode is not null
      then
      cast(cast(concat(CapRqmtItmCap._OrderOperation.OpActualExecutionStartDate,CapRqmtItmCap._OrderOperation.OpActualExecutionStartTime)as abap.numc(15)) as timestamp)
      when CapRqmtItmCap.OperationLatestStartTime >= '240000'
      then cast(cast(concat(dats_add_days(CapRqmtItmCap.OperationLatestStartDate,1,'NULL'),'000000')as abap.numc(15)) as timestamp)
      else cast(cast(concat(CapRqmtItmCap.OperationLatestStartDate,CapRqmtItmCap.OperationLatestStartTime)as abap.numc(15)) as timestamp)      
      end                                                                  as LtstSchedldExecStrtDteTme,
      
      
//      case

//      when CapRqmtItmCap.OperationLatestStartTime >= '240000'

////      then cast(cast(concat(dats_add_days(CapRqmtItmCap.OperationLatestStartDate,1,'NULL'),'000000')as abap.numc(15)) as timestamp)

//      then cast (DATS_ADD_DAYS(CapRqmtItmCap.OperationLatestStartDate,-90,'FAIL') as sydatum ) 

//      else cast(cast(concat(CapRqmtItmCap.OperationLatestStartDate,CapRqmtItmCap.OperationLatestStartTime)as abap.numc(15)) as timestamp)

//      end                                                                   as LtstSchedldExecStrtDate,

      

      CapRqmtItmCap.OperationLatestEndDate                                  as OperationLatestEndDate,

      CapRqmtItmCap.OperationLatestEndTime                                  as OperationLatestEndTime,

//When final confirmation is executed, remaining capacity requirement becomes ZERO.

//Hence scheduled capacity requirement does not change even after rescheduling.

//To handle this we need to pick Actual Execution Start/End date/time from OrderOperation.

      case 
      when _StatusConfirmed.StatusCode is not null
      then
      cast(cast(concat(CapRqmtItmCap._OrderOperation.OpActualExecutionEndDate,CapRqmtItmCap._OrderOperation.OpActualExecutionEndTime)as abap.numc(15)) as timestamp)
      when CapRqmtItmCap.OperationLatestEndTime >= '240000'
      then cast(cast(concat(dats_add_days(CapRqmtItmCap.OperationLatestEndDate,1,'NULL'),'000000')as abap.numc(15)) as timestamp)
      else cast(cast(concat(CapRqmtItmCap.OperationLatestEndDate,CapRqmtItmCap.OperationLatestEndTime)as abap.numc(15)) as timestamp)
      end                                                                   as LtstSchedldExecEndDteTme,

      CapRqmtItmCap.LtstSchedldProcStrtDte                                  as OpLtstSchedldProcgStrtDte,

      CapRqmtItmCap.LtstSchedldProcStrtTime                                 as OpLtstSchedldProcgStrtTme,

      CapRqmtItmCap.LtstSchedldTrdwnStrtDte                                 as OpLtstSchedldTrdwnStrtDte,

      CapRqmtItmCap.LtstSchedldTrdwnStrtTime                                as OpLtstSchedldTrdwnStrtTme,

      CapRqmtItmCap._Order.PlannedEndDate                                   as RequirementDate,

      cast ( CapRqmtItmCap._Order.OrderPlannedTotalQty as mgvrg )           as OrderPlannedTotalQty,

      cast (CapRqmtItmCap._Order.BaseUnit as meins preserving type)         as BaseUnit,

      case
      when CapRqmtItmCap.OperationIsPhase = '1'
      then ProcOpDurn.CapacityRequirementUnit
      else CapRqmtItmCap.CapacityRequirementUnit
      end                                                                   as CapacityRequirementUnit,

      case CapRqmtItmCap.RemainingCapReqOpSegSetupDurn
      when 0
      then 0
      else cast(unit_conversion(
                QUANTITY => CapRqmtItmCap.RemainingCapReqOpSegSetupDurn,
                SOURCE_UNIT => CapRqmtItmCap.CapacityRequirementUnit,
                TARGET_UNIT => cast( 'H' as abap.unit(3) ),
                ERROR_HANDLING => 'SET_TO_NULL' ) as cy_krueres )
      end                                                                   as RemCapReqOpSegSetupDurnInHours,

      case
      when CapRqmtItmCap.OperationIsPhase = '1'
      then case ProcOpDurn.TotRemCapReqOpSegProcgDurn
           when 0
           then 0
           else cast( unit_conversion(
                      QUANTITY => ProcOpDurn.TotRemCapReqOpSegProcgDurn,
                      SOURCE_UNIT => ProcOpDurn.CapacityRequirementUnit,
                      TARGET_UNIT => cast( 'H' as abap.unit(3) ),
                      ERROR_HANDLING => 'SET_TO_NULL' ) as cy_kbeares )
           end
      else case CapRqmtItmCap.RemainingCapReqOpSegProcgDurn
           when 0
           then 0
           else cast( unit_conversion(
                      QUANTITY => CapRqmtItmCap.RemainingCapReqOpSegProcgDurn,
                      SOURCE_UNIT => CapRqmtItmCap.CapacityRequirementUnit,
                      TARGET_UNIT => cast( 'H' as abap.unit(3) ),
                      ERROR_HANDLING => 'SET_TO_NULL' ) as cy_kbeares )
           end
      end                                                                   as RemCapReqOpSegProcgDurnInHours,

      case CapRqmtItmCap.RemainingCapReqOpSegTrdwnDurn
      when 0
      then 0
      else cast(unit_conversion(
                QUANTITY => CapRqmtItmCap.RemainingCapReqOpSegTrdwnDurn,
                SOURCE_UNIT => CapRqmtItmCap.CapacityRequirementUnit,
                TARGET_UNIT => cast( 'H' as abap.unit(3) ),
                ERROR_HANDLING => 'SET_TO_NULL' ) as cy_kabrres )
      end                                                                   as RemCapReqOpSegTrdwnDurnInHours,

      //Convert it into days

      //     case CapRqmtItmCap.RemainingCapReqOpSegProcgDurn

      //              when 0

      //                then 0

      //              else

      //               cast (CapRqmtItmCap.RemainingCapReqOpSegProcgDurn / 24.0 as abap.fltp )

      //               end                                                                             as    RemCapReqOpSegProcgDurnInDays,

      //                      cast( unit_conversion(

      //                      QUANTITY => CapRqmtItmCap.RemainingCapReqOpSegProcgDurn,

      //                      SOURCE_UNIT => CapRqmtItmCap.CapacityRequirementUnit,

      //                      TARGET_UNIT => cast( 'D' as abap.unit(3) ),

      //                     ERROR_HANDLING => 'SET_TO_NULL' ) as cy_kbeares )

      //                     end                                                                                       as    RemCapReqOpSegProcgDurnInDays,

      //Remaining duration in secs

      //      CapRqmtItmCap.RemainingCapReqOpSegProcgDurn   as RemCapReqOpSegProcgDurnInSecs,


      CapRqmtItmCap._Order.BOOExplosionDate                                 as BOOExplosionDate,               //Routing Explosion Date for Manufacturing Orders


      CapRqmtItmCap._Order.BOOExplosionDate                                 as BOMExplosionDate,               //BOM Explosion Date for Manufacturing Orders


      CapRqmtItmCap._PlannedOrder.PlndOrderPlannedStartDate, // Planned Order Start Date


      CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate, //Planned Order end date


      CapRqmtItmCap.OrderObjectInternalID,


      /*Set up Type Key, Setup Group Category and Set Group Key */
      CapRqmtItmCap._OrderOperation.OperationSetupGroup,
      CapRqmtItmCap._OrderOperation.OperationSetupGroupCategory,
      CapRqmtItmCap._OrderOperation.OperationSetupType,
//      cast (DATS_ADD_DAYS(cast ($session.system_date as sydatum),-365,'FAIL') as sydatum) as HorizonStartDate,

      cast(cast(concat(dats_add_days(cast($session.system_date as sydatum),-90,'NULL'),'000000')as abap.numc(15) ) as timestamp) 
      as HorizonStartDatetime,
      cast (dats_add_days(cast ($session.system_date as sydatum),-90,'FAIL') as sydatum) as HorizonStartDate,
      //-----------Realign schedule plantCurrentDateTime----------// 

      case
      when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is null
      then
         tstmp_to_dats( tstmp_current_utctimestamp(),
                         abap_system_timezone($session.client, 'NULL'),
                         $session.client,
                         'NULL' )

        when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is not null
        then
         tstmp_to_dats( tstmp_current_utctimestamp(),
         CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,
                         $session.client,
                         'NULL' )
        end                                                                 as PlantCurrentDate,
      case
       when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is null
        then
           tstmp_to_tims( tstmp_current_utctimestamp(),
                         abap_system_timezone($session.client, 'NULL'),
                         $session.client,
                         'NULL' )
        when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is not null
        then
        tstmp_to_tims( tstmp_current_utctimestamp(),
                  CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,
                         $session.client,
                         'NULL' )

        end                                                                 as PlantCurrentTime,
        //------------end of Plant Current date time -----------------//

        
       /*AVC Configuration */
      case when CapRqmtItmCap._OrderItem.ProductConfiguration is not initial and CapRqmtItmCap._Order.OrderCategory = '10' then
      cast('X' as boole_d preserving type )
      when CapRqmtItmCap._OrderItem.ProductConfiguration is initial and CapRqmtItmCap._Order.OrderCategory != '10'
      then cast('' as boole_d preserving type )
      end                                                                   as OrderHasConfiguration,
      
      /* Receipt Date Calculation for I_Logistics Order */
      case 
        when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is null or CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is initial
        then
        case
          when CapRqmtItmCap._Order.PlannedEndTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap._Order.PlannedEndDate,1,'NULL'),
                                        '000000',
                                        abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
          when CapRqmtItmCap._Order.ScheduledBasicEndTime <= '240000'
            then cast(dats_tims_to_tstmp(CapRqmtItmCap._Order.PlannedEndDate,
                                        CapRqmtItmCap._Order.PlannedEndTime,
                                        abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
        end
        when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is not null
        then
        case
          when CapRqmtItmCap._Order.PlannedEndTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap._Order.PlannedEndDate,1,'NULL'),
                                        '000000',
                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL')as timestamp )
          when CapRqmtItmCap._Order.ScheduledBasicEndTime <= '240000'
            then cast(dats_tims_to_tstmp(CapRqmtItmCap._Order.PlannedEndDate,
                                        CapRqmtItmCap._Order.PlannedEndTime,
                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL')as timestamp )
        end
        end                                                                as ScheduledOrderReceiptDateTime,
        
        // Calculating OperationLatestStartDateTime Field of Plant Time Zone for displaying in UI

       case
          when _StatusConfirmed.StatusCode is not null
          then
        //  cast(cast(concat(CapRqmtItmCap._OrderOperation.OpActualExecutionStartDate,CapRqmtItmCap._OrderOperation.OpActualExecutionStartTime)as abap.numc(15)) as timestamp)

         cast(dats_tims_to_tstmp(CapRqmtItmCap._OrderOperation.OpActualExecutionStartDate,CapRqmtItmCap._OrderOperation.OpActualExecutionStartTime,
                                  CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL') as timestamp )
          when OperationLatestStartTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(OperationLatestStartDate,1,'NULL'),
                                        '000000',
                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL')as timestamp )  
          when OperationLatestStartTime <= '240000'
            then cast(dats_tims_to_tstmp(OperationLatestStartDate,
                                        OperationLatestStartTime,
                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL')as timestamp )
        end                                                                 as OperationLatestStartDateTime,
        
        // Calculating OperationLatestEndDateTime Field of Plant Time Zone for displaying in UI           

       case
          when _StatusConfirmed.StatusCode is not null
          then
         // cast(cast(concat(CapRqmtItmCap._OrderOperation.OpActualExecutionEndDate,CapRqmtItmCap._OrderOperation.OpActualExecutionEndTime)as abap.numc(15)) as timestamp)

          cast(dats_tims_to_tstmp(CapRqmtItmCap._OrderOperation.OpActualExecutionEndDate,CapRqmtItmCap._OrderOperation.OpActualExecutionEndTime,
                                  CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL') as timestamp )
          when OperationLatestEndTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(OperationLatestEndDate,1,'NULL'),
                                        '000000',
                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL')as timestamp )  
          when OperationLatestEndTime <= '240000'
            then cast(dats_tims_to_tstmp(OperationLatestEndDate,
                                        OperationLatestEndTime,
                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone,$session.client,'NULL')as timestamp )
        end                                                                 as OperationLatestEndDateTime,
      
      
      CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone as PlantTimeZone

}

where
           CapRqmtItmCap._CapacityRequirement.CapacityRequirementOrigin =  '1'
  //and  CapRqmtItmCap.Operation != ''

  and  CapRqmtItmCap.CapacityInternalID                             =  CapRqmtItmCap._WorkCenter.CapacityInternalID
  and  CapRqmtItmCap._WorkCenter.AdvancedPlanningIsSupported        =  ''
  and  CapRqmtItmCap._Capacity.CapacityIsFinite                     =  'X'
  and(
       CapRqmtItmCap._Order.OrderCategory                           =  '10'
       and CapRqmtItmCap._OrderOperation.Sequence                   =  '000000'
    or CapRqmtItmCap._Order.OrderCategory                           =  '40'
       and CapRqmtItmCap._OrderOperation.Sequence                   =  '000000'
    or CapRqmtItmCap._Order.OrderCategory                           =  '30'
  )
  //and  _StatusConfirmed.StatusCode                                  is null

  and(
       CapRqmtItmCap.OperationIsPhase                               <> '2'
  )

union all select from I_CapacityRequirementItemCap as CapRqmtItmCap

//Obtain all operations from KBED. Not just the first operation.

  left outer join     P_ProcCapReqOpDurn           as ProcOpDurn on  CapRqmtItmCap.CapacityRequirement      = ProcOpDurn.CapacityRequirement
                                                                 and CapRqmtItmCap.CapacityRequirementItem  = ProcOpDurn.SuperiorOperationInternalID
                                                                 and CapRqmtItmCap.CapacityRqmtItemCapacity = ProcOpDurn.CapacityRqmtItemCapacity
                                                                 and CapRqmtItmCap.OperationIsPhase         = '1'
//association [1..1] to I_StatusObjectStatus as _StatusConfirmed on  CapRqmtItmCap.OrderObjectInternalID = _StatusConfirmed.StatusObject

//                                                               and _StatusConfirmed.StatusCode         = 'I0009'

//                                                               and _StatusConfirmed.StatusIsActive     = 'X'


{

  key cast ( CapRqmtItmCap.PlannedOrder as pph_cfs_ordernumber)                           as OrderID,

  key CapRqmtItmCap.CapacityRequirementItem                                               as CapacityRequirementItem,

  key CapRqmtItmCap.CapacityRqmtItemCapacity,

  key cast(CapRqmtItmCap._PlannedOrder.PlannedOrderCategory as pph_cfs_sch_ordercategory) as OrderCategory,

      cast( CapRqmtItmCap.Operation as vornr)                                             as Operation,

      cast ( CapRqmtItmCap._PlannedOrder.Material as matnr )                              as Material,

      CapRqmtItmCap._WorkCenter.Plant                                                     as Plant,
////performance changes as this fields are unused       

//      case 

//      when CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone is null

//        then

//         tstmp_to_dats( tstmp_current_utctimestamp(),

//                         abap_system_timezone($session.client, 'NULL'),

//                         $session.client,

//                         'NULL' )

//

//        else

//         tstmp_to_dats( tstmp_current_utctimestamp(),

//                         CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone,

//                         $session.client,

//                         'NULL' )

//        end                                                                 as PlantCurrentDate,

//      case

//        when CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone is null

//        then

//           tstmp_to_tims( tstmp_current_utctimestamp(),

//                         abap_system_timezone($session.client, 'NULL'),

//                         $session.client,

//                         'NULL' )

//        else

//        tstmp_to_tims( tstmp_current_utctimestamp(),

//                         CapRqmtItmCap._OrderOperation._WorkCenter._Plant._Address.AddressTimeZone,

//                         $session.client,

//                         'NULL' )

//

//        end                                                                 as PlantCurrentTime,


      cast ( CapRqmtItmCap._PlannedOrder.MRPArea as berid )                               as MRPArea,

      cast ( CapRqmtItmCap._PlannedOrder.MRPController as dispo )                         as MRPController,

      cast( CapRqmtItmCap._PlannedOrder.LastChangeDateTime as timestamp)                  as LastChangeDateTime,

      CapRqmtItmCap.CapacityRequirement                                                   as CapacityRequirement,

      CapRqmtItmCap._WorkCenter.WorkCenterInternalID                                      as WorkCenterInternalID,

      CapRqmtItmCap._WorkCenter.WorkCenter                                                as WorkCenter,

      CapRqmtItmCap._WorkCenter.WorkCenterCategoryCode                                    as WorkCenterCategoryCode,

      CapRqmtItmCap._WorkCenter._Text[1:Language=$session.system_language].WorkCenterText,

      cast(CapRqmtItmCap.WorkCenterTypeCode as cr_objty)                                  as WorkCenterTypeCode,

      CapRqmtItmCap.CapacityInternalID,

      CapRqmtItmCap._Capacity.CapacityCategoryCode,

      CapRqmtItmCap._WorkCenter.WorkCenterResponsible                                     as WorkCenterResponsible,

      CapRqmtItmCap._Capacity.CapacityResponsiblePlanner                                  as CapacityResponsiblePlanner,

      CapRqmtItmCap.OrderInternalID                                                       as OrderInternalID,

      cast(CapRqmtItmCap.OrderID as aufnr)                                                as ManufacturingOrder,

      cast(CapRqmtItmCap.PlannedOrder as plnum)                                           as PlannedOrder,

      cast ( CapRqmtItmCap._PlannedOrder.ProductionVersion as verid )                     as ProductionVersion,

      CapRqmtItmCap._PlannedOrder._ProductionVersion.ProductionLine                       as ProductionLine,

//      // Mapping Repetetive manufacturing to Discrete Industry

//      cast(

//      case

//      when CapRqmtItmCap._BillOfOperationsType.BillOfOperationsApplication = 'R'

//      then cast ('P' as plnaw)

//      else cast (CapRqmtItmCap._BillOfOperationsType.BillOfOperationsApplication as plnaw)

//      end as plnaw preserving type )                                                      as IndustryType,


        // Mapping Repetetive manufacturing to Discrete Industry

        cast(
        case 
        when CapRqmtItmCap._BillOfOperationsType.BillOfOperationsApplication = 'R' or 
        (CapRqmtItmCap._PlannedOrder.PlannedOrderCategory = '30' and CapRqmtItmCap._WorkCenter.WorkCenterCategoryCode <> '0008')
        then cast ('P' as plnaw)
        when (CapRqmtItmCap._PlannedOrder.PlannedOrderCategory = '30' and CapRqmtItmCap._WorkCenter.WorkCenterCategoryCode = '0008')
        then cast ('C' as plnaw)
        else cast (CapRqmtItmCap._BillOfOperationsType.BillOfOperationsApplication as plnaw)
        end as plnaw preserving type ) as IndustryType,

      case
      when CapRqmtItmCap._OrderStatusObjectStatus.StatusCode = 'I0117' and CapRqmtItmCap._OrderStatusObjectStatus.StatusIsInactive = ''
      then cast('DSPT' as pph_cfs_sch_status)
      else cast('DALL' as pph_cfs_sch_status)
      end