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