Quintiq file version 2.0
#parent: #root
Method DeleteScenario (
  structured[ScenarioNode] scenarionodes
) id:Method_FormScenarioManager_DeleteScenario
{
  #keys: '[134266.1.2130318926]'
  Body:
  [*
    // Delete selected scenarios
    trash := select ( ScenarioManager, ScenarioNode.astype( ScenarioFolder ), s, s.IsRecycleBin(), true );
    if ( not isnull( trash ) )
    { 
      if( exists( scenarionodes, Elements, e, not e.IsDeleted() ) )
      {
        ScenarioNode::Delete( scenarionodes, ScenarioManager )
        ListScenario.SelectByKey( trash.Key() );
    
        if( not isnull( MacroPlan ) // Dont need to delected scenario if there is no active scenario
            and exists( scenarionodes, Elements.astype( ScenarioMP ), smp,
                        smp.DatasetMDSID() = MacroPlan.MDSID() ) ) // Do not deselect the active scenario if it is not deleted.
        {
          DataHolderActiveScenario.Data( null( Scenario ) );
          ApplicationMacroPlanner.DeselectScenario();
          
          // Get active scenario
          activescenarios := selectset( ScenarioManager, ScenarioNode, node, not node.IsDeleted() );
          activescenarios := activescenarios.Difference( scenarionodes );
          // Get the remaining active ScenarioMP
          activescenario := maxselect( activescenarios, Elements.astype( ScenarioMP ), e,
                                       true, // e.IsVisible( DataHolderUserGroup.Data(), false, ApplicationMacroPlanner.GetUserName() ),
                                       e.CreatedOn() );
          // Select the scenario if found any
          if( not isnull( activescenario ) )
          {
            ApplicationMacroPlanner.SelectScenario( activescenario );
          }
        }
      }
      else
      {
        ScenarioNode::Delete( scenarionodes, ScenarioManager );
        ListScenario.SelectByKey( trash.Key() );
      }
    }
  *]
}
