admin
2025-01-22 7e31442f0e9b07764e9c6a9680d3d4aeba5fe1de
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Quintiq file version 2.0
#parent: #root
Method AddStockingPoints (
  StockingPoint_MPs stockingpoints,
  Boolean asorigin,
  Boolean isfromdb
)
{
  Description: 'Create stocking point in lane instances and create lane leg if necessary'
  TextBody:
  [*
    spiltocreatelegs := construct( StockingPointInLanes );
    
    traverse( stockingpoints, Elements, stockingpoint )
    {
      /*
        In most cases, a StockingPointInLane will have either its OriginLaneID or DestinationLane ID left blank.
        This usually occurs when the origin and destination are in different lanes.
    
        However, there is also a case when a stocking point is assigned to both origin and destination in lane.
        In such case, there should be only one StockingPointInLane created, with its origin and destination
        set to the same LaneID.
    
        Using the FindStockingPointInLaneTypeIndex method will create another StockingPointInLane, instead
        of detecing existing one ( and update its OriginLaneID/DestinationLaneID attribute )
    
        Hence, we will use select instead.
      */
      originlaneid := ifexpr( asorigin, this.ID(), '' );
      destinationlaneid := ifexpr( asorigin, '', this.ID() );
      
      createlaneleg := false;
        
    
      spil := ifexpr( asorigin,
                      select( this, Destination, spil, // Check whether existing SPIL exists in destination
                              spil.StockingPointID() = stockingpoint.ID()
                              and spil.DestinationLaneID() = this.ID() ),
                      select( this, Origin, spil, // Check whether existing SPIL exists in origin
                              spil.StockingPointID() = stockingpoint.ID()
                              and spil.OriginLaneID() = this.ID() ) );
      
      isopposite := true; 
      if( isnull( spil ) )
      {
        isopposite := false;
        spil := ifexpr( asorigin,
                      select( this, Origin, e, // Check whether existing SPIL exists in origin
                              e.StockingPointID() = stockingpoint.ID()
                              and e.OriginLaneID() = this.ID() ),
                      select( this, Destination, e, // Check whether existing SPIL exists in destination
                              e.StockingPointID() = stockingpoint.ID()
                              and e.DestinationLaneID() = this.ID() ) );
      }
      
      if( isnull( spil ) )
      {
        
        spil := StockingPointInLane::Create( stockingpoint, originlaneid, destinationlaneid );
        createlaneleg := true;
      }
      else if( isopposite )// to determine the origin and destination are from the same stockingpoint
      {
        if( asorigin )
        {
          originlaneid := spil.DestinationLaneID();
          destinationlaneid := spil.DestinationLaneID();
          createlaneleg := createlaneleg or not ( spil.AsOrigin() = this );     // lane leg has to be created if the stocking point is assigned as new origin
        }
        else
        {
          originlaneid := spil.OriginLaneID()
          destinationlaneid := spil.OriginLaneID();
          createlaneleg := createlaneleg or not ( spil.AsDestination() = this );  // lane leg has to be created if the stocking point is assigned as new destination
        }
    
        spil.UpdateTypeIndex( stockingpoint.ID(), originlaneid, destinationlaneid, false );
      }
    
      if( createlaneleg and ( not isfromdb ) )
      {
        spiltocreatelegs.Add( spil );
      }
    
      // Create product in stocking point based on product in lanes
      traverse( this, ProductInLane, pil )
      {
        pil.Product_MP().AddToStockingPoint( spil.StockingPoint_MP() );
      } 
    }
    // Propagate the origin relation, when there is existing spil and sp had been added as origin/destination
    Transaction::Transaction().Propagate( ifexpr( asorigin, relation( Lane, Origin ),
                                                            relation( Lane, Destination ) ) );
    
    // Create lane legs
    traverse( spiltocreatelegs, Elements, e )
    {
      this.CreateLaneLegs( e, asorigin );
    }
  *]
}