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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Quintiq file version 2.0
#parent: #root
Method MPSyncFulfillmentTarget (
  Boolean isoverwritemanualconfig,
  IOFulfillmentTargets iofulfillmenttargets
)
{
  Description: 'Synchronization of fulfillment target for MPSync'
  TextBody:
  [*
    // Select a set of existing FulfillmentTarget
    existing := selectset( this, FulfillmentTarget, item, true );
    updates := construct( FulfillmentTargets );
    
    // Traverse the new set of valid FulfillmentTarget
    traverse( iofulfillmenttargets , Elements, io, io.IsValid() )
    {
      id := io.ID();
      productid := io.ProductID();
      spid := io.StockingPointID();
    
      // Find existing FulfillmentTarget
      objectinstance:= FulfillmentTarget::FindFulfillmentTargetTypeIndex( id );
     
      // Find existing Product_MP
      product := Product_MP::FindProductTypeIndex( productid );
      // Find existing StockingPoint_MP
      stockingpoint := StockingPoint_MP::FindStockingPointTypeIndex( spid );
      // Find existing SalesSegment_MP
      salessegment := SalesSegment_MP::FindSalesSegmentTypeIndex( io.SalesSegmentName() );
      
      // If no existing FulfillmentTarget is found, and there is valid SalesSegment_MP, StockingPoint_MP, and Product_MP, create one
      if( not isnull( salessegment ) or not isnull( product ) or not isnull( stockingpoint ) )
      {
        if( isnull( objectinstance ) )
        {
            objectinstance := FulfillmentTarget::Create( this,
                                                         id,
                                                         io.IsEnabled(),
                                                         io.Name(),
                                                         product,
                                                         stockingpoint,
                                                         salessegment,
                                                         io.TargetPercentage(),
                                                         io.Start(),
                                                         io.End(),
                                                         io.IsUsedForSafetyStockCalculation(),
                                                         io.IsUsedForPlanningFulfillment(),
                                                         true );
        }
        // Else if the FulfillmentTarget is not manually configured or the imported instance should overwrite manual configuration,
        // update the existing FulfillmentTarget
        else if( not objectinstance.IsManuallyConfigured() or isoverwritemanualconfig )
        {
          // Update FulfillmentTarget
          objectinstance.Update( io.IsEnabled(),
                                  io.Name(),
                                  product,
                                  stockingpoint,
                                  salessegment,
                                  io.TargetPercentage(),
                                  io.Start(),
                                  io.End(),
                                  io.IsUsedForSafetyStockCalculation(),
                                  io.IsUsedForPlanningFulfillment(),
                                  true );
          updates.Add( objectinstance );
        }
        
        if( not isnull( objectinstance ) )
        {
          objectinstance.CustomUpdate( io, isoverwritemanualconfig );  
        }
      }  
    }
    
    // Get the set of old FulfillmentTarget to be deleted
    tobedeleteset := existing.Difference( updates );
    
    // Traverse the set of FulfillmentTarget to be deleted
    // Only delete FulfillmentTarget if the imported instance should overwrite manual configuration
    traverse( tobedeleteset, Elements, ele,
              not ele.IsManuallyConfigured() or isoverwritemanualconfig )
    {
      ele.Delete();
    }
  *]
}