P_MNTRWRKCTRORDEROPS

CDS View

Work Center Order Operation

P_MNTRWRKCTRORDEROPS is a CDS View in S/4HANA. Work Center Order Operation. 1 CDS views read from this table.

CDS Views using this table (1)

ViewTypeJoinVDMDescription
P_MntrWrkCtrOrderOperations view from COMPOSITE Work Center Order Operations
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Work Center Order Operation'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
  serviceQuality: #X,
  sizeCategory: #S,
  dataClass: #MIXED
}
@VDM.private:true 
@VDM.viewType: #COMPOSITE
define view entity P_MntrWrkCtrOrderOps as select from I_CapacityRequirementItemCap as CapRqmtItmCap

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

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

  left outer to one join ftg_enabled_for_client as FeatureToggle on FeatureToggle.ftg_id = 'FTGL_CPS_MAINT_ORDER'
  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,
  
  key CapRqmtItmCap.CapacityRequirement                                     as CapacityRequirement,

      cast( CapRqmtItmCap.Operation as vornr)                               as Operation,

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

      CapRqmtItmCap._WorkCenter.Plant                                       as Plant,

      cast( CapRqmtItmCap._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._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._Order._OrderMainItem.ProductionVersion as verid)               as ProductionVersion,

      CapRqmtItmCap._Order._OrderMainItem._ProductionVersion.ProductionLine               as ProductionLine,

//       //Mapping Repetitive manufacturing to Discrete Industry


//        cast(

//        case 

//        when CapRqmtItmCap._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 (CapRqmtItmCap._Order._OrderMainItem._ProductionVersion._BillOfOperationsType.BillOfOperationsApplication as plnaw)

