yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Quintiq file version 2.0
#parent: #root
MethodOverride Copy (
  ScenarioNode scenarionode,
  String scenarioname,
  String newassumption,
  String comment,
  String storagestate,
  String kbdescription,
  String kbname,
  Key macroplanmdskey,
  String datasetname
) as Scenario
{
  Description: 'Make a copy of this scenario'
  TextBody:
  [*
    // Get current user's name
    currentusername := GlobalParameters_MP::GetUserName();
    
    // Get mds info of the selected dataset
    mdsEditor       := MDSEditor::Editor();
    mdsObjectInfo   := mdsEditor.LoadedObjectInfo( macroplanmdskey );
    
    //create a new scenario
    scenario := construct( ScenarioMP,
                           Name                    := scenarioname,
                           CreatedBy               := currentusername,
                           CreatedOn               := DateTime::Now(),
                           DatasetMDSID            := macroplanmdskey,
                           DatasetName             := datasetname,
                           Comment                 := comment,
                           IsLoaded                := true,
                           KBName                  := kbname,
                           State                   := storagestate,
                           KBDescription           := kbdescription
                         );
    
    // Copy all assumptions over from the current scenario to the new scenario
    traverse( this, Assumption, assumption )
    {
     assumption.Copy( scenario );
    }
    
    // Create new assumption for the new scenario
    scenario.CreateNewAssumption( newassumption );
    // Update last changed status of the new scenario
    scenario.UpdateLastChanged();
    // Call job to update MacroPlan dataset with the info from the new scenario
    MacroPlan::JobUpdateCompany( mdsObjectInfo.MDSID(), scenario );
    
    //Copy authorization
    groupnames := selectvalues( this, ScenarioAuthorization, sa, true, sa.Name() )
    scenario.Authorize( groupnames, true );
    
    // refresh KPI after copy
    /*Create MDSID from ScenarioManager, then pass the arguments
      to existing method to refresh newly copied scenario KPI*/
    smMDSID := MDSID::Create( this.ScenarioManager().MDSID() );
    ScenarioManager::JobUpdateKPIValue( smMDSID, false, mdsObjectInfo.MDSID() )
    
    // Add the new scenario into scenario manager
    return this.ScenarioManager().AddLeaf( scenarionode, &scenario ).astype( Scenario );
  *]
}