| Quintiq file version 2.0 | 
| #parent: #root | 
| MethodOverride GetPeriodTasksToSearch ( | 
|   ProductInStockingPointInPeriodPlanningLeaf pispip, | 
|   LibOpt_Scope scope, | 
|   RunContextForCapacityPlanning runcontext, | 
|   Units units, | 
|   Boolean isusingselectedunits, | 
|   Number depth | 
| ) as owning PeriodTaskOperations | 
| { | 
|   TextBody: | 
|   [* | 
|     ptforrecursive := construct(  PeriodTaskOperations ); | 
|      | 
|     if ( runcontext.IsSmartPlan() )  | 
|     { | 
|       period := pispip.Period_MP();   | 
|       // Traverse over the operations that produce this product as an output | 
|       // If this product is a byproduct, we do not want to add its operations, unless they only produce byproduct. | 
|       // This is to avoid adding products to the smart plan that are only related to the smart plan input pispips because they are produced together with the same byproduct | 
|       operations := selectset( pispip,  | 
|                                ProductInStockingPoint_MP.OperationOutput.Operation,  | 
|                                operation, | 
|                                operation.GetIsAvailableForOptimization()                                // The operation must be available | 
|                                and PeriodTaskOperation::GetIsValidPeriodTask( operation, period )       // The resulting period task must be valid | 
|                                and ( not pispip.ProductInStockingPoint_MP().Product_MP().IsDerivedByProduct()  // This product is not a byproduct | 
|                                      or  operation.AllOutputAreDerivedByProducts() ) // or all outputs are byproducts | 
|                                and ( not runcontext.IsOnlyPlanOneStepUpstream()  //When only planning 1 step upstream the operation should be part of a routing that produce the product of the selected PISPIPs | 
|                                      or exists( scope.GetRoutingOfSmartPlanPISPIPs(), Elements, routing, routing = operation.RoutingStep().Routing() ) ) | 
|                                and ( not isusingselectedunits or exists( units, Elements, unit, unit = operation.Unit() ) ) | 
|                                /*and ( not exists ( operation, OperationOutput.ProductInStockingPoint_MP.ProductInStockingPointInPeriodPlanning, p, p.Period_MP() = period, not p.GetIsInOptimizerRun() ) ) */ //exclude if output is from a pispip not in scope) | 
|                              ); | 
|      | 
|      | 
|       traverse( operations, Elements, operation )   | 
|       { | 
|         // Create/Select the related periodtaskoperation and add it to PTOperationsForOptimization | 
|         periodtaskoperation := PeriodTaskOperation::FindPeriodTaskOperationTypeIndex( period.Start() , operation.ID() ); | 
|          | 
|         if( isnull( periodtaskoperation ) ) | 
|         { | 
|           unit := operation.Unit(); | 
|           // Add the related unit periods to unitperiodsforoptimization | 
|           unitperiod := UnitPeriod::FindUnitPeriodTypeIndex( unit.ID(), period.Start(), period.End() ); | 
|           periodtaskoperation := PeriodTaskOperation::Create( operation, unitperiod, 0.0, false ); | 
|           // this transaction propagate is necessary, we need to get the dependent demand from the newly created periodtask.  | 
|           Transaction::Transaction().Propagate( relation( PeriodTaskOperation, DependentDemand ) ); | 
|           Transaction::Transaction().Propagate( relation( PeriodTaskOperation, NewSupply ) ); | 
|           Transaction::Transaction().Propagate( relation(  NewSupply, ProcessOutput ) );  | 
|           Transaction::Transaction().Propagate( relation(  DependentDemand, ProcessInput ) );  | 
|           Transaction::Transaction().Propagate( attribute( PeriodTask_MP, IsFrozenForOptimizerUpstream ) );  | 
|         } | 
|          | 
|         if ( not periodtaskoperation.IsFrozenForOptimizerUpstream() )  | 
|         { | 
|           ptforrecursive.Add( periodtaskoperation );  | 
|         } | 
|       } // traverse operations | 
|     } // end smart plan case | 
|     else | 
|     { | 
|       // case for meta optimizer | 
|       ptforrecursive := selectset( pispip, NewSupplyOperationForMeta.PeriodTaskOperation, pto, true, true );  | 
|     } | 
|     return &ptforrecursive | 
|   *] | 
| } |