yypsybs
2023-09-09 3cb5a54def670d97301f07170fcaad213bfc54f2
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
StaticMethod GetSalesSegmentToSave (
  SalesSegment_MPs checkSalesSegments
) as owning SalesSegment_MPs
{
  Description:
  [*
    Web introduced new bookmark logic. Keep bookmark as short as possible.
    If all children (and descendants) checked, store the parent only. If partial, then no choice we store the checked children.
  *]
  TextBody:
  [*
    result := construct( SalesSegment_MPs ); // Sales Segment to bookmark
    
    // Filter out parent sales segments
    checkLeafSalesSegments := selectset( checkSalesSegments, Elements, c, c.Child( relsize ) = 0 );
    
    // Based on filtered sales segment, get all their parents
    allParentsSorted := selectsortedset( SalesSegment_MP::GetAllParents( checkLeafSalesSegments ), 
                                         Elements, e, 
                                         e.Child( relsize ) > 0,  // Filter out the leaf as GetAllParents return the sales segment itself as well
                                         e.DisplayIndex() );      // Sort from root down to lower level sales segment 
    
    // Important to start with root, as we will remove 'intersecting' leaf salse segment as we loop
    traverse( allParentsSorted, Elements, parent, checkLeafSalesSegments.Size() > 0 ) // Continue as long as there's sales segment to be considered
    {
      leafDescendants := selectset( parent, AllChildren.AsChildren, ss, ss.Child( relsize ) = 0 ); 
    
      // If current parent's leaf descendants are found in user checked sales segment
      // We only bookmark the parent and remove leaf descendants from the list and proceed next parent
      // Example (Food demo): Add sales segment Target if ALL its children are part of user checked sales segment
      if( checkLeafSalesSegments.ContainsAll( leafDescendants ) )
      {
        checkLeafSalesSegments.Remove( leafDescendants );  
        result.Add( parent );  
      }
    }
    
    // If there's leaf sales segment left (not all siblings checked), add them to the result
    if( checkLeafSalesSegments.Size() > 0 )
    {
      result := result.Union( checkLeafSalesSegments );
    }
    
    return &result;
  *]
}