| 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 ); | 
|     } | 
|   *] | 
| } |