| Quintiq file version 2.0 | 
| #parent: #root | 
| Method CreateSafetyStocks | 
| { | 
|   Description: 'Create safety stocks for the product after the procedure is done for all periods' | 
|   TextBody: | 
|   [* | 
|     // Traverse through all PISPs and PISPIPs, if MEIO created a target delete all old inventory specifications | 
|     traverse( this, Product_MP.ProductInStockingPoint_MP, pisp )  | 
|     { | 
|       if ( not pisp.IsActiveInMEIO() )  // not active == no safety stock kept and no sales demand | 
|       { | 
|         traverse( pisp, SafetyStock, ss, not ss.HasUserTarget() )  | 
|         { | 
|           ss.Delete(); | 
|         } | 
|       } | 
|        | 
|       traverse( pisp, ProductInStockingPointInPeriodPlanning.astype( ProductInStockingPointInPeriodPlanningLeaf ), pispip, pispip.MEIO_PISPIP_Target( relsize ) > 0 ) | 
|       { | 
|         oldss := pispip.SafetyStock() // remove old inv spec if it only ranges over MEIO pispips  | 
|         if ( not isnull( oldss ) and not oldss.HasUserTarget())   | 
|         { | 
|           if ( forall( oldss, ProductInStockingPointInPeriod.astype( ProductInStockingPointInPeriodPlanningLeaf ), p, true, p.MEIO_PISPIP_Target( relsize ) > 0 ) )  | 
|           { | 
|             oldss.Delete(); | 
|           } | 
|         } | 
|       } | 
|     } | 
|      | 
|     // create new inventory specifications | 
|     traverse( this, Product_MP.ProductInStockingPoint_MP, pisp )  | 
|     { | 
|       traverse( pisp, ProductInStockingPointInPeriodPlanning.astype( ProductInStockingPointInPeriodPlanningLeaf ), pispip )  | 
|       { | 
|         target := maxselect(  pispip, MEIO_PISPIP_Target, t, true, t.Target() ); | 
|         if( not isnull( target ) ) | 
|         { | 
|          | 
|           // Create safety stock for the given PISPIP | 
|           target.IsSelected( true ); | 
|           pispip.ServiceLevel( target.ServiceLevel() ); | 
|           ss:=SafetyStock::FindTypeIndexSafetyStock( pisp.ProductID(), pisp.StockingPointID(), pispip.Start().Date() ); | 
|           if ( isnull( ss ) ) | 
|           { | 
|             SafetyStock::Create( pisp.Product_MP(), | 
|                                pisp.StockingPoint_MP(), | 
|                                pispip.Start().Date(), | 
|                                false, 0.0, target.Target(),            // Does not have a target in days, the initial targets are 0 | 
|                                true, false ,false );            // Is calculated and is not from database  | 
|           } | 
|         } | 
|       } | 
|     } | 
|   *] | 
| } |