| Quintiq file version 2.0 | 
| #parent: #root | 
| Method SyncPlannedOrdersFromMacroPlan ( | 
|   MacroPlan macroplan, | 
|   String mpsplanid | 
| ) | 
| { | 
|   Description: 'Create planned orders from period tasks from macroplan.' | 
|   TextBody: | 
|   [* | 
|     existing := this.IOMPSPlannedOrder( relget ); | 
|     updates := construct( IOMPSPlannedOrders ); | 
|      | 
|     // Get periods within the planning horizon | 
|     end := macroplan.LastPlanningPeriod().End(); | 
|     if( macroplan.GlobalParameters_MP().ExportHorizon().IsFinite() ) | 
|     { | 
|       end := macroplan.StartOfPlanning() + macroplan.GlobalParameters_MP().ExportHorizon(); | 
|     } | 
|      | 
|     periods := selectset( macroplan, PlanningPeriod, pp, pp.IsInPeriod( macroplan.StartOfPlanning(), end ) ); | 
|     routings := selectset( macroplan, Routing, r, r.IsEnabled() ); | 
|      | 
|     // Group by Period, RoutingID | 
|     traverse( periods, Elements, p ) | 
|     { | 
|       traverse( routings, Elements.LastStep, ls ) | 
|       { | 
|         qty := sum( ls, Operation.PeriodTaskOperation, pto, pto.Quantity() > 0.0 and pto.UnitPeriod().Period_MP() = p, pto.Quantity() ); | 
|          | 
|         if( qty > 0.0 ) | 
|         { | 
|           ioplannedorder := IOMPSPlannedOrder::Create( this, ls.RoutingID(), p.End(), mpsplanid, qty, p.Start(), p.End() ); | 
|          | 
|           updates.Add( ioplannedorder ); | 
|         } | 
|       } | 
|     } | 
|      | 
|     // Get the set of old PlannedOrder to be deleted | 
|     tobedeleteset := existing.Difference( updates ); | 
|      | 
|     traverse( tobedeleteset, Elements, ele ) | 
|     { | 
|       ele.Delete(); | 
|     } | 
|   *] | 
|   InterfaceProperties { Accessibility: 'Module' } | 
| } |