Quintiq file version 2.0
#parent: #root
Method OnOk () id:Method_DialogCreateEditScenario_OnOk #extension
{
  Body:
  [*
    // Disabled button to prevent the possibility of multiple firing
    
    //macroPlanOld := MacroPlan;
    
    btnOK.Enabled( false, '' )
    
    // On ok execution, create new scenario, copy or edit
    this.ApplyChanges();
    
    data := DataHolderDialogData.Data();
    newscenario := null( Scenario );
    strategy := null( Strategy );
    
    // New version
    globalotdtable := GlobalOTDTable;
    globalotdsop := GlobalOTDSOP;
    testbusinesstype := "集团";
    if( data.IsCreateNewVersion() ){
      VersionControl::CreateNewSceneVersion( globalotdsop, testbusinesstype );
      }else{
        VersionControl::CopyVersion( globalotdsop, testbusinesstype );
        }
    versionname := VersionControl::GetVersionInfo( globalotdsop, testbusinesstype ).Element( 3 );
    
    // Get Version Name
    if( data.Name() = "" ){
      data.Name( versionname );
      }
    
    // Get Business Type from Scenario Name
    businesstype := guard( select( globalotdtable, 
                                   BusinessType, 
                                   b, 
                                   b.ScenarioName()=data.ScenarioName() ).BusinessTypeName(), 
                           "" );
    data.BusinessType( businesstype );
    
    // Copy scenario
    if( isnull( data.WrappedInstance() ) )
    {
      if(  DataHolderActionCopy.Data() )
      { 
        scenario := data.Parent().astype( ScenarioMP );
        strategy := data.SelectedStrategy();
             
        newscenario:= scenario.Copy( data.Parent(), data.Name(), edtAssumption.Text(),
                                     data.Comment(), data.State() );
        scenario.EnableSync( data.EnableSync() );
        scenario.BusinessType( data.BusinessType() );
        scenario.IsKeyProduct( data.IsKeyProduct() );
        scenario.CreatePurchaseSupplyMaterial( data.CreatePurchaseSupplyMaterial() );
      }
      // If DatasetMDSID is set, it means the scenario
      // will be linked to an existing dataset
      else if( data.DatasetMDSID() <> Key::ZeroKey() )
      {
        sc := ScenarioManager.CreateScenarioMPFromDataset( data.Parent(),
                                                     data.Name(),
                                                     edtAssumption.Text(),
                                                     data.Comment(),
                                                     '',
                                                     data.DatasetName(),
                                                     data.DatasetMDSID(),
                                                     data.State() );
        sc.EnableSync( data.EnableSync() );
        sc.BusinessType( data.BusinessType() );
        sc.IsKeyProduct( data.IsKeyProduct() );
        sc.CreatePurchaseSupplyMaterial( data.CreatePurchaseSupplyMaterial() );
      }
      else
      {
        newscenario := ScenarioManager.CreateScenarioMP( data.Parent(),
                                                         data.Name(),
                                                         edtAssumption.Text(),
                                                         data.Comment(),
                                                         data.State(),
                                                         '',
                                                         ApplicationMacroPlanner.GetUserName() );
        newscenario.EnableSync( data.EnableSync() );
        newscenario.BusinessType( data.BusinessType() );
        newscenario.IsKeyProduct( data.IsKeyProduct() );
        newscenario.CreatePurchaseSupplyMaterial( data.CreatePurchaseSupplyMaterial() );
        strategy := data.SelectedStrategy();
      }
    }
    // Edit scenario
    else
    {
      data.WrappedInstance().Update( data.Name(), data.Comment(), data.State() );
      data.WrappedInstance().SelectedStrategy( relset, data.SelectedStrategy() );
      data.WrappedInstance().EnableSync( data.EnableSync() );
      data.WrappedInstance().BusinessType( data.BusinessType() );
      data.WrappedInstance().IsKeyProduct( data.IsKeyProduct() );
      data.WrappedInstance().CreatePurchaseSupplyMaterial( data.CreatePurchaseSupplyMaterial() );
    }
    
    // Post scenario creation
    // Set kb and strategy after creation of scenario and set the scenario as active
    if( not isnull( newscenario ) )
    {           
      newscenario.SelectedStrategy( relset, strategy ); 
      if ( not DataHolderActionCopy.Data() )
      {                                                                                                    
        ApplicationMacroPlanner.SelectScenario( newscenario.astype( ScenarioMP ) );
      }  
    }
    
    macroPlanNew := MacroPlan;
    //ManufactureLTImputation::Transfer( macroPlanOld, macroPlanNew );
    
    // sync data
    if( data.EnableSync() ) {
        MacroPlan::DoSync( macroPlanNew, 
                           data.BusinessType(), data.IsKeyProduct(), data.CreatePurchaseSupplyMaterial() ,GlobalOTDTable);
    }
    macroPlanNew.BusinessType( businesstype );
    macroPlanNew.IsKeyProduct( data.IsKeyProduct() );
    macroPlanNew.CreatePurchaseSupplyMaterial( data.CreatePurchaseSupplyMaterial() );
    
    this.Close();
  *]
}