//        end as plnaw preserving type ) as IndustryType,

      
      CapRqmtItmCap._Order._OrderMainItem._ProductionVersion._BillOfOperationsType.BillOfOperationsApplication as BillOfOperationsApplication,
      case
      when CapRqmtItmCap._OrderOperation.OpDispatchedExecStartDate = '00000000'
      then cast('DALL' as pph_cfs_sch_status)
      else cast('DSPT' as pph_cfs_sch_status)
      end                                                                   as OperationPlanningStatusCode,

      CapRqmtItmCap._Order.ScheduledBasicStartDate                                        as ScheduledBasicStartDate,

      CapRqmtItmCap._Order.ScheduledBasicStartTime                                        as ScheduledBasicStartTime,

     CapRqmtItmCap._Order.ScheduledBasicEndDate                                          as ScheduledBasicEndDate,

      CapRqmtItmCap._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,
   
      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,

        @Semantics.quantity.unitOfMeasure: 'BaseUnit'
      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,

      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 CapRqmtItmCap._Order.OrderCategory ='30' and FeatureToggle.ftg_id is not null
         then case 
             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
       else
       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._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL') as timestamp )
          when CapRqmtItmCap.OperationLatestStartTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap.OperationLatestStartDate,1,'NULL'),
                                        cast( '000000' as tims),
                                        CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )  
          when CapRqmtItmCap.OperationLatestStartTime <= '240000'
            then cast(dats_tims_to_tstmp(CapRqmtItmCap.OperationLatestStartDate,
                                       CapRqmtItmCap.OperationLatestStartTime,
                                       CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )
        end   
        end                                                              as OperationLatestStartDateTime,
        
        // Calculating OperationLatestEndDateTime Field of Plant Time Zone for displaying in UI     

        
       case when CapRqmtItmCap._Order.OrderCategory ='30' and FeatureToggle.ftg_id is not null
          then case 
             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
       else      
       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._StandardOrganizationAddress.AddressTimeZone,$session.client,'NULL') as timestamp )
          when CapRqmtItmCap.OperationLatestEndTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap.OperationLatestEndDate,1,'NULL'),
                                        cast( '000000' as tims),
                                        CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone,$session.client,'NULL')as timestamp )  
          when CapRqmtItmCap.OperationLatestEndTime <= '240000'
            then cast(dats_tims_to_tstmp(CapRqmtItmCap.OperationLatestEndDate,
                                       CapRqmtItmCap.OperationLatestEndTime,
                                       CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )
        end
        end                                                                 as OperationLatestEndDateTime,
//      

    
      CapRqmtItmCap._Order.PlannedEndDate as PlannedEndDate,
      CapRqmtItmCap._Order.PlannedEndTime as PlannedEndTime,
      CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone as PlantTimeZone,
      CapRqmtItmCap._Order.OrderType as OrderType,
 
     //process start date time 

       case
          when _StatusConfirmed.StatusCode is not null
             then cast(cast(concat(CapRqmtItmCap._OrderOperation.OpActualExecutionEndDate,CapRqmtItmCap._OrderOperation.OpActualExecutionEndTime)as abap.numc(15)) as timestamp) 
        
        else
          case when CapRqmtItmCap.LtstSchedldProcStrtTime >= '240000'
          then cast(cast(concat(dats_add_days(CapRqmtItmCap.LtstSchedldProcStrtDte,1,'NULL'),'000000')as abap.numc(15)) as timestamp) 
          else
          cast(cast(concat(CapRqmtItmCap.LtstSchedldProcStrtDte,CapRqmtItmCap.LtstSchedldProcStrtTime)as abap.numc(15)) as timestamp) 
          end
        end as LtstSchdlProcStrtDteTime,
     //teardown start date time     

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

}

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 to many 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,

  key CapRqmtItmCap.CapacityRequirement                                     as CapacityRequirement,
  
      cast( CapRqmtItmCap.Operation as vornr)                                             as Operation,

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

      CapRqmtItmCap._WorkCenter.Plant                                                     as Plant,


      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' 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,

      CapRqmtItmCap._BillOfOperationsType.BillOfOperationsApplication as BillOfOperationsApplication,
      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                                                                                 as OperationPlanningStatusCode,

      CapRqmtItmCap._CapacityRequirement.ScheduledBasicStartDate                                        as ScheduledBasicStartDate,

      CapRqmtItmCap._CapacityRequirement.ScheduledBasicStartTime                                        as ScheduledBasicStartTime,

      CapRqmtItmCap._CapacityRequirement.ScheduledBasicEndDate                                          as ScheduledBasicEndDate,

      CapRqmtItmCap._CapacityRequirement.ScheduledBasicEndTime                                          as ScheduledBasicEndTime,

      CapRqmtItmCap.OperationLatestStartDate                                              as OperationLatestStartDate,

      CapRqmtItmCap.OperationLatestStartTime                                              as OperationLatestStartTime,

      case
      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,

      CapRqmtItmCap.OperationLatestEndDate                                                as OperationLatestEndDate,

      CapRqmtItmCap.OperationLatestEndTime                                                as OperationLatestEndTime,

      case
      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,

      case CapRqmtItmCap._CapacityRequirement.CapacityRequirementOrigin
      when '1' then CapRqmtItmCap._Order.PlannedEndDate
      when '2' then CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate
      end                                                                                 as RequirementDate,

      cast (  CapRqmtItmCap._PlannedOrder.PlannedTotalQtyInBaseUnit as mgvrg )            as OrderPlannedTotalQty,

      cast(CapRqmtItmCap._PlannedOrder.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,

      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._BOOOperationInternalVersion.OperationSetupGroup,
      CapRqmtItmCap._BOOOperationInternalVersion.OperationSetupGroupCategory,
      CapRqmtItmCap._BOOOperationInternalVersion.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 */
      cast('' as boole_d preserving type )                                  as OrderHasConfiguration,
      
      /* Receipt Date Calculation for I_Planned Order */
     // @Semantics.dateTime:true

//      case 

//        when CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is null or CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone is initial

//        then

//        case

//          when CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndTime >= '240000'

//             then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate,1,'NULL'),

//                                        '000000',

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

//          when CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndTime <= '240000'

//            then cast(dats_tims_to_tstmp(CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate,

//                                        CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndTime,

//                                        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._PlannedOrder.PlndOrderPlannedEndTime >= '240000'

//            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate,1,'NULL'),

//                                        '000000',

//                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone, $session.client,'NULL')as timestamp )

//          when CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndTime <= '240000'

//            then cast(dats_tims_to_tstmp(CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate,

//                                        CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndTime,

//                                        CapRqmtItmCap._WorkCenter._Plant._Address.AddressTimeZone, $session.client,'NULL')as timestamp )

//        end

//        end                                                                as ScheduledOrderReceiptDateTime,

        
        // Calculation OperationLatestStartDateTime Field for Plant Time Zone  for display in UI 

       case
          when CapRqmtItmCap.OperationLatestStartTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap.OperationLatestStartDate,1,'NULL'),
                                        cast( '000000' as tims),
                                        CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )  
           when CapRqmtItmCap.OperationLatestStartTime <= '240000'
            then cast(dats_tims_to_tstmp(CapRqmtItmCap.OperationLatestStartDate,
                                        CapRqmtItmCap.OperationLatestStartTime,
                                        CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )
        end                                                                 as OperationLatestStartDateTime,
        
        // Calculation OperationLatestEndDateTime Field for Plant Time Zone for display in UI          

      case
          when CapRqmtItmCap.OperationLatestEndTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(CapRqmtItmCap.OperationLatestEndDate,1,'NULL'),
                                        cast( '000000' as tims),
                                        CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )  
          when CapRqmtItmCap.OperationLatestEndTime <= '240000'
            then cast(dats_tims_to_tstmp(CapRqmtItmCap.OperationLatestEndDate,
                                        CapRqmtItmCap.OperationLatestEndTime,
                                        CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone, $session.client,'NULL')as timestamp )
        end                                                                 as OperationLatestEndDateTime,
    
      CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndDate as PlannedEndDate,
      CapRqmtItmCap._PlannedOrder.PlndOrderPlannedEndTime as PlannedEndTime,
      CapRqmtItmCap._WorkCenter._Plant._StandardOrganizationAddress.AddressTimeZone as PlantTimeZone,
      CapRqmtItmCap._PlannedOrder.PlannedOrderType as OrderType,
      
      case
      when CapRqmtItmCap.LtstSchedldProcStrtTime >= '240000'
          then cast(cast(concat(dats_add_days(CapRqmtItmCap.LtstSchedldProcStrtDte,1,'NULL'),'000000')as abap.numc(15)) as timestamp) 
       else
      cast(cast(concat(CapRqmtItmCap.LtstSchedldProcStrtDte,CapRqmtItmCap.LtstSchedldProcStrtTime)as abap.numc(15)) as timestamp)
      end as LtstSchdlProcStrtDteTime,
      
      case
      when CapRqmtItmCap.LtstSchedldTrdwnStrtTime >= '240000'
          then cast(cast(concat(dats_add_days(CapRqmtItmCap.LtstSchedldTrdwnStrtDte,1,'NULL'),'000000')as abap.numc(15)) as timestamp) 
       else
      cast(cast(concat(CapRqmtItmCap.LtstSchedldTrdwnStrtDte,CapRqmtItmCap.LtstSchedldTrdwnStrtTime  )as abap.numc(15)) as timestamp) 
      end as LtstSchdlTrdwnStrtDteTime
}

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

  and  CapRqmtItmCap.CapacityInternalID                             = CapRqmtItmCap._WorkCenter.CapacityInternalID
  and  CapRqmtItmCap._WorkCenter.AdvancedPlanningIsSupported        = ''
  //and  _StatusConfirmed.StatusCode                                  is null

  and  CapRqmtItmCap._Capacity.CapacityIsFinite                     = 'X'
  and(
       CapRqmtItmCap.OperationIsPhase                               <> '2'
  )