P_MntrWrkCtrOrderOperations

DDL: P_MNTRWRKCTRORDEROPERATIONS SQL: PMWCORDOP Type: view COMPOSITE Package: PPH_VDM_CAP_EVAL

Work Center Order Operations

P_MntrWrkCtrOrderOperations is a Composite CDS View that provides data about "Work Center Order Operations" in SAP S/4HANA. It reads from 1 data source (P_MntrWrkCtrOrderOps) and exposes 70 fields with key fields OrderID, CapacityRequirementItem, CapacityRqmtItemCapacity, OrderCategory, CapacityRequirement. Part of development package PPH_VDM_CAP_EVAL.

Data Sources (1)

SourceAliasJoin Type
P_MntrWrkCtrOrderOps PMntrWrkCtrOrd from

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 (70)

KeyFieldSource TableSource FieldDescription
KEY OrderID OrderID
KEY CapacityRequirementItem CapacityRequirementItem
KEY CapacityRqmtItemCapacity CapacityRqmtItemCapacity
KEY OrderCategory OrderCategory
KEY CapacityRequirement CapacityRequirement
Operation Operation
Material Material
Plant Plant
MRPArea MRPArea
MRPController MRPController
LastChangeDateTime LastChangeDateTime
WorkCenterInternalID WorkCenterInternalID
WorkCenter WorkCenter
WorkCenterCategoryCode WorkCenterCategoryCode
WorkCenterText WorkCenterText
WorkCenterTypeCode WorkCenterTypeCode
CapacityInternalID CapacityInternalID
CapacityCategoryCode CapacityCategoryCode
WorkCenterResponsible WorkCenterResponsible
CapacityResponsiblePlanner CapacityResponsiblePlanner
OrderInternalID OrderInternalID
ManufacturingOrder ManufacturingOrder
PlannedOrder PlannedOrder
ProductionVersion ProductionVersion
ProductionLine ProductionLine
OperationPlanningStatusCode OperationPlanningStatusCode
ScheduledBasicStartDate ScheduledBasicStartDate
ScheduledBasicStartTime ScheduledBasicStartTime
ScheduledBasicEndDate ScheduledBasicEndDate
ScheduledBasicEndTime ScheduledBasicEndTime
OperationLatestStartDate OperationLatestStartDate
OperationLatestStartTime OperationLatestStartTime
LtstSchedldExecStrtDteTme LtstSchedldExecStrtDteTme
OperationLatestEndDate OperationLatestEndDate
OperationLatestEndTime OperationLatestEndTime
LtstSchedldExecEndDteTme LtstSchedldExecEndDteTme
OpLtstSchedldProcgStrtDte OpLtstSchedldProcgStrtDte
OpLtstSchedldProcgStrtTme OpLtstSchedldProcgStrtTme
OpLtstSchedldTrdwnStrtDte OpLtstSchedldTrdwnStrtDte
OpLtstSchedldTrdwnStrtTme OpLtstSchedldTrdwnStrtTme
RequirementDate RequirementDate
OrderPlannedTotalQty OrderPlannedTotalQty
BaseUnit BaseUnit
CapacityRequirementUnit CapacityRequirementUnit
RemCapReqOpSegSetupDurnInHours RemCapReqOpSegSetupDurnInHours
RemCapReqOpSegProcgDurnInHours RemCapReqOpSegProcgDurnInHours
RemCapReqOpSegTrdwnDurnInHours RemCapReqOpSegTrdwnDurnInHours
BOOExplosionDate BOOExplosionDate
BOMExplosionDate BOMExplosionDate
PlndOrderPlannedStartDate PlndOrderPlannedStartDate
PlndOrderPlannedEndDate PlndOrderPlannedEndDate
OrderObjectInternalID OrderObjectInternalID
OperationSetupGroup OperationSetupGroup
OperationSetupGroupCategory OperationSetupGroupCategory
OperationSetupType OperationSetupType
HorizonStartDate HorizonStartDate
HorizonStartDatetime HorizonStartDatetime
clientNULLendasPlantCurrentDate
clientNULLendasPlantCurrentTime
OrderHasConfiguration OrderHasConfiguration
OperationLatestStartDateTime OperationLatestStartDateTime
OperationLatestEndDateTime OperationLatestEndDateTime
PlantTimeZone PlantTimeZone
OrderType OrderType
OpLtstSchedldSetupStrtDteTme OperationLatestStartDateTime
OpLtstSchedldTrdwnEndDteTme OperationLatestEndDateTime
TotalDuration
SetupDuration
ProcessDuration
TearDownDuration
@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    P_MntrWrkCtrOrderOps as PMntrWrkCtrOrd
{
    key OrderID,
    key CapacityRequirementItem,
    key CapacityRqmtItemCapacity,
    key OrderCategory,
    key CapacityRequirement,
    
    Operation,
    
    Material,
    
    Plant,
    
    MRPArea,
    
    MRPController,
    
    LastChangeDateTime,
     
    WorkCenterInternalID,
    
    WorkCenter,
    
    WorkCenterCategoryCode,
    
    WorkCenterText,
    
    WorkCenterTypeCode,
    
    CapacityInternalID,
    
    CapacityCategoryCode,
    
    WorkCenterResponsible,
    
    CapacityResponsiblePlanner,
    
    OrderInternalID,
    
    ManufacturingOrder,
    
    PlannedOrder,
    
    ProductionVersion,
    
    ProductionLine,
    
    // Mapping Repetetive manufacturing to Discrete Industry

           cast(
            case 
            when BillOfOperationsApplication = 'R' or 
            (OrderCategory = '30' and WorkCenterCategoryCode <> '0008')
            then cast ('P' as plnaw)
            when (OrderCategory = '30' and WorkCenterCategoryCode = '0008')
            then cast ('C' as plnaw)
            else cast (BillOfOperationsApplication as plnaw)
            end as plnaw preserving type ) as IndustryType,
            
      OperationPlanningStatusCode,
      
      ScheduledBasicStartDate ,
      ScheduledBasicStartTime ,

      ScheduledBasicEndDate ,

      ScheduledBasicEndTime ,
      
      OperationLatestStartDate ,

      OperationLatestStartTime ,
      
      LtstSchedldExecStrtDteTme, 
      
      OperationLatestEndDate,
      
      OperationLatestEndTime,
      
      LtstSchedldExecEndDteTme,
      OpLtstSchedldProcgStrtDte,
      OpLtstSchedldProcgStrtTme,
      OpLtstSchedldTrdwnStrtDte,
      OpLtstSchedldTrdwnStrtTme,
      RequirementDate,
      OrderPlannedTotalQty ,
      BaseUnit,
      CapacityRequirementUnit,
      RemCapReqOpSegSetupDurnInHours,    
      RemCapReqOpSegProcgDurnInHours,    
      RemCapReqOpSegTrdwnDurnInHours,    
      BOOExplosionDate,
      BOMExplosionDate,
      PlndOrderPlannedStartDate,
      PlndOrderPlannedEndDate,
      OrderObjectInternalID,    
      OperationSetupGroup,    
      OperationSetupGroupCategory,    
      OperationSetupType,
      HorizonStartDate,
      HorizonStartDatetime,             
    
    //-----------Realign schedule plantCurrentDateTime----------// 

          case
          when PlantTimeZone is null
          then
             tstmp_to_dats( tstmp_current_utctimestamp(),abap_system_timezone($session.client, 'NULL'),
                             $session.client, 'NULL' )
    
           when PlantTimeZone is not null
            then
             tstmp_to_dats( tstmp_current_utctimestamp(), PlantTimeZone, $session.client, 'NULL' )
            end                                                                 as PlantCurrentDate,
          case
           when PlantTimeZone is null
            then
              tstmp_to_tims( tstmp_current_utctimestamp(),
                             abap_system_timezone($session.client, 'NULL'),
                             $session.client,  'NULL' )
            when PlantTimeZone is not null
            then
            tstmp_to_tims( tstmp_current_utctimestamp(),
                      PlantTimeZone,  $session.client,'NULL' )    
            end                                                                 as PlantCurrentTime,
   
    
    OrderHasConfiguration,
    @Semantics.dateTime:true
      case 
        when PlantTimeZone is null or PlantTimeZone is initial
        then
        case
          when PlannedEndTime >= '240000'
             then cast(dats_tims_to_tstmp(dats_add_days( PlannedEndDate,1,'NULL'), '000000',
                                        abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
          when PlannedEndTime <= '240000'
            then cast(dats_tims_to_tstmp(PlannedEndDate, PlannedEndTime,
                                        abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
        end
        when PlantTimeZone is not null
        then
        case
          when PlannedEndTime >= '240000'
            then cast(dats_tims_to_tstmp(dats_add_days(PlannedEndDate,1,'NULL'), '000000',
                                        PlantTimeZone, $session.client,'NULL')as timestamp )
          when PlannedEndTime <= '240000'
            then cast(dats_tims_to_tstmp(PlannedEndDate,  PlannedEndTime,
                                        PlantTimeZone, $session.client,'NULL')as timestamp )
        end
        end                                                                as ScheduledOrderReceiptDateTime,
    
    
    OperationLatestStartDateTime,
    OperationLatestEndDateTime,
    PlantTimeZone,
    OrderType,
    //Activity coloring

    //calculaing the setup, process and teardown duration in accordance with plant timezone

    @Semantics.dateTime:true 
    OperationLatestStartDateTime as OpLtstSchedldSetupStrtDteTme,
    @Semantics.dateTime:true
    case 
      when PlantTimeZone is null or PlantTimeZone is initial
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldProcgStrtDte, OpLtstSchedldProcgStrtTme,
                                      abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
      when PlantTimeZone is not null
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldProcgStrtDte,  OpLtstSchedldProcgStrtTme,
                                      PlantTimeZone, $session.client,'NULL')as timestamp )
      end                                                                as OpLtstSchedldSetupEndDteTme,
     @Semantics.dateTime:true  
     case 
      when PlantTimeZone is null or PlantTimeZone is initial
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldProcgStrtDte, OpLtstSchedldProcgStrtTme,
                                      abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
      when PlantTimeZone is not null
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldProcgStrtDte,  OpLtstSchedldProcgStrtTme,
                                      PlantTimeZone, $session.client,'NULL')as timestamp )
      end                                                                as OpLtstSchedldProcgStrtDteTme,
    @Semantics.dateTime:true  
    case 
      when PlantTimeZone is null or PlantTimeZone is initial
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldTrdwnStrtDte, OpLtstSchedldTrdwnStrtTme,
                                      abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
      when PlantTimeZone is not null
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldTrdwnStrtDte,  OpLtstSchedldTrdwnStrtTme,
                                      PlantTimeZone, $session.client,'NULL')as timestamp )
      end                                                                as OpLtstSchedldProcgEndDteTme ,    
    @Semantics.dateTime:true
    case 
      when PlantTimeZone is null or PlantTimeZone is initial
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldTrdwnStrtDte, OpLtstSchedldTrdwnStrtTme,
                                      abap_system_timezone($session.client, 'NULL'),$session.client,'NULL')as timestamp )
      when PlantTimeZone is not null
      then
      cast(dats_tims_to_tstmp(OpLtstSchedldTrdwnStrtDte,  OpLtstSchedldTrdwnStrtTme,
                                      PlantTimeZone, $session.client,'NULL')as timestamp )
      end                                                                as OpLtstSchedldTrdwnStrtDteTme ,
    @Semantics.dateTime:true
    OperationLatestEndDateTime as OpLtstSchedldTrdwnEndDteTme,
