| Quintiq file version 2.0 | 
| #parent: #root | 
| Method GenerateSafetyStock | 
| { | 
|   Description: 'Generate a safety stock (inventory specification) for this PISPIP.' | 
|   TextBody: | 
|   [* | 
|     //calculate safety stock from attributes derived from PISP and PISPIP | 
|     standarddeviationdemand := 0.0; | 
|     sumofvariance := 0.0; | 
|      | 
|     // Calculate the sum of variance | 
|     traverse ( this, PlanningBaseSalesDemandInPeriod , sdip ) | 
|     { | 
|       sumofvariance := sumofvariance + sqr( sdip.Quantity() * ( sdip.MasterSalesDemand().DemandUncertaintyPercentage() / 100 ) ); | 
|     } | 
|     // Calculate standard deviation of demand | 
|     standarddeviationdemand := sqrt( sumofvariance ); | 
|      | 
|     pisp := this.ProductInStockingPoint_MP(); | 
|     // Get the value of safety stock | 
|      | 
|     safetystock := this.GetSafetyStockValue( pisp.LeadTime(), | 
|                                              pisp.StandardDeviationLeadTime(), | 
|                                              this.SalesDemandQuantity(), | 
|                                              standarddeviationdemand, | 
|                                              NormalDistribution::Construct().Quantile( this.GetServiceLevelPercentage() / 100 ) ); | 
|      | 
|     //checks if any existing SafetyStock exists | 
|     newis := SafetyStock::FindTypeIndexSafetyStock(  pisp.Product_MP().ID(), | 
|                                                      pisp.StockingPointID(), | 
|                                                      this.Start().Date() ); | 
|      | 
|     //if no existing safetystock is found | 
|     if( isnull( newis ) ) | 
|     { | 
|       //only create a new SafetyStock (safety stock) only when the safetystock value >0 | 
|       if(( safetystock >0 ) ) | 
|       { | 
|         newis := SafetyStock::Create(  pisp.Product_MP(), | 
|                                                   pisp.StockingPoint_MP(), | 
|                                                   this.Start().Date(), | 
|                                                   false, | 
|                                                   0.0, | 
|                                                   safetystock, | 
|                                                   true, | 
|                                                   false, | 
|                                                   false ); | 
|       }                                               | 
|     } | 
|     //if existing InventorySpecification is found, update its value instead | 
|     else if( not newis.HasUserTarget() ) | 
|     { | 
|       newis.Update( pisp.Product_MP(), | 
|                     pisp.StockingPoint_MP(), | 
|                     this.Start().Date(), | 
|                     false, | 
|                     0.0, | 
|                     safetystock, | 
|                     true, | 
|                     false, | 
|                     false ); | 
|     } | 
|   *] | 
| } |