| Quintiq file version 2.0 | 
| #parent: #root | 
| Method SynchronizeBasePeriods | 
| { | 
|   Description: 'Synchronize base periods that will be used for planning' | 
|   TextBody: | 
|   [* | 
|     // DWE2 May-17-2016 (created) | 
|     // Reset all periods to none base and assume as deleted, we will flush the useless period at the end of this method | 
|     traverse( this, Period_MP, period ) | 
|     { | 
|       period.IsBase( false ); | 
|     } | 
|      | 
|     sortedperiods := selectsortedset( this, Period_MP, period, | 
|                                       not period.IsDeleted() | 
|                                       and not period.GetIsSystemCreated(), | 
|                                       period.Start() ); | 
|     if( sortedperiods.Size() > 0 ) | 
|     { | 
|       starts := selectvalues( sortedperiods, Elements, period, true, period.Start() ); | 
|       starts.Add( this.PeriodParameter_MP().StartOfPlanning() ); // Include start of planning as a chop off for period spans across it | 
|      | 
|       ends := selectvalues( sortedperiods, Elements, period, true, period.End() ); | 
|      | 
|       periods := starts.Union( ends ).Unique(); | 
|       periods.Sort(); | 
|      | 
|       i := 0; | 
|       while( i < periods.Size() - 1 ) | 
|       { | 
|         start := periods.Element( i ); | 
|         end := periods.Element( i + 1 ); | 
|          | 
|         // Use select instead of typeindex  | 
|         systemperiod := select( this, Period_MP, period, period.Start() = start, period.End() = end ); | 
|          | 
|                                                                                      | 
|         // If there is no period, create new and mark as system created. (null period spec) | 
|         if( isnull( systemperiod ) ) | 
|         { | 
|           systemperiod := Period_MP::Create( this, null( PeriodSpecification_MP ), start, end, true, false ); | 
|         } | 
|         // If there is existing one which is marked as deleted, make it a system period | 
|         else if( systemperiod.IsDeleted() ) | 
|         {                 | 
|           systemperiod.PeriodSpecification_MP( relflush ); | 
|           Period_MP::ChangeKeyPeriodTypeIndex( systemperiod, "", systemperiod.Start(), systemperiod.End() ); | 
|         } | 
|      | 
|         systemperiod.IsBase( true ); | 
|         systemperiod.IsDeleted( false );  | 
|         i++; | 
|       } | 
|     } | 
|      | 
|     if( not this.IsRollingPeriod() ) | 
|     { | 
|       this.DeleteObsoleteBasePeriod(); | 
|     } | 
|   *] | 
| } |