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
Quintiq file version 2.0
#parent: #root
StaticMethod SynchronizeWithTemplate (
  MacroPlan owner,
  Accounts accounttemplates
)
{
  Description: 'Synchronize with accounts in scenario manager'
  TextBody:
  [*
    // Reset all account's IsInTemplate flag
    traverse( owner, Account_MP, a )
    {
      a.IsInTemplate( false );
    }
    
    // Traverse all passed in accounts and use them as templates to update/create accounts
    traverse( accounttemplates, Elements, template )
    {
      // Look for the account with the same name as template
      account := select( owner, Account_MP, acct, acct.Name() = template.Name() );
    
      // Create account if it is not found
      if( isnull( account ) )
      {
        account := Account_MP::Create( owner, template );
      }
      // Else, update the matching account
      else
      {
        account.Update( template.AccountTypeName(),
                        template.ParentName(),
                        template.ReportType(),
                        template.DefaultCost(),
                        template.DefaultCostDriver(),
                        template.DefaultLengthOfTime(),
                        template.DefaultTimeUnit(),
                        template.DisplayIndex(),
                        template.IsMaximize(),
                        template.IsUsedByOptimizer() );
      }
      // Set account's IsInTemplate flag to true
      account.IsInTemplate( true );
    }
    
    // Delete all accounts that is not is template
    traverse( owner, Account_MP, a, not a.IsInTemplate() )
    {
      a.Delete();
    }
    
    // Sort Account_MP according to it's display index
    owner.SortAccount( attribute( Account_MP, DisplayIndexForTemplate ), true );
  *]
}