yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
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
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 );
    }
  *]
}