yypsybs
2023-09-09 3cb5a54def670d97301f07170fcaad213bfc54f2
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
Quintiq file version 2.0
#parent: #root
Method MPSyncFulfillmentRestriction (
  Boolean isoverwritemanualconfig,
  IOFulfillmentRestrictions iofulfillmentrestrictions
)
{
  Description: 'Synchronization of FulfillmentRestriction for MPSync'
  TextBody:
  [*
    // Select a set of existing FulfillmentRestriction
    existing := selectset( this, SalesSegment_MP.FulfillmentRestriction, item, true );
    updates := construct( FulfillmentRestrictions );
    
    // Traverse the new set of valid FulfillmentRestriction
    traverse( iofulfillmentrestrictions, Elements, io, io.IsValid() )
    {  
      // Find existing FulfillmentRestriction
      objectinstance := FulfillmentRestriction::FindFulfillmentRestrictionTypeIndex( io.SalesSegmentName(), io.ProductID(), io.StartDate() );
      // Find existing SalesSegment_MP
      ss := SalesSegment_MP::FindSalesSegmentTypeIndex( io.SalesSegmentName() );
      // Find existing Product_MP
      product := Product_MP::FindProductTypeIndex( io.ProductID() );
                                                            
      // If no existing FulfillmentRestriction is found, and there is valid SalesSegment_MP and Product_MP, create one
      if( isnull( objectinstance ) )
      { 
        if( not( isnull( ss ) or isnull( product ) ) )
        {
          objectinstance := FulfillmentRestriction::Create( ss, 
                                                            io.ProductID(), 
                                                            io.StartDate(),
                                                            io.EndDate() );
        }
      }
      
      // If the FulfillmentRestriction is found or created, update the existing FulfillmentRestriction
      if( not isnull( objectinstance ) )
      {
        // Update FulfillmentRestriction
        objectinstance.Update( io.SalesSegmentName(), 
                               io.ProductID(), 
                               io.StartDate(), 
                               io.EndDate() );
        objectinstance.CustomUpdate( io, isoverwritemanualconfig )                       
        updates.Add( objectinstance );
      }
    }
    
    // Get the set of old FulfillmentRestriction to be deleted
    tobedeleteset := existing.Difference( updates );
    
    // Traverse the set of FulfillmentRestriction to be deleted
    // Only delete FulfillmentRestriction if the imported instance should overwrite manual configuration
    traverse( tobedeleteset, Elements, ele, isoverwritemanualconfig )
    {
      ele.Delete();
    }
  *]
}