| Quintiq file version 2.0 | 
| #parent: #root | 
| Method MPSyncUnitAvailability ( | 
|   Boolean isoverwritemanualconfig, | 
|   IOUnitAvailabilitys iounitavailabilitys | 
| ) | 
| { | 
|   Description: 'Synchronization of Unit Availability for MPSync' | 
|   TextBody: | 
|   [* | 
|     // Select a set of existing UnitAvailability | 
|     existing := selectset( this, Unit.UnitAvailability, item, true ); | 
|     updates := construct( UnitAvailabilitys ); | 
|      | 
|     // Traverse the new set of valid UnitAvailability | 
|     traverse( iounitavailabilitys , Elements, io, io.IsValid() ) | 
|     { | 
|       // Find existing UnitAvailability | 
|       objectinstance := UnitAvailability::FindUnitAvailabilityTypeIndex( io.UnitID(), io.Start().DateTime() ); | 
|       // Find existing Unit | 
|       unit := this.FindUnit( io.UnitID() ); | 
|       // Find existing ShiftPattern | 
|       shiftpattern := ShiftPattern::FindShiftPatternTypeIndex( io.ShiftPatternName() ); | 
|        | 
|       // Check if there's valid Unit | 
|       if( not ( isnull( unit ) ) ) | 
|       { | 
|         // If no existing UnitAvailability is found, create one | 
|         if( isnull( objectinstance ) ) | 
|         {     | 
|           objectinstance := UnitAvailability::Create( unit, | 
|                                                       io.Start().DateTime(), | 
|                                                       io.TimeUnit(), | 
|                                                       io.Maintenance(), | 
|                                                       io.Efficiency(), | 
|                                                       io.Allocation(), | 
|                                                       shiftpattern, | 
|                                                       io.MaximumLoadPercentage(), | 
|                                                       io.NrOfUnitsOpen(), | 
|                                                       io.MinimumLoadThreshold(), | 
|                                                       true ); | 
|         } | 
|         // Else if the UnitAvailability is not manually configured or the imported instance should overwrite manual configuration, | 
|         // update the existing UnitAvailability | 
|         else if( not objectinstance.IsManuallyConfigured() or isoverwritemanualconfig ) | 
|         { | 
|           // Update UnitAvailability | 
|           objectinstance.Update( unit,  | 
|                                  io.Start().DateTime(),  | 
|                                  io.TimeUnit(), | 
|                                  io.Maintenance(), | 
|                                  io.Efficiency(),  | 
|                                  io.Allocation(), | 
|                                  shiftpattern, | 
|                                  io.MaximumLoadPercentage(),  | 
|                                  io.NrOfUnitsOpen(),  | 
|                                  io.MinimumLoadThreshold(),true  ); | 
|                                   | 
|           updates.Add( objectinstance ); | 
|         } | 
|          | 
|         if( not isnull( objectinstance ) ) | 
|         { | 
|           objectinstance.CustomUpdate( io, isoverwritemanualconfig ); | 
|         } | 
|       } | 
|     } | 
|      | 
|     // Get the set of old UnitAvailability to be deleted | 
|     tobedeleteset := existing.Difference( updates ); | 
|      | 
|     // Traverse the set of UnitAvailability to be deleted | 
|     // Only delete UnitAvailability that is not manually configured or the imported instance should overwrite manual configuration | 
|     traverse( tobedeleteset, Elements, ele, | 
|               not ele.IsManuallyConfigured() or isoverwritemanualconfig ) | 
|     { | 
|       ele.Delete(); | 
|     } | 
|   *] | 
| } |