lazhen
2025-01-09 8afe90b633046db39042aada36b88193062f8cff
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
Quintiq file version 2.0
#parent: #root
Method CleanUpUnitCapacity
{
  Description: 'Find sets of equivalent unit capacities in consecutive periods and remove all but the first one'
  TextBody:
  [*
    // Clean up unit capacity
    // Get a sorted set of all unit capacities of this unit
    sorteducs := selectsortedset( this, UnitCapacity, uc, 
                                  true, 
                                  uc.Start() );
    
    head := null( UnitCapacity );
    
    // Traverse the sorted set of unit capacities
    traverse( sorteducs, Elements, uc )
    {
      // Check if there's any unit capacity is selected, if there's none, set the current unit capacity as selected
      if( isnull( head ) )
      {
        head := uc;
      }
    
      // Get the index of the selected unit capacity in the sorted set
      headpos := sorteducs.Find( head );
      // Get the next unit capacity after the selected unit capacity in the sorted set
      nextuc := guard( sorteducs.Element( headpos + 1 ), null( UnitCapacity ) );
    
      // Check if there's any next unit capacity
      if( not isnull( nextuc ) )
      {
        // Check if the next unit capacity has the same data as the selected one
        // If the data is the same, remove the next unit capacity from the sorted set and dataset
        if( head.HasSameData( nextuc ) )
        {
          sorteducs.Remove( nextuc );
          nextuc.Delete();
        }
        // Else, set the next unit capacity as the selected one
        else
        {
          head := nextuc;
        }
      }
    }
  *]
}