| Quintiq file version 2.0 | 
| #parent: #root | 
| Method MPSyncUnitCost ( | 
|   Boolean isoverwritemanualconfig, | 
|   IOUnitCosts iounitcosts | 
| ) | 
| { | 
|   Description: 'Synchronization of UnitCost for MPSync' | 
|   TextBody: | 
|   [* | 
|     // Select a set of existing UnitCost | 
|     existing := selectset( this, Unit.UnitCost, item, true ); | 
|     updates := construct( UnitCosts ); | 
|      | 
|     // Traverse the new set of valid UnitCost | 
|     traverse( iounitcosts , Elements, io, io.IsValid() ) | 
|     { | 
|       indexkey := io.ID(); | 
|       // Find existing UnitCost | 
|       objectinstance := UnitCost::FindUnitCostTypeIndex( indexkey ); | 
|       // Find existing Account_MP | 
|       account := Account_MP::FindAccountTypeIndex( io.AccountName() ); | 
|       // Find existing Unit | 
|       unit := Unit::FindUnitTypeIndex( io.UnitID() ); | 
|        | 
|       // If no existing UnitCost is found, create one | 
|       if( isnull( objectinstance ) ) | 
|       { | 
|         // Check if there's valid Account_MP and Unit | 
|         if( not ( isnull( account ) or isnull( unit ) ) ) | 
|         {   | 
|           objectinstance := UnitCost::Create( indexkey, | 
|                                               unit, | 
|                                               account, | 
|                                               io.CostDriver(), | 
|                                               io.Start(), | 
|                                               io.TimeUnit(), | 
|                                               io.LengthOfTime(), | 
|                                               io.Cost(), | 
|                                               true ).astype( UnitCost ); | 
|         } | 
|       } | 
|       // Else if the UnitCost is not manually configured or the imported instance should overwrite manual configuration, | 
|       // update the existing UnitCost | 
|       else if( not objectinstance.IsManuallyConfigured() or isoverwritemanualconfig ) | 
|       { | 
|         // Update UnitCost | 
|         if( not ( isnull( account ) or isnull( unit ) ) ) | 
|         { | 
|           objectinstance.Update( account, io.CostDriver(), io.Start(), io.TimeUnit(), io.LengthOfTime(), io.Cost(), true ); | 
|           updates.Add( objectinstance ); | 
|         } | 
|       } | 
|        | 
|       if( not isnull( objectinstance ) ) | 
|       { | 
|         // Extended method | 
|         objectinstance.CustomUpdate( io, isoverwritemanualconfig );  | 
|       } | 
|     } | 
|      | 
|     // Get the set of old UnitCost to be deleted | 
|     tobedeleteset := existing.Difference( updates ); | 
|      | 
|     // Traverse the set of UnitCost to be deleted | 
|     // Only delete UnitCost that is not manually configured or the imported instance should overwrite manual configuration | 
|     traverse( tobedeleteset, Elements, ele, | 
|               not ele.IsManuallyConfigured() or isoverwritemanualconfig ) | 
|     { | 
|       ele.Delete(); | 
|     } | 
|   *] | 
| } |