//    (LtstSchedldExecEndDteTme   -  LtstSchedldExecStrtDteTme)  as TotalDuration,

//    (LtstSchdlProcStrtDteTime  -  LtstSchedldExecStrtDteTme )  as SetupDuration,

//    (LtstSchdlTrdwnStrtDteTime  -  LtstSchdlProcStrtDteTime )  as ProcessDuration,   

//    (LtstSchedldExecEndDteTme   -  LtstSchdlTrdwnStrtDteTime  )  as TearDownDuration 

//


    tstmp_seconds_between(cast(LtstSchedldExecStrtDteTme as abap.dec(15,0)), cast(LtstSchedldExecEndDteTme as abap.dec(15,0)), 'FAIL' ) as TotalDuration,
    tstmp_seconds_between(cast(LtstSchedldExecStrtDteTme as abap.dec(15,0)) , cast(LtstSchdlProcStrtDteTime as abap.dec(15,0)), 'FAIL') as SetupDuration,
    tstmp_seconds_between(cast( LtstSchdlProcStrtDteTime as abap.dec(15,0)), cast(LtstSchdlTrdwnStrtDteTime as abap.dec(15,0)), 'FAIL') as ProcessDuration,
    tstmp_seconds_between(cast(LtstSchdlTrdwnStrtDteTime as abap.dec(15,0)), cast(LtstSchedldExecEndDteTme  as abap.dec(15,0)), 'FAIL') as TearDownDuration
  
    
        
 }




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

//  

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

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

//      CapRqmtItmCap._Order.OrderType as OrderType

//

//}

//

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

//

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

//////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                                                                                 as OperationPlanningStatusCode,

//

//      _CapacityRequirement.ScheduledBasicStartDate                                        as ScheduledBasicStartDate,

//

//      _CapacityRequirement.ScheduledBasicStartTime                                        as ScheduledBasicStartTime,

//

//      _CapacityRequirement.ScheduledBasicEndDate                                          as ScheduledBasicEndDate,

//

//      _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,

//

//      //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    LtstSchedldExecStrtDteTmeCpy,

//

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

//

//      //Convert it into days

//      //      case CapRqmtItmCap.RemainingCapReqOpSegProcgDurn

//      //              when 0

//      //                then 0

//      //              else

//      //              cast (CapRqmtItmCap.RemainingCapReqOpSegProcgDurn / 86400.00 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._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 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,

//        

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

//       case

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

//      CapRqmtItmCap._PlannedOrder.PlannedOrderType as OrderType

//

//}

//

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

//  )