yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
  *]
